xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision ace1a5f11236a072fca1b5e0ea1416a083a9f2aa)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1990 Mentat Inc. */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * This file contains the interface control functions for IP.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/stream.h>
36 #include <sys/dlpi.h>
37 #include <sys/stropts.h>
38 #include <sys/strsun.h>
39 #include <sys/sysmacros.h>
40 #include <sys/strlog.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/cmn_err.h>
44 #include <sys/kstat.h>
45 #include <sys/debug.h>
46 #include <sys/zone.h>
47 
48 #include <sys/kmem.h>
49 #include <sys/systm.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #define	_SUN_TPI_VERSION	2
53 #include <sys/tihdr.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/tun.h>
84 #include <inet/sctp_ip.h>
85 
86 #include <net/pfkeyv2.h>
87 #include <inet/ipsec_info.h>
88 #include <inet/sadb.h>
89 #include <inet/ipsec_impl.h>
90 #include <sys/iphada.h>
91 
92 
93 #include <netinet/igmp.h>
94 #include <inet/ip_listutils.h>
95 #include <netinet/ip_mroute.h>
96 #include <inet/ipclassifier.h>
97 #include <sys/mac.h>
98 
99 #include <sys/systeminfo.h>
100 #include <sys/bootconf.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_poll_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_poll_reset(ill_t *, mblk_t **);
240 
241 static void	illgrp_cache_delete(ire_t *, char *);
242 static void	illgrp_delete(ill_t *ill);
243 static void	illgrp_reset_schednext(ill_t *ill);
244 
245 static ill_t	*ill_prev_usesrc(ill_t *);
246 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
247 static void	ill_disband_usesrc_group(ill_t *);
248 
249 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
250 
251 /*
252  * if we go over the memory footprint limit more than once in this msec
253  * interval, we'll start pruning aggressively.
254  */
255 int ip_min_frag_prune_time = 0;
256 
257 /*
258  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
259  * and the IPsec DOI
260  */
261 #define	MAX_IPSEC_ALGS	256
262 
263 #define	BITSPERBYTE	8
264 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
265 
266 #define	IPSEC_ALG_ENABLE(algs, algid) \
267 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
268 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
269 
270 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
271 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
272 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
273 
274 typedef uint8_t ipsec_capab_elem_t;
275 
276 /*
277  * Per-algorithm parameters.  Note that at present, only encryption
278  * algorithms have variable keysize (IKE does not provide a way to negotiate
279  * auth algorithm keysize).
280  *
281  * All sizes here are in bits.
282  */
283 typedef struct
284 {
285 	uint16_t	minkeylen;
286 	uint16_t	maxkeylen;
287 } ipsec_capab_algparm_t;
288 
289 /*
290  * Per-ill capabilities.
291  */
292 struct ill_ipsec_capab_s {
293 	ipsec_capab_elem_t *encr_hw_algs;
294 	ipsec_capab_elem_t *auth_hw_algs;
295 	uint32_t algs_size;	/* size of _hw_algs in bytes */
296 	/* algorithm key lengths */
297 	ipsec_capab_algparm_t *encr_algparm;
298 	uint32_t encr_algparm_size;
299 	uint32_t encr_algparm_end;
300 };
301 
302 /*
303  * List of AH and ESP IPsec acceleration capable ills
304  */
305 typedef struct ipsec_capab_ill_s {
306 	uint_t ill_index;
307 	boolean_t ill_isv6;
308 	struct ipsec_capab_ill_s *next;
309 } ipsec_capab_ill_t;
310 
311 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
312 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
313 krwlock_t ipsec_capab_ills_lock;
314 
315 /*
316  * The field values are larger than strictly necessary for simple
317  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
318  */
319 static area_t	ip_area_template = {
320 	AR_ENTRY_ADD,			/* area_cmd */
321 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
322 					/* area_name_offset */
323 	/* area_name_length temporarily holds this structure length */
324 	sizeof (area_t),			/* area_name_length */
325 	IP_ARP_PROTO_TYPE,		/* area_proto */
326 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
327 	IP_ADDR_LEN,			/* area_proto_addr_length */
328 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
329 					/* area_proto_mask_offset */
330 	0,				/* area_flags */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
332 					/* area_hw_addr_offset */
333 	/* Zero length hw_addr_length means 'use your idea of the address' */
334 	0				/* area_hw_addr_length */
335 };
336 
337 /*
338  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
339  * support
340  */
341 static area_t	ip6_area_template = {
342 	AR_ENTRY_ADD,			/* area_cmd */
343 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
344 					/* area_name_offset */
345 	/* area_name_length temporarily holds this structure length */
346 	sizeof (area_t),			/* area_name_length */
347 	IP_ARP_PROTO_TYPE,		/* area_proto */
348 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
349 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
350 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
351 					/* area_proto_mask_offset */
352 	0,				/* area_flags */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
354 					/* area_hw_addr_offset */
355 	/* Zero length hw_addr_length means 'use your idea of the address' */
356 	0				/* area_hw_addr_length */
357 };
358 
359 static ared_t	ip_ared_template = {
360 	AR_ENTRY_DELETE,
361 	sizeof (ared_t) + IP_ADDR_LEN,
362 	sizeof (ared_t),
363 	IP_ARP_PROTO_TYPE,
364 	sizeof (ared_t),
365 	IP_ADDR_LEN
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN
375 };
376 
377 /*
378  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
379  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
380  * areq is used).
381  */
382 static areq_t	ip_areq_template = {
383 	AR_ENTRY_QUERY,			/* cmd */
384 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
385 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
386 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
387 	sizeof (areq_t),			/* target addr offset */
388 	IP_ADDR_LEN,			/* target addr_length */
389 	0,				/* flags */
390 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
391 	IP_ADDR_LEN,			/* sender addr length */
392 	6,				/* xmit_count */
393 	1000,				/* (re)xmit_interval in milliseconds */
394 	4				/* max # of requests to buffer */
395 	/* anything else filled in by the code */
396 };
397 
398 static arc_t	ip_aru_template = {
399 	AR_INTERFACE_UP,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_ard_template = {
405 	AR_INTERFACE_DOWN,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aron_template = {
411 	AR_INTERFACE_ON,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 static arc_t	ip_aroff_template = {
417 	AR_INTERFACE_OFF,
418 	sizeof (arc_t),		/* Name offset */
419 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
420 };
421 
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_MIPRUNNING,	"MIP" },
486 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
487 	{ PHYI_FAILED,		"FAILED" },
488 	{ PHYI_STANDBY,		"STANDBY" },
489 	{ PHYI_INACTIVE,	"INACTIVE" },
490 	{ PHYI_OFFLINE,		"OFFLINE" },
491 };
492 
493 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
494 
495 static ip_m_t	ip_m_tbl[] = {
496 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_ether_v6intfid },
498 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_nodef_v6intfid },
500 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_nodef_v6intfid },
502 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid },
504 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_ether_v6intfid },
506 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
507 	    ip_ib_v6intfid },
508 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
509 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
510 	    ip_nodef_v6intfid }
511 };
512 
513 static ill_t	ill_null;		/* Empty ILL for init. */
514 char	ipif_loopback_name[] = "lo0";
515 static char *ipv4_forward_suffix = ":ip_forwarding";
516 static char *ipv6_forward_suffix = ":ip6_forwarding";
517 static kstat_t *loopback_ksp = NULL;
518 static	sin6_t	sin6_null;	/* Zero address for quick clears */
519 static	sin_t	sin_null;	/* Zero address for quick clears */
520 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
521 /* When set search for unused index */
522 static boolean_t ill_index_wrap = B_FALSE;
523 /* When set search for unused ipif_seqid */
524 static ipif_t	ipif_zero;
525 uint_t	ipif_src_random;
526 
527 /*
528  * For details on the protection offered by these locks please refer
529  * to the notes under the Synchronization section at the start of ip.c
530  */
531 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
532 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
533 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
534 
535 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
536 
537 /*
538  * illgrp_head/ifgrp_head is protected by IP's perimeter.
539  */
540 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
541 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
542 
543 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
544 
545 /*
546  * ppa arena is created after these many
547  * interfaces have been plumbed.
548  */
549 uint_t	ill_no_arena = 12;
550 
551 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
552 static phyint_list_t phyint_g_list;	/* start of phyint list */
553 
554 static uint_t
555 ipif_rand(void)
556 {
557 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
558 	return ((ipif_src_random >> 16) & 0x7fff);
559 }
560 
561 /*
562  * Allocate per-interface mibs. Only used for ipv6.
563  * Returns true if ok. False otherwise.
564  *  ipsq  may not yet be allocated (loopback case ).
565  */
566 static boolean_t
567 ill_allocate_mibs(ill_t *ill)
568 {
569 	ASSERT(ill->ill_isv6);
570 
571 	/* Already allocated? */
572 	if (ill->ill_ip6_mib != NULL) {
573 		ASSERT(ill->ill_icmp6_mib != NULL);
574 		return (B_TRUE);
575 	}
576 
577 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
578 	    KM_NOSLEEP);
579 	if (ill->ill_ip6_mib == NULL) {
580 		return (B_FALSE);
581 	}
582 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
583 	    KM_NOSLEEP);
584 	if (ill->ill_icmp6_mib == NULL) {
585 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
586 		ill->ill_ip6_mib = NULL;
587 		return (B_FALSE);
588 	}
589 	/*
590 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
591 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
592 	 * -> ill_phyint_reinit
593 	 */
594 	return (B_TRUE);
595 }
596 
597 /*
598  * Common code for preparation of ARP commands.  Two points to remember:
599  * 	1) The ill_name is tacked on at the end of the allocated space so
600  *	   the templates name_offset field must contain the total space
601  *	   to allocate less the name length.
602  *
603  *	2) The templates name_length field should contain the *template*
604  *	   length.  We use it as a parameter to bcopy() and then write
605  *	   the real ill_name_length into the name_length field of the copy.
606  * (Always called as writer.)
607  */
608 mblk_t *
609 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
610 {
611 	arc_t	*arc = (arc_t *)template;
612 	char	*cp;
613 	int	len;
614 	mblk_t	*mp;
615 	uint_t	name_length = ill->ill_name_length;
616 	uint_t	template_len = arc->arc_name_length;
617 
618 	len = arc->arc_name_offset + name_length;
619 	mp = allocb(len, BPRI_HI);
620 	if (mp == NULL)
621 		return (NULL);
622 	cp = (char *)mp->b_rptr;
623 	mp->b_wptr = (uchar_t *)&cp[len];
624 	if (template_len)
625 		bcopy(template, cp, template_len);
626 	if (len > template_len)
627 		bzero(&cp[template_len], len - template_len);
628 	mp->b_datap->db_type = M_PROTO;
629 
630 	arc = (arc_t *)cp;
631 	arc->arc_name_length = name_length;
632 	cp = (char *)arc + arc->arc_name_offset;
633 	bcopy(ill->ill_name, cp, name_length);
634 
635 	if (addr) {
636 		area_t	*area = (area_t *)mp->b_rptr;
637 
638 		cp = (char *)area + area->area_proto_addr_offset;
639 		bcopy(addr, cp, area->area_proto_addr_length);
640 		if (area->area_cmd == AR_ENTRY_ADD) {
641 			cp = (char *)area;
642 			len = area->area_proto_addr_length;
643 			if (area->area_proto_mask_offset)
644 				cp += area->area_proto_mask_offset;
645 			else
646 				cp += area->area_proto_addr_offset + len;
647 			while (len-- > 0)
648 				*cp++ = (char)~0;
649 		}
650 	}
651 	return (mp);
652 }
653 
654 /*
655  * Completely vaporize a lower level tap and all associated interfaces.
656  * ill_delete is called only out of ip_close when the device control
657  * stream is being closed.
658  */
659 void
660 ill_delete(ill_t *ill)
661 {
662 	ipif_t	*ipif;
663 	ill_t	*prev_ill;
664 
665 	/*
666 	 * ill_delete may be forcibly entering the ipsq. The previous
667 	 * ioctl may not have completed and may need to be aborted.
668 	 * ipsq_flush takes care of it. If we don't need to enter the
669 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
670 	 * ill_delete_tail is sufficient.
671 	 */
672 	ipsq_flush(ill);
673 
674 	/*
675 	 * Nuke all interfaces.  ipif_free will take down the interface,
676 	 * remove it from the list, and free the data structure.
677 	 * Walk down the ipif list and remove the logical interfaces
678 	 * first before removing the main ipif. We can't unplumb
679 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
680 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
681 	 * POINTOPOINT.
682 	 *
683 	 * If ill_ipif was not properly initialized (i.e low on memory),
684 	 * then no interfaces to clean up. In this case just clean up the
685 	 * ill.
686 	 */
687 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
688 		ipif_free(ipif);
689 
690 	/*
691 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
692 	 * So nobody can be using this mp now. Free the mp allocated for
693 	 * honoring ILLF_NOARP
694 	 */
695 	freemsg(ill->ill_arp_on_mp);
696 	ill->ill_arp_on_mp = NULL;
697 
698 	/* Clean up msgs on pending upcalls for mrouted */
699 	reset_mrt_ill(ill);
700 
701 	/*
702 	 * ipif_free -> reset_conn_ipif will remove all multicast
703 	 * references for IPv4. For IPv6, we need to do it here as
704 	 * it points only at ills.
705 	 */
706 	reset_conn_ill(ill);
707 
708 	/*
709 	 * ill_down will arrange to blow off any IRE's dependent on this
710 	 * ILL, and shut down fragmentation reassembly.
711 	 */
712 	ill_down(ill);
713 
714 	/* Let SCTP know, so that it can remove this from its list. */
715 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
716 
717 	/*
718 	 * If an address on this ILL is being used as a source address then
719 	 * clear out the pointers in other ILLs that point to this ILL.
720 	 */
721 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
722 	if (ill->ill_usesrc_grp_next != NULL) {
723 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
724 			ill_disband_usesrc_group(ill);
725 		} else {	/* consumer of the usesrc ILL */
726 			prev_ill = ill_prev_usesrc(ill);
727 			prev_ill->ill_usesrc_grp_next =
728 			    ill->ill_usesrc_grp_next;
729 		}
730 	}
731 	rw_exit(&ill_g_usesrc_lock);
732 }
733 
734 /*
735  * ill_delete_tail is called from ip_modclose after all references
736  * to the closing ill are gone. The wait is done in ip_modclose
737  */
738 void
739 ill_delete_tail(ill_t *ill)
740 {
741 	mblk_t	**mpp;
742 	ipif_t	*ipif;
743 
744 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
745 		ipif_down_tail(ipif);
746 
747 	/*
748 	 * Send the detach if there's one to send (i.e., if we're above a
749 	 * style 2 DLPI driver).
750 	 */
751 	if (ill->ill_detach_mp != NULL) {
752 		ill_dlpi_send(ill, ill->ill_detach_mp);
753 		ill->ill_detach_mp = NULL;
754 	}
755 
756 	/*
757 	 * If polling capability is enabled (which signifies direct
758 	 * upcall into IP and driver has ill saved as a handle),
759 	 * we need to make sure that unbind has completed before we
760 	 * let the ill disappear and driver no longer has any reference
761 	 * to this ill.
762 	 */
763 	mutex_enter(&ill->ill_lock);
764 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
765 		while (!(ill->ill_state_flags & ILL_DL_UNBIND_DONE))
766 			cv_wait(&ill->ill_cv, &ill->ill_lock);
767 	}
768 	mutex_exit(&ill->ill_lock);
769 
770 	if (ill->ill_net_type != IRE_LOOPBACK)
771 		qprocsoff(ill->ill_rq);
772 
773 	/*
774 	 * We do an ipsq_flush once again now. New messages could have
775 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
776 	 * could also have landed up if an ioctl thread had looked up
777 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
778 	 * enqueued the ioctl when we did the ipsq_flush last time.
779 	 */
780 	ipsq_flush(ill);
781 
782 	/*
783 	 * Free capabilities.
784 	 */
785 	if (ill->ill_ipsec_capab_ah != NULL) {
786 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
787 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
788 		ill->ill_ipsec_capab_ah = NULL;
789 	}
790 
791 	if (ill->ill_ipsec_capab_esp != NULL) {
792 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
793 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
794 		ill->ill_ipsec_capab_esp = NULL;
795 	}
796 
797 	if (ill->ill_mdt_capab != NULL) {
798 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
799 		ill->ill_mdt_capab = NULL;
800 	}
801 
802 	if (ill->ill_hcksum_capab != NULL) {
803 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
804 		ill->ill_hcksum_capab = NULL;
805 	}
806 
807 	if (ill->ill_zerocopy_capab != NULL) {
808 		kmem_free(ill->ill_zerocopy_capab,
809 		    sizeof (ill_zerocopy_capab_t));
810 		ill->ill_zerocopy_capab = NULL;
811 	}
812 
813 	/*
814 	 * Clean up polling capabilities
815 	 */
816 	if (ill->ill_capabilities & ILL_CAPAB_POLL)
817 		ipsq_clean_all(ill);
818 
819 	if (ill->ill_poll_capab != NULL) {
820 		CONN_DEC_REF(ill->ill_poll_capab->ill_unbind_conn);
821 		ill->ill_poll_capab->ill_unbind_conn = NULL;
822 		kmem_free(ill->ill_poll_capab,
823 		    sizeof (ill_poll_capab_t) +
824 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
825 		ill->ill_poll_capab = NULL;
826 	}
827 
828 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
829 
830 	while (ill->ill_ipif != NULL)
831 		ipif_free_tail(ill->ill_ipif);
832 
833 	ill_down_tail(ill);
834 
835 	/*
836 	 * We have removed all references to ilm from conn and the ones joined
837 	 * within the kernel.
838 	 *
839 	 * We don't walk conns, mrts and ires because
840 	 *
841 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
842 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
843 	 *    ill references.
844 	 */
845 	ASSERT(ilm_walk_ill(ill) == 0);
846 	/*
847 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
848 	 * could free the phyint. No more reference to the phyint after this
849 	 * point.
850 	 */
851 	(void) ill_glist_delete(ill);
852 
853 	rw_enter(&ip_g_nd_lock, RW_WRITER);
854 	if (ill->ill_ndd_name != NULL)
855 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
856 	rw_exit(&ip_g_nd_lock);
857 
858 
859 	if (ill->ill_frag_ptr != NULL) {
860 		uint_t count;
861 
862 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
863 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
864 		}
865 		mi_free(ill->ill_frag_ptr);
866 		ill->ill_frag_ptr = NULL;
867 		ill->ill_frag_hash_tbl = NULL;
868 	}
869 	if (ill->ill_nd_lla_mp != NULL)
870 		freemsg(ill->ill_nd_lla_mp);
871 	/* Free all retained control messages. */
872 	mpp = &ill->ill_first_mp_to_free;
873 	do {
874 		while (mpp[0]) {
875 			mblk_t  *mp;
876 			mblk_t  *mp1;
877 
878 			mp = mpp[0];
879 			mpp[0] = mp->b_next;
880 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
881 				mp1->b_next = NULL;
882 				mp1->b_prev = NULL;
883 			}
884 			freemsg(mp);
885 		}
886 	} while (mpp++ != &ill->ill_last_mp_to_free);
887 
888 	ill_free_mib(ill);
889 	ILL_TRACE_CLEANUP(ill);
890 }
891 
892 static void
893 ill_free_mib(ill_t *ill)
894 {
895 	if (ill->ill_ip6_mib != NULL) {
896 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
897 		ill->ill_ip6_mib = NULL;
898 	}
899 	if (ill->ill_icmp6_mib != NULL) {
900 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
901 		ill->ill_icmp6_mib = NULL;
902 	}
903 }
904 
905 /*
906  * Concatenate together a physical address and a sap.
907  *
908  * Sap_lengths are interpreted as follows:
909  *   sap_length == 0	==>	no sap
910  *   sap_length > 0	==>	sap is at the head of the dlpi address
911  *   sap_length < 0	==>	sap is at the tail of the dlpi address
912  */
913 static void
914 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
915     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
916 {
917 	uint16_t sap_addr = (uint16_t)sap_src;
918 
919 	if (sap_length == 0) {
920 		if (phys_src == NULL)
921 			bzero(dst, phys_length);
922 		else
923 			bcopy(phys_src, dst, phys_length);
924 	} else if (sap_length < 0) {
925 		if (phys_src == NULL)
926 			bzero(dst, phys_length);
927 		else
928 			bcopy(phys_src, dst, phys_length);
929 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
930 	} else {
931 		bcopy(&sap_addr, dst, sizeof (sap_addr));
932 		if (phys_src == NULL)
933 			bzero((char *)dst + sap_length, phys_length);
934 		else
935 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
936 	}
937 }
938 
939 /*
940  * Generate a dl_unitdata_req mblk for the device and address given.
941  * addr_length is the length of the physical portion of the address.
942  * If addr is NULL include an all zero address of the specified length.
943  * TRUE? In any case, addr_length is taken to be the entire length of the
944  * dlpi address, including the absolute value of sap_length.
945  */
946 mblk_t *
947 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
948 		t_scalar_t sap_length)
949 {
950 	dl_unitdata_req_t *dlur;
951 	mblk_t	*mp;
952 	t_scalar_t	abs_sap_length;		/* absolute value */
953 
954 	abs_sap_length = ABS(sap_length);
955 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
956 		DL_UNITDATA_REQ);
957 	if (mp == NULL)
958 		return (NULL);
959 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
960 	/* HACK: accomodate incompatible DLPI drivers */
961 	if (addr_length == 8)
962 		addr_length = 6;
963 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
964 	dlur->dl_dest_addr_offset = sizeof (*dlur);
965 	dlur->dl_priority.dl_min = 0;
966 	dlur->dl_priority.dl_max = 0;
967 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
968 	    (uchar_t *)&dlur[1]);
969 	return (mp);
970 }
971 
972 /*
973  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
974  * Return an error if we already have 1 or more ioctls in progress.
975  * This is used only for non-exclusive ioctls. Currently this is used
976  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
977  * and thus need to use ipsq_pending_mp_add.
978  */
979 boolean_t
980 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
981 {
982 	ASSERT(MUTEX_HELD(&ill->ill_lock));
983 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
984 	/*
985 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
986 	 */
987 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
988 	    (add_mp->b_datap->db_type == M_IOCTL));
989 
990 	ASSERT(MUTEX_HELD(&connp->conn_lock));
991 	/*
992 	 * Return error if the conn has started closing. The conn
993 	 * could have finished cleaning up the pending mp list,
994 	 * If so we should not add another mp to the list negating
995 	 * the cleanup.
996 	 */
997 	if (connp->conn_state_flags & CONN_CLOSING)
998 		return (B_FALSE);
999 	/*
1000 	 * Add the pending mp to the head of the list, chained by b_next.
1001 	 * Note down the conn on which the ioctl request came, in b_prev.
1002 	 * This will be used to later get the conn, when we get a response
1003 	 * on the ill queue, from some other module (typically arp)
1004 	 */
1005 	add_mp->b_next = (void *)ill->ill_pending_mp;
1006 	add_mp->b_queue = CONNP_TO_WQ(connp);
1007 	ill->ill_pending_mp = add_mp;
1008 	if (connp != NULL)
1009 		connp->conn_oper_pending_ill = ill;
1010 	return (B_TRUE);
1011 }
1012 
1013 /*
1014  * Retrieve the ill_pending_mp and return it. We have to walk the list
1015  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1016  */
1017 mblk_t *
1018 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1019 {
1020 	mblk_t	*prev = NULL;
1021 	mblk_t	*curr = NULL;
1022 	uint_t	id;
1023 	conn_t	*connp;
1024 
1025 	/*
1026 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1027 	 * up the pending mp, but it does not know the ioc_id and
1028 	 * passes in a zero for it.
1029 	 */
1030 	mutex_enter(&ill->ill_lock);
1031 	if (ioc_id != 0)
1032 		*connpp = NULL;
1033 
1034 	/* Search the list for the appropriate ioctl based on ioc_id */
1035 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1036 	    prev = curr, curr = curr->b_next) {
1037 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1038 		connp = Q_TO_CONN(curr->b_queue);
1039 		/* Match based on the ioc_id or based on the conn */
1040 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1041 			break;
1042 	}
1043 
1044 	if (curr != NULL) {
1045 		/* Unlink the mblk from the pending mp list */
1046 		if (prev != NULL) {
1047 			prev->b_next = curr->b_next;
1048 		} else {
1049 			ASSERT(ill->ill_pending_mp == curr);
1050 			ill->ill_pending_mp = curr->b_next;
1051 		}
1052 
1053 		/*
1054 		 * conn refcnt must have been bumped up at the start of
1055 		 * the ioctl. So we can safely access the conn.
1056 		 */
1057 		ASSERT(CONN_Q(curr->b_queue));
1058 		*connpp = Q_TO_CONN(curr->b_queue);
1059 		curr->b_next = NULL;
1060 		curr->b_queue = NULL;
1061 	}
1062 
1063 	mutex_exit(&ill->ill_lock);
1064 
1065 	return (curr);
1066 }
1067 
1068 /*
1069  * Add the pending mp to the list. There can be only 1 pending mp
1070  * in the list. Any exclusive ioctl that needs to wait for a response
1071  * from another module or driver needs to use this function to set
1072  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1073  * the other module/driver. This is also used while waiting for the
1074  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1075  */
1076 boolean_t
1077 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1078     int waitfor)
1079 {
1080 	ipsq_t	*ipsq;
1081 
1082 	ASSERT(IAM_WRITER_IPIF(ipif));
1083 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1084 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1085 	/*
1086 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1087 	 * M_ERROR/M_HANGUP from driver
1088 	 */
1089 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1090 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1091 
1092 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1093 	if (connp != NULL) {
1094 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1095 		/*
1096 		 * Return error if the conn has started closing. The conn
1097 		 * could have finished cleaning up the pending mp list,
1098 		 * If so we should not add another mp to the list negating
1099 		 * the cleanup.
1100 		 */
1101 		if (connp->conn_state_flags & CONN_CLOSING)
1102 			return (B_FALSE);
1103 	}
1104 	mutex_enter(&ipsq->ipsq_lock);
1105 	ipsq->ipsq_pending_ipif = ipif;
1106 	/*
1107 	 * Note down the queue in b_queue. This will be returned by
1108 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1109 	 * the processing
1110 	 */
1111 	add_mp->b_next = NULL;
1112 	add_mp->b_queue = q;
1113 	ipsq->ipsq_pending_mp = add_mp;
1114 	ipsq->ipsq_waitfor = waitfor;
1115 	/*
1116 	 * ipsq_current_ipif is needed to restart the operation from
1117 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1118 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1119 	 * been set until now.
1120 	 */
1121 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1122 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1123 		ipsq->ipsq_current_ipif = ipif;
1124 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1125 	}
1126 	if (connp != NULL)
1127 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1128 	mutex_exit(&ipsq->ipsq_lock);
1129 	return (B_TRUE);
1130 }
1131 
1132 /*
1133  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1134  * queued in the list.
1135  */
1136 mblk_t *
1137 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1138 {
1139 	mblk_t	*curr = NULL;
1140 
1141 	mutex_enter(&ipsq->ipsq_lock);
1142 	*connpp = NULL;
1143 	if (ipsq->ipsq_pending_mp == NULL) {
1144 		mutex_exit(&ipsq->ipsq_lock);
1145 		return (NULL);
1146 	}
1147 
1148 	/* There can be only 1 such excl message */
1149 	curr = ipsq->ipsq_pending_mp;
1150 	ASSERT(curr != NULL && curr->b_next == NULL);
1151 	ipsq->ipsq_pending_ipif = NULL;
1152 	ipsq->ipsq_pending_mp = NULL;
1153 	ipsq->ipsq_waitfor = 0;
1154 	mutex_exit(&ipsq->ipsq_lock);
1155 
1156 	if (CONN_Q(curr->b_queue)) {
1157 		/*
1158 		 * This mp did a refhold on the conn, at the start of the ioctl.
1159 		 * So we can safely return a pointer to the conn to the caller.
1160 		 */
1161 		*connpp = Q_TO_CONN(curr->b_queue);
1162 	} else {
1163 		*connpp = NULL;
1164 	}
1165 	curr->b_next = NULL;
1166 	curr->b_prev = NULL;
1167 	return (curr);
1168 }
1169 
1170 /*
1171  * Cleanup the ioctl mp queued in ipsq_pending_mp
1172  * - Called in the ill_delete path
1173  * - Called in the M_ERROR or M_HANGUP path on the ill.
1174  * - Called in the conn close path.
1175  */
1176 boolean_t
1177 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1178 {
1179 	mblk_t	*mp;
1180 	ipsq_t	*ipsq;
1181 	queue_t	*q;
1182 	ipif_t	*ipif;
1183 
1184 	ASSERT(IAM_WRITER_ILL(ill));
1185 	ipsq = ill->ill_phyint->phyint_ipsq;
1186 	mutex_enter(&ipsq->ipsq_lock);
1187 	/*
1188 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1189 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1190 	 * even if it is meant for another ill, since we have to enqueue
1191 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1192 	 * If connp is non-null we are called from the conn close path.
1193 	 */
1194 	mp = ipsq->ipsq_pending_mp;
1195 	if (mp == NULL || (connp != NULL &&
1196 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1197 		mutex_exit(&ipsq->ipsq_lock);
1198 		return (B_FALSE);
1199 	}
1200 	/* Now remove from the ipsq_pending_mp */
1201 	ipsq->ipsq_pending_mp = NULL;
1202 	q = mp->b_queue;
1203 	mp->b_next = NULL;
1204 	mp->b_prev = NULL;
1205 	mp->b_queue = NULL;
1206 
1207 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1208 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1209 	if (ill->ill_move_in_progress) {
1210 		ILL_CLEAR_MOVE(ill);
1211 	} else if (ill->ill_up_ipifs) {
1212 		ill_group_cleanup(ill);
1213 	}
1214 
1215 	ipif = ipsq->ipsq_pending_ipif;
1216 	ipsq->ipsq_pending_ipif = NULL;
1217 	ipsq->ipsq_waitfor = 0;
1218 	ipsq->ipsq_current_ipif = NULL;
1219 	mutex_exit(&ipsq->ipsq_lock);
1220 
1221 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1222 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1223 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1224 	} else {
1225 		/*
1226 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1227 		 * be just ip_ioctl_freemsg. we have to restart it
1228 		 * otherwise the thread will be stuck.
1229 		 */
1230 		ip_ioctl_freemsg(mp);
1231 	}
1232 	return (B_TRUE);
1233 }
1234 
1235 /*
1236  * The ill is closing. Cleanup all the pending mps. Called exclusively
1237  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1238  * knows this ill, and hence nobody can add an mp to this list
1239  */
1240 static void
1241 ill_pending_mp_cleanup(ill_t *ill)
1242 {
1243 	mblk_t	*mp;
1244 	queue_t	*q;
1245 
1246 	ASSERT(IAM_WRITER_ILL(ill));
1247 
1248 	mutex_enter(&ill->ill_lock);
1249 	/*
1250 	 * Every mp on the pending mp list originating from an ioctl
1251 	 * added 1 to the conn refcnt, at the start of the ioctl.
1252 	 * So bump it down now.  See comments in ip_wput_nondata()
1253 	 */
1254 	while (ill->ill_pending_mp != NULL) {
1255 		mp = ill->ill_pending_mp;
1256 		ill->ill_pending_mp = mp->b_next;
1257 		mutex_exit(&ill->ill_lock);
1258 
1259 		q = mp->b_queue;
1260 		ASSERT(CONN_Q(q));
1261 		mp->b_next = NULL;
1262 		mp->b_prev = NULL;
1263 		mp->b_queue = NULL;
1264 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1265 		mutex_enter(&ill->ill_lock);
1266 	}
1267 	ill->ill_pending_ipif = NULL;
1268 
1269 	mutex_exit(&ill->ill_lock);
1270 }
1271 
1272 /*
1273  * Called in the conn close path and ill delete path
1274  */
1275 static void
1276 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1277 {
1278 	ipsq_t	*ipsq;
1279 	mblk_t	*prev;
1280 	mblk_t	*curr;
1281 	mblk_t	*next;
1282 	queue_t	*q;
1283 	mblk_t	*tmp_list = NULL;
1284 
1285 	ASSERT(IAM_WRITER_ILL(ill));
1286 	if (connp != NULL)
1287 		q = CONNP_TO_WQ(connp);
1288 	else
1289 		q = ill->ill_wq;
1290 
1291 	ipsq = ill->ill_phyint->phyint_ipsq;
1292 	/*
1293 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1294 	 * In the case of ioctl from a conn, there can be only 1 mp
1295 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1296 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1297 	 * ioctls meant for this ill form conn's are not flushed. They will
1298 	 * be processed during ipsq_exit and will not find the ill and will
1299 	 * return error.
1300 	 */
1301 	mutex_enter(&ipsq->ipsq_lock);
1302 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1303 	    curr = next) {
1304 		next = curr->b_next;
1305 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1306 			/* Unlink the mblk from the pending mp list */
1307 			if (prev != NULL) {
1308 				prev->b_next = curr->b_next;
1309 			} else {
1310 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1311 				ipsq->ipsq_xopq_mphead = curr->b_next;
1312 			}
1313 			if (ipsq->ipsq_xopq_mptail == curr)
1314 				ipsq->ipsq_xopq_mptail = prev;
1315 			/*
1316 			 * Create a temporary list and release the ipsq lock
1317 			 * New elements are added to the head of the tmp_list
1318 			 */
1319 			curr->b_next = tmp_list;
1320 			tmp_list = curr;
1321 		} else {
1322 			prev = curr;
1323 		}
1324 	}
1325 	mutex_exit(&ipsq->ipsq_lock);
1326 
1327 	while (tmp_list != NULL) {
1328 		curr = tmp_list;
1329 		tmp_list = curr->b_next;
1330 		curr->b_next = NULL;
1331 		curr->b_prev = NULL;
1332 		curr->b_queue = NULL;
1333 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1334 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1335 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1336 		} else {
1337 			/*
1338 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1339 			 * this can't be just ip_ioctl_freemsg. we have to
1340 			 * restart it otherwise the thread will be stuck.
1341 			 */
1342 			ip_ioctl_freemsg(curr);
1343 		}
1344 	}
1345 }
1346 
1347 /*
1348  * This conn has started closing. Cleanup any pending ioctl from this conn.
1349  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1350  */
1351 void
1352 conn_ioctl_cleanup(conn_t *connp)
1353 {
1354 	mblk_t *curr;
1355 	ipsq_t	*ipsq;
1356 	ill_t	*ill;
1357 	boolean_t refheld;
1358 
1359 	/*
1360 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1361 	 * ioctl has not yet started, the mp is pending in the list headed by
1362 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1363 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1364 	 * is currently executing now the mp is not queued anywhere but
1365 	 * conn_oper_pending_ill is null. The conn close will wait
1366 	 * till the conn_ref drops to zero.
1367 	 */
1368 	mutex_enter(&connp->conn_lock);
1369 	ill = connp->conn_oper_pending_ill;
1370 	if (ill == NULL) {
1371 		mutex_exit(&connp->conn_lock);
1372 		return;
1373 	}
1374 
1375 	curr = ill_pending_mp_get(ill, &connp, 0);
1376 	if (curr != NULL) {
1377 		mutex_exit(&connp->conn_lock);
1378 		CONN_DEC_REF(connp);
1379 		ip_ioctl_freemsg(curr);
1380 		return;
1381 	}
1382 	/*
1383 	 * We may not be able to refhold the ill if the ill/ipif
1384 	 * is changing. But we need to make sure that the ill will
1385 	 * not vanish. So we just bump up the ill_waiter count.
1386 	 */
1387 	refheld = ill_waiter_inc(ill);
1388 	mutex_exit(&connp->conn_lock);
1389 	if (refheld) {
1390 		if (ipsq_enter(ill, B_TRUE)) {
1391 			ill_waiter_dcr(ill);
1392 			/*
1393 			 * Check whether this ioctl has started and is
1394 			 * pending now in ipsq_pending_mp. If it is not
1395 			 * found there then check whether this ioctl has
1396 			 * not even started and is in the ipsq_xopq list.
1397 			 */
1398 			if (!ipsq_pending_mp_cleanup(ill, connp))
1399 				ipsq_xopq_mp_cleanup(ill, connp);
1400 			ipsq = ill->ill_phyint->phyint_ipsq;
1401 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1402 			return;
1403 		}
1404 	}
1405 
1406 	/*
1407 	 * The ill is also closing and we could not bump up the
1408 	 * ill_waiter_count or we could not enter the ipsq. Leave
1409 	 * the cleanup to ill_delete
1410 	 */
1411 	mutex_enter(&connp->conn_lock);
1412 	while (connp->conn_oper_pending_ill != NULL)
1413 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1414 	mutex_exit(&connp->conn_lock);
1415 	if (refheld)
1416 		ill_waiter_dcr(ill);
1417 }
1418 
1419 /*
1420  * ipcl_walk function for cleaning up conn_*_ill fields.
1421  */
1422 static void
1423 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1424 {
1425 	ill_t	*ill = (ill_t *)arg;
1426 	ire_t	*ire;
1427 
1428 	mutex_enter(&connp->conn_lock);
1429 	if (connp->conn_multicast_ill == ill) {
1430 		/* Revert to late binding */
1431 		connp->conn_multicast_ill = NULL;
1432 		connp->conn_orig_multicast_ifindex = 0;
1433 	}
1434 	if (connp->conn_incoming_ill == ill)
1435 		connp->conn_incoming_ill = NULL;
1436 	if (connp->conn_outgoing_ill == ill)
1437 		connp->conn_outgoing_ill = NULL;
1438 	if (connp->conn_outgoing_pill == ill)
1439 		connp->conn_outgoing_pill = NULL;
1440 	if (connp->conn_nofailover_ill == ill)
1441 		connp->conn_nofailover_ill = NULL;
1442 	if (connp->conn_xmit_if_ill == ill)
1443 		connp->conn_xmit_if_ill = NULL;
1444 	if (connp->conn_ire_cache != NULL) {
1445 		ire = connp->conn_ire_cache;
1446 		/*
1447 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1448 		 * interface X and ipif coming from interface Y, if interface
1449 		 * X and Y are part of the same IPMPgroup. Thus whenever
1450 		 * interface X goes down, remove all references to it by
1451 		 * checking both on ire_ipif and ire_stq.
1452 		 */
1453 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1454 		    (ire->ire_type == IRE_CACHE &&
1455 		    ire->ire_stq == ill->ill_wq)) {
1456 			connp->conn_ire_cache = NULL;
1457 			mutex_exit(&connp->conn_lock);
1458 			ire_refrele_notr(ire);
1459 			return;
1460 		}
1461 	}
1462 	mutex_exit(&connp->conn_lock);
1463 
1464 }
1465 
1466 /* ARGSUSED */
1467 void
1468 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1469 {
1470 	ill_t	*ill = q->q_ptr;
1471 	ipif_t	*ipif;
1472 
1473 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1474 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1475 		ipif_down_tail(ipif);
1476 	ill_down_tail(ill);
1477 	freemsg(mp);
1478 	ipsq->ipsq_current_ipif = NULL;
1479 }
1480 
1481 /*
1482  * ill_down_start is called when we want to down this ill and bring it up again
1483  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1484  * all interfaces, but don't tear down any plumbing.
1485  */
1486 boolean_t
1487 ill_down_start(queue_t *q, mblk_t *mp)
1488 {
1489 	ill_t	*ill;
1490 	ipif_t	*ipif;
1491 
1492 	ill = q->q_ptr;
1493 
1494 	ASSERT(IAM_WRITER_ILL(ill));
1495 
1496 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1497 		(void) ipif_down(ipif, NULL, NULL);
1498 
1499 	ill_down(ill);
1500 
1501 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1502 	mutex_enter(&ill->ill_lock);
1503 	/*
1504 	 * Atomically test and add the pending mp if references are
1505 	 * still active.
1506 	 */
1507 	if (!ill_is_quiescent(ill)) {
1508 		/*
1509 		 * Get rid of any pending mps and cleanup. Call will
1510 		 * not fail since we are passing a null connp.
1511 		 */
1512 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1513 		    mp, ILL_DOWN);
1514 		mutex_exit(&ill->ill_lock);
1515 		return (B_FALSE);
1516 	}
1517 	mutex_exit(&ill->ill_lock);
1518 	return (B_TRUE);
1519 }
1520 
1521 static void
1522 ill_down(ill_t *ill)
1523 {
1524 	/* Blow off any IREs dependent on this ILL. */
1525 	ire_walk(ill_downi, (char *)ill);
1526 
1527 	mutex_enter(&ire_mrtun_lock);
1528 	if (ire_mrtun_count != 0) {
1529 		mutex_exit(&ire_mrtun_lock);
1530 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1531 		    (char *)ill, NULL);
1532 	} else {
1533 		mutex_exit(&ire_mrtun_lock);
1534 	}
1535 
1536 	/*
1537 	 * If any interface based forwarding table exists
1538 	 * Blow off the ires there dependent on this ill
1539 	 */
1540 	mutex_enter(&ire_srcif_table_lock);
1541 	if (ire_srcif_table_count > 0) {
1542 		mutex_exit(&ire_srcif_table_lock);
1543 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1544 	} else {
1545 		mutex_exit(&ire_srcif_table_lock);
1546 	}
1547 
1548 	/* Remove any conn_*_ill depending on this ill */
1549 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1550 
1551 	if (ill->ill_group != NULL) {
1552 		illgrp_delete(ill);
1553 	}
1554 
1555 }
1556 
1557 static void
1558 ill_down_tail(ill_t *ill)
1559 {
1560 	int	i;
1561 
1562 	/* Destroy ill_srcif_table if it exists */
1563 	/* Lock not reqd really because nobody should be able to access */
1564 	mutex_enter(&ill->ill_lock);
1565 	if (ill->ill_srcif_table != NULL) {
1566 		ill->ill_srcif_refcnt = 0;
1567 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1568 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1569 		}
1570 		kmem_free(ill->ill_srcif_table,
1571 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1572 		ill->ill_srcif_table = NULL;
1573 		ill->ill_srcif_refcnt = 0;
1574 		ill->ill_mrtun_refcnt = 0;
1575 	}
1576 	mutex_exit(&ill->ill_lock);
1577 }
1578 
1579 /*
1580  * ire_walk routine used to delete every IRE that depends on queues
1581  * associated with 'ill'.  (Always called as writer.)
1582  */
1583 static void
1584 ill_downi(ire_t *ire, char *ill_arg)
1585 {
1586 	ill_t	*ill = (ill_t *)ill_arg;
1587 
1588 	/*
1589 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1590 	 * interface X and ipif coming from interface Y, if interface
1591 	 * X and Y are part of the same IPMP group. Thus whenever interface
1592 	 * X goes down, remove all references to it by checking both
1593 	 * on ire_ipif and ire_stq.
1594 	 */
1595 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1596 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1597 		ire_delete(ire);
1598 	}
1599 }
1600 
1601 /*
1602  * A seperate routine for deleting revtun and srcif based routes
1603  * are needed because the ires only deleted when the interface
1604  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1605  * we want to keep mobile IP specific code separate.
1606  */
1607 static void
1608 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1609 {
1610 	ill_t   *ill = (ill_t *)ill_arg;
1611 
1612 	ASSERT(ire->ire_in_ill != NULL);
1613 
1614 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1615 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1616 		ire_delete(ire);
1617 	}
1618 }
1619 
1620 /*
1621  * Remove ire/nce from the fastpath list.
1622  */
1623 void
1624 ill_fastpath_nack(ill_t *ill)
1625 {
1626 	if (ill->ill_isv6) {
1627 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1628 	} else {
1629 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1630 	}
1631 }
1632 
1633 /* Consume an M_IOCACK of the fastpath probe. */
1634 void
1635 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1636 {
1637 	mblk_t	*mp1 = mp;
1638 
1639 	/*
1640 	 * If this was the first attempt turn on the fastpath probing.
1641 	 */
1642 	mutex_enter(&ill->ill_lock);
1643 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1644 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1645 	mutex_exit(&ill->ill_lock);
1646 
1647 	/* Free the M_IOCACK mblk, hold on to the data */
1648 	mp = mp->b_cont;
1649 	freeb(mp1);
1650 	if (mp == NULL)
1651 		return;
1652 	if (mp->b_cont != NULL) {
1653 		/*
1654 		 * Update all IRE's or NCE's that are waiting for
1655 		 * fastpath update.
1656 		 */
1657 		if (ill->ill_isv6) {
1658 			/*
1659 			 * update nce's in the fastpath list.
1660 			 */
1661 			nce_fastpath_list_dispatch(ill,
1662 			    ndp_fastpath_update, mp);
1663 		} else {
1664 
1665 			/*
1666 			 * update ire's in the fastpath list.
1667 			 */
1668 			ire_fastpath_list_dispatch(ill,
1669 			    ire_fastpath_update, mp);
1670 			/*
1671 			 * Check if we need to traverse reverse tunnel table.
1672 			 * Since there is only single ire_type (IRE_MIPRTUN)
1673 			 * in the table, we don't need to match on ire_type.
1674 			 * We have to check ire_mrtun_count and not the
1675 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1676 			 * on the incoming ill and here we are dealing with
1677 			 * outgoing ill.
1678 			 */
1679 			mutex_enter(&ire_mrtun_lock);
1680 			if (ire_mrtun_count != 0) {
1681 				mutex_exit(&ire_mrtun_lock);
1682 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1683 				    (void (*)(ire_t *, void *))
1684 					ire_fastpath_update, mp, ill);
1685 			} else {
1686 				mutex_exit(&ire_mrtun_lock);
1687 			}
1688 		}
1689 		mp1 = mp->b_cont;
1690 		freeb(mp);
1691 		mp = mp1;
1692 	} else {
1693 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1694 	}
1695 
1696 	freeb(mp);
1697 }
1698 
1699 /*
1700  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1701  * The data portion of the request is a dl_unitdata_req_t template for
1702  * what we would send downstream in the absence of a fastpath confirmation.
1703  */
1704 int
1705 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1706 {
1707 	struct iocblk	*ioc;
1708 	mblk_t	*mp;
1709 
1710 	if (dlur_mp == NULL)
1711 		return (EINVAL);
1712 
1713 	mutex_enter(&ill->ill_lock);
1714 	switch (ill->ill_dlpi_fastpath_state) {
1715 	case IDMS_FAILED:
1716 		/*
1717 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1718 		 * support it.
1719 		 */
1720 		mutex_exit(&ill->ill_lock);
1721 		return (ENOTSUP);
1722 	case IDMS_UNKNOWN:
1723 		/* This is the first probe */
1724 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1725 		break;
1726 	default:
1727 		break;
1728 	}
1729 	mutex_exit(&ill->ill_lock);
1730 
1731 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1732 		return (EAGAIN);
1733 
1734 	mp->b_cont = copyb(dlur_mp);
1735 	if (mp->b_cont == NULL) {
1736 		freeb(mp);
1737 		return (EAGAIN);
1738 	}
1739 
1740 	ioc = (struct iocblk *)mp->b_rptr;
1741 	ioc->ioc_count = msgdsize(mp->b_cont);
1742 
1743 	putnext(ill->ill_wq, mp);
1744 	return (0);
1745 }
1746 
1747 void
1748 ill_capability_probe(ill_t *ill)
1749 {
1750 	/*
1751 	 * Do so only if negotiation is enabled, capabilities are unknown,
1752 	 * and a capability negotiation is not already in progress.
1753 	 */
1754 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1755 	    ill->ill_capab_state != IDMS_RENEG)
1756 		return;
1757 
1758 	ill->ill_capab_state = IDMS_INPROGRESS;
1759 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1760 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1761 }
1762 
1763 void
1764 ill_capability_reset(ill_t *ill)
1765 {
1766 	mblk_t *sc_mp = NULL;
1767 	mblk_t *tmp;
1768 
1769 	/*
1770 	 * Note here that we reset the state to UNKNOWN, and later send
1771 	 * down the DL_CAPABILITY_REQ without first setting the state to
1772 	 * INPROGRESS.  We do this in order to distinguish the
1773 	 * DL_CAPABILITY_ACK response which may come back in response to
1774 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1775 	 * also handle the case where the driver doesn't send us back
1776 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1777 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1778 	 * features are turned off until the state reaches IDMS_OK.
1779 	 */
1780 	ill->ill_capab_state = IDMS_UNKNOWN;
1781 
1782 	/*
1783 	 * Disable sub-capabilities and request a list of sub-capability
1784 	 * messages which will be sent down to the driver.  Each handler
1785 	 * allocates the corresponding dl_capability_sub_t inside an
1786 	 * mblk, and links it to the existing sc_mp mblk, or return it
1787 	 * as sc_mp if it's the first sub-capability (the passed in
1788 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1789 	 * sc_mp will be pulled-up, before passing it downstream.
1790 	 */
1791 	ill_capability_mdt_reset(ill, &sc_mp);
1792 	ill_capability_hcksum_reset(ill, &sc_mp);
1793 	ill_capability_zerocopy_reset(ill, &sc_mp);
1794 	ill_capability_ipsec_reset(ill, &sc_mp);
1795 	ill_capability_poll_reset(ill, &sc_mp);
1796 
1797 	/* Nothing to send down in order to disable the capabilities? */
1798 	if (sc_mp == NULL)
1799 		return;
1800 
1801 	tmp = msgpullup(sc_mp, -1);
1802 	freemsg(sc_mp);
1803 	if ((sc_mp = tmp) == NULL) {
1804 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1805 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1806 		return;
1807 	}
1808 
1809 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1810 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1811 }
1812 
1813 /*
1814  * Request or set new-style hardware capabilities supported by DLS provider.
1815  */
1816 static void
1817 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1818 {
1819 	mblk_t *mp;
1820 	dl_capability_req_t *capb;
1821 	size_t size = 0;
1822 	uint8_t *ptr;
1823 
1824 	if (reqp != NULL)
1825 		size = MBLKL(reqp);
1826 
1827 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1828 	if (mp == NULL) {
1829 		freemsg(reqp);
1830 		return;
1831 	}
1832 	ptr = mp->b_rptr;
1833 
1834 	capb = (dl_capability_req_t *)ptr;
1835 	ptr += sizeof (dl_capability_req_t);
1836 
1837 	if (reqp != NULL) {
1838 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1839 		capb->dl_sub_length = size;
1840 		bcopy(reqp->b_rptr, ptr, size);
1841 		ptr += size;
1842 		mp->b_cont = reqp->b_cont;
1843 		freeb(reqp);
1844 	}
1845 	ASSERT(ptr == mp->b_wptr);
1846 
1847 	ill_dlpi_send(ill, mp);
1848 }
1849 
1850 static void
1851 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1852 {
1853 	dl_capab_id_t *id_ic;
1854 	uint_t sub_dl_cap = outers->dl_cap;
1855 	dl_capability_sub_t *inners;
1856 	uint8_t *capend;
1857 
1858 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1859 
1860 	/*
1861 	 * Note: range checks here are not absolutely sufficient to
1862 	 * make us robust against malformed messages sent by drivers;
1863 	 * this is in keeping with the rest of IP's dlpi handling.
1864 	 * (Remember, it's coming from something else in the kernel
1865 	 * address space)
1866 	 */
1867 
1868 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1869 	if (capend > mp->b_wptr) {
1870 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1871 		    "malformed sub-capability too long for mblk");
1872 		return;
1873 	}
1874 
1875 	id_ic = (dl_capab_id_t *)(outers + 1);
1876 
1877 	if (outers->dl_length < sizeof (*id_ic) ||
1878 	    (inners = &id_ic->id_subcap,
1879 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1880 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1881 		    "encapsulated capab type %d too long for mblk",
1882 		    inners->dl_cap);
1883 		return;
1884 	}
1885 
1886 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1887 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1888 		    "isn't as expected; pass-thru module(s) detected, "
1889 		    "discarding capability\n", inners->dl_cap));
1890 		return;
1891 	}
1892 
1893 	/* Process the encapsulated sub-capability */
1894 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1895 }
1896 
1897 /*
1898  * Process Multidata Transmit capability negotiation ack received from a
1899  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1900  * DL_CAPABILITY_ACK message.
1901  */
1902 static void
1903 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1904 {
1905 	mblk_t *nmp = NULL;
1906 	dl_capability_req_t *oc;
1907 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1908 	ill_mdt_capab_t **ill_mdt_capab;
1909 	uint_t sub_dl_cap = isub->dl_cap;
1910 	uint8_t *capend;
1911 
1912 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1913 
1914 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1915 
1916 	/*
1917 	 * Note: range checks here are not absolutely sufficient to
1918 	 * make us robust against malformed messages sent by drivers;
1919 	 * this is in keeping with the rest of IP's dlpi handling.
1920 	 * (Remember, it's coming from something else in the kernel
1921 	 * address space)
1922 	 */
1923 
1924 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1925 	if (capend > mp->b_wptr) {
1926 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1927 		    "malformed sub-capability too long for mblk");
1928 		return;
1929 	}
1930 
1931 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1932 
1933 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1934 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1935 		    "unsupported MDT sub-capability (version %d, expected %d)",
1936 		    mdt_ic->mdt_version, MDT_VERSION_2);
1937 		return;
1938 	}
1939 
1940 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1941 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1942 		    "capability isn't as expected; pass-thru module(s) "
1943 		    "detected, discarding capability\n"));
1944 		return;
1945 	}
1946 
1947 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1948 
1949 		if (*ill_mdt_capab == NULL) {
1950 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1951 			    KM_NOSLEEP);
1952 
1953 			if (*ill_mdt_capab == NULL) {
1954 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1955 				    "could not enable MDT version %d "
1956 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1957 				    ill->ill_name);
1958 				return;
1959 			}
1960 		}
1961 
1962 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1963 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1964 		    "header spaces, %d max pld bufs, %d span limit)\n",
1965 		    ill->ill_name, MDT_VERSION_2,
1966 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1967 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1968 
1969 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1970 		(*ill_mdt_capab)->ill_mdt_on = 1;
1971 		/*
1972 		 * Round the following values to the nearest 32-bit; ULP
1973 		 * may further adjust them to accomodate for additional
1974 		 * protocol headers.  We pass these values to ULP during
1975 		 * bind time.
1976 		 */
1977 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1978 		    roundup(mdt_ic->mdt_hdr_head, 4);
1979 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1980 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1981 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1982 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1983 
1984 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1985 	} else {
1986 		uint_t size;
1987 		uchar_t *rptr;
1988 
1989 		size = sizeof (dl_capability_req_t) +
1990 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1991 
1992 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1993 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1994 			    "could not enable MDT for %s (ENOMEM)\n",
1995 			    ill->ill_name);
1996 			return;
1997 		}
1998 
1999 		rptr = nmp->b_rptr;
2000 		/* initialize dl_capability_req_t */
2001 		oc = (dl_capability_req_t *)nmp->b_rptr;
2002 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2003 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2004 		    sizeof (dl_capab_mdt_t);
2005 		nmp->b_rptr += sizeof (dl_capability_req_t);
2006 
2007 		/* initialize dl_capability_sub_t */
2008 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2009 		nmp->b_rptr += sizeof (*isub);
2010 
2011 		/* initialize dl_capab_mdt_t */
2012 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2013 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2014 
2015 		nmp->b_rptr = rptr;
2016 
2017 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2018 		    "to enable MDT version %d\n", ill->ill_name,
2019 		    MDT_VERSION_2));
2020 
2021 		/* set ENABLE flag */
2022 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2023 
2024 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2025 		ill_dlpi_send(ill, nmp);
2026 	}
2027 }
2028 
2029 static void
2030 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2031 {
2032 	mblk_t *mp;
2033 	dl_capab_mdt_t *mdt_subcap;
2034 	dl_capability_sub_t *dl_subcap;
2035 	int size;
2036 
2037 	if (!(ill->ill_capabilities & ILL_CAPAB_MDT))
2038 		return;
2039 
2040 	ASSERT(ill->ill_mdt_capab != NULL);
2041 	/*
2042 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2043 	 * structure since it's possible that another thread is still
2044 	 * referring to it.  The structure only gets deallocated when
2045 	 * we destroy the ill.
2046 	 */
2047 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2048 
2049 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2050 
2051 	mp = allocb(size, BPRI_HI);
2052 	if (mp == NULL) {
2053 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2054 		    "request to disable MDT\n"));
2055 		return;
2056 	}
2057 
2058 	mp->b_wptr = mp->b_rptr + size;
2059 
2060 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2061 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2062 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2063 
2064 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2065 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2066 	mdt_subcap->mdt_flags = 0;
2067 	mdt_subcap->mdt_hdr_head = 0;
2068 	mdt_subcap->mdt_hdr_tail = 0;
2069 
2070 	if (*sc_mp != NULL)
2071 		linkb(*sc_mp, mp);
2072 	else
2073 		*sc_mp = mp;
2074 }
2075 
2076 /*
2077  * Send a DL_NOTIFY_REQ to the specified ill to enable
2078  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2079  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2080  * acceleration.
2081  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2082  */
2083 static boolean_t
2084 ill_enable_promisc_notify(ill_t *ill)
2085 {
2086 	mblk_t *mp;
2087 	dl_notify_req_t *req;
2088 
2089 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2090 
2091 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2092 	if (mp == NULL)
2093 		return (B_FALSE);
2094 
2095 	req = (dl_notify_req_t *)mp->b_rptr;
2096 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2097 	    DL_NOTE_PROMISC_OFF_PHYS;
2098 
2099 	ill_dlpi_send(ill, mp);
2100 
2101 	return (B_TRUE);
2102 }
2103 
2104 
2105 /*
2106  * Allocate an IPsec capability request which will be filled by our
2107  * caller to turn on support for one or more algorithms.
2108  */
2109 static mblk_t *
2110 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2111 {
2112 	mblk_t *nmp;
2113 	dl_capability_req_t	*ocap;
2114 	dl_capab_ipsec_t	*ocip;
2115 	dl_capab_ipsec_t	*icip;
2116 	uint8_t			*ptr;
2117 	icip = (dl_capab_ipsec_t *)(isub + 1);
2118 
2119 	/*
2120 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2121 	 * PROMISC_ON/OFF notification from the provider. We need to
2122 	 * do this before enabling the algorithms to avoid leakage of
2123 	 * cleartext packets.
2124 	 */
2125 
2126 	if (!ill_enable_promisc_notify(ill))
2127 		return (NULL);
2128 
2129 	/*
2130 	 * Allocate new mblk which will contain a new capability
2131 	 * request to enable the capabilities.
2132 	 */
2133 
2134 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2135 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2136 	if (nmp == NULL)
2137 		return (NULL);
2138 
2139 	ptr = nmp->b_rptr;
2140 
2141 	/* initialize dl_capability_req_t */
2142 	ocap = (dl_capability_req_t *)ptr;
2143 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2144 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2145 	ptr += sizeof (dl_capability_req_t);
2146 
2147 	/* initialize dl_capability_sub_t */
2148 	bcopy(isub, ptr, sizeof (*isub));
2149 	ptr += sizeof (*isub);
2150 
2151 	/* initialize dl_capab_ipsec_t */
2152 	ocip = (dl_capab_ipsec_t *)ptr;
2153 	bcopy(icip, ocip, sizeof (*icip));
2154 
2155 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2156 	return (nmp);
2157 }
2158 
2159 /*
2160  * Process an IPsec capability negotiation ack received from a DLS Provider.
2161  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2162  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2163  */
2164 static void
2165 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2166 {
2167 	dl_capab_ipsec_t	*icip;
2168 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2169 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2170 	uint_t cipher, nciphers;
2171 	mblk_t *nmp;
2172 	uint_t alg_len;
2173 	boolean_t need_sadb_dump;
2174 	uint_t sub_dl_cap = isub->dl_cap;
2175 	ill_ipsec_capab_t **ill_capab;
2176 	uint64_t ill_capab_flag;
2177 	uint8_t *capend, *ciphend;
2178 	boolean_t sadb_resync;
2179 
2180 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2181 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2182 
2183 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2184 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2185 		ill_capab_flag = ILL_CAPAB_AH;
2186 	} else {
2187 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2188 		ill_capab_flag = ILL_CAPAB_ESP;
2189 	}
2190 
2191 	/*
2192 	 * If the ill capability structure exists, then this incoming
2193 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2194 	 * If this is so, then we'd need to resynchronize the SADB
2195 	 * after re-enabling the offloaded ciphers.
2196 	 */
2197 	sadb_resync = (*ill_capab != NULL);
2198 
2199 	/*
2200 	 * Note: range checks here are not absolutely sufficient to
2201 	 * make us robust against malformed messages sent by drivers;
2202 	 * this is in keeping with the rest of IP's dlpi handling.
2203 	 * (Remember, it's coming from something else in the kernel
2204 	 * address space)
2205 	 */
2206 
2207 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2208 	if (capend > mp->b_wptr) {
2209 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2210 		    "malformed sub-capability too long for mblk");
2211 		return;
2212 	}
2213 
2214 	/*
2215 	 * There are two types of acks we process here:
2216 	 * 1. acks in reply to a (first form) generic capability req
2217 	 *    (no ENABLE flag set)
2218 	 * 2. acks in reply to a ENABLE capability req.
2219 	 *    (ENABLE flag set)
2220 	 *
2221 	 * We process the subcapability passed as argument as follows:
2222 	 * 1 do initializations
2223 	 *   1.1 initialize nmp = NULL
2224 	 *   1.2 set need_sadb_dump to B_FALSE
2225 	 * 2 for each cipher in subcapability:
2226 	 *   2.1 if ENABLE flag is set:
2227 	 *	2.1.1 update per-ill ipsec capabilities info
2228 	 *	2.1.2 set need_sadb_dump to B_TRUE
2229 	 *   2.2 if ENABLE flag is not set:
2230 	 *	2.2.1 if nmp is NULL:
2231 	 *		2.2.1.1 allocate and initialize nmp
2232 	 *		2.2.1.2 init current pos in nmp
2233 	 *	2.2.2 copy current cipher to current pos in nmp
2234 	 *	2.2.3 set ENABLE flag in nmp
2235 	 *	2.2.4 update current pos
2236 	 * 3 if nmp is not equal to NULL, send enable request
2237 	 *   3.1 send capability request
2238 	 * 4 if need_sadb_dump is B_TRUE
2239 	 *   4.1 enable promiscuous on/off notifications
2240 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2241 	 *	AH or ESP SA's to interface.
2242 	 */
2243 
2244 	nmp = NULL;
2245 	oalg = NULL;
2246 	need_sadb_dump = B_FALSE;
2247 	icip = (dl_capab_ipsec_t *)(isub + 1);
2248 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2249 
2250 	nciphers = icip->cip_nciphers;
2251 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2252 
2253 	if (ciphend > capend) {
2254 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2255 		    "too many ciphers for sub-capability len");
2256 		return;
2257 	}
2258 
2259 	for (cipher = 0; cipher < nciphers; cipher++) {
2260 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2261 
2262 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2263 			/*
2264 			 * TBD: when we provide a way to disable capabilities
2265 			 * from above, need to manage the request-pending state
2266 			 * and fail if we were not expecting this ACK.
2267 			 */
2268 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2269 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2270 
2271 			/*
2272 			 * Update IPsec capabilities for this ill
2273 			 */
2274 
2275 			if (*ill_capab == NULL) {
2276 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2277 				    ("ill_capability_ipsec_ack: "
2278 					"allocating ipsec_capab for ill\n"));
2279 				*ill_capab = ill_ipsec_capab_alloc();
2280 
2281 				if (*ill_capab == NULL) {
2282 					cmn_err(CE_WARN,
2283 					    "ill_capability_ipsec_ack: "
2284 					    "could not enable IPsec Hardware "
2285 					    "acceleration for %s (ENOMEM)\n",
2286 					    ill->ill_name);
2287 					return;
2288 				}
2289 			}
2290 
2291 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2292 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2293 
2294 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2295 				cmn_err(CE_WARN,
2296 				    "ill_capability_ipsec_ack: "
2297 				    "malformed IPsec algorithm id %d",
2298 				    ialg->alg_prim);
2299 				continue;
2300 			}
2301 
2302 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2303 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2304 				    ialg->alg_prim);
2305 			} else {
2306 				ipsec_capab_algparm_t *alp;
2307 
2308 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2309 				    ialg->alg_prim);
2310 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2311 				    ialg->alg_prim)) {
2312 					cmn_err(CE_WARN,
2313 					    "ill_capability_ipsec_ack: "
2314 					    "no space for IPsec alg id %d",
2315 					    ialg->alg_prim);
2316 					continue;
2317 				}
2318 				alp = &((*ill_capab)->encr_algparm[
2319 						ialg->alg_prim]);
2320 				alp->minkeylen = ialg->alg_minbits;
2321 				alp->maxkeylen = ialg->alg_maxbits;
2322 			}
2323 			ill->ill_capabilities |= ill_capab_flag;
2324 			/*
2325 			 * indicate that a capability was enabled, which
2326 			 * will be used below to kick off a SADB dump
2327 			 * to the ill.
2328 			 */
2329 			need_sadb_dump = B_TRUE;
2330 		} else {
2331 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2332 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2333 				ialg->alg_prim));
2334 
2335 			if (nmp == NULL) {
2336 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2337 				if (nmp == NULL) {
2338 					/*
2339 					 * Sending the PROMISC_ON/OFF
2340 					 * notification request failed.
2341 					 * We cannot enable the algorithms
2342 					 * since the Provider will not
2343 					 * notify IP of promiscous mode
2344 					 * changes, which could lead
2345 					 * to leakage of packets.
2346 					 */
2347 					cmn_err(CE_WARN,
2348 					    "ill_capability_ipsec_ack: "
2349 					    "could not enable IPsec Hardware "
2350 					    "acceleration for %s (ENOMEM)\n",
2351 					    ill->ill_name);
2352 					return;
2353 				}
2354 				/* ptr to current output alg specifier */
2355 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2356 			}
2357 
2358 			/*
2359 			 * Copy current alg specifier, set ENABLE
2360 			 * flag, and advance to next output alg.
2361 			 * For now we enable all IPsec capabilities.
2362 			 */
2363 			ASSERT(oalg != NULL);
2364 			bcopy(ialg, oalg, alg_len);
2365 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2366 			nmp->b_wptr += alg_len;
2367 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2368 		}
2369 
2370 		/* move to next input algorithm specifier */
2371 		ialg = (dl_capab_ipsec_alg_t *)
2372 		    ((char *)ialg + alg_len);
2373 	}
2374 
2375 	if (nmp != NULL)
2376 		/*
2377 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2378 		 * IPsec hardware acceleration.
2379 		 */
2380 		ill_dlpi_send(ill, nmp);
2381 
2382 	if (need_sadb_dump)
2383 		/*
2384 		 * An acknowledgement corresponding to a request to
2385 		 * enable acceleration was received, notify SADB.
2386 		 */
2387 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2388 }
2389 
2390 /*
2391  * Given an mblk with enough space in it, create sub-capability entries for
2392  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2393  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2394  * in preparation for the reset the DL_CAPABILITY_REQ message.
2395  */
2396 static void
2397 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2398     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2399 {
2400 	dl_capab_ipsec_t *oipsec;
2401 	dl_capab_ipsec_alg_t *oalg;
2402 	dl_capability_sub_t *dl_subcap;
2403 	int i, k;
2404 
2405 	ASSERT(nciphers > 0);
2406 	ASSERT(ill_cap != NULL);
2407 	ASSERT(mp != NULL);
2408 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2409 
2410 	/* dl_capability_sub_t for "stype" */
2411 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2412 	dl_subcap->dl_cap = stype;
2413 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2414 	mp->b_wptr += sizeof (dl_capability_sub_t);
2415 
2416 	/* dl_capab_ipsec_t for "stype" */
2417 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2418 	oipsec->cip_version = 1;
2419 	oipsec->cip_nciphers = nciphers;
2420 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2421 
2422 	/* create entries for "stype" AUTH ciphers */
2423 	for (i = 0; i < ill_cap->algs_size; i++) {
2424 		for (k = 0; k < BITSPERBYTE; k++) {
2425 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2426 				continue;
2427 
2428 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2429 			bzero((void *)oalg, sizeof (*oalg));
2430 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2431 			oalg->alg_prim = k + (BITSPERBYTE * i);
2432 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2433 		}
2434 	}
2435 	/* create entries for "stype" ENCR ciphers */
2436 	for (i = 0; i < ill_cap->algs_size; i++) {
2437 		for (k = 0; k < BITSPERBYTE; k++) {
2438 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2439 				continue;
2440 
2441 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2442 			bzero((void *)oalg, sizeof (*oalg));
2443 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2444 			oalg->alg_prim = k + (BITSPERBYTE * i);
2445 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2446 		}
2447 	}
2448 }
2449 
2450 /*
2451  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2452  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2453  * POPC instruction, but our macro is more flexible for an arbitrary length
2454  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2455  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2456  * stays that way, we can reduce the number of iterations required.
2457  */
2458 #define	COUNT_1S(val, sum) {					\
2459 	uint8_t x = val & 0xff;					\
2460 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2461 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2462 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2463 }
2464 
2465 /* ARGSUSED */
2466 static void
2467 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2468 {
2469 	mblk_t *mp;
2470 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2471 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2472 	uint64_t ill_capabilities = ill->ill_capabilities;
2473 	int ah_cnt = 0, esp_cnt = 0;
2474 	int ah_len = 0, esp_len = 0;
2475 	int i, size = 0;
2476 
2477 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2478 		return;
2479 
2480 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2481 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2482 
2483 	/* Find out the number of ciphers for AH */
2484 	if (cap_ah != NULL) {
2485 		for (i = 0; i < cap_ah->algs_size; i++) {
2486 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2487 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2488 		}
2489 		if (ah_cnt > 0) {
2490 			size += sizeof (dl_capability_sub_t) +
2491 			    sizeof (dl_capab_ipsec_t);
2492 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2493 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2494 			size += ah_len;
2495 		}
2496 	}
2497 
2498 	/* Find out the number of ciphers for ESP */
2499 	if (cap_esp != NULL) {
2500 		for (i = 0; i < cap_esp->algs_size; i++) {
2501 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2502 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2503 		}
2504 		if (esp_cnt > 0) {
2505 			size += sizeof (dl_capability_sub_t) +
2506 			    sizeof (dl_capab_ipsec_t);
2507 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2508 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2509 			size += esp_len;
2510 		}
2511 	}
2512 
2513 	if (size == 0) {
2514 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2515 		    "there's nothing to reset\n"));
2516 		return;
2517 	}
2518 
2519 	mp = allocb(size, BPRI_HI);
2520 	if (mp == NULL) {
2521 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2522 		    "request to disable IPSEC Hardware Acceleration\n"));
2523 		return;
2524 	}
2525 
2526 	/*
2527 	 * Clear the capability flags for IPSec HA but retain the ill
2528 	 * capability structures since it's possible that another thread
2529 	 * is still referring to them.  The structures only get deallocated
2530 	 * when we destroy the ill.
2531 	 *
2532 	 * Various places check the flags to see if the ill is capable of
2533 	 * hardware acceleration, and by clearing them we ensure that new
2534 	 * outbound IPSec packets are sent down encrypted.
2535 	 */
2536 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2537 
2538 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2539 	if (ah_cnt > 0) {
2540 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2541 		    cap_ah, mp);
2542 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2543 	}
2544 
2545 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2546 	if (esp_cnt > 0) {
2547 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2548 		    cap_esp, mp);
2549 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2550 	}
2551 
2552 	/*
2553 	 * At this point we've composed a bunch of sub-capabilities to be
2554 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2555 	 * by the caller.  Upon receiving this reset message, the driver
2556 	 * must stop inbound decryption (by destroying all inbound SAs)
2557 	 * and let the corresponding packets come in encrypted.
2558 	 */
2559 
2560 	if (*sc_mp != NULL)
2561 		linkb(*sc_mp, mp);
2562 	else
2563 		*sc_mp = mp;
2564 }
2565 
2566 static void
2567 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2568     boolean_t encapsulated)
2569 {
2570 	boolean_t legacy = B_FALSE;
2571 
2572 	/*
2573 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2574 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2575 	 * instructed the driver to disable its advertised capabilities,
2576 	 * so there's no point in accepting any response at this moment.
2577 	 */
2578 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2579 		return;
2580 
2581 	/*
2582 	 * Note that only the following two sub-capabilities may be
2583 	 * considered as "legacy", since their original definitions
2584 	 * do not incorporate the dl_mid_t module ID token, and hence
2585 	 * may require the use of the wrapper sub-capability.
2586 	 */
2587 	switch (subp->dl_cap) {
2588 	case DL_CAPAB_IPSEC_AH:
2589 	case DL_CAPAB_IPSEC_ESP:
2590 		legacy = B_TRUE;
2591 		break;
2592 	}
2593 
2594 	/*
2595 	 * For legacy sub-capabilities which don't incorporate a queue_t
2596 	 * pointer in their structures, discard them if we detect that
2597 	 * there are intermediate modules in between IP and the driver.
2598 	 */
2599 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2600 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2601 		    "%d discarded; %d module(s) present below IP\n",
2602 		    subp->dl_cap, ill->ill_lmod_cnt));
2603 		return;
2604 	}
2605 
2606 	switch (subp->dl_cap) {
2607 	case DL_CAPAB_IPSEC_AH:
2608 	case DL_CAPAB_IPSEC_ESP:
2609 		ill_capability_ipsec_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_MDT:
2612 		ill_capability_mdt_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_HCKSUM:
2615 		ill_capability_hcksum_ack(ill, mp, subp);
2616 		break;
2617 	case DL_CAPAB_ZEROCOPY:
2618 		ill_capability_zerocopy_ack(ill, mp, subp);
2619 		break;
2620 	case DL_CAPAB_POLL:
2621 		ill_capability_poll_ack(ill, mp, subp);
2622 		break;
2623 	default:
2624 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2625 		    subp->dl_cap));
2626 	}
2627 }
2628 
2629 /*
2630  * As part of negotiating polling capability, the driver tells us
2631  * the default (or normal) blanking interval and packet threshold
2632  * (the receive timer fires if blanking interval is reached or
2633  * the packet threshold is reached).
2634  *
2635  * As part of manipulating the polling interval, we always use our
2636  * estimated interval (avg service time * number of packets queued
2637  * on the squeue) but we try to blank for a minimum of
2638  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2639  * packet threshold during this time. When we are not in polling mode
2640  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2641  * rr_min_blank_ratio but up the packet cnt by a ratio of
2642  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2643  * possible although for a shorter interval.
2644  */
2645 #define	RR_MAX_BLANK_RATIO	20
2646 #define	RR_MIN_BLANK_RATIO	10
2647 #define	RR_MAX_PKT_CNT_RATIO	3
2648 #define	RR_MIN_PKT_CNT_RATIO	3
2649 
2650 /*
2651  * These can be tuned via /etc/system.
2652  */
2653 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2654 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2655 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2656 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2657 
2658 static mac_resource_handle_t
2659 ill_ring_add(void *arg, mac_resource_t *mrp)
2660 {
2661 	ill_t			*ill = (ill_t *)arg;
2662 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2663 	ill_rx_ring_t		*rx_ring;
2664 	int			ip_rx_index;
2665 
2666 	if (mrp->mr_type != MAC_RX_FIFO) {
2667 		return (NULL);
2668 	}
2669 	ASSERT(ill != NULL);
2670 	ASSERT(ill->ill_poll_capab != NULL);
2671 	ASSERT(mrp != NULL);
2672 
2673 	mutex_enter(&ill->ill_lock);
2674 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2675 		rx_ring = &ill->ill_poll_capab->ill_ring_tbl[ip_rx_index];
2676 		ASSERT(rx_ring != NULL);
2677 
2678 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2679 			time_t normal_blank_time =
2680 			    mrfp->mrf_normal_blank_time;
2681 			uint_t normal_pkt_cnt =
2682 			    mrfp->mrf_normal_pkt_count;
2683 
2684 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2685 
2686 			rx_ring->rr_blank = mrfp->mrf_blank;
2687 			rx_ring->rr_handle = mrfp->mrf_arg;
2688 			rx_ring->rr_ill = ill;
2689 			rx_ring->rr_normal_blank_time = normal_blank_time;
2690 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2691 
2692 			rx_ring->rr_max_blank_time =
2693 			    normal_blank_time * rr_max_blank_ratio;
2694 			rx_ring->rr_min_blank_time =
2695 			    normal_blank_time * rr_min_blank_ratio;
2696 			rx_ring->rr_max_pkt_cnt =
2697 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2698 			rx_ring->rr_min_pkt_cnt =
2699 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2700 
2701 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2702 			mutex_exit(&ill->ill_lock);
2703 
2704 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2705 			    (int), ip_rx_index);
2706 			return ((mac_resource_handle_t)rx_ring);
2707 		}
2708 	}
2709 
2710 	/*
2711 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2712 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2713 	 * should be made configurable. Meanwhile it cause no panic because
2714 	 * driver will pass ip_input a NULL handle which will make
2715 	 * IP allocate the default squeue and Polling mode will not
2716 	 * be used for this ring.
2717 	 */
2718 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2719 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2720 
2721 	mutex_exit(&ill->ill_lock);
2722 	return (NULL);
2723 }
2724 
2725 static boolean_t
2726 ill_capability_poll_init(ill_t *ill)
2727 {
2728 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2729 	conn_t 			*connp;
2730 	size_t			sz;
2731 
2732 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2733 		if (ill_poll == NULL) {
2734 			cmn_err(CE_PANIC, "ill_capability_poll_init: "
2735 			    "polling enabled for ill=%s (%p) but data "
2736 			    "structs uninitialized\n", ill->ill_name,
2737 			    (void *)ill);
2738 		}
2739 		return (B_TRUE);
2740 	}
2741 
2742 	if (ill_poll != NULL) {
2743 		ill_rx_ring_t 	*rx_ring = ill_poll->ill_ring_tbl;
2744 		/* Polling is being re-enabled */
2745 
2746 		connp = ill_poll->ill_unbind_conn;
2747 		ASSERT(rx_ring != NULL);
2748 		bzero((void *)ill_poll, sizeof (ill_poll_capab_t));
2749 		bzero((void *)rx_ring,
2750 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2751 		ill_poll->ill_ring_tbl = rx_ring;
2752 		ill_poll->ill_unbind_conn = connp;
2753 		return (B_TRUE);
2754 	}
2755 
2756 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2757 		return (B_FALSE);
2758 
2759 	sz = sizeof (ill_poll_capab_t);
2760 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2761 
2762 	ill_poll = kmem_zalloc(sz, KM_NOSLEEP);
2763 	if (ill_poll == NULL) {
2764 		cmn_err(CE_WARN, "ill_capability_poll_init: could not "
2765 		    "allocate poll_capab for %s (%p)\n", ill->ill_name,
2766 		    (void *)ill);
2767 		CONN_DEC_REF(connp);
2768 		return (B_FALSE);
2769 	}
2770 
2771 	/* Allocate space to hold ring table */
2772 	ill_poll->ill_ring_tbl = (ill_rx_ring_t *)&ill_poll[1];
2773 	ill->ill_poll_capab = ill_poll;
2774 	ill_poll->ill_unbind_conn = connp;
2775 	return (B_TRUE);
2776 }
2777 
2778 /*
2779  * ill_capability_poll_disable: disable polling capability. Since
2780  * any of the rings might already be in use, need to call ipsq_clean_all()
2781  * which gets behind the squeue to disable direct calls if necessary.
2782  * Clean up the direct tx function pointers as well.
2783  */
2784 static void
2785 ill_capability_poll_disable(ill_t *ill)
2786 {
2787 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2788 
2789 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2790 		ipsq_clean_all(ill);
2791 		ill_poll->ill_tx = NULL;
2792 		ill_poll->ill_tx_handle = NULL;
2793 	}
2794 
2795 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
2796 }
2797 
2798 static void
2799 ill_capability_poll_capable(ill_t *ill, dl_capab_poll_t *ipoll,
2800     dl_capability_sub_t *isub)
2801 {
2802 	uint_t			size;
2803 	uchar_t			*rptr;
2804 	dl_capab_poll_t		poll, *opoll;
2805 	ill_poll_capab_t	*ill_poll;
2806 	mblk_t			*nmp = NULL;
2807 	dl_capability_req_t	*ocap;
2808 
2809 	if (!ill_capability_poll_init(ill))
2810 		return;
2811 	ill_poll = ill->ill_poll_capab;
2812 
2813 	/* Copy locally to get the members aligned */
2814 	bcopy((void *)ipoll, (void *)&poll, sizeof (dl_capab_poll_t));
2815 
2816 	/* Get the tx function and handle from dld */
2817 	ill_poll->ill_tx = (ip_dld_tx_t)poll.poll_tx;
2818 	ill_poll->ill_tx_handle = (void *)poll.poll_tx_handle;
2819 
2820 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2821 	    isub->dl_length;
2822 
2823 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2824 		cmn_err(CE_WARN, "ill_capability_poll_ack: could not allocate "
2825 		    "memory for CAPAB_REQ for %s (%p)\n", ill->ill_name,
2826 		    (void *)ill);
2827 		return;
2828 	}
2829 
2830 	/* initialize dl_capability_req_t */
2831 	rptr = nmp->b_rptr;
2832 	ocap = (dl_capability_req_t *)rptr;
2833 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2834 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2835 	rptr += sizeof (dl_capability_req_t);
2836 
2837 	/* initialize dl_capability_sub_t */
2838 	bcopy(isub, rptr, sizeof (*isub));
2839 	rptr += sizeof (*isub);
2840 
2841 	opoll = (dl_capab_poll_t *)rptr;
2842 	rptr += sizeof (dl_capab_poll_t);
2843 
2844 	/* initialize dl_capab_poll_t to be sent down */
2845 	poll.poll_rx_handle = (uintptr_t)ill;
2846 	poll.poll_rx = (uintptr_t)ip_input;
2847 	poll.poll_ring_add = (uintptr_t)ill_ring_add;
2848 	poll.poll_flags = POLL_ENABLE;
2849 	bcopy((void *)&poll, (void *)opoll, sizeof (dl_capab_poll_t));
2850 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2851 
2852 	/* nmp points to a DL_CAPABILITY_REQ message to enable polling */
2853 	ill_dlpi_send(ill, nmp);
2854 }
2855 
2856 
2857 /*
2858  * Process a polling capability negotiation ack received
2859  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_POLL)
2860  * of a DL_CAPABILITY_ACK message.
2861  */
2862 static void
2863 ill_capability_poll_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2864 {
2865 	dl_capab_poll_t		*ipoll;
2866 	uint_t			sub_dl_cap = isub->dl_cap;
2867 	uint8_t			*capend;
2868 
2869 
2870 	ASSERT(sub_dl_cap == DL_CAPAB_POLL);
2871 
2872 	/*
2873 	 * Don't enable polling for ipv6 ill's
2874 	 */
2875 	if (ill->ill_isv6) {
2876 		return;
2877 	}
2878 
2879 	/*
2880 	 * Note: range checks here are not absolutely sufficient to
2881 	 * make us robust against malformed messages sent by drivers;
2882 	 * this is in keeping with the rest of IP's dlpi handling.
2883 	 * (Remember, it's coming from something else in the kernel
2884 	 * address space)
2885 	 */
2886 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2887 	if (capend > mp->b_wptr) {
2888 		cmn_err(CE_WARN, "ill_capability_poll_ack: "
2889 		    "malformed sub-capability too long for mblk");
2890 		return;
2891 	}
2892 
2893 	/*
2894 	 * There are two types of acks we process here:
2895 	 * 1. acks in reply to a (first form) generic capability req
2896 	 *    (poll_flag will be set to POLL_CAPABLE)
2897 	 * 2. acks in reply to a POLL_ENABLE capability req.
2898 	 *    (POLL_ENABLE flag set)
2899 	 */
2900 	ipoll = (dl_capab_poll_t *)(isub + 1);
2901 
2902 	if (!dlcapabcheckqid(&ipoll->poll_mid, ill->ill_lmod_rq)) {
2903 		ip1dbg(("ill_capability_poll_ack: mid token for polling "
2904 		    "capability isn't as expected; pass-thru "
2905 		    "module(s) detected, discarding capability\n"));
2906 		if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2907 			/*
2908 			 * This is a capability renegotitation case.
2909 			 * The interface better be unusable at this
2910 			 * point other wise bad things will happen
2911 			 * if we disable direct calls on a running
2912 			 * and up interface.
2913 			 */
2914 			ill_capability_poll_disable(ill);
2915 		}
2916 		return;
2917 	}
2918 
2919 	switch (ipoll->poll_flags) {
2920 	default:
2921 		/* Disable if unknown flag */
2922 	case POLL_DISABLE:
2923 		ill_capability_poll_disable(ill);
2924 		break;
2925 	case POLL_CAPABLE:
2926 		/*
2927 		 * If the capability was already enabled, its safe
2928 		 * to disable it first to get rid of stale information
2929 		 * and then start enabling it again.
2930 		 */
2931 		ill_capability_poll_disable(ill);
2932 		ill_capability_poll_capable(ill, ipoll, isub);
2933 		break;
2934 	case POLL_ENABLE:
2935 		if (!(ill->ill_capabilities & ILL_CAPAB_POLL)) {
2936 			ASSERT(ill->ill_poll_capab != NULL);
2937 			ill->ill_capabilities |= ILL_CAPAB_POLL;
2938 		}
2939 		break;
2940 	}
2941 }
2942 
2943 static void
2944 ill_capability_poll_reset(ill_t *ill, mblk_t **sc_mp)
2945 {
2946 	mblk_t *mp;
2947 	dl_capab_poll_t *ipoll;
2948 	dl_capability_sub_t *dl_subcap;
2949 	int size;
2950 
2951 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
2952 		return;
2953 
2954 	ASSERT(ill->ill_poll_capab != NULL);
2955 
2956 	/*
2957 	 * Disable polling capability
2958 	 */
2959 	ill_capability_poll_disable(ill);
2960 
2961 	size = sizeof (*dl_subcap) + sizeof (*ipoll);
2962 
2963 	mp = allocb(size, BPRI_HI);
2964 	if (mp == NULL) {
2965 		ip1dbg(("ill_capability_poll_reset: unable to allocate "
2966 		    "request to disable polling\n"));
2967 		return;
2968 	}
2969 
2970 	mp->b_wptr = mp->b_rptr + size;
2971 
2972 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2973 	dl_subcap->dl_cap = DL_CAPAB_POLL;
2974 	dl_subcap->dl_length = sizeof (*ipoll);
2975 
2976 	ipoll = (dl_capab_poll_t *)(dl_subcap + 1);
2977 	ipoll->poll_flags = POLL_DISABLE;
2978 
2979 	if (*sc_mp != NULL)
2980 		linkb(*sc_mp, mp);
2981 	else
2982 		*sc_mp = mp;
2983 }
2984 
2985 
2986 /*
2987  * Process a hardware checksum offload capability negotiation ack received
2988  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2989  * of a DL_CAPABILITY_ACK message.
2990  */
2991 static void
2992 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2993 {
2994 	dl_capability_req_t	*ocap;
2995 	dl_capab_hcksum_t	*ihck, *ohck;
2996 	ill_hcksum_capab_t	**ill_hcksum;
2997 	mblk_t			*nmp = NULL;
2998 	uint_t			sub_dl_cap = isub->dl_cap;
2999 	uint8_t			*capend;
3000 
3001 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3002 
3003 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3004 
3005 	/*
3006 	 * Note: range checks here are not absolutely sufficient to
3007 	 * make us robust against malformed messages sent by drivers;
3008 	 * this is in keeping with the rest of IP's dlpi handling.
3009 	 * (Remember, it's coming from something else in the kernel
3010 	 * address space)
3011 	 */
3012 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3013 	if (capend > mp->b_wptr) {
3014 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3015 		    "malformed sub-capability too long for mblk");
3016 		return;
3017 	}
3018 
3019 	/*
3020 	 * There are two types of acks we process here:
3021 	 * 1. acks in reply to a (first form) generic capability req
3022 	 *    (no ENABLE flag set)
3023 	 * 2. acks in reply to a ENABLE capability req.
3024 	 *    (ENABLE flag set)
3025 	 */
3026 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3027 
3028 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3029 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3030 		    "unsupported hardware checksum "
3031 		    "sub-capability (version %d, expected %d)",
3032 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3033 		return;
3034 	}
3035 
3036 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3037 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3038 		    "checksum capability isn't as expected; pass-thru "
3039 		    "module(s) detected, discarding capability\n"));
3040 		return;
3041 	}
3042 
3043 #define	CURR_HCKSUM_CAPAB \
3044 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM)
3045 
3046 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3047 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3048 		/* do ENABLE processing */
3049 		if (*ill_hcksum == NULL) {
3050 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3051 			    KM_NOSLEEP);
3052 
3053 			if (*ill_hcksum == NULL) {
3054 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3055 				    "could not enable hcksum version %d "
3056 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3057 				    ill->ill_name);
3058 				return;
3059 			}
3060 		}
3061 
3062 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3063 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3064 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3065 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3066 		    "has enabled hardware checksumming\n ",
3067 		    ill->ill_name));
3068 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3069 		/*
3070 		 * Enabling hardware checksum offload
3071 		 * Currently IP supports {TCP,UDP}/IPv4
3072 		 * partial and full cksum offload and
3073 		 * IPv4 header checksum offload.
3074 		 * Allocate new mblk which will
3075 		 * contain a new capability request
3076 		 * to enable hardware checksum offload.
3077 		 */
3078 		uint_t	size;
3079 		uchar_t	*rptr;
3080 
3081 		size = sizeof (dl_capability_req_t) +
3082 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3083 
3084 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3085 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3086 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3087 			    ill->ill_name);
3088 			return;
3089 		}
3090 
3091 		rptr = nmp->b_rptr;
3092 		/* initialize dl_capability_req_t */
3093 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3094 		ocap->dl_sub_offset =
3095 		    sizeof (dl_capability_req_t);
3096 		ocap->dl_sub_length =
3097 		    sizeof (dl_capability_sub_t) +
3098 		    isub->dl_length;
3099 		nmp->b_rptr += sizeof (dl_capability_req_t);
3100 
3101 		/* initialize dl_capability_sub_t */
3102 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3103 		nmp->b_rptr += sizeof (*isub);
3104 
3105 		/* initialize dl_capab_hcksum_t */
3106 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3107 		bcopy(ihck, ohck, sizeof (*ihck));
3108 
3109 		nmp->b_rptr = rptr;
3110 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3111 
3112 		/* Set ENABLE flag */
3113 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3114 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3115 
3116 		/*
3117 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3118 		 * hardware checksum acceleration.
3119 		 */
3120 		ill_dlpi_send(ill, nmp);
3121 	} else
3122 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3123 		    "advertised %x hardware checksum capability flags\n",
3124 		    ill->ill_name, ihck->hcksum_txflags));
3125 }
3126 
3127 static void
3128 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3129 {
3130 	mblk_t *mp;
3131 	dl_capab_hcksum_t *hck_subcap;
3132 	dl_capability_sub_t *dl_subcap;
3133 	int size;
3134 
3135 	if (!(ill->ill_capabilities & ILL_CAPAB_HCKSUM))
3136 		return;
3137 
3138 	ASSERT(ill->ill_hcksum_capab != NULL);
3139 	/*
3140 	 * Clear the capability flag for hardware checksum offload but
3141 	 * retain the ill_hcksum_capab structure since it's possible that
3142 	 * another thread is still referring to it.  The structure only
3143 	 * gets deallocated when we destroy the ill.
3144 	 */
3145 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3146 
3147 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3148 
3149 	mp = allocb(size, BPRI_HI);
3150 	if (mp == NULL) {
3151 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3152 		    "request to disable hardware checksum offload\n"));
3153 		return;
3154 	}
3155 
3156 	mp->b_wptr = mp->b_rptr + size;
3157 
3158 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3159 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3160 	dl_subcap->dl_length = sizeof (*hck_subcap);
3161 
3162 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3163 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3164 	hck_subcap->hcksum_txflags = 0;
3165 
3166 	if (*sc_mp != NULL)
3167 		linkb(*sc_mp, mp);
3168 	else
3169 		*sc_mp = mp;
3170 }
3171 
3172 static void
3173 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3174 {
3175 	mblk_t *nmp = NULL;
3176 	dl_capability_req_t *oc;
3177 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3178 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3179 	uint_t sub_dl_cap = isub->dl_cap;
3180 	uint8_t *capend;
3181 
3182 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3183 
3184 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3185 
3186 	/*
3187 	 * Note: range checks here are not absolutely sufficient to
3188 	 * make us robust against malformed messages sent by drivers;
3189 	 * this is in keeping with the rest of IP's dlpi handling.
3190 	 * (Remember, it's coming from something else in the kernel
3191 	 * address space)
3192 	 */
3193 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3194 	if (capend > mp->b_wptr) {
3195 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3196 		    "malformed sub-capability too long for mblk");
3197 		return;
3198 	}
3199 
3200 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3201 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3202 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3203 		    "unsupported ZEROCOPY sub-capability (version %d, "
3204 		    "expected %d)", zc_ic->zerocopy_version,
3205 		    ZEROCOPY_VERSION_1);
3206 		return;
3207 	}
3208 
3209 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3210 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3211 		    "capability isn't as expected; pass-thru module(s) "
3212 		    "detected, discarding capability\n"));
3213 		return;
3214 	}
3215 
3216 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3217 		if (*ill_zerocopy_capab == NULL) {
3218 			*ill_zerocopy_capab =
3219 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3220 			    KM_NOSLEEP);
3221 
3222 			if (*ill_zerocopy_capab == NULL) {
3223 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3224 				    "could not enable Zero-copy version %d "
3225 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3226 				    ill->ill_name);
3227 				return;
3228 			}
3229 		}
3230 
3231 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3232 		    "supports Zero-copy version %d\n", ill->ill_name,
3233 		    ZEROCOPY_VERSION_1));
3234 
3235 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3236 		    zc_ic->zerocopy_version;
3237 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3238 		    zc_ic->zerocopy_flags;
3239 
3240 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3241 	} else {
3242 		uint_t size;
3243 		uchar_t *rptr;
3244 
3245 		size = sizeof (dl_capability_req_t) +
3246 		    sizeof (dl_capability_sub_t) +
3247 		    sizeof (dl_capab_zerocopy_t);
3248 
3249 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3250 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3251 			    "could not enable zerocopy for %s (ENOMEM)\n",
3252 			    ill->ill_name);
3253 			return;
3254 		}
3255 
3256 		rptr = nmp->b_rptr;
3257 		/* initialize dl_capability_req_t */
3258 		oc = (dl_capability_req_t *)rptr;
3259 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3260 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3261 		    sizeof (dl_capab_zerocopy_t);
3262 		rptr += sizeof (dl_capability_req_t);
3263 
3264 		/* initialize dl_capability_sub_t */
3265 		bcopy(isub, rptr, sizeof (*isub));
3266 		rptr += sizeof (*isub);
3267 
3268 		/* initialize dl_capab_zerocopy_t */
3269 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3270 		*zc_oc = *zc_ic;
3271 
3272 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3273 		    "to enable zero-copy version %d\n", ill->ill_name,
3274 		    ZEROCOPY_VERSION_1));
3275 
3276 		/* set VMSAFE_MEM flag */
3277 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3278 
3279 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3280 		ill_dlpi_send(ill, nmp);
3281 	}
3282 }
3283 
3284 static void
3285 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3286 {
3287 	mblk_t *mp;
3288 	dl_capab_zerocopy_t *zerocopy_subcap;
3289 	dl_capability_sub_t *dl_subcap;
3290 	int size;
3291 
3292 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3293 		return;
3294 
3295 	ASSERT(ill->ill_zerocopy_capab != NULL);
3296 	/*
3297 	 * Clear the capability flag for Zero-copy but retain the
3298 	 * ill_zerocopy_capab structure since it's possible that another
3299 	 * thread is still referring to it.  The structure only gets
3300 	 * deallocated when we destroy the ill.
3301 	 */
3302 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3303 
3304 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3305 
3306 	mp = allocb(size, BPRI_HI);
3307 	if (mp == NULL) {
3308 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3309 		    "request to disable Zero-copy\n"));
3310 		return;
3311 	}
3312 
3313 	mp->b_wptr = mp->b_rptr + size;
3314 
3315 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3316 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3317 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3318 
3319 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3320 	zerocopy_subcap->zerocopy_version =
3321 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3322 	zerocopy_subcap->zerocopy_flags = 0;
3323 
3324 	if (*sc_mp != NULL)
3325 		linkb(*sc_mp, mp);
3326 	else
3327 		*sc_mp = mp;
3328 }
3329 
3330 /*
3331  * Consume a new-style hardware capabilities negotiation ack.
3332  * Called from ip_rput_dlpi_writer().
3333  */
3334 void
3335 ill_capability_ack(ill_t *ill, mblk_t *mp)
3336 {
3337 	dl_capability_ack_t *capp;
3338 	dl_capability_sub_t *subp, *endp;
3339 
3340 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3341 		ill->ill_capab_state = IDMS_OK;
3342 
3343 	capp = (dl_capability_ack_t *)mp->b_rptr;
3344 
3345 	if (capp->dl_sub_length == 0)
3346 		/* no new-style capabilities */
3347 		return;
3348 
3349 	/* make sure the driver supplied correct dl_sub_length */
3350 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3351 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3352 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3353 		return;
3354 	}
3355 
3356 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3357 	/*
3358 	 * There are sub-capabilities. Process the ones we know about.
3359 	 * Loop until we don't have room for another sub-cap header..
3360 	 */
3361 	for (subp = SC(capp, capp->dl_sub_offset),
3362 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3363 	    subp <= endp;
3364 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3365 
3366 		switch (subp->dl_cap) {
3367 		case DL_CAPAB_ID_WRAPPER:
3368 			ill_capability_id_ack(ill, mp, subp);
3369 			break;
3370 		default:
3371 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3372 			break;
3373 		}
3374 	}
3375 #undef SC
3376 }
3377 
3378 /*
3379  * This routine is called to scan the fragmentation reassembly table for
3380  * the specified ILL for any packets that are starting to smell.
3381  * dead_interval is the maximum time in seconds that will be tolerated.  It
3382  * will either be the value specified in ip_g_frag_timeout, or zero if the
3383  * ILL is shutting down and it is time to blow everything off.
3384  *
3385  * It returns the number of seconds (as a time_t) that the next frag timer
3386  * should be scheduled for, 0 meaning that the timer doesn't need to be
3387  * re-started.  Note that the method of calculating next_timeout isn't
3388  * entirely accurate since time will flow between the time we grab
3389  * current_time and the time we schedule the next timeout.  This isn't a
3390  * big problem since this is the timer for sending an ICMP reassembly time
3391  * exceeded messages, and it doesn't have to be exactly accurate.
3392  *
3393  * This function is
3394  * sometimes called as writer, although this is not required.
3395  */
3396 time_t
3397 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3398 {
3399 	ipfb_t	*ipfb;
3400 	ipfb_t	*endp;
3401 	ipf_t	*ipf;
3402 	ipf_t	*ipfnext;
3403 	mblk_t	*mp;
3404 	time_t	current_time = gethrestime_sec();
3405 	time_t	next_timeout = 0;
3406 	uint32_t	hdr_length;
3407 	mblk_t	*send_icmp_head;
3408 	mblk_t	*send_icmp_head_v6;
3409 
3410 	ipfb = ill->ill_frag_hash_tbl;
3411 	if (ipfb == NULL)
3412 		return (B_FALSE);
3413 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3414 	/* Walk the frag hash table. */
3415 	for (; ipfb < endp; ipfb++) {
3416 		send_icmp_head = NULL;
3417 		send_icmp_head_v6 = NULL;
3418 		mutex_enter(&ipfb->ipfb_lock);
3419 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3420 			time_t frag_time = current_time - ipf->ipf_timestamp;
3421 			time_t frag_timeout;
3422 
3423 			if (frag_time < dead_interval) {
3424 				/*
3425 				 * There are some outstanding fragments
3426 				 * that will timeout later.  Make note of
3427 				 * the time so that we can reschedule the
3428 				 * next timeout appropriately.
3429 				 */
3430 				frag_timeout = dead_interval - frag_time;
3431 				if (next_timeout == 0 ||
3432 				    frag_timeout < next_timeout) {
3433 					next_timeout = frag_timeout;
3434 				}
3435 				break;
3436 			}
3437 			/* Time's up.  Get it out of here. */
3438 			hdr_length = ipf->ipf_nf_hdr_len;
3439 			ipfnext = ipf->ipf_hash_next;
3440 			if (ipfnext)
3441 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3442 			*ipf->ipf_ptphn = ipfnext;
3443 			mp = ipf->ipf_mp->b_cont;
3444 			for (; mp; mp = mp->b_cont) {
3445 				/* Extra points for neatness. */
3446 				IP_REASS_SET_START(mp, 0);
3447 				IP_REASS_SET_END(mp, 0);
3448 			}
3449 			mp = ipf->ipf_mp->b_cont;
3450 			ill->ill_frag_count -= ipf->ipf_count;
3451 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3452 			ipfb->ipfb_count -= ipf->ipf_count;
3453 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3454 			ipfb->ipfb_frag_pkts--;
3455 			/*
3456 			 * We do not send any icmp message from here because
3457 			 * we currently are holding the ipfb_lock for this
3458 			 * hash chain. If we try and send any icmp messages
3459 			 * from here we may end up via a put back into ip
3460 			 * trying to get the same lock, causing a recursive
3461 			 * mutex panic. Instead we build a list and send all
3462 			 * the icmp messages after we have dropped the lock.
3463 			 */
3464 			if (ill->ill_isv6) {
3465 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3466 				if (hdr_length != 0) {
3467 					mp->b_next = send_icmp_head_v6;
3468 					send_icmp_head_v6 = mp;
3469 				} else {
3470 					freemsg(mp);
3471 				}
3472 			} else {
3473 				BUMP_MIB(&ip_mib, ipReasmFails);
3474 				if (hdr_length != 0) {
3475 					mp->b_next = send_icmp_head;
3476 					send_icmp_head = mp;
3477 				} else {
3478 					freemsg(mp);
3479 				}
3480 			}
3481 			freeb(ipf->ipf_mp);
3482 		}
3483 		mutex_exit(&ipfb->ipfb_lock);
3484 		/*
3485 		 * Now need to send any icmp messages that we delayed from
3486 		 * above.
3487 		 */
3488 		while (send_icmp_head_v6 != NULL) {
3489 			mp = send_icmp_head_v6;
3490 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3491 			mp->b_next = NULL;
3492 			icmp_time_exceeded_v6(ill->ill_wq, mp,
3493 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, B_FALSE);
3494 		}
3495 		while (send_icmp_head != NULL) {
3496 			mp = send_icmp_head;
3497 			send_icmp_head = send_icmp_head->b_next;
3498 			mp->b_next = NULL;
3499 			icmp_time_exceeded(ill->ill_wq, mp,
3500 			    ICMP_REASSEMBLY_TIME_EXCEEDED);
3501 		}
3502 	}
3503 	/*
3504 	 * A non-dying ILL will use the return value to decide whether to
3505 	 * restart the frag timer, and for how long.
3506 	 */
3507 	return (next_timeout);
3508 }
3509 
3510 /*
3511  * This routine is called when the approximate count of mblk memory used
3512  * for the specified ILL has exceeded max_count.
3513  */
3514 void
3515 ill_frag_prune(ill_t *ill, uint_t max_count)
3516 {
3517 	ipfb_t	*ipfb;
3518 	ipf_t	*ipf;
3519 	size_t	count;
3520 
3521 	/*
3522 	 * If we are here within ip_min_frag_prune_time msecs remove
3523 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3524 	 * ill_frag_free_num_pkts.
3525 	 */
3526 	mutex_enter(&ill->ill_lock);
3527 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3528 	    (ip_min_frag_prune_time != 0 ?
3529 	    ip_min_frag_prune_time : msec_per_tick)) {
3530 
3531 		ill->ill_frag_free_num_pkts++;
3532 
3533 	} else {
3534 		ill->ill_frag_free_num_pkts = 0;
3535 	}
3536 	ill->ill_last_frag_clean_time = lbolt;
3537 	mutex_exit(&ill->ill_lock);
3538 
3539 	/*
3540 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3541 	 */
3542 	if (ill->ill_frag_free_num_pkts != 0) {
3543 		int ix;
3544 
3545 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3546 			ipfb = &ill->ill_frag_hash_tbl[ix];
3547 			mutex_enter(&ipfb->ipfb_lock);
3548 			if (ipfb->ipfb_ipf != NULL) {
3549 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3550 				    ill->ill_frag_free_num_pkts);
3551 			}
3552 			mutex_exit(&ipfb->ipfb_lock);
3553 		}
3554 	}
3555 	/*
3556 	 * While the reassembly list for this ILL is too big, prune a fragment
3557 	 * queue by age, oldest first.  Note that the per ILL count is
3558 	 * approximate, while the per frag hash bucket counts are accurate.
3559 	 */
3560 	while (ill->ill_frag_count > max_count) {
3561 		int	ix;
3562 		ipfb_t	*oipfb = NULL;
3563 		uint_t	oldest = UINT_MAX;
3564 
3565 		count = 0;
3566 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3567 			ipfb = &ill->ill_frag_hash_tbl[ix];
3568 			mutex_enter(&ipfb->ipfb_lock);
3569 			ipf = ipfb->ipfb_ipf;
3570 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3571 				oldest = ipf->ipf_gen;
3572 				oipfb = ipfb;
3573 			}
3574 			count += ipfb->ipfb_count;
3575 			mutex_exit(&ipfb->ipfb_lock);
3576 		}
3577 		/* Refresh the per ILL count */
3578 		ill->ill_frag_count = count;
3579 		if (oipfb == NULL) {
3580 			ill->ill_frag_count = 0;
3581 			break;
3582 		}
3583 		if (count <= max_count)
3584 			return;	/* Somebody beat us to it, nothing to do */
3585 		mutex_enter(&oipfb->ipfb_lock);
3586 		ipf = oipfb->ipfb_ipf;
3587 		if (ipf != NULL) {
3588 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3589 		}
3590 		mutex_exit(&oipfb->ipfb_lock);
3591 	}
3592 }
3593 
3594 /*
3595  * free 'free_cnt' fragmented packets starting at ipf.
3596  */
3597 void
3598 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3599 {
3600 	size_t	count;
3601 	mblk_t	*mp;
3602 	mblk_t	*tmp;
3603 	ipf_t **ipfp = ipf->ipf_ptphn;
3604 
3605 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3606 	ASSERT(ipfp != NULL);
3607 	ASSERT(ipf != NULL);
3608 
3609 	while (ipf != NULL && free_cnt-- > 0) {
3610 		count = ipf->ipf_count;
3611 		mp = ipf->ipf_mp;
3612 		ipf = ipf->ipf_hash_next;
3613 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3614 			IP_REASS_SET_START(tmp, 0);
3615 			IP_REASS_SET_END(tmp, 0);
3616 		}
3617 		ill->ill_frag_count -= count;
3618 		ASSERT(ipfb->ipfb_count >= count);
3619 		ipfb->ipfb_count -= count;
3620 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3621 		ipfb->ipfb_frag_pkts--;
3622 		freemsg(mp);
3623 		BUMP_MIB(&ip_mib, ipReasmFails);
3624 	}
3625 
3626 	if (ipf)
3627 		ipf->ipf_ptphn = ipfp;
3628 	ipfp[0] = ipf;
3629 }
3630 
3631 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3632 	"obsolete and may be removed in a future release of Solaris.  Use " \
3633 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3634 
3635 /*
3636  * For obsolete per-interface forwarding configuration;
3637  * called in response to ND_GET.
3638  */
3639 /* ARGSUSED */
3640 static int
3641 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3642 {
3643 	ill_t *ill = (ill_t *)cp;
3644 
3645 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3646 
3647 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3648 	return (0);
3649 }
3650 
3651 /*
3652  * For obsolete per-interface forwarding configuration;
3653  * called in response to ND_SET.
3654  */
3655 /* ARGSUSED */
3656 static int
3657 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3658     cred_t *ioc_cr)
3659 {
3660 	long value;
3661 	int retval;
3662 
3663 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3664 
3665 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3666 	    value < 0 || value > 1) {
3667 		return (EINVAL);
3668 	}
3669 
3670 	rw_enter(&ill_g_lock, RW_READER);
3671 	retval = ill_forward_set(q, mp, (value != 0), cp);
3672 	rw_exit(&ill_g_lock);
3673 	return (retval);
3674 }
3675 
3676 /*
3677  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3678  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3679  * up RTS_IFINFO routing socket messages for each interface whose flags we
3680  * change.
3681  */
3682 /* ARGSUSED */
3683 int
3684 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3685 {
3686 	ill_t *ill = (ill_t *)cp;
3687 	ill_group_t *illgrp;
3688 
3689 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3690 
3691 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3692 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3693 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3694 		return (EINVAL);
3695 
3696 	/*
3697 	 * If the ill is in an IPMP group, set the forwarding policy on all
3698 	 * members of the group to the same value.
3699 	 */
3700 	illgrp = ill->ill_group;
3701 	if (illgrp != NULL) {
3702 		ill_t *tmp_ill;
3703 
3704 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3705 		    tmp_ill = tmp_ill->ill_group_next) {
3706 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3707 			    (enable ? "Enabling" : "Disabling"),
3708 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3709 			    tmp_ill->ill_name));
3710 			mutex_enter(&tmp_ill->ill_lock);
3711 			if (enable)
3712 				tmp_ill->ill_flags |= ILLF_ROUTER;
3713 			else
3714 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3715 			mutex_exit(&tmp_ill->ill_lock);
3716 			if (tmp_ill->ill_isv6)
3717 				ill_set_nce_router_flags(tmp_ill, enable);
3718 			/* Notify routing socket listeners of this change. */
3719 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3720 		}
3721 	} else {
3722 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3723 		    (enable ? "Enabling" : "Disabling"),
3724 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3725 		mutex_enter(&ill->ill_lock);
3726 		if (enable)
3727 			ill->ill_flags |= ILLF_ROUTER;
3728 		else
3729 			ill->ill_flags &= ~ILLF_ROUTER;
3730 		mutex_exit(&ill->ill_lock);
3731 		if (ill->ill_isv6)
3732 			ill_set_nce_router_flags(ill, enable);
3733 		/* Notify routing socket listeners of this change. */
3734 		ip_rts_ifmsg(ill->ill_ipif);
3735 	}
3736 
3737 	return (0);
3738 }
3739 
3740 /*
3741  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3742  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3743  * set or clear.
3744  */
3745 static void
3746 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3747 {
3748 	ipif_t *ipif;
3749 	nce_t *nce;
3750 
3751 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3752 		nce = ndp_lookup(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3753 		if (nce != NULL) {
3754 			mutex_enter(&nce->nce_lock);
3755 			if (enable)
3756 				nce->nce_flags |= NCE_F_ISROUTER;
3757 			else
3758 				nce->nce_flags &= ~NCE_F_ISROUTER;
3759 			mutex_exit(&nce->nce_lock);
3760 			NCE_REFRELE(nce);
3761 		}
3762 	}
3763 }
3764 
3765 /*
3766  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3767  * for this ill.  Make sure the v6/v4 question has been answered about this
3768  * ill.  The creation of this ndd variable is only for backwards compatibility.
3769  * The preferred way to control per-interface IP forwarding is through the
3770  * ILLF_ROUTER interface flag.
3771  */
3772 static int
3773 ill_set_ndd_name(ill_t *ill)
3774 {
3775 	char *suffix;
3776 
3777 	ASSERT(IAM_WRITER_ILL(ill));
3778 
3779 	if (ill->ill_isv6)
3780 		suffix = ipv6_forward_suffix;
3781 	else
3782 		suffix = ipv4_forward_suffix;
3783 
3784 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3785 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3786 	/*
3787 	 * Copies over the '\0'.
3788 	 * Note that strlen(suffix) is always bounded.
3789 	 */
3790 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3791 	    strlen(suffix) + 1);
3792 
3793 	/*
3794 	 * Use of the nd table requires holding the reader lock.
3795 	 * Modifying the nd table thru nd_load/nd_unload requires
3796 	 * the writer lock.
3797 	 */
3798 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3799 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3800 	    nd_ill_forward_set, (caddr_t)ill)) {
3801 		/*
3802 		 * If the nd_load failed, it only meant that it could not
3803 		 * allocate a new bunch of room for further NDD expansion.
3804 		 * Because of that, the ill_ndd_name will be set to 0, and
3805 		 * this interface is at the mercy of the global ip_forwarding
3806 		 * variable.
3807 		 */
3808 		rw_exit(&ip_g_nd_lock);
3809 		ill->ill_ndd_name = NULL;
3810 		return (ENOMEM);
3811 	}
3812 	rw_exit(&ip_g_nd_lock);
3813 	return (0);
3814 }
3815 
3816 /*
3817  * Intializes the context structure and returns the first ill in the list
3818  * cuurently start_list and end_list can have values:
3819  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3820  * IP_V4_G_HEAD		Traverse IPV4 list only.
3821  * IP_V6_G_HEAD		Traverse IPV6 list only.
3822  */
3823 
3824 /*
3825  * We don't check for CONDEMNED ills here. Caller must do that if
3826  * necessary under the ill lock.
3827  */
3828 ill_t *
3829 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3830 {
3831 	ill_if_t *ifp;
3832 	ill_t *ill;
3833 	avl_tree_t *avl_tree;
3834 
3835 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3836 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3837 
3838 	/*
3839 	 * setup the lists to search
3840 	 */
3841 	if (end_list != MAX_G_HEADS) {
3842 		ctx->ctx_current_list = start_list;
3843 		ctx->ctx_last_list = end_list;
3844 	} else {
3845 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3846 		ctx->ctx_current_list = 0;
3847 	}
3848 
3849 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3850 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3851 		if (ifp != (ill_if_t *)
3852 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3853 			avl_tree = &ifp->illif_avl_by_ppa;
3854 			ill = avl_first(avl_tree);
3855 			/*
3856 			 * ill is guaranteed to be non NULL or ifp should have
3857 			 * not existed.
3858 			 */
3859 			ASSERT(ill != NULL);
3860 			return (ill);
3861 		}
3862 		ctx->ctx_current_list++;
3863 	}
3864 
3865 	return (NULL);
3866 }
3867 
3868 /*
3869  * returns the next ill in the list. ill_first() must have been called
3870  * before calling ill_next() or bad things will happen.
3871  */
3872 
3873 /*
3874  * We don't check for CONDEMNED ills here. Caller must do that if
3875  * necessary under the ill lock.
3876  */
3877 ill_t *
3878 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3879 {
3880 	ill_if_t *ifp;
3881 	ill_t *ill;
3882 
3883 
3884 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3885 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3886 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
3887 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3888 	    AVL_AFTER)) != NULL) {
3889 		return (ill);
3890 	}
3891 
3892 	/* goto next ill_ifp in the list. */
3893 	ifp = lastill->ill_ifptr->illif_next;
3894 
3895 	/* make sure not at end of circular list */
3896 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3897 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3898 			return (NULL);
3899 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3900 	}
3901 
3902 	return (avl_first(&ifp->illif_avl_by_ppa));
3903 }
3904 
3905 /*
3906  * Check interface name for correct format which is name+ppa.
3907  * name can contain characters and digits, the right most digits
3908  * make up the ppa number. use of octal is not allowed, name must contain
3909  * a ppa, return pointer to the start of ppa.
3910  * In case of error return NULL.
3911  */
3912 static char *
3913 ill_get_ppa_ptr(char *name)
3914 {
3915 	int namelen = mi_strlen(name);
3916 
3917 	int len = namelen;
3918 
3919 	name += len;
3920 	while (len > 0) {
3921 		name--;
3922 		if (*name < '0' || *name > '9')
3923 			break;
3924 		len--;
3925 	}
3926 
3927 	/* empty string, all digits, or no trailing digits */
3928 	if (len == 0 || len == (int)namelen)
3929 		return (NULL);
3930 
3931 	name++;
3932 	/* check for attempted use of octal */
3933 	if (*name == '0' && len != (int)namelen - 1)
3934 		return (NULL);
3935 	return (name);
3936 }
3937 
3938 /*
3939  * use avl tree to locate the ill.
3940  */
3941 static ill_t *
3942 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3943     ipsq_func_t func, int *error)
3944 {
3945 	char *ppa_ptr = NULL;
3946 	int len;
3947 	uint_t ppa;
3948 	ill_t *ill = NULL;
3949 	ill_if_t *ifp;
3950 	int list;
3951 	ipsq_t *ipsq;
3952 
3953 	if (error != NULL)
3954 		*error = 0;
3955 
3956 	/*
3957 	 * get ppa ptr
3958 	 */
3959 	if (isv6)
3960 		list = IP_V6_G_HEAD;
3961 	else
3962 		list = IP_V4_G_HEAD;
3963 
3964 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3965 		if (error != NULL)
3966 			*error = ENXIO;
3967 		return (NULL);
3968 	}
3969 
3970 	len = ppa_ptr - name + 1;
3971 
3972 	ppa = stoi(&ppa_ptr);
3973 
3974 	ifp = IP_VX_ILL_G_LIST(list);
3975 
3976 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
3977 		/*
3978 		 * match is done on len - 1 as the name is not null
3979 		 * terminated it contains ppa in addition to the interface
3980 		 * name.
3981 		 */
3982 		if ((ifp->illif_name_len == len) &&
3983 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
3984 			break;
3985 		} else {
3986 			ifp = ifp->illif_next;
3987 		}
3988 	}
3989 
3990 
3991 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
3992 		/*
3993 		 * Even the interface type does not exist.
3994 		 */
3995 		if (error != NULL)
3996 			*error = ENXIO;
3997 		return (NULL);
3998 	}
3999 
4000 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4001 	if (ill != NULL) {
4002 		/*
4003 		 * The block comment at the start of ipif_down
4004 		 * explains the use of the macros used below
4005 		 */
4006 		GRAB_CONN_LOCK(q);
4007 		mutex_enter(&ill->ill_lock);
4008 		if (ILL_CAN_LOOKUP(ill)) {
4009 			ill_refhold_locked(ill);
4010 			mutex_exit(&ill->ill_lock);
4011 			RELEASE_CONN_LOCK(q);
4012 			return (ill);
4013 		} else if (ILL_CAN_WAIT(ill, q)) {
4014 			ipsq = ill->ill_phyint->phyint_ipsq;
4015 			mutex_enter(&ipsq->ipsq_lock);
4016 			mutex_exit(&ill->ill_lock);
4017 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4018 			mutex_exit(&ipsq->ipsq_lock);
4019 			RELEASE_CONN_LOCK(q);
4020 			*error = EINPROGRESS;
4021 			return (NULL);
4022 		}
4023 		mutex_exit(&ill->ill_lock);
4024 		RELEASE_CONN_LOCK(q);
4025 	}
4026 	if (error != NULL)
4027 		*error = ENXIO;
4028 	return (NULL);
4029 }
4030 
4031 /*
4032  * comparison function for use with avl.
4033  */
4034 static int
4035 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4036 {
4037 	uint_t ppa;
4038 	uint_t ill_ppa;
4039 
4040 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4041 
4042 	ppa = *((uint_t *)ppa_ptr);
4043 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4044 	/*
4045 	 * We want the ill with the lowest ppa to be on the
4046 	 * top.
4047 	 */
4048 	if (ill_ppa < ppa)
4049 		return (1);
4050 	if (ill_ppa > ppa)
4051 		return (-1);
4052 	return (0);
4053 }
4054 
4055 /*
4056  * remove an interface type from the global list.
4057  */
4058 static void
4059 ill_delete_interface_type(ill_if_t *interface)
4060 {
4061 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4062 
4063 	ASSERT(interface != NULL);
4064 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4065 
4066 	avl_destroy(&interface->illif_avl_by_ppa);
4067 	if (interface->illif_ppa_arena != NULL)
4068 		vmem_destroy(interface->illif_ppa_arena);
4069 
4070 	remque(interface);
4071 
4072 	mi_free(interface);
4073 }
4074 
4075 /*
4076  * remove ill from the global list.
4077  */
4078 static void
4079 ill_glist_delete(ill_t *ill)
4080 {
4081 	if (ill == NULL)
4082 		return;
4083 
4084 	rw_enter(&ill_g_lock, RW_WRITER);
4085 	/*
4086 	 * If the ill was never inserted into the AVL tree
4087 	 * we skip the if branch.
4088 	 */
4089 	if (ill->ill_ifptr != NULL) {
4090 		/*
4091 		 * remove from AVL tree and free ppa number
4092 		 */
4093 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4094 
4095 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4096 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4097 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4098 		}
4099 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4100 			ill_delete_interface_type(ill->ill_ifptr);
4101 		}
4102 
4103 		/*
4104 		 * Indicate ill is no longer in the list.
4105 		 */
4106 		ill->ill_ifptr = NULL;
4107 		ill->ill_name_length = 0;
4108 		ill->ill_name[0] = '\0';
4109 		ill->ill_ppa = UINT_MAX;
4110 	}
4111 	ill_phyint_free(ill);
4112 	rw_exit(&ill_g_lock);
4113 }
4114 
4115 /*
4116  * allocate a ppa, if the number of plumbed interfaces of this type are
4117  * less than ill_no_arena do a linear search to find a unused ppa.
4118  * When the number goes beyond ill_no_arena switch to using an arena.
4119  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4120  * is the return value for an error condition, so allocation starts at one
4121  * and is decremented by one.
4122  */
4123 static int
4124 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4125 {
4126 	ill_t *tmp_ill;
4127 	uint_t start, end;
4128 	int ppa;
4129 
4130 	if (ifp->illif_ppa_arena == NULL &&
4131 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4132 		/*
4133 		 * Create an arena.
4134 		 */
4135 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4136 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4137 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4138 			/* allocate what has already been assigned */
4139 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4140 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4141 		    tmp_ill, AVL_AFTER)) {
4142 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4143 			    1,		/* size */
4144 			    1,		/* align/quantum */
4145 			    0,		/* phase */
4146 			    0,		/* nocross */
4147 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4148 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4149 			    VM_NOSLEEP|VM_FIRSTFIT);
4150 			if (ppa == 0) {
4151 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4152 				    " failed while switching"));
4153 				vmem_destroy(ifp->illif_ppa_arena);
4154 				ifp->illif_ppa_arena = NULL;
4155 				break;
4156 			}
4157 		}
4158 	}
4159 
4160 	if (ifp->illif_ppa_arena != NULL) {
4161 		if (ill->ill_ppa == UINT_MAX) {
4162 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4163 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4164 			if (ppa == 0)
4165 				return (EAGAIN);
4166 			ill->ill_ppa = --ppa;
4167 		} else {
4168 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4169 			    1, 		/* size */
4170 			    1, 		/* align/quantum */
4171 			    0, 		/* phase */
4172 			    0, 		/* nocross */
4173 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4174 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4175 			    VM_NOSLEEP|VM_FIRSTFIT);
4176 			/*
4177 			 * Most likely the allocation failed because
4178 			 * the requested ppa was in use.
4179 			 */
4180 			if (ppa == 0)
4181 				return (EEXIST);
4182 		}
4183 		return (0);
4184 	}
4185 
4186 	/*
4187 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4188 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4189 	 */
4190 	if (ill->ill_ppa == UINT_MAX) {
4191 		end = UINT_MAX - 1;
4192 		start = 0;
4193 	} else {
4194 		end = start = ill->ill_ppa;
4195 	}
4196 
4197 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4198 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4199 		if (start++ >= end) {
4200 			if (ill->ill_ppa == UINT_MAX)
4201 				return (EAGAIN);
4202 			else
4203 				return (EEXIST);
4204 		}
4205 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4206 	}
4207 	ill->ill_ppa = start;
4208 	return (0);
4209 }
4210 
4211 /*
4212  * Insert ill into the list of configured ill's. Once this function completes,
4213  * the ill is globally visible and is available through lookups. More precisely
4214  * this happens after the caller drops the ill_g_lock.
4215  */
4216 static int
4217 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4218 {
4219 	ill_if_t *ill_interface;
4220 	avl_index_t where = 0;
4221 	int error;
4222 	int name_length;
4223 	int index;
4224 	boolean_t check_length = B_FALSE;
4225 
4226 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4227 
4228 	name_length = mi_strlen(name) + 1;
4229 
4230 	if (isv6)
4231 		index = IP_V6_G_HEAD;
4232 	else
4233 		index = IP_V4_G_HEAD;
4234 
4235 	ill_interface = IP_VX_ILL_G_LIST(index);
4236 	/*
4237 	 * Search for interface type based on name
4238 	 */
4239 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4240 		if ((ill_interface->illif_name_len == name_length) &&
4241 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4242 			break;
4243 		}
4244 		ill_interface = ill_interface->illif_next;
4245 	}
4246 
4247 	/*
4248 	 * Interface type not found, create one.
4249 	 */
4250 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4251 
4252 		ill_g_head_t ghead;
4253 
4254 		/*
4255 		 * allocate ill_if_t structure
4256 		 */
4257 
4258 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4259 		if (ill_interface == NULL) {
4260 			return (ENOMEM);
4261 		}
4262 
4263 
4264 
4265 		(void) strcpy(ill_interface->illif_name, name);
4266 		ill_interface->illif_name_len = name_length;
4267 
4268 		avl_create(&ill_interface->illif_avl_by_ppa,
4269 		    ill_compare_ppa, sizeof (ill_t),
4270 		    offsetof(struct ill_s, ill_avl_byppa));
4271 
4272 		/*
4273 		 * link the structure in the back to maintain order
4274 		 * of configuration for ifconfig output.
4275 		 */
4276 		ghead = ill_g_heads[index];
4277 		insque(ill_interface, ghead.ill_g_list_tail);
4278 
4279 	}
4280 
4281 	if (ill->ill_ppa == UINT_MAX)
4282 		check_length = B_TRUE;
4283 
4284 	error = ill_alloc_ppa(ill_interface, ill);
4285 	if (error != 0) {
4286 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4287 			ill_delete_interface_type(ill->ill_ifptr);
4288 		return (error);
4289 	}
4290 
4291 	/*
4292 	 * When the ppa is choosen by the system, check that there is
4293 	 * enough space to insert ppa. if a specific ppa was passed in this
4294 	 * check is not required as the interface name passed in will have
4295 	 * the right ppa in it.
4296 	 */
4297 	if (check_length) {
4298 		/*
4299 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4300 		 */
4301 		char buf[sizeof (uint_t) * 3];
4302 
4303 		/*
4304 		 * convert ppa to string to calculate the amount of space
4305 		 * required for it in the name.
4306 		 */
4307 		numtos(ill->ill_ppa, buf);
4308 
4309 		/* Do we have enough space to insert ppa ? */
4310 
4311 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4312 			/* Free ppa and interface type struct */
4313 			if (ill_interface->illif_ppa_arena != NULL) {
4314 				vmem_free(ill_interface->illif_ppa_arena,
4315 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4316 			}
4317 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4318 			    0) {
4319 				ill_delete_interface_type(ill->ill_ifptr);
4320 			}
4321 
4322 			return (EINVAL);
4323 		}
4324 	}
4325 
4326 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4327 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4328 
4329 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4330 	    &where);
4331 	ill->ill_ifptr = ill_interface;
4332 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4333 
4334 	ill_phyint_reinit(ill);
4335 	return (0);
4336 }
4337 
4338 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4339 static boolean_t
4340 ipsq_init(ill_t *ill)
4341 {
4342 	ipsq_t  *ipsq;
4343 
4344 	/* Init the ipsq and impicitly enter as writer */
4345 	ill->ill_phyint->phyint_ipsq =
4346 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4347 	if (ill->ill_phyint->phyint_ipsq == NULL)
4348 		return (B_FALSE);
4349 	ipsq = ill->ill_phyint->phyint_ipsq;
4350 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4351 	ill->ill_phyint->phyint_ipsq_next = NULL;
4352 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4353 	ipsq->ipsq_refs = 1;
4354 	ipsq->ipsq_writer = curthread;
4355 	ipsq->ipsq_reentry_cnt = 1;
4356 #ifdef ILL_DEBUG
4357 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4358 #endif
4359 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4360 	return (B_TRUE);
4361 }
4362 
4363 /*
4364  * ill_init is called by ip_open when a device control stream is opened.
4365  * It does a few initializations, and shoots a DL_INFO_REQ message down
4366  * to the driver.  The response is later picked up in ip_rput_dlpi and
4367  * used to set up default mechanisms for talking to the driver.  (Always
4368  * called as writer.)
4369  *
4370  * If this function returns error, ip_open will call ip_close which in
4371  * turn will call ill_delete to clean up any memory allocated here that
4372  * is not yet freed.
4373  */
4374 int
4375 ill_init(queue_t *q, ill_t *ill)
4376 {
4377 	int	count;
4378 	dl_info_req_t	*dlir;
4379 	mblk_t	*info_mp;
4380 	uchar_t *frag_ptr;
4381 
4382 	/*
4383 	 * The ill is initialized to zero by mi_alloc*(). In addition
4384 	 * some fields already contain valid values, initialized in
4385 	 * ip_open(), before we reach here.
4386 	 */
4387 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4388 
4389 	ill->ill_rq = q;
4390 	ill->ill_wq = WR(q);
4391 
4392 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4393 	    BPRI_HI);
4394 	if (info_mp == NULL)
4395 		return (ENOMEM);
4396 
4397 	/*
4398 	 * Allocate sufficient space to contain our fragment hash table and
4399 	 * the device name.
4400 	 */
4401 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4402 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4403 	if (frag_ptr == NULL) {
4404 		freemsg(info_mp);
4405 		return (ENOMEM);
4406 	}
4407 	ill->ill_frag_ptr = frag_ptr;
4408 	ill->ill_frag_free_num_pkts = 0;
4409 	ill->ill_last_frag_clean_time = 0;
4410 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4411 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4412 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4413 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4414 		    NULL, MUTEX_DEFAULT, NULL);
4415 	}
4416 
4417 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4418 	if (ill->ill_phyint == NULL) {
4419 		freemsg(info_mp);
4420 		mi_free(frag_ptr);
4421 		return (ENOMEM);
4422 	}
4423 
4424 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4425 	/*
4426 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4427 	 * at this point because of the following reason. If we can't
4428 	 * enter the ipsq at some point and cv_wait, the writer that
4429 	 * wakes us up tries to locate us using the list of all phyints
4430 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4431 	 * If we don't set it now, we risk a missed wakeup.
4432 	 */
4433 	ill->ill_phyint->phyint_illv4 = ill;
4434 	ill->ill_ppa = UINT_MAX;
4435 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4436 
4437 	if (!ipsq_init(ill)) {
4438 		freemsg(info_mp);
4439 		mi_free(frag_ptr);
4440 		mi_free(ill->ill_phyint);
4441 		return (ENOMEM);
4442 	}
4443 
4444 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4445 
4446 
4447 	/* Frag queue limit stuff */
4448 	ill->ill_frag_count = 0;
4449 	ill->ill_ipf_gen = 0;
4450 
4451 	ill->ill_global_timer = INFINITY;
4452 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4453 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4454 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4455 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4456 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4457 
4458 	/*
4459 	 * Initialize IPv6 configuration variables.  The IP module is always
4460 	 * opened as an IPv4 module.  Instead tracking down the cases where
4461 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4462 	 * here for convenience, this has no effect until the ill is set to do
4463 	 * IPv6.
4464 	 */
4465 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4466 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4467 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4468 	ill->ill_max_buf = ND_MAX_Q;
4469 	ill->ill_refcnt = 0;
4470 
4471 	/* Send down the Info Request to the driver. */
4472 	info_mp->b_datap->db_type = M_PCPROTO;
4473 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4474 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4475 	dlir->dl_primitive = DL_INFO_REQ;
4476 
4477 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4478 
4479 	qprocson(q);
4480 	ill_dlpi_send(ill, info_mp);
4481 
4482 	return (0);
4483 }
4484 
4485 /*
4486  * ill_dls_info
4487  * creates datalink socket info from the device.
4488  */
4489 int
4490 ill_dls_info(struct sockaddr_dl *sdl, ipif_t *ipif)
4491 {
4492 	size_t	length;
4493 	ill_t	*ill = ipif->ipif_ill;
4494 
4495 	sdl->sdl_family = AF_LINK;
4496 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4497 	sdl->sdl_type = ipif->ipif_type;
4498 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4499 	length = mi_strlen(sdl->sdl_data);
4500 	ASSERT(length < 256);
4501 	sdl->sdl_nlen = (uchar_t)length;
4502 	sdl->sdl_alen = ill->ill_phys_addr_length;
4503 	mutex_enter(&ill->ill_lock);
4504 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4505 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4506 		    ill->ill_phys_addr_length);
4507 	}
4508 	mutex_exit(&ill->ill_lock);
4509 	sdl->sdl_slen = 0;
4510 	return (sizeof (struct sockaddr_dl));
4511 }
4512 
4513 /*
4514  * ill_xarp_info
4515  * creates xarp info from the device.
4516  */
4517 static int
4518 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4519 {
4520 	sdl->sdl_family = AF_LINK;
4521 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4522 	sdl->sdl_type = ill->ill_type;
4523 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4524 	    sizeof (sdl->sdl_data));
4525 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4526 	sdl->sdl_alen = ill->ill_phys_addr_length;
4527 	sdl->sdl_slen = 0;
4528 	return (sdl->sdl_nlen);
4529 }
4530 
4531 static int
4532 loopback_kstat_update(kstat_t *ksp, int rw)
4533 {
4534 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4535 
4536 	if (rw == KSTAT_WRITE)
4537 		return (EACCES);
4538 	kn[0].value.ui32 = loopback_packets;
4539 	kn[1].value.ui32 = loopback_packets;
4540 	return (0);
4541 }
4542 
4543 
4544 /*
4545  * Has ifindex been plumbed already.
4546  */
4547 static boolean_t
4548 phyint_exists(uint_t index)
4549 {
4550 	phyint_t *phyi;
4551 
4552 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4553 	/*
4554 	 * Indexes are stored in the phyint - a common structure
4555 	 * to both IPv4 and IPv6.
4556 	 */
4557 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4558 	    (void *) &index, NULL);
4559 	return (phyi != NULL);
4560 }
4561 
4562 /*
4563  * Assign a unique interface index for the phyint.
4564  */
4565 static boolean_t
4566 phyint_assign_ifindex(phyint_t *phyi)
4567 {
4568 	uint_t starting_index;
4569 
4570 	ASSERT(phyi->phyint_ifindex == 0);
4571 	if (!ill_index_wrap) {
4572 		phyi->phyint_ifindex = ill_index++;
4573 		if (ill_index == 0) {
4574 			/* Reached the uint_t limit Next time wrap  */
4575 			ill_index_wrap = B_TRUE;
4576 		}
4577 		return (B_TRUE);
4578 	}
4579 
4580 	/*
4581 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4582 	 * at this point and don't want to call any function that attempts
4583 	 * to get the lock again.
4584 	 */
4585 	starting_index = ill_index++;
4586 	for (; ill_index != starting_index; ill_index++) {
4587 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4588 			/* found unused index - use it */
4589 			phyi->phyint_ifindex = ill_index;
4590 			return (B_TRUE);
4591 		}
4592 	}
4593 
4594 	/*
4595 	 * all interface indicies are inuse.
4596 	 */
4597 	return (B_FALSE);
4598 }
4599 
4600 /*
4601  * Return a pointer to the ill which matches the supplied name.  Note that
4602  * the ill name length includes the null termination character.  (May be
4603  * called as writer.)
4604  * If do_alloc and the interface is "lo0" it will be automatically created.
4605  * Cannot bump up reference on condemned ills. So dup detect can't be done
4606  * using this func.
4607  */
4608 ill_t *
4609 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4610     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4611 {
4612 	ill_t	*ill;
4613 	ipif_t	*ipif;
4614 	kstat_named_t	*kn;
4615 	boolean_t isloopback;
4616 	ipsq_t *old_ipsq;
4617 
4618 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4619 
4620 	rw_enter(&ill_g_lock, RW_READER);
4621 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4622 	rw_exit(&ill_g_lock);
4623 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4624 		return (ill);
4625 
4626 	/*
4627 	 * Couldn't find it.  Does this happen to be a lookup for the
4628 	 * loopback device and are we allowed to allocate it?
4629 	 */
4630 	if (!isloopback || !do_alloc)
4631 		return (NULL);
4632 
4633 	rw_enter(&ill_g_lock, RW_WRITER);
4634 
4635 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4636 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4637 		rw_exit(&ill_g_lock);
4638 		return (ill);
4639 	}
4640 
4641 	/* Create the loopback device on demand */
4642 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4643 	    sizeof (ipif_loopback_name), BPRI_MED));
4644 	if (ill == NULL)
4645 		goto done;
4646 
4647 	*ill = ill_null;
4648 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4649 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4650 	if (ill->ill_phyint == NULL)
4651 		goto done;
4652 
4653 	if (isv6)
4654 		ill->ill_phyint->phyint_illv6 = ill;
4655 	else
4656 		ill->ill_phyint->phyint_illv4 = ill;
4657 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4658 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4659 	/* Add room for tcp+ip headers */
4660 	if (isv6) {
4661 		ill->ill_isv6 = B_TRUE;
4662 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4663 		if (!ill_allocate_mibs(ill))
4664 			goto done;
4665 	} else {
4666 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4667 	}
4668 	ill->ill_max_mtu = ill->ill_max_frag;
4669 	/*
4670 	 * ipif_loopback_name can't be pointed at directly because its used
4671 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4672 	 * from the glist, ill_glist_delete() sets the first character of
4673 	 * ill_name to '\0'.
4674 	 */
4675 	ill->ill_name = (char *)ill + sizeof (*ill);
4676 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4677 	ill->ill_name_length = sizeof (ipif_loopback_name);
4678 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4679 
4680 	ill->ill_global_timer = INFINITY;
4681 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4682 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4683 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4684 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4685 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4686 
4687 	/* No resolver here. */
4688 	ill->ill_net_type = IRE_LOOPBACK;
4689 
4690 	/* Initialize the ipsq */
4691 	if (!ipsq_init(ill))
4692 		goto done;
4693 
4694 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4695 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4696 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4697 #ifdef ILL_DEBUG
4698 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4699 #endif
4700 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4701 	if (ipif == NULL)
4702 		goto done;
4703 
4704 	ill->ill_flags = ILLF_MULTICAST;
4705 
4706 	/* Set up default loopback address and mask. */
4707 	if (!isv6) {
4708 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4709 
4710 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4711 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4712 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4713 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4714 		    ipif->ipif_v6subnet);
4715 		ill->ill_flags |= ILLF_IPV4;
4716 	} else {
4717 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4718 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4719 		ipif->ipif_v6net_mask = ipv6_all_ones;
4720 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4721 		    ipif->ipif_v6subnet);
4722 		ill->ill_flags |= ILLF_IPV6;
4723 	}
4724 
4725 	/*
4726 	 * Chain us in at the end of the ill list. hold the ill
4727 	 * before we make it globally visible. 1 for the lookup.
4728 	 */
4729 	ill->ill_refcnt = 0;
4730 	ill_refhold(ill);
4731 
4732 	ill->ill_frag_count = 0;
4733 	ill->ill_frag_free_num_pkts = 0;
4734 	ill->ill_last_frag_clean_time = 0;
4735 
4736 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4737 
4738 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4739 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4740 
4741 	/* Let SCTP know so that it can add this to its list */
4742 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4743 
4744 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4745 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4746 
4747 	/*
4748 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4749 	 */
4750 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4751 		/* Loopback ills aren't in any IPMP group */
4752 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4753 		ipsq_delete(old_ipsq);
4754 	}
4755 
4756 	/*
4757 	 * Delay this till the ipif is allocated as ipif_allocate
4758 	 * de-references ill_phyint for getting the ifindex. We
4759 	 * can't do this before ipif_allocate because ill_phyint_reinit
4760 	 * -> phyint_assign_ifindex expects ipif to be present.
4761 	 */
4762 	mutex_enter(&ill->ill_phyint->phyint_lock);
4763 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4764 	mutex_exit(&ill->ill_phyint->phyint_lock);
4765 
4766 	if (loopback_ksp == NULL) {
4767 		/* Export loopback interface statistics */
4768 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4769 		    KSTAT_TYPE_NAMED, 2, 0);
4770 		if (loopback_ksp != NULL) {
4771 			loopback_ksp->ks_update = loopback_kstat_update;
4772 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4773 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4774 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4775 			kstat_install(loopback_ksp);
4776 		}
4777 	}
4778 
4779 	if (error != NULL)
4780 		*error = 0;
4781 	*did_alloc = B_TRUE;
4782 	rw_exit(&ill_g_lock);
4783 	return (ill);
4784 done:
4785 	if (ill != NULL) {
4786 		if (ill->ill_phyint != NULL) {
4787 			ipsq_t	*ipsq;
4788 
4789 			ipsq = ill->ill_phyint->phyint_ipsq;
4790 			if (ipsq != NULL)
4791 				kmem_free(ipsq, sizeof (ipsq_t));
4792 			mi_free(ill->ill_phyint);
4793 		}
4794 		ill_free_mib(ill);
4795 		mi_free(ill);
4796 	}
4797 	rw_exit(&ill_g_lock);
4798 	if (error != NULL)
4799 		*error = ENOMEM;
4800 	return (NULL);
4801 }
4802 
4803 /*
4804  * Return a pointer to the ill which matches the index and IP version type.
4805  */
4806 ill_t *
4807 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4808     ipsq_func_t func, int *err)
4809 {
4810 	ill_t	*ill;
4811 	ipsq_t  *ipsq;
4812 	phyint_t *phyi;
4813 
4814 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4815 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4816 
4817 	if (err != NULL)
4818 		*err = 0;
4819 
4820 	/*
4821 	 * Indexes are stored in the phyint - a common structure
4822 	 * to both IPv4 and IPv6.
4823 	 */
4824 	rw_enter(&ill_g_lock, RW_READER);
4825 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4826 	    (void *) &index, NULL);
4827 	if (phyi != NULL) {
4828 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4829 		if (ill != NULL) {
4830 			/*
4831 			 * The block comment at the start of ipif_down
4832 			 * explains the use of the macros used below
4833 			 */
4834 			GRAB_CONN_LOCK(q);
4835 			mutex_enter(&ill->ill_lock);
4836 			if (ILL_CAN_LOOKUP(ill)) {
4837 				ill_refhold_locked(ill);
4838 				mutex_exit(&ill->ill_lock);
4839 				RELEASE_CONN_LOCK(q);
4840 				rw_exit(&ill_g_lock);
4841 				return (ill);
4842 			} else if (ILL_CAN_WAIT(ill, q)) {
4843 				ipsq = ill->ill_phyint->phyint_ipsq;
4844 				mutex_enter(&ipsq->ipsq_lock);
4845 				rw_exit(&ill_g_lock);
4846 				mutex_exit(&ill->ill_lock);
4847 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4848 				mutex_exit(&ipsq->ipsq_lock);
4849 				RELEASE_CONN_LOCK(q);
4850 				*err = EINPROGRESS;
4851 				return (NULL);
4852 			}
4853 			RELEASE_CONN_LOCK(q);
4854 			mutex_exit(&ill->ill_lock);
4855 		}
4856 	}
4857 	rw_exit(&ill_g_lock);
4858 	if (err != NULL)
4859 		*err = ENXIO;
4860 	return (NULL);
4861 }
4862 
4863 /*
4864  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4865  * that gives a running thread a reference to the ill. This reference must be
4866  * released by the thread when it is done accessing the ill and related
4867  * objects. ill_refcnt can not be used to account for static references
4868  * such as other structures pointing to an ill. Callers must generally
4869  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4870  * or be sure that the ill is not being deleted or changing state before
4871  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4872  * ill won't change any of its critical state such as address, netmask etc.
4873  */
4874 void
4875 ill_refhold(ill_t *ill)
4876 {
4877 	mutex_enter(&ill->ill_lock);
4878 	ill->ill_refcnt++;
4879 	ILL_TRACE_REF(ill);
4880 	mutex_exit(&ill->ill_lock);
4881 }
4882 
4883 void
4884 ill_refhold_locked(ill_t *ill)
4885 {
4886 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4887 	ill->ill_refcnt++;
4888 	ILL_TRACE_REF(ill);
4889 }
4890 
4891 int
4892 ill_check_and_refhold(ill_t *ill)
4893 {
4894 	mutex_enter(&ill->ill_lock);
4895 	if (ILL_CAN_LOOKUP(ill)) {
4896 		ill_refhold_locked(ill);
4897 		mutex_exit(&ill->ill_lock);
4898 		return (0);
4899 	}
4900 	mutex_exit(&ill->ill_lock);
4901 	return (ILL_LOOKUP_FAILED);
4902 }
4903 
4904 /*
4905  * Must not be called while holding any locks. Otherwise if this is
4906  * the last reference to be released, there is a chance of recursive mutex
4907  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4908  * to restart an ioctl.
4909  */
4910 void
4911 ill_refrele(ill_t *ill)
4912 {
4913 	mutex_enter(&ill->ill_lock);
4914 	ASSERT(ill->ill_refcnt != 0);
4915 	ill->ill_refcnt--;
4916 	ILL_UNTRACE_REF(ill);
4917 	if (ill->ill_refcnt != 0) {
4918 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4919 		mutex_exit(&ill->ill_lock);
4920 		return;
4921 	}
4922 
4923 	/* Drops the ill_lock */
4924 	ipif_ill_refrele_tail(ill);
4925 }
4926 
4927 /*
4928  * Obtain a weak reference count on the ill. This reference ensures the
4929  * ill won't be freed, but the ill may change any of its critical state
4930  * such as netmask, address etc. Returns an error if the ill has started
4931  * closing.
4932  */
4933 boolean_t
4934 ill_waiter_inc(ill_t *ill)
4935 {
4936 	mutex_enter(&ill->ill_lock);
4937 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4938 		mutex_exit(&ill->ill_lock);
4939 		return (B_FALSE);
4940 	}
4941 	ill->ill_waiters++;
4942 	mutex_exit(&ill->ill_lock);
4943 	return (B_TRUE);
4944 }
4945 
4946 void
4947 ill_waiter_dcr(ill_t *ill)
4948 {
4949 	mutex_enter(&ill->ill_lock);
4950 	ill->ill_waiters--;
4951 	if (ill->ill_waiters == 0)
4952 		cv_broadcast(&ill->ill_cv);
4953 	mutex_exit(&ill->ill_lock);
4954 }
4955 
4956 /*
4957  * Named Dispatch routine to produce a formatted report on all ILLs.
4958  * This report is accessed by using the ndd utility to "get" ND variable
4959  * "ip_ill_status".
4960  */
4961 /* ARGSUSED */
4962 int
4963 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
4964 {
4965 	ill_t		*ill;
4966 	ill_walk_context_t ctx;
4967 
4968 	(void) mi_mpprintf(mp,
4969 	    "ILL      " MI_COL_HDRPAD_STR
4970 	/*   01234567[89ABCDEF] */
4971 	    "rq       " MI_COL_HDRPAD_STR
4972 	/*   01234567[89ABCDEF] */
4973 	    "wq       " MI_COL_HDRPAD_STR
4974 	/*   01234567[89ABCDEF] */
4975 	    "upcnt mxfrg err name");
4976 	/*   12345 12345 123 xxxxxxxx  */
4977 
4978 	rw_enter(&ill_g_lock, RW_READER);
4979 	ill = ILL_START_WALK_ALL(&ctx);
4980 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4981 		(void) mi_mpprintf(mp,
4982 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
4983 		    "%05u %05u %03d %s",
4984 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
4985 		    ill->ill_ipif_up_count,
4986 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
4987 	}
4988 	rw_exit(&ill_g_lock);
4989 
4990 	return (0);
4991 }
4992 
4993 /*
4994  * Named Dispatch routine to produce a formatted report on all IPIFs.
4995  * This report is accessed by using the ndd utility to "get" ND variable
4996  * "ip_ipif_status".
4997  */
4998 /* ARGSUSED */
4999 int
5000 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5001 {
5002 	char	buf1[INET6_ADDRSTRLEN];
5003 	char	buf2[INET6_ADDRSTRLEN];
5004 	char	buf3[INET6_ADDRSTRLEN];
5005 	char	buf4[INET6_ADDRSTRLEN];
5006 	char	buf5[INET6_ADDRSTRLEN];
5007 	char	buf6[INET6_ADDRSTRLEN];
5008 	char	buf[LIFNAMSIZ];
5009 	ill_t	*ill;
5010 	ipif_t	*ipif;
5011 	nv_t	*nvp;
5012 	uint64_t flags;
5013 	zoneid_t zoneid;
5014 	ill_walk_context_t ctx;
5015 
5016 	(void) mi_mpprintf(mp,
5017 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5018 	    "\tlocal address\n"
5019 	    "\tsrc address\n"
5020 	    "\tsubnet\n"
5021 	    "\tmask\n"
5022 	    "\tbroadcast\n"
5023 	    "\tp-p-dst");
5024 
5025 	ASSERT(q->q_next == NULL);
5026 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5027 
5028 	rw_enter(&ill_g_lock, RW_READER);
5029 	ill = ILL_START_WALK_ALL(&ctx);
5030 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5031 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
5032 			if (zoneid != GLOBAL_ZONEID &&
5033 			    zoneid != ipif->ipif_zoneid)
5034 				continue;
5035 			(void) mi_mpprintf(mp,
5036 			    MI_COL_PTRFMT_STR
5037 			    "%04u %05u %u/%u/%u %s %d",
5038 			    (void *)ipif,
5039 			    ipif->ipif_metric, ipif->ipif_mtu,
5040 			    ipif->ipif_ib_pkt_count,
5041 			    ipif->ipif_ob_pkt_count,
5042 			    ipif->ipif_fo_pkt_count,
5043 			    ipif_get_name(ipif, buf, sizeof (buf)),
5044 			    ipif->ipif_zoneid);
5045 
5046 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5047 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5048 
5049 		/* Tack on text strings for any flags. */
5050 		nvp = ipif_nv_tbl;
5051 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5052 			if (nvp->nv_value & flags)
5053 				(void) mi_mpprintf_nr(mp, " %s",
5054 				    nvp->nv_name);
5055 		}
5056 		(void) mi_mpprintf(mp,
5057 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5058 		    inet_ntop(AF_INET6,
5059 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5060 		    inet_ntop(AF_INET6,
5061 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5062 		    inet_ntop(AF_INET6,
5063 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5064 		    inet_ntop(AF_INET6,
5065 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5066 		    inet_ntop(AF_INET6,
5067 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5068 		    inet_ntop(AF_INET6,
5069 			&ipif->ipif_v6pp_dst_addr,
5070 			buf6, sizeof (buf6)));
5071 		}
5072 	}
5073 	rw_exit(&ill_g_lock);
5074 	return (0);
5075 }
5076 
5077 /*
5078  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5079  * driver.  We construct best guess defaults for lower level information that
5080  * we need.  If an interface is brought up without injection of any overriding
5081  * information from outside, we have to be ready to go with these defaults.
5082  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5083  * we primarely want the dl_provider_style.
5084  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5085  * at which point we assume the other part of the information is valid.
5086  */
5087 void
5088 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5089 {
5090 	uchar_t		*brdcst_addr;
5091 	uint_t		brdcst_addr_length, phys_addr_length;
5092 	t_scalar_t	sap_length;
5093 	dl_info_ack_t	*dlia;
5094 	ip_m_t		*ipm;
5095 	dl_qos_cl_sel1_t *sel1;
5096 
5097 	ASSERT(IAM_WRITER_ILL(ill));
5098 
5099 	/*
5100 	 * Till the ill is fully up ILL_CHANGING will be set and
5101 	 * the ill is not globally visible. So no need for a lock.
5102 	 */
5103 	dlia = (dl_info_ack_t *)mp->b_rptr;
5104 	ill->ill_mactype = dlia->dl_mac_type;
5105 
5106 	ipm = ip_m_lookup(dlia->dl_mac_type);
5107 	if (ipm == NULL) {
5108 		ipm = ip_m_lookup(DL_OTHER);
5109 		ASSERT(ipm != NULL);
5110 	}
5111 	ill->ill_media = ipm;
5112 
5113 	/*
5114 	 * When the new DLPI stuff is ready we'll pull lengths
5115 	 * from dlia.
5116 	 */
5117 	if (dlia->dl_version == DL_VERSION_2) {
5118 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5119 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5120 		    brdcst_addr_length);
5121 		if (brdcst_addr == NULL) {
5122 			brdcst_addr_length = 0;
5123 		}
5124 		sap_length = dlia->dl_sap_length;
5125 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5126 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5127 		    brdcst_addr_length, sap_length, phys_addr_length));
5128 	} else {
5129 		brdcst_addr_length = 6;
5130 		brdcst_addr = ip_six_byte_all_ones;
5131 		sap_length = -2;
5132 		phys_addr_length = brdcst_addr_length;
5133 	}
5134 
5135 	ill->ill_bcast_addr_length = brdcst_addr_length;
5136 	ill->ill_phys_addr_length = phys_addr_length;
5137 	ill->ill_sap_length = sap_length;
5138 	ill->ill_max_frag = dlia->dl_max_sdu;
5139 	ill->ill_max_mtu = ill->ill_max_frag;
5140 
5141 	ill->ill_type = ipm->ip_m_type;
5142 
5143 	if (!ill->ill_dlpi_style_set) {
5144 		if (dlia->dl_provider_style == DL_STYLE2)
5145 			ill->ill_needs_attach = 1;
5146 
5147 		/*
5148 		 * Allocate the first ipif on this ill. We don't delay it
5149 		 * further as ioctl handling assumes atleast one ipif to
5150 		 * be present.
5151 		 *
5152 		 * At this point we don't know whether the ill is v4 or v6.
5153 		 * We will know this whan the SIOCSLIFNAME happens and
5154 		 * the correct value for ill_isv6 will be assigned in
5155 		 * ipif_set_values(). We need to hold the ill lock and
5156 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5157 		 * the wakeup.
5158 		 */
5159 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5160 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5161 		mutex_enter(&ill->ill_lock);
5162 		ASSERT(ill->ill_dlpi_style_set == 0);
5163 		ill->ill_dlpi_style_set = 1;
5164 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5165 		cv_broadcast(&ill->ill_cv);
5166 		mutex_exit(&ill->ill_lock);
5167 		freemsg(mp);
5168 		return;
5169 	}
5170 	ASSERT(ill->ill_ipif != NULL);
5171 	/*
5172 	 * We know whether it is IPv4 or IPv6 now, as this is the
5173 	 * second DL_INFO_ACK we are recieving in response to the
5174 	 * DL_INFO_REQ sent in ipif_set_values.
5175 	 */
5176 	if (ill->ill_isv6)
5177 		ill->ill_sap = IP6_DL_SAP;
5178 	else
5179 		ill->ill_sap = IP_DL_SAP;
5180 	/*
5181 	 * Set ipif_mtu which is used to set the IRE's
5182 	 * ire_max_frag value. The driver could have sent
5183 	 * a different mtu from what it sent last time. No
5184 	 * need to call ipif_mtu_change because IREs have
5185 	 * not yet been created.
5186 	 */
5187 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5188 	/*
5189 	 * Clear all the flags that were set based on ill_bcast_addr_length
5190 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5191 	 * changed now and we need to re-evaluate.
5192 	 */
5193 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5194 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5195 
5196 	/*
5197 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5198 	 * changed now.
5199 	 */
5200 	if (ill->ill_bcast_addr_length == 0) {
5201 		if (ill->ill_resolver_mp != NULL)
5202 			freemsg(ill->ill_resolver_mp);
5203 		if (ill->ill_bcast_mp != NULL)
5204 			freemsg(ill->ill_bcast_mp);
5205 		if (ill->ill_flags & ILLF_XRESOLV)
5206 			ill->ill_net_type = IRE_IF_RESOLVER;
5207 		else
5208 			ill->ill_net_type = IRE_IF_NORESOLVER;
5209 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5210 		    ill->ill_phys_addr_length,
5211 		    ill->ill_sap,
5212 		    ill->ill_sap_length);
5213 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5214 
5215 		if (ill->ill_isv6)
5216 			/*
5217 			 * Note: xresolv interfaces will eventually need NOARP
5218 			 * set here as well, but that will require those
5219 			 * external resolvers to have some knowledge of
5220 			 * that flag and act appropriately. Not to be changed
5221 			 * at present.
5222 			 */
5223 			ill->ill_flags |= ILLF_NONUD;
5224 		else
5225 			ill->ill_flags |= ILLF_NOARP;
5226 
5227 		if (ill->ill_phys_addr_length == 0) {
5228 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5229 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5230 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5231 			} else {
5232 				/* pt-pt supports multicast. */
5233 				ill->ill_flags |= ILLF_MULTICAST;
5234 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5235 			}
5236 		}
5237 	} else {
5238 		ill->ill_net_type = IRE_IF_RESOLVER;
5239 		if (ill->ill_bcast_mp != NULL)
5240 			freemsg(ill->ill_bcast_mp);
5241 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5242 		    ill->ill_bcast_addr_length, ill->ill_sap,
5243 		    ill->ill_sap_length);
5244 		/*
5245 		 * Later detect lack of DLPI driver multicast
5246 		 * capability by catching DL_ENABMULTI errors in
5247 		 * ip_rput_dlpi.
5248 		 */
5249 		ill->ill_flags |= ILLF_MULTICAST;
5250 		if (!ill->ill_isv6)
5251 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5252 	}
5253 	/* By default an interface does not support any CoS marking */
5254 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5255 
5256 	/*
5257 	 * If we get QoS information in DL_INFO_ACK, the device supports
5258 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5259 	 */
5260 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5261 	    dlia->dl_qos_length);
5262 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5263 		ill->ill_flags |= ILLF_COS_ENABLED;
5264 	}
5265 
5266 	/* Clear any previous error indication. */
5267 	ill->ill_error = 0;
5268 	freemsg(mp);
5269 }
5270 
5271 /*
5272  * Perform various checks to verify that an address would make sense as a
5273  * local, remote, or subnet interface address.
5274  */
5275 static boolean_t
5276 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5277 {
5278 	ipaddr_t	net_mask;
5279 
5280 	/*
5281 	 * Don't allow all zeroes, all ones or experimental address, but allow
5282 	 * all ones netmask.
5283 	 */
5284 	if ((net_mask = ip_net_mask(addr)) == 0)
5285 		return (B_FALSE);
5286 	/* A given netmask overrides the "guess" netmask */
5287 	if (subnet_mask != 0)
5288 		net_mask = subnet_mask;
5289 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5290 	    (addr == (addr | ~net_mask)))) {
5291 		return (B_FALSE);
5292 	}
5293 	if (CLASSD(addr))
5294 		return (B_FALSE);
5295 
5296 	return (B_TRUE);
5297 }
5298 
5299 /*
5300  * ipif_lookup_group
5301  * Returns held ipif
5302  */
5303 ipif_t *
5304 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5305 {
5306 	ire_t	*ire;
5307 	ipif_t	*ipif;
5308 
5309 	ire = ire_lookup_multi(group, zoneid);
5310 	if (ire == NULL)
5311 		return (NULL);
5312 	ipif = ire->ire_ipif;
5313 	ipif_refhold(ipif);
5314 	ire_refrele(ire);
5315 	return (ipif);
5316 }
5317 
5318 /*
5319  * Look for an ipif with the specified interface address and destination.
5320  * The destination address is used only for matching point-to-point interfaces.
5321  */
5322 ipif_t *
5323 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5324     ipsq_func_t func, int *error)
5325 {
5326 	ipif_t	*ipif;
5327 	ill_t	*ill;
5328 	ill_walk_context_t ctx;
5329 	ipsq_t	*ipsq;
5330 
5331 	if (error != NULL)
5332 		*error = 0;
5333 
5334 	/*
5335 	 * First match all the point-to-point interfaces
5336 	 * before looking at non-point-to-point interfaces.
5337 	 * This is done to avoid returning non-point-to-point
5338 	 * ipif instead of unnumbered point-to-point ipif.
5339 	 */
5340 	rw_enter(&ill_g_lock, RW_READER);
5341 	ill = ILL_START_WALK_V4(&ctx);
5342 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5343 		GRAB_CONN_LOCK(q);
5344 		mutex_enter(&ill->ill_lock);
5345 		for (ipif = ill->ill_ipif; ipif != NULL;
5346 		    ipif = ipif->ipif_next) {
5347 			/* Allow the ipif to be down */
5348 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5349 			    (ipif->ipif_lcl_addr == if_addr) &&
5350 			    (ipif->ipif_pp_dst_addr == dst)) {
5351 				/*
5352 				 * The block comment at the start of ipif_down
5353 				 * explains the use of the macros used below
5354 				 */
5355 				if (IPIF_CAN_LOOKUP(ipif)) {
5356 					ipif_refhold_locked(ipif);
5357 					mutex_exit(&ill->ill_lock);
5358 					RELEASE_CONN_LOCK(q);
5359 					rw_exit(&ill_g_lock);
5360 					return (ipif);
5361 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5362 					ipsq = ill->ill_phyint->phyint_ipsq;
5363 					mutex_enter(&ipsq->ipsq_lock);
5364 					mutex_exit(&ill->ill_lock);
5365 					rw_exit(&ill_g_lock);
5366 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5367 						ill);
5368 					mutex_exit(&ipsq->ipsq_lock);
5369 					RELEASE_CONN_LOCK(q);
5370 					*error = EINPROGRESS;
5371 					return (NULL);
5372 				}
5373 			}
5374 		}
5375 		mutex_exit(&ill->ill_lock);
5376 		RELEASE_CONN_LOCK(q);
5377 	}
5378 	rw_exit(&ill_g_lock);
5379 
5380 	/* lookup the ipif based on interface address */
5381 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5382 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5383 	return (ipif);
5384 }
5385 
5386 /*
5387  * Look for an ipif with the specified address. For point-point links
5388  * we look for matches on either the destination address and the local
5389  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5390  * is set.
5391  * Matches on a specific ill if match_ill is set.
5392  */
5393 ipif_t *
5394 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5395     mblk_t *mp, ipsq_func_t func, int *error)
5396 {
5397 	ipif_t  *ipif;
5398 	ill_t   *ill;
5399 	boolean_t ptp = B_FALSE;
5400 	ipsq_t	*ipsq;
5401 	ill_walk_context_t	ctx;
5402 
5403 	if (error != NULL)
5404 		*error = 0;
5405 
5406 	rw_enter(&ill_g_lock, RW_READER);
5407 	/*
5408 	 * Repeat twice, first based on local addresses and
5409 	 * next time for pointopoint.
5410 	 */
5411 repeat:
5412 	ill = ILL_START_WALK_V4(&ctx);
5413 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5414 		if (match_ill != NULL && ill != match_ill) {
5415 			continue;
5416 		}
5417 		GRAB_CONN_LOCK(q);
5418 		mutex_enter(&ill->ill_lock);
5419 		for (ipif = ill->ill_ipif; ipif != NULL;
5420 		    ipif = ipif->ipif_next) {
5421 			if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5422 				continue;
5423 			/* Allow the ipif to be down */
5424 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5425 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5426 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5427 			    (ipif->ipif_pp_dst_addr == addr))) {
5428 				/*
5429 				 * The block comment at the start of ipif_down
5430 				 * explains the use of the macros used below
5431 				 */
5432 				if (IPIF_CAN_LOOKUP(ipif)) {
5433 					ipif_refhold_locked(ipif);
5434 					mutex_exit(&ill->ill_lock);
5435 					RELEASE_CONN_LOCK(q);
5436 					rw_exit(&ill_g_lock);
5437 					return (ipif);
5438 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5439 					ipsq = ill->ill_phyint->phyint_ipsq;
5440 					mutex_enter(&ipsq->ipsq_lock);
5441 					mutex_exit(&ill->ill_lock);
5442 					rw_exit(&ill_g_lock);
5443 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5444 						ill);
5445 					mutex_exit(&ipsq->ipsq_lock);
5446 					RELEASE_CONN_LOCK(q);
5447 					*error = EINPROGRESS;
5448 					return (NULL);
5449 				}
5450 			}
5451 		}
5452 		mutex_exit(&ill->ill_lock);
5453 		RELEASE_CONN_LOCK(q);
5454 	}
5455 
5456 	/* Now try the ptp case */
5457 	if (ptp) {
5458 		rw_exit(&ill_g_lock);
5459 		if (error != NULL)
5460 			*error = ENXIO;
5461 		return (NULL);
5462 	}
5463 	ptp = B_TRUE;
5464 	goto repeat;
5465 }
5466 
5467 /*
5468  * Look for an ipif that matches the specified remote address i.e. the
5469  * ipif that would receive the specified packet.
5470  * First look for directly connected interfaces and then do a recursive
5471  * IRE lookup and pick the first ipif corresponding to the source address in the
5472  * ire.
5473  * Returns: held ipif
5474  */
5475 ipif_t *
5476 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5477 {
5478 	ipif_t	*ipif;
5479 	ire_t	*ire;
5480 
5481 	ASSERT(!ill->ill_isv6);
5482 
5483 	/*
5484 	 * Someone could be changing this ipif currently or change it
5485 	 * after we return this. Thus  a few packets could use the old
5486 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5487 	 * will atomically be updated or cleaned up with the new value
5488 	 * Thus we don't need a lock to check the flags or other attrs below.
5489 	 */
5490 	mutex_enter(&ill->ill_lock);
5491 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5492 		if (!IPIF_CAN_LOOKUP(ipif))
5493 			continue;
5494 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5495 			continue;
5496 		/* Allow the ipif to be down */
5497 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5498 			if ((ipif->ipif_pp_dst_addr == addr) ||
5499 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5500 			    ipif->ipif_lcl_addr == addr)) {
5501 				ipif_refhold_locked(ipif);
5502 				mutex_exit(&ill->ill_lock);
5503 				return (ipif);
5504 			}
5505 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5506 			ipif_refhold_locked(ipif);
5507 			mutex_exit(&ill->ill_lock);
5508 			return (ipif);
5509 		}
5510 	}
5511 	mutex_exit(&ill->ill_lock);
5512 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5513 	    MATCH_IRE_RECURSIVE);
5514 	if (ire != NULL) {
5515 		/*
5516 		 * The callers of this function wants to know the
5517 		 * interface on which they have to send the replies
5518 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5519 		 * derived from different ills, we really don't care
5520 		 * what we return here.
5521 		 */
5522 		ipif = ire->ire_ipif;
5523 		if (ipif != NULL) {
5524 			ipif_refhold(ipif);
5525 			ire_refrele(ire);
5526 			return (ipif);
5527 		}
5528 		ire_refrele(ire);
5529 	}
5530 	/* Pick the first interface */
5531 	ipif = ipif_get_next_ipif(NULL, ill);
5532 	return (ipif);
5533 }
5534 
5535 /*
5536  * This func does not prevent refcnt from increasing. But if
5537  * the caller has taken steps to that effect, then this func
5538  * can be used to determine whether the ill has become quiescent
5539  */
5540 boolean_t
5541 ill_is_quiescent(ill_t *ill)
5542 {
5543 	ipif_t	*ipif;
5544 
5545 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5546 
5547 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5548 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5549 			return (B_FALSE);
5550 	}
5551 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5552 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5553 	    ill->ill_mrtun_refcnt != 0)
5554 		return (B_FALSE);
5555 	return (B_TRUE);
5556 }
5557 
5558 /*
5559  * This func does not prevent refcnt from increasing. But if
5560  * the caller has taken steps to that effect, then this func
5561  * can be used to determine whether the ipif has become quiescent
5562  */
5563 static boolean_t
5564 ipif_is_quiescent(ipif_t *ipif)
5565 {
5566 	ill_t *ill;
5567 
5568 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5569 
5570 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5571 		return (B_FALSE);
5572 
5573 	ill = ipif->ipif_ill;
5574 	if (ill->ill_ipif_up_count != 0 || ill->ill_logical_down)
5575 		return (B_TRUE);
5576 
5577 	/* This is the last ipif going down or being deleted on this ill */
5578 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0)
5579 		return (B_FALSE);
5580 
5581 	return (B_TRUE);
5582 }
5583 
5584 /*
5585  * This func does not prevent refcnt from increasing. But if
5586  * the caller has taken steps to that effect, then this func
5587  * can be used to determine whether the ipifs marked with IPIF_MOVING
5588  * have become quiescent and can be moved in a failover/failback.
5589  */
5590 static ipif_t *
5591 ill_quiescent_to_move(ill_t *ill)
5592 {
5593 	ipif_t  *ipif;
5594 
5595 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5596 
5597 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5598 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5599 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5600 				return (ipif);
5601 			}
5602 		}
5603 	}
5604 	return (NULL);
5605 }
5606 
5607 /*
5608  * The ipif/ill/ire has been refreled. Do the tail processing.
5609  * Determine if the ipif or ill in question has become quiescent and if so
5610  * wakeup close and/or restart any queued pending ioctl that is waiting
5611  * for the ipif_down (or ill_down)
5612  */
5613 void
5614 ipif_ill_refrele_tail(ill_t *ill)
5615 {
5616 	mblk_t	*mp;
5617 	conn_t	*connp;
5618 	ipsq_t	*ipsq;
5619 	ipif_t	*ipif;
5620 
5621 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5622 
5623 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5624 	    ill_is_quiescent(ill)) {
5625 		/* ill_close may be waiting */
5626 		cv_broadcast(&ill->ill_cv);
5627 	}
5628 
5629 	/* ipsq can't change because ill_lock  is held */
5630 	ipsq = ill->ill_phyint->phyint_ipsq;
5631 	if (ipsq->ipsq_waitfor == 0) {
5632 		/* Not waiting for anything, just return. */
5633 		mutex_exit(&ill->ill_lock);
5634 		return;
5635 	}
5636 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5637 		ipsq->ipsq_pending_ipif != NULL);
5638 	/*
5639 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5640 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5641 	 * be zero for restarting an ioctl that ends up downing the ill.
5642 	 */
5643 	ipif = ipsq->ipsq_pending_ipif;
5644 	if (ipif->ipif_ill != ill) {
5645 		/* The ioctl is pending on some other ill. */
5646 		mutex_exit(&ill->ill_lock);
5647 		return;
5648 	}
5649 
5650 	switch (ipsq->ipsq_waitfor) {
5651 	case IPIF_DOWN:
5652 	case IPIF_FREE:
5653 		if (!ipif_is_quiescent(ipif)) {
5654 			mutex_exit(&ill->ill_lock);
5655 			return;
5656 		}
5657 		break;
5658 
5659 	case ILL_DOWN:
5660 	case ILL_FREE:
5661 		/*
5662 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5663 		 * waits synchronously in ip_close, and no message is queued in
5664 		 * ipsq_pending_mp at all in this case
5665 		 */
5666 		if (!ill_is_quiescent(ill)) {
5667 			mutex_exit(&ill->ill_lock);
5668 			return;
5669 		}
5670 
5671 		break;
5672 
5673 	case ILL_MOVE_OK:
5674 		if (ill_quiescent_to_move(ill) != NULL) {
5675 			mutex_exit(&ill->ill_lock);
5676 			return;
5677 		}
5678 
5679 		break;
5680 	default:
5681 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5682 		    (void *)ipsq, ipsq->ipsq_waitfor);
5683 	}
5684 
5685 	/*
5686 	 * Incr refcnt for the qwriter_ip call below which
5687 	 * does a refrele
5688 	 */
5689 	ill_refhold_locked(ill);
5690 	mutex_exit(&ill->ill_lock);
5691 
5692 	mp = ipsq_pending_mp_get(ipsq, &connp);
5693 	ASSERT(mp != NULL);
5694 
5695 	switch (mp->b_datap->db_type) {
5696 	case M_ERROR:
5697 	case M_HANGUP:
5698 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5699 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5700 		return;
5701 
5702 	case M_IOCTL:
5703 	case M_IOCDATA:
5704 		(void) qwriter_ip(NULL, ill,
5705 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5706 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5707 		return;
5708 
5709 	default:
5710 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5711 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5712 	}
5713 }
5714 
5715 #ifdef ILL_DEBUG
5716 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5717 void
5718 th_trace_rrecord(th_trace_t *th_trace)
5719 {
5720 	tr_buf_t *tr_buf;
5721 	uint_t lastref;
5722 
5723 	lastref = th_trace->th_trace_lastref;
5724 	lastref++;
5725 	if (lastref == TR_BUF_MAX)
5726 		lastref = 0;
5727 	th_trace->th_trace_lastref = lastref;
5728 	tr_buf = &th_trace->th_trbuf[lastref];
5729 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5730 }
5731 
5732 th_trace_t *
5733 th_trace_ipif_lookup(ipif_t *ipif)
5734 {
5735 	int bucket_id;
5736 	th_trace_t *th_trace;
5737 
5738 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5739 
5740 	bucket_id = IP_TR_HASH(curthread);
5741 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5742 
5743 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5744 	    th_trace = th_trace->th_next) {
5745 		if (th_trace->th_id == curthread)
5746 			return (th_trace);
5747 	}
5748 	return (NULL);
5749 }
5750 
5751 void
5752 ipif_trace_ref(ipif_t *ipif)
5753 {
5754 	int bucket_id;
5755 	th_trace_t *th_trace;
5756 
5757 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5758 
5759 	if (ipif->ipif_trace_disable)
5760 		return;
5761 
5762 	/*
5763 	 * Attempt to locate the trace buffer for the curthread.
5764 	 * If it does not exist, then allocate a new trace buffer
5765 	 * and link it in list of trace bufs for this ipif, at the head
5766 	 */
5767 	th_trace = th_trace_ipif_lookup(ipif);
5768 	if (th_trace == NULL) {
5769 		bucket_id = IP_TR_HASH(curthread);
5770 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5771 		    KM_NOSLEEP);
5772 		if (th_trace == NULL) {
5773 			ipif->ipif_trace_disable = B_TRUE;
5774 			ipif_trace_cleanup(ipif);
5775 			return;
5776 		}
5777 		th_trace->th_id = curthread;
5778 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5779 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5780 		if (th_trace->th_next != NULL)
5781 			th_trace->th_next->th_prev = &th_trace->th_next;
5782 		ipif->ipif_trace[bucket_id] = th_trace;
5783 	}
5784 	ASSERT(th_trace->th_refcnt >= 0 &&
5785 		th_trace->th_refcnt < TR_BUF_MAX -1);
5786 	th_trace->th_refcnt++;
5787 	th_trace_rrecord(th_trace);
5788 }
5789 
5790 void
5791 ipif_untrace_ref(ipif_t *ipif)
5792 {
5793 	th_trace_t *th_trace;
5794 
5795 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5796 
5797 	if (ipif->ipif_trace_disable)
5798 		return;
5799 	th_trace = th_trace_ipif_lookup(ipif);
5800 	ASSERT(th_trace != NULL);
5801 	ASSERT(th_trace->th_refcnt > 0);
5802 
5803 	th_trace->th_refcnt--;
5804 	th_trace_rrecord(th_trace);
5805 }
5806 
5807 th_trace_t *
5808 th_trace_ill_lookup(ill_t *ill)
5809 {
5810 	th_trace_t *th_trace;
5811 	int bucket_id;
5812 
5813 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5814 
5815 	bucket_id = IP_TR_HASH(curthread);
5816 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5817 
5818 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
5819 	    th_trace = th_trace->th_next) {
5820 		if (th_trace->th_id == curthread)
5821 			return (th_trace);
5822 	}
5823 	return (NULL);
5824 }
5825 
5826 void
5827 ill_trace_ref(ill_t *ill)
5828 {
5829 	int bucket_id;
5830 	th_trace_t *th_trace;
5831 
5832 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5833 	if (ill->ill_trace_disable)
5834 		return;
5835 	/*
5836 	 * Attempt to locate the trace buffer for the curthread.
5837 	 * If it does not exist, then allocate a new trace buffer
5838 	 * and link it in list of trace bufs for this ill, at the head
5839 	 */
5840 	th_trace = th_trace_ill_lookup(ill);
5841 	if (th_trace == NULL) {
5842 		bucket_id = IP_TR_HASH(curthread);
5843 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5844 		    KM_NOSLEEP);
5845 		if (th_trace == NULL) {
5846 			ill->ill_trace_disable = B_TRUE;
5847 			ill_trace_cleanup(ill);
5848 			return;
5849 		}
5850 		th_trace->th_id = curthread;
5851 		th_trace->th_next = ill->ill_trace[bucket_id];
5852 		th_trace->th_prev = &ill->ill_trace[bucket_id];
5853 		if (th_trace->th_next != NULL)
5854 			th_trace->th_next->th_prev = &th_trace->th_next;
5855 		ill->ill_trace[bucket_id] = th_trace;
5856 	}
5857 	ASSERT(th_trace->th_refcnt >= 0 &&
5858 		th_trace->th_refcnt < TR_BUF_MAX - 1);
5859 
5860 	th_trace->th_refcnt++;
5861 	th_trace_rrecord(th_trace);
5862 }
5863 
5864 void
5865 ill_untrace_ref(ill_t *ill)
5866 {
5867 	th_trace_t *th_trace;
5868 
5869 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5870 
5871 	if (ill->ill_trace_disable)
5872 		return;
5873 	th_trace = th_trace_ill_lookup(ill);
5874 	ASSERT(th_trace != NULL);
5875 	ASSERT(th_trace->th_refcnt > 0);
5876 
5877 	th_trace->th_refcnt--;
5878 	th_trace_rrecord(th_trace);
5879 }
5880 
5881 /*
5882  * Verify that this thread has no refs to the ipif and free
5883  * the trace buffers
5884  */
5885 /* ARGSUSED */
5886 void
5887 ipif_thread_exit(ipif_t *ipif, void *dummy)
5888 {
5889 	th_trace_t *th_trace;
5890 
5891 	mutex_enter(&ipif->ipif_ill->ill_lock);
5892 
5893 	th_trace = th_trace_ipif_lookup(ipif);
5894 	if (th_trace == NULL) {
5895 		mutex_exit(&ipif->ipif_ill->ill_lock);
5896 		return;
5897 	}
5898 	ASSERT(th_trace->th_refcnt == 0);
5899 	/* unlink th_trace and free it */
5900 	*th_trace->th_prev = th_trace->th_next;
5901 	if (th_trace->th_next != NULL)
5902 		th_trace->th_next->th_prev = th_trace->th_prev;
5903 	th_trace->th_next = NULL;
5904 	th_trace->th_prev = NULL;
5905 	kmem_free(th_trace, sizeof (th_trace_t));
5906 
5907 	mutex_exit(&ipif->ipif_ill->ill_lock);
5908 }
5909 
5910 /*
5911  * Verify that this thread has no refs to the ill and free
5912  * the trace buffers
5913  */
5914 /* ARGSUSED */
5915 void
5916 ill_thread_exit(ill_t *ill, void *dummy)
5917 {
5918 	th_trace_t *th_trace;
5919 
5920 	mutex_enter(&ill->ill_lock);
5921 
5922 	th_trace = th_trace_ill_lookup(ill);
5923 	if (th_trace == NULL) {
5924 		mutex_exit(&ill->ill_lock);
5925 		return;
5926 	}
5927 	ASSERT(th_trace->th_refcnt == 0);
5928 	/* unlink th_trace and free it */
5929 	*th_trace->th_prev = th_trace->th_next;
5930 	if (th_trace->th_next != NULL)
5931 		th_trace->th_next->th_prev = th_trace->th_prev;
5932 	th_trace->th_next = NULL;
5933 	th_trace->th_prev = NULL;
5934 	kmem_free(th_trace, sizeof (th_trace_t));
5935 
5936 	mutex_exit(&ill->ill_lock);
5937 }
5938 #endif
5939 
5940 #ifdef ILL_DEBUG
5941 void
5942 ip_thread_exit(void)
5943 {
5944 	ill_t	*ill;
5945 	ipif_t	*ipif;
5946 	ill_walk_context_t	ctx;
5947 
5948 	rw_enter(&ill_g_lock, RW_READER);
5949 	ill = ILL_START_WALK_ALL(&ctx);
5950 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5951 		for (ipif = ill->ill_ipif; ipif != NULL;
5952 		    ipif = ipif->ipif_next) {
5953 			ipif_thread_exit(ipif, NULL);
5954 		}
5955 		ill_thread_exit(ill, NULL);
5956 	}
5957 	rw_exit(&ill_g_lock);
5958 
5959 	ire_walk(ire_thread_exit, NULL);
5960 	ndp_walk_impl(NULL, nce_thread_exit, NULL, B_FALSE);
5961 }
5962 
5963 /*
5964  * Called when ipif is unplumbed or when memory alloc fails
5965  */
5966 void
5967 ipif_trace_cleanup(ipif_t *ipif)
5968 {
5969 	int	i;
5970 	th_trace_t	*th_trace;
5971 	th_trace_t	*th_trace_next;
5972 
5973 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
5974 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
5975 		    th_trace = th_trace_next) {
5976 			th_trace_next = th_trace->th_next;
5977 			kmem_free(th_trace, sizeof (th_trace_t));
5978 		}
5979 		ipif->ipif_trace[i] = NULL;
5980 	}
5981 }
5982 
5983 /*
5984  * Called when ill is unplumbed or when memory alloc fails
5985  */
5986 void
5987 ill_trace_cleanup(ill_t *ill)
5988 {
5989 	int	i;
5990 	th_trace_t	*th_trace;
5991 	th_trace_t	*th_trace_next;
5992 
5993 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
5994 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
5995 		    th_trace = th_trace_next) {
5996 			th_trace_next = th_trace->th_next;
5997 			kmem_free(th_trace, sizeof (th_trace_t));
5998 		}
5999 		ill->ill_trace[i] = NULL;
6000 	}
6001 }
6002 
6003 #else
6004 void ip_thread_exit(void) {}
6005 #endif
6006 
6007 void
6008 ipif_refhold_locked(ipif_t *ipif)
6009 {
6010 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6011 	ipif->ipif_refcnt++;
6012 	IPIF_TRACE_REF(ipif);
6013 }
6014 
6015 void
6016 ipif_refhold(ipif_t *ipif)
6017 {
6018 	ill_t	*ill;
6019 
6020 	ill = ipif->ipif_ill;
6021 	mutex_enter(&ill->ill_lock);
6022 	ipif->ipif_refcnt++;
6023 	IPIF_TRACE_REF(ipif);
6024 	mutex_exit(&ill->ill_lock);
6025 }
6026 
6027 /*
6028  * Must not be called while holding any locks. Otherwise if this is
6029  * the last reference to be released there is a chance of recursive mutex
6030  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6031  * to restart an ioctl.
6032  */
6033 void
6034 ipif_refrele(ipif_t *ipif)
6035 {
6036 	ill_t	*ill;
6037 
6038 	ill = ipif->ipif_ill;
6039 
6040 	mutex_enter(&ill->ill_lock);
6041 	ASSERT(ipif->ipif_refcnt != 0);
6042 	ipif->ipif_refcnt--;
6043 	IPIF_UNTRACE_REF(ipif);
6044 	if (ipif->ipif_refcnt != 0) {
6045 		mutex_exit(&ill->ill_lock);
6046 		return;
6047 	}
6048 
6049 	/* Drops the ill_lock */
6050 	ipif_ill_refrele_tail(ill);
6051 }
6052 
6053 ipif_t *
6054 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6055 {
6056 	ipif_t	*ipif;
6057 
6058 	mutex_enter(&ill->ill_lock);
6059 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6060 	    ipif != NULL; ipif = ipif->ipif_next) {
6061 		if (!IPIF_CAN_LOOKUP(ipif))
6062 			continue;
6063 		ipif_refhold_locked(ipif);
6064 		mutex_exit(&ill->ill_lock);
6065 		return (ipif);
6066 	}
6067 	mutex_exit(&ill->ill_lock);
6068 	return (NULL);
6069 }
6070 
6071 /*
6072  * TODO: make this table extendible at run time
6073  * Return a pointer to the mac type info for 'mac_type'
6074  */
6075 static ip_m_t *
6076 ip_m_lookup(t_uscalar_t mac_type)
6077 {
6078 	ip_m_t	*ipm;
6079 
6080 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6081 		if (ipm->ip_m_mac_type == mac_type)
6082 			return (ipm);
6083 	return (NULL);
6084 }
6085 
6086 /*
6087  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6088  * ipif_arg is passed in to associate it with the correct interface.
6089  * We may need to restart this operation if the ipif cannot be looked up
6090  * due to an exclusive operation that is currently in progress. The restart
6091  * entry point is specified by 'func'
6092  */
6093 int
6094 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6095     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6096     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6097     ipsq_func_t func)
6098 {
6099 	ire_t	*ire;
6100 	ire_t	*gw_ire = NULL;
6101 	ipif_t	*ipif = NULL;
6102 	boolean_t ipif_refheld = B_FALSE;
6103 	uint_t	type;
6104 	int	match_flags = MATCH_IRE_TYPE;
6105 	int	error;
6106 
6107 	ip1dbg(("ip_rt_add:"));
6108 
6109 	if (ire_arg != NULL)
6110 		*ire_arg = NULL;
6111 
6112 	/*
6113 	 * If this is the case of RTF_HOST being set, then we set the netmask
6114 	 * to all ones (regardless if one was supplied).
6115 	 */
6116 	if (flags & RTF_HOST)
6117 		mask = IP_HOST_MASK;
6118 
6119 	/*
6120 	 * Prevent routes with a zero gateway from being created (since
6121 	 * interfaces can currently be plumbed and brought up no assigned
6122 	 * address).
6123 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6124 	 */
6125 	if (gw_addr == 0 && src_ipif == NULL)
6126 		return (ENETUNREACH);
6127 	/*
6128 	 * Get the ipif, if any, corresponding to the gw_addr
6129 	 */
6130 	if (gw_addr != 0) {
6131 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6132 		    &error);
6133 		if (ipif != NULL) {
6134 			if (IS_VNI(ipif->ipif_ill)) {
6135 				ipif_refrele(ipif);
6136 				return (EINVAL);
6137 			}
6138 			ipif_refheld = B_TRUE;
6139 		} else if (error == EINPROGRESS) {
6140 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6141 			return (EINPROGRESS);
6142 		} else {
6143 			error = 0;
6144 		}
6145 	}
6146 
6147 	if (ipif != NULL) {
6148 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6149 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6150 	} else {
6151 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6152 	}
6153 
6154 	/*
6155 	 * GateD will attempt to create routes with a loopback interface
6156 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6157 	 * these routes to be added, but create them as interface routes
6158 	 * since the gateway is an interface address.
6159 	 */
6160 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK))
6161 		flags &= ~RTF_GATEWAY;
6162 
6163 	/*
6164 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6165 	 * and the gateway address provided is one of the system's interface
6166 	 * addresses.  By using the routing socket interface and supplying an
6167 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6168 	 * specifying an interface route to be created is available which uses
6169 	 * the interface index that specifies the outgoing interface rather than
6170 	 * the address of an outgoing interface (which may not be able to
6171 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6172 	 * flag, routes can be specified which not only specify the next-hop to
6173 	 * be used when routing to a certain prefix, but also which outgoing
6174 	 * interface should be used.
6175 	 *
6176 	 * Previously, interfaces would have unique addresses assigned to them
6177 	 * and so the address assigned to a particular interface could be used
6178 	 * to identify a particular interface.  One exception to this was the
6179 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6180 	 *
6181 	 * With the advent of IPv6 and its link-local addresses, this
6182 	 * restriction was relaxed and interfaces could share addresses between
6183 	 * themselves.  In fact, typically all of the link-local interfaces on
6184 	 * an IPv6 node or router will have the same link-local address.  In
6185 	 * order to differentiate between these interfaces, the use of an
6186 	 * interface index is necessary and this index can be carried inside a
6187 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6188 	 * of using the interface index, however, is that all of the ipif's that
6189 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6190 	 * cannot be used to differentiate between ipif's (or logical
6191 	 * interfaces) that belong to the same ill (physical interface).
6192 	 *
6193 	 * For example, in the following case involving IPv4 interfaces and
6194 	 * logical interfaces
6195 	 *
6196 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6197 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6198 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6199 	 *
6200 	 * the ipif's corresponding to each of these interface routes can be
6201 	 * uniquely identified by the "gateway" (actually interface address).
6202 	 *
6203 	 * In this case involving multiple IPv6 default routes to a particular
6204 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6205 	 * default route is of interest:
6206 	 *
6207 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6208 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6209 	 */
6210 
6211 	/* RTF_GATEWAY not set */
6212 	if (!(flags & RTF_GATEWAY)) {
6213 		queue_t	*stq;
6214 		queue_t	*rfq = NULL;
6215 		ill_t	*in_ill = NULL;
6216 
6217 		/*
6218 		 * As the interface index specified with the RTA_IFP sockaddr is
6219 		 * the same for all ipif's off of an ill, the matching logic
6220 		 * below uses MATCH_IRE_ILL if such an index was specified.
6221 		 * This means that routes sharing the same prefix when added
6222 		 * using a RTA_IFP sockaddr must have distinct interface
6223 		 * indices (namely, they must be on distinct ill's).
6224 		 *
6225 		 * On the other hand, since the gateway address will usually be
6226 		 * different for each ipif on the system, the matching logic
6227 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6228 		 * route.  This means that interface routes for the same prefix
6229 		 * can be created if they belong to distinct ipif's and if a
6230 		 * RTA_IFP sockaddr is not present.
6231 		 */
6232 		if (ipif_arg != NULL) {
6233 			if (ipif_refheld)  {
6234 				ipif_refrele(ipif);
6235 				ipif_refheld = B_FALSE;
6236 			}
6237 			ipif = ipif_arg;
6238 			match_flags |= MATCH_IRE_ILL;
6239 		} else {
6240 			/*
6241 			 * Check the ipif corresponding to the gw_addr
6242 			 */
6243 			if (ipif == NULL)
6244 				return (ENETUNREACH);
6245 			match_flags |= MATCH_IRE_IPIF;
6246 		}
6247 		ASSERT(ipif != NULL);
6248 		/*
6249 		 * If src_ipif is not NULL, we have to create
6250 		 * an ire with non-null ire_in_ill value
6251 		 */
6252 		if (src_ipif != NULL) {
6253 			in_ill = src_ipif->ipif_ill;
6254 		}
6255 
6256 		/*
6257 		 * We check for an existing entry at this point.
6258 		 *
6259 		 * Since a netmask isn't passed in via the ioctl interface
6260 		 * (SIOCADDRT), we don't check for a matching netmask in that
6261 		 * case.
6262 		 */
6263 		if (!ioctl_msg)
6264 			match_flags |= MATCH_IRE_MASK;
6265 		if (src_ipif != NULL) {
6266 			/* Look up in the special table */
6267 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6268 			    ipif, src_ipif->ipif_ill, match_flags);
6269 		} else {
6270 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6271 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6272 			    match_flags);
6273 		}
6274 		if (ire != NULL) {
6275 			ire_refrele(ire);
6276 			if (ipif_refheld)
6277 				ipif_refrele(ipif);
6278 			return (EEXIST);
6279 		}
6280 
6281 		if (src_ipif != NULL) {
6282 			/*
6283 			 * Create the special ire for the IRE table
6284 			 * which hangs out of ire_in_ill. This ire
6285 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6286 			 * Thus rfq is non-NULL.
6287 			 */
6288 			rfq = ipif->ipif_rq;
6289 		}
6290 		/* Create the usual interface ires */
6291 
6292 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6293 		    ? ipif->ipif_rq : ipif->ipif_wq;
6294 
6295 		/*
6296 		 * Create a copy of the IRE_LOOPBACK,
6297 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6298 		 * the modified address and netmask.
6299 		 */
6300 		ire = ire_create(
6301 		    (uchar_t *)&dst_addr,
6302 		    (uint8_t *)&mask,
6303 		    (uint8_t *)&ipif->ipif_src_addr,
6304 		    NULL,
6305 		    NULL,
6306 		    &ipif->ipif_mtu,
6307 		    NULL,
6308 		    rfq,
6309 		    stq,
6310 		    ipif->ipif_net_type,
6311 		    ipif->ipif_resolver_mp,
6312 		    ipif,
6313 		    in_ill,
6314 		    0,
6315 		    0,
6316 		    0,
6317 		    flags,
6318 		    &ire_uinfo_null);
6319 		if (ire == NULL) {
6320 			if (ipif_refheld)
6321 				ipif_refrele(ipif);
6322 			return (ENOMEM);
6323 		}
6324 
6325 		/*
6326 		 * Some software (for example, GateD and Sun Cluster) attempts
6327 		 * to create (what amount to) IRE_PREFIX routes with the
6328 		 * loopback address as the gateway.  This is primarily done to
6329 		 * set up prefixes with the RTF_REJECT flag set (for example,
6330 		 * when generating aggregate routes.)
6331 		 *
6332 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6333 		 * IRE_LOOPBACK, then we map the request into a
6334 		 * IRE_IF_NORESOLVER.
6335 		 *
6336 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6337 		 * routine, but rather using ire_create() directly.
6338 		 */
6339 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6340 			ire->ire_type = IRE_IF_NORESOLVER;
6341 		error = ire_add(&ire, q, mp, func);
6342 		if (error == 0)
6343 			goto save_ire;
6344 
6345 		/*
6346 		 * In the result of failure, ire_add() will have already
6347 		 * deleted the ire in question, so there is no need to
6348 		 * do that here.
6349 		 */
6350 		if (ipif_refheld)
6351 			ipif_refrele(ipif);
6352 		return (error);
6353 	}
6354 	if (ipif_refheld) {
6355 		ipif_refrele(ipif);
6356 		ipif_refheld = B_FALSE;
6357 	}
6358 
6359 	if (src_ipif != NULL) {
6360 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6361 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6362 		return (EINVAL);
6363 	}
6364 	/*
6365 	 * Get an interface IRE for the specified gateway.
6366 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6367 	 * gateway, it is currently unreachable and we fail the request
6368 	 * accordingly.
6369 	 */
6370 	ipif = ipif_arg;
6371 	if (ipif_arg != NULL)
6372 		match_flags |= MATCH_IRE_ILL;
6373 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6374 	    ALL_ZONES, 0, match_flags);
6375 	if (gw_ire == NULL)
6376 		return (ENETUNREACH);
6377 
6378 	/*
6379 	 * We create one of three types of IREs as a result of this request
6380 	 * based on the netmask.  A netmask of all ones (which is automatically
6381 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6382 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6383 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6384 	 * destination prefix.
6385 	 */
6386 	if (mask == IP_HOST_MASK)
6387 		type = IRE_HOST;
6388 	else if (mask == 0)
6389 		type = IRE_DEFAULT;
6390 	else
6391 		type = IRE_PREFIX;
6392 
6393 	/* check for a duplicate entry */
6394 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6395 	    NULL, ALL_ZONES, 0, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6396 	if (ire != NULL) {
6397 		ire_refrele(gw_ire);
6398 		ire_refrele(ire);
6399 		return (EEXIST);
6400 	}
6401 
6402 	/* Create the IRE. */
6403 	ire = ire_create(
6404 	    (uchar_t *)&dst_addr,		/* dest address */
6405 	    (uchar_t *)&mask,			/* mask */
6406 	    /* src address assigned by the caller? */
6407 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6408 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6409 	    (uchar_t *)&gw_addr,		/* gateway address */
6410 	    NULL,				/* no in-srcaddress */
6411 	    &gw_ire->ire_max_frag,
6412 	    NULL,				/* no Fast Path header */
6413 	    NULL,				/* no recv-from queue */
6414 	    NULL,				/* no send-to queue */
6415 	    (ushort_t)type,			/* IRE type */
6416 	    NULL,
6417 	    ipif_arg,
6418 	    NULL,
6419 	    0,
6420 	    0,
6421 	    0,
6422 	    flags,
6423 	    &gw_ire->ire_uinfo);		/* Inherit ULP info from gw */
6424 	if (ire == NULL) {
6425 		ire_refrele(gw_ire);
6426 		return (ENOMEM);
6427 	}
6428 
6429 	/*
6430 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6431 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6432 	 */
6433 
6434 	/* Add the new IRE. */
6435 	error = ire_add(&ire, q, mp, func);
6436 	if (error != 0) {
6437 		/*
6438 		 * In the result of failure, ire_add() will have already
6439 		 * deleted the ire in question, so there is no need to
6440 		 * do that here.
6441 		 */
6442 		ire_refrele(gw_ire);
6443 		return (error);
6444 	}
6445 
6446 	if (flags & RTF_MULTIRT) {
6447 		/*
6448 		 * Invoke the CGTP (multirouting) filtering module
6449 		 * to add the dst address in the filtering database.
6450 		 * Replicated inbound packets coming from that address
6451 		 * will be filtered to discard the duplicates.
6452 		 * It is not necessary to call the CGTP filter hook
6453 		 * when the dst address is a broadcast or multicast,
6454 		 * because an IP source address cannot be a broadcast
6455 		 * or a multicast.
6456 		 */
6457 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6458 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
6459 		if (ire_dst != NULL) {
6460 			ip_cgtp_bcast_add(ire, ire_dst);
6461 			ire_refrele(ire_dst);
6462 			goto save_ire;
6463 		}
6464 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6465 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6466 			    ire->ire_addr,
6467 			    ire->ire_gateway_addr,
6468 			    ire->ire_src_addr,
6469 			    gw_ire->ire_src_addr);
6470 			if (res != 0) {
6471 				ire_refrele(gw_ire);
6472 				ire_delete(ire);
6473 				return (res);
6474 			}
6475 		}
6476 	}
6477 
6478 save_ire:
6479 	if (gw_ire != NULL) {
6480 		ire_refrele(gw_ire);
6481 	}
6482 	/*
6483 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6484 	 * flag. This route is only added and deleted by mipagent.
6485 	 * So, for simplicity of design, we refrain from saving
6486 	 * ires that are created with srcif value. This may change
6487 	 * in future if we find more usage of srcifp feature.
6488 	 */
6489 	if (ipif != NULL && src_ipif == NULL) {
6490 		/*
6491 		 * Save enough information so that we can recreate the IRE if
6492 		 * the interface goes down and then up.  The metrics associated
6493 		 * with the route will be saved as well when rts_setmetrics() is
6494 		 * called after the IRE has been created.  In the case where
6495 		 * memory cannot be allocated, none of this information will be
6496 		 * saved.
6497 		 */
6498 		ipif_save_ire(ipif, ire);
6499 	}
6500 	if (ioctl_msg)
6501 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6502 	if (ire_arg != NULL) {
6503 		/*
6504 		 * Store the ire that was successfully added into where ire_arg
6505 		 * points to so that callers don't have to look it up
6506 		 * themselves (but they are responsible for ire_refrele()ing
6507 		 * the ire when they are finished with it).
6508 		 */
6509 		*ire_arg = ire;
6510 	} else {
6511 		ire_refrele(ire);		/* Held in ire_add */
6512 	}
6513 	if (ipif_refheld)
6514 		ipif_refrele(ipif);
6515 	return (0);
6516 }
6517 
6518 /*
6519  * ip_rt_delete is called to delete an IPv4 route.
6520  * ipif_arg is passed in to associate it with the correct interface.
6521  * src_ipif is passed to associate the incoming interface of the packet.
6522  * We may need to restart this operation if the ipif cannot be looked up
6523  * due to an exclusive operation that is currently in progress. The restart
6524  * entry point is specified by 'func'
6525  */
6526 /* ARGSUSED4 */
6527 int
6528 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6529     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6530     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6531 {
6532 	ire_t	*ire = NULL;
6533 	ipif_t	*ipif;
6534 	boolean_t ipif_refheld = B_FALSE;
6535 	uint_t	type;
6536 	uint_t	match_flags = MATCH_IRE_TYPE;
6537 	int	err = 0;
6538 
6539 	ip1dbg(("ip_rt_delete:"));
6540 	/*
6541 	 * If this is the case of RTF_HOST being set, then we set the netmask
6542 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6543 	 */
6544 	if (flags & RTF_HOST) {
6545 		mask = IP_HOST_MASK;
6546 		match_flags |= MATCH_IRE_MASK;
6547 	} else if (rtm_addrs & RTA_NETMASK) {
6548 		match_flags |= MATCH_IRE_MASK;
6549 	}
6550 
6551 	/*
6552 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6553 	 * we check if the gateway address is one of our interfaces first,
6554 	 * and fall back on RTF_GATEWAY routes.
6555 	 *
6556 	 * This makes it possible to delete an original
6557 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6558 	 *
6559 	 * As the interface index specified with the RTA_IFP sockaddr is the
6560 	 * same for all ipif's off of an ill, the matching logic below uses
6561 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6562 	 * sharing the same prefix and interface index as the the route
6563 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6564 	 * is specified in the request.
6565 	 *
6566 	 * On the other hand, since the gateway address will usually be
6567 	 * different for each ipif on the system, the matching logic
6568 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6569 	 * route.  This means that interface routes for the same prefix can be
6570 	 * uniquely identified if they belong to distinct ipif's and if a
6571 	 * RTA_IFP sockaddr is not present.
6572 	 *
6573 	 * For more detail on specifying routes by gateway address and by
6574 	 * interface index, see the comments in ip_rt_add().
6575 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6576 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6577 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6578 	 * succeed.
6579 	 */
6580 	if (src_ipif != NULL) {
6581 		if (ipif_arg == NULL && gw_addr != 0) {
6582 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6583 			    q, mp, func, &err);
6584 			if (ipif_arg != NULL)
6585 				ipif_refheld = B_TRUE;
6586 		}
6587 		if (ipif_arg == NULL) {
6588 			err = (err == EINPROGRESS) ? err : ESRCH;
6589 			return (err);
6590 		}
6591 		ipif = ipif_arg;
6592 	} else {
6593 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6594 			    q, mp, func, &err);
6595 		if (ipif != NULL)
6596 			ipif_refheld = B_TRUE;
6597 		else if (err == EINPROGRESS)
6598 			return (err);
6599 		else
6600 			err = 0;
6601 	}
6602 	if (ipif != NULL) {
6603 		if (ipif_arg != NULL) {
6604 			if (ipif_refheld) {
6605 				ipif_refrele(ipif);
6606 				ipif_refheld = B_FALSE;
6607 			}
6608 			ipif = ipif_arg;
6609 			match_flags |= MATCH_IRE_ILL;
6610 		} else {
6611 			match_flags |= MATCH_IRE_IPIF;
6612 		}
6613 		if (src_ipif != NULL) {
6614 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6615 			    ipif, src_ipif->ipif_ill, match_flags);
6616 		} else {
6617 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6618 				ire = ire_ctable_lookup(dst_addr, 0,
6619 				    IRE_LOOPBACK, ipif, ALL_ZONES, match_flags);
6620 			}
6621 			if (ire == NULL) {
6622 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6623 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6624 				    match_flags);
6625 			}
6626 		}
6627 	}
6628 
6629 	if (ire == NULL) {
6630 		/*
6631 		 * At this point, the gateway address is not one of our own
6632 		 * addresses or a matching interface route was not found.  We
6633 		 * set the IRE type to lookup based on whether
6634 		 * this is a host route, a default route or just a prefix.
6635 		 *
6636 		 * If an ipif_arg was passed in, then the lookup is based on an
6637 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6638 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6639 		 * set as the route being looked up is not a traditional
6640 		 * interface route.
6641 		 * Since we do not add gateway route with srcipif, we don't
6642 		 * expect to find it either.
6643 		 */
6644 		if (src_ipif != NULL) {
6645 			if (ipif_refheld)
6646 				ipif_refrele(ipif);
6647 			return (ESRCH);
6648 		} else {
6649 			match_flags &= ~MATCH_IRE_IPIF;
6650 			match_flags |= MATCH_IRE_GW;
6651 			if (ipif_arg != NULL)
6652 				match_flags |= MATCH_IRE_ILL;
6653 			if (mask == IP_HOST_MASK)
6654 				type = IRE_HOST;
6655 			else if (mask == 0)
6656 				type = IRE_DEFAULT;
6657 			else
6658 				type = IRE_PREFIX;
6659 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6660 			    ipif_arg, NULL, ALL_ZONES, 0, match_flags);
6661 			if (ire == NULL && type == IRE_HOST) {
6662 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6663 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6664 				    ALL_ZONES, 0, match_flags);
6665 			}
6666 		}
6667 	}
6668 
6669 	if (ipif_refheld)
6670 		ipif_refrele(ipif);
6671 
6672 	/* ipif is not refheld anymore */
6673 	if (ire == NULL)
6674 		return (ESRCH);
6675 
6676 	if (ire->ire_flags & RTF_MULTIRT) {
6677 		/*
6678 		 * Invoke the CGTP (multirouting) filtering module
6679 		 * to remove the dst address from the filtering database.
6680 		 * Packets coming from that address will no longer be
6681 		 * filtered to remove duplicates.
6682 		 */
6683 		if (ip_cgtp_filter_ops != NULL) {
6684 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6685 			    ire->ire_gateway_addr);
6686 		}
6687 		ip_cgtp_bcast_delete(ire);
6688 	}
6689 
6690 	ipif = ire->ire_ipif;
6691 	/*
6692 	 * Removing from ipif_saved_ire_mp is not necessary
6693 	 * when src_ipif being non-NULL. ip_rt_add does not
6694 	 * save the ires which src_ipif being non-NULL.
6695 	 */
6696 	if (ipif != NULL && src_ipif == NULL) {
6697 		ipif_remove_ire(ipif, ire);
6698 	}
6699 	if (ioctl_msg)
6700 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6701 	ire_delete(ire);
6702 	ire_refrele(ire);
6703 	return (err);
6704 }
6705 
6706 /*
6707  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6708  */
6709 /* ARGSUSED */
6710 int
6711 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6712     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6713 {
6714 	ipaddr_t dst_addr;
6715 	ipaddr_t gw_addr;
6716 	ipaddr_t mask;
6717 	int error = 0;
6718 	mblk_t *mp1;
6719 	struct rtentry *rt;
6720 	ipif_t *ipif = NULL;
6721 
6722 	ip1dbg(("ip_siocaddrt:"));
6723 	/* Existence of mp1 verified in ip_wput_nondata */
6724 	mp1 = mp->b_cont->b_cont;
6725 	rt = (struct rtentry *)mp1->b_rptr;
6726 
6727 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6728 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6729 
6730 	/*
6731 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6732 	 * to a particular host address.  In this case, we set the netmask to
6733 	 * all ones for the particular destination address.  Otherwise,
6734 	 * determine the netmask to be used based on dst_addr and the interfaces
6735 	 * in use.
6736 	 */
6737 	if (rt->rt_flags & RTF_HOST) {
6738 		mask = IP_HOST_MASK;
6739 	} else {
6740 		/*
6741 		 * Note that ip_subnet_mask returns a zero mask in the case of
6742 		 * default (an all-zeroes address).
6743 		 */
6744 		mask = ip_subnet_mask(dst_addr, &ipif);
6745 	}
6746 
6747 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags,
6748 	    NULL, NULL, NULL, B_TRUE, q, mp, ip_process_ioctl);
6749 	if (ipif != NULL)
6750 		ipif_refrele(ipif);
6751 	return (error);
6752 }
6753 
6754 /*
6755  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6756  */
6757 /* ARGSUSED */
6758 int
6759 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6760     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6761 {
6762 	ipaddr_t dst_addr;
6763 	ipaddr_t gw_addr;
6764 	ipaddr_t mask;
6765 	int error;
6766 	mblk_t *mp1;
6767 	struct rtentry *rt;
6768 	ipif_t *ipif = NULL;
6769 
6770 	ip1dbg(("ip_siocdelrt:"));
6771 	/* Existence of mp1 verified in ip_wput_nondata */
6772 	mp1 = mp->b_cont->b_cont;
6773 	rt = (struct rtentry *)mp1->b_rptr;
6774 
6775 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6776 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6777 
6778 	/*
6779 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6780 	 * to a particular host address.  In this case, we set the netmask to
6781 	 * all ones for the particular destination address.  Otherwise,
6782 	 * determine the netmask to be used based on dst_addr and the interfaces
6783 	 * in use.
6784 	 */
6785 	if (rt->rt_flags & RTF_HOST) {
6786 		mask = IP_HOST_MASK;
6787 	} else {
6788 		/*
6789 		 * Note that ip_subnet_mask returns a zero mask in the case of
6790 		 * default (an all-zeroes address).
6791 		 */
6792 		mask = ip_subnet_mask(dst_addr, &ipif);
6793 	}
6794 
6795 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6796 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6797 	    B_TRUE, q, mp, ip_process_ioctl);
6798 	if (ipif != NULL)
6799 		ipif_refrele(ipif);
6800 	return (error);
6801 }
6802 
6803 /*
6804  * Enqueue the mp onto the ipsq, chained by b_next.
6805  * b_prev stores the function to be executed later, and b_queue the queue
6806  * where this mp originated.
6807  */
6808 void
6809 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6810     ill_t *pending_ill)
6811 {
6812 	conn_t	*connp = NULL;
6813 
6814 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6815 	ASSERT(func != NULL);
6816 
6817 	mp->b_queue = q;
6818 	mp->b_prev = (void *)func;
6819 	mp->b_next = NULL;
6820 
6821 	switch (type) {
6822 	case CUR_OP:
6823 		if (ipsq->ipsq_mptail != NULL) {
6824 			ASSERT(ipsq->ipsq_mphead != NULL);
6825 			ipsq->ipsq_mptail->b_next = mp;
6826 		} else {
6827 			ASSERT(ipsq->ipsq_mphead == NULL);
6828 			ipsq->ipsq_mphead = mp;
6829 		}
6830 		ipsq->ipsq_mptail = mp;
6831 		break;
6832 
6833 	case NEW_OP:
6834 		if (ipsq->ipsq_xopq_mptail != NULL) {
6835 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6836 			ipsq->ipsq_xopq_mptail->b_next = mp;
6837 		} else {
6838 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6839 			ipsq->ipsq_xopq_mphead = mp;
6840 		}
6841 		ipsq->ipsq_xopq_mptail = mp;
6842 		break;
6843 	default:
6844 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6845 	}
6846 
6847 	if (CONN_Q(q) && pending_ill != NULL) {
6848 		connp = Q_TO_CONN(q);
6849 
6850 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6851 		connp->conn_oper_pending_ill = pending_ill;
6852 	}
6853 }
6854 
6855 /*
6856  * Return the mp at the head of the ipsq. After emptying the ipsq
6857  * look at the next ioctl, if this ioctl is complete. Otherwise
6858  * return, we will resume when we complete the current ioctl.
6859  * The current ioctl will wait till it gets a response from the
6860  * driver below.
6861  */
6862 static mblk_t *
6863 ipsq_dq(ipsq_t *ipsq)
6864 {
6865 	mblk_t	*mp;
6866 
6867 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6868 
6869 	mp = ipsq->ipsq_mphead;
6870 	if (mp != NULL) {
6871 		ipsq->ipsq_mphead = mp->b_next;
6872 		if (ipsq->ipsq_mphead == NULL)
6873 			ipsq->ipsq_mptail = NULL;
6874 		mp->b_next = NULL;
6875 		return (mp);
6876 	}
6877 	if (ipsq->ipsq_current_ipif != NULL)
6878 		return (NULL);
6879 	mp = ipsq->ipsq_xopq_mphead;
6880 	if (mp != NULL) {
6881 		ipsq->ipsq_xopq_mphead = mp->b_next;
6882 		if (ipsq->ipsq_xopq_mphead == NULL)
6883 			ipsq->ipsq_xopq_mptail = NULL;
6884 		mp->b_next = NULL;
6885 		return (mp);
6886 	}
6887 	return (NULL);
6888 }
6889 
6890 /*
6891  * Enter the ipsq corresponding to ill, by waiting synchronously till
6892  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6893  * will have to drain completely before ipsq_enter returns success.
6894  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
6895  * and the ipsq_exit logic will start the next enqueued ioctl after
6896  * completion of the current ioctl. If 'force' is used, we don't wait
6897  * for the enqueued ioctls. This is needed when a conn_close wants to
6898  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6899  * of an ill can also use this option. But we dont' use it currently.
6900  */
6901 #define	ENTER_SQ_WAIT_TICKS 100
6902 boolean_t
6903 ipsq_enter(ill_t *ill, boolean_t force)
6904 {
6905 	ipsq_t	*ipsq;
6906 	boolean_t waited_enough = B_FALSE;
6907 
6908 	/*
6909 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
6910 	 * Since the <ill-ipsq> assocs could change while we wait for the
6911 	 * writer, it is easier to wait on a fixed global rather than try to
6912 	 * cv_wait on a changing ipsq.
6913 	 */
6914 	mutex_enter(&ill->ill_lock);
6915 	for (;;) {
6916 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6917 			mutex_exit(&ill->ill_lock);
6918 			return (B_FALSE);
6919 		}
6920 
6921 		ipsq = ill->ill_phyint->phyint_ipsq;
6922 		mutex_enter(&ipsq->ipsq_lock);
6923 		if (ipsq->ipsq_writer == NULL &&
6924 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
6925 			break;
6926 		} else if (ipsq->ipsq_writer != NULL) {
6927 			mutex_exit(&ipsq->ipsq_lock);
6928 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6929 		} else {
6930 			mutex_exit(&ipsq->ipsq_lock);
6931 			if (force) {
6932 				(void) cv_timedwait(&ill->ill_cv,
6933 				    &ill->ill_lock,
6934 				    lbolt + ENTER_SQ_WAIT_TICKS);
6935 				waited_enough = B_TRUE;
6936 				continue;
6937 			} else {
6938 				cv_wait(&ill->ill_cv, &ill->ill_lock);
6939 			}
6940 		}
6941 	}
6942 
6943 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
6944 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
6945 	ipsq->ipsq_writer = curthread;
6946 	ipsq->ipsq_reentry_cnt++;
6947 #ifdef ILL_DEBUG
6948 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
6949 #endif
6950 	mutex_exit(&ipsq->ipsq_lock);
6951 	mutex_exit(&ill->ill_lock);
6952 	return (B_TRUE);
6953 }
6954 
6955 /*
6956  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6957  * certain critical operations like plumbing (i.e. most set ioctls),
6958  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
6959  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
6960  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
6961  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
6962  * threads executing in the ipsq. Responses from the driver pertain to the
6963  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
6964  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
6965  *
6966  * If a thread does not want to reenter the ipsq when it is already writer,
6967  * it must make sure that the specified reentry point to be called later
6968  * when the ipsq is empty, nor any code path starting from the specified reentry
6969  * point must never ever try to enter the ipsq again. Otherwise it can lead
6970  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6971  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6972  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
6973  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
6974  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6975  * ioctl if the current ioctl has completed. If the current ioctl is still
6976  * in progress it simply returns. The current ioctl could be waiting for
6977  * a response from another module (arp_ or the driver or could be waiting for
6978  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
6979  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
6980  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6981  * ipsq_current_ipif is clear which happens only on ioctl completion.
6982  */
6983 
6984 /*
6985  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
6986  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
6987  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
6988  * completion.
6989  */
6990 ipsq_t *
6991 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6992     ipsq_func_t func, int type, boolean_t reentry_ok)
6993 {
6994 	ipsq_t	*ipsq;
6995 
6996 	/* Only 1 of ipif or ill can be specified */
6997 	ASSERT((ipif != NULL) ^ (ill != NULL));
6998 	if (ipif != NULL)
6999 		ill = ipif->ipif_ill;
7000 
7001 	/*
7002 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7003 	 * ipsq of an ill can't change when ill_lock is held.
7004 	 */
7005 	GRAB_CONN_LOCK(q);
7006 	mutex_enter(&ill->ill_lock);
7007 	ipsq = ill->ill_phyint->phyint_ipsq;
7008 	mutex_enter(&ipsq->ipsq_lock);
7009 
7010 	/*
7011 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7012 	 *    (Note: If the caller does not specify reentry_ok then neither
7013 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7014 	 *    again. Otherwise it can lead to an infinite loop
7015 	 * 2. Enter the ipsq if there is no current writer and this attempted
7016 	 *    entry is part of the current ioctl or operation
7017 	 * 3. Enter the ipsq if there is no current writer and this is a new
7018 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7019 	 *    empty and there is no ioctl (or operation) currently in progress
7020 	 */
7021 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7022 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7023 	    ipsq->ipsq_current_ipif == NULL))) ||
7024 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7025 		/* Success. */
7026 		ipsq->ipsq_reentry_cnt++;
7027 		ipsq->ipsq_writer = curthread;
7028 		mutex_exit(&ipsq->ipsq_lock);
7029 		mutex_exit(&ill->ill_lock);
7030 		RELEASE_CONN_LOCK(q);
7031 #ifdef ILL_DEBUG
7032 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7033 #endif
7034 		return (ipsq);
7035 	}
7036 
7037 	ipsq_enq(ipsq, q, mp, func, type, ill);
7038 
7039 	mutex_exit(&ipsq->ipsq_lock);
7040 	mutex_exit(&ill->ill_lock);
7041 	RELEASE_CONN_LOCK(q);
7042 	return (NULL);
7043 }
7044 
7045 /*
7046  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7047  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7048  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7049  * completion.
7050  *
7051  * This function does a refrele on the ipif/ill.
7052  */
7053 void
7054 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7055     ipsq_func_t func, int type, boolean_t reentry_ok)
7056 {
7057 	ipsq_t	*ipsq;
7058 
7059 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7060 	/*
7061 	 * Caller must have done a refhold on the ipif. ipif_refrele
7062 	 * happens on the passed ipif. We can do this since we are
7063 	 * already exclusive, or we won't access ipif henceforth, Both
7064 	 * this func and caller will just return if we ipsq_try_enter
7065 	 * fails above. This is needed because func needs to
7066 	 * see the correct refcount. Eg. removeif can work only then.
7067 	 */
7068 	if (ipif != NULL)
7069 		ipif_refrele(ipif);
7070 	else
7071 		ill_refrele(ill);
7072 	if (ipsq != NULL) {
7073 		(*func)(ipsq, q, mp, NULL);
7074 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7075 	}
7076 }
7077 
7078 /*
7079  * If there are more than ILL_GRP_CNT ills in a group,
7080  * we use kmem alloc'd buffers, else use the stack
7081  */
7082 #define	ILL_GRP_CNT	14
7083 /*
7084  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7085  * Called by a thread that is currently exclusive on this ipsq.
7086  */
7087 void
7088 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7089 {
7090 	queue_t	*q;
7091 	mblk_t	*mp;
7092 	ipsq_func_t	func;
7093 	int	next;
7094 	ill_t	**ill_list = NULL;
7095 	size_t	ill_list_size = 0;
7096 	int	cnt = 0;
7097 	boolean_t need_ipsq_free = B_FALSE;
7098 
7099 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7100 	mutex_enter(&ipsq->ipsq_lock);
7101 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7102 	if (ipsq->ipsq_reentry_cnt != 1) {
7103 		ipsq->ipsq_reentry_cnt--;
7104 		mutex_exit(&ipsq->ipsq_lock);
7105 		return;
7106 	}
7107 
7108 	mp = ipsq_dq(ipsq);
7109 	while (mp != NULL) {
7110 again:
7111 		mutex_exit(&ipsq->ipsq_lock);
7112 		func = (ipsq_func_t)mp->b_prev;
7113 		q = (queue_t *)mp->b_queue;
7114 		mp->b_prev = NULL;
7115 		mp->b_queue = NULL;
7116 
7117 		/*
7118 		 * If 'q' is an conn queue, it is valid, since we did a
7119 		 * a refhold on the connp, at the start of the ioctl.
7120 		 * If 'q' is an ill queue, it is valid, since close of an
7121 		 * ill will clean up the 'ipsq'.
7122 		 */
7123 		(*func)(ipsq, q, mp, NULL);
7124 
7125 		mutex_enter(&ipsq->ipsq_lock);
7126 		mp = ipsq_dq(ipsq);
7127 	}
7128 
7129 	mutex_exit(&ipsq->ipsq_lock);
7130 
7131 	/*
7132 	 * Need to grab the locks in the right order. Need to
7133 	 * atomically check (under ipsq_lock) that there are no
7134 	 * messages before relinquishing the ipsq. Also need to
7135 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7136 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7137 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7138 	 * to grab ill_g_lock as writer.
7139 	 */
7140 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7141 
7142 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7143 	if (ipsq->ipsq_refs != 0) {
7144 		/* At most 2 ills v4/v6 per phyint */
7145 		cnt = ipsq->ipsq_refs << 1;
7146 		ill_list_size = cnt * sizeof (ill_t *);
7147 		/*
7148 		 * If memory allocation fails, we will do the split
7149 		 * the next time ipsq_exit is called for whatever reason.
7150 		 * As long as the ipsq_split flag is set the need to
7151 		 * split is remembered.
7152 		 */
7153 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7154 		if (ill_list != NULL)
7155 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7156 	}
7157 	mutex_enter(&ipsq->ipsq_lock);
7158 	mp = ipsq_dq(ipsq);
7159 	if (mp != NULL) {
7160 		/* oops, some message has landed up, we can't get out */
7161 		if (ill_list != NULL)
7162 			ill_unlock_ills(ill_list, cnt);
7163 		rw_exit(&ill_g_lock);
7164 		if (ill_list != NULL)
7165 			kmem_free(ill_list, ill_list_size);
7166 		ill_list = NULL;
7167 		ill_list_size = 0;
7168 		cnt = 0;
7169 		goto again;
7170 	}
7171 
7172 	/*
7173 	 * Split only if no ioctl is pending and if memory alloc succeeded
7174 	 * above.
7175 	 */
7176 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7177 		ill_list != NULL) {
7178 		/*
7179 		 * No new ill can join this ipsq since we are holding the
7180 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7181 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7182 		 * If so we will retry on the next ipsq_exit.
7183 		 */
7184 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7185 	}
7186 
7187 	/*
7188 	 * We are holding the ipsq lock, hence no new messages can
7189 	 * land up on the ipsq, and there are no messages currently.
7190 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7191 	 * atomically while holding ill locks.
7192 	 */
7193 	ipsq->ipsq_writer = NULL;
7194 	ipsq->ipsq_reentry_cnt--;
7195 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7196 #ifdef ILL_DEBUG
7197 	ipsq->ipsq_depth = 0;
7198 #endif
7199 	mutex_exit(&ipsq->ipsq_lock);
7200 	/*
7201 	 * For IPMP this should wake up all ills in this ipsq.
7202 	 * We need to hold the ill_lock while waking up waiters to
7203 	 * avoid missed wakeups. But there is no need to acquire all
7204 	 * the ill locks and then wakeup. If we have not acquired all
7205 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7206 	 * wakes up ills one at a time after getting the right ill_lock
7207 	 */
7208 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7209 	if (ill_list != NULL)
7210 		ill_unlock_ills(ill_list, cnt);
7211 	if (ipsq->ipsq_refs == 0)
7212 		need_ipsq_free = B_TRUE;
7213 	rw_exit(&ill_g_lock);
7214 	if (ill_list != 0)
7215 		kmem_free(ill_list, ill_list_size);
7216 
7217 	if (need_ipsq_free) {
7218 		/*
7219 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7220 		 * looked up. ipsq can be looked up only thru ill or phyint
7221 		 * and there are no ills/phyint on this ipsq.
7222 		 */
7223 		ipsq_delete(ipsq);
7224 	}
7225 	/*
7226 	 * Now start any igmp or mld timers that could not be started
7227 	 * while inside the ipsq. The timers can't be started while inside
7228 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7229 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7230 	 * there could be a deadlock since the timeout handlers
7231 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7232 	 * wait in ipsq_enter() trying to get the ipsq.
7233 	 *
7234 	 * However there is one exception to the above. If this thread is
7235 	 * itself the igmp/mld timeout handler thread, then we don't want
7236 	 * to start any new timer until the current handler is done. The
7237 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7238 	 * all others pass B_TRUE.
7239 	 */
7240 	if (start_igmp_timer) {
7241 		mutex_enter(&igmp_timer_lock);
7242 		next = igmp_deferred_next;
7243 		igmp_deferred_next = INFINITY;
7244 		mutex_exit(&igmp_timer_lock);
7245 
7246 		if (next != INFINITY)
7247 			igmp_start_timers(next);
7248 	}
7249 
7250 	if (start_mld_timer) {
7251 		mutex_enter(&mld_timer_lock);
7252 		next = mld_deferred_next;
7253 		mld_deferred_next = INFINITY;
7254 		mutex_exit(&mld_timer_lock);
7255 
7256 		if (next != INFINITY)
7257 			mld_start_timers(next);
7258 	}
7259 }
7260 
7261 /*
7262  * The ill is closing. Flush all messages on the ipsq that originated
7263  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7264  * for this ill since ipsq_enter could not have entered until then.
7265  * New messages can't be queued since the CONDEMNED flag is set.
7266  */
7267 static void
7268 ipsq_flush(ill_t *ill)
7269 {
7270 	queue_t	*q;
7271 	mblk_t	*prev;
7272 	mblk_t	*mp;
7273 	mblk_t	*mp_next;
7274 	ipsq_t	*ipsq;
7275 
7276 	ASSERT(IAM_WRITER_ILL(ill));
7277 	ipsq = ill->ill_phyint->phyint_ipsq;
7278 	/*
7279 	 * Flush any messages sent up by the driver.
7280 	 */
7281 	mutex_enter(&ipsq->ipsq_lock);
7282 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7283 		mp_next = mp->b_next;
7284 		q = mp->b_queue;
7285 		if (q == ill->ill_rq || q == ill->ill_wq) {
7286 			/* Remove the mp from the ipsq */
7287 			if (prev == NULL)
7288 				ipsq->ipsq_mphead = mp->b_next;
7289 			else
7290 				prev->b_next = mp->b_next;
7291 			if (ipsq->ipsq_mptail == mp) {
7292 				ASSERT(mp_next == NULL);
7293 				ipsq->ipsq_mptail = prev;
7294 			}
7295 			ip_ioctl_freemsg(mp);
7296 		} else {
7297 			prev = mp;
7298 		}
7299 	}
7300 	mutex_exit(&ipsq->ipsq_lock);
7301 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7302 	ipsq_xopq_mp_cleanup(ill, NULL);
7303 	ill_pending_mp_cleanup(ill);
7304 }
7305 
7306 /*
7307  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7308  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7309  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7310  * time (possible with one port going down for aggr and someone tearing down the
7311  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7312  * to indicate when the cleanup has started (1 ref) and when the cleanup
7313  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7314  * putting 2 ref on ill_inuse_ref.
7315  */
7316 static void
7317 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7318 {
7319 	conn_t *connp;
7320 	squeue_t *sqp;
7321 	mblk_t *mp;
7322 
7323 	ASSERT(rx_ring != NULL);
7324 
7325 	/* Just clean one squeue */
7326 	mutex_enter(&ill->ill_lock);
7327 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7328 		/* Some operations pending on the ring. Wait */
7329 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7330 
7331 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7332 		/*
7333 		 * Someone already trying to clean
7334 		 * this squeue or its already been cleaned.
7335 		 */
7336 		mutex_exit(&ill->ill_lock);
7337 		return;
7338 	}
7339 	sqp = rx_ring->rr_sqp;
7340 
7341 	if (sqp == NULL) {
7342 		/*
7343 		 * The rx_ring never had a squeue assigned to it.
7344 		 * We are under ill_lock so we can clean it up
7345 		 * here itself since no one can get to it.
7346 		 */
7347 		rx_ring->rr_blank = NULL;
7348 		rx_ring->rr_handle = NULL;
7349 		rx_ring->rr_sqp = NULL;
7350 		rx_ring->rr_ring_state = ILL_RING_FREE;
7351 		mutex_exit(&ill->ill_lock);
7352 		return;
7353 	}
7354 
7355 	/* Set the state that its being cleaned */
7356 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7357 	ASSERT(sqp != NULL);
7358 	mutex_exit(&ill->ill_lock);
7359 
7360 	/*
7361 	 * Use the preallocated ill_unbind_conn for this purpose
7362 	 */
7363 	connp = ill->ill_poll_capab->ill_unbind_conn;
7364 	mp = &connp->conn_tcp->tcp_closemp;
7365 	CONN_INC_REF(connp);
7366 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7367 
7368 	mutex_enter(&ill->ill_lock);
7369 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7370 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7371 
7372 	mutex_exit(&ill->ill_lock);
7373 }
7374 
7375 static void
7376 ipsq_clean_all(ill_t *ill)
7377 {
7378 	int idx;
7379 
7380 	/*
7381 	 * No need to clean if poll_capab isn't set for this ill
7382 	 */
7383 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
7384 		return;
7385 
7386 	ill->ill_capabilities &= ~ILL_CAPAB_POLL;
7387 
7388 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7389 		ill_rx_ring_t *ipr = &ill->ill_poll_capab->ill_ring_tbl[idx];
7390 		ipsq_clean_ring(ill, ipr);
7391 	}
7392 }
7393 
7394 /* ARGSUSED */
7395 int
7396 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7397     ip_ioctl_cmd_t *ipip, void *ifreq)
7398 {
7399 	ill_t	*ill;
7400 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7401 	boolean_t isv6;
7402 	conn_t	*connp;
7403 
7404 	connp = Q_TO_CONN(q);
7405 	isv6 = connp->conn_af_isv6;
7406 	/*
7407 	 * Set original index.
7408 	 * Failover and failback move logical interfaces
7409 	 * from one physical interface to another.  The
7410 	 * original index indicates the parent of a logical
7411 	 * interface, in other words, the physical interface
7412 	 * the logical interface will be moved back to on
7413 	 * failback.
7414 	 */
7415 
7416 	/*
7417 	 * Don't allow the original index to be changed
7418 	 * for non-failover addresses, autoconfigured
7419 	 * addresses, or IPv6 link local addresses.
7420 	 */
7421 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7422 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7423 		return (EINVAL);
7424 	}
7425 	/*
7426 	 * The new original index must be in use by some
7427 	 * physical interface.
7428 	 */
7429 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7430 	    NULL, NULL);
7431 	if (ill == NULL)
7432 		return (ENXIO);
7433 	ill_refrele(ill);
7434 
7435 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7436 	/*
7437 	 * When this ipif gets failed back, don't
7438 	 * preserve the original id, as it is no
7439 	 * longer applicable.
7440 	 */
7441 	ipif->ipif_orig_ipifid = 0;
7442 	/*
7443 	 * For IPv4, change the original index of any
7444 	 * multicast addresses associated with the
7445 	 * ipif to the new value.
7446 	 */
7447 	if (!isv6) {
7448 		ilm_t *ilm;
7449 
7450 		mutex_enter(&ipif->ipif_ill->ill_lock);
7451 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7452 		    ilm = ilm->ilm_next) {
7453 			if (ilm->ilm_ipif == ipif) {
7454 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7455 			}
7456 		}
7457 		mutex_exit(&ipif->ipif_ill->ill_lock);
7458 	}
7459 	return (0);
7460 }
7461 
7462 /* ARGSUSED */
7463 int
7464 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7465     ip_ioctl_cmd_t *ipip, void *ifreq)
7466 {
7467 	struct lifreq *lifr = (struct lifreq *)ifreq;
7468 
7469 	/*
7470 	 * Get the original interface index i.e the one
7471 	 * before FAILOVER if it ever happened.
7472 	 */
7473 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7474 	return (0);
7475 }
7476 
7477 /*
7478  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7479  * refhold and return the associated ipif
7480  */
7481 int
7482 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7483 {
7484 	boolean_t exists;
7485 	struct iftun_req *ta;
7486 	ipif_t	*ipif;
7487 	ill_t	*ill;
7488 	boolean_t isv6;
7489 	mblk_t	*mp1;
7490 	int	error;
7491 	conn_t	*connp;
7492 
7493 	/* Existence verified in ip_wput_nondata */
7494 	mp1 = mp->b_cont->b_cont;
7495 	ta = (struct iftun_req *)mp1->b_rptr;
7496 	/*
7497 	 * Null terminate the string to protect against buffer
7498 	 * overrun. String was generated by user code and may not
7499 	 * be trusted.
7500 	 */
7501 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7502 
7503 	connp = Q_TO_CONN(q);
7504 	isv6 = connp->conn_af_isv6;
7505 
7506 	/* Disallows implicit create */
7507 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7508 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7509 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7510 	if (ipif == NULL)
7511 		return (error);
7512 
7513 	if (ipif->ipif_id != 0) {
7514 		/*
7515 		 * We really don't want to set/get tunnel parameters
7516 		 * on virtual tunnel interfaces.  Only allow the
7517 		 * base tunnel to do these.
7518 		 */
7519 		ipif_refrele(ipif);
7520 		return (EINVAL);
7521 	}
7522 
7523 	/*
7524 	 * Send down to tunnel mod for ioctl processing.
7525 	 * Will finish ioctl in ip_rput_other().
7526 	 */
7527 	ill = ipif->ipif_ill;
7528 	if (ill->ill_net_type == IRE_LOOPBACK) {
7529 		ipif_refrele(ipif);
7530 		return (EOPNOTSUPP);
7531 	}
7532 
7533 	if (ill->ill_wq == NULL) {
7534 		ipif_refrele(ipif);
7535 		return (ENXIO);
7536 	}
7537 	/*
7538 	 * Mark the ioctl as coming from an IPv6 interface for
7539 	 * tun's convenience.
7540 	 */
7541 	if (ill->ill_isv6)
7542 		ta->ifta_flags |= 0x80000000;
7543 	*ipifp = ipif;
7544 	return (0);
7545 }
7546 
7547 /*
7548  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7549  * and return the associated ipif.
7550  * Return value:
7551  *	Non zero: An error has occurred. ci may not be filled out.
7552  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7553  *	a held ipif in ci.ci_ipif.
7554  */
7555 int
7556 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7557     cmd_info_t *ci, ipsq_func_t func)
7558 {
7559 	sin_t		*sin;
7560 	sin6_t		*sin6;
7561 	char		*name;
7562 	struct ifreq    *ifr;
7563 	struct lifreq    *lifr;
7564 	ipif_t		*ipif = NULL;
7565 	ill_t		*ill;
7566 	conn_t		*connp;
7567 	boolean_t	isv6;
7568 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7569 	boolean_t	exists;
7570 	int		err;
7571 	mblk_t		*mp1;
7572 	zoneid_t	zoneid;
7573 
7574 	if (q->q_next != NULL) {
7575 		ill = (ill_t *)q->q_ptr;
7576 		isv6 = ill->ill_isv6;
7577 		connp = NULL;
7578 		zoneid = ALL_ZONES;
7579 	} else {
7580 		ill = NULL;
7581 		connp = Q_TO_CONN(q);
7582 		isv6 = connp->conn_af_isv6;
7583 		zoneid = connp->conn_zoneid;
7584 		if (zoneid == GLOBAL_ZONEID) {
7585 			/* global zone can access ipifs in all zones */
7586 			zoneid = ALL_ZONES;
7587 		}
7588 	}
7589 
7590 	/* Has been checked in ip_wput_nondata */
7591 	mp1 = mp->b_cont->b_cont;
7592 
7593 
7594 	if (cmd_type == IF_CMD) {
7595 		/* This a old style SIOC[GS]IF* command */
7596 		ifr = (struct ifreq *)mp1->b_rptr;
7597 		/*
7598 		 * Null terminate the string to protect against buffer
7599 		 * overrun. String was generated by user code and may not
7600 		 * be trusted.
7601 		 */
7602 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7603 		sin = (sin_t *)&ifr->ifr_addr;
7604 		name = ifr->ifr_name;
7605 		ci->ci_sin = sin;
7606 		ci->ci_sin6 = NULL;
7607 		ci->ci_lifr = (struct lifreq *)ifr;
7608 	} else {
7609 		/* This a new style SIOC[GS]LIF* command */
7610 		ASSERT(cmd_type == LIF_CMD);
7611 		lifr = (struct lifreq *)mp1->b_rptr;
7612 		/*
7613 		 * Null terminate the string to protect against buffer
7614 		 * overrun. String was generated by user code and may not
7615 		 * be trusted.
7616 		 */
7617 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7618 		name = lifr->lifr_name;
7619 		sin = (sin_t *)&lifr->lifr_addr;
7620 		sin6 = (sin6_t *)&lifr->lifr_addr;
7621 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7622 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7623 			    LIFNAMSIZ);
7624 		}
7625 		ci->ci_sin = sin;
7626 		ci->ci_sin6 = sin6;
7627 		ci->ci_lifr = lifr;
7628 	}
7629 
7630 
7631 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7632 		/*
7633 		 * The ioctl will be failed if the ioctl comes down
7634 		 * an conn stream
7635 		 */
7636 		if (ill == NULL) {
7637 			/*
7638 			 * Not an ill queue, return EINVAL same as the
7639 			 * old error code.
7640 			 */
7641 			return (ENXIO);
7642 		}
7643 		ipif = ill->ill_ipif;
7644 		ipif_refhold(ipif);
7645 	} else {
7646 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7647 		    &exists, isv6, zoneid,
7648 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7649 		if (ipif == NULL) {
7650 			if (err == EINPROGRESS)
7651 				return (err);
7652 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7653 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7654 				/*
7655 				 * Need to try both v4 and v6 since this
7656 				 * ioctl can come down either v4 or v6
7657 				 * socket. The lifreq.lifr_family passed
7658 				 * down by this ioctl is AF_UNSPEC.
7659 				 */
7660 				ipif = ipif_lookup_on_name(name,
7661 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7662 				    zoneid, (connp == NULL) ? q :
7663 				    CONNP_TO_WQ(connp), mp, func, &err);
7664 				if (err == EINPROGRESS)
7665 					return (err);
7666 			}
7667 			err = 0;	/* Ensure we don't use it below */
7668 		}
7669 	}
7670 
7671 	/*
7672 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7673 	 */
7674 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7675 		ipif_refrele(ipif);
7676 		return (ENXIO);
7677 	}
7678 
7679 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7680 	    name[0] == '\0') {
7681 		/*
7682 		 * Handle a or a SIOC?IF* with a null name
7683 		 * during plumb (on the ill queue before the I_PLINK).
7684 		 */
7685 		ipif = ill->ill_ipif;
7686 		ipif_refhold(ipif);
7687 	}
7688 
7689 	if (ipif == NULL)
7690 		return (ENXIO);
7691 
7692 	/*
7693 	 * Allow only GET operations if this ipif has been created
7694 	 * temporarily due to a MOVE operation.
7695 	 */
7696 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7697 		ipif_refrele(ipif);
7698 		return (EINVAL);
7699 	}
7700 
7701 	ci->ci_ipif = ipif;
7702 	return (0);
7703 }
7704 
7705 /*
7706  * Return the total number of ipifs.
7707  */
7708 static uint_t
7709 ip_get_numifs(zoneid_t zoneid)
7710 {
7711 	uint_t numifs = 0;
7712 	ill_t	*ill;
7713 	ill_walk_context_t	ctx;
7714 	ipif_t	*ipif;
7715 
7716 	rw_enter(&ill_g_lock, RW_READER);
7717 	ill = ILL_START_WALK_V4(&ctx);
7718 
7719 	while (ill != NULL) {
7720 		for (ipif = ill->ill_ipif; ipif != NULL;
7721 		    ipif = ipif->ipif_next) {
7722 			if (ipif->ipif_zoneid == zoneid)
7723 				numifs++;
7724 		}
7725 		ill = ill_next(&ctx, ill);
7726 	}
7727 	rw_exit(&ill_g_lock);
7728 	return (numifs);
7729 }
7730 
7731 /*
7732  * Return the total number of ipifs.
7733  */
7734 static uint_t
7735 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7736 {
7737 	uint_t numifs = 0;
7738 	ill_t	*ill;
7739 	ipif_t	*ipif;
7740 	ill_walk_context_t	ctx;
7741 
7742 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7743 
7744 	rw_enter(&ill_g_lock, RW_READER);
7745 	if (family == AF_INET)
7746 		ill = ILL_START_WALK_V4(&ctx);
7747 	else if (family == AF_INET6)
7748 		ill = ILL_START_WALK_V6(&ctx);
7749 	else
7750 		ill = ILL_START_WALK_ALL(&ctx);
7751 
7752 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7753 		for (ipif = ill->ill_ipif; ipif != NULL;
7754 		    ipif = ipif->ipif_next) {
7755 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7756 			    !(lifn_flags & LIFC_NOXMIT))
7757 				continue;
7758 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7759 			    !(lifn_flags & LIFC_TEMPORARY))
7760 				continue;
7761 			if (((ipif->ipif_flags &
7762 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7763 			    IPIF_DEPRECATED)) ||
7764 			    (ill->ill_phyint->phyint_flags &
7765 			    PHYI_LOOPBACK) ||
7766 			    !(ipif->ipif_flags & IPIF_UP)) &&
7767 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7768 				continue;
7769 
7770 			if (zoneid != ipif->ipif_zoneid &&
7771 			    (zoneid != GLOBAL_ZONEID ||
7772 			    !(lifn_flags & LIFC_ALLZONES)))
7773 				continue;
7774 
7775 			numifs++;
7776 		}
7777 	}
7778 	rw_exit(&ill_g_lock);
7779 	return (numifs);
7780 }
7781 
7782 uint_t
7783 ip_get_lifsrcofnum(ill_t *ill)
7784 {
7785 	uint_t numifs = 0;
7786 	ill_t	*ill_head = ill;
7787 
7788 	/*
7789 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7790 	 * other thread may be trying to relink the ILLs in this usesrc group
7791 	 * and adjusting the ill_usesrc_grp_next pointers
7792 	 */
7793 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7794 	if ((ill->ill_usesrc_ifindex == 0) &&
7795 	    (ill->ill_usesrc_grp_next != NULL)) {
7796 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7797 		    ill = ill->ill_usesrc_grp_next)
7798 			numifs++;
7799 	}
7800 	rw_exit(&ill_g_usesrc_lock);
7801 
7802 	return (numifs);
7803 }
7804 
7805 /* Null values are passed in for ipif, sin, and ifreq */
7806 /* ARGSUSED */
7807 int
7808 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7809     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7810 {
7811 	int *nump;
7812 
7813 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7814 
7815 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7816 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7817 
7818 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
7819 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7820 	return (0);
7821 }
7822 
7823 /* Null values are passed in for ipif, sin, and ifreq */
7824 /* ARGSUSED */
7825 int
7826 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7827     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7828 {
7829 	struct lifnum *lifn;
7830 	mblk_t	*mp1;
7831 
7832 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7833 
7834 	/* Existence checked in ip_wput_nondata */
7835 	mp1 = mp->b_cont->b_cont;
7836 
7837 	lifn = (struct lifnum *)mp1->b_rptr;
7838 	switch (lifn->lifn_family) {
7839 	case AF_UNSPEC:
7840 	case AF_INET:
7841 	case AF_INET6:
7842 		break;
7843 	default:
7844 		return (EAFNOSUPPORT);
7845 	}
7846 
7847 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7848 	    Q_TO_CONN(q)->conn_zoneid);
7849 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7850 	return (0);
7851 }
7852 
7853 /* ARGSUSED */
7854 int
7855 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7856     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7857 {
7858 	STRUCT_HANDLE(ifconf, ifc);
7859 	mblk_t *mp1;
7860 	struct iocblk *iocp;
7861 	struct ifreq *ifr;
7862 	ill_walk_context_t	ctx;
7863 	ill_t	*ill;
7864 	ipif_t	*ipif;
7865 	struct sockaddr_in *sin;
7866 	int32_t	ifclen;
7867 	zoneid_t zoneid;
7868 
7869 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7870 
7871 	ip1dbg(("ip_sioctl_get_ifconf"));
7872 	/* Existence verified in ip_wput_nondata */
7873 	mp1 = mp->b_cont->b_cont;
7874 	iocp = (struct iocblk *)mp->b_rptr;
7875 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7876 
7877 	/*
7878 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7879 	 * the user buffer address and length into which the list of struct
7880 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7881 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7882 	 * the SIOCGIFCONF operation was redefined to simply provide
7883 	 * a large output buffer into which we are supposed to jam the ifreq
7884 	 * array.  The same ioctl command code was used, despite the fact that
7885 	 * both the applications and the kernel code had to change, thus making
7886 	 * it impossible to support both interfaces.
7887 	 *
7888 	 * For reasons not good enough to try to explain, the following
7889 	 * algorithm is used for deciding what to do with one of these:
7890 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7891 	 * form with the output buffer coming down as the continuation message.
7892 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7893 	 * and we have to copy in the ifconf structure to find out how big the
7894 	 * output buffer is and where to copy out to.  Sure no problem...
7895 	 *
7896 	 */
7897 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7898 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7899 		int numifs = 0;
7900 		size_t ifc_bufsize;
7901 
7902 		/*
7903 		 * Must be (better be!) continuation of a TRANSPARENT
7904 		 * IOCTL.  We just copied in the ifconf structure.
7905 		 */
7906 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7907 		    (struct ifconf *)mp1->b_rptr);
7908 
7909 		/*
7910 		 * Allocate a buffer to hold requested information.
7911 		 *
7912 		 * If ifc_len is larger than what is needed, we only
7913 		 * allocate what we will use.
7914 		 *
7915 		 * If ifc_len is smaller than what is needed, return
7916 		 * EINVAL.
7917 		 *
7918 		 * XXX: the ill_t structure can hava 2 counters, for
7919 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7920 		 * number of interfaces for a device, so we don't need
7921 		 * to count them here...
7922 		 */
7923 		numifs = ip_get_numifs(zoneid);
7924 
7925 		ifclen = STRUCT_FGET(ifc, ifc_len);
7926 		ifc_bufsize = numifs * sizeof (struct ifreq);
7927 		if (ifc_bufsize > ifclen) {
7928 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7929 				/* old behaviour */
7930 				return (EINVAL);
7931 			} else {
7932 				ifc_bufsize = ifclen;
7933 			}
7934 		}
7935 
7936 		mp1 = mi_copyout_alloc(q, mp,
7937 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7938 		if (mp1 == NULL)
7939 			return (ENOMEM);
7940 
7941 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7942 	}
7943 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7944 	/*
7945 	 * the SIOCGIFCONF ioctl only knows about
7946 	 * IPv4 addresses, so don't try to tell
7947 	 * it about interfaces with IPv6-only
7948 	 * addresses. (Last parm 'isv6' is B_FALSE)
7949 	 */
7950 
7951 	ifr = (struct ifreq *)mp1->b_rptr;
7952 
7953 	rw_enter(&ill_g_lock, RW_READER);
7954 	ill = ILL_START_WALK_V4(&ctx);
7955 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7956 		for (ipif = ill->ill_ipif; ipif;
7957 		    ipif = ipif->ipif_next) {
7958 			if (zoneid != ipif->ipif_zoneid)
7959 				continue;
7960 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7961 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7962 					/* old behaviour */
7963 					rw_exit(&ill_g_lock);
7964 					return (EINVAL);
7965 				} else {
7966 					goto if_copydone;
7967 				}
7968 			}
7969 			(void) ipif_get_name(ipif,
7970 			    ifr->ifr_name,
7971 			    sizeof (ifr->ifr_name));
7972 			sin = (sin_t *)&ifr->ifr_addr;
7973 			*sin = sin_null;
7974 			sin->sin_family = AF_INET;
7975 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7976 			ifr++;
7977 		}
7978 	}
7979 if_copydone:
7980 	rw_exit(&ill_g_lock);
7981 	mp1->b_wptr = (uchar_t *)ifr;
7982 
7983 	if (STRUCT_BUF(ifc) != NULL) {
7984 		STRUCT_FSET(ifc, ifc_len,
7985 			(int)((uchar_t *)ifr - mp1->b_rptr));
7986 	}
7987 	return (0);
7988 }
7989 
7990 /*
7991  * Get the interfaces using the address hosted on the interface passed in,
7992  * as a source adddress
7993  */
7994 /* ARGSUSED */
7995 int
7996 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7997     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7998 {
7999 	mblk_t *mp1;
8000 	ill_t	*ill, *ill_head;
8001 	ipif_t	*ipif, *orig_ipif;
8002 	int	numlifs = 0;
8003 	size_t	lifs_bufsize, lifsmaxlen;
8004 	struct	lifreq *lifr;
8005 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8006 	uint_t	ifindex;
8007 	zoneid_t zoneid;
8008 	int err = 0;
8009 	boolean_t isv6 = B_FALSE;
8010 	struct	sockaddr_in	*sin;
8011 	struct	sockaddr_in6	*sin6;
8012 
8013 	STRUCT_HANDLE(lifsrcof, lifs);
8014 
8015 	ASSERT(q->q_next == NULL);
8016 
8017 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8018 
8019 	/* Existence verified in ip_wput_nondata */
8020 	mp1 = mp->b_cont->b_cont;
8021 
8022 	/*
8023 	 * Must be (better be!) continuation of a TRANSPARENT
8024 	 * IOCTL.  We just copied in the lifsrcof structure.
8025 	 */
8026 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8027 	    (struct lifsrcof *)mp1->b_rptr);
8028 
8029 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8030 		return (EINVAL);
8031 
8032 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8033 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8034 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8035 	    ip_process_ioctl, &err);
8036 	if (ipif == NULL) {
8037 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8038 		    ifindex));
8039 		return (err);
8040 	}
8041 
8042 
8043 	/* Allocate a buffer to hold requested information */
8044 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8045 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8046 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8047 	/* The actual size needed is always returned in lifs_len */
8048 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8049 
8050 	/* If the amount we need is more than what is passed in, abort */
8051 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8052 		ipif_refrele(ipif);
8053 		return (0);
8054 	}
8055 
8056 	mp1 = mi_copyout_alloc(q, mp,
8057 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8058 	if (mp1 == NULL) {
8059 		ipif_refrele(ipif);
8060 		return (ENOMEM);
8061 	}
8062 
8063 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8064 	bzero(mp1->b_rptr, lifs_bufsize);
8065 
8066 	lifr = (struct lifreq *)mp1->b_rptr;
8067 
8068 	ill = ill_head = ipif->ipif_ill;
8069 	orig_ipif = ipif;
8070 
8071 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8072 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8073 	rw_enter(&ill_g_lock, RW_READER);
8074 
8075 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8076 	for (; (ill != NULL) && (ill != ill_head);
8077 	    ill = ill->ill_usesrc_grp_next) {
8078 
8079 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8080 			break;
8081 
8082 		ipif = ill->ill_ipif;
8083 		(void) ipif_get_name(ipif,
8084 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8085 		if (ipif->ipif_isv6) {
8086 			sin6 = (sin6_t *)&lifr->lifr_addr;
8087 			*sin6 = sin6_null;
8088 			sin6->sin6_family = AF_INET6;
8089 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8090 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8091 			    &ipif->ipif_v6net_mask);
8092 		} else {
8093 			sin = (sin_t *)&lifr->lifr_addr;
8094 			*sin = sin_null;
8095 			sin->sin_family = AF_INET;
8096 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8097 			lifr->lifr_addrlen = ip_mask_to_plen(
8098 			    ipif->ipif_net_mask);
8099 		}
8100 		lifr++;
8101 	}
8102 	rw_exit(&ill_g_usesrc_lock);
8103 	rw_exit(&ill_g_lock);
8104 	ipif_refrele(orig_ipif);
8105 	mp1->b_wptr = (uchar_t *)lifr;
8106 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8107 
8108 	return (0);
8109 }
8110 
8111 /* ARGSUSED */
8112 int
8113 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8114     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8115 {
8116 	mblk_t *mp1;
8117 	int	list;
8118 	ill_t	*ill;
8119 	ipif_t	*ipif;
8120 	int	flags;
8121 	int	numlifs = 0;
8122 	size_t	lifc_bufsize;
8123 	struct	lifreq *lifr;
8124 	sa_family_t	family;
8125 	struct	sockaddr_in	*sin;
8126 	struct	sockaddr_in6	*sin6;
8127 	ill_walk_context_t	ctx;
8128 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8129 	int32_t	lifclen;
8130 	zoneid_t zoneid;
8131 	STRUCT_HANDLE(lifconf, lifc);
8132 
8133 	ip1dbg(("ip_sioctl_get_lifconf"));
8134 
8135 	ASSERT(q->q_next == NULL);
8136 
8137 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8138 
8139 	/* Existence verified in ip_wput_nondata */
8140 	mp1 = mp->b_cont->b_cont;
8141 
8142 	/*
8143 	 * An extended version of SIOCGIFCONF that takes an
8144 	 * additional address family and flags field.
8145 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8146 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8147 	 * interfaces are omitted.
8148 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8149 	 * unless LIFC_TEMPORARY is specified.
8150 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8151 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8152 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8153 	 * has priority over LIFC_NOXMIT.
8154 	 */
8155 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8156 
8157 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8158 		return (EINVAL);
8159 
8160 	/*
8161 	 * Must be (better be!) continuation of a TRANSPARENT
8162 	 * IOCTL.  We just copied in the lifconf structure.
8163 	 */
8164 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8165 
8166 	family = STRUCT_FGET(lifc, lifc_family);
8167 	flags = STRUCT_FGET(lifc, lifc_flags);
8168 
8169 	switch (family) {
8170 	case AF_UNSPEC:
8171 		/*
8172 		 * walk all ILL's.
8173 		 */
8174 		list = MAX_G_HEADS;
8175 		break;
8176 	case AF_INET:
8177 		/*
8178 		 * walk only IPV4 ILL's.
8179 		 */
8180 		list = IP_V4_G_HEAD;
8181 		break;
8182 	case AF_INET6:
8183 		/*
8184 		 * walk only IPV6 ILL's.
8185 		 */
8186 		list = IP_V6_G_HEAD;
8187 		break;
8188 	default:
8189 		return (EAFNOSUPPORT);
8190 	}
8191 
8192 	/*
8193 	 * Allocate a buffer to hold requested information.
8194 	 *
8195 	 * If lifc_len is larger than what is needed, we only
8196 	 * allocate what we will use.
8197 	 *
8198 	 * If lifc_len is smaller than what is needed, return
8199 	 * EINVAL.
8200 	 */
8201 	numlifs = ip_get_numlifs(family, flags, zoneid);
8202 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8203 	lifclen = STRUCT_FGET(lifc, lifc_len);
8204 	if (lifc_bufsize > lifclen) {
8205 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8206 			return (EINVAL);
8207 		else
8208 			lifc_bufsize = lifclen;
8209 	}
8210 
8211 	mp1 = mi_copyout_alloc(q, mp,
8212 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8213 	if (mp1 == NULL)
8214 		return (ENOMEM);
8215 
8216 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8217 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8218 
8219 	lifr = (struct lifreq *)mp1->b_rptr;
8220 
8221 	rw_enter(&ill_g_lock, RW_READER);
8222 	ill = ill_first(list, list, &ctx);
8223 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8224 		for (ipif = ill->ill_ipif; ipif != NULL;
8225 		    ipif = ipif->ipif_next) {
8226 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8227 			    !(flags & LIFC_NOXMIT))
8228 				continue;
8229 
8230 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8231 			    !(flags & LIFC_TEMPORARY))
8232 				continue;
8233 
8234 			if (((ipif->ipif_flags &
8235 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8236 			    IPIF_DEPRECATED)) ||
8237 			    (ill->ill_phyint->phyint_flags &
8238 			    PHYI_LOOPBACK) ||
8239 			    !(ipif->ipif_flags & IPIF_UP)) &&
8240 			    (flags & LIFC_EXTERNAL_SOURCE))
8241 				continue;
8242 
8243 			if (zoneid != ipif->ipif_zoneid &&
8244 			    (zoneid != GLOBAL_ZONEID ||
8245 			    !(flags & LIFC_ALLZONES)))
8246 				continue;
8247 
8248 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8249 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8250 					rw_exit(&ill_g_lock);
8251 					return (EINVAL);
8252 				} else {
8253 					goto lif_copydone;
8254 				}
8255 			}
8256 
8257 			(void) ipif_get_name(ipif,
8258 				lifr->lifr_name,
8259 				sizeof (lifr->lifr_name));
8260 			if (ipif->ipif_isv6) {
8261 				sin6 = (sin6_t *)&lifr->lifr_addr;
8262 				*sin6 = sin6_null;
8263 				sin6->sin6_family = AF_INET6;
8264 				sin6->sin6_addr =
8265 				ipif->ipif_v6lcl_addr;
8266 				lifr->lifr_addrlen =
8267 				ip_mask_to_plen_v6(
8268 				    &ipif->ipif_v6net_mask);
8269 			} else {
8270 				sin = (sin_t *)&lifr->lifr_addr;
8271 				*sin = sin_null;
8272 				sin->sin_family = AF_INET;
8273 				sin->sin_addr.s_addr =
8274 				    ipif->ipif_lcl_addr;
8275 				lifr->lifr_addrlen =
8276 				    ip_mask_to_plen(
8277 				    ipif->ipif_net_mask);
8278 			}
8279 			lifr++;
8280 		}
8281 	}
8282 lif_copydone:
8283 	rw_exit(&ill_g_lock);
8284 
8285 	mp1->b_wptr = (uchar_t *)lifr;
8286 	if (STRUCT_BUF(lifc) != NULL) {
8287 		STRUCT_FSET(lifc, lifc_len,
8288 			(int)((uchar_t *)lifr - mp1->b_rptr));
8289 	}
8290 	return (0);
8291 }
8292 
8293 static void
8294 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8295 {
8296 	ip6_asp_t *table;
8297 	size_t table_size;
8298 	mblk_t *data_mp;
8299 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8300 
8301 	/* These two ioctls are I_STR only */
8302 	if (iocp->ioc_count == TRANSPARENT) {
8303 		miocnak(q, mp, 0, EINVAL);
8304 		return;
8305 	}
8306 
8307 	data_mp = mp->b_cont;
8308 	if (data_mp == NULL) {
8309 		/* The user passed us a NULL argument */
8310 		table = NULL;
8311 		table_size = iocp->ioc_count;
8312 	} else {
8313 		/*
8314 		 * The user provided a table.  The stream head
8315 		 * may have copied in the user data in chunks,
8316 		 * so make sure everything is pulled up
8317 		 * properly.
8318 		 */
8319 		if (MBLKL(data_mp) < iocp->ioc_count) {
8320 			mblk_t *new_data_mp;
8321 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8322 			    NULL) {
8323 				miocnak(q, mp, 0, ENOMEM);
8324 				return;
8325 			}
8326 			freemsg(data_mp);
8327 			data_mp = new_data_mp;
8328 			mp->b_cont = data_mp;
8329 		}
8330 		table = (ip6_asp_t *)data_mp->b_rptr;
8331 		table_size = iocp->ioc_count;
8332 	}
8333 
8334 	switch (iocp->ioc_cmd) {
8335 	case SIOCGIP6ADDRPOLICY:
8336 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8337 		if (iocp->ioc_rval == -1)
8338 			iocp->ioc_error = EINVAL;
8339 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8340 		else if (table != NULL &&
8341 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8342 			ip6_asp_t *src = table;
8343 			ip6_asp32_t *dst = (void *)table;
8344 			int count = table_size / sizeof (ip6_asp_t);
8345 			int i;
8346 
8347 			/*
8348 			 * We need to do an in-place shrink of the array
8349 			 * to match the alignment attributes of the
8350 			 * 32-bit ABI looking at it.
8351 			 */
8352 			/* LINTED: logical expression always true: op "||" */
8353 			ASSERT(sizeof (*src) > sizeof (*dst));
8354 			for (i = 1; i < count; i++)
8355 				bcopy(src + i, dst + i, sizeof (*dst));
8356 		}
8357 #endif
8358 		break;
8359 
8360 	case SIOCSIP6ADDRPOLICY:
8361 		ASSERT(mp->b_prev == NULL);
8362 		mp->b_prev = (void *)q;
8363 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8364 		/*
8365 		 * We pass in the datamodel here so that the ip6_asp_replace()
8366 		 * routine can handle converting from 32-bit to native formats
8367 		 * where necessary.
8368 		 *
8369 		 * A better way to handle this might be to convert the inbound
8370 		 * data structure here, and hang it off a new 'mp'; thus the
8371 		 * ip6_asp_replace() logic would always be dealing with native
8372 		 * format data structures..
8373 		 *
8374 		 * (An even simpler way to handle these ioctls is to just
8375 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8376 		 * and just recompile everything that depends on it.)
8377 		 */
8378 #endif
8379 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8380 		    iocp->ioc_flag & IOC_MODELS);
8381 		return;
8382 	}
8383 
8384 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8385 	qreply(q, mp);
8386 }
8387 
8388 static void
8389 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8390 {
8391 	mblk_t 		*data_mp;
8392 	struct dstinforeq	*dir;
8393 	uint8_t		*end, *cur;
8394 	in6_addr_t	*daddr, *saddr;
8395 	ipaddr_t	v4daddr;
8396 	ire_t		*ire;
8397 	char		*slabel, *dlabel;
8398 	boolean_t	isipv4;
8399 	int		match_ire;
8400 	ill_t		*dst_ill;
8401 	ipif_t		*src_ipif, *ire_ipif;
8402 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8403 	zoneid_t	zoneid;
8404 
8405 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8406 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8407 
8408 	/*
8409 	 * This ioctl is I_STR only, and must have a
8410 	 * data mblk following the M_IOCTL mblk.
8411 	 */
8412 	data_mp = mp->b_cont;
8413 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8414 		miocnak(q, mp, 0, EINVAL);
8415 		return;
8416 	}
8417 
8418 	if (MBLKL(data_mp) < iocp->ioc_count) {
8419 		mblk_t *new_data_mp;
8420 
8421 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8422 			miocnak(q, mp, 0, ENOMEM);
8423 			return;
8424 		}
8425 		freemsg(data_mp);
8426 		data_mp = new_data_mp;
8427 		mp->b_cont = data_mp;
8428 	}
8429 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8430 
8431 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8432 	    end - cur >= sizeof (struct dstinforeq);
8433 	    cur += sizeof (struct dstinforeq)) {
8434 		dir = (struct dstinforeq *)cur;
8435 		daddr = &dir->dir_daddr;
8436 		saddr = &dir->dir_saddr;
8437 
8438 		/*
8439 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8440 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8441 		 * and ipif_select_source[_v6]() do not.
8442 		 */
8443 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8444 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8445 
8446 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8447 		if (isipv4) {
8448 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8449 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8450 			    0, NULL, NULL, zoneid, 0, match_ire);
8451 		} else {
8452 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8453 			    0, NULL, NULL, zoneid, 0, match_ire);
8454 		}
8455 		if (ire == NULL) {
8456 			dir->dir_dreachable = 0;
8457 
8458 			/* move on to next dst addr */
8459 			continue;
8460 		}
8461 		dir->dir_dreachable = 1;
8462 
8463 		ire_ipif = ire->ire_ipif;
8464 		if (ire_ipif == NULL)
8465 			goto next_dst;
8466 
8467 		/*
8468 		 * We expect to get back an interface ire or a
8469 		 * gateway ire cache entry.  For both types, the
8470 		 * output interface is ire_ipif->ipif_ill.
8471 		 */
8472 		dst_ill = ire_ipif->ipif_ill;
8473 		dir->dir_dmactype = dst_ill->ill_mactype;
8474 
8475 		if (isipv4) {
8476 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8477 		} else {
8478 			src_ipif = ipif_select_source_v6(dst_ill,
8479 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8480 			    zoneid);
8481 		}
8482 		if (src_ipif == NULL)
8483 			goto next_dst;
8484 
8485 		*saddr = src_ipif->ipif_v6lcl_addr;
8486 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8487 		slabel = ip6_asp_lookup(saddr, NULL);
8488 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8489 		dir->dir_sdeprecated =
8490 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8491 		ipif_refrele(src_ipif);
8492 next_dst:
8493 		ire_refrele(ire);
8494 	}
8495 	miocack(q, mp, iocp->ioc_count, 0);
8496 }
8497 
8498 
8499 /*
8500  * Check if this is an address assigned to this machine.
8501  * Skips interfaces that are down by using ire checks.
8502  * Translates mapped addresses to v4 addresses and then
8503  * treats them as such, returning true if the v4 address
8504  * associated with this mapped address is configured.
8505  * Note: Applications will have to be careful what they do
8506  * with the response; use of mapped addresses limits
8507  * what can be done with the socket, especially with
8508  * respect to socket options and ioctls - neither IPv4
8509  * options nor IPv6 sticky options/ancillary data options
8510  * may be used.
8511  */
8512 /* ARGSUSED */
8513 int
8514 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8515     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8516 {
8517 	struct sioc_addrreq *sia;
8518 	sin_t *sin;
8519 	ire_t *ire;
8520 	mblk_t *mp1;
8521 	zoneid_t zoneid;
8522 
8523 	ip1dbg(("ip_sioctl_tmyaddr"));
8524 
8525 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8526 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8527 
8528 	/* Existence verified in ip_wput_nondata */
8529 	mp1 = mp->b_cont->b_cont;
8530 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8531 	sin = (sin_t *)&sia->sa_addr;
8532 	switch (sin->sin_family) {
8533 	case AF_INET6: {
8534 		sin6_t *sin6 = (sin6_t *)sin;
8535 
8536 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8537 			ipaddr_t v4_addr;
8538 
8539 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8540 			    v4_addr);
8541 			ire = ire_ctable_lookup(v4_addr, 0,
8542 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8543 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8544 		} else {
8545 			in6_addr_t v6addr;
8546 
8547 			v6addr = sin6->sin6_addr;
8548 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8549 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8550 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8551 		}
8552 		break;
8553 	}
8554 	case AF_INET: {
8555 		ipaddr_t v4addr;
8556 
8557 		v4addr = sin->sin_addr.s_addr;
8558 		ire = ire_ctable_lookup(v4addr, 0,
8559 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8560 		    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8561 		break;
8562 	}
8563 	default:
8564 		return (EAFNOSUPPORT);
8565 	}
8566 	if (ire != NULL) {
8567 		sia->sa_res = 1;
8568 		ire_refrele(ire);
8569 	} else {
8570 		sia->sa_res = 0;
8571 	}
8572 	return (0);
8573 }
8574 
8575 /*
8576  * Check if this is an address assigned on-link i.e. neighbor,
8577  * and makes sure it's reachable from the current zone.
8578  * Returns true for my addresses as well.
8579  * Translates mapped addresses to v4 addresses and then
8580  * treats them as such, returning true if the v4 address
8581  * associated with this mapped address is configured.
8582  * Note: Applications will have to be careful what they do
8583  * with the response; use of mapped addresses limits
8584  * what can be done with the socket, especially with
8585  * respect to socket options and ioctls - neither IPv4
8586  * options nor IPv6 sticky options/ancillary data options
8587  * may be used.
8588  */
8589 /* ARGSUSED */
8590 int
8591 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8592     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8593 {
8594 	struct sioc_addrreq *sia;
8595 	sin_t *sin;
8596 	mblk_t	*mp1;
8597 	ire_t *ire = NULL;
8598 	zoneid_t zoneid;
8599 
8600 	ip1dbg(("ip_sioctl_tonlink"));
8601 
8602 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8603 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8604 
8605 	/* Existence verified in ip_wput_nondata */
8606 	mp1 = mp->b_cont->b_cont;
8607 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8608 	sin = (sin_t *)&sia->sa_addr;
8609 
8610 	/*
8611 	 * Match addresses with a zero gateway field to avoid
8612 	 * routes going through a router.
8613 	 * Exclude broadcast and multicast addresses.
8614 	 */
8615 	switch (sin->sin_family) {
8616 	case AF_INET6: {
8617 		sin6_t *sin6 = (sin6_t *)sin;
8618 
8619 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8620 			ipaddr_t v4_addr;
8621 
8622 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8623 			    v4_addr);
8624 			if (!CLASSD(v4_addr)) {
8625 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8626 				    NULL, NULL, zoneid, MATCH_IRE_GW);
8627 			}
8628 		} else {
8629 			in6_addr_t v6addr;
8630 			in6_addr_t v6gw;
8631 
8632 			v6addr = sin6->sin6_addr;
8633 			v6gw = ipv6_all_zeros;
8634 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8635 				ire = ire_route_lookup_v6(&v6addr, 0,
8636 				    &v6gw, 0, NULL, NULL, zoneid,
8637 				    MATCH_IRE_GW);
8638 			}
8639 		}
8640 		break;
8641 	}
8642 	case AF_INET: {
8643 		ipaddr_t v4addr;
8644 
8645 		v4addr = sin->sin_addr.s_addr;
8646 		if (!CLASSD(v4addr)) {
8647 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8648 			    NULL, NULL, zoneid, MATCH_IRE_GW);
8649 		}
8650 		break;
8651 	}
8652 	default:
8653 		return (EAFNOSUPPORT);
8654 	}
8655 	sia->sa_res = 0;
8656 	if (ire != NULL) {
8657 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8658 		    IRE_LOCAL|IRE_LOOPBACK)) {
8659 			sia->sa_res = 1;
8660 		}
8661 		ire_refrele(ire);
8662 	}
8663 	return (0);
8664 }
8665 
8666 /*
8667  * TBD: implement when kernel maintaines a list of site prefixes.
8668  */
8669 /* ARGSUSED */
8670 int
8671 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8672     ip_ioctl_cmd_t *ipip, void *ifreq)
8673 {
8674 	return (ENXIO);
8675 }
8676 
8677 /* ARGSUSED */
8678 int
8679 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8680     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8681 {
8682 	ill_t  		*ill;
8683 	mblk_t		*mp1;
8684 	conn_t		*connp;
8685 	boolean_t	success;
8686 
8687 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8688 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8689 	/* ioctl comes down on an conn */
8690 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8691 	connp = Q_TO_CONN(q);
8692 
8693 	mp->b_datap->db_type = M_IOCTL;
8694 
8695 	/*
8696 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8697 	 * The original mp contains contaminated b_next values due to 'mi',
8698 	 * which is needed to do the mi_copy_done. Unfortunately if we
8699 	 * send down the original mblk itself and if we are popped due to an
8700 	 * an unplumb before the response comes back from tunnel,
8701 	 * the streamhead (which does a freemsg) will see this contaminated
8702 	 * message and the assertion in freemsg about non-null b_next/b_prev
8703 	 * will panic a DEBUG kernel.
8704 	 */
8705 	mp1 = copymsg(mp);
8706 	if (mp1 == NULL)
8707 		return (ENOMEM);
8708 
8709 	ill = ipif->ipif_ill;
8710 	mutex_enter(&connp->conn_lock);
8711 	mutex_enter(&ill->ill_lock);
8712 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8713 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8714 		    mp, 0);
8715 	} else {
8716 		success = ill_pending_mp_add(ill, connp, mp);
8717 	}
8718 	mutex_exit(&ill->ill_lock);
8719 	mutex_exit(&connp->conn_lock);
8720 
8721 	if (success) {
8722 		ip1dbg(("sending down tunparam request "));
8723 		putnext(ill->ill_wq, mp1);
8724 		return (EINPROGRESS);
8725 	} else {
8726 		/* The conn has started closing */
8727 		freemsg(mp1);
8728 		return (EINTR);
8729 	}
8730 }
8731 
8732 static int
8733 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8734     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8735 {
8736 	mblk_t *mp1;
8737 	mblk_t *mp2;
8738 	mblk_t *pending_mp;
8739 	ipaddr_t ipaddr;
8740 	area_t *area;
8741 	struct iocblk *iocp;
8742 	conn_t *connp;
8743 	struct arpreq *ar;
8744 	struct xarpreq *xar;
8745 	boolean_t success;
8746 	int flags, alength;
8747 	char *lladdr;
8748 
8749 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8750 	connp = Q_TO_CONN(q);
8751 
8752 	iocp = (struct iocblk *)mp->b_rptr;
8753 	/*
8754 	 * ill has already been set depending on whether
8755 	 * bsd style or interface style ioctl.
8756 	 */
8757 	ASSERT(ill != NULL);
8758 
8759 	/*
8760 	 * Is this one of the new SIOC*XARP ioctls?
8761 	 */
8762 	if (x_arp_ioctl) {
8763 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8764 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8765 		ar = NULL;
8766 
8767 		flags = xar->xarp_flags;
8768 		lladdr = LLADDR(&xar->xarp_ha);
8769 		/*
8770 		 * Validate against user's link layer address length
8771 		 * input and name and addr length limits.
8772 		 */
8773 		alength = ill->ill_phys_addr_length;
8774 		if (iocp->ioc_cmd == SIOCSXARP) {
8775 			if (alength != xar->xarp_ha.sdl_alen ||
8776 			    (alength + xar->xarp_ha.sdl_nlen >
8777 			    sizeof (xar->xarp_ha.sdl_data)))
8778 				return (EINVAL);
8779 		}
8780 	} else {
8781 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8782 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8783 		xar = NULL;
8784 
8785 		flags = ar->arp_flags;
8786 		lladdr = ar->arp_ha.sa_data;
8787 		/*
8788 		 * Theoretically, the sa_family could tell us what link
8789 		 * layer type this operation is trying to deal with. By
8790 		 * common usage AF_UNSPEC means ethernet. We'll assume
8791 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8792 		 * for now. Our new SIOC*XARP ioctls can be used more
8793 		 * generally.
8794 		 *
8795 		 * If the underlying media happens to have a non 6 byte
8796 		 * address, arp module will fail set/get, but the del
8797 		 * operation will succeed.
8798 		 */
8799 		alength = 6;
8800 		if ((iocp->ioc_cmd != SIOCDARP) &&
8801 		    (alength != ill->ill_phys_addr_length)) {
8802 			return (EINVAL);
8803 		}
8804 	}
8805 
8806 	/*
8807 	 * We are going to pass up to ARP a packet chain that looks
8808 	 * like:
8809 	 *
8810 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
8811 	 *
8812 	 * Get a copy of the original IOCTL mblk to head the chain,
8813 	 * to be sent up (in mp1). Also get another copy to store
8814 	 * in the ill_pending_mp list, for matching the response
8815 	 * when it comes back from ARP.
8816 	 */
8817 	mp1 = copyb(mp);
8818 	pending_mp = copymsg(mp);
8819 	if (mp1 == NULL || pending_mp == NULL) {
8820 		if (mp1 != NULL)
8821 			freeb(mp1);
8822 		if (pending_mp != NULL)
8823 			ip_ioctl_freemsg(pending_mp);
8824 		return (ENOMEM);
8825 	}
8826 
8827 	ipaddr = sin->sin_addr.s_addr;
8828 
8829 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
8830 	    (caddr_t)&ipaddr);
8831 	if (mp2 == NULL) {
8832 		freeb(mp1);
8833 		ip_ioctl_freemsg(pending_mp);
8834 		return (ENOMEM);
8835 	}
8836 	/* Put together the chain. */
8837 	mp1->b_cont = mp2;
8838 	mp1->b_datap->db_type = M_IOCTL;
8839 	mp2->b_cont = mp;
8840 	mp2->b_datap->db_type = M_DATA;
8841 
8842 	iocp = (struct iocblk *)mp1->b_rptr;
8843 
8844 	/*
8845 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
8846 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
8847 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
8848 	 * ioc_count field; set ioc_count to be correct.
8849 	 */
8850 	iocp->ioc_count = MBLKL(mp1->b_cont);
8851 
8852 	/*
8853 	 * Set the proper command in the ARP message.
8854 	 * Convert the SIOC{G|S|D}ARP calls into our
8855 	 * AR_ENTRY_xxx calls.
8856 	 */
8857 	area = (area_t *)mp2->b_rptr;
8858 	switch (iocp->ioc_cmd) {
8859 	case SIOCDARP:
8860 	case SIOCDXARP:
8861 		/*
8862 		 * We defer deleting the corresponding IRE until
8863 		 * we return from arp.
8864 		 */
8865 		area->area_cmd = AR_ENTRY_DELETE;
8866 		area->area_proto_mask_offset = 0;
8867 		break;
8868 	case SIOCGARP:
8869 	case SIOCGXARP:
8870 		area->area_cmd = AR_ENTRY_SQUERY;
8871 		area->area_proto_mask_offset = 0;
8872 		break;
8873 	case SIOCSARP:
8874 	case SIOCSXARP: {
8875 		/*
8876 		 * Delete the corresponding ire to make sure IP will
8877 		 * pick up any change from arp.
8878 		 */
8879 		if (!if_arp_ioctl) {
8880 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
8881 			break;
8882 		} else {
8883 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8884 			if (ipif != NULL) {
8885 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
8886 				ipif_refrele(ipif);
8887 			}
8888 			break;
8889 		}
8890 	}
8891 	}
8892 	iocp->ioc_cmd = area->area_cmd;
8893 
8894 	/*
8895 	 * Before sending 'mp' to ARP, we have to clear the b_next
8896 	 * and b_prev. Otherwise if STREAMS encounters such a message
8897 	 * in freemsg(), (because ARP can close any time) it can cause
8898 	 * a panic. But mi code needs the b_next and b_prev values of
8899 	 * mp->b_cont, to complete the ioctl. So we store it here
8900 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
8901 	 * when the response comes down from ARP.
8902 	 */
8903 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
8904 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
8905 	mp->b_cont->b_next = NULL;
8906 	mp->b_cont->b_prev = NULL;
8907 
8908 	mutex_enter(&connp->conn_lock);
8909 	mutex_enter(&ill->ill_lock);
8910 	/* conn has not yet started closing, hence this can't fail */
8911 	success = ill_pending_mp_add(ill, connp, pending_mp);
8912 	ASSERT(success);
8913 	mutex_exit(&ill->ill_lock);
8914 	mutex_exit(&connp->conn_lock);
8915 
8916 	/*
8917 	 * Fill in the rest of the ARP operation fields.
8918 	 */
8919 	area->area_hw_addr_length = alength;
8920 	bcopy(lladdr,
8921 	    (char *)area + area->area_hw_addr_offset,
8922 	    area->area_hw_addr_length);
8923 	/* Translate the flags. */
8924 	if (flags & ATF_PERM)
8925 		area->area_flags |= ACE_F_PERMANENT;
8926 	if (flags & ATF_PUBL)
8927 		area->area_flags |= ACE_F_PUBLISH;
8928 
8929 	/*
8930 	 * Up to ARP it goes.  The response will come
8931 	 * back in ip_wput as an M_IOCACK message, and
8932 	 * will be handed to ip_sioctl_iocack for
8933 	 * completion.
8934 	 */
8935 	putnext(ill->ill_rq, mp1);
8936 	return (EINPROGRESS);
8937 }
8938 
8939 /* ARGSUSED */
8940 int
8941 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8942     ip_ioctl_cmd_t *ipip, void *ifreq)
8943 {
8944 	struct xarpreq *xar;
8945 	boolean_t isv6;
8946 	mblk_t	*mp1;
8947 	int	err;
8948 	conn_t	*connp;
8949 	int ifnamelen;
8950 	ire_t	*ire = NULL;
8951 	ill_t	*ill = NULL;
8952 	struct sockaddr_in *sin;
8953 	boolean_t if_arp_ioctl = B_FALSE;
8954 
8955 	/* ioctl comes down on an conn */
8956 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8957 	connp = Q_TO_CONN(q);
8958 	isv6 = connp->conn_af_isv6;
8959 
8960 	/* Existance verified in ip_wput_nondata */
8961 	mp1 = mp->b_cont->b_cont;
8962 
8963 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
8964 	xar = (struct xarpreq *)mp1->b_rptr;
8965 	sin = (sin_t *)&xar->xarp_pa;
8966 
8967 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
8968 	    (xar->xarp_pa.ss_family != AF_INET))
8969 		return (ENXIO);
8970 
8971 	ifnamelen = xar->xarp_ha.sdl_nlen;
8972 	if (ifnamelen != 0) {
8973 		char	*cptr, cval;
8974 
8975 		if (ifnamelen >= LIFNAMSIZ)
8976 			return (EINVAL);
8977 
8978 		/*
8979 		 * Instead of bcopying a bunch of bytes,
8980 		 * null-terminate the string in-situ.
8981 		 */
8982 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
8983 		cval = *cptr;
8984 		*cptr = '\0';
8985 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
8986 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
8987 		    &err, NULL);
8988 		*cptr = cval;
8989 		if (ill == NULL)
8990 			return (err);
8991 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
8992 			ill_refrele(ill);
8993 			return (ENXIO);
8994 		}
8995 
8996 		if_arp_ioctl = B_TRUE;
8997 	} else {
8998 		/*
8999 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9000 		 * as an extended BSD ioctl. The kernel uses the IP address
9001 		 * to figure out the network interface.
9002 		 */
9003 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9004 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9005 		    ((ill = ire_to_ill(ire)) == NULL)) {
9006 			if (ire != NULL)
9007 				ire_refrele(ire);
9008 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9009 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9010 			    MATCH_IRE_TYPE);
9011 			if ((ire == NULL) ||
9012 			    ((ill = ire_to_ill(ire)) == NULL)) {
9013 				if (ire != NULL)
9014 					ire_refrele(ire);
9015 				return (ENXIO);
9016 			}
9017 		}
9018 		ASSERT(ire != NULL && ill != NULL);
9019 	}
9020 
9021 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9022 	if (if_arp_ioctl)
9023 		ill_refrele(ill);
9024 	if (ire != NULL)
9025 		ire_refrele(ire);
9026 
9027 	return (err);
9028 }
9029 
9030 /*
9031  * ARP IOCTLs.
9032  * How does IP get in the business of fronting ARP configuration/queries?
9033  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9034  * are by tradition passed in through a datagram socket.  That lands in IP.
9035  * As it happens, this is just as well since the interface is quite crude in
9036  * that it passes in no information about protocol or hardware types, or
9037  * interface association.  After making the protocol assumption, IP is in
9038  * the position to look up the name of the ILL, which ARP will need, and
9039  * format a request that can be handled by ARP.	 The request is passed up
9040  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9041  * back a response.  ARP supports its own set of more general IOCTLs, in
9042  * case anyone is interested.
9043  */
9044 /* ARGSUSED */
9045 int
9046 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9047     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9048 {
9049 	struct arpreq *ar;
9050 	struct sockaddr_in *sin;
9051 	ire_t	*ire;
9052 	boolean_t isv6;
9053 	mblk_t	*mp1;
9054 	int	err;
9055 	conn_t	*connp;
9056 	ill_t	*ill;
9057 
9058 	/* ioctl comes down on an conn */
9059 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9060 	connp = Q_TO_CONN(q);
9061 	isv6 = connp->conn_af_isv6;
9062 	if (isv6)
9063 		return (ENXIO);
9064 
9065 	/* Existance verified in ip_wput_nondata */
9066 	mp1 = mp->b_cont->b_cont;
9067 
9068 	ar = (struct arpreq *)mp1->b_rptr;
9069 	sin = (sin_t *)&ar->arp_pa;
9070 
9071 	/*
9072 	 * We need to let ARP know on which interface the IP
9073 	 * address has an ARP mapping. In the IPMP case, a
9074 	 * simple forwarding table lookup will return the
9075 	 * IRE_IF_RESOLVER for the first interface in the group,
9076 	 * which might not be the interface on which the
9077 	 * requested IP address was resolved due to the ill
9078 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9079 	 * So we do a cache table lookup first: if the IRE cache
9080 	 * entry for the IP address is still there, it will
9081 	 * contain the ill pointer for the right interface, so
9082 	 * we use that. If the cache entry has been flushed, we
9083 	 * fall back to the forwarding table lookup. This should
9084 	 * be rare enough since IRE cache entries have a longer
9085 	 * life expectancy than ARP cache entries.
9086 	 */
9087 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9088 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9089 	    ((ill = ire_to_ill(ire)) == NULL)) {
9090 		if (ire != NULL)
9091 			ire_refrele(ire);
9092 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9093 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9094 		    MATCH_IRE_TYPE);
9095 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9096 			if (ire != NULL)
9097 				ire_refrele(ire);
9098 			return (ENXIO);
9099 		}
9100 	}
9101 	ASSERT(ire != NULL && ill != NULL);
9102 
9103 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9104 	ire_refrele(ire);
9105 	return (err);
9106 }
9107 
9108 /*
9109  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9110  * atomically set/clear the muxids. Also complete the ioctl by acking or
9111  * naking it.  Note that the code is structured such that the link type,
9112  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9113  * its clones use the persistent link, while pppd(1M) and perhaps many
9114  * other daemons may use non-persistent link.  When combined with some
9115  * ill_t states, linking and unlinking lower streams may be used as
9116  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9117  */
9118 /* ARGSUSED */
9119 void
9120 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9121 {
9122 	mblk_t *mp1;
9123 	mblk_t *mp2;
9124 	struct linkblk *li;
9125 	queue_t	*ipwq;
9126 	char	*name;
9127 	struct qinit *qinfo;
9128 	struct ipmx_s *ipmxp;
9129 	ill_t	*ill = NULL;
9130 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9131 	int	err = 0;
9132 	boolean_t	entered_ipsq = B_FALSE;
9133 	boolean_t islink;
9134 	queue_t *dwq = NULL;
9135 
9136 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9137 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9138 
9139 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9140 	    B_TRUE : B_FALSE;
9141 
9142 	mp1 = mp->b_cont;	/* This is the linkblk info */
9143 	li = (struct linkblk *)mp1->b_rptr;
9144 
9145 	/*
9146 	 * ARP has added this special mblk, and the utility is asking us
9147 	 * to perform consistency checks, and also atomically set the
9148 	 * muxid. Ifconfig is an example.  It achieves this by using
9149 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9150 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9151 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9152 	 * and other comments in this routine for more details.
9153 	 */
9154 	mp2 = mp1->b_cont;	/* This is added by ARP */
9155 
9156 	/*
9157 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9158 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9159 	 * get the special mblk above.  For backward compatibility, we just
9160 	 * return success.  The utility will use SIOCSLIFMUXID to store
9161 	 * the muxids.  This is not atomic, and can leave the streams
9162 	 * unplumbable if the utility is interrrupted, before it does the
9163 	 * SIOCSLIFMUXID.
9164 	 */
9165 	if (mp2 == NULL) {
9166 		/*
9167 		 * At this point we don't know whether or not this is the
9168 		 * IP module stream or the ARP device stream.  We need to
9169 		 * walk the lower stream in order to find this out, since
9170 		 * the capability negotiation is done only on the IP module
9171 		 * stream.  IP module instance is identified by the module
9172 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9173 		 * STREAMS ensures that the lower stream (l_qbot) will not
9174 		 * vanish until this ioctl completes. So we can safely walk
9175 		 * the stream or refer to the q_ptr.
9176 		 */
9177 		ipwq = li->l_qbot;
9178 		while (ipwq != NULL) {
9179 			qinfo = ipwq->q_qinfo;
9180 			name = qinfo->qi_minfo->mi_idname;
9181 			if (name != NULL && name[0] != NULL &&
9182 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9183 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9184 			    (ipwq->q_next != NULL)) {
9185 				break;
9186 			}
9187 			ipwq = ipwq->q_next;
9188 		}
9189 		/*
9190 		 * This looks like an IP module stream, so trigger
9191 		 * the capability reset or re-negotiation if necessary.
9192 		 */
9193 		if (ipwq != NULL) {
9194 			ill = ipwq->q_ptr;
9195 			ASSERT(ill != NULL);
9196 
9197 			if (ipsq == NULL) {
9198 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9199 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9200 				if (ipsq == NULL)
9201 					return;
9202 				entered_ipsq = B_TRUE;
9203 			}
9204 			ASSERT(IAM_WRITER_ILL(ill));
9205 			/*
9206 			 * Store the upper read queue of the module
9207 			 * immediately below IP, and count the total
9208 			 * number of lower modules.  Do this only
9209 			 * for I_PLINK or I_LINK event.
9210 			 */
9211 			ill->ill_lmod_rq = NULL;
9212 			ill->ill_lmod_cnt = 0;
9213 			if (islink && (dwq = ipwq->q_next) != NULL) {
9214 				ill->ill_lmod_rq = RD(dwq);
9215 
9216 				while (dwq != NULL) {
9217 					ill->ill_lmod_cnt++;
9218 					dwq = dwq->q_next;
9219 				}
9220 			}
9221 			/*
9222 			 * There's no point in resetting or re-negotiating if
9223 			 * we are not bound to the driver, so only do this if
9224 			 * the DLPI state is idle (up); we assume such state
9225 			 * since ill_ipif_up_count gets incremented in
9226 			 * ipif_up_done(), which is after we are bound to the
9227 			 * driver.  Note that in the case of logical
9228 			 * interfaces, IP won't rebind to the driver unless
9229 			 * the ill_ipif_up_count is 0, meaning that all other
9230 			 * IP interfaces (including the main ipif) are in the
9231 			 * down state.  Because of this, we use such counter
9232 			 * as an indicator, instead of relying on the IPIF_UP
9233 			 * flag, which is per ipif instance.
9234 			 */
9235 			if (ill->ill_ipif_up_count > 0) {
9236 				if (islink)
9237 					ill_capability_probe(ill);
9238 				else
9239 					ill_capability_reset(ill);
9240 			}
9241 		}
9242 		goto done;
9243 	}
9244 
9245 	/*
9246 	 * This is an I_{P}LINK sent down by ifconfig on
9247 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9248 	 * giving more info. STREAMS ensures that the lower
9249 	 * stream (l_qbot) will not vanish until this ioctl
9250 	 * completes. So we can safely walk the stream or refer
9251 	 * to the q_ptr.
9252 	 */
9253 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9254 	if (ipmxp->ipmx_arpdev_stream) {
9255 		/*
9256 		 * The operation is occuring on the arp-device
9257 		 * stream.
9258 		 */
9259 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9260 		    q, mp, ip_sioctl_plink, &err, NULL);
9261 		if (ill == NULL) {
9262 			if (err == EINPROGRESS) {
9263 				return;
9264 			} else {
9265 				err = EINVAL;
9266 				goto done;
9267 			}
9268 		}
9269 
9270 		if (ipsq == NULL) {
9271 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9272 			    NEW_OP, B_TRUE);
9273 			if (ipsq == NULL) {
9274 				ill_refrele(ill);
9275 				return;
9276 			}
9277 			entered_ipsq = B_TRUE;
9278 		}
9279 		ASSERT(IAM_WRITER_ILL(ill));
9280 		ill_refrele(ill);
9281 		/*
9282 		 * To ensure consistency between IP and ARP,
9283 		 * the following LIFO scheme is used in
9284 		 * plink/punlink. (IP first, ARP last).
9285 		 * This is because the muxid's are stored
9286 		 * in the IP stream on the ill.
9287 		 *
9288 		 * I_{P}LINK: ifconfig plinks the IP stream before
9289 		 * plinking the ARP stream. On an arp-dev
9290 		 * stream, IP checks that it is not yet
9291 		 * plinked, and it also checks that the
9292 		 * corresponding IP stream is already plinked.
9293 		 *
9294 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9295 		 * before punlinking the IP stream. IP does
9296 		 * not allow punlink of the IP stream unless
9297 		 * the arp stream has been punlinked.
9298 		 *
9299 		 */
9300 		if ((islink &&
9301 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9302 		    (!islink &&
9303 		    ill->ill_arp_muxid != li->l_index)) {
9304 			err = EINVAL;
9305 			goto done;
9306 		}
9307 		if (islink) {
9308 			ill->ill_arp_muxid = li->l_index;
9309 		} else {
9310 			ill->ill_arp_muxid = 0;
9311 		}
9312 	} else {
9313 		/*
9314 		 * This must be the IP module stream with or
9315 		 * without arp. Walk the stream and locate the
9316 		 * IP module. An IP module instance is
9317 		 * identified by the module name IP, non-null
9318 		 * q_next, and it's wput not being ip_lwput.
9319 		 */
9320 		ipwq = li->l_qbot;
9321 		while (ipwq != NULL) {
9322 			qinfo = ipwq->q_qinfo;
9323 			name = qinfo->qi_minfo->mi_idname;
9324 			if (name != NULL && name[0] != NULL &&
9325 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9326 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9327 			    (ipwq->q_next != NULL)) {
9328 				break;
9329 			}
9330 			ipwq = ipwq->q_next;
9331 		}
9332 		if (ipwq != NULL) {
9333 			ill = ipwq->q_ptr;
9334 			ASSERT(ill != NULL);
9335 
9336 			if (ipsq == NULL) {
9337 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9338 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9339 				if (ipsq == NULL)
9340 					return;
9341 				entered_ipsq = B_TRUE;
9342 			}
9343 			ASSERT(IAM_WRITER_ILL(ill));
9344 			/*
9345 			 * Return error if the ip_mux_id is
9346 			 * non-zero and command is I_{P}LINK.
9347 			 * If command is I_{P}UNLINK, return
9348 			 * error if the arp-devstr is not
9349 			 * yet punlinked.
9350 			 */
9351 			if ((islink && ill->ill_ip_muxid != 0) ||
9352 			    (!islink && ill->ill_arp_muxid != 0)) {
9353 				err = EINVAL;
9354 				goto done;
9355 			}
9356 			ill->ill_lmod_rq = NULL;
9357 			ill->ill_lmod_cnt = 0;
9358 			if (islink) {
9359 				/*
9360 				 * Store the upper read queue of the module
9361 				 * immediately below IP, and count the total
9362 				 * number of lower modules.
9363 				 */
9364 				if ((dwq = ipwq->q_next) != NULL) {
9365 					ill->ill_lmod_rq = RD(dwq);
9366 
9367 					while (dwq != NULL) {
9368 						ill->ill_lmod_cnt++;
9369 						dwq = dwq->q_next;
9370 					}
9371 				}
9372 				ill->ill_ip_muxid = li->l_index;
9373 			} else {
9374 				ill->ill_ip_muxid = 0;
9375 			}
9376 
9377 			/*
9378 			 * See comments above about resetting/re-
9379 			 * negotiating driver sub-capabilities.
9380 			 */
9381 			if (ill->ill_ipif_up_count > 0) {
9382 				if (islink)
9383 					ill_capability_probe(ill);
9384 				else
9385 					ill_capability_reset(ill);
9386 			}
9387 		}
9388 	}
9389 done:
9390 	iocp->ioc_count = 0;
9391 	iocp->ioc_error = err;
9392 	if (err == 0)
9393 		mp->b_datap->db_type = M_IOCACK;
9394 	else
9395 		mp->b_datap->db_type = M_IOCNAK;
9396 	qreply(q, mp);
9397 
9398 	/* Conn was refheld in ip_sioctl_copyin_setup */
9399 	if (CONN_Q(q))
9400 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9401 	if (entered_ipsq)
9402 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9403 }
9404 
9405 /*
9406  * Search the ioctl command in the ioctl tables and return a pointer
9407  * to the ioctl command information. The ioctl command tables are
9408  * static and fully populated at compile time.
9409  */
9410 ip_ioctl_cmd_t *
9411 ip_sioctl_lookup(int ioc_cmd)
9412 {
9413 	int index;
9414 	ip_ioctl_cmd_t *ipip;
9415 	ip_ioctl_cmd_t *ipip_end;
9416 
9417 	if (ioc_cmd == IPI_DONTCARE)
9418 		return (NULL);
9419 
9420 	/*
9421 	 * Do a 2 step search. First search the indexed table
9422 	 * based on the least significant byte of the ioctl cmd.
9423 	 * If we don't find a match, then search the misc table
9424 	 * serially.
9425 	 */
9426 	index = ioc_cmd & 0xFF;
9427 	if (index < ip_ndx_ioctl_count) {
9428 		ipip = &ip_ndx_ioctl_table[index];
9429 		if (ipip->ipi_cmd == ioc_cmd) {
9430 			/* Found a match in the ndx table */
9431 			return (ipip);
9432 		}
9433 	}
9434 
9435 	/* Search the misc table */
9436 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9437 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9438 		if (ipip->ipi_cmd == ioc_cmd)
9439 			/* Found a match in the misc table */
9440 			return (ipip);
9441 	}
9442 
9443 	return (NULL);
9444 }
9445 
9446 /*
9447  * Wrapper function for resuming deferred ioctl processing
9448  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9449  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9450  */
9451 /* ARGSUSED */
9452 void
9453 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9454     void *dummy_arg)
9455 {
9456 	ip_sioctl_copyin_setup(q, mp);
9457 }
9458 
9459 /*
9460  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9461  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9462  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9463  * We establish here the size of the block to be copied in.  mi_copyin
9464  * arranges for this to happen, an processing continues in ip_wput with
9465  * an M_IOCDATA message.
9466  */
9467 void
9468 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9469 {
9470 	int	copyin_size;
9471 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9472 	ip_ioctl_cmd_t *ipip;
9473 	cred_t *cr;
9474 
9475 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9476 	if (ipip == NULL) {
9477 		/*
9478 		 * The ioctl is not one we understand or own.
9479 		 * Pass it along to be processed down stream,
9480 		 * if this is a module instance of IP, else nak
9481 		 * the ioctl.
9482 		 */
9483 		if (q->q_next == NULL) {
9484 			goto nak;
9485 		} else {
9486 			putnext(q, mp);
9487 			return;
9488 		}
9489 	}
9490 
9491 	/*
9492 	 * If this is deferred, then we will do all the checks when we
9493 	 * come back.
9494 	 */
9495 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9496 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9497 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9498 		return;
9499 	}
9500 
9501 	/*
9502 	 * Only allow a very small subset of IP ioctls on this stream if
9503 	 * IP is a module and not a driver. Allowing ioctls to be processed
9504 	 * in this case may cause assert failures or data corruption.
9505 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9506 	 * ioctls allowed on an IP module stream, after which this stream
9507 	 * normally becomes a multiplexor (at which time the stream head
9508 	 * will fail all ioctls).
9509 	 */
9510 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9511 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9512 			/*
9513 			 * Pass common Streams ioctls which the IP
9514 			 * module does not own or consume along to
9515 			 * be processed down stream.
9516 			 */
9517 			putnext(q, mp);
9518 			return;
9519 		} else {
9520 			goto nak;
9521 		}
9522 	}
9523 
9524 	/* Make sure we have ioctl data to process. */
9525 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9526 		goto nak;
9527 
9528 	/*
9529 	 * Prefer dblk credential over ioctl credential; some synthesized
9530 	 * ioctls have kcred set because there's no way to crhold()
9531 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9532 	 * the framework; the caller of ioctl needs to hold the reference
9533 	 * for the duration of the call).
9534 	 */
9535 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9536 
9537 	/* Make sure normal users don't send down privileged ioctls */
9538 	if ((ipip->ipi_flags & IPI_PRIV) &&
9539 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9540 		/* We checked the privilege earlier but log it here */
9541 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9542 		return;
9543 	}
9544 
9545 	/*
9546 	 * The ioctl command tables can only encode fixed length
9547 	 * ioctl data. If the length is variable, the table will
9548 	 * encode the length as zero. Such special cases are handled
9549 	 * below in the switch.
9550 	 */
9551 	if (ipip->ipi_copyin_size != 0) {
9552 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9553 		return;
9554 	}
9555 
9556 	switch (iocp->ioc_cmd) {
9557 	case O_SIOCGIFCONF:
9558 	case SIOCGIFCONF:
9559 		/*
9560 		 * This IOCTL is hilarious.  See comments in
9561 		 * ip_sioctl_get_ifconf for the story.
9562 		 */
9563 		if (iocp->ioc_count == TRANSPARENT)
9564 			copyin_size = SIZEOF_STRUCT(ifconf,
9565 			    iocp->ioc_flag);
9566 		else
9567 			copyin_size = iocp->ioc_count;
9568 		mi_copyin(q, mp, NULL, copyin_size);
9569 		return;
9570 
9571 	case O_SIOCGLIFCONF:
9572 	case SIOCGLIFCONF:
9573 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9574 		mi_copyin(q, mp, NULL, copyin_size);
9575 		return;
9576 
9577 	case SIOCGLIFSRCOF:
9578 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9579 		mi_copyin(q, mp, NULL, copyin_size);
9580 		return;
9581 	case SIOCGIP6ADDRPOLICY:
9582 		ip_sioctl_ip6addrpolicy(q, mp);
9583 		ip6_asp_table_refrele();
9584 		return;
9585 
9586 	case SIOCSIP6ADDRPOLICY:
9587 		ip_sioctl_ip6addrpolicy(q, mp);
9588 		return;
9589 
9590 	case SIOCGDSTINFO:
9591 		ip_sioctl_dstinfo(q, mp);
9592 		ip6_asp_table_refrele();
9593 		return;
9594 
9595 	case I_PLINK:
9596 	case I_PUNLINK:
9597 	case I_LINK:
9598 	case I_UNLINK:
9599 		/*
9600 		 * We treat non-persistent link similarly as the persistent
9601 		 * link case, in terms of plumbing/unplumbing, as well as
9602 		 * dynamic re-plumbing events indicator.  See comments
9603 		 * in ip_sioctl_plink() for more.
9604 		 *
9605 		 * Request can be enqueued in the 'ipsq' while waiting
9606 		 * to become exclusive. So bump up the conn ref.
9607 		 */
9608 		if (CONN_Q(q))
9609 			CONN_INC_REF(Q_TO_CONN(q));
9610 		ip_sioctl_plink(NULL, q, mp, NULL);
9611 		return;
9612 
9613 	case ND_GET:
9614 	case ND_SET:
9615 		/*
9616 		 * Use of the nd table requires holding the reader lock.
9617 		 * Modifying the nd table thru nd_load/nd_unload requires
9618 		 * the writer lock.
9619 		 */
9620 		rw_enter(&ip_g_nd_lock, RW_READER);
9621 		if (nd_getset(q, ip_g_nd, mp)) {
9622 			rw_exit(&ip_g_nd_lock);
9623 
9624 			if (iocp->ioc_error)
9625 				iocp->ioc_count = 0;
9626 			mp->b_datap->db_type = M_IOCACK;
9627 			qreply(q, mp);
9628 			return;
9629 		}
9630 		rw_exit(&ip_g_nd_lock);
9631 		/*
9632 		 * We don't understand this subioctl of ND_GET / ND_SET.
9633 		 * Maybe intended for some driver / module below us
9634 		 */
9635 		if (q->q_next) {
9636 			putnext(q, mp);
9637 		} else {
9638 			iocp->ioc_error = ENOENT;
9639 			mp->b_datap->db_type = M_IOCNAK;
9640 			iocp->ioc_count = 0;
9641 			qreply(q, mp);
9642 		}
9643 		return;
9644 
9645 	case IP_IOCTL:
9646 		ip_wput_ioctl(q, mp);
9647 		return;
9648 	default:
9649 		cmn_err(CE_PANIC, "should not happen ");
9650 	}
9651 nak:
9652 	if (mp->b_cont != NULL) {
9653 		freemsg(mp->b_cont);
9654 		mp->b_cont = NULL;
9655 	}
9656 	iocp->ioc_error = EINVAL;
9657 	mp->b_datap->db_type = M_IOCNAK;
9658 	iocp->ioc_count = 0;
9659 	qreply(q, mp);
9660 }
9661 
9662 /* ip_wput hands off ARP IOCTL responses to us */
9663 void
9664 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9665 {
9666 	struct arpreq *ar;
9667 	struct xarpreq *xar;
9668 	area_t	*area;
9669 	mblk_t	*area_mp;
9670 	struct iocblk *iocp;
9671 	mblk_t	*orig_ioc_mp, *tmp;
9672 	struct iocblk	*orig_iocp;
9673 	ill_t *ill;
9674 	conn_t *connp = NULL;
9675 	uint_t ioc_id;
9676 	mblk_t *pending_mp;
9677 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9678 	int *flagsp;
9679 	char *storage = NULL;
9680 	sin_t *sin;
9681 	ipaddr_t addr;
9682 	int err;
9683 
9684 	ill = q->q_ptr;
9685 	ASSERT(ill != NULL);
9686 
9687 	/*
9688 	 * We should get back from ARP a packet chain that looks like:
9689 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9690 	 */
9691 	if (!(area_mp = mp->b_cont) ||
9692 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9693 	    !(orig_ioc_mp = area_mp->b_cont) ||
9694 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9695 		freemsg(mp);
9696 		return;
9697 	}
9698 
9699 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9700 
9701 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9702 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9703 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9704 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9705 		x_arp_ioctl = B_TRUE;
9706 		xar = (struct xarpreq *)tmp->b_rptr;
9707 		sin = (sin_t *)&xar->xarp_pa;
9708 		flagsp = &xar->xarp_flags;
9709 		storage = xar->xarp_ha.sdl_data;
9710 		if (xar->xarp_ha.sdl_nlen != 0)
9711 			ifx_arp_ioctl = B_TRUE;
9712 	} else {
9713 		ar = (struct arpreq *)tmp->b_rptr;
9714 		sin = (sin_t *)&ar->arp_pa;
9715 		flagsp = &ar->arp_flags;
9716 		storage = ar->arp_ha.sa_data;
9717 	}
9718 
9719 	iocp = (struct iocblk *)mp->b_rptr;
9720 
9721 	/*
9722 	 * Pick out the originating queue based on the ioc_id.
9723 	 */
9724 	ioc_id = iocp->ioc_id;
9725 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9726 	if (pending_mp == NULL) {
9727 		ASSERT(connp == NULL);
9728 		ip_ioctl_freemsg(mp);
9729 		return;
9730 	}
9731 	ASSERT(connp != NULL);
9732 	q = CONNP_TO_WQ(connp);
9733 
9734 	/* Uncouple the internally generated IOCTL from the original one */
9735 	area = (area_t *)area_mp->b_rptr;
9736 	area_mp->b_cont = NULL;
9737 
9738 	/*
9739 	 * Restore the b_next and b_prev used by mi code. This is needed
9740 	 * to complete the ioctl using mi* functions. We stored them in
9741 	 * the pending mp prior to sending the request to ARP.
9742 	 */
9743 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9744 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9745 	ip_ioctl_freemsg(pending_mp);
9746 
9747 	/*
9748 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9749 	 * Catch the case where there is an IRE_CACHE by no entry in the
9750 	 * arp table.
9751 	 */
9752 	addr = sin->sin_addr.s_addr;
9753 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9754 		ire_t			*ire;
9755 		dl_unitdata_req_t	*dlup;
9756 		mblk_t			*llmp;
9757 		int			addr_len;
9758 		ill_t			*ipsqill = NULL;
9759 
9760 		if (ifx_arp_ioctl) {
9761 			/*
9762 			 * There's no need to lookup the ill, since
9763 			 * we've already done that when we started
9764 			 * processing the ioctl and sent the message
9765 			 * to ARP on that ill.  So use the ill that
9766 			 * is stored in q->q_ptr.
9767 			 */
9768 			ipsqill = ill;
9769 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9770 			    ipsqill->ill_ipif, ALL_ZONES,
9771 			    MATCH_IRE_TYPE | MATCH_IRE_ILL);
9772 		} else {
9773 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9774 			    NULL, ALL_ZONES, MATCH_IRE_TYPE);
9775 			if (ire != NULL)
9776 				ipsqill = ire_to_ill(ire);
9777 		}
9778 
9779 		if ((x_arp_ioctl) && (ipsqill != NULL))
9780 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9781 
9782 		if (ire != NULL) {
9783 			*flagsp = ATF_INUSE;
9784 			llmp = ire->ire_dlureq_mp;
9785 			if (llmp != NULL && ipsqill != NULL) {
9786 				uchar_t *macaddr;
9787 
9788 				addr_len = ipsqill->ill_phys_addr_length;
9789 				if (x_arp_ioctl && ((addr_len +
9790 				    ipsqill->ill_name_length) >
9791 				    sizeof (xar->xarp_ha.sdl_data))) {
9792 					ire_refrele(ire);
9793 					freemsg(mp);
9794 					ip_ioctl_finish(q, orig_ioc_mp,
9795 					    EINVAL, NO_COPYOUT, NULL, NULL);
9796 					return;
9797 				}
9798 				*flagsp |= ATF_COM;
9799 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
9800 				if (ipsqill->ill_sap_length < 0)
9801 					macaddr = llmp->b_rptr +
9802 					    dlup->dl_dest_addr_offset;
9803 				else
9804 					macaddr = llmp->b_rptr +
9805 					    dlup->dl_dest_addr_offset +
9806 					    ipsqill->ill_sap_length;
9807 				/*
9808 				 * For SIOCGARP, MAC address length
9809 				 * validation has already been done
9810 				 * before the ioctl was issued to ARP to
9811 				 * allow it to progress only on 6 byte
9812 				 * addressable (ethernet like) media. Thus
9813 				 * the mac address copying can not overwrite
9814 				 * the sa_data area below.
9815 				 */
9816 				bcopy(macaddr, storage, addr_len);
9817 			}
9818 			/* Ditch the internal IOCTL. */
9819 			freemsg(mp);
9820 			ire_refrele(ire);
9821 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9822 			return;
9823 		}
9824 	}
9825 
9826 	/*
9827 	 * Delete the coresponding IRE_CACHE if any.
9828 	 * Reset the error if there was one (in case there was no entry
9829 	 * in arp.)
9830 	 */
9831 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
9832 		ipif_t *ipintf = NULL;
9833 
9834 		if (ifx_arp_ioctl) {
9835 			/*
9836 			 * There's no need to lookup the ill, since
9837 			 * we've already done that when we started
9838 			 * processing the ioctl and sent the message
9839 			 * to ARP on that ill.  So use the ill that
9840 			 * is stored in q->q_ptr.
9841 			 */
9842 			ipintf = ill->ill_ipif;
9843 		}
9844 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
9845 			/*
9846 			 * The address in "addr" may be an entry for a
9847 			 * router. If that's true, then any off-net
9848 			 * IRE_CACHE entries that go through the router
9849 			 * with address "addr" must be clobbered. Use
9850 			 * ire_walk to achieve this goal.
9851 			 */
9852 			if (ifx_arp_ioctl)
9853 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
9854 				    ire_delete_cache_gw, (char *)&addr, ill);
9855 			else
9856 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
9857 				    ALL_ZONES);
9858 			iocp->ioc_error = 0;
9859 		}
9860 	}
9861 
9862 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
9863 		err = iocp->ioc_error;
9864 		freemsg(mp);
9865 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
9866 		return;
9867 	}
9868 
9869 	/*
9870 	 * Completion of an SIOCG{X}ARP.  Translate the information from
9871 	 * the area_t into the struct {x}arpreq.
9872 	 */
9873 	if (x_arp_ioctl) {
9874 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9875 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9876 		    sizeof (xar->xarp_ha.sdl_data)) {
9877 			freemsg(mp);
9878 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
9879 			    NO_COPYOUT, NULL, NULL);
9880 			return;
9881 		}
9882 	}
9883 	*flagsp = ATF_INUSE;
9884 	if (area->area_flags & ACE_F_PERMANENT)
9885 		*flagsp |= ATF_PERM;
9886 	if (area->area_flags & ACE_F_PUBLISH)
9887 		*flagsp |= ATF_PUBL;
9888 	if (area->area_hw_addr_length != 0) {
9889 		*flagsp |= ATF_COM;
9890 		/*
9891 		 * For SIOCGARP, MAC address length validation has
9892 		 * already been done before the ioctl was issued to ARP
9893 		 * to allow it to progress only on 6 byte addressable
9894 		 * (ethernet like) media. Thus the mac address copying
9895 		 * can not overwrite the sa_data area below.
9896 		 */
9897 		bcopy((char *)area + area->area_hw_addr_offset,
9898 		    storage, area->area_hw_addr_length);
9899 	}
9900 
9901 	/* Ditch the internal IOCTL. */
9902 	freemsg(mp);
9903 	/* Complete the original. */
9904 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9905 }
9906 
9907 /*
9908  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9909  * interface) create the next available logical interface for this
9910  * physical interface.
9911  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9912  * ipif with the specified name.
9913  *
9914  * If the address family is not AF_UNSPEC then set the address as well.
9915  *
9916  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9917  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9918  *
9919  * Executed as a writer on the ill or ill group.
9920  * So no lock is needed to traverse the ipif chain, or examine the
9921  * phyint flags.
9922  */
9923 /* ARGSUSED */
9924 int
9925 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9926     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9927 {
9928 	mblk_t	*mp1;
9929 	struct lifreq *lifr;
9930 	boolean_t	isv6;
9931 	boolean_t	exists;
9932 	char 	*name;
9933 	char	*endp;
9934 	char	*cp;
9935 	int	namelen;
9936 	ipif_t	*ipif;
9937 	long	id;
9938 	ipsq_t	*ipsq;
9939 	ill_t	*ill;
9940 	sin_t	*sin;
9941 	int	err = 0;
9942 	boolean_t found_sep = B_FALSE;
9943 	conn_t	*connp;
9944 	zoneid_t zoneid;
9945 	int	orig_ifindex = 0;
9946 
9947 	ip1dbg(("ip_sioctl_addif\n"));
9948 	/* Existence of mp1 has been checked in ip_wput_nondata */
9949 	mp1 = mp->b_cont->b_cont;
9950 	/*
9951 	 * Null terminate the string to protect against buffer
9952 	 * overrun. String was generated by user code and may not
9953 	 * be trusted.
9954 	 */
9955 	lifr = (struct lifreq *)mp1->b_rptr;
9956 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9957 	name = lifr->lifr_name;
9958 	ASSERT(CONN_Q(q));
9959 	connp = Q_TO_CONN(q);
9960 	isv6 = connp->conn_af_isv6;
9961 	zoneid = connp->conn_zoneid;
9962 	namelen = mi_strlen(name);
9963 	if (namelen == 0)
9964 		return (EINVAL);
9965 
9966 	exists = B_FALSE;
9967 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9968 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9969 		/*
9970 		 * Allow creating lo0 using SIOCLIFADDIF.
9971 		 * can't be any other writer thread. So can pass null below
9972 		 * for the last 4 args to ipif_lookup_name.
9973 		 */
9974 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
9975 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
9976 		/* Prevent any further action */
9977 		if (ipif == NULL) {
9978 			return (ENOBUFS);
9979 		} else if (!exists) {
9980 			/* We created the ipif now and as writer */
9981 			ipif_refrele(ipif);
9982 			return (0);
9983 		} else {
9984 			ill = ipif->ipif_ill;
9985 			ill_refhold(ill);
9986 			ipif_refrele(ipif);
9987 		}
9988 	} else {
9989 		/* Look for a colon in the name. */
9990 		endp = &name[namelen];
9991 		for (cp = endp; --cp > name; ) {
9992 			if (*cp == IPIF_SEPARATOR_CHAR) {
9993 				found_sep = B_TRUE;
9994 				/*
9995 				 * Reject any non-decimal aliases for plumbing
9996 				 * of logical interfaces. Aliases with leading
9997 				 * zeroes are also rejected as they introduce
9998 				 * ambiguity in the naming of the interfaces.
9999 				 * Comparing with "0" takes care of all such
10000 				 * cases.
10001 				 */
10002 				if ((strncmp("0", cp+1, 1)) == 0)
10003 					return (EINVAL);
10004 
10005 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10006 				    id <= 0 || *endp != '\0') {
10007 					return (EINVAL);
10008 				}
10009 				*cp = '\0';
10010 				break;
10011 			}
10012 		}
10013 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10014 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10015 		if (found_sep)
10016 			*cp = IPIF_SEPARATOR_CHAR;
10017 		if (ill == NULL)
10018 			return (err);
10019 	}
10020 
10021 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10022 	    B_TRUE);
10023 
10024 	/*
10025 	 * Release the refhold due to the lookup, now that we are excl
10026 	 * or we are just returning
10027 	 */
10028 	ill_refrele(ill);
10029 
10030 	if (ipsq == NULL)
10031 		return (EINPROGRESS);
10032 
10033 	/*
10034 	 * If the interface is failed, inactive or offlined, look for a working
10035 	 * interface in the ill group and create the ipif there. If we can't
10036 	 * find a good interface, create the ipif anyway so that in.mpathd can
10037 	 * move it to the first repaired interface.
10038 	 */
10039 	if ((ill->ill_phyint->phyint_flags &
10040 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10041 	    ill->ill_phyint->phyint_groupname_len != 0) {
10042 		phyint_t *phyi;
10043 		char *groupname = ill->ill_phyint->phyint_groupname;
10044 
10045 		/*
10046 		 * We're looking for a working interface, but it doesn't matter
10047 		 * if it's up or down; so instead of following the group lists,
10048 		 * we look at each physical interface and compare the groupname.
10049 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10050 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10051 		 * Otherwise we create the ipif on the failed interface.
10052 		 */
10053 		rw_enter(&ill_g_lock, RW_READER);
10054 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10055 		for (; phyi != NULL;
10056 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10057 		    phyi, AVL_AFTER)) {
10058 			if (phyi->phyint_groupname_len == 0)
10059 				continue;
10060 			ASSERT(phyi->phyint_groupname != NULL);
10061 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10062 			    !(phyi->phyint_flags &
10063 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10064 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10065 			    (phyi->phyint_illv4 != NULL))) {
10066 				break;
10067 			}
10068 		}
10069 		rw_exit(&ill_g_lock);
10070 
10071 		if (phyi != NULL) {
10072 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10073 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10074 			    phyi->phyint_illv4);
10075 		}
10076 	}
10077 
10078 	/*
10079 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10080 	 * before or after us.
10081 	 */
10082 	ASSERT(IAM_WRITER_ILL(ill));
10083 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10084 
10085 	if (found_sep && orig_ifindex == 0) {
10086 		/* Now see if there is an IPIF with this unit number. */
10087 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10088 			if (ipif->ipif_id == id) {
10089 				err = EEXIST;
10090 				goto done;
10091 			}
10092 		}
10093 	}
10094 
10095 	/*
10096 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10097 	 * of lo0. We never come here when we plumb lo0:0. It
10098 	 * happens in ipif_lookup_on_name.
10099 	 * The specified unit number is ignored when we create the ipif on a
10100 	 * different interface. However, we save it in ipif_orig_ipifid below so
10101 	 * that the ipif fails back to the right position.
10102 	 */
10103 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10104 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10105 		err = ENOBUFS;
10106 		goto done;
10107 	}
10108 
10109 	/* Return created name with ioctl */
10110 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10111 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10112 	ip1dbg(("created %s\n", lifr->lifr_name));
10113 
10114 	/* Set address */
10115 	sin = (sin_t *)&lifr->lifr_addr;
10116 	if (sin->sin_family != AF_UNSPEC) {
10117 		err = ip_sioctl_addr(ipif, sin, q, mp,
10118 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10119 	}
10120 
10121 	/* Set ifindex and unit number for failback */
10122 	if (err == 0 && orig_ifindex != 0) {
10123 		ipif->ipif_orig_ifindex = orig_ifindex;
10124 		if (found_sep) {
10125 			ipif->ipif_orig_ipifid = id;
10126 		}
10127 	}
10128 
10129 done:
10130 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10131 	return (err);
10132 }
10133 
10134 /*
10135  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10136  * interface) delete it based on the IP address (on this physical interface).
10137  * Otherwise delete it based on the ipif_id.
10138  * Also, special handling to allow a removeif of lo0.
10139  */
10140 /* ARGSUSED */
10141 int
10142 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10143     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10144 {
10145 	conn_t		*connp;
10146 	ill_t		*ill = ipif->ipif_ill;
10147 	boolean_t	 success;
10148 
10149 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10150 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10151 	ASSERT(IAM_WRITER_IPIF(ipif));
10152 
10153 	connp = Q_TO_CONN(q);
10154 	/*
10155 	 * Special case for unplumbing lo0 (the loopback physical interface).
10156 	 * If unplumbing lo0, the incoming address structure has been
10157 	 * initialized to all zeros. When unplumbing lo0, all its logical
10158 	 * interfaces must be removed too.
10159 	 *
10160 	 * Note that this interface may be called to remove a specific
10161 	 * loopback logical interface (eg, lo0:1). But in that case
10162 	 * ipif->ipif_id != 0 so that the code path for that case is the
10163 	 * same as any other interface (meaning it skips the code directly
10164 	 * below).
10165 	 */
10166 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10167 		if (sin->sin_family == AF_UNSPEC &&
10168 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10169 			/*
10170 			 * Mark it condemned. No new ref. will be made to ill.
10171 			 */
10172 			mutex_enter(&ill->ill_lock);
10173 			ill->ill_state_flags |= ILL_CONDEMNED;
10174 			for (ipif = ill->ill_ipif; ipif != NULL;
10175 			    ipif = ipif->ipif_next) {
10176 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10177 			}
10178 			mutex_exit(&ill->ill_lock);
10179 
10180 			ipif = ill->ill_ipif;
10181 			/* unplumb the loopback interface */
10182 			ill_delete(ill);
10183 			mutex_enter(&connp->conn_lock);
10184 			mutex_enter(&ill->ill_lock);
10185 			ASSERT(ill->ill_group == NULL);
10186 
10187 			/* Are any references to this ill active */
10188 			if (ill_is_quiescent(ill)) {
10189 				mutex_exit(&ill->ill_lock);
10190 				mutex_exit(&connp->conn_lock);
10191 				ill_delete_tail(ill);
10192 				return (0);
10193 			}
10194 			success = ipsq_pending_mp_add(connp, ipif,
10195 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10196 			mutex_exit(&connp->conn_lock);
10197 			mutex_exit(&ill->ill_lock);
10198 			if (success)
10199 				return (EINPROGRESS);
10200 			else
10201 				return (EINTR);
10202 		}
10203 	}
10204 
10205 	/*
10206 	 * We are exclusive on the ipsq, so an ill move will be serialized
10207 	 * before or after us.
10208 	 */
10209 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10210 
10211 	if (ipif->ipif_id == 0) {
10212 		/* Find based on address */
10213 		if (ipif->ipif_isv6) {
10214 			sin6_t *sin6;
10215 
10216 			if (sin->sin_family != AF_INET6)
10217 				return (EAFNOSUPPORT);
10218 
10219 			sin6 = (sin6_t *)sin;
10220 			/* We are a writer, so we should be able to lookup */
10221 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10222 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10223 			if (ipif == NULL) {
10224 				/*
10225 				 * Maybe the address in on another interface in
10226 				 * the same IPMP group? We check this below.
10227 				 */
10228 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10229 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10230 			}
10231 		} else {
10232 			ipaddr_t addr;
10233 
10234 			if (sin->sin_family != AF_INET)
10235 				return (EAFNOSUPPORT);
10236 
10237 			addr = sin->sin_addr.s_addr;
10238 			/* We are a writer, so we should be able to lookup */
10239 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10240 			    NULL, NULL, NULL);
10241 			if (ipif == NULL) {
10242 				/*
10243 				 * Maybe the address in on another interface in
10244 				 * the same IPMP group? We check this below.
10245 				 */
10246 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10247 				    NULL, NULL, NULL, NULL);
10248 			}
10249 		}
10250 		if (ipif == NULL) {
10251 			return (EADDRNOTAVAIL);
10252 		}
10253 		/*
10254 		 * When the address to be removed is hosted on a different
10255 		 * interface, we check if the interface is in the same IPMP
10256 		 * group as the specified one; if so we proceed with the
10257 		 * removal.
10258 		 * ill->ill_group is NULL when the ill is down, so we have to
10259 		 * compare the group names instead.
10260 		 */
10261 		if (ipif->ipif_ill != ill &&
10262 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10263 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10264 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10265 		    ill->ill_phyint->phyint_groupname) != 0)) {
10266 			ipif_refrele(ipif);
10267 			return (EADDRNOTAVAIL);
10268 		}
10269 
10270 		/* This is a writer */
10271 		ipif_refrele(ipif);
10272 	}
10273 
10274 	/*
10275 	 * Can not delete instance zero since it is tied to the ill.
10276 	 */
10277 	if (ipif->ipif_id == 0)
10278 		return (EBUSY);
10279 
10280 	mutex_enter(&ill->ill_lock);
10281 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10282 	mutex_exit(&ill->ill_lock);
10283 
10284 	ipif_free(ipif);
10285 
10286 	mutex_enter(&connp->conn_lock);
10287 	mutex_enter(&ill->ill_lock);
10288 
10289 	/* Are any references to this ipif active */
10290 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10291 		mutex_exit(&ill->ill_lock);
10292 		mutex_exit(&connp->conn_lock);
10293 		ipif_down_tail(ipif);
10294 		ipif_free_tail(ipif);
10295 		return (0);
10296 	    }
10297 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10298 	    IPIF_FREE);
10299 	mutex_exit(&ill->ill_lock);
10300 	mutex_exit(&connp->conn_lock);
10301 	if (success)
10302 		return (EINPROGRESS);
10303 	else
10304 		return (EINTR);
10305 }
10306 
10307 /*
10308  * Restart the removeif ioctl. The refcnt has gone down to 0.
10309  * The ipif is already condemned. So can't find it thru lookups.
10310  */
10311 /* ARGSUSED */
10312 int
10313 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10314     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10315 {
10316 	ill_t *ill;
10317 
10318 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10319 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10320 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10321 		ill = ipif->ipif_ill;
10322 		ASSERT(IAM_WRITER_ILL(ill));
10323 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10324 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10325 		ill_delete_tail(ill);
10326 		return (0);
10327 	}
10328 
10329 	ill = ipif->ipif_ill;
10330 	ASSERT(IAM_WRITER_IPIF(ipif));
10331 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10332 
10333 	ipif_down_tail(ipif);
10334 	ipif_free_tail(ipif);
10335 
10336 	ILL_UNMARK_CHANGING(ill);
10337 	return (0);
10338 }
10339 
10340 /*
10341  * Set the local interface address.
10342  * Allow an address of all zero when the interface is down.
10343  */
10344 /* ARGSUSED */
10345 int
10346 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10347     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10348 {
10349 	int err = 0;
10350 	in6_addr_t v6addr;
10351 	boolean_t need_up = B_FALSE;
10352 
10353 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10354 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10355 
10356 	ASSERT(IAM_WRITER_IPIF(ipif));
10357 
10358 	if (ipif->ipif_isv6) {
10359 		sin6_t *sin6;
10360 		ill_t *ill;
10361 		phyint_t *phyi;
10362 
10363 		if (sin->sin_family != AF_INET6)
10364 			return (EAFNOSUPPORT);
10365 
10366 		sin6 = (sin6_t *)sin;
10367 		v6addr = sin6->sin6_addr;
10368 		ill = ipif->ipif_ill;
10369 		phyi = ill->ill_phyint;
10370 
10371 		/*
10372 		 * Enforce that true multicast interfaces have a link-local
10373 		 * address for logical unit 0.
10374 		 */
10375 		if (ipif->ipif_id == 0 &&
10376 		    (ill->ill_flags & ILLF_MULTICAST) &&
10377 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10378 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10379 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10380 			return (EADDRNOTAVAIL);
10381 		}
10382 
10383 		/*
10384 		 * up interfaces shouldn't have the unspecified address
10385 		 * unless they also have the IPIF_NOLOCAL flags set and
10386 		 * have a subnet assigned.
10387 		 */
10388 		if ((ipif->ipif_flags & IPIF_UP) &&
10389 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10390 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10391 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10392 			return (EADDRNOTAVAIL);
10393 		}
10394 
10395 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10396 			return (EADDRNOTAVAIL);
10397 	} else {
10398 		ipaddr_t addr;
10399 
10400 		if (sin->sin_family != AF_INET)
10401 			return (EAFNOSUPPORT);
10402 
10403 		addr = sin->sin_addr.s_addr;
10404 
10405 		/* Allow 0 as the local address. */
10406 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10407 			return (EADDRNOTAVAIL);
10408 
10409 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10410 	}
10411 
10412 
10413 	/*
10414 	 * Even if there is no change we redo things just to rerun
10415 	 * ipif_set_default.
10416 	 */
10417 	if (ipif->ipif_flags & IPIF_UP) {
10418 		/*
10419 		 * Setting a new local address, make sure
10420 		 * we have net and subnet bcast ire's for
10421 		 * the old address if we need them.
10422 		 */
10423 		if (!ipif->ipif_isv6)
10424 			ipif_check_bcast_ires(ipif);
10425 		/*
10426 		 * If the interface is already marked up,
10427 		 * we call ipif_down which will take care
10428 		 * of ditching any IREs that have been set
10429 		 * up based on the old interface address.
10430 		 */
10431 		err = ipif_logical_down(ipif, q, mp);
10432 		if (err == EINPROGRESS)
10433 			return (err);
10434 		ipif_down_tail(ipif);
10435 		need_up = 1;
10436 	}
10437 
10438 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10439 	return (err);
10440 }
10441 
10442 int
10443 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10444     boolean_t need_up)
10445 {
10446 	in6_addr_t v6addr;
10447 	ipaddr_t addr;
10448 	sin6_t	*sin6;
10449 	int	err = 0;
10450 
10451 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10452 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10453 	ASSERT(IAM_WRITER_IPIF(ipif));
10454 	if (ipif->ipif_isv6) {
10455 		sin6 = (sin6_t *)sin;
10456 		v6addr = sin6->sin6_addr;
10457 	} else {
10458 		addr = sin->sin_addr.s_addr;
10459 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10460 	}
10461 	mutex_enter(&ipif->ipif_ill->ill_lock);
10462 	ipif->ipif_v6lcl_addr = v6addr;
10463 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10464 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10465 	} else {
10466 		ipif->ipif_v6src_addr = v6addr;
10467 	}
10468 
10469 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10470 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10471 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10472 
10473 		/*
10474 		 * The local address of this interface is a 6to4 address,
10475 		 * check if this interface is in fact a 6to4 tunnel or just
10476 		 * an interface configured with a 6to4 address.  We are only
10477 		 * interested in the former.
10478 		 */
10479 		if (wqp != NULL) {
10480 			while ((wqp->q_next != NULL) &&
10481 			    (wqp->q_next->q_qinfo != NULL) &&
10482 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10483 
10484 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10485 				    == TUN6TO4_MODID) {
10486 					/* set for use in IP */
10487 					ipif->ipif_ill->ill_is_6to4tun = 1;
10488 					break;
10489 				}
10490 				wqp = wqp->q_next;
10491 			}
10492 		}
10493 	}
10494 
10495 	ipif_set_default(ipif);
10496 	mutex_exit(&ipif->ipif_ill->ill_lock);
10497 
10498 	if (need_up) {
10499 		/*
10500 		 * Now bring the interface back up.  If this
10501 		 * is the only IPIF for the ILL, ipif_up
10502 		 * will have to re-bind to the device, so
10503 		 * we may get back EINPROGRESS, in which
10504 		 * case, this IOCTL will get completed in
10505 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10506 		 */
10507 		err = ipif_up(ipif, q, mp);
10508 	} else {
10509 		/*
10510 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10511 		 * if need_up is true.
10512 		 */
10513 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10514 	}
10515 
10516 	return (err);
10517 }
10518 
10519 
10520 /*
10521  * Restart entry point to restart the address set operation after the
10522  * refcounts have dropped to zero.
10523  */
10524 /* ARGSUSED */
10525 int
10526 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10527     ip_ioctl_cmd_t *ipip, void *ifreq)
10528 {
10529 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10530 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10531 	ASSERT(IAM_WRITER_IPIF(ipif));
10532 	ipif_down_tail(ipif);
10533 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10534 }
10535 
10536 /* ARGSUSED */
10537 int
10538 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10539     ip_ioctl_cmd_t *ipip, void *if_req)
10540 {
10541 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10542 	struct lifreq *lifr = (struct lifreq *)if_req;
10543 
10544 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10545 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10546 	/*
10547 	 * The net mask and address can't change since we have a
10548 	 * reference to the ipif. So no lock is necessary.
10549 	 */
10550 	if (ipif->ipif_isv6) {
10551 		*sin6 = sin6_null;
10552 		sin6->sin6_family = AF_INET6;
10553 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10554 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10555 		lifr->lifr_addrlen =
10556 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10557 	} else {
10558 		*sin = sin_null;
10559 		sin->sin_family = AF_INET;
10560 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10561 		if (ipip->ipi_cmd_type == LIF_CMD) {
10562 			lifr->lifr_addrlen =
10563 			    ip_mask_to_plen(ipif->ipif_net_mask);
10564 		}
10565 	}
10566 	return (0);
10567 }
10568 
10569 /*
10570  * Set the destination address for a pt-pt interface.
10571  */
10572 /* ARGSUSED */
10573 int
10574 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10575     ip_ioctl_cmd_t *ipip, void *if_req)
10576 {
10577 	int err = 0;
10578 	in6_addr_t v6addr;
10579 	boolean_t need_up = B_FALSE;
10580 
10581 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10582 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10583 	ASSERT(IAM_WRITER_IPIF(ipif));
10584 
10585 	if (ipif->ipif_isv6) {
10586 		sin6_t *sin6;
10587 
10588 		if (sin->sin_family != AF_INET6)
10589 			return (EAFNOSUPPORT);
10590 
10591 		sin6 = (sin6_t *)sin;
10592 		v6addr = sin6->sin6_addr;
10593 
10594 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10595 			return (EADDRNOTAVAIL);
10596 	} else {
10597 		ipaddr_t addr;
10598 
10599 		if (sin->sin_family != AF_INET)
10600 			return (EAFNOSUPPORT);
10601 
10602 		addr = sin->sin_addr.s_addr;
10603 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10604 			return (EADDRNOTAVAIL);
10605 
10606 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10607 	}
10608 
10609 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10610 		return (0);	/* No change */
10611 
10612 	if (ipif->ipif_flags & IPIF_UP) {
10613 		/*
10614 		 * If the interface is already marked up,
10615 		 * we call ipif_down which will take care
10616 		 * of ditching any IREs that have been set
10617 		 * up based on the old pp dst address.
10618 		 */
10619 		err = ipif_logical_down(ipif, q, mp);
10620 		if (err == EINPROGRESS)
10621 			return (err);
10622 		ipif_down_tail(ipif);
10623 		need_up = B_TRUE;
10624 	}
10625 	/*
10626 	 * could return EINPROGRESS. If so ioctl will complete in
10627 	 * ip_rput_dlpi_writer
10628 	 */
10629 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10630 	return (err);
10631 }
10632 
10633 static int
10634 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10635     boolean_t need_up)
10636 {
10637 	in6_addr_t v6addr;
10638 	ill_t	*ill = ipif->ipif_ill;
10639 	int	err = 0;
10640 
10641 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10642 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10643 	if (ipif->ipif_isv6) {
10644 		sin6_t *sin6;
10645 
10646 		sin6 = (sin6_t *)sin;
10647 		v6addr = sin6->sin6_addr;
10648 	} else {
10649 		ipaddr_t addr;
10650 
10651 		addr = sin->sin_addr.s_addr;
10652 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10653 	}
10654 	mutex_enter(&ill->ill_lock);
10655 	/* Set point to point destination address. */
10656 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10657 		/*
10658 		 * Allow this as a means of creating logical
10659 		 * pt-pt interfaces on top of e.g. an Ethernet.
10660 		 * XXX Undocumented HACK for testing.
10661 		 * pt-pt interfaces are created with NUD disabled.
10662 		 */
10663 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10664 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10665 		if (ipif->ipif_isv6)
10666 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10667 	}
10668 
10669 	/* Set the new address. */
10670 	ipif->ipif_v6pp_dst_addr = v6addr;
10671 	/* Make sure subnet tracks pp_dst */
10672 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10673 	mutex_exit(&ill->ill_lock);
10674 
10675 	if (need_up) {
10676 		/*
10677 		 * Now bring the interface back up.  If this
10678 		 * is the only IPIF for the ILL, ipif_up
10679 		 * will have to re-bind to the device, so
10680 		 * we may get back EINPROGRESS, in which
10681 		 * case, this IOCTL will get completed in
10682 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10683 		 */
10684 		err = ipif_up(ipif, q, mp);
10685 	}
10686 	return (err);
10687 }
10688 
10689 /*
10690  * Restart entry point to restart the dstaddress set operation after the
10691  * refcounts have dropped to zero.
10692  */
10693 /* ARGSUSED */
10694 int
10695 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10696     ip_ioctl_cmd_t *ipip, void *ifreq)
10697 {
10698 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10699 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10700 	ipif_down_tail(ipif);
10701 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10702 }
10703 
10704 /* ARGSUSED */
10705 int
10706 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10707     ip_ioctl_cmd_t *ipip, void *if_req)
10708 {
10709 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10710 
10711 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10712 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10713 	/*
10714 	 * Get point to point destination address. The addresses can't
10715 	 * change since we hold a reference to the ipif.
10716 	 */
10717 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10718 		return (EADDRNOTAVAIL);
10719 
10720 	if (ipif->ipif_isv6) {
10721 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10722 		*sin6 = sin6_null;
10723 		sin6->sin6_family = AF_INET6;
10724 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10725 	} else {
10726 		*sin = sin_null;
10727 		sin->sin_family = AF_INET;
10728 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10729 	}
10730 	return (0);
10731 }
10732 
10733 /*
10734  * part of ipmp, make this func return the active/inactive state and
10735  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10736  */
10737 /*
10738  * This function either sets or clears the IFF_INACTIVE flag.
10739  *
10740  * As long as there are some addresses or multicast memberships on the
10741  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10742  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10743  * will be used for outbound packets.
10744  */
10745 static void
10746 phyint_standby_inactive(phyint_t *phyi)
10747 {
10748 	ill_t *ill_v4;
10749 	ill_t *ill_v6;
10750 	ipif_t *ipif;
10751 	ilm_t *ilm;
10752 
10753 	ill_v4 = phyi->phyint_illv4;
10754 	ill_v6 = phyi->phyint_illv6;
10755 
10756 	/*
10757 	 * No need for a lock while traversing the list since iam
10758 	 * a writer
10759 	 */
10760 	if (ill_v4 != NULL) {
10761 		ASSERT(IAM_WRITER_ILL(ill_v4));
10762 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10763 		    ipif = ipif->ipif_next) {
10764 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10765 				mutex_enter(&phyi->phyint_lock);
10766 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10767 				mutex_exit(&phyi->phyint_lock);
10768 				return;
10769 			}
10770 		}
10771 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10772 		    ilm = ilm->ilm_next) {
10773 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10774 				mutex_enter(&phyi->phyint_lock);
10775 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10776 				mutex_exit(&phyi->phyint_lock);
10777 				return;
10778 			}
10779 		}
10780 	}
10781 	if (ill_v6 != NULL) {
10782 		ill_v6 = phyi->phyint_illv6;
10783 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
10784 		    ipif = ipif->ipif_next) {
10785 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10786 				mutex_enter(&phyi->phyint_lock);
10787 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10788 				mutex_exit(&phyi->phyint_lock);
10789 				return;
10790 			}
10791 		}
10792 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
10793 		    ilm = ilm->ilm_next) {
10794 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10795 				mutex_enter(&phyi->phyint_lock);
10796 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10797 				mutex_exit(&phyi->phyint_lock);
10798 				return;
10799 			}
10800 		}
10801 	}
10802 	mutex_enter(&phyi->phyint_lock);
10803 	phyi->phyint_flags |= PHYI_INACTIVE;
10804 	mutex_exit(&phyi->phyint_lock);
10805 }
10806 
10807 /*
10808  * This function is called only when the phyint flags change. Currently
10809  * called from ip_sioctl_flags. We re-do the broadcast nomination so
10810  * that we can select a good ill.
10811  */
10812 static void
10813 ip_redo_nomination(phyint_t *phyi)
10814 {
10815 	ill_t *ill_v4;
10816 
10817 	ill_v4 = phyi->phyint_illv4;
10818 
10819 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
10820 		ASSERT(IAM_WRITER_ILL(ill_v4));
10821 		if (ill_v4->ill_group->illgrp_ill_count > 1)
10822 			ill_nominate_bcast_rcv(ill_v4->ill_group);
10823 	}
10824 }
10825 
10826 /*
10827  * Set interface flags.
10828  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
10829  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
10830  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
10831  *
10832  * NOTE : We really don't enforce that ipif_id zero should be used
10833  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10834  *	  is because applications generally does SICGLIFFLAGS and
10835  *	  ORs in the new flags (that affects the logical) and does a
10836  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10837  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10838  *	  flags that will be turned on is correct with respect to
10839  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10840  */
10841 /* ARGSUSED */
10842 int
10843 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10844     ip_ioctl_cmd_t *ipip, void *if_req)
10845 {
10846 	uint64_t turn_on;
10847 	uint64_t turn_off;
10848 	int	err;
10849 	boolean_t need_up = B_FALSE;
10850 	phyint_t *phyi;
10851 	ill_t *ill;
10852 	uint64_t intf_flags;
10853 	boolean_t phyint_flags_modified = B_FALSE;
10854 	uint64_t flags;
10855 	struct ifreq *ifr;
10856 	struct lifreq *lifr;
10857 	boolean_t set_linklocal = B_FALSE;
10858 	boolean_t zero_source = B_FALSE;
10859 
10860 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10861 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10862 
10863 	ASSERT(IAM_WRITER_IPIF(ipif));
10864 
10865 	ill = ipif->ipif_ill;
10866 	phyi = ill->ill_phyint;
10867 
10868 	if (ipip->ipi_cmd_type == IF_CMD) {
10869 		ifr = (struct ifreq *)if_req;
10870 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10871 	} else {
10872 		lifr = (struct lifreq *)if_req;
10873 		flags = lifr->lifr_flags;
10874 	}
10875 
10876 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10877 
10878 	/*
10879 	 * Has the flags been set correctly till now ?
10880 	 */
10881 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10882 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10883 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10884 	/*
10885 	 * Compare the new flags to the old, and partition
10886 	 * into those coming on and those going off.
10887 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10888 	 */
10889 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10890 		flags |= intf_flags & ~0xFFFF;
10891 
10892 	/*
10893 	 * First check which bits will change and then which will
10894 	 * go on and off
10895 	 */
10896 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
10897 	if (!turn_on)
10898 		return (0);	/* No change */
10899 
10900 	turn_off = intf_flags & turn_on;
10901 	turn_on ^= turn_off;
10902 	err = 0;
10903 
10904 	/*
10905 	 * Don't allow any bits belonging to the logical interface
10906 	 * to be set or cleared on the replacement ipif that was
10907 	 * created temporarily during a MOVE.
10908 	 */
10909 	if (ipif->ipif_replace_zero &&
10910 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
10911 		return (EINVAL);
10912 	}
10913 
10914 	/*
10915 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
10916 	 * IPv6 interfaces.
10917 	 */
10918 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
10919 		return (EINVAL);
10920 
10921 	/*
10922 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10923 	 * interfaces.  It makes no sense in that context.
10924 	 */
10925 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10926 		return (EINVAL);
10927 
10928 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
10929 		zero_source = B_TRUE;
10930 
10931 	/*
10932 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10933 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10934 	 * If the link local address isn't set, and can be set, it will get
10935 	 * set later on in this function.
10936 	 */
10937 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10938 	    (flags & IFF_UP) && !zero_source &&
10939 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10940 		if (ipif_cant_setlinklocal(ipif))
10941 			return (EINVAL);
10942 		set_linklocal = B_TRUE;
10943 	}
10944 
10945 	/*
10946 	 * ILL cannot be part of a usesrc group and and IPMP group at the
10947 	 * same time. No need to grab ill_g_usesrc_lock here, see
10948 	 * synchronization notes in ip.c
10949 	 */
10950 	if (turn_on & PHYI_STANDBY &&
10951 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
10952 		return (EINVAL);
10953 	}
10954 
10955 	/*
10956 	 * If we modify physical interface flags, we'll potentially need to
10957 	 * send up two routing socket messages for the changes (one for the
10958 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10959 	 */
10960 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10961 		phyint_flags_modified = B_TRUE;
10962 
10963 	/*
10964 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
10965 	 * we need to flush the IRE_CACHES belonging to this ill.
10966 	 * We handle this case here without doing the DOWN/UP dance
10967 	 * like it is done for other flags. If some other flags are
10968 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
10969 	 * below will handle it by bringing it down and then
10970 	 * bringing it UP.
10971 	 */
10972 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
10973 		ill_t *ill_v4, *ill_v6;
10974 
10975 		ill_v4 = phyi->phyint_illv4;
10976 		ill_v6 = phyi->phyint_illv6;
10977 
10978 		/*
10979 		 * First set the INACTIVE flag if needed. Then delete the ires.
10980 		 * ire_add will atomically prevent creating new IRE_CACHEs
10981 		 * unless hidden flag is set
10982 		 */
10983 		if (turn_on & PHYI_STANDBY) {
10984 			/*
10985 			 * We set INACTIVE only when STANDBY is set.
10986 			 */
10987 			ASSERT(!(phyi->phyint_flags & PHYI_INACTIVE));
10988 			phyint_standby_inactive(phyi);
10989 		}
10990 		if (turn_off & PHYI_STANDBY) {
10991 			/*
10992 			 * PHYI_INACTIVE makes sense only when PHYI_STANDBY is
10993 			 * set.
10994 			 */
10995 			phyi->phyint_flags &= ~PHYI_INACTIVE;
10996 		}
10997 		/*
10998 		 * We should always send up a message so that the
10999 		 * daemons come to know of it. Note that the zeroth
11000 		 * interface can be down and the check below for IPIF_UP
11001 		 * will not make sense as we are actually setting
11002 		 * a phyint flag here. We assume that the ipif used
11003 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11004 		 * send up any message for non-zero ipifs).
11005 		 */
11006 		phyint_flags_modified = B_TRUE;
11007 
11008 		if (ill_v4 != NULL) {
11009 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11010 			    IRE_CACHE, ill_stq_cache_delete,
11011 			    (char *)ill_v4, ill_v4);
11012 			illgrp_reset_schednext(ill_v4);
11013 		}
11014 		if (ill_v6 != NULL) {
11015 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11016 			    IRE_CACHE, ill_stq_cache_delete,
11017 			    (char *)ill_v6, ill_v6);
11018 			illgrp_reset_schednext(ill_v6);
11019 		}
11020 	}
11021 
11022 	/*
11023 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11024 	 * status of the interface and, if the interface is part of an IPMP
11025 	 * group, all other interfaces that are part of the same IPMP
11026 	 * group.
11027 	 */
11028 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11029 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11030 		    (caddr_t)ill);
11031 	}
11032 
11033 	/*
11034 	 * If the interface is not UP and we are not going to
11035 	 * bring it UP, record the flags and return. When the
11036 	 * interface comes UP later, the right actions will be
11037 	 * taken.
11038 	 */
11039 	if (!(ipif->ipif_flags & IPIF_UP) &&
11040 	    !(turn_on & IPIF_UP)) {
11041 		/* Record new flags in their respective places. */
11042 		mutex_enter(&ill->ill_lock);
11043 		mutex_enter(&ill->ill_phyint->phyint_lock);
11044 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11045 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11046 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11047 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11048 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11049 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11050 		mutex_exit(&ill->ill_lock);
11051 		mutex_exit(&ill->ill_phyint->phyint_lock);
11052 
11053 		/*
11054 		 * We do the broadcast and nomination here rather
11055 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11056 		 * the case of FAILBACK from INACTIVE standby to the
11057 		 * interface that has been repaired, PHYI_FAILED has not
11058 		 * been cleared yet. If there are only two interfaces in
11059 		 * that group, all we have is a FAILED and INACTIVE
11060 		 * interface. If we do the nomination soon after a failback,
11061 		 * the broadcast nomination code would select the
11062 		 * INACTIVE interface for receiving broadcasts as FAILED is
11063 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11064 		 * receive broadcast packets, we need to redo nomination
11065 		 * when the FAILED is cleared here. Thus, in general we
11066 		 * always do the nomination here for FAILED, STANDBY
11067 		 * and OFFLINE.
11068 		 */
11069 		if (((turn_on | turn_off) &
11070 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11071 			ip_redo_nomination(phyi);
11072 		}
11073 		if (phyint_flags_modified) {
11074 			if (phyi->phyint_illv4 != NULL) {
11075 				ip_rts_ifmsg(phyi->phyint_illv4->
11076 				    ill_ipif);
11077 			}
11078 			if (phyi->phyint_illv6 != NULL) {
11079 				ip_rts_ifmsg(phyi->phyint_illv6->
11080 				    ill_ipif);
11081 			}
11082 		}
11083 		return (0);
11084 	} else if (set_linklocal || zero_source) {
11085 		mutex_enter(&ill->ill_lock);
11086 		if (set_linklocal)
11087 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11088 		if (zero_source)
11089 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11090 		mutex_exit(&ill->ill_lock);
11091 	}
11092 
11093 	/*
11094 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11095 	 * or point-to-point interfaces with an unspecified destination. We do
11096 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11097 	 * have a subnet assigned, which is how in.ndpd currently manages its
11098 	 * onlink prefix list when no addresses are configured with those
11099 	 * prefixes.
11100 	 */
11101 	if (ipif->ipif_isv6 &&
11102 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11103 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11104 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11105 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11106 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11107 		return (EINVAL);
11108 	}
11109 
11110 	/*
11111 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11112 	 * from being brought up.
11113 	 */
11114 	if (!ipif->ipif_isv6 &&
11115 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11116 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11117 		return (EINVAL);
11118 	}
11119 
11120 	/*
11121 	 * The only flag changes that we currently take specific action on
11122 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11123 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11124 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11125 	 * the flags and bringing it back up again.
11126 	 */
11127 	if ((turn_on|turn_off) &
11128 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11129 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11130 		/*
11131 		 * Taking this ipif down, make sure we have
11132 		 * valid net and subnet bcast ire's for other
11133 		 * logical interfaces, if we need them.
11134 		 */
11135 		if (!ipif->ipif_isv6)
11136 			ipif_check_bcast_ires(ipif);
11137 
11138 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11139 		    !(turn_off & IPIF_UP)) {
11140 			need_up = B_TRUE;
11141 			if (ipif->ipif_flags & IPIF_UP)
11142 				ill->ill_logical_down = 1;
11143 			turn_on &= ~IPIF_UP;
11144 		}
11145 		err = ipif_down(ipif, q, mp);
11146 		ip1dbg(("ipif_down returns %d err ", err));
11147 		if (err == EINPROGRESS)
11148 			return (err);
11149 		ipif_down_tail(ipif);
11150 	}
11151 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11152 }
11153 
11154 static int
11155 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11156     boolean_t need_up)
11157 {
11158 	ill_t	*ill;
11159 	phyint_t *phyi;
11160 	uint64_t turn_on;
11161 	uint64_t turn_off;
11162 	uint64_t intf_flags;
11163 	boolean_t phyint_flags_modified = B_FALSE;
11164 	int	err = 0;
11165 	boolean_t set_linklocal = B_FALSE;
11166 	boolean_t zero_source = B_FALSE;
11167 
11168 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11169 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11170 
11171 	ASSERT(IAM_WRITER_IPIF(ipif));
11172 
11173 	ill = ipif->ipif_ill;
11174 	phyi = ill->ill_phyint;
11175 
11176 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11177 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11178 
11179 	turn_off = intf_flags & turn_on;
11180 	turn_on ^= turn_off;
11181 
11182 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11183 		phyint_flags_modified = B_TRUE;
11184 
11185 	/*
11186 	 * Now we change the flags. Track current value of
11187 	 * other flags in their respective places.
11188 	 */
11189 	mutex_enter(&ill->ill_lock);
11190 	mutex_enter(&phyi->phyint_lock);
11191 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11192 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11193 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11194 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11195 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11196 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11197 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11198 		set_linklocal = B_TRUE;
11199 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11200 	}
11201 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11202 		zero_source = B_TRUE;
11203 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11204 	}
11205 	mutex_exit(&ill->ill_lock);
11206 	mutex_exit(&phyi->phyint_lock);
11207 
11208 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11209 		ip_redo_nomination(phyi);
11210 
11211 	if (set_linklocal)
11212 		(void) ipif_setlinklocal(ipif);
11213 
11214 	if (zero_source)
11215 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11216 	else
11217 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11218 
11219 	if (need_up) {
11220 		/*
11221 		 * XXX ipif_up really does not know whether a phyint flags
11222 		 * was modified or not. So, it sends up information on
11223 		 * only one routing sockets message. As we don't bring up
11224 		 * the interface and also set STANDBY/FAILED simultaneously
11225 		 * it should be okay.
11226 		 */
11227 		err = ipif_up(ipif, q, mp);
11228 	} else {
11229 		/*
11230 		 * Make sure routing socket sees all changes to the flags.
11231 		 * ipif_up_done* handles this when we use ipif_up.
11232 		 */
11233 		if (phyint_flags_modified) {
11234 			if (phyi->phyint_illv4 != NULL) {
11235 				ip_rts_ifmsg(phyi->phyint_illv4->
11236 				    ill_ipif);
11237 			}
11238 			if (phyi->phyint_illv6 != NULL) {
11239 				ip_rts_ifmsg(phyi->phyint_illv6->
11240 				    ill_ipif);
11241 			}
11242 		} else {
11243 			ip_rts_ifmsg(ipif);
11244 		}
11245 	}
11246 	return (err);
11247 }
11248 
11249 /*
11250  * Restart entry point to restart the flags restart operation after the
11251  * refcounts have dropped to zero.
11252  */
11253 /* ARGSUSED */
11254 int
11255 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11256     ip_ioctl_cmd_t *ipip, void *if_req)
11257 {
11258 	int	err;
11259 	struct ifreq *ifr = (struct ifreq *)if_req;
11260 	struct lifreq *lifr = (struct lifreq *)if_req;
11261 
11262 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11263 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11264 
11265 	ipif_down_tail(ipif);
11266 	if (ipip->ipi_cmd_type == IF_CMD) {
11267 		/*
11268 		 * Since ip_sioctl_flags expects an int and ifr_flags
11269 		 * is a short we need to cast ifr_flags into an int
11270 		 * to avoid having sign extension cause bits to get
11271 		 * set that should not be.
11272 		 */
11273 		err = ip_sioctl_flags_tail(ipif,
11274 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11275 		    q, mp, B_TRUE);
11276 	} else {
11277 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11278 		    q, mp, B_TRUE);
11279 	}
11280 	return (err);
11281 }
11282 
11283 /* ARGSUSED */
11284 int
11285 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11286     ip_ioctl_cmd_t *ipip, void *if_req)
11287 {
11288 	/*
11289 	 * Has the flags been set correctly till now ?
11290 	 */
11291 	ill_t *ill = ipif->ipif_ill;
11292 	phyint_t *phyi = ill->ill_phyint;
11293 
11294 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11295 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11296 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11297 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11298 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11299 
11300 	/*
11301 	 * Need a lock since some flags can be set even when there are
11302 	 * references to the ipif.
11303 	 */
11304 	mutex_enter(&ill->ill_lock);
11305 	if (ipip->ipi_cmd_type == IF_CMD) {
11306 		struct ifreq *ifr = (struct ifreq *)if_req;
11307 
11308 		/* Get interface flags (low 16 only). */
11309 		ifr->ifr_flags = ((ipif->ipif_flags |
11310 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11311 	} else {
11312 		struct lifreq *lifr = (struct lifreq *)if_req;
11313 
11314 		/* Get interface flags. */
11315 		lifr->lifr_flags = ipif->ipif_flags |
11316 		    ill->ill_flags | phyi->phyint_flags;
11317 	}
11318 	mutex_exit(&ill->ill_lock);
11319 	return (0);
11320 }
11321 
11322 /* ARGSUSED */
11323 int
11324 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11325     ip_ioctl_cmd_t *ipip, void *if_req)
11326 {
11327 	int mtu;
11328 	int ip_min_mtu;
11329 	struct ifreq	*ifr;
11330 	struct lifreq *lifr;
11331 	ire_t	*ire;
11332 
11333 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11334 	    ipif->ipif_id, (void *)ipif));
11335 	if (ipip->ipi_cmd_type == IF_CMD) {
11336 		ifr = (struct ifreq *)if_req;
11337 		mtu = ifr->ifr_metric;
11338 	} else {
11339 		lifr = (struct lifreq *)if_req;
11340 		mtu = lifr->lifr_mtu;
11341 	}
11342 
11343 	if (ipif->ipif_isv6)
11344 		ip_min_mtu = IPV6_MIN_MTU;
11345 	else
11346 		ip_min_mtu = IP_MIN_MTU;
11347 
11348 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11349 		return (EINVAL);
11350 
11351 	/*
11352 	 * Change the MTU size in all relevant ire's.
11353 	 * Mtu change Vs. new ire creation - protocol below.
11354 	 * First change ipif_mtu and the ire_max_frag of the
11355 	 * interface ire. Then do an ire walk and change the
11356 	 * ire_max_frag of all affected ires. During ire_add
11357 	 * under the bucket lock, set the ire_max_frag of the
11358 	 * new ire being created from the ipif/ire from which
11359 	 * it is being derived. If an mtu change happens after
11360 	 * the ire is added, the new ire will be cleaned up.
11361 	 * Conversely if the mtu change happens before the ire
11362 	 * is added, ire_add will see the new value of the mtu.
11363 	 */
11364 	ipif->ipif_mtu = mtu;
11365 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11366 
11367 	if (ipif->ipif_isv6)
11368 		ire = ipif_to_ire_v6(ipif);
11369 	else
11370 		ire = ipif_to_ire(ipif);
11371 	if (ire != NULL) {
11372 		ire->ire_max_frag = ipif->ipif_mtu;
11373 		ire_refrele(ire);
11374 	}
11375 	if (ipif->ipif_flags & IPIF_UP) {
11376 		if (ipif->ipif_isv6)
11377 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11378 		else
11379 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11380 	}
11381 	/* Update the MTU in SCTP's list */
11382 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11383 	return (0);
11384 }
11385 
11386 /* Get interface MTU. */
11387 /* ARGSUSED */
11388 int
11389 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11390 	ip_ioctl_cmd_t *ipip, void *if_req)
11391 {
11392 	struct ifreq	*ifr;
11393 	struct lifreq	*lifr;
11394 
11395 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11396 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11397 	if (ipip->ipi_cmd_type == IF_CMD) {
11398 		ifr = (struct ifreq *)if_req;
11399 		ifr->ifr_metric = ipif->ipif_mtu;
11400 	} else {
11401 		lifr = (struct lifreq *)if_req;
11402 		lifr->lifr_mtu = ipif->ipif_mtu;
11403 	}
11404 	return (0);
11405 }
11406 
11407 /* Set interface broadcast address. */
11408 /* ARGSUSED2 */
11409 int
11410 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11411 	ip_ioctl_cmd_t *ipip, void *if_req)
11412 {
11413 	ipaddr_t addr;
11414 	ire_t	*ire;
11415 
11416 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11417 	    ipif->ipif_id));
11418 
11419 	ASSERT(IAM_WRITER_IPIF(ipif));
11420 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11421 		return (EADDRNOTAVAIL);
11422 
11423 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11424 
11425 	if (sin->sin_family != AF_INET)
11426 		return (EAFNOSUPPORT);
11427 
11428 	addr = sin->sin_addr.s_addr;
11429 	if (ipif->ipif_flags & IPIF_UP) {
11430 		/*
11431 		 * If we are already up, make sure the new
11432 		 * broadcast address makes sense.  If it does,
11433 		 * there should be an IRE for it already.
11434 		 * Don't match on ipif, only on the ill
11435 		 * since we are sharing these now. Don't use
11436 		 * MATCH_IRE_ILL_GROUP as we are looking for
11437 		 * the broadcast ire on this ill and each ill
11438 		 * in the group has its own broadcast ire.
11439 		 */
11440 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11441 		    ipif, ALL_ZONES, (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11442 		if (ire == NULL) {
11443 			return (EINVAL);
11444 		} else {
11445 			ire_refrele(ire);
11446 		}
11447 	}
11448 	/*
11449 	 * Changing the broadcast addr for this ipif.
11450 	 * Make sure we have valid net and subnet bcast
11451 	 * ire's for other logical interfaces, if needed.
11452 	 */
11453 	if (addr != ipif->ipif_brd_addr)
11454 		ipif_check_bcast_ires(ipif);
11455 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11456 	return (0);
11457 }
11458 
11459 /* Get interface broadcast address. */
11460 /* ARGSUSED */
11461 int
11462 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11463     ip_ioctl_cmd_t *ipip, void *if_req)
11464 {
11465 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11466 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11467 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11468 		return (EADDRNOTAVAIL);
11469 
11470 	/* IPIF_BROADCAST not possible with IPv6 */
11471 	ASSERT(!ipif->ipif_isv6);
11472 	*sin = sin_null;
11473 	sin->sin_family = AF_INET;
11474 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11475 	return (0);
11476 }
11477 
11478 /*
11479  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11480  */
11481 /* ARGSUSED */
11482 int
11483 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11484     ip_ioctl_cmd_t *ipip, void *if_req)
11485 {
11486 	int err = 0;
11487 	in6_addr_t v6mask;
11488 
11489 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11490 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11491 
11492 	ASSERT(IAM_WRITER_IPIF(ipif));
11493 
11494 	if (ipif->ipif_isv6) {
11495 		sin6_t *sin6;
11496 
11497 		if (sin->sin_family != AF_INET6)
11498 			return (EAFNOSUPPORT);
11499 
11500 		sin6 = (sin6_t *)sin;
11501 		v6mask = sin6->sin6_addr;
11502 	} else {
11503 		ipaddr_t mask;
11504 
11505 		if (sin->sin_family != AF_INET)
11506 			return (EAFNOSUPPORT);
11507 
11508 		mask = sin->sin_addr.s_addr;
11509 		V4MASK_TO_V6(mask, v6mask);
11510 	}
11511 
11512 	/*
11513 	 * No big deal if the interface isn't already up, or the mask
11514 	 * isn't really changing, or this is pt-pt.
11515 	 */
11516 	if (!(ipif->ipif_flags & IPIF_UP) ||
11517 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11518 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11519 		ipif->ipif_v6net_mask = v6mask;
11520 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11521 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11522 			    ipif->ipif_v6net_mask,
11523 			    ipif->ipif_v6subnet);
11524 		}
11525 		return (0);
11526 	}
11527 	/*
11528 	 * Make sure we have valid net and subnet broadcast ire's
11529 	 * for the old netmask, if needed by other logical interfaces.
11530 	 */
11531 	if (!ipif->ipif_isv6)
11532 		ipif_check_bcast_ires(ipif);
11533 
11534 	err = ipif_logical_down(ipif, q, mp);
11535 	if (err == EINPROGRESS)
11536 		return (err);
11537 	ipif_down_tail(ipif);
11538 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11539 	return (err);
11540 }
11541 
11542 static int
11543 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11544 {
11545 	in6_addr_t v6mask;
11546 	int err = 0;
11547 
11548 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11549 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11550 
11551 	if (ipif->ipif_isv6) {
11552 		sin6_t *sin6;
11553 
11554 		sin6 = (sin6_t *)sin;
11555 		v6mask = sin6->sin6_addr;
11556 	} else {
11557 		ipaddr_t mask;
11558 
11559 		mask = sin->sin_addr.s_addr;
11560 		V4MASK_TO_V6(mask, v6mask);
11561 	}
11562 
11563 	ipif->ipif_v6net_mask = v6mask;
11564 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11565 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11566 		    ipif->ipif_v6subnet);
11567 	}
11568 	err = ipif_up(ipif, q, mp);
11569 
11570 	if (err == 0 || err == EINPROGRESS) {
11571 		/*
11572 		 * The interface must be DL_BOUND if this packet has to
11573 		 * go out on the wire. Since we only go through a logical
11574 		 * down and are bound with the driver during an internal
11575 		 * down/up that is satisfied.
11576 		 */
11577 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11578 			/* Potentially broadcast an address mask reply. */
11579 			ipif_mask_reply(ipif);
11580 		}
11581 	}
11582 	return (err);
11583 }
11584 
11585 /* ARGSUSED */
11586 int
11587 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11588     ip_ioctl_cmd_t *ipip, void *if_req)
11589 {
11590 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11591 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11592 	ipif_down_tail(ipif);
11593 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11594 }
11595 
11596 /* Get interface net mask. */
11597 /* ARGSUSED */
11598 int
11599 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11600     ip_ioctl_cmd_t *ipip, void *if_req)
11601 {
11602 	struct lifreq *lifr = (struct lifreq *)if_req;
11603 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11604 
11605 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11606 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11607 
11608 	/*
11609 	 * net mask can't change since we have a reference to the ipif.
11610 	 */
11611 	if (ipif->ipif_isv6) {
11612 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11613 		*sin6 = sin6_null;
11614 		sin6->sin6_family = AF_INET6;
11615 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11616 		lifr->lifr_addrlen =
11617 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11618 	} else {
11619 		*sin = sin_null;
11620 		sin->sin_family = AF_INET;
11621 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11622 		if (ipip->ipi_cmd_type == LIF_CMD) {
11623 			lifr->lifr_addrlen =
11624 			    ip_mask_to_plen(ipif->ipif_net_mask);
11625 		}
11626 	}
11627 	return (0);
11628 }
11629 
11630 /* ARGSUSED */
11631 int
11632 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11633     ip_ioctl_cmd_t *ipip, void *if_req)
11634 {
11635 
11636 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11637 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11638 	/*
11639 	 * Set interface metric.  We don't use this for
11640 	 * anything but we keep track of it in case it is
11641 	 * important to routing applications or such.
11642 	 */
11643 	if (ipip->ipi_cmd_type == IF_CMD) {
11644 		struct ifreq    *ifr;
11645 
11646 		ifr = (struct ifreq *)if_req;
11647 		ipif->ipif_metric = ifr->ifr_metric;
11648 	} else {
11649 		struct lifreq   *lifr;
11650 
11651 		lifr = (struct lifreq *)if_req;
11652 		ipif->ipif_metric = lifr->lifr_metric;
11653 	}
11654 	return (0);
11655 }
11656 
11657 
11658 /* ARGSUSED */
11659 int
11660 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11661     ip_ioctl_cmd_t *ipip, void *if_req)
11662 {
11663 
11664 	/* Get interface metric. */
11665 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11666 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11667 	if (ipip->ipi_cmd_type == IF_CMD) {
11668 		struct ifreq    *ifr;
11669 
11670 		ifr = (struct ifreq *)if_req;
11671 		ifr->ifr_metric = ipif->ipif_metric;
11672 	} else {
11673 		struct lifreq   *lifr;
11674 
11675 		lifr = (struct lifreq *)if_req;
11676 		lifr->lifr_metric = ipif->ipif_metric;
11677 	}
11678 
11679 	return (0);
11680 }
11681 
11682 /* ARGSUSED */
11683 int
11684 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11685     ip_ioctl_cmd_t *ipip, void *if_req)
11686 {
11687 
11688 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11689 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11690 	/*
11691 	 * Set the muxid returned from I_PLINK.
11692 	 */
11693 	if (ipip->ipi_cmd_type == IF_CMD) {
11694 		struct ifreq *ifr = (struct ifreq *)if_req;
11695 
11696 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11697 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11698 	} else {
11699 		struct lifreq *lifr = (struct lifreq *)if_req;
11700 
11701 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11702 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11703 	}
11704 	return (0);
11705 }
11706 
11707 /* ARGSUSED */
11708 int
11709 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11710     ip_ioctl_cmd_t *ipip, void *if_req)
11711 {
11712 
11713 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11714 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11715 	/*
11716 	 * Get the muxid saved in ill for I_PUNLINK.
11717 	 */
11718 	if (ipip->ipi_cmd_type == IF_CMD) {
11719 		struct ifreq *ifr = (struct ifreq *)if_req;
11720 
11721 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11722 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11723 	} else {
11724 		struct lifreq *lifr = (struct lifreq *)if_req;
11725 
11726 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11727 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11728 	}
11729 	return (0);
11730 }
11731 
11732 /*
11733  * Set the subnet prefix. Does not modify the broadcast address.
11734  */
11735 /* ARGSUSED */
11736 int
11737 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11738     ip_ioctl_cmd_t *ipip, void *if_req)
11739 {
11740 	int err = 0;
11741 	in6_addr_t v6addr;
11742 	in6_addr_t v6mask;
11743 	boolean_t need_up = B_FALSE;
11744 	int addrlen;
11745 
11746 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11747 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11748 
11749 	ASSERT(IAM_WRITER_IPIF(ipif));
11750 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11751 
11752 	if (ipif->ipif_isv6) {
11753 		sin6_t *sin6;
11754 
11755 		if (sin->sin_family != AF_INET6)
11756 			return (EAFNOSUPPORT);
11757 
11758 		sin6 = (sin6_t *)sin;
11759 		v6addr = sin6->sin6_addr;
11760 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11761 			return (EADDRNOTAVAIL);
11762 	} else {
11763 		ipaddr_t addr;
11764 
11765 		if (sin->sin_family != AF_INET)
11766 			return (EAFNOSUPPORT);
11767 
11768 		addr = sin->sin_addr.s_addr;
11769 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11770 			return (EADDRNOTAVAIL);
11771 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11772 		/* Add 96 bits */
11773 		addrlen += IPV6_ABITS - IP_ABITS;
11774 	}
11775 
11776 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11777 		return (EINVAL);
11778 
11779 	/* Check if bits in the address is set past the mask */
11780 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11781 		return (EINVAL);
11782 
11783 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11784 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11785 		return (0);	/* No change */
11786 
11787 	if (ipif->ipif_flags & IPIF_UP) {
11788 		/*
11789 		 * If the interface is already marked up,
11790 		 * we call ipif_down which will take care
11791 		 * of ditching any IREs that have been set
11792 		 * up based on the old interface address.
11793 		 */
11794 		err = ipif_logical_down(ipif, q, mp);
11795 		if (err == EINPROGRESS)
11796 			return (err);
11797 		ipif_down_tail(ipif);
11798 		need_up = B_TRUE;
11799 	}
11800 
11801 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11802 	return (err);
11803 }
11804 
11805 static int
11806 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11807     queue_t *q, mblk_t *mp, boolean_t need_up)
11808 {
11809 	ill_t	*ill = ipif->ipif_ill;
11810 	int	err = 0;
11811 
11812 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11813 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11814 
11815 	/* Set the new address. */
11816 	mutex_enter(&ill->ill_lock);
11817 	ipif->ipif_v6net_mask = v6mask;
11818 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11819 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11820 		    ipif->ipif_v6subnet);
11821 	}
11822 	mutex_exit(&ill->ill_lock);
11823 
11824 	if (need_up) {
11825 		/*
11826 		 * Now bring the interface back up.  If this
11827 		 * is the only IPIF for the ILL, ipif_up
11828 		 * will have to re-bind to the device, so
11829 		 * we may get back EINPROGRESS, in which
11830 		 * case, this IOCTL will get completed in
11831 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11832 		 */
11833 		err = ipif_up(ipif, q, mp);
11834 		if (err == EINPROGRESS)
11835 			return (err);
11836 	}
11837 	return (err);
11838 }
11839 
11840 /* ARGSUSED */
11841 int
11842 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11843     ip_ioctl_cmd_t *ipip, void *if_req)
11844 {
11845 	int	addrlen;
11846 	in6_addr_t v6addr;
11847 	in6_addr_t v6mask;
11848 	struct lifreq *lifr = (struct lifreq *)if_req;
11849 
11850 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11851 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11852 	ipif_down_tail(ipif);
11853 
11854 	addrlen = lifr->lifr_addrlen;
11855 	if (ipif->ipif_isv6) {
11856 		sin6_t *sin6;
11857 
11858 		sin6 = (sin6_t *)sin;
11859 		v6addr = sin6->sin6_addr;
11860 	} else {
11861 		ipaddr_t addr;
11862 
11863 		addr = sin->sin_addr.s_addr;
11864 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11865 		addrlen += IPV6_ABITS - IP_ABITS;
11866 	}
11867 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11868 
11869 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11870 }
11871 
11872 /* ARGSUSED */
11873 int
11874 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11875     ip_ioctl_cmd_t *ipip, void *if_req)
11876 {
11877 	struct lifreq *lifr = (struct lifreq *)if_req;
11878 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11879 
11880 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11881 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11882 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11883 
11884 	if (ipif->ipif_isv6) {
11885 		*sin6 = sin6_null;
11886 		sin6->sin6_family = AF_INET6;
11887 		sin6->sin6_addr = ipif->ipif_v6subnet;
11888 		lifr->lifr_addrlen =
11889 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11890 	} else {
11891 		*sin = sin_null;
11892 		sin->sin_family = AF_INET;
11893 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11894 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11895 	}
11896 	return (0);
11897 }
11898 
11899 /*
11900  * Set the IPv6 address token.
11901  */
11902 /* ARGSUSED */
11903 int
11904 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11905     ip_ioctl_cmd_t *ipi, void *if_req)
11906 {
11907 	ill_t *ill = ipif->ipif_ill;
11908 	int err;
11909 	in6_addr_t v6addr;
11910 	in6_addr_t v6mask;
11911 	boolean_t need_up = B_FALSE;
11912 	int i;
11913 	sin6_t *sin6 = (sin6_t *)sin;
11914 	struct lifreq *lifr = (struct lifreq *)if_req;
11915 	int addrlen;
11916 
11917 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11918 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11919 	ASSERT(IAM_WRITER_IPIF(ipif));
11920 
11921 	addrlen = lifr->lifr_addrlen;
11922 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11923 	if (ipif->ipif_id != 0)
11924 		return (EINVAL);
11925 
11926 	if (!ipif->ipif_isv6)
11927 		return (EINVAL);
11928 
11929 	if (addrlen > IPV6_ABITS)
11930 		return (EINVAL);
11931 
11932 	v6addr = sin6->sin6_addr;
11933 
11934 	/*
11935 	 * The length of the token is the length from the end.  To get
11936 	 * the proper mask for this, compute the mask of the bits not
11937 	 * in the token; ie. the prefix, and then xor to get the mask.
11938 	 */
11939 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11940 		return (EINVAL);
11941 	for (i = 0; i < 4; i++) {
11942 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11943 	}
11944 
11945 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11946 	    ill->ill_token_length == addrlen)
11947 		return (0);	/* No change */
11948 
11949 	if (ipif->ipif_flags & IPIF_UP) {
11950 		err = ipif_logical_down(ipif, q, mp);
11951 		if (err == EINPROGRESS)
11952 			return (err);
11953 		ipif_down_tail(ipif);
11954 		need_up = B_TRUE;
11955 	}
11956 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11957 	return (err);
11958 }
11959 
11960 static int
11961 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11962     mblk_t *mp, boolean_t need_up)
11963 {
11964 	in6_addr_t v6addr;
11965 	in6_addr_t v6mask;
11966 	ill_t	*ill = ipif->ipif_ill;
11967 	int	i;
11968 	int	err = 0;
11969 
11970 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11971 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11972 	v6addr = sin6->sin6_addr;
11973 	/*
11974 	 * The length of the token is the length from the end.  To get
11975 	 * the proper mask for this, compute the mask of the bits not
11976 	 * in the token; ie. the prefix, and then xor to get the mask.
11977 	 */
11978 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11979 	for (i = 0; i < 4; i++)
11980 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11981 
11982 	mutex_enter(&ill->ill_lock);
11983 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11984 	ill->ill_token_length = addrlen;
11985 	mutex_exit(&ill->ill_lock);
11986 
11987 	if (need_up) {
11988 		/*
11989 		 * Now bring the interface back up.  If this
11990 		 * is the only IPIF for the ILL, ipif_up
11991 		 * will have to re-bind to the device, so
11992 		 * we may get back EINPROGRESS, in which
11993 		 * case, this IOCTL will get completed in
11994 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11995 		 */
11996 		err = ipif_up(ipif, q, mp);
11997 		if (err == EINPROGRESS)
11998 			return (err);
11999 	}
12000 	return (err);
12001 }
12002 
12003 /* ARGSUSED */
12004 int
12005 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12006     ip_ioctl_cmd_t *ipi, void *if_req)
12007 {
12008 	ill_t *ill;
12009 	sin6_t *sin6 = (sin6_t *)sin;
12010 	struct lifreq *lifr = (struct lifreq *)if_req;
12011 
12012 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12013 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12014 	if (ipif->ipif_id != 0)
12015 		return (EINVAL);
12016 
12017 	ill = ipif->ipif_ill;
12018 	if (!ill->ill_isv6)
12019 		return (ENXIO);
12020 
12021 	*sin6 = sin6_null;
12022 	sin6->sin6_family = AF_INET6;
12023 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12024 	sin6->sin6_addr = ill->ill_token;
12025 	lifr->lifr_addrlen = ill->ill_token_length;
12026 	return (0);
12027 }
12028 
12029 /*
12030  * Set (hardware) link specific information that might override
12031  * what was acquired through the DL_INFO_ACK.
12032  * The logic is as follows.
12033  *
12034  * become exclusive
12035  * set CHANGING flag
12036  * change mtu on affected IREs
12037  * clear CHANGING flag
12038  *
12039  * An ire add that occurs before the CHANGING flag is set will have its mtu
12040  * changed by the ip_sioctl_lnkinfo.
12041  *
12042  * During the time the CHANGING flag is set, no new ires will be added to the
12043  * bucket, and ire add will fail (due the CHANGING flag).
12044  *
12045  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12046  * before it is added to the bucket.
12047  *
12048  * Obviously only 1 thread can set the CHANGING flag and we need to become
12049  * exclusive to set the flag.
12050  */
12051 /* ARGSUSED */
12052 int
12053 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12054     ip_ioctl_cmd_t *ipi, void *if_req)
12055 {
12056 	ill_t		*ill = ipif->ipif_ill;
12057 	ipif_t		*nipif;
12058 	int		ip_min_mtu;
12059 	boolean_t	mtu_walk = B_FALSE;
12060 	struct lifreq	*lifr = (struct lifreq *)if_req;
12061 	lif_ifinfo_req_t *lir;
12062 	ire_t		*ire;
12063 
12064 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12065 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12066 	lir = &lifr->lifr_ifinfo;
12067 	ASSERT(IAM_WRITER_IPIF(ipif));
12068 
12069 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12070 	if (ipif->ipif_id != 0)
12071 		return (EINVAL);
12072 
12073 	/* Set interface MTU. */
12074 	if (ipif->ipif_isv6)
12075 		ip_min_mtu = IPV6_MIN_MTU;
12076 	else
12077 		ip_min_mtu = IP_MIN_MTU;
12078 
12079 	/*
12080 	 * Verify values before we set anything. Allow zero to
12081 	 * mean unspecified.
12082 	 */
12083 	if (lir->lir_maxmtu != 0 &&
12084 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12085 	    lir->lir_maxmtu < ip_min_mtu))
12086 		return (EINVAL);
12087 	if (lir->lir_reachtime != 0 &&
12088 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12089 		return (EINVAL);
12090 	if (lir->lir_reachretrans != 0 &&
12091 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12092 		return (EINVAL);
12093 
12094 	mutex_enter(&ill->ill_lock);
12095 	ill->ill_state_flags |= ILL_CHANGING;
12096 	for (nipif = ill->ill_ipif; nipif != NULL;
12097 	    nipif = nipif->ipif_next) {
12098 		nipif->ipif_state_flags |= IPIF_CHANGING;
12099 	}
12100 
12101 	mutex_exit(&ill->ill_lock);
12102 
12103 	if (lir->lir_maxmtu != 0) {
12104 		ill->ill_max_mtu = lir->lir_maxmtu;
12105 		ill->ill_mtu_userspecified = 1;
12106 		mtu_walk = B_TRUE;
12107 	}
12108 
12109 	if (lir->lir_reachtime != 0)
12110 		ill->ill_reachable_time = lir->lir_reachtime;
12111 
12112 	if (lir->lir_reachretrans != 0)
12113 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12114 
12115 	ill->ill_max_hops = lir->lir_maxhops;
12116 
12117 	ill->ill_max_buf = ND_MAX_Q;
12118 
12119 	if (mtu_walk) {
12120 		/*
12121 		 * Set the MTU on all ipifs associated with this ill except
12122 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12123 		 */
12124 		for (nipif = ill->ill_ipif; nipif != NULL;
12125 		    nipif = nipif->ipif_next) {
12126 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12127 				continue;
12128 
12129 			nipif->ipif_mtu = ill->ill_max_mtu;
12130 
12131 			if (!(nipif->ipif_flags & IPIF_UP))
12132 				continue;
12133 
12134 			if (nipif->ipif_isv6)
12135 				ire = ipif_to_ire_v6(nipif);
12136 			else
12137 				ire = ipif_to_ire(nipif);
12138 			if (ire != NULL) {
12139 				ire->ire_max_frag = ipif->ipif_mtu;
12140 				ire_refrele(ire);
12141 			}
12142 			if (ill->ill_isv6) {
12143 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12144 				    ipif_mtu_change, (char *)nipif,
12145 				    ill);
12146 			} else {
12147 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12148 				    ipif_mtu_change, (char *)nipif,
12149 				    ill);
12150 			}
12151 		}
12152 	}
12153 
12154 	mutex_enter(&ill->ill_lock);
12155 	for (nipif = ill->ill_ipif; nipif != NULL;
12156 	    nipif = nipif->ipif_next) {
12157 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12158 	}
12159 	ILL_UNMARK_CHANGING(ill);
12160 	mutex_exit(&ill->ill_lock);
12161 
12162 	return (0);
12163 }
12164 
12165 /* ARGSUSED */
12166 int
12167 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12168     ip_ioctl_cmd_t *ipi, void *if_req)
12169 {
12170 	struct lif_ifinfo_req *lir;
12171 	ill_t *ill = ipif->ipif_ill;
12172 
12173 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12174 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12175 	if (ipif->ipif_id != 0)
12176 		return (EINVAL);
12177 
12178 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12179 	lir->lir_maxhops = ill->ill_max_hops;
12180 	lir->lir_reachtime = ill->ill_reachable_time;
12181 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12182 	lir->lir_maxmtu = ill->ill_max_mtu;
12183 
12184 	return (0);
12185 }
12186 
12187 /*
12188  * Return best guess as to the subnet mask for the specified address.
12189  * Based on the subnet masks for all the configured interfaces.
12190  *
12191  * We end up returning a zero mask in the case of default, multicast or
12192  * experimental.
12193  */
12194 static ipaddr_t
12195 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12196 {
12197 	ipaddr_t net_mask;
12198 	ill_t	*ill;
12199 	ipif_t	*ipif;
12200 	ill_walk_context_t ctx;
12201 	ipif_t	*fallback_ipif = NULL;
12202 
12203 	net_mask = ip_net_mask(addr);
12204 	if (net_mask == 0) {
12205 		*ipifp = NULL;
12206 		return (0);
12207 	}
12208 
12209 	/* Let's check to see if this is maybe a local subnet route. */
12210 	/* this function only applies to IPv4 interfaces */
12211 	rw_enter(&ill_g_lock, RW_READER);
12212 	ill = ILL_START_WALK_V4(&ctx);
12213 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12214 		mutex_enter(&ill->ill_lock);
12215 		for (ipif = ill->ill_ipif; ipif != NULL;
12216 		    ipif = ipif->ipif_next) {
12217 			if (!IPIF_CAN_LOOKUP(ipif))
12218 				continue;
12219 			if (!(ipif->ipif_flags & IPIF_UP))
12220 				continue;
12221 			if ((ipif->ipif_subnet & net_mask) ==
12222 			    (addr & net_mask)) {
12223 				/*
12224 				 * Don't trust pt-pt interfaces if there are
12225 				 * other interfaces.
12226 				 */
12227 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12228 					if (fallback_ipif == NULL) {
12229 						ipif_refhold_locked(ipif);
12230 						fallback_ipif = ipif;
12231 					}
12232 					continue;
12233 				}
12234 
12235 				/*
12236 				 * Fine. Just assume the same net mask as the
12237 				 * directly attached subnet interface is using.
12238 				 */
12239 				ipif_refhold_locked(ipif);
12240 				mutex_exit(&ill->ill_lock);
12241 				rw_exit(&ill_g_lock);
12242 				if (fallback_ipif != NULL)
12243 					ipif_refrele(fallback_ipif);
12244 				*ipifp = ipif;
12245 				return (ipif->ipif_net_mask);
12246 			}
12247 		}
12248 		mutex_exit(&ill->ill_lock);
12249 	}
12250 	rw_exit(&ill_g_lock);
12251 
12252 	*ipifp = fallback_ipif;
12253 	return ((fallback_ipif != NULL) ?
12254 	    fallback_ipif->ipif_net_mask : net_mask);
12255 }
12256 
12257 /*
12258  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12259  */
12260 static void
12261 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12262 {
12263 	IOCP	iocp;
12264 	ipft_t	*ipft;
12265 	ipllc_t	*ipllc;
12266 	mblk_t	*mp1;
12267 	cred_t	*cr;
12268 	int	error = 0;
12269 	conn_t	*connp;
12270 
12271 	ip1dbg(("ip_wput_ioctl"));
12272 	iocp = (IOCP)mp->b_rptr;
12273 	mp1 = mp->b_cont;
12274 	if (mp1 == NULL) {
12275 		iocp->ioc_error = EINVAL;
12276 		mp->b_datap->db_type = M_IOCNAK;
12277 		iocp->ioc_count = 0;
12278 		qreply(q, mp);
12279 		return;
12280 	}
12281 
12282 	/*
12283 	 * These IOCTLs provide various control capabilities to
12284 	 * upstream agents such as ULPs and processes.	There
12285 	 * are currently two such IOCTLs implemented.  They
12286 	 * are used by TCP to provide update information for
12287 	 * existing IREs and to forcibly delete an IRE for a
12288 	 * host that is not responding, thereby forcing an
12289 	 * attempt at a new route.
12290 	 */
12291 	iocp->ioc_error = EINVAL;
12292 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12293 		goto done;
12294 
12295 	ipllc = (ipllc_t *)mp1->b_rptr;
12296 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12297 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12298 			break;
12299 	}
12300 	/*
12301 	 * prefer credential from mblk over ioctl;
12302 	 * see ip_sioctl_copyin_setup
12303 	 */
12304 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12305 
12306 	/*
12307 	 * Refhold the conn in case the request gets queued up in some lookup
12308 	 */
12309 	ASSERT(CONN_Q(q));
12310 	connp = Q_TO_CONN(q);
12311 	CONN_INC_REF(connp);
12312 	if (ipft->ipft_pfi &&
12313 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12314 		pullupmsg(mp1, ipft->ipft_min_size))) {
12315 		error = (*ipft->ipft_pfi)(q,
12316 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12317 	}
12318 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12319 		/*
12320 		 * CONN_OPER_PENDING_DONE happens in the function called
12321 		 * through ipft_pfi above.
12322 		 */
12323 		return;
12324 	}
12325 
12326 	CONN_OPER_PENDING_DONE(connp);
12327 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12328 		freemsg(mp);
12329 		return;
12330 	}
12331 	iocp->ioc_error = error;
12332 
12333 done:
12334 	mp->b_datap->db_type = M_IOCACK;
12335 	if (iocp->ioc_error)
12336 		iocp->ioc_count = 0;
12337 	qreply(q, mp);
12338 }
12339 
12340 /*
12341  * Lookup an ipif using the sequence id (ipif_seqid)
12342  */
12343 ipif_t *
12344 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12345 {
12346 	ipif_t *ipif;
12347 
12348 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12349 
12350 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12351 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12352 			return (ipif);
12353 	}
12354 	return (NULL);
12355 }
12356 
12357 uint64_t ipif_g_seqid;
12358 
12359 /*
12360  * Assign a unique id for the ipif. This is used later when we send
12361  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12362  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12363  * IRE is added, we verify that ipif has not disappeared.
12364  */
12365 
12366 static void
12367 ipif_assign_seqid(ipif_t *ipif)
12368 {
12369 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12370 }
12371 
12372 /*
12373  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12374  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12375  * be inserted into the first space available in the list. The value of
12376  * ipif_id will then be set to the appropriate value for its position.
12377  */
12378 static int
12379 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12380 {
12381 	ill_t *ill;
12382 	ipif_t *tipif;
12383 	ipif_t **tipifp;
12384 	int id;
12385 
12386 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12387 	    IAM_WRITER_IPIF(ipif));
12388 
12389 	ill = ipif->ipif_ill;
12390 	ASSERT(ill != NULL);
12391 
12392 	/*
12393 	 * In the case of lo0:0 we already hold the ill_g_lock.
12394 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12395 	 * ipif_insert. Another such caller is ipif_move.
12396 	 */
12397 	if (acquire_g_lock)
12398 		rw_enter(&ill_g_lock, RW_WRITER);
12399 	if (acquire_ill_lock)
12400 		mutex_enter(&ill->ill_lock);
12401 	id = ipif->ipif_id;
12402 	tipifp = &(ill->ill_ipif);
12403 	if (id == -1) {	/* need to find a real id */
12404 		id = 0;
12405 		while ((tipif = *tipifp) != NULL) {
12406 			ASSERT(tipif->ipif_id >= id);
12407 			if (tipif->ipif_id != id)
12408 				break; /* non-consecutive id */
12409 			id++;
12410 			tipifp = &(tipif->ipif_next);
12411 		}
12412 		/* limit number of logical interfaces */
12413 		if (id >= ip_addrs_per_if) {
12414 			if (acquire_ill_lock)
12415 				mutex_exit(&ill->ill_lock);
12416 			if (acquire_g_lock)
12417 				rw_exit(&ill_g_lock);
12418 			return (-1);
12419 		}
12420 		ipif->ipif_id = id; /* assign new id */
12421 	} else if (id < ip_addrs_per_if) {
12422 		/* we have a real id; insert ipif in the right place */
12423 		while ((tipif = *tipifp) != NULL) {
12424 			ASSERT(tipif->ipif_id != id);
12425 			if (tipif->ipif_id > id)
12426 				break; /* found correct location */
12427 			tipifp = &(tipif->ipif_next);
12428 		}
12429 	} else {
12430 		if (acquire_ill_lock)
12431 			mutex_exit(&ill->ill_lock);
12432 		if (acquire_g_lock)
12433 			rw_exit(&ill_g_lock);
12434 		return (-1);
12435 	}
12436 
12437 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12438 
12439 	ipif->ipif_next = tipif;
12440 	*tipifp = ipif;
12441 	if (acquire_ill_lock)
12442 		mutex_exit(&ill->ill_lock);
12443 	if (acquire_g_lock)
12444 		rw_exit(&ill_g_lock);
12445 	return (0);
12446 }
12447 
12448 /*
12449  * Allocate and initialize a new interface control structure.  (Always
12450  * called as writer.)
12451  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12452  * is not part of the global linked list of ills. ipif_seqid is unique
12453  * in the system and to preserve the uniqueness, it is assigned only
12454  * when ill becomes part of the global list. At that point ill will
12455  * have a name. If it doesn't get assigned here, it will get assigned
12456  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12457  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12458  * the interface flags or any other information from the DL_INFO_ACK for
12459  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12460  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12461  * second DL_INFO_ACK comes in from the driver.
12462  */
12463 static ipif_t *
12464 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12465 {
12466 	ipif_t	*ipif;
12467 	phyint_t *phyi;
12468 
12469 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12470 	    ill->ill_name, id, (void *)ill));
12471 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12472 
12473 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12474 		return (NULL);
12475 	*ipif = ipif_zero;	/* start clean */
12476 
12477 	ipif->ipif_ill = ill;
12478 	ipif->ipif_id = id;	/* could be -1 */
12479 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12480 
12481 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12482 
12483 	ipif->ipif_refcnt = 0;
12484 	ipif->ipif_saved_ire_cnt = 0;
12485 
12486 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12487 		mi_free(ipif);
12488 		return (NULL);
12489 	}
12490 	/* -1 id should have been replaced by real id */
12491 	id = ipif->ipif_id;
12492 	ASSERT(id >= 0);
12493 
12494 	if (ill->ill_name[0] != '\0') {
12495 		ipif_assign_seqid(ipif);
12496 		if (ill->ill_phyint->phyint_ifindex != 0)
12497 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12498 	}
12499 	/*
12500 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12501 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12502 	 * ioctl sets ipif_orig_ipifid to zero.
12503 	 */
12504 	ipif->ipif_orig_ipifid = id;
12505 
12506 	/*
12507 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12508 	 * The ipif is still not up and can't be looked up until the
12509 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12510 	 */
12511 	mutex_enter(&ill->ill_lock);
12512 	mutex_enter(&ill->ill_phyint->phyint_lock);
12513 	/*
12514 	 * Set the running flag when logical interface zero is created.
12515 	 * For subsequent logical interfaces, a DLPI link down
12516 	 * notification message may have cleared the running flag to
12517 	 * indicate the link is down, so we shouldn't just blindly set it.
12518 	 */
12519 	if (id == 0)
12520 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12521 	ipif->ipif_ire_type = ire_type;
12522 	phyi = ill->ill_phyint;
12523 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12524 
12525 	if (ipif->ipif_isv6) {
12526 		ill->ill_flags |= ILLF_IPV6;
12527 	} else {
12528 		ipaddr_t inaddr_any = INADDR_ANY;
12529 
12530 		ill->ill_flags |= ILLF_IPV4;
12531 
12532 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12533 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12534 		    &ipif->ipif_v6lcl_addr);
12535 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12536 		    &ipif->ipif_v6src_addr);
12537 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12538 		    &ipif->ipif_v6subnet);
12539 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12540 		    &ipif->ipif_v6net_mask);
12541 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12542 		    &ipif->ipif_v6brd_addr);
12543 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12544 		    &ipif->ipif_v6pp_dst_addr);
12545 	}
12546 
12547 	/*
12548 	 * Don't set the interface flags etc. now, will do it in
12549 	 * ip_ll_subnet_defaults.
12550 	 */
12551 	if (!initialize) {
12552 		mutex_exit(&ill->ill_lock);
12553 		mutex_exit(&ill->ill_phyint->phyint_lock);
12554 		return (ipif);
12555 	}
12556 	ipif->ipif_mtu = ill->ill_max_mtu;
12557 
12558 	if (ill->ill_bcast_addr_length != 0) {
12559 		/*
12560 		 * Later detect lack of DLPI driver multicast
12561 		 * capability by catching DL_ENABMULTI errors in
12562 		 * ip_rput_dlpi.
12563 		 */
12564 		ill->ill_flags |= ILLF_MULTICAST;
12565 		if (!ipif->ipif_isv6)
12566 			ipif->ipif_flags |= IPIF_BROADCAST;
12567 	} else {
12568 		if (ill->ill_net_type != IRE_LOOPBACK) {
12569 			if (ipif->ipif_isv6)
12570 				/*
12571 				 * Note: xresolv interfaces will eventually need
12572 				 * NOARP set here as well, but that will require
12573 				 * those external resolvers to have some
12574 				 * knowledge of that flag and act appropriately.
12575 				 * Not to be changed at present.
12576 				 */
12577 				ill->ill_flags |= ILLF_NONUD;
12578 			else
12579 				ill->ill_flags |= ILLF_NOARP;
12580 		}
12581 		if (ill->ill_phys_addr_length == 0) {
12582 			if (ill->ill_media &&
12583 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12584 				ipif->ipif_flags |= IPIF_NOXMIT;
12585 				phyi->phyint_flags |= PHYI_VIRTUAL;
12586 			} else {
12587 				/* pt-pt supports multicast. */
12588 				ill->ill_flags |= ILLF_MULTICAST;
12589 				if (ill->ill_net_type == IRE_LOOPBACK) {
12590 					phyi->phyint_flags |=
12591 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12592 				} else {
12593 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12594 				}
12595 			}
12596 		}
12597 	}
12598 	mutex_exit(&ill->ill_lock);
12599 	mutex_exit(&ill->ill_phyint->phyint_lock);
12600 	return (ipif);
12601 }
12602 
12603 /*
12604  * If appropriate, send a message up to the resolver delete the entry
12605  * for the address of this interface which is going out of business.
12606  * (Always called as writer).
12607  *
12608  * NOTE : We need to check for NULL mps as some of the fields are
12609  *	  initialized only for some interface types. See ipif_resolver_up()
12610  *	  for details.
12611  */
12612 void
12613 ipif_arp_down(ipif_t *ipif)
12614 {
12615 	mblk_t	*mp;
12616 
12617 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12618 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12619 	ASSERT(IAM_WRITER_IPIF(ipif));
12620 
12621 	/* Delete the mapping for the local address */
12622 	mp = ipif->ipif_arp_del_mp;
12623 	if (mp != NULL) {
12624 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12625 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12626 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12627 		putnext(ipif->ipif_ill->ill_rq, mp);
12628 		ipif->ipif_arp_del_mp = NULL;
12629 	}
12630 
12631 	/*
12632 	 * If this is the last ipif that is going down, we need
12633 	 * to clean up ARP completely.
12634 	 */
12635 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12636 
12637 		/* Send up AR_INTERFACE_DOWN message */
12638 		mp = ipif->ipif_ill->ill_arp_down_mp;
12639 		if (mp != NULL) {
12640 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12641 			    dlpi_prim_str(*(int *)mp->b_rptr),
12642 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12643 			    ipif->ipif_id));
12644 			putnext(ipif->ipif_ill->ill_rq, mp);
12645 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12646 		}
12647 
12648 		/* Tell ARP to delete the multicast mappings */
12649 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12650 		if (mp != NULL) {
12651 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12652 			    dlpi_prim_str(*(int *)mp->b_rptr),
12653 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12654 			    ipif->ipif_id));
12655 			putnext(ipif->ipif_ill->ill_rq, mp);
12656 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12657 		}
12658 	}
12659 }
12660 
12661 /*
12662  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12663  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12664  * that it wants the add_mp allocated in this function to be returned
12665  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12666  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12667  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12668  * as it does a ipif_arp_down after calling this function - which will
12669  * remove what we add here.
12670  *
12671  * Returns -1 on failures and 0 on success.
12672  */
12673 int
12674 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12675 {
12676 	mblk_t	*del_mp = NULL;
12677 	mblk_t *add_mp = NULL;
12678 	mblk_t *mp;
12679 	ill_t	*ill = ipif->ipif_ill;
12680 	phyint_t *phyi = ill->ill_phyint;
12681 	ipaddr_t addr, mask, extract_mask = 0;
12682 	arma_t	*arma;
12683 	uint8_t *maddr, *bphys_addr;
12684 	uint32_t hw_start;
12685 	dl_unitdata_req_t *dlur;
12686 
12687 	ASSERT(IAM_WRITER_IPIF(ipif));
12688 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12689 		return (0);
12690 
12691 	/*
12692 	 * Delete the existing mapping from ARP. Normally ipif_down
12693 	 * -> ipif_arp_down should send this up to ARP. The only
12694 	 * reason we would find this when we are switching from
12695 	 * Multicast to Broadcast where we did not do a down.
12696 	 */
12697 	mp = ill->ill_arp_del_mapping_mp;
12698 	if (mp != NULL) {
12699 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12700 		    dlpi_prim_str(*(int *)mp->b_rptr),
12701 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12702 		putnext(ill->ill_rq, mp);
12703 		ill->ill_arp_del_mapping_mp = NULL;
12704 	}
12705 
12706 	if (arp_add_mapping_mp != NULL)
12707 		*arp_add_mapping_mp = NULL;
12708 
12709 	/*
12710 	 * Check that the address is not to long for the constant
12711 	 * length reserved in the template arma_t.
12712 	 */
12713 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12714 		return (-1);
12715 
12716 	/* Add mapping mblk */
12717 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
12718 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
12719 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
12720 	    (caddr_t)&addr);
12721 	if (add_mp == NULL)
12722 		return (-1);
12723 	arma = (arma_t *)add_mp->b_rptr;
12724 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
12725 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
12726 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
12727 
12728 	/*
12729 	 * Determine the broadcast address.
12730 	 */
12731 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
12732 	if (ill->ill_sap_length < 0)
12733 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
12734 	else
12735 		bphys_addr = (uchar_t *)dlur +
12736 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
12737 	/*
12738 	 * Check PHYI_MULTI_BCAST and length of physical
12739 	 * address to determine if we use the mapping or the
12740 	 * broadcast address.
12741 	 */
12742 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
12743 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
12744 		    bphys_addr, maddr, &hw_start, &extract_mask))
12745 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
12746 
12747 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
12748 	    (ill->ill_flags & ILLF_MULTICAST)) {
12749 		/* Make sure this will not match the "exact" entry. */
12750 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
12751 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
12752 		    (caddr_t)&addr);
12753 		if (del_mp == NULL) {
12754 			freemsg(add_mp);
12755 			return (-1);
12756 		}
12757 		bcopy(&extract_mask, (char *)arma +
12758 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
12759 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
12760 			/* Use link-layer broadcast address for MULTI_BCAST */
12761 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
12762 			ip2dbg(("ipif_arp_setup_multicast: adding"
12763 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
12764 		} else {
12765 			arma->arma_hw_mapping_start = hw_start;
12766 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
12767 			    " ARP setup for %s\n", ill->ill_name));
12768 		}
12769 	} else {
12770 		freemsg(add_mp);
12771 		ASSERT(del_mp == NULL);
12772 		/* It is neither MULTICAST nor MULTI_BCAST */
12773 		return (0);
12774 	}
12775 	ASSERT(add_mp != NULL && del_mp != NULL);
12776 	ill->ill_arp_del_mapping_mp = del_mp;
12777 	if (arp_add_mapping_mp != NULL) {
12778 		/* The caller just wants the mblks allocated */
12779 		*arp_add_mapping_mp = add_mp;
12780 	} else {
12781 		/* The caller wants us to send it to arp */
12782 		putnext(ill->ill_rq, add_mp);
12783 	}
12784 	return (0);
12785 }
12786 
12787 /*
12788  * Get the resolver set up for a new interface address.
12789  * (Always called as writer.)
12790  * Called both for IPv4 and IPv6 interfaces,
12791  * though it only sets up the resolver for v6
12792  * if it's an xresolv interface (one using an external resolver).
12793  * Honors ILLF_NOARP.
12794  * The boolean value arp_just_publish, if B_TRUE, indicates that
12795  * it only needs to send an AR_ENTRY_ADD message up to ARP for
12796  * IPv4 interfaces. Currently, B_TRUE is only set when this
12797  * function is called by ip_rput_dlpi_writer() to handle
12798  * asynchronous hardware address change notification.
12799  * Returns error on failure.
12800  */
12801 int
12802 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
12803 {
12804 	caddr_t	addr;
12805 	mblk_t	*arp_up_mp = NULL;
12806 	mblk_t	*arp_down_mp = NULL;
12807 	mblk_t	*arp_add_mp = NULL;
12808 	mblk_t	*arp_del_mp = NULL;
12809 	mblk_t	*arp_add_mapping_mp = NULL;
12810 	mblk_t	*arp_del_mapping_mp = NULL;
12811 	ill_t	*ill = ipif->ipif_ill;
12812 	uchar_t	*area_p = NULL;
12813 	uchar_t	*ared_p = NULL;
12814 	int	err = ENOMEM;
12815 
12816 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12817 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
12818 	    (uint_t)ipif->ipif_flags));
12819 	ASSERT(IAM_WRITER_IPIF(ipif));
12820 
12821 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
12822 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
12823 		return (0);
12824 	}
12825 
12826 	if (ill->ill_isv6) {
12827 		/*
12828 		 * External resolver for IPv6
12829 		 */
12830 		ASSERT(!arp_just_publish);
12831 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12832 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
12833 			area_p = (uchar_t *)&ip6_area_template;
12834 			ared_p = (uchar_t *)&ip6_ared_template;
12835 		}
12836 	} else {
12837 		/*
12838 		 * IPv4 arp case. If the ARP stream has already started
12839 		 * closing, fail this request for ARP bringup. Else
12840 		 * record the fact that an ARP bringup is pending.
12841 		 */
12842 		mutex_enter(&ill->ill_lock);
12843 		if (ill->ill_arp_closing) {
12844 			mutex_exit(&ill->ill_lock);
12845 			err = EINVAL;
12846 			goto failed;
12847 		} else {
12848 			if (ill->ill_ipif_up_count == 0)
12849 				ill->ill_arp_bringup_pending = 1;
12850 			mutex_exit(&ill->ill_lock);
12851 		}
12852 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
12853 			addr = (caddr_t)&ipif->ipif_lcl_addr;
12854 			area_p = (uchar_t *)&ip_area_template;
12855 			ared_p = (uchar_t *)&ip_ared_template;
12856 		}
12857 	}
12858 
12859 	/*
12860 	 * Add an entry for the local address in ARP only if it
12861 	 * is not UNNUMBERED and the address is not INADDR_ANY.
12862 	 */
12863 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
12864 		/* Now ask ARP to publish our address. */
12865 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
12866 		if (arp_add_mp == NULL)
12867 			goto failed;
12868 		if (arp_just_publish) {
12869 			/*
12870 			 * Copy the new hardware address and length into
12871 			 * arp_add_mp to be sent to ARP.
12872 			 */
12873 			area_t *area = (area_t *)arp_add_mp->b_rptr;
12874 			area->area_hw_addr_length =
12875 			    ill->ill_phys_addr_length;
12876 			bcopy((char *)ill->ill_phys_addr,
12877 			    ((char *)area + area->area_hw_addr_offset),
12878 			    area->area_hw_addr_length);
12879 		}
12880 
12881 		((area_t *)arp_add_mp->b_rptr)->area_flags =
12882 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
12883 
12884 		if (arp_just_publish)
12885 			goto arp_setup_multicast;
12886 
12887 		/*
12888 		 * Allocate an ARP deletion message so we know we can tell ARP
12889 		 * when the interface goes down.
12890 		 */
12891 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
12892 		if (arp_del_mp == NULL)
12893 			goto failed;
12894 
12895 	} else {
12896 		if (arp_just_publish)
12897 			goto done;
12898 	}
12899 	/*
12900 	 * Need to bring up ARP or setup multicast mapping only
12901 	 * when the first interface is coming UP.
12902 	 */
12903 	if (ill->ill_ipif_up_count != 0)
12904 		goto done;
12905 
12906 	/*
12907 	 * Allocate an ARP down message (to be saved) and an ARP up
12908 	 * message.
12909 	 */
12910 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
12911 	if (arp_down_mp == NULL)
12912 		goto failed;
12913 
12914 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
12915 	if (arp_up_mp == NULL)
12916 		goto failed;
12917 
12918 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12919 		goto done;
12920 
12921 arp_setup_multicast:
12922 	/*
12923 	 * Setup the multicast mappings. This function initializes
12924 	 * ill_arp_del_mapping_mp also. This does not need to be done for
12925 	 * IPv6.
12926 	 */
12927 	if (!ill->ill_isv6) {
12928 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
12929 		if (err != 0)
12930 			goto failed;
12931 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
12932 		ASSERT(arp_add_mapping_mp != NULL);
12933 	}
12934 
12935 done:;
12936 	if (arp_del_mp != NULL) {
12937 		ASSERT(ipif->ipif_arp_del_mp == NULL);
12938 		ipif->ipif_arp_del_mp = arp_del_mp;
12939 	}
12940 	if (arp_down_mp != NULL) {
12941 		ASSERT(ill->ill_arp_down_mp == NULL);
12942 		ill->ill_arp_down_mp = arp_down_mp;
12943 	}
12944 	if (arp_del_mapping_mp != NULL) {
12945 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
12946 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
12947 	}
12948 	if (arp_up_mp != NULL) {
12949 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
12950 			ipif->ipif_ill->ill_name, ipif->ipif_id));
12951 		putnext(ill->ill_rq, arp_up_mp);
12952 	}
12953 	if (arp_add_mp != NULL) {
12954 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
12955 			ipif->ipif_ill->ill_name, ipif->ipif_id));
12956 		putnext(ill->ill_rq, arp_add_mp);
12957 	}
12958 	if (arp_add_mapping_mp != NULL) {
12959 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
12960 			ipif->ipif_ill->ill_name, ipif->ipif_id));
12961 		putnext(ill->ill_rq, arp_add_mapping_mp);
12962 	}
12963 	if (arp_just_publish)
12964 		return (0);
12965 
12966 	if (ill->ill_flags & ILLF_NOARP)
12967 		err = ill_arp_off(ill);
12968 	else
12969 		err = ill_arp_on(ill);
12970 	if (err) {
12971 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
12972 		freemsg(ipif->ipif_arp_del_mp);
12973 		if (arp_down_mp != NULL)
12974 			freemsg(ill->ill_arp_down_mp);
12975 		if (ill->ill_arp_del_mapping_mp != NULL)
12976 			freemsg(ill->ill_arp_del_mapping_mp);
12977 		ipif->ipif_arp_del_mp = NULL;
12978 		ill->ill_arp_down_mp = NULL;
12979 		ill->ill_arp_del_mapping_mp = NULL;
12980 		return (err);
12981 	}
12982 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
12983 
12984 failed:;
12985 	ip1dbg(("ipif_resolver_up: FAILED\n"));
12986 	freemsg(arp_add_mp);
12987 	freemsg(arp_del_mp);
12988 	freemsg(arp_add_mapping_mp);
12989 	freemsg(arp_up_mp);
12990 	freemsg(arp_down_mp);
12991 	ill->ill_arp_bringup_pending = 0;
12992 	return (err);
12993 }
12994 
12995 /*
12996  * Wakeup all threads waiting to enter the ipsq, and sleeping
12997  * on any of the ills in this ipsq. The ill_lock of the ill
12998  * must be held so that waiters don't miss wakeups
12999  */
13000 static void
13001 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13002 {
13003 	phyint_t *phyint;
13004 
13005 	phyint = ipsq->ipsq_phyint_list;
13006 	while (phyint != NULL) {
13007 		if (phyint->phyint_illv4) {
13008 			if (!caller_holds_lock)
13009 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13010 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13011 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13012 			if (!caller_holds_lock)
13013 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13014 		}
13015 		if (phyint->phyint_illv6) {
13016 			if (!caller_holds_lock)
13017 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13018 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13019 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13020 			if (!caller_holds_lock)
13021 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13022 		}
13023 		phyint = phyint->phyint_ipsq_next;
13024 	}
13025 }
13026 
13027 static ipsq_t *
13028 ipsq_create(char *groupname)
13029 {
13030 	ipsq_t	*ipsq;
13031 
13032 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13033 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13034 	if (ipsq == NULL) {
13035 		return (NULL);
13036 	}
13037 
13038 	if (groupname != NULL)
13039 		(void) strcpy(ipsq->ipsq_name, groupname);
13040 	else
13041 		ipsq->ipsq_name[0] = '\0';
13042 
13043 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13044 	ipsq->ipsq_flags |= IPSQ_GROUP;
13045 	ipsq->ipsq_next = ipsq_g_head;
13046 	ipsq_g_head = ipsq;
13047 	return (ipsq);
13048 }
13049 
13050 /*
13051  * Return an ipsq correspoding to the groupname. If 'create' is true
13052  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13053  * uniquely with an IPMP group. However during IPMP groupname operations,
13054  * multiple IPMP groups may be associated with a single ipsq. But no
13055  * IPMP group can be associated with more than 1 ipsq at any time.
13056  * For example
13057  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13058  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13059  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13060  *
13061  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13062  * status shown below during the execution of the above command.
13063  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13064  *
13065  * After the completion of the above groupname command we return to the stable
13066  * state shown below.
13067  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13068  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13069  *
13070  * Because of the above, we don't search based on the ipsq_name since that
13071  * would miss the correct ipsq during certain windows as shown above.
13072  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13073  * natural state.
13074  */
13075 static ipsq_t *
13076 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13077 {
13078 	ipsq_t	*ipsq;
13079 	int	group_len;
13080 	phyint_t *phyint;
13081 
13082 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13083 
13084 	group_len = strlen(groupname);
13085 	ASSERT(group_len != 0);
13086 	group_len++;
13087 
13088 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13089 		/*
13090 		 * When an ipsq is being split, and ill_split_ipsq
13091 		 * calls this function, we exclude it from being considered.
13092 		 */
13093 		if (ipsq == exclude_ipsq)
13094 			continue;
13095 
13096 		/*
13097 		 * Compare against the ipsq_name. The groupname change happens
13098 		 * in 2 phases. The 1st phase merges the from group into
13099 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13100 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13101 		 * ipsq_name. At this point the phyint_groupname has not been
13102 		 * updated.
13103 		 */
13104 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13105 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13106 			/*
13107 			 * Verify that an ipmp groupname is exactly
13108 			 * part of 1 ipsq and is not found in any other
13109 			 * ipsq.
13110 			 */
13111 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13112 			    NULL);
13113 			return (ipsq);
13114 		}
13115 
13116 		/*
13117 		 * Comparison against ipsq_name alone is not sufficient.
13118 		 * In the case when groups are currently being
13119 		 * merged, the ipsq could hold other IPMP groups temporarily.
13120 		 * so we walk the phyint list and compare against the
13121 		 * phyint_groupname as well.
13122 		 */
13123 		phyint = ipsq->ipsq_phyint_list;
13124 		while (phyint != NULL) {
13125 			if ((group_len == phyint->phyint_groupname_len) &&
13126 			    (bcmp(phyint->phyint_groupname, groupname,
13127 			    group_len) == 0)) {
13128 				/*
13129 				 * Verify that an ipmp groupname is exactly
13130 				 * part of 1 ipsq and is not found in any other
13131 				 * ipsq.
13132 				 */
13133 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13134 					== NULL);
13135 				return (ipsq);
13136 			}
13137 			phyint = phyint->phyint_ipsq_next;
13138 		}
13139 	}
13140 	if (create)
13141 		ipsq = ipsq_create(groupname);
13142 	return (ipsq);
13143 }
13144 
13145 static void
13146 ipsq_delete(ipsq_t *ipsq)
13147 {
13148 	ipsq_t *nipsq;
13149 	ipsq_t *pipsq = NULL;
13150 
13151 	/*
13152 	 * We don't hold the ipsq lock, but we are sure no new
13153 	 * messages can land up, since the ipsq_refs is zero.
13154 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13155 	 * is associated with this ipsq. (Lookups are based on ill_name
13156 	 * or phyint_group_name)
13157 	 */
13158 	ASSERT(ipsq->ipsq_refs == 0);
13159 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13160 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13161 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13162 		/*
13163 		 * This is not the ipsq of an IPMP group.
13164 		 */
13165 		kmem_free(ipsq, sizeof (ipsq_t));
13166 		return;
13167 	}
13168 
13169 	rw_enter(&ill_g_lock, RW_WRITER);
13170 
13171 	/*
13172 	 * Locate the ipsq  before we can remove it from
13173 	 * the singly linked list of ipsq's.
13174 	 */
13175 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13176 		if (nipsq == ipsq) {
13177 			break;
13178 		}
13179 		pipsq = nipsq;
13180 	}
13181 
13182 	ASSERT(nipsq == ipsq);
13183 
13184 	/* unlink ipsq from the list */
13185 	if (pipsq != NULL)
13186 		pipsq->ipsq_next = ipsq->ipsq_next;
13187 	else
13188 		ipsq_g_head = ipsq->ipsq_next;
13189 	kmem_free(ipsq, sizeof (ipsq_t));
13190 	rw_exit(&ill_g_lock);
13191 }
13192 
13193 static void
13194 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13195     queue_t *q)
13196 
13197 {
13198 
13199 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13200 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13201 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13202 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13203 	ASSERT(current_mp != NULL);
13204 
13205 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13206 		NEW_OP, NULL);
13207 
13208 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13209 	    new_ipsq->ipsq_xopq_mphead != NULL);
13210 
13211 	/*
13212 	 * move from old ipsq to the new ipsq.
13213 	 */
13214 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13215 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13216 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13217 
13218 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13219 }
13220 
13221 void
13222 ill_group_cleanup(ill_t *ill)
13223 {
13224 	ill_t *ill_v4;
13225 	ill_t *ill_v6;
13226 	ipif_t *ipif;
13227 
13228 	ill_v4 = ill->ill_phyint->phyint_illv4;
13229 	ill_v6 = ill->ill_phyint->phyint_illv6;
13230 
13231 	if (ill_v4 != NULL) {
13232 		mutex_enter(&ill_v4->ill_lock);
13233 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13234 		    ipif = ipif->ipif_next) {
13235 			IPIF_UNMARK_MOVING(ipif);
13236 		}
13237 		ill_v4->ill_up_ipifs = B_FALSE;
13238 		mutex_exit(&ill_v4->ill_lock);
13239 	}
13240 
13241 	if (ill_v6 != NULL) {
13242 		mutex_enter(&ill_v6->ill_lock);
13243 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13244 		    ipif = ipif->ipif_next) {
13245 			IPIF_UNMARK_MOVING(ipif);
13246 		}
13247 		ill_v6->ill_up_ipifs = B_FALSE;
13248 		mutex_exit(&ill_v6->ill_lock);
13249 	}
13250 }
13251 /*
13252  * This function is called when an ill has had a change in its group status
13253  * to bring up all the ipifs that were up before the change.
13254  */
13255 int
13256 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13257 {
13258 	ipif_t *ipif;
13259 	ill_t *ill_v4;
13260 	ill_t *ill_v6;
13261 	ill_t *from_ill;
13262 	int err = 0;
13263 
13264 
13265 	ASSERT(IAM_WRITER_ILL(ill));
13266 
13267 	/*
13268 	 * Except for ipif_state_flags and ill_state_flags the other
13269 	 * fields of the ipif/ill that are modified below are protected
13270 	 * implicitly since we are a writer. We would have tried to down
13271 	 * even an ipif that was already down, in ill_down_ipifs. So we
13272 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13273 	 */
13274 	ill_v4 = ill->ill_phyint->phyint_illv4;
13275 	ill_v6 = ill->ill_phyint->phyint_illv6;
13276 	if (ill_v4 != NULL) {
13277 		ill_v4->ill_up_ipifs = B_TRUE;
13278 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13279 		    ipif = ipif->ipif_next) {
13280 			mutex_enter(&ill_v4->ill_lock);
13281 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13282 			IPIF_UNMARK_MOVING(ipif);
13283 			mutex_exit(&ill_v4->ill_lock);
13284 			if (ipif->ipif_was_up) {
13285 				if (!(ipif->ipif_flags & IPIF_UP))
13286 					err = ipif_up(ipif, q, mp);
13287 				ipif->ipif_was_up = B_FALSE;
13288 				if (err != 0) {
13289 					/*
13290 					 * Can there be any other error ?
13291 					 */
13292 					ASSERT(err == EINPROGRESS);
13293 					return (err);
13294 				}
13295 			}
13296 		}
13297 		mutex_enter(&ill_v4->ill_lock);
13298 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13299 		mutex_exit(&ill_v4->ill_lock);
13300 		ill_v4->ill_up_ipifs = B_FALSE;
13301 		if (ill_v4->ill_move_in_progress) {
13302 			ASSERT(ill_v4->ill_move_peer != NULL);
13303 			ill_v4->ill_move_in_progress = B_FALSE;
13304 			from_ill = ill_v4->ill_move_peer;
13305 			from_ill->ill_move_in_progress = B_FALSE;
13306 			from_ill->ill_move_peer = NULL;
13307 			mutex_enter(&from_ill->ill_lock);
13308 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13309 			mutex_exit(&from_ill->ill_lock);
13310 			if (ill_v6 == NULL) {
13311 				if (from_ill->ill_phyint->phyint_flags &
13312 				    PHYI_STANDBY) {
13313 					phyint_standby_inactive
13314 					    (from_ill->ill_phyint);
13315 				}
13316 				if (ill_v4->ill_phyint->phyint_flags &
13317 				    PHYI_STANDBY) {
13318 					phyint_standby_inactive
13319 						(ill_v4->ill_phyint);
13320 				}
13321 			}
13322 			ill_v4->ill_move_peer = NULL;
13323 		}
13324 	}
13325 
13326 	if (ill_v6 != NULL) {
13327 		ill_v6->ill_up_ipifs = B_TRUE;
13328 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13329 		    ipif = ipif->ipif_next) {
13330 			mutex_enter(&ill_v6->ill_lock);
13331 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13332 			IPIF_UNMARK_MOVING(ipif);
13333 			mutex_exit(&ill_v6->ill_lock);
13334 			if (ipif->ipif_was_up) {
13335 				if (!(ipif->ipif_flags & IPIF_UP))
13336 					err = ipif_up(ipif, q, mp);
13337 				ipif->ipif_was_up = B_FALSE;
13338 				if (err != 0) {
13339 					/*
13340 					 * Can there be any other error ?
13341 					 */
13342 					ASSERT(err == EINPROGRESS);
13343 					return (err);
13344 				}
13345 			}
13346 		}
13347 		mutex_enter(&ill_v6->ill_lock);
13348 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13349 		mutex_exit(&ill_v6->ill_lock);
13350 		ill_v6->ill_up_ipifs = B_FALSE;
13351 		if (ill_v6->ill_move_in_progress) {
13352 			ASSERT(ill_v6->ill_move_peer != NULL);
13353 			ill_v6->ill_move_in_progress = B_FALSE;
13354 			from_ill = ill_v6->ill_move_peer;
13355 			from_ill->ill_move_in_progress = B_FALSE;
13356 			from_ill->ill_move_peer = NULL;
13357 			mutex_enter(&from_ill->ill_lock);
13358 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13359 			mutex_exit(&from_ill->ill_lock);
13360 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13361 				phyint_standby_inactive(from_ill->ill_phyint);
13362 			}
13363 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13364 				phyint_standby_inactive(ill_v6->ill_phyint);
13365 			}
13366 			ill_v6->ill_move_peer = NULL;
13367 		}
13368 	}
13369 	return (0);
13370 }
13371 
13372 /*
13373  * bring down all the approriate ipifs.
13374  */
13375 /* ARGSUSED */
13376 static void
13377 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13378 {
13379 	ipif_t *ipif;
13380 
13381 	ASSERT(IAM_WRITER_ILL(ill));
13382 
13383 	/*
13384 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13385 	 * are modified below are protected implicitly since we are a writer
13386 	 */
13387 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13388 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13389 			continue;
13390 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13391 			/*
13392 			 * We go through the ipif_down logic even if the ipif
13393 			 * is already down, since routes can be added based
13394 			 * on down ipifs. Going through ipif_down once again
13395 			 * will delete any IREs created based on these routes.
13396 			 */
13397 			if (ipif->ipif_flags & IPIF_UP)
13398 				ipif->ipif_was_up = B_TRUE;
13399 			/*
13400 			 * If called with chk_nofailover true ipif is moving.
13401 			 */
13402 			mutex_enter(&ill->ill_lock);
13403 			if (chk_nofailover) {
13404 				ipif->ipif_state_flags |=
13405 					IPIF_MOVING | IPIF_CHANGING;
13406 			} else {
13407 				ipif->ipif_state_flags |= IPIF_CHANGING;
13408 			}
13409 			mutex_exit(&ill->ill_lock);
13410 			/*
13411 			 * Need to re-create net/subnet bcast ires if
13412 			 * they are dependent on ipif.
13413 			 */
13414 			if (!ipif->ipif_isv6)
13415 				ipif_check_bcast_ires(ipif);
13416 			(void) ipif_logical_down(ipif, NULL, NULL);
13417 			ipif_down_tail(ipif);
13418 			/*
13419 			 * We don't do ipif_multicast_down for IPv4 in
13420 			 * ipif_down. We need to set this so that
13421 			 * ipif_multicast_up will join the
13422 			 * ALLHOSTS_GROUP on to_ill.
13423 			 */
13424 			ipif->ipif_multicast_up = B_FALSE;
13425 		}
13426 	}
13427 }
13428 
13429 #define	IPSQ_INC_REF(ipsq)	{			\
13430 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13431 	(ipsq)->ipsq_refs++;				\
13432 }
13433 
13434 #define	IPSQ_DEC_REF(ipsq)	{			\
13435 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13436 	(ipsq)->ipsq_refs--;				\
13437 	if ((ipsq)->ipsq_refs == 0)				\
13438 		(ipsq)->ipsq_name[0] = '\0'; 		\
13439 }
13440 
13441 /*
13442  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13443  * new_ipsq.
13444  */
13445 static void
13446 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13447 {
13448 	phyint_t *phyint;
13449 	phyint_t *next_phyint;
13450 
13451 	/*
13452 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13453 	 * writer and the ill_lock of the ill in question. Also the dest
13454 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13455 	 */
13456 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13457 
13458 	phyint = cur_ipsq->ipsq_phyint_list;
13459 	cur_ipsq->ipsq_phyint_list = NULL;
13460 	while (phyint != NULL) {
13461 		next_phyint = phyint->phyint_ipsq_next;
13462 		IPSQ_DEC_REF(cur_ipsq);
13463 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13464 		new_ipsq->ipsq_phyint_list = phyint;
13465 		IPSQ_INC_REF(new_ipsq);
13466 		phyint->phyint_ipsq = new_ipsq;
13467 		phyint = next_phyint;
13468 	}
13469 }
13470 
13471 #define	SPLIT_SUCCESS		0
13472 #define	SPLIT_NOT_NEEDED	1
13473 #define	SPLIT_FAILED		2
13474 
13475 int
13476 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13477 {
13478 	ipsq_t *newipsq = NULL;
13479 
13480 	/*
13481 	 * Assertions denote pre-requisites for changing the ipsq of
13482 	 * a phyint
13483 	 */
13484 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13485 	/*
13486 	 * <ill-phyint> assocs can't change while ill_g_lock
13487 	 * is held as writer. See ill_phyint_reinit()
13488 	 */
13489 	ASSERT(phyint->phyint_illv4 == NULL ||
13490 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13491 	ASSERT(phyint->phyint_illv6 == NULL ||
13492 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13493 
13494 	if ((phyint->phyint_groupname_len !=
13495 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13496 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13497 	    phyint->phyint_groupname_len) != 0)) {
13498 		/*
13499 		 * Once we fail in creating a new ipsq due to memory shortage,
13500 		 * don't attempt to create new ipsq again, based on another
13501 		 * phyint, since we want all phyints belonging to an IPMP group
13502 		 * to be in the same ipsq even in the event of mem alloc fails.
13503 		 */
13504 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13505 		    cur_ipsq);
13506 		if (newipsq == NULL) {
13507 			/* Memory allocation failure */
13508 			return (SPLIT_FAILED);
13509 		} else {
13510 			/* ipsq_refs protected by ill_g_lock (writer) */
13511 			IPSQ_DEC_REF(cur_ipsq);
13512 			phyint->phyint_ipsq = newipsq;
13513 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13514 			newipsq->ipsq_phyint_list = phyint;
13515 			IPSQ_INC_REF(newipsq);
13516 			return (SPLIT_SUCCESS);
13517 		}
13518 	}
13519 	return (SPLIT_NOT_NEEDED);
13520 }
13521 
13522 /*
13523  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13524  * to do this split
13525  */
13526 static int
13527 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13528 {
13529 	ipsq_t *newipsq;
13530 
13531 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13532 	/*
13533 	 * <ill-phyint> assocs can't change while ill_g_lock
13534 	 * is held as writer. See ill_phyint_reinit()
13535 	 */
13536 
13537 	ASSERT(phyint->phyint_illv4 == NULL ||
13538 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13539 	ASSERT(phyint->phyint_illv6 == NULL ||
13540 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13541 
13542 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13543 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13544 		/*
13545 		 * ipsq_init failed due to no memory
13546 		 * caller will use the same ipsq
13547 		 */
13548 		return (SPLIT_FAILED);
13549 	}
13550 
13551 	/* ipsq_ref is protected by ill_g_lock (writer) */
13552 	IPSQ_DEC_REF(cur_ipsq);
13553 
13554 	/*
13555 	 * This is a new ipsq that is unknown to the world.
13556 	 * So we don't need to hold ipsq_lock,
13557 	 */
13558 	newipsq = phyint->phyint_ipsq;
13559 	newipsq->ipsq_writer = NULL;
13560 	newipsq->ipsq_reentry_cnt--;
13561 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13562 #ifdef ILL_DEBUG
13563 	newipsq->ipsq_depth = 0;
13564 #endif
13565 
13566 	return (SPLIT_SUCCESS);
13567 }
13568 
13569 /*
13570  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13571  * ipsq's representing their individual groups or themselves. Return
13572  * whether split needs to be retried again later.
13573  */
13574 static boolean_t
13575 ill_split_ipsq(ipsq_t *cur_ipsq)
13576 {
13577 	phyint_t *phyint;
13578 	phyint_t *next_phyint;
13579 	int	error;
13580 	boolean_t need_retry = B_FALSE;
13581 
13582 	phyint = cur_ipsq->ipsq_phyint_list;
13583 	cur_ipsq->ipsq_phyint_list = NULL;
13584 	while (phyint != NULL) {
13585 		next_phyint = phyint->phyint_ipsq_next;
13586 		/*
13587 		 * 'created' will tell us whether the callee actually
13588 		 * created an ipsq. Lack of memory may force the callee
13589 		 * to return without creating an ipsq.
13590 		 */
13591 		if (phyint->phyint_groupname == NULL) {
13592 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13593 		} else {
13594 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13595 					need_retry);
13596 		}
13597 
13598 		switch (error) {
13599 		case SPLIT_FAILED:
13600 			need_retry = B_TRUE;
13601 			/* FALLTHRU */
13602 		case SPLIT_NOT_NEEDED:
13603 			/*
13604 			 * Keep it on the list.
13605 			 */
13606 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13607 			cur_ipsq->ipsq_phyint_list = phyint;
13608 			break;
13609 		case SPLIT_SUCCESS:
13610 			break;
13611 		default:
13612 			ASSERT(0);
13613 		}
13614 
13615 		phyint = next_phyint;
13616 	}
13617 	return (need_retry);
13618 }
13619 
13620 /*
13621  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13622  * and return the ills in the list. This list will be
13623  * needed to unlock all the ills later on by the caller.
13624  * The <ill-ipsq> associations could change between the
13625  * lock and unlock. Hence the unlock can't traverse the
13626  * ipsq to get the list of ills.
13627  */
13628 static int
13629 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13630 {
13631 	int	cnt = 0;
13632 	phyint_t	*phyint;
13633 
13634 	/*
13635 	 * The caller holds ill_g_lock to ensure that the ill memberships
13636 	 * of the ipsq don't change
13637 	 */
13638 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13639 
13640 	phyint = ipsq->ipsq_phyint_list;
13641 	while (phyint != NULL) {
13642 		if (phyint->phyint_illv4 != NULL) {
13643 			ASSERT(cnt < list_max);
13644 			list[cnt++] = phyint->phyint_illv4;
13645 		}
13646 		if (phyint->phyint_illv6 != NULL) {
13647 			ASSERT(cnt < list_max);
13648 			list[cnt++] = phyint->phyint_illv6;
13649 		}
13650 		phyint = phyint->phyint_ipsq_next;
13651 	}
13652 	ill_lock_ills(list, cnt);
13653 	return (cnt);
13654 }
13655 
13656 void
13657 ill_lock_ills(ill_t **list, int cnt)
13658 {
13659 	int	i;
13660 
13661 	if (cnt > 1) {
13662 		boolean_t try_again;
13663 		do {
13664 			try_again = B_FALSE;
13665 			for (i = 0; i < cnt - 1; i++) {
13666 				if (list[i] < list[i + 1]) {
13667 					ill_t	*tmp;
13668 
13669 					/* swap the elements */
13670 					tmp = list[i];
13671 					list[i] = list[i + 1];
13672 					list[i + 1] = tmp;
13673 					try_again = B_TRUE;
13674 				}
13675 			}
13676 		} while (try_again);
13677 	}
13678 
13679 	for (i = 0; i < cnt; i++) {
13680 		if (i == 0) {
13681 			if (list[i] != NULL)
13682 				mutex_enter(&list[i]->ill_lock);
13683 			else
13684 				return;
13685 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13686 			mutex_enter(&list[i]->ill_lock);
13687 		}
13688 	}
13689 }
13690 
13691 void
13692 ill_unlock_ills(ill_t **list, int cnt)
13693 {
13694 	int	i;
13695 
13696 	for (i = 0; i < cnt; i++) {
13697 		if ((i == 0) && (list[i] != NULL)) {
13698 			mutex_exit(&list[i]->ill_lock);
13699 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13700 			mutex_exit(&list[i]->ill_lock);
13701 		}
13702 	}
13703 }
13704 
13705 /*
13706  * Merge all the ills from 1 ipsq group into another ipsq group.
13707  * The source ipsq group is specified by the ipsq associated with
13708  * 'from_ill'. The destination ipsq group is specified by the ipsq
13709  * associated with 'to_ill' or 'groupname' respectively.
13710  * Note that ipsq itself does not have a reference count mechanism
13711  * and functions don't look up an ipsq and pass it around. Instead
13712  * functions pass around an ill or groupname, and the ipsq is looked
13713  * up from the ill or groupname and the required operation performed
13714  * atomically with the lookup on the ipsq.
13715  */
13716 static int
13717 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13718     queue_t *q)
13719 {
13720 	ipsq_t *old_ipsq;
13721 	ipsq_t *new_ipsq;
13722 	ill_t	**ill_list;
13723 	int	cnt;
13724 	size_t	ill_list_size;
13725 	boolean_t became_writer_on_new_sq = B_FALSE;
13726 
13727 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
13728 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
13729 
13730 	/*
13731 	 * Need to hold ill_g_lock as writer and also the ill_lock to
13732 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
13733 	 * ipsq_lock to prevent new messages from landing on an ipsq.
13734 	 */
13735 	rw_enter(&ill_g_lock, RW_WRITER);
13736 
13737 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
13738 	if (groupname != NULL)
13739 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
13740 	else {
13741 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
13742 	}
13743 
13744 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
13745 
13746 	/*
13747 	 * both groups are on the same ipsq.
13748 	 */
13749 	if (old_ipsq == new_ipsq) {
13750 		rw_exit(&ill_g_lock);
13751 		return (0);
13752 	}
13753 
13754 	cnt = old_ipsq->ipsq_refs << 1;
13755 	ill_list_size = cnt * sizeof (ill_t *);
13756 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
13757 	if (ill_list == NULL) {
13758 		rw_exit(&ill_g_lock);
13759 		return (ENOMEM);
13760 	}
13761 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
13762 
13763 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
13764 	mutex_enter(&new_ipsq->ipsq_lock);
13765 	if ((new_ipsq->ipsq_writer == NULL &&
13766 		new_ipsq->ipsq_current_ipif == NULL) ||
13767 	    (new_ipsq->ipsq_writer == curthread)) {
13768 		new_ipsq->ipsq_writer = curthread;
13769 		new_ipsq->ipsq_reentry_cnt++;
13770 		became_writer_on_new_sq = B_TRUE;
13771 	}
13772 
13773 	/*
13774 	 * We are holding ill_g_lock as writer and all the ill locks of
13775 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
13776 	 * message can land up on the old ipsq even though we don't hold the
13777 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
13778 	 */
13779 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
13780 
13781 	/*
13782 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
13783 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
13784 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
13785 	 */
13786 	ill_merge_ipsq(old_ipsq, new_ipsq);
13787 
13788 	/*
13789 	 * Mark the new ipsq as needing a split since it is currently
13790 	 * being shared by more than 1 IPMP group. The split will
13791 	 * occur at the end of ipsq_exit
13792 	 */
13793 	new_ipsq->ipsq_split = B_TRUE;
13794 
13795 	/* Now release all the locks */
13796 	mutex_exit(&new_ipsq->ipsq_lock);
13797 	ill_unlock_ills(ill_list, cnt);
13798 	rw_exit(&ill_g_lock);
13799 
13800 	kmem_free(ill_list, ill_list_size);
13801 
13802 	/*
13803 	 * If we succeeded in becoming writer on the new ipsq, then
13804 	 * drain the new ipsq and start processing  all enqueued messages
13805 	 * including the current ioctl we are processing which is either
13806 	 * a set groupname or failover/failback.
13807 	 */
13808 	if (became_writer_on_new_sq)
13809 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
13810 
13811 	/*
13812 	 * syncq has been changed and all the messages have been moved.
13813 	 */
13814 	mutex_enter(&old_ipsq->ipsq_lock);
13815 	old_ipsq->ipsq_current_ipif = NULL;
13816 	mutex_exit(&old_ipsq->ipsq_lock);
13817 	return (EINPROGRESS);
13818 }
13819 
13820 /*
13821  * Delete and add the loopback copy and non-loopback copy of
13822  * the BROADCAST ire corresponding to ill and addr. Used to
13823  * group broadcast ires together when ill becomes part of
13824  * a group.
13825  *
13826  * This function is also called when ill is leaving the group
13827  * so that the ires belonging to the group gets re-grouped.
13828  */
13829 static void
13830 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
13831 {
13832 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
13833 	ire_t **ire_ptpn = &ire_head;
13834 
13835 	/*
13836 	 * The loopback and non-loopback IREs are inserted in the order in which
13837 	 * they're found, on the basis that they are correctly ordered (loopback
13838 	 * first).
13839 	 */
13840 	for (;;) {
13841 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
13842 		    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
13843 		if (ire == NULL)
13844 			break;
13845 
13846 		/*
13847 		 * we are passing in KM_SLEEP because it is not easy to
13848 		 * go back to a sane state in case of memory failure.
13849 		 */
13850 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
13851 		ASSERT(nire != NULL);
13852 		bzero(nire, sizeof (ire_t));
13853 		/*
13854 		 * Don't use ire_max_frag directly since we don't
13855 		 * hold on to 'ire' until we add the new ire 'nire' and
13856 		 * we don't want the new ire to have a dangling reference
13857 		 * to 'ire'. The ire_max_frag of a broadcast ire must
13858 		 * be in sync with the ipif_mtu of the associate ipif.
13859 		 * For eg. this happens as a result of SIOCSLIFNAME,
13860 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
13861 		 * the driver. A change in ire_max_frag triggered as
13862 		 * as a result of path mtu discovery, or due to an
13863 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
13864 		 * route change -mtu command does not apply to broadcast ires.
13865 		 *
13866 		 * XXX We need a recovery strategy here if ire_init fails
13867 		 */
13868 		if (ire_init(nire,
13869 		    (uchar_t *)&ire->ire_addr,
13870 		    (uchar_t *)&ire->ire_mask,
13871 		    (uchar_t *)&ire->ire_src_addr,
13872 		    (uchar_t *)&ire->ire_gateway_addr,
13873 		    (uchar_t *)&ire->ire_in_src_addr,
13874 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
13875 			&ire->ire_ipif->ipif_mtu,
13876 		    ire->ire_fp_mp,
13877 		    ire->ire_rfq,
13878 		    ire->ire_stq,
13879 		    ire->ire_type,
13880 		    ire->ire_dlureq_mp,
13881 		    ire->ire_ipif,
13882 		    ire->ire_in_ill,
13883 		    ire->ire_cmask,
13884 		    ire->ire_phandle,
13885 		    ire->ire_ihandle,
13886 		    ire->ire_flags,
13887 		    &ire->ire_uinfo) == NULL) {
13888 			cmn_err(CE_PANIC, "ire_init() failed");
13889 		}
13890 		ire_delete(ire);
13891 		ire_refrele(ire);
13892 
13893 		/*
13894 		 * The newly created IREs are inserted at the tail of the list
13895 		 * starting with ire_head. As we've just allocated them no one
13896 		 * knows about them so it's safe.
13897 		 */
13898 		*ire_ptpn = nire;
13899 		ire_ptpn = &nire->ire_next;
13900 	}
13901 
13902 	for (nire = ire_head; nire != NULL; nire = nire_next) {
13903 		int error;
13904 		ire_t *oire;
13905 		/* unlink the IRE from our list before calling ire_add() */
13906 		nire_next = nire->ire_next;
13907 		nire->ire_next = NULL;
13908 
13909 		/* ire_add adds the ire at the right place in the list */
13910 		oire = nire;
13911 		error = ire_add(&nire, NULL, NULL, NULL);
13912 		ASSERT(error == 0);
13913 		ASSERT(oire == nire);
13914 		ire_refrele(nire);	/* Held in ire_add */
13915 	}
13916 }
13917 
13918 /*
13919  * This function is usually called when an ill is inserted in
13920  * a group and all the ipifs are already UP. As all the ipifs
13921  * are already UP, the broadcast ires have already been created
13922  * and been inserted. But, ire_add_v4 would not have grouped properly.
13923  * We need to re-group for the benefit of ip_wput_ire which
13924  * expects BROADCAST ires to be grouped properly to avoid sending
13925  * more than one copy of the broadcast packet per group.
13926  *
13927  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
13928  *	  because when ipif_up_done ends up calling this, ires have
13929  *        already been added before illgrp_insert i.e before ill_group
13930  *	  has been initialized.
13931  */
13932 static void
13933 ill_group_bcast_for_xmit(ill_t *ill)
13934 {
13935 	ill_group_t *illgrp;
13936 	ipif_t *ipif;
13937 	ipaddr_t addr;
13938 	ipaddr_t net_mask;
13939 	ipaddr_t subnet_netmask;
13940 
13941 	illgrp = ill->ill_group;
13942 
13943 	/*
13944 	 * This function is called even when an ill is deleted from
13945 	 * the group. Hence, illgrp could be null.
13946 	 */
13947 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
13948 		return;
13949 
13950 	/*
13951 	 * Delete all the BROADCAST ires matching this ill and add
13952 	 * them back. This time, ire_add_v4 should take care of
13953 	 * grouping them with others because ill is part of the
13954 	 * group.
13955 	 */
13956 	ill_bcast_delete_and_add(ill, 0);
13957 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
13958 
13959 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13960 
13961 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
13962 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13963 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
13964 		} else {
13965 			net_mask = htonl(IN_CLASSA_NET);
13966 		}
13967 		addr = net_mask & ipif->ipif_subnet;
13968 		ill_bcast_delete_and_add(ill, addr);
13969 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
13970 
13971 		subnet_netmask = ipif->ipif_net_mask;
13972 		addr = ipif->ipif_subnet;
13973 		ill_bcast_delete_and_add(ill, addr);
13974 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
13975 	}
13976 }
13977 
13978 /*
13979  * This function is called from illgrp_delete when ill is being deleted
13980  * from the group.
13981  *
13982  * As ill is not there in the group anymore, any address belonging
13983  * to this ill should be cleared of IRE_MARK_NORECV.
13984  */
13985 static void
13986 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
13987 {
13988 	ire_t *ire;
13989 	irb_t *irb;
13990 
13991 	ASSERT(ill->ill_group == NULL);
13992 
13993 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
13994 	    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
13995 
13996 	if (ire != NULL) {
13997 		/*
13998 		 * IPMP and plumbing operations are serialized on the ipsq, so
13999 		 * no one will insert or delete a broadcast ire under our feet.
14000 		 */
14001 		irb = ire->ire_bucket;
14002 		rw_enter(&irb->irb_lock, RW_READER);
14003 		ire_refrele(ire);
14004 
14005 		for (; ire != NULL; ire = ire->ire_next) {
14006 			if (ire->ire_addr != addr)
14007 				break;
14008 			if (ire_to_ill(ire) != ill)
14009 				continue;
14010 
14011 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14012 			ire->ire_marks &= ~IRE_MARK_NORECV;
14013 		}
14014 		rw_exit(&irb->irb_lock);
14015 	}
14016 }
14017 
14018 /*
14019  * This function must be called only after the broadcast ires
14020  * have been grouped together. For a given address addr, nominate
14021  * only one of the ires whose interface is not FAILED or OFFLINE.
14022  *
14023  * This is also called when an ipif goes down, so that we can nominate
14024  * a different ire with the same address for receiving.
14025  */
14026 static void
14027 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14028 {
14029 	irb_t *irb;
14030 	ire_t *ire;
14031 	ire_t *ire1;
14032 	ire_t *save_ire;
14033 	ire_t **irep = NULL;
14034 	boolean_t first = B_TRUE;
14035 	ire_t *clear_ire = NULL;
14036 	ire_t *start_ire = NULL;
14037 	ire_t	*new_lb_ire;
14038 	ire_t	*new_nlb_ire;
14039 	boolean_t new_lb_ire_used = B_FALSE;
14040 	boolean_t new_nlb_ire_used = B_FALSE;
14041 	uint64_t match_flags;
14042 	uint64_t phyi_flags;
14043 	boolean_t fallback = B_FALSE;
14044 
14045 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14046 	    MATCH_IRE_TYPE);
14047 	/*
14048 	 * We may not be able to find some ires if a previous
14049 	 * ire_create failed. This happens when an ipif goes
14050 	 * down and we are unable to create BROADCAST ires due
14051 	 * to memory failure. Thus, we have to check for NULL
14052 	 * below. This should handle the case for LOOPBACK,
14053 	 * POINTOPOINT and interfaces with some POINTOPOINT
14054 	 * logicals for which there are no BROADCAST ires.
14055 	 */
14056 	if (ire == NULL)
14057 		return;
14058 	/*
14059 	 * Currently IRE_BROADCASTS are deleted when an ipif
14060 	 * goes down which runs exclusively. Thus, setting
14061 	 * IRE_MARK_RCVD should not race with ire_delete marking
14062 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14063 	 * be consistent with other parts of the code that walks
14064 	 * a given bucket.
14065 	 */
14066 	save_ire = ire;
14067 	irb = ire->ire_bucket;
14068 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14069 	if (new_lb_ire == NULL) {
14070 		ire_refrele(ire);
14071 		return;
14072 	}
14073 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14074 	if (new_nlb_ire == NULL) {
14075 		ire_refrele(ire);
14076 		kmem_cache_free(ire_cache, new_lb_ire);
14077 		return;
14078 	}
14079 	IRB_REFHOLD(irb);
14080 	rw_enter(&irb->irb_lock, RW_WRITER);
14081 	/*
14082 	 * Get to the first ire matching the address and the
14083 	 * group. If the address does not match we are done
14084 	 * as we could not find the IRE. If the address matches
14085 	 * we should get to the first one matching the group.
14086 	 */
14087 	while (ire != NULL) {
14088 		if (ire->ire_addr != addr ||
14089 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14090 			break;
14091 		}
14092 		ire = ire->ire_next;
14093 	}
14094 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14095 	start_ire = ire;
14096 redo:
14097 	while (ire != NULL && ire->ire_addr == addr &&
14098 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14099 		/*
14100 		 * The first ire for any address within a group
14101 		 * should always be the one with IRE_MARK_NORECV cleared
14102 		 * so that ip_wput_ire can avoid searching for one.
14103 		 * Note down the insertion point which will be used
14104 		 * later.
14105 		 */
14106 		if (first && (irep == NULL))
14107 			irep = ire->ire_ptpn;
14108 		/*
14109 		 * PHYI_FAILED is set when the interface fails.
14110 		 * This interface might have become good, but the
14111 		 * daemon has not yet detected. We should still
14112 		 * not receive on this. PHYI_OFFLINE should never
14113 		 * be picked as this has been offlined and soon
14114 		 * be removed.
14115 		 */
14116 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14117 		if (phyi_flags & PHYI_OFFLINE) {
14118 			ire->ire_marks |= IRE_MARK_NORECV;
14119 			ire = ire->ire_next;
14120 			continue;
14121 		}
14122 		if (phyi_flags & match_flags) {
14123 			ire->ire_marks |= IRE_MARK_NORECV;
14124 			ire = ire->ire_next;
14125 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14126 			    PHYI_INACTIVE) {
14127 				fallback = B_TRUE;
14128 			}
14129 			continue;
14130 		}
14131 		if (first) {
14132 			/*
14133 			 * We will move this to the front of the list later
14134 			 * on.
14135 			 */
14136 			clear_ire = ire;
14137 			ire->ire_marks &= ~IRE_MARK_NORECV;
14138 		} else {
14139 			ire->ire_marks |= IRE_MARK_NORECV;
14140 		}
14141 		first = B_FALSE;
14142 		ire = ire->ire_next;
14143 	}
14144 	/*
14145 	 * If we never nominated anybody, try nominating at least
14146 	 * an INACTIVE, if we found one. Do it only once though.
14147 	 */
14148 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14149 	    fallback) {
14150 		match_flags = PHYI_FAILED;
14151 		ire = start_ire;
14152 		irep = NULL;
14153 		goto redo;
14154 	}
14155 	ire_refrele(save_ire);
14156 
14157 	/*
14158 	 * irep non-NULL indicates that we entered the while loop
14159 	 * above. If clear_ire is at the insertion point, we don't
14160 	 * have to do anything. clear_ire will be NULL if all the
14161 	 * interfaces are failed.
14162 	 *
14163 	 * We cannot unlink and reinsert the ire at the right place
14164 	 * in the list since there can be other walkers of this bucket.
14165 	 * Instead we delete and recreate the ire
14166 	 */
14167 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14168 		ire_t *clear_ire_stq = NULL;
14169 		bzero(new_lb_ire, sizeof (ire_t));
14170 		/* XXX We need a recovery strategy here. */
14171 		if (ire_init(new_lb_ire,
14172 		    (uchar_t *)&clear_ire->ire_addr,
14173 		    (uchar_t *)&clear_ire->ire_mask,
14174 		    (uchar_t *)&clear_ire->ire_src_addr,
14175 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14176 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14177 		    &clear_ire->ire_max_frag,
14178 		    clear_ire->ire_fp_mp,
14179 		    clear_ire->ire_rfq,
14180 		    clear_ire->ire_stq,
14181 		    clear_ire->ire_type,
14182 		    clear_ire->ire_dlureq_mp,
14183 		    clear_ire->ire_ipif,
14184 		    clear_ire->ire_in_ill,
14185 		    clear_ire->ire_cmask,
14186 		    clear_ire->ire_phandle,
14187 		    clear_ire->ire_ihandle,
14188 		    clear_ire->ire_flags,
14189 		    &clear_ire->ire_uinfo) == NULL)
14190 			cmn_err(CE_PANIC, "ire_init() failed");
14191 		if (clear_ire->ire_stq == NULL) {
14192 			ire_t *ire_next = clear_ire->ire_next;
14193 			if (ire_next != NULL &&
14194 			    ire_next->ire_stq != NULL &&
14195 			    ire_next->ire_addr == clear_ire->ire_addr &&
14196 			    ire_next->ire_ipif->ipif_ill ==
14197 			    clear_ire->ire_ipif->ipif_ill) {
14198 				clear_ire_stq = ire_next;
14199 
14200 				bzero(new_nlb_ire, sizeof (ire_t));
14201 				/* XXX We need a recovery strategy here. */
14202 				if (ire_init(new_nlb_ire,
14203 				    (uchar_t *)&clear_ire_stq->ire_addr,
14204 				    (uchar_t *)&clear_ire_stq->ire_mask,
14205 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14206 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14207 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14208 				    &clear_ire_stq->ire_max_frag,
14209 				    clear_ire_stq->ire_fp_mp,
14210 				    clear_ire_stq->ire_rfq,
14211 				    clear_ire_stq->ire_stq,
14212 				    clear_ire_stq->ire_type,
14213 				    clear_ire_stq->ire_dlureq_mp,
14214 				    clear_ire_stq->ire_ipif,
14215 				    clear_ire_stq->ire_in_ill,
14216 				    clear_ire_stq->ire_cmask,
14217 				    clear_ire_stq->ire_phandle,
14218 				    clear_ire_stq->ire_ihandle,
14219 				    clear_ire_stq->ire_flags,
14220 				    &clear_ire_stq->ire_uinfo) == NULL)
14221 					cmn_err(CE_PANIC, "ire_init() failed");
14222 			}
14223 		}
14224 
14225 		/*
14226 		 * Delete the ire. We can't call ire_delete() since
14227 		 * we are holding the bucket lock. We can't release the
14228 		 * bucket lock since we can't allow irep to change. So just
14229 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14230 		 * ire from the list and do the refrele.
14231 		 */
14232 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14233 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14234 
14235 		if (clear_ire_stq != NULL) {
14236 			ire_fastpath_list_delete(
14237 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14238 			    clear_ire_stq);
14239 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14240 		}
14241 
14242 		/*
14243 		 * Also take care of otherfields like ib/ob pkt count
14244 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14245 		 */
14246 
14247 		/* Add the new ire's. Insert at *irep */
14248 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14249 		ire1 = *irep;
14250 		if (ire1 != NULL)
14251 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14252 		new_lb_ire->ire_next = ire1;
14253 		/* Link the new one in. */
14254 		new_lb_ire->ire_ptpn = irep;
14255 		membar_producer();
14256 		*irep = new_lb_ire;
14257 		new_lb_ire_used = B_TRUE;
14258 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14259 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14260 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14261 
14262 		if (clear_ire_stq != NULL) {
14263 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14264 			irep = &new_lb_ire->ire_next;
14265 			/* Add the new ire. Insert at *irep */
14266 			ire1 = *irep;
14267 			if (ire1 != NULL)
14268 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14269 			new_nlb_ire->ire_next = ire1;
14270 			/* Link the new one in. */
14271 			new_nlb_ire->ire_ptpn = irep;
14272 			membar_producer();
14273 			*irep = new_nlb_ire;
14274 			new_nlb_ire_used = B_TRUE;
14275 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14276 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14277 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14278 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14279 		}
14280 	}
14281 	rw_exit(&irb->irb_lock);
14282 	if (!new_lb_ire_used)
14283 		kmem_cache_free(ire_cache, new_lb_ire);
14284 	if (!new_nlb_ire_used)
14285 		kmem_cache_free(ire_cache, new_nlb_ire);
14286 	IRB_REFRELE(irb);
14287 }
14288 
14289 /*
14290  * Whenever an ipif goes down we have to renominate a different
14291  * broadcast ire to receive. Whenever an ipif comes up, we need
14292  * to make sure that we have only one nominated to receive.
14293  */
14294 static void
14295 ipif_renominate_bcast(ipif_t *ipif)
14296 {
14297 	ill_t *ill = ipif->ipif_ill;
14298 	ipaddr_t subnet_addr;
14299 	ipaddr_t net_addr;
14300 	ipaddr_t net_mask = 0;
14301 	ipaddr_t subnet_netmask;
14302 	ipaddr_t addr;
14303 	ill_group_t *illgrp;
14304 
14305 	illgrp = ill->ill_group;
14306 	/*
14307 	 * If this is the last ipif going down, it might take
14308 	 * the ill out of the group. In that case ipif_down ->
14309 	 * illgrp_delete takes care of doing the nomination.
14310 	 * ipif_down does not call for this case.
14311 	 */
14312 	ASSERT(illgrp != NULL);
14313 
14314 	/* There could not have been any ires associated with this */
14315 	if (ipif->ipif_subnet == 0)
14316 		return;
14317 
14318 	ill_mark_bcast(illgrp, 0);
14319 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14320 
14321 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14322 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14323 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14324 	} else {
14325 		net_mask = htonl(IN_CLASSA_NET);
14326 	}
14327 	addr = net_mask & ipif->ipif_subnet;
14328 	ill_mark_bcast(illgrp, addr);
14329 
14330 	net_addr = ~net_mask | addr;
14331 	ill_mark_bcast(illgrp, net_addr);
14332 
14333 	subnet_netmask = ipif->ipif_net_mask;
14334 	addr = ipif->ipif_subnet;
14335 	ill_mark_bcast(illgrp, addr);
14336 
14337 	subnet_addr = ~subnet_netmask | addr;
14338 	ill_mark_bcast(illgrp, subnet_addr);
14339 }
14340 
14341 /*
14342  * Whenever we form or delete ill groups, we need to nominate one set of
14343  * BROADCAST ires for receiving in the group.
14344  *
14345  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14346  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14347  *    for ill_ipif_up_count to be non-zero. This is the only case where
14348  *    ill_ipif_up_count is zero and we would still find the ires.
14349  *
14350  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14351  *    ipif is UP and we just have to do the nomination.
14352  *
14353  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14354  *    from the group. So, we have to do the nomination.
14355  *
14356  * Because of (3), there could be just one ill in the group. But we have
14357  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14358  * Thus, this function does not optimize when there is only one ill as
14359  * it is not correct for (3).
14360  */
14361 static void
14362 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14363 {
14364 	ill_t *ill;
14365 	ipif_t *ipif;
14366 	ipaddr_t subnet_addr;
14367 	ipaddr_t prev_subnet_addr = 0;
14368 	ipaddr_t net_addr;
14369 	ipaddr_t prev_net_addr = 0;
14370 	ipaddr_t net_mask = 0;
14371 	ipaddr_t subnet_netmask;
14372 	ipaddr_t addr;
14373 
14374 	/*
14375 	 * When the last memeber is leaving, there is nothing to
14376 	 * nominate.
14377 	 */
14378 	if (illgrp->illgrp_ill_count == 0) {
14379 		ASSERT(illgrp->illgrp_ill == NULL);
14380 		return;
14381 	}
14382 
14383 	ill = illgrp->illgrp_ill;
14384 	ASSERT(!ill->ill_isv6);
14385 	/*
14386 	 * We assume that ires with same address and belonging to the
14387 	 * same group, has been grouped together. Nominating a *single*
14388 	 * ill in the group for sending and receiving broadcast is done
14389 	 * by making sure that the first BROADCAST ire (which will be
14390 	 * the one returned by ire_ctable_lookup for ip_rput and the
14391 	 * one that will be used in ip_wput_ire) will be the one that
14392 	 * will not have IRE_MARK_NORECV set.
14393 	 *
14394 	 * 1) ip_rput checks and discards packets received on ires marked
14395 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14396 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14397 	 *    first ire in the group for every broadcast address in the group.
14398 	 *    ip_rput will accept packets only on the first ire i.e only
14399 	 *    one copy of the ill.
14400 	 *
14401 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14402 	 *    packet for the whole group. It needs to send out on the ill
14403 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14404 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14405 	 *    the copy echoed back on other port where the ire is not marked
14406 	 *    with IRE_MARK_NORECV.
14407 	 *
14408 	 * Note that we just need to have the first IRE either loopback or
14409 	 * non-loopback (either of them may not exist if ire_create failed
14410 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14411 	 * always hit the first one and hence will always accept one copy.
14412 	 *
14413 	 * We have a broadcast ire per ill for all the unique prefixes
14414 	 * hosted on that ill. As we don't have a way of knowing the
14415 	 * unique prefixes on a given ill and hence in the whole group,
14416 	 * we just call ill_mark_bcast on all the prefixes that exist
14417 	 * in the group. For the common case of one prefix, the code
14418 	 * below optimizes by remebering the last address used for
14419 	 * markng. In the case of multiple prefixes, this will still
14420 	 * optimize depending the order of prefixes.
14421 	 *
14422 	 * The only unique address across the whole group is 0.0.0.0 and
14423 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14424 	 * the first ire in the bucket for receiving and disables the
14425 	 * others.
14426 	 */
14427 	ill_mark_bcast(illgrp, 0);
14428 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14429 	for (; ill != NULL; ill = ill->ill_group_next) {
14430 
14431 		for (ipif = ill->ill_ipif; ipif != NULL;
14432 		    ipif = ipif->ipif_next) {
14433 
14434 			if (!(ipif->ipif_flags & IPIF_UP) ||
14435 			    ipif->ipif_subnet == 0) {
14436 				continue;
14437 			}
14438 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14439 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14440 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14441 			} else {
14442 				net_mask = htonl(IN_CLASSA_NET);
14443 			}
14444 			addr = net_mask & ipif->ipif_subnet;
14445 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14446 				ill_mark_bcast(illgrp, addr);
14447 				net_addr = ~net_mask | addr;
14448 				ill_mark_bcast(illgrp, net_addr);
14449 			}
14450 			prev_net_addr = addr;
14451 
14452 			subnet_netmask = ipif->ipif_net_mask;
14453 			addr = ipif->ipif_subnet;
14454 			if (prev_subnet_addr == 0 ||
14455 			    prev_subnet_addr != addr) {
14456 				ill_mark_bcast(illgrp, addr);
14457 				subnet_addr = ~subnet_netmask | addr;
14458 				ill_mark_bcast(illgrp, subnet_addr);
14459 			}
14460 			prev_subnet_addr = addr;
14461 		}
14462 	}
14463 }
14464 
14465 /*
14466  * This function is called while forming ill groups.
14467  *
14468  * Currently, we handle only allmulti groups. We want to join
14469  * allmulti on only one of the ills in the groups. In future,
14470  * when we have link aggregation, we may have to join normal
14471  * multicast groups on multiple ills as switch does inbound load
14472  * balancing. Following are the functions that calls this
14473  * function :
14474  *
14475  * 1) ill_recover_multicast : Interface is coming back UP.
14476  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14477  *    will call ill_recover_multicast to recover all the multicast
14478  *    groups. We need to make sure that only one member is joined
14479  *    in the ill group.
14480  *
14481  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14482  *    Somebody is joining allmulti. We need to make sure that only one
14483  *    member is joined in the group.
14484  *
14485  * 3) illgrp_insert : If allmulti has already joined, we need to make
14486  *    sure that only one member is joined in the group.
14487  *
14488  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14489  *    allmulti who we have nominated. We need to pick someother ill.
14490  *
14491  * 5) illgrp_delete : The ill we nominated is leaving the group,
14492  *    we need to pick a new ill to join the group.
14493  *
14494  * For (1), (2), (5) - we just have to check whether there is
14495  * a good ill joined in the group. If we could not find any ills
14496  * joined the group, we should join.
14497  *
14498  * For (4), the one that was nominated to receive, left the group.
14499  * There could be nobody joined in the group when this function is
14500  * called.
14501  *
14502  * For (3) - we need to explicitly check whether there are multiple
14503  * ills joined in the group.
14504  *
14505  * For simplicity, we don't differentiate any of the above cases. We
14506  * just leave the group if it is joined on any of them and join on
14507  * the first good ill.
14508  */
14509 int
14510 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14511 {
14512 	ilm_t *ilm;
14513 	ill_t *ill;
14514 	ill_t *fallback_stand_ill = NULL;
14515 	ill_t *fallback_failed_ill = NULL;
14516 	int ret = 0;
14517 
14518 	/*
14519 	 * Leave the allmulti on all the ills and start fresh.
14520 	 */
14521 	for (ill = illgrp->illgrp_ill; ill != NULL;
14522 	    ill = ill->ill_group_next) {
14523 		if (ill->ill_join_allmulti)
14524 			(void) ip_leave_allmulti(ill->ill_ipif);
14525 	}
14526 
14527 	/*
14528 	 * Choose a good ill. Fallback to standby or failed if
14529 	 * none available. We need to fallback to FAILED in the
14530 	 * case where we have 2 interfaces in a group - where
14531 	 * one of them is failed and another is a good one and
14532 	 * the good one (not marked standby) is leaving the group.
14533 	 */
14534 	ret = 0;
14535 	for (ill = illgrp->illgrp_ill; ill != NULL;
14536 	    ill = ill->ill_group_next) {
14537 		/* Never pick an offline interface */
14538 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14539 			continue;
14540 
14541 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14542 			fallback_failed_ill = ill;
14543 			continue;
14544 		}
14545 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14546 			fallback_stand_ill = ill;
14547 			continue;
14548 		}
14549 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14550 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14551 				ret = ip_join_allmulti(ill->ill_ipif);
14552 				/*
14553 				 * ip_join_allmulti can fail because of memory
14554 				 * failures. So, make sure we join at least
14555 				 * on one ill.
14556 				 */
14557 				if (ill->ill_join_allmulti)
14558 					return (0);
14559 			}
14560 		}
14561 	}
14562 	if (ret != 0) {
14563 		/*
14564 		 * If we tried nominating above and failed to do so,
14565 		 * return error. We might have tried multiple times.
14566 		 * But, return the latest error.
14567 		 */
14568 		return (ret);
14569 	}
14570 	if ((ill = fallback_stand_ill) != NULL) {
14571 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14572 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14573 				ret = ip_join_allmulti(ill->ill_ipif);
14574 				return (ret);
14575 			}
14576 		}
14577 	} else if ((ill = fallback_failed_ill) != NULL) {
14578 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14579 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14580 				ret = ip_join_allmulti(ill->ill_ipif);
14581 				return (ret);
14582 			}
14583 		}
14584 	}
14585 	return (0);
14586 }
14587 
14588 /*
14589  * This function is called from illgrp_delete after it is
14590  * deleted from the group to reschedule responsibilities
14591  * to a different ill.
14592  */
14593 static void
14594 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14595 {
14596 	ilm_t	*ilm;
14597 	ipif_t	*ipif;
14598 	ipaddr_t subnet_addr;
14599 	ipaddr_t net_addr;
14600 	ipaddr_t net_mask = 0;
14601 	ipaddr_t subnet_netmask;
14602 	ipaddr_t addr;
14603 
14604 	ASSERT(ill->ill_group == NULL);
14605 	/*
14606 	 * Broadcast Responsibility:
14607 	 *
14608 	 * 1. If this ill has been nominated for receiving broadcast
14609 	 * packets, we need to find a new one. Before we find a new
14610 	 * one, we need to re-group the ires that are part of this new
14611 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14612 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14613 	 * thing for us.
14614 	 *
14615 	 * 2. If this ill was not nominated for receiving broadcast
14616 	 * packets, we need to clear the IRE_MARK_NORECV flag
14617 	 * so that we continue to send up broadcast packets.
14618 	 */
14619 	if (!ill->ill_isv6) {
14620 		/*
14621 		 * Case 1 above : No optimization here. Just redo the
14622 		 * nomination.
14623 		 */
14624 		ill_group_bcast_for_xmit(ill);
14625 		ill_nominate_bcast_rcv(illgrp);
14626 
14627 		/*
14628 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14629 		 */
14630 		ill_clear_bcast_mark(ill, 0);
14631 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14632 
14633 		for (ipif = ill->ill_ipif; ipif != NULL;
14634 		    ipif = ipif->ipif_next) {
14635 
14636 			if (!(ipif->ipif_flags & IPIF_UP) ||
14637 			    ipif->ipif_subnet == 0) {
14638 				continue;
14639 			}
14640 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14641 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14642 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14643 			} else {
14644 				net_mask = htonl(IN_CLASSA_NET);
14645 			}
14646 			addr = net_mask & ipif->ipif_subnet;
14647 			ill_clear_bcast_mark(ill, addr);
14648 
14649 			net_addr = ~net_mask | addr;
14650 			ill_clear_bcast_mark(ill, net_addr);
14651 
14652 			subnet_netmask = ipif->ipif_net_mask;
14653 			addr = ipif->ipif_subnet;
14654 			ill_clear_bcast_mark(ill, addr);
14655 
14656 			subnet_addr = ~subnet_netmask | addr;
14657 			ill_clear_bcast_mark(ill, subnet_addr);
14658 		}
14659 	}
14660 
14661 	/*
14662 	 * Multicast Responsibility.
14663 	 *
14664 	 * If we have joined allmulti on this one, find a new member
14665 	 * in the group to join allmulti. As this ill is already part
14666 	 * of allmulti, we don't have to join on this one.
14667 	 *
14668 	 * If we have not joined allmulti on this one, there is no
14669 	 * responsibility to handoff. But we need to take new
14670 	 * responsibility i.e, join allmulti on this one if we need
14671 	 * to.
14672 	 */
14673 	if (ill->ill_join_allmulti) {
14674 		(void) ill_nominate_mcast_rcv(illgrp);
14675 	} else {
14676 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14677 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14678 				(void) ip_join_allmulti(ill->ill_ipif);
14679 				break;
14680 			}
14681 		}
14682 	}
14683 
14684 	/*
14685 	 * We intentionally do the flushing of IRE_CACHES only matching
14686 	 * on the ill and not on groups. Note that we are already deleted
14687 	 * from the group.
14688 	 *
14689 	 * This will make sure that all IRE_CACHES whose stq is pointing
14690 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14691 	 * deleted and IRE_CACHES that are not pointing at this ill will
14692 	 * be left alone.
14693 	 */
14694 	if (ill->ill_isv6) {
14695 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14696 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14697 	} else {
14698 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14699 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14700 	}
14701 
14702 	/*
14703 	 * Some conn may have cached one of the IREs deleted above. By removing
14704 	 * the ire reference, we clean up the extra reference to the ill held in
14705 	 * ire->ire_stq.
14706 	 */
14707 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14708 
14709 	/*
14710 	 * Re-do source address selection for all the members in the
14711 	 * group, if they borrowed source address from one of the ipifs
14712 	 * in this ill.
14713 	 */
14714 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14715 		if (ill->ill_isv6) {
14716 			ipif_update_other_ipifs_v6(ipif, illgrp);
14717 		} else {
14718 			ipif_update_other_ipifs(ipif, illgrp);
14719 		}
14720 	}
14721 }
14722 
14723 /*
14724  * Delete the ill from the group. The caller makes sure that it is
14725  * in a group and it okay to delete from the group. So, we always
14726  * delete here.
14727  */
14728 static void
14729 illgrp_delete(ill_t *ill)
14730 {
14731 	ill_group_t *illgrp;
14732 	ill_group_t *tmpg;
14733 	ill_t *tmp_ill;
14734 
14735 	/*
14736 	 * Reset illgrp_ill_schednext if it was pointing at us.
14737 	 * We need to do this before we set ill_group to NULL.
14738 	 */
14739 	rw_enter(&ill_g_lock, RW_WRITER);
14740 	mutex_enter(&ill->ill_lock);
14741 
14742 	illgrp_reset_schednext(ill);
14743 
14744 	illgrp = ill->ill_group;
14745 
14746 	/* Delete the ill from illgrp. */
14747 	if (illgrp->illgrp_ill == ill) {
14748 		illgrp->illgrp_ill = ill->ill_group_next;
14749 	} else {
14750 		tmp_ill = illgrp->illgrp_ill;
14751 		while (tmp_ill->ill_group_next != ill) {
14752 			tmp_ill = tmp_ill->ill_group_next;
14753 			ASSERT(tmp_ill != NULL);
14754 		}
14755 		tmp_ill->ill_group_next = ill->ill_group_next;
14756 	}
14757 	ill->ill_group = NULL;
14758 	ill->ill_group_next = NULL;
14759 
14760 	illgrp->illgrp_ill_count--;
14761 	mutex_exit(&ill->ill_lock);
14762 	rw_exit(&ill_g_lock);
14763 
14764 	/*
14765 	 * As this ill is leaving the group, we need to hand off
14766 	 * the responsibilities to the other ills in the group, if
14767 	 * this ill had some responsibilities.
14768 	 */
14769 
14770 	ill_handoff_responsibility(ill, illgrp);
14771 
14772 	rw_enter(&ill_g_lock, RW_WRITER);
14773 
14774 	if (illgrp->illgrp_ill_count == 0) {
14775 
14776 		ASSERT(illgrp->illgrp_ill == NULL);
14777 		if (ill->ill_isv6) {
14778 			if (illgrp == illgrp_head_v6) {
14779 				illgrp_head_v6 = illgrp->illgrp_next;
14780 			} else {
14781 				tmpg = illgrp_head_v6;
14782 				while (tmpg->illgrp_next != illgrp) {
14783 					tmpg = tmpg->illgrp_next;
14784 					ASSERT(tmpg != NULL);
14785 				}
14786 				tmpg->illgrp_next = illgrp->illgrp_next;
14787 			}
14788 		} else {
14789 			if (illgrp == illgrp_head_v4) {
14790 				illgrp_head_v4 = illgrp->illgrp_next;
14791 			} else {
14792 				tmpg = illgrp_head_v4;
14793 				while (tmpg->illgrp_next != illgrp) {
14794 					tmpg = tmpg->illgrp_next;
14795 					ASSERT(tmpg != NULL);
14796 				}
14797 				tmpg->illgrp_next = illgrp->illgrp_next;
14798 			}
14799 		}
14800 		mutex_destroy(&illgrp->illgrp_lock);
14801 		mi_free(illgrp);
14802 	}
14803 	rw_exit(&ill_g_lock);
14804 
14805 	/*
14806 	 * Even though the ill is out of the group its not necessary
14807 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
14808 	 * We will split the ipsq when phyint_groupname is set to NULL.
14809 	 */
14810 
14811 	/*
14812 	 * Send a routing sockets message if we are deleting from
14813 	 * groups with names.
14814 	 */
14815 	if (ill->ill_phyint->phyint_groupname_len != 0)
14816 		ip_rts_ifmsg(ill->ill_ipif);
14817 }
14818 
14819 /*
14820  * Re-do source address selection. This is normally called when
14821  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
14822  * ipif comes up.
14823  */
14824 void
14825 ill_update_source_selection(ill_t *ill)
14826 {
14827 	ipif_t *ipif;
14828 
14829 	ASSERT(IAM_WRITER_ILL(ill));
14830 
14831 	if (ill->ill_group != NULL)
14832 		ill = ill->ill_group->illgrp_ill;
14833 
14834 	for (; ill != NULL; ill = ill->ill_group_next) {
14835 		for (ipif = ill->ill_ipif; ipif != NULL;
14836 		    ipif = ipif->ipif_next) {
14837 			if (ill->ill_isv6)
14838 				ipif_recreate_interface_routes_v6(NULL, ipif);
14839 			else
14840 				ipif_recreate_interface_routes(NULL, ipif);
14841 		}
14842 	}
14843 }
14844 
14845 /*
14846  * Insert ill in a group headed by illgrp_head. The caller can either
14847  * pass a groupname in which case we search for a group with the
14848  * same name to insert in or pass a group to insert in. This function
14849  * would only search groups with names.
14850  *
14851  * NOTE : The caller should make sure that there is at least one ipif
14852  *	  UP on this ill so that illgrp_scheduler can pick this ill
14853  *	  for outbound packets. If ill_ipif_up_count is zero, we have
14854  *	  already sent a DL_UNBIND to the driver and we don't want to
14855  *	  send anymore packets. We don't assert for ipif_up_count
14856  *	  to be greater than zero, because ipif_up_done wants to call
14857  *	  this function before bumping up the ipif_up_count. See
14858  *	  ipif_up_done() for details.
14859  */
14860 int
14861 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
14862     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
14863 {
14864 	ill_group_t *illgrp;
14865 	ill_t *prev_ill;
14866 	phyint_t *phyi;
14867 
14868 	ASSERT(ill->ill_group == NULL);
14869 
14870 	rw_enter(&ill_g_lock, RW_WRITER);
14871 	mutex_enter(&ill->ill_lock);
14872 
14873 	if (groupname != NULL) {
14874 		/*
14875 		 * Look for a group with a matching groupname to insert.
14876 		 */
14877 		for (illgrp = *illgrp_head; illgrp != NULL;
14878 		    illgrp = illgrp->illgrp_next) {
14879 
14880 			ill_t *tmp_ill;
14881 
14882 			tmp_ill = illgrp->illgrp_ill;
14883 			ASSERT(tmp_ill != NULL && tmp_ill->ill_phyint != NULL);
14884 			phyi = tmp_ill->ill_phyint;
14885 			/*
14886 			 * Look at groups which has names only.
14887 			 */
14888 			if (phyi->phyint_groupname_len == 0)
14889 				continue;
14890 			/*
14891 			 * Names are stored in the phyint common to both
14892 			 * IPv4 and IPv6.
14893 			 */
14894 			if (mi_strcmp(phyi->phyint_groupname,
14895 			    groupname) == 0) {
14896 				break;
14897 			}
14898 		}
14899 	} else {
14900 		/*
14901 		 * If the caller passes in a NULL "grp_to_insert", we
14902 		 * allocate one below and insert this singleton.
14903 		 */
14904 		illgrp = grp_to_insert;
14905 	}
14906 
14907 	ill->ill_group_next = NULL;
14908 
14909 	if (illgrp == NULL) {
14910 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
14911 		if (illgrp == NULL) {
14912 			return (ENOMEM);
14913 		}
14914 		illgrp->illgrp_next = *illgrp_head;
14915 		*illgrp_head = illgrp;
14916 		illgrp->illgrp_ill = ill;
14917 		illgrp->illgrp_ill_count = 1;
14918 		ill->ill_group = illgrp;
14919 		/*
14920 		 * Used in illgrp_scheduler to protect multiple threads
14921 		 * from traversing the list.
14922 		 */
14923 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
14924 	} else {
14925 		ASSERT(ill->ill_net_type ==
14926 		    illgrp->illgrp_ill->ill_net_type);
14927 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
14928 
14929 		/* Insert ill at tail of this group */
14930 		prev_ill = illgrp->illgrp_ill;
14931 		while (prev_ill->ill_group_next != NULL)
14932 			prev_ill = prev_ill->ill_group_next;
14933 		prev_ill->ill_group_next = ill;
14934 		ill->ill_group = illgrp;
14935 		illgrp->illgrp_ill_count++;
14936 		/*
14937 		 * Inherit group properties. Currently only forwarding
14938 		 * is the property we try to keep the same with all the
14939 		 * ills. When there are more, we will abstract this into
14940 		 * a function.
14941 		 */
14942 		ill->ill_flags &= ~ILLF_ROUTER;
14943 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
14944 	}
14945 	mutex_exit(&ill->ill_lock);
14946 	rw_exit(&ill_g_lock);
14947 
14948 	/*
14949 	 * 1) When ipif_up_done() calls this function, ipif_up_count
14950 	 *    may be zero as it has not yet been bumped. But the ires
14951 	 *    have already been added. So, we do the nomination here
14952 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
14953 	 *    for ill_ipif_up_count != 0. Thus we don't check for
14954 	 *    ill_ipif_up_count here while nominating broadcast ires for
14955 	 *    receive.
14956 	 *
14957 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
14958 	 *    to group them properly as ire_add() has already happened
14959 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
14960 	 *    case, we need to do it here anyway.
14961 	 */
14962 	if (!ill->ill_isv6) {
14963 		ill_group_bcast_for_xmit(ill);
14964 		ill_nominate_bcast_rcv(illgrp);
14965 	}
14966 
14967 	if (!ipif_is_coming_up) {
14968 		/*
14969 		 * When ipif_up_done() calls this function, the multicast
14970 		 * groups have not been joined yet. So, there is no point in
14971 		 * nomination. ip_join_allmulti will handle groups when
14972 		 * ill_recover_multicast is called from ipif_up_done() later.
14973 		 */
14974 		(void) ill_nominate_mcast_rcv(illgrp);
14975 		/*
14976 		 * ipif_up_done calls ill_update_source_selection
14977 		 * anyway. Moreover, we don't want to re-create
14978 		 * interface routes while ipif_up_done() still has reference
14979 		 * to them. Refer to ipif_up_done() for more details.
14980 		 */
14981 		ill_update_source_selection(ill);
14982 	}
14983 
14984 	/*
14985 	 * Send a routing sockets message if we are inserting into
14986 	 * groups with names.
14987 	 */
14988 	if (groupname != NULL)
14989 		ip_rts_ifmsg(ill->ill_ipif);
14990 	return (0);
14991 }
14992 
14993 /*
14994  * Return the first phyint matching the groupname. There could
14995  * be more than one when there are ill groups.
14996  *
14997  * Needs work: called only from ip_sioctl_groupname
14998  */
14999 static phyint_t *
15000 phyint_lookup_group(char *groupname)
15001 {
15002 	phyint_t *phyi;
15003 
15004 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15005 	/*
15006 	 * Group names are stored in the phyint - a common structure
15007 	 * to both IPv4 and IPv6.
15008 	 */
15009 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15010 	for (; phyi != NULL;
15011 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15012 	    phyi, AVL_AFTER)) {
15013 		if (phyi->phyint_groupname_len == 0)
15014 			continue;
15015 		ASSERT(phyi->phyint_groupname != NULL);
15016 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15017 			return (phyi);
15018 	}
15019 	return (NULL);
15020 }
15021 
15022 
15023 
15024 /*
15025  * MT notes on creation and deletion of IPMP groups
15026  *
15027  * Creation and deletion of IPMP groups introduce the need to merge or
15028  * split the associated serialization objects i.e the ipsq's. Normally all
15029  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15030  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15031  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15032  * is a need to change the <ill-ipsq> association and we have to operate on both
15033  * the source and destination IPMP groups. For eg. attempting to set the
15034  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15035  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15036  * source or destination IPMP group are mapped to a single ipsq for executing
15037  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15038  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15039  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15040  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15041  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15042  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15043  *
15044  * In the above example the ioctl handling code locates the current ipsq of hme0
15045  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15046  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15047  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15048  * the destination ipsq. If the destination ipsq is not busy, it also enters
15049  * the destination ipsq exclusively. Now the actual groupname setting operation
15050  * can proceed. If the destination ipsq is busy, the operation is enqueued
15051  * on the destination (merged) ipsq and will be handled in the unwind from
15052  * ipsq_exit.
15053  *
15054  * To prevent other threads accessing the ill while the group name change is
15055  * in progres, we bring down the ipifs which also removes the ill from the
15056  * group. The group is changed in phyint and when the first ipif on the ill
15057  * is brought up, the ill is inserted into the right IPMP group by
15058  * illgrp_insert.
15059  */
15060 /* ARGSUSED */
15061 int
15062 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15063     ip_ioctl_cmd_t *ipip, void *ifreq)
15064 {
15065 	int i;
15066 	char *tmp;
15067 	int namelen;
15068 	ill_t *ill = ipif->ipif_ill;
15069 	ill_t *ill_v4, *ill_v6;
15070 	int err = 0;
15071 	phyint_t *phyi;
15072 	phyint_t *phyi_tmp;
15073 	struct lifreq *lifr;
15074 	mblk_t	*mp1;
15075 	char *groupname;
15076 	ipsq_t *ipsq;
15077 
15078 	ASSERT(IAM_WRITER_IPIF(ipif));
15079 
15080 	/* Existance verified in ip_wput_nondata */
15081 	mp1 = mp->b_cont->b_cont;
15082 	lifr = (struct lifreq *)mp1->b_rptr;
15083 	groupname = lifr->lifr_groupname;
15084 
15085 	if (ipif->ipif_id != 0)
15086 		return (EINVAL);
15087 
15088 	phyi = ill->ill_phyint;
15089 	ASSERT(phyi != NULL);
15090 
15091 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15092 		return (EINVAL);
15093 
15094 	tmp = groupname;
15095 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15096 		;
15097 
15098 	if (i == LIFNAMSIZ) {
15099 		/* no null termination */
15100 		return (EINVAL);
15101 	}
15102 
15103 	/*
15104 	 * Calculate the namelen exclusive of the null
15105 	 * termination character.
15106 	 */
15107 	namelen = tmp - groupname;
15108 
15109 	ill_v4 = phyi->phyint_illv4;
15110 	ill_v6 = phyi->phyint_illv6;
15111 
15112 	/*
15113 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15114 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15115 	 * synchronization notes in ip.c
15116 	 */
15117 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15118 		return (EINVAL);
15119 	}
15120 
15121 	/*
15122 	 * mark the ill as changing.
15123 	 * this should queue all new requests on the syncq.
15124 	 */
15125 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15126 
15127 	if (ill_v4 != NULL)
15128 		ill_v4->ill_state_flags |= ILL_CHANGING;
15129 	if (ill_v6 != NULL)
15130 		ill_v6->ill_state_flags |= ILL_CHANGING;
15131 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15132 
15133 	if (namelen == 0) {
15134 		/*
15135 		 * Null string means remove this interface from the
15136 		 * existing group.
15137 		 */
15138 		if (phyi->phyint_groupname_len == 0) {
15139 			/*
15140 			 * Never was in a group.
15141 			 */
15142 			err = 0;
15143 			goto done;
15144 		}
15145 
15146 		/*
15147 		 * IPv4 or IPv6 may be temporarily out of the group when all
15148 		 * the ipifs are down. Thus, we need to check for ill_group to
15149 		 * be non-NULL.
15150 		 */
15151 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15152 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15153 			mutex_enter(&ill_v4->ill_lock);
15154 			if (!ill_is_quiescent(ill_v4)) {
15155 				/*
15156 				 * ipsq_pending_mp_add will not fail since
15157 				 * connp is NULL
15158 				 */
15159 				(void) ipsq_pending_mp_add(NULL,
15160 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15161 				mutex_exit(&ill_v4->ill_lock);
15162 				err = EINPROGRESS;
15163 				goto done;
15164 			}
15165 			mutex_exit(&ill_v4->ill_lock);
15166 		}
15167 
15168 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15169 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15170 			mutex_enter(&ill_v6->ill_lock);
15171 			if (!ill_is_quiescent(ill_v6)) {
15172 				(void) ipsq_pending_mp_add(NULL,
15173 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15174 				mutex_exit(&ill_v6->ill_lock);
15175 				err = EINPROGRESS;
15176 				goto done;
15177 			}
15178 			mutex_exit(&ill_v6->ill_lock);
15179 		}
15180 
15181 		rw_enter(&ill_g_lock, RW_WRITER);
15182 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15183 		mutex_enter(&phyi->phyint_lock);
15184 		ASSERT(phyi->phyint_groupname != NULL);
15185 		mi_free(phyi->phyint_groupname);
15186 		phyi->phyint_groupname = NULL;
15187 		phyi->phyint_groupname_len = 0;
15188 		mutex_exit(&phyi->phyint_lock);
15189 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15190 		rw_exit(&ill_g_lock);
15191 		err = ill_up_ipifs(ill, q, mp);
15192 
15193 		/*
15194 		 * set the split flag so that the ipsq can be split
15195 		 */
15196 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15197 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15198 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15199 
15200 	} else {
15201 		if (phyi->phyint_groupname_len != 0) {
15202 			ASSERT(phyi->phyint_groupname != NULL);
15203 			/* Are we inserting in the same group ? */
15204 			if (mi_strcmp(groupname,
15205 			    phyi->phyint_groupname) == 0) {
15206 				err = 0;
15207 				goto done;
15208 			}
15209 		}
15210 
15211 		rw_enter(&ill_g_lock, RW_READER);
15212 		/*
15213 		 * Merge ipsq for the group's.
15214 		 * This check is here as multiple groups/ills might be
15215 		 * sharing the same ipsq.
15216 		 * If we have to merege than the operation is restarted
15217 		 * on the new ipsq.
15218 		 */
15219 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15220 		if (phyi->phyint_ipsq != ipsq) {
15221 			rw_exit(&ill_g_lock);
15222 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15223 			goto done;
15224 		}
15225 		/*
15226 		 * Running exclusive on new ipsq.
15227 		 */
15228 
15229 		ASSERT(ipsq != NULL);
15230 		ASSERT(ipsq->ipsq_writer == curthread);
15231 
15232 		/*
15233 		 * Check whether the ill_type and ill_net_type matches before
15234 		 * we allocate any memory so that the cleanup is easier.
15235 		 *
15236 		 * We can't group dissimilar ones as we can't load spread
15237 		 * packets across the group because of potential link-level
15238 		 * header differences.
15239 		 */
15240 		phyi_tmp = phyint_lookup_group(groupname);
15241 		if (phyi_tmp != NULL) {
15242 			if ((ill_v4 != NULL &&
15243 			    phyi_tmp->phyint_illv4 != NULL) &&
15244 			    ((ill_v4->ill_net_type !=
15245 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15246 			    (ill_v4->ill_type !=
15247 			    phyi_tmp->phyint_illv4->ill_type))) {
15248 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15249 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15250 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15251 				rw_exit(&ill_g_lock);
15252 				return (EINVAL);
15253 			}
15254 			if ((ill_v6 != NULL &&
15255 			    phyi_tmp->phyint_illv6 != NULL) &&
15256 			    ((ill_v6->ill_net_type !=
15257 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15258 			    (ill_v6->ill_type !=
15259 			    phyi_tmp->phyint_illv6->ill_type))) {
15260 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15261 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15262 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15263 				rw_exit(&ill_g_lock);
15264 				return (EINVAL);
15265 			}
15266 		}
15267 
15268 		rw_exit(&ill_g_lock);
15269 
15270 		/*
15271 		 * bring down all v4 ipifs.
15272 		 */
15273 		if (ill_v4 != NULL) {
15274 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15275 		}
15276 
15277 		/*
15278 		 * bring down all v6 ipifs.
15279 		 */
15280 		if (ill_v6 != NULL) {
15281 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15282 		}
15283 
15284 		/*
15285 		 * make sure all ipifs are down and there are no active
15286 		 * references. Call to ipsq_pending_mp_add will not fail
15287 		 * since connp is NULL.
15288 		 */
15289 		if (ill_v4 != NULL) {
15290 			mutex_enter(&ill_v4->ill_lock);
15291 			if (!ill_is_quiescent(ill_v4)) {
15292 				(void) ipsq_pending_mp_add(NULL,
15293 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15294 				mutex_exit(&ill_v4->ill_lock);
15295 				err = EINPROGRESS;
15296 				goto done;
15297 			}
15298 			mutex_exit(&ill_v4->ill_lock);
15299 		}
15300 
15301 		if (ill_v6 != NULL) {
15302 			mutex_enter(&ill_v6->ill_lock);
15303 			if (!ill_is_quiescent(ill_v6)) {
15304 				(void) ipsq_pending_mp_add(NULL,
15305 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15306 				mutex_exit(&ill_v6->ill_lock);
15307 				err = EINPROGRESS;
15308 				goto done;
15309 			}
15310 			mutex_exit(&ill_v6->ill_lock);
15311 		}
15312 
15313 		/*
15314 		 * allocate including space for null terminator
15315 		 * before we insert.
15316 		 */
15317 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15318 		if (tmp == NULL)
15319 			return (ENOMEM);
15320 
15321 		rw_enter(&ill_g_lock, RW_WRITER);
15322 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15323 		mutex_enter(&phyi->phyint_lock);
15324 		if (phyi->phyint_groupname_len != 0) {
15325 			ASSERT(phyi->phyint_groupname != NULL);
15326 			mi_free(phyi->phyint_groupname);
15327 		}
15328 
15329 		/*
15330 		 * setup the new group name.
15331 		 */
15332 		phyi->phyint_groupname = tmp;
15333 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15334 		phyi->phyint_groupname_len = namelen + 1;
15335 		mutex_exit(&phyi->phyint_lock);
15336 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15337 		rw_exit(&ill_g_lock);
15338 
15339 		err = ill_up_ipifs(ill, q, mp);
15340 	}
15341 
15342 done:
15343 	/*
15344 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15345 	 */
15346 	if (err != EINPROGRESS) {
15347 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15348 		if (ill_v4 != NULL)
15349 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15350 		if (ill_v6 != NULL)
15351 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15352 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15353 	}
15354 	return (err);
15355 }
15356 
15357 /* ARGSUSED */
15358 int
15359 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15360     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15361 {
15362 	ill_t *ill;
15363 	phyint_t *phyi;
15364 	struct lifreq *lifr;
15365 	mblk_t	*mp1;
15366 
15367 	/* Existence verified in ip_wput_nondata */
15368 	mp1 = mp->b_cont->b_cont;
15369 	lifr = (struct lifreq *)mp1->b_rptr;
15370 	ill = ipif->ipif_ill;
15371 	phyi = ill->ill_phyint;
15372 
15373 	lifr->lifr_groupname[0] = '\0';
15374 	/*
15375 	 * ill_group may be null if all the interfaces
15376 	 * are down. But still, the phyint should always
15377 	 * hold the name.
15378 	 */
15379 	if (phyi->phyint_groupname_len != 0) {
15380 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15381 		    phyi->phyint_groupname_len);
15382 	}
15383 
15384 	return (0);
15385 }
15386 
15387 
15388 typedef struct conn_move_s {
15389 	ill_t	*cm_from_ill;
15390 	ill_t	*cm_to_ill;
15391 	int	cm_ifindex;
15392 } conn_move_t;
15393 
15394 /*
15395  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15396  */
15397 static void
15398 conn_move(conn_t *connp, caddr_t arg)
15399 {
15400 	conn_move_t *connm;
15401 	int ifindex;
15402 	int i;
15403 	ill_t *from_ill;
15404 	ill_t *to_ill;
15405 	ilg_t *ilg;
15406 	ilm_t *ret_ilm;
15407 
15408 	connm = (conn_move_t *)arg;
15409 	ifindex = connm->cm_ifindex;
15410 	from_ill = connm->cm_from_ill;
15411 	to_ill = connm->cm_to_ill;
15412 
15413 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15414 
15415 	/* All multicast fields protected by conn_lock */
15416 	mutex_enter(&connp->conn_lock);
15417 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15418 	if ((connp->conn_outgoing_ill == from_ill) &&
15419 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15420 		connp->conn_outgoing_ill = to_ill;
15421 		connp->conn_incoming_ill = to_ill;
15422 	}
15423 
15424 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15425 
15426 	if ((connp->conn_multicast_ill == from_ill) &&
15427 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15428 		connp->conn_multicast_ill = connm->cm_to_ill;
15429 	}
15430 
15431 	/* Change IP_XMIT_IF associations */
15432 	if ((connp->conn_xmit_if_ill == from_ill) &&
15433 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15434 		connp->conn_xmit_if_ill = to_ill;
15435 	}
15436 	/*
15437 	 * Change the ilg_ill to point to the new one. This assumes
15438 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15439 	 * has been told to receive packets on this interface.
15440 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15441 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15442 	 * some ilms may not have moved. We check to see whether
15443 	 * the ilms have moved to to_ill. We can't check on from_ill
15444 	 * as in the process of moving, we could have split an ilm
15445 	 * in to two - which has the same orig_ifindex and v6group.
15446 	 *
15447 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15448 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15449 	 */
15450 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15451 		ilg = &connp->conn_ilg[i];
15452 		if ((ilg->ilg_ill == from_ill) &&
15453 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15454 			/* ifindex != 0 indicates failback */
15455 			if (ifindex != 0) {
15456 				connp->conn_ilg[i].ilg_ill = to_ill;
15457 				continue;
15458 			}
15459 
15460 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15461 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15462 			    connp->conn_zoneid);
15463 
15464 			if (ret_ilm != NULL)
15465 				connp->conn_ilg[i].ilg_ill = to_ill;
15466 		}
15467 	}
15468 	mutex_exit(&connp->conn_lock);
15469 }
15470 
15471 static void
15472 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15473 {
15474 	conn_move_t connm;
15475 
15476 	connm.cm_from_ill = from_ill;
15477 	connm.cm_to_ill = to_ill;
15478 	connm.cm_ifindex = ifindex;
15479 
15480 	ipcl_walk(conn_move, (caddr_t)&connm);
15481 }
15482 
15483 /*
15484  * ilm has been moved from from_ill to to_ill.
15485  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15486  * appropriately.
15487  *
15488  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15489  *	  the code there de-references ipif_ill to get the ill to
15490  *	  send multicast requests. It does not work as ipif is on its
15491  *	  move and already moved when this function is called.
15492  *	  Thus, we need to use from_ill and to_ill send down multicast
15493  *	  requests.
15494  */
15495 static void
15496 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15497 {
15498 	ipif_t *ipif;
15499 	ilm_t *ilm;
15500 
15501 	/*
15502 	 * See whether we need to send down DL_ENABMULTI_REQ on
15503 	 * to_ill as ilm has just been added.
15504 	 */
15505 	ASSERT(IAM_WRITER_ILL(to_ill));
15506 	ASSERT(IAM_WRITER_ILL(from_ill));
15507 
15508 	ILM_WALKER_HOLD(to_ill);
15509 	for (ilm = to_ill->ill_ilm; ilm != NULL && ilm->ilm_is_new &&
15510 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm->ilm_next) {
15511 
15512 		/*
15513 		 * no locks held, ill/ipif cannot dissappear as long
15514 		 * as we are writer.
15515 		 */
15516 		ipif = to_ill->ill_ipif;
15517 		/*
15518 		 * No need to hold any lock as we are the writer and this
15519 		 * can only be changed by a writer.
15520 		 */
15521 		ilm->ilm_is_new = B_FALSE;
15522 
15523 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15524 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15525 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15526 			    "resolver\n"));
15527 			continue;		/* Must be IRE_IF_NORESOLVER */
15528 		}
15529 
15530 
15531 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15532 			ip1dbg(("ilm_send_multicast_reqs: "
15533 			    "to_ill MULTI_BCAST\n"));
15534 			ilm->ilm_join_mld = B_FALSE;
15535 			goto from;
15536 		}
15537 
15538 		if (ilm->ilm_join_mld) {
15539 			ASSERT(to_ill->ill_isv6);
15540 			mld_joingroup(ilm);
15541 		}
15542 
15543 		ilm->ilm_join_mld = B_FALSE;
15544 
15545 		if (to_ill->ill_ipif_up_count == 0) {
15546 			/*
15547 			 * Nobody there. All multicast addresses will be
15548 			 * re-joined when we get the DL_BIND_ACK bringing the
15549 			 * interface up.
15550 			 */
15551 			ilm->ilm_notify_driver = B_FALSE;
15552 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15553 			goto from;
15554 		}
15555 
15556 		/*
15557 		 * For allmulti address, we want to join on only one interface.
15558 		 * Checking for ilm_numentries_v6 is not correct as you may
15559 		 * find an ilm with zero address on to_ill, but we may not
15560 		 * have nominated to_ill for receiving. Thus, if we have
15561 		 * nominated from_ill (ill_join_allmulti is set), nominate
15562 		 * only if to_ill is not already nominated (to_ill normally
15563 		 * should not have been nominated if "from_ill" has already
15564 		 * been nominated. As we don't prevent failovers from happening
15565 		 * across groups, we don't assert).
15566 		 */
15567 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15568 			/*
15569 			 * There is no need to hold ill locks as we are
15570 			 * writer on both ills and when ill_join_allmulti
15571 			 * is changed the thread is always a writer.
15572 			 */
15573 			if (from_ill->ill_join_allmulti &&
15574 			    !to_ill->ill_join_allmulti) {
15575 				(void) ip_join_allmulti(to_ill->ill_ipif);
15576 			}
15577 		} else if (ilm->ilm_notify_driver) {
15578 
15579 			/*
15580 			 * This is a newly moved ilm so we need to tell the
15581 			 * driver about the new group. There can be more than
15582 			 * one ilm's for the same group in the list each with a
15583 			 * different orig_ifindex. We have to inform the driver
15584 			 * once. In ilm_move_v[4,6] we only set the flag
15585 			 * ilm_notify_driver for the first ilm.
15586 			 */
15587 
15588 			(void) ip_ll_send_enabmulti_req(to_ill,
15589 			    &ilm->ilm_v6addr);
15590 		}
15591 
15592 		ilm->ilm_notify_driver = B_FALSE;
15593 
15594 		/*
15595 		 * See whether we need to send down DL_DISABMULTI_REQ on
15596 		 * from_ill as ilm has just been removed.
15597 		 */
15598 from:
15599 		ipif = from_ill->ill_ipif;
15600 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15601 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15602 			ip1dbg(("ilm_send_multicast_reqs: "
15603 			    "from_ill not resolver\n"));
15604 			continue;		/* Must be IRE_IF_NORESOLVER */
15605 		}
15606 
15607 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15608 			ip1dbg(("ilm_send_multicast_reqs: "
15609 			    "from_ill MULTI_BCAST\n"));
15610 			continue;
15611 		}
15612 
15613 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15614 			if (from_ill->ill_join_allmulti)
15615 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15616 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15617 			(void) ip_ll_send_disabmulti_req(from_ill,
15618 		    &ilm->ilm_v6addr);
15619 		}
15620 	}
15621 	ILM_WALKER_RELE(to_ill);
15622 }
15623 
15624 /*
15625  * This function is called when all multicast memberships needs
15626  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15627  * called only once unlike the IPv4 counterpart where it is called after
15628  * every logical interface is moved. The reason is due to multicast
15629  * memberships are joined using an interface address in IPv4 while in
15630  * IPv6, interface index is used.
15631  */
15632 static void
15633 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15634 {
15635 	ilm_t	*ilm;
15636 	ilm_t	*ilm_next;
15637 	ilm_t	*new_ilm;
15638 	ilm_t	**ilmp;
15639 	int	count;
15640 	char buf[INET6_ADDRSTRLEN];
15641 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15642 
15643 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15644 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15645 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15646 
15647 	if (ifindex == 0) {
15648 		/*
15649 		 * Form the solicited node mcast address which is used later.
15650 		 */
15651 		ipif_t *ipif;
15652 
15653 		ipif = from_ill->ill_ipif;
15654 		ASSERT(ipif->ipif_id == 0);
15655 
15656 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15657 	}
15658 
15659 	ilmp = &from_ill->ill_ilm;
15660 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
15661 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
15662 		ilm_next = ilm->ilm_next;
15663 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15664 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15665 		ASSERT(ilm->ilm_orig_ifindex != 0);
15666 		if (ilm->ilm_orig_ifindex == ifindex) {
15667 			/*
15668 			 * We are failing back multicast memberships.
15669 			 * If the same ilm exists in to_ill, it means somebody
15670 			 * has joined the same group there e.g. ff02::1
15671 			 * is joined within the kernel when the interfaces
15672 			 * came UP.
15673 			 */
15674 			ASSERT(ilm->ilm_ipif == NULL);
15675 			if (new_ilm != NULL) {
15676 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15677 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15678 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15679 					new_ilm->ilm_join_mld = B_TRUE;
15680 				}
15681 			} else {
15682 				/*
15683 				 * check if we can just move the ilm
15684 				 */
15685 				if (from_ill->ill_ilm_walker_cnt != 0) {
15686 					/*
15687 					 * We have walkers we cannot move
15688 					 * the ilm, so allocate a new ilm,
15689 					 * this (old) ilm will be marked
15690 					 * ILM_DELETED at the end of the loop
15691 					 * and will be freed when the
15692 					 * last walker exits.
15693 					 */
15694 					new_ilm = (ilm_t *)mi_zalloc
15695 					    (sizeof (ilm_t));
15696 					if (new_ilm == NULL) {
15697 						ip0dbg(("ilm_move_v6: "
15698 						    "FAILBACK of IPv6"
15699 						    " multicast address %s : "
15700 						    "from %s to"
15701 						    " %s failed : ENOMEM \n",
15702 						    inet_ntop(AF_INET6,
15703 						    &ilm->ilm_v6addr, buf,
15704 						    sizeof (buf)),
15705 						    from_ill->ill_name,
15706 						    to_ill->ill_name));
15707 
15708 							ilmp = &ilm->ilm_next;
15709 							continue;
15710 					}
15711 					*new_ilm = *ilm;
15712 					/*
15713 					 * we don't want new_ilm linked to
15714 					 * ilm's filter list.
15715 					 */
15716 					new_ilm->ilm_filter = NULL;
15717 				} else {
15718 					/*
15719 					 * No walkers we can move the ilm.
15720 					 * lets take it out of the list.
15721 					 */
15722 					*ilmp = ilm->ilm_next;
15723 					ilm->ilm_next = NULL;
15724 					new_ilm = ilm;
15725 				}
15726 
15727 				new_ilm->ilm_ill = to_ill;
15728 				/* Add to the to_ill's list */
15729 				new_ilm->ilm_next = to_ill->ill_ilm;
15730 				to_ill->ill_ilm = new_ilm;
15731 				/*
15732 				 * set the flag so that mld_joingroup is
15733 				 * called in ilm_send_multicast_reqs().
15734 				 */
15735 				new_ilm->ilm_join_mld = B_TRUE;
15736 				/*
15737 				 * if this is the first ilm for the group
15738 				 * set ilm_notify_driver so that we notify the
15739 				 * driver in ilm_send_multicast_reqs.
15740 				 */
15741 				if (ilm_lookup_ill_v6(to_ill,
15742 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15743 					new_ilm->ilm_notify_driver = B_TRUE;
15744 			}
15745 			goto bottom;
15746 		} else if (ifindex != 0) {
15747 			/*
15748 			 * If this is FAILBACK (ifindex != 0) and the ifindex
15749 			 * has not matched above, look at the next ilm.
15750 			 */
15751 			ilmp = &ilm->ilm_next;
15752 			continue;
15753 		}
15754 		/*
15755 		 * If we are here, it means ifindex is 0. Failover
15756 		 * everything.
15757 		 *
15758 		 * We need to handle solicited node mcast address
15759 		 * and all_nodes mcast address differently as they
15760 		 * are joined witin the kenrel (ipif_multicast_up)
15761 		 * and potentially from the userland. We are called
15762 		 * after the ipifs of from_ill has been moved.
15763 		 * If we still find ilms on ill with solicited node
15764 		 * mcast address or all_nodes mcast address, it must
15765 		 * belong to the UP interface that has not moved e.g.
15766 		 * ipif_id 0 with the link local prefix does not move.
15767 		 * We join this on the new ill accounting for all the
15768 		 * userland memberships so that applications don't
15769 		 * see any failure.
15770 		 *
15771 		 * We need to make sure that we account only for the
15772 		 * solicited node and all node multicast addresses
15773 		 * that was brought UP on these. In the case of
15774 		 * a failover from A to B, we might have ilms belonging
15775 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
15776 		 * for the membership from the userland. If we are failing
15777 		 * over from B to C now, we will find the ones belonging
15778 		 * to A on B. These don't account for the ill_ipif_up_count.
15779 		 * They just move from B to C. The check below on
15780 		 * ilm_orig_ifindex ensures that.
15781 		 */
15782 		if ((ilm->ilm_orig_ifindex ==
15783 		    from_ill->ill_phyint->phyint_ifindex) &&
15784 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
15785 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
15786 		    &ilm->ilm_v6addr))) {
15787 			ASSERT(ilm->ilm_refcnt > 0);
15788 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
15789 			/*
15790 			 * For indentation reasons, we are not using a
15791 			 * "else" here.
15792 			 */
15793 			if (count == 0) {
15794 				ilmp = &ilm->ilm_next;
15795 				continue;
15796 			}
15797 			ilm->ilm_refcnt -= count;
15798 			if (new_ilm != NULL) {
15799 				/*
15800 				 * Can find one with the same
15801 				 * ilm_orig_ifindex, if we are failing
15802 				 * over to a STANDBY. This happens
15803 				 * when somebody wants to join a group
15804 				 * on a STANDBY interface and we
15805 				 * internally join on a different one.
15806 				 * If we had joined on from_ill then, a
15807 				 * failover now will find a new ilm
15808 				 * with this index.
15809 				 */
15810 				ip1dbg(("ilm_move_v6: FAILOVER, found"
15811 				    " new ilm on %s, group address %s\n",
15812 				    to_ill->ill_name,
15813 				    inet_ntop(AF_INET6,
15814 				    &ilm->ilm_v6addr, buf,
15815 				    sizeof (buf))));
15816 				new_ilm->ilm_refcnt += count;
15817 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15818 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15819 					new_ilm->ilm_join_mld = B_TRUE;
15820 				}
15821 			} else {
15822 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
15823 				if (new_ilm == NULL) {
15824 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
15825 					    " multicast address %s : from %s to"
15826 					    " %s failed : ENOMEM \n",
15827 					    inet_ntop(AF_INET6,
15828 					    &ilm->ilm_v6addr, buf,
15829 					    sizeof (buf)), from_ill->ill_name,
15830 					    to_ill->ill_name));
15831 					ilmp = &ilm->ilm_next;
15832 					continue;
15833 				}
15834 				*new_ilm = *ilm;
15835 				new_ilm->ilm_filter = NULL;
15836 				new_ilm->ilm_refcnt = count;
15837 				new_ilm->ilm_ill = to_ill;
15838 				new_ilm->ilm_timer = INFINITY;
15839 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
15840 				new_ilm->ilm_join_mld = B_TRUE;
15841 				/* Add to the to_ill's list */
15842 				new_ilm->ilm_next = to_ill->ill_ilm;
15843 				to_ill->ill_ilm = new_ilm;
15844 				/*
15845 				 * If the to_ill has not joined this
15846 				 * group we need to tell the driver in
15847 				 * ill_send_multicast_reqs.
15848 				 */
15849 				if (ilm_lookup_ill_v6(to_ill,
15850 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15851 					new_ilm->ilm_notify_driver = B_TRUE;
15852 				ASSERT(new_ilm->ilm_ipif == NULL);
15853 			}
15854 			if (ilm->ilm_refcnt == 0) {
15855 				goto bottom;
15856 			} else {
15857 				new_ilm->ilm_is_new = B_TRUE;
15858 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
15859 				CLEAR_SLIST(new_ilm->ilm_filter);
15860 				ilmp = &ilm->ilm_next;
15861 			}
15862 			continue;
15863 		} else {
15864 			/*
15865 			 * ifindex = 0 means, move everything pointing at
15866 			 * from_ill. We are doing this becuase ill has
15867 			 * either FAILED or became INACTIVE.
15868 			 *
15869 			 * As we would like to move things later back to
15870 			 * from_ill, we want to retain the identity of this
15871 			 * ilm. Thus, we don't blindly increment the reference
15872 			 * count on the ilms matching the address alone. We
15873 			 * need to match on the ilm_orig_index also. new_ilm
15874 			 * was obtained by matching ilm_orig_index also.
15875 			 */
15876 			if (new_ilm != NULL) {
15877 				/*
15878 				 * This is possible only if a previous restore
15879 				 * was incomplete i.e restore to
15880 				 * ilm_orig_ifindex left some ilms because
15881 				 * of some failures. Thus when we are failing
15882 				 * again, we might find our old friends there.
15883 				 */
15884 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
15885 				    " on %s, group address %s\n",
15886 				    to_ill->ill_name,
15887 				    inet_ntop(AF_INET6,
15888 				    &ilm->ilm_v6addr, buf,
15889 				    sizeof (buf))));
15890 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15891 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15892 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15893 					new_ilm->ilm_join_mld = B_TRUE;
15894 				}
15895 			} else {
15896 				if (from_ill->ill_ilm_walker_cnt != 0) {
15897 					new_ilm = (ilm_t *)
15898 					    mi_zalloc(sizeof (ilm_t));
15899 					if (new_ilm == NULL) {
15900 						ip0dbg(("ilm_move_v6: "
15901 						    "FAILOVER of IPv6"
15902 						    " multicast address %s : "
15903 						    "from %s to"
15904 						    " %s failed : ENOMEM \n",
15905 						    inet_ntop(AF_INET6,
15906 						    &ilm->ilm_v6addr, buf,
15907 						    sizeof (buf)),
15908 						    from_ill->ill_name,
15909 						    to_ill->ill_name));
15910 
15911 							ilmp = &ilm->ilm_next;
15912 							continue;
15913 					}
15914 					*new_ilm = *ilm;
15915 					new_ilm->ilm_filter = NULL;
15916 				} else {
15917 					*ilmp = ilm->ilm_next;
15918 					new_ilm = ilm;
15919 				}
15920 				/* Add to the to_ill's list */
15921 				new_ilm->ilm_next = to_ill->ill_ilm;
15922 				to_ill->ill_ilm = new_ilm;
15923 				ASSERT(ilm->ilm_ipif == NULL);
15924 				new_ilm->ilm_ill = to_ill;
15925 				new_ilm->ilm_join_mld = B_TRUE;
15926 				/*
15927 				 * If the to_ill has not joined this
15928 				 * group we need to tell the driver in
15929 				 * ill_send_multicast_reqs.
15930 				 */
15931 				if (ilm_lookup_ill_v6(to_ill,
15932 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15933 					new_ilm->ilm_notify_driver = B_TRUE;
15934 			}
15935 
15936 		}
15937 
15938 bottom:
15939 		/*
15940 		 * set ilm_send_multicast_reqs so that we inform the
15941 		 * driver about the multicast group.
15942 		 */
15943 		new_ilm->ilm_is_new = B_TRUE;
15944 		/*
15945 		 * Revert multicast filter state to (EXCLUDE, NULL).
15946 		 * new_ilm->ilm_join_mld should already be set if needed.
15947 		 */
15948 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
15949 		CLEAR_SLIST(new_ilm->ilm_filter);
15950 		/*
15951 		 * We allocated/got a new ilm, free the old one.
15952 		 */
15953 		if (new_ilm != ilm) {
15954 			if (from_ill->ill_ilm_walker_cnt == 0) {
15955 				*ilmp = ilm->ilm_next;
15956 				ilm->ilm_next = NULL;
15957 				FREE_SLIST(ilm->ilm_filter);
15958 				FREE_SLIST(ilm->ilm_pendsrcs);
15959 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
15960 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
15961 				mi_free((char *)ilm);
15962 			} else {
15963 				ilm->ilm_flags |= ILM_DELETED;
15964 				from_ill->ill_ilm_cleanup_reqd = 1;
15965 				ilmp = &ilm->ilm_next;
15966 			}
15967 		}
15968 	}
15969 }
15970 
15971 /*
15972  * Move all the multicast memberships to to_ill. Called when
15973  * an ipif moves from "from_ill" to "to_ill". This function is slightly
15974  * different from IPv6 counterpart as multicast memberships are associated
15975  * with ills in IPv6. This function is called after every ipif is moved
15976  * unlike IPv6, where it is moved only once.
15977  */
15978 static void
15979 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
15980 {
15981 	ilm_t	*ilm;
15982 	ilm_t	*ilm_next;
15983 	ilm_t	*new_ilm;
15984 	ilm_t	**ilmp;
15985 
15986 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15987 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15988 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15989 
15990 	ilmp = &from_ill->ill_ilm;
15991 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
15992 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
15993 		ilm_next = ilm->ilm_next;
15994 		ASSERT(ilm->ilm_ipif != NULL);
15995 
15996 		if (ilm->ilm_ipif != ipif) {
15997 			ilmp = &ilm->ilm_next;
15998 			continue;
15999 		}
16000 
16001 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16002 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16003 			/*
16004 			 * We joined this in ipif_multicast_up
16005 			 * and we never did an ipif_multicast_down
16006 			 * for IPv4. If nobody else from the userland
16007 			 * has reference, we free the ilm, and later
16008 			 * when this ipif comes up on the new ill,
16009 			 * we will join this again.
16010 			 */
16011 			if (--ilm->ilm_refcnt == 0)
16012 				goto delete_ilm;
16013 
16014 			new_ilm = ilm_lookup_ipif(ipif,
16015 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16016 			if (new_ilm != NULL) {
16017 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16018 				/*
16019 				 * We still need to deal with the from_ill.
16020 				 */
16021 				new_ilm->ilm_is_new = B_TRUE;
16022 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16023 				CLEAR_SLIST(new_ilm->ilm_filter);
16024 				goto delete_ilm;
16025 			}
16026 			/*
16027 			 * If we could not find one e.g. ipif is
16028 			 * still down on to_ill, we add this ilm
16029 			 * on ill_new to preserve the reference
16030 			 * count.
16031 			 */
16032 		}
16033 		/*
16034 		 * When ipifs move, ilms always move with it
16035 		 * to the NEW ill. Thus we should never be
16036 		 * able to find ilm till we really move it here.
16037 		 */
16038 		ASSERT(ilm_lookup_ipif(ipif,
16039 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16040 
16041 		if (from_ill->ill_ilm_walker_cnt != 0) {
16042 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16043 			if (new_ilm == NULL) {
16044 				char buf[INET6_ADDRSTRLEN];
16045 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16046 				    " multicast address %s : "
16047 				    "from %s to"
16048 				    " %s failed : ENOMEM \n",
16049 				    inet_ntop(AF_INET,
16050 				    &ilm->ilm_v6addr, buf,
16051 				    sizeof (buf)),
16052 				    from_ill->ill_name,
16053 				    to_ill->ill_name));
16054 
16055 				ilmp = &ilm->ilm_next;
16056 				continue;
16057 			}
16058 			*new_ilm = *ilm;
16059 			/* We don't want new_ilm linked to ilm's filter list */
16060 			new_ilm->ilm_filter = NULL;
16061 		} else {
16062 			/* Remove from the list */
16063 			*ilmp = ilm->ilm_next;
16064 			new_ilm = ilm;
16065 		}
16066 
16067 		/*
16068 		 * If we have never joined this group on the to_ill
16069 		 * make sure we tell the driver.
16070 		 */
16071 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16072 		    ALL_ZONES) == NULL)
16073 			new_ilm->ilm_notify_driver = B_TRUE;
16074 
16075 		/* Add to the to_ill's list */
16076 		new_ilm->ilm_next = to_ill->ill_ilm;
16077 		to_ill->ill_ilm = new_ilm;
16078 		new_ilm->ilm_is_new = B_TRUE;
16079 
16080 		/*
16081 		 * Revert multicast filter state to (EXCLUDE, NULL)
16082 		 */
16083 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16084 		CLEAR_SLIST(new_ilm->ilm_filter);
16085 
16086 		/*
16087 		 * Delete only if we have allocated a new ilm.
16088 		 */
16089 		if (new_ilm != ilm) {
16090 delete_ilm:
16091 			if (from_ill->ill_ilm_walker_cnt == 0) {
16092 				/* Remove from the list */
16093 				*ilmp = ilm->ilm_next;
16094 				ilm->ilm_next = NULL;
16095 				FREE_SLIST(ilm->ilm_filter);
16096 				FREE_SLIST(ilm->ilm_pendsrcs);
16097 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16098 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16099 				mi_free((char *)ilm);
16100 			} else {
16101 				ilm->ilm_flags |= ILM_DELETED;
16102 				from_ill->ill_ilm_cleanup_reqd = 1;
16103 				ilmp = &ilm->ilm_next;
16104 			}
16105 		}
16106 	}
16107 }
16108 
16109 static uint_t
16110 ipif_get_id(ill_t *ill, uint_t id)
16111 {
16112 	uint_t	unit;
16113 	ipif_t	*tipif;
16114 	boolean_t found = B_FALSE;
16115 
16116 	/*
16117 	 * During failback, we want to go back to the same id
16118 	 * instead of the smallest id so that the original
16119 	 * configuration is maintained. id is non-zero in that
16120 	 * case.
16121 	 */
16122 	if (id != 0) {
16123 		/*
16124 		 * While failing back, if we still have an ipif with
16125 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16126 		 * as soon as we return from this function. It was
16127 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16128 		 * we can choose the smallest id. Thus we return zero
16129 		 * in that case ignoring the hint.
16130 		 */
16131 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16132 			return (0);
16133 		for (tipif = ill->ill_ipif; tipif != NULL;
16134 		    tipif = tipif->ipif_next) {
16135 			if (tipif->ipif_id == id) {
16136 				found = B_TRUE;
16137 				break;
16138 			}
16139 		}
16140 		/*
16141 		 * If somebody already plumbed another logical
16142 		 * with the same id, we won't be able to find it.
16143 		 */
16144 		if (!found)
16145 			return (id);
16146 	}
16147 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16148 		found = B_FALSE;
16149 		for (tipif = ill->ill_ipif; tipif != NULL;
16150 		    tipif = tipif->ipif_next) {
16151 			if (tipif->ipif_id == unit) {
16152 				found = B_TRUE;
16153 				break;
16154 			}
16155 		}
16156 		if (!found)
16157 			break;
16158 	}
16159 	return (unit);
16160 }
16161 
16162 /* ARGSUSED */
16163 static int
16164 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16165     ipif_t **rep_ipif_ptr)
16166 {
16167 	ill_t	*from_ill;
16168 	ipif_t	*rep_ipif;
16169 	ipif_t	**ipifp;
16170 	uint_t	unit;
16171 	int err = 0;
16172 	ipif_t	*to_ipif;
16173 	struct iocblk	*iocp;
16174 	boolean_t failback_cmd;
16175 	boolean_t remove_ipif;
16176 	int	rc;
16177 
16178 	ASSERT(IAM_WRITER_ILL(to_ill));
16179 	ASSERT(IAM_WRITER_IPIF(ipif));
16180 
16181 	iocp = (struct iocblk *)mp->b_rptr;
16182 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16183 	remove_ipif = B_FALSE;
16184 
16185 	from_ill = ipif->ipif_ill;
16186 
16187 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16188 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16189 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16190 
16191 	/*
16192 	 * Don't move LINK LOCAL addresses as they are tied to
16193 	 * physical interface.
16194 	 */
16195 	if (from_ill->ill_isv6 &&
16196 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16197 		ipif->ipif_was_up = B_FALSE;
16198 		IPIF_UNMARK_MOVING(ipif);
16199 		return (0);
16200 	}
16201 
16202 	/*
16203 	 * We set the ipif_id to maximum so that the search for
16204 	 * ipif_id will pick the lowest number i.e 0 in the
16205 	 * following 2 cases :
16206 	 *
16207 	 * 1) We have a replacement ipif at the head of to_ill.
16208 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16209 	 *    on to_ill and hence the MOVE might fail. We want to
16210 	 *    remove it only if we could move the ipif. Thus, by
16211 	 *    setting it to the MAX value, we make the search in
16212 	 *    ipif_get_id return the zeroth id.
16213 	 *
16214 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16215 	 *    we might just have a zero address plumbed on the ipif
16216 	 *    with zero id in the case of IPv4. We remove that while
16217 	 *    doing the failback. We want to remove it only if we
16218 	 *    could move the ipif. Thus, by setting it to the MAX
16219 	 *    value, we make the search in ipif_get_id return the
16220 	 *    zeroth id.
16221 	 *
16222 	 * Both (1) and (2) are done only when when we are moving
16223 	 * an ipif (either due to failover/failback) which originally
16224 	 * belonged to this interface i.e the ipif_orig_ifindex is
16225 	 * the same as to_ill's ifindex. This is needed so that
16226 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16227 	 * from B -> A (B is being removed from the group) and
16228 	 * FAILBACK from A -> B restores the original configuration.
16229 	 * Without the check for orig_ifindex, the second FAILOVER
16230 	 * could make the ipif belonging to B replace the A's zeroth
16231 	 * ipif and the subsequent failback re-creating the replacement
16232 	 * ipif again.
16233 	 *
16234 	 * NOTE : We created the replacement ipif when we did a
16235 	 * FAILOVER (See below). We could check for FAILBACK and
16236 	 * then look for replacement ipif to be removed. But we don't
16237 	 * want to do that because we wan't to allow the possibility
16238 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16239 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16240 	 * from B -> A.
16241 	 */
16242 	to_ipif = to_ill->ill_ipif;
16243 	if ((to_ill->ill_phyint->phyint_ifindex ==
16244 	    ipif->ipif_orig_ifindex) &&
16245 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16246 		ASSERT(to_ipif->ipif_id == 0);
16247 		remove_ipif = B_TRUE;
16248 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16249 	}
16250 	/*
16251 	 * Find the lowest logical unit number on the to_ill.
16252 	 * If we are failing back, try to get the original id
16253 	 * rather than the lowest one so that the original
16254 	 * configuration is maintained.
16255 	 *
16256 	 * XXX need a better scheme for this.
16257 	 */
16258 	if (failback_cmd) {
16259 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16260 	} else {
16261 		unit = ipif_get_id(to_ill, 0);
16262 	}
16263 
16264 	/* Reset back to zero in case we fail below */
16265 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16266 		to_ipif->ipif_id = 0;
16267 
16268 	if (unit == ip_addrs_per_if) {
16269 		ipif->ipif_was_up = B_FALSE;
16270 		IPIF_UNMARK_MOVING(ipif);
16271 		return (EINVAL);
16272 	}
16273 
16274 	/*
16275 	 * ipif is ready to move from "from_ill" to "to_ill".
16276 	 *
16277 	 * 1) If we are moving ipif with id zero, create a
16278 	 *    replacement ipif for this ipif on from_ill. If this fails
16279 	 *    fail the MOVE operation.
16280 	 *
16281 	 * 2) Remove the replacement ipif on to_ill if any.
16282 	 *    We could remove the replacement ipif when we are moving
16283 	 *    the ipif with id zero. But what if somebody already
16284 	 *    unplumbed it ? Thus we always remove it if it is present.
16285 	 *    We want to do it only if we are sure we are going to
16286 	 *    move the ipif to to_ill which is why there are no
16287 	 *    returns due to error till ipif is linked to to_ill.
16288 	 *    Note that the first ipif that we failback will always
16289 	 *    be zero if it is present.
16290 	 */
16291 	if (ipif->ipif_id == 0) {
16292 		ipaddr_t inaddr_any = INADDR_ANY;
16293 
16294 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16295 		if (rep_ipif == NULL) {
16296 			ipif->ipif_was_up = B_FALSE;
16297 			IPIF_UNMARK_MOVING(ipif);
16298 			return (ENOMEM);
16299 		}
16300 		*rep_ipif = ipif_zero;
16301 		/*
16302 		 * Before we put the ipif on the list, store the addresses
16303 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16304 		 * assumes so. This logic is not any different from what
16305 		 * ipif_allocate does.
16306 		 */
16307 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16308 		    &rep_ipif->ipif_v6lcl_addr);
16309 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16310 		    &rep_ipif->ipif_v6src_addr);
16311 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16312 		    &rep_ipif->ipif_v6subnet);
16313 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16314 		    &rep_ipif->ipif_v6net_mask);
16315 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16316 		    &rep_ipif->ipif_v6brd_addr);
16317 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16318 		    &rep_ipif->ipif_v6pp_dst_addr);
16319 		/*
16320 		 * We mark IPIF_NOFAILOVER so that this can never
16321 		 * move.
16322 		 */
16323 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16324 		rep_ipif->ipif_flags &= ~IPIF_UP;
16325 		rep_ipif->ipif_replace_zero = B_TRUE;
16326 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16327 		    MUTEX_DEFAULT, NULL);
16328 		rep_ipif->ipif_id = 0;
16329 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16330 		rep_ipif->ipif_ill = from_ill;
16331 		rep_ipif->ipif_orig_ifindex =
16332 		    from_ill->ill_phyint->phyint_ifindex;
16333 		/* Insert at head */
16334 		rep_ipif->ipif_next = from_ill->ill_ipif;
16335 		from_ill->ill_ipif = rep_ipif;
16336 		/*
16337 		 * We don't really care to let apps know about
16338 		 * this interface.
16339 		 */
16340 	}
16341 
16342 	if (remove_ipif) {
16343 		/*
16344 		 * We set to a max value above for this case to get
16345 		 * id zero. ASSERT that we did get one.
16346 		 */
16347 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16348 		rep_ipif = to_ipif;
16349 		to_ill->ill_ipif = rep_ipif->ipif_next;
16350 		rep_ipif->ipif_next = NULL;
16351 		/*
16352 		 * If some apps scanned and find this interface,
16353 		 * it is time to let them know, so that they can
16354 		 * delete it.
16355 		 */
16356 
16357 		*rep_ipif_ptr = rep_ipif;
16358 	}
16359 
16360 	/* Get it out of the ILL interface list. */
16361 	ipifp = &ipif->ipif_ill->ill_ipif;
16362 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16363 		if (*ipifp == ipif) {
16364 			*ipifp = ipif->ipif_next;
16365 			break;
16366 		}
16367 	}
16368 
16369 	/* Assign the new ill */
16370 	ipif->ipif_ill = to_ill;
16371 	ipif->ipif_id = unit;
16372 	/* id has already been checked */
16373 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16374 	ASSERT(rc == 0);
16375 	/* Let SCTP update its list */
16376 	sctp_move_ipif(ipif, from_ill, to_ill);
16377 	/*
16378 	 * Handle the failover and failback of ipif_t between
16379 	 * ill_t that have differing maximum mtu values.
16380 	 */
16381 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16382 		if (ipif->ipif_saved_mtu == 0) {
16383 			/*
16384 			 * As this ipif_t is moving to an ill_t
16385 			 * that has a lower ill_max_mtu, its
16386 			 * ipif_mtu needs to be saved so it can
16387 			 * be restored during failback or during
16388 			 * failover to an ill_t which has a
16389 			 * higher ill_max_mtu.
16390 			 */
16391 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16392 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16393 		} else {
16394 			/*
16395 			 * The ipif_t is, once again, moving to
16396 			 * an ill_t that has a lower maximum mtu
16397 			 * value.
16398 			 */
16399 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16400 		}
16401 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16402 	    ipif->ipif_saved_mtu != 0) {
16403 		/*
16404 		 * The mtu of this ipif_t had to be reduced
16405 		 * during an earlier failover; this is an
16406 		 * opportunity for it to be increased (either as
16407 		 * part of another failover or a failback).
16408 		 */
16409 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16410 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16411 			ipif->ipif_saved_mtu = 0;
16412 		} else {
16413 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16414 		}
16415 	}
16416 
16417 	/*
16418 	 * We preserve all the other fields of the ipif including
16419 	 * ipif_saved_ire_mp. The routes that are saved here will
16420 	 * be recreated on the new interface and back on the old
16421 	 * interface when we move back.
16422 	 */
16423 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16424 
16425 	return (err);
16426 }
16427 
16428 static int
16429 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16430     int ifindex, ipif_t **rep_ipif_ptr)
16431 {
16432 	ipif_t *mipif;
16433 	ipif_t *ipif_next;
16434 	int err;
16435 
16436 	/*
16437 	 * We don't really try to MOVE back things if some of the
16438 	 * operations fail. The daemon will take care of moving again
16439 	 * later on.
16440 	 */
16441 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16442 		ipif_next = mipif->ipif_next;
16443 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16444 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16445 
16446 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16447 
16448 			/*
16449 			 * When the MOVE fails, it is the job of the
16450 			 * application to take care of this properly
16451 			 * i.e try again if it is ENOMEM.
16452 			 */
16453 			if (mipif->ipif_ill != from_ill) {
16454 				/*
16455 				 * ipif has moved.
16456 				 *
16457 				 * Move the multicast memberships associated
16458 				 * with this ipif to the new ill. For IPv6, we
16459 				 * do it once after all the ipifs are moved
16460 				 * (in ill_move) as they are not associated
16461 				 * with ipifs.
16462 				 *
16463 				 * We need to move the ilms as the ipif has
16464 				 * already been moved to a new ill even
16465 				 * in the case of errors. Neither
16466 				 * ilm_free(ipif) will find the ilm
16467 				 * when somebody unplumbs this ipif nor
16468 				 * ilm_delete(ilm) will be able to find the
16469 				 * ilm, if we don't move now.
16470 				 */
16471 				if (!from_ill->ill_isv6)
16472 					ilm_move_v4(from_ill, to_ill, mipif);
16473 			}
16474 
16475 			if (err != 0)
16476 				return (err);
16477 		}
16478 	}
16479 	return (0);
16480 }
16481 
16482 static int
16483 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16484 {
16485 	int ifindex;
16486 	int err;
16487 	struct iocblk	*iocp;
16488 	ipif_t	*ipif;
16489 	ipif_t *rep_ipif_ptr = NULL;
16490 	ipif_t	*from_ipif = NULL;
16491 	boolean_t check_rep_if = B_FALSE;
16492 
16493 	iocp = (struct iocblk *)mp->b_rptr;
16494 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16495 		/*
16496 		 * Move everything pointing at from_ill to to_ill.
16497 		 * We acheive this by passing in 0 as ifindex.
16498 		 */
16499 		ifindex = 0;
16500 	} else {
16501 		/*
16502 		 * Move everything pointing at from_ill whose original
16503 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16504 		 * We acheive this by passing in ifindex rather than 0.
16505 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16506 		 */
16507 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16508 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16509 	}
16510 
16511 	/*
16512 	 * Determine if there is at least one ipif that would move from
16513 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16514 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16515 	 * the move, in which case we need to quiesce the replacement ipif also.
16516 	 */
16517 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16518 	    from_ipif = from_ipif->ipif_next) {
16519 		if (((ifindex == 0) ||
16520 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16521 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16522 			check_rep_if = B_TRUE;
16523 			break;
16524 		}
16525 	}
16526 
16527 
16528 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16529 
16530 	GRAB_ILL_LOCKS(from_ill, to_ill);
16531 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16532 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16533 		    mp, ILL_MOVE_OK);
16534 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16535 		return (EINPROGRESS);
16536 	}
16537 
16538 	/* Check if the replacement ipif is quiescent to delete */
16539 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16540 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16541 		to_ill->ill_ipif->ipif_state_flags |=
16542 		    IPIF_MOVING | IPIF_CHANGING;
16543 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16544 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16545 			    mp, ILL_MOVE_OK);
16546 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16547 			return (EINPROGRESS);
16548 		}
16549 	}
16550 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16551 
16552 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16553 	rw_enter(&ill_g_lock, RW_WRITER);
16554 	GRAB_ILL_LOCKS(from_ill, to_ill);
16555 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16556 
16557 	/* ilm_move is done inside ipif_move for IPv4 */
16558 	if (err == 0 && from_ill->ill_isv6)
16559 		ilm_move_v6(from_ill, to_ill, ifindex);
16560 
16561 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16562 	rw_exit(&ill_g_lock);
16563 
16564 	/*
16565 	 * send rts messages and multicast messages.
16566 	 */
16567 	if (rep_ipif_ptr != NULL) {
16568 		ip_rts_ifmsg(rep_ipif_ptr);
16569 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16570 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16571 		mi_free(rep_ipif_ptr);
16572 	}
16573 
16574 	ilm_send_multicast_reqs(from_ill, to_ill);
16575 
16576 	conn_move_ill(from_ill, to_ill, ifindex);
16577 
16578 	return (err);
16579 }
16580 
16581 /*
16582  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16583  * Also checks for the validity of the arguments.
16584  * Note: We are already exclusive inside the from group.
16585  * It is upto the caller to release refcnt on the to_ill's.
16586  */
16587 static int
16588 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16589     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16590 {
16591 	int dst_index;
16592 	ipif_t *ipif_v4, *ipif_v6;
16593 	struct lifreq *lifr;
16594 	mblk_t *mp1;
16595 	boolean_t exists;
16596 	sin_t	*sin;
16597 	int	err = 0;
16598 
16599 	if ((mp1 = mp->b_cont) == NULL)
16600 		return (EPROTO);
16601 
16602 	if ((mp1 = mp1->b_cont) == NULL)
16603 		return (EPROTO);
16604 
16605 	lifr = (struct lifreq *)mp1->b_rptr;
16606 	sin = (sin_t *)&lifr->lifr_addr;
16607 
16608 	/*
16609 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16610 	 * specific operations.
16611 	 */
16612 	if (sin->sin_family != AF_UNSPEC)
16613 		return (EINVAL);
16614 
16615 	/*
16616 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16617 	 * NULLs for the last 4 args and we know the lookup won't fail
16618 	 * with EINPROGRESS.
16619 	 */
16620 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16621 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16622 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16623 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16624 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16625 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16626 
16627 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16628 		return (ENXIO);
16629 
16630 	if (ipif_v4 != NULL) {
16631 		ASSERT(ipif_v4->ipif_refcnt != 0);
16632 		if (ipif_v4->ipif_id != 0) {
16633 			err = EINVAL;
16634 			goto done;
16635 		}
16636 
16637 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16638 		*ill_from_v4 = ipif_v4->ipif_ill;
16639 	}
16640 
16641 	if (ipif_v6 != NULL) {
16642 		ASSERT(ipif_v6->ipif_refcnt != 0);
16643 		if (ipif_v6->ipif_id != 0) {
16644 			err = EINVAL;
16645 			goto done;
16646 		}
16647 
16648 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16649 		*ill_from_v6 = ipif_v6->ipif_ill;
16650 	}
16651 
16652 	err = 0;
16653 	dst_index = lifr->lifr_movetoindex;
16654 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16655 	    q, mp, ip_process_ioctl, &err);
16656 	if (err != 0) {
16657 		/*
16658 		 * There could be only v6.
16659 		 */
16660 		if (err != ENXIO)
16661 			goto done;
16662 		err = 0;
16663 	}
16664 
16665 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16666 	    q, mp, ip_process_ioctl, &err);
16667 	if (err != 0) {
16668 		if (err != ENXIO)
16669 			goto done;
16670 		if (*ill_to_v4 == NULL) {
16671 			err = ENXIO;
16672 			goto done;
16673 		}
16674 		err = 0;
16675 	}
16676 
16677 	/*
16678 	 * If we have something to MOVE i.e "from" not NULL,
16679 	 * "to" should be non-NULL.
16680 	 */
16681 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16682 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16683 		err = EINVAL;
16684 	}
16685 
16686 done:
16687 	if (ipif_v4 != NULL)
16688 		ipif_refrele(ipif_v4);
16689 	if (ipif_v6 != NULL)
16690 		ipif_refrele(ipif_v6);
16691 	return (err);
16692 }
16693 
16694 /*
16695  * FAILOVER and FAILBACK are modelled as MOVE operations.
16696  *
16697  * We don't check whether the MOVE is within the same group or
16698  * not, because this ioctl can be used as a generic mechanism
16699  * to failover from interface A to B, though things will function
16700  * only if they are really part of the same group. Moreover,
16701  * all ipifs may be down and hence temporarily out of the group.
16702  *
16703  * ipif's that need to be moved are first brought down; V4 ipifs are brought
16704  * down first and then V6.  For each we wait for the ipif's to become quiescent.
16705  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
16706  * have been deleted and there are no active references. Once quiescent the
16707  * ipif's are moved and brought up on the new ill.
16708  *
16709  * Normally the source ill and destination ill belong to the same IPMP group
16710  * and hence the same ipsq_t. In the event they don't belong to the same
16711  * same group the two ipsq's are first merged into one ipsq - that of the
16712  * to_ill. The multicast memberships on the source and destination ill cannot
16713  * change during the move operation since multicast joins/leaves also have to
16714  * execute on the same ipsq and are hence serialized.
16715  */
16716 /* ARGSUSED */
16717 int
16718 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16719     ip_ioctl_cmd_t *ipip, void *ifreq)
16720 {
16721 	ill_t *ill_to_v4 = NULL;
16722 	ill_t *ill_to_v6 = NULL;
16723 	ill_t *ill_from_v4 = NULL;
16724 	ill_t *ill_from_v6 = NULL;
16725 	int err = 0;
16726 
16727 	/*
16728 	 * setup from and to ill's, we can get EINPROGRESS only for
16729 	 * to_ill's.
16730 	 */
16731 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
16732 	    &ill_to_v4, &ill_to_v6);
16733 
16734 	if (err != 0) {
16735 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
16736 		goto done;
16737 	}
16738 
16739 	/*
16740 	 * nothing to do.
16741 	 */
16742 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
16743 		goto done;
16744 	}
16745 
16746 	/*
16747 	 * nothing to do.
16748 	 */
16749 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
16750 		goto done;
16751 	}
16752 
16753 	/*
16754 	 * Mark the ill as changing.
16755 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
16756 	 * in ill_up_ipifs in case of error they are cleared below.
16757 	 */
16758 
16759 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16760 	if (ill_from_v4 != NULL)
16761 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
16762 	if (ill_from_v6 != NULL)
16763 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
16764 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16765 
16766 	/*
16767 	 * Make sure that both src and dst are
16768 	 * in the same syncq group. If not make it happen.
16769 	 * We are not holding any locks because we are the writer
16770 	 * on the from_ipsq and we will hold locks in ill_merge_groups
16771 	 * to protect to_ipsq against changing.
16772 	 */
16773 	if (ill_from_v4 != NULL) {
16774 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
16775 		    ill_to_v4->ill_phyint->phyint_ipsq) {
16776 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
16777 			    NULL, mp, q);
16778 			goto err_ret;
16779 
16780 		}
16781 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
16782 	} else {
16783 
16784 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
16785 		    ill_to_v6->ill_phyint->phyint_ipsq) {
16786 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
16787 			    NULL, mp, q);
16788 			goto err_ret;
16789 
16790 		}
16791 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
16792 	}
16793 
16794 	/*
16795 	 * Now that the ipsq's have been merged and we are the writer
16796 	 * lets mark to_ill as changing as well.
16797 	 */
16798 
16799 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16800 	if (ill_to_v4 != NULL)
16801 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
16802 	if (ill_to_v6 != NULL)
16803 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
16804 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16805 
16806 	/*
16807 	 * Its ok for us to proceed with the move even if
16808 	 * ill_pending_mp is non null on one of the from ill's as the reply
16809 	 * should not be looking at the ipif, it should only care about the
16810 	 * ill itself.
16811 	 */
16812 
16813 	/*
16814 	 * lets move ipv4 first.
16815 	 */
16816 	if (ill_from_v4 != NULL) {
16817 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
16818 		ill_from_v4->ill_move_in_progress = B_TRUE;
16819 		ill_to_v4->ill_move_in_progress = B_TRUE;
16820 		ill_to_v4->ill_move_peer = ill_from_v4;
16821 		ill_from_v4->ill_move_peer = ill_to_v4;
16822 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
16823 	}
16824 
16825 	/*
16826 	 * Now lets move ipv6.
16827 	 */
16828 	if (err == 0 && ill_from_v6 != NULL) {
16829 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
16830 		ill_from_v6->ill_move_in_progress = B_TRUE;
16831 		ill_to_v6->ill_move_in_progress = B_TRUE;
16832 		ill_to_v6->ill_move_peer = ill_from_v6;
16833 		ill_from_v6->ill_move_peer = ill_to_v6;
16834 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
16835 	}
16836 
16837 err_ret:
16838 	if (err == 0)
16839 		goto no_err;
16840 	/*
16841 	 * EINPROGRESS means we are waiting for the ipif's that need to be
16842 	 * moved to become quiescent.
16843 	 */
16844 	if (err == EINPROGRESS) {
16845 		goto done;
16846 	}
16847 
16848 	/*
16849 	 * if err is set ill_up_ipifs will not be called
16850 	 * lets clear the flags.
16851 	 */
16852 
16853 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16854 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16855 	/*
16856 	 * Some of the clearing may be redundant. But it is simple
16857 	 * not making any extra checks.
16858 	 */
16859 	if (ill_from_v6 != NULL) {
16860 		ill_from_v6->ill_move_in_progress = B_FALSE;
16861 		ill_from_v6->ill_move_peer = NULL;
16862 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
16863 	}
16864 	if (ill_from_v4 != NULL) {
16865 		ill_from_v4->ill_move_in_progress = B_FALSE;
16866 		ill_from_v4->ill_move_peer = NULL;
16867 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
16868 	}
16869 	if (ill_to_v6 != NULL) {
16870 		ill_to_v6->ill_move_in_progress = B_FALSE;
16871 		ill_to_v6->ill_move_peer = NULL;
16872 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
16873 	}
16874 	if (ill_to_v4 != NULL) {
16875 		ill_to_v4->ill_move_in_progress = B_FALSE;
16876 		ill_to_v4->ill_move_peer = NULL;
16877 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
16878 	}
16879 	/*
16880 	 * Check for setting INACTIVE, if STANDBY is set. Do this always
16881 	 * to maintain proper state i.e even in the case of errors.
16882 	 * As phyint_standby_inactive looks at both v4 and v6 interfaces,
16883 	 * we need not call on both v4 and v6 interfaces.
16884 	 */
16885 	if (ill_from_v4 != NULL) {
16886 		if (ill_from_v4->ill_phyint->phyint_flags & PHYI_STANDBY) {
16887 			phyint_standby_inactive(ill_from_v4->ill_phyint);
16888 		}
16889 	} else if (ill_from_v6 != NULL) {
16890 		if (ill_from_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
16891 			phyint_standby_inactive(ill_from_v6->ill_phyint);
16892 		}
16893 	}
16894 
16895 	if (ill_to_v4 != NULL) {
16896 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_STANDBY) {
16897 			phyint_standby_inactive(ill_to_v4->ill_phyint);
16898 		}
16899 
16900 	} else if (ill_to_v6 != NULL) {
16901 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
16902 			phyint_standby_inactive(ill_to_v6->ill_phyint);
16903 		}
16904 	}
16905 
16906 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16907 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16908 
16909 no_err:
16910 	/*
16911 	 * lets bring the interfaces up on the to_ill.
16912 	 */
16913 	if (err == 0) {
16914 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
16915 		    q, mp);
16916 	}
16917 done:
16918 
16919 	if (ill_to_v4 != NULL) {
16920 		ill_refrele(ill_to_v4);
16921 	}
16922 	if (ill_to_v6 != NULL) {
16923 		ill_refrele(ill_to_v6);
16924 	}
16925 
16926 	return (err);
16927 }
16928 
16929 static void
16930 ill_dl_down(ill_t *ill)
16931 {
16932 	/*
16933 	 * The ill is down; unbind but stay attached since we're still
16934 	 * associated with a PPA.
16935 	 */
16936 	mblk_t	*mp = ill->ill_unbind_mp;
16937 
16938 	ill->ill_unbind_mp = NULL;
16939 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
16940 	if (mp != NULL) {
16941 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
16942 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
16943 		    ill->ill_name));
16944 		ill_dlpi_send(ill, mp);
16945 	}
16946 
16947 	/*
16948 	 * Toss all of our multicast memberships.  We could keep them, but
16949 	 * then we'd have to do bookkeeping of any joins and leaves performed
16950 	 * by the application while the the interface is down (we can't just
16951 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
16952 	 * on a downed interface).
16953 	 */
16954 	ill_leave_multicast(ill);
16955 
16956 	mutex_enter(&ill->ill_lock);
16957 	ill->ill_dl_up = 0;
16958 	mutex_exit(&ill->ill_lock);
16959 }
16960 
16961 void
16962 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
16963 {
16964 	union DL_primitives *dlp;
16965 	t_uscalar_t prim;
16966 
16967 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
16968 
16969 	dlp = (union DL_primitives *)mp->b_rptr;
16970 	prim = dlp->dl_primitive;
16971 
16972 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
16973 		dlpi_prim_str(prim), prim, ill->ill_name));
16974 
16975 	switch (prim) {
16976 	case DL_PHYS_ADDR_REQ:
16977 	{
16978 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
16979 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
16980 		break;
16981 	}
16982 	case DL_BIND_REQ:
16983 		mutex_enter(&ill->ill_lock);
16984 		ill->ill_state_flags &= ~ILL_DL_UNBIND_DONE;
16985 		mutex_exit(&ill->ill_lock);
16986 		break;
16987 	}
16988 
16989 	ill->ill_dlpi_pending = prim;
16990 
16991 	/*
16992 	 * Some drivers send M_FLUSH up to IP as part of unbind
16993 	 * request.  When this M_FLUSH is sent back to the driver,
16994 	 * this can go after we send the detach request if the
16995 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
16996 	 * to the M_FLUSH in ip_rput and locally generate another
16997 	 * M_FLUSH for the correctness.  This will get freed in
16998 	 * ip_wput_nondata.
16999 	 */
17000 	if (prim == DL_UNBIND_REQ)
17001 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17002 
17003 	putnext(ill->ill_wq, mp);
17004 }
17005 
17006 /*
17007  * Send a DLPI control message to the driver but make sure there
17008  * is only one outstanding message. Uses ill_dlpi_pending to tell
17009  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17010  * when an ACK or a NAK is received to process the next queued message.
17011  *
17012  * We don't protect ill_dlpi_pending with any lock. This is okay as
17013  * every place where its accessed, ip is exclusive while accessing
17014  * ill_dlpi_pending except when this function is called from ill_init()
17015  */
17016 void
17017 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17018 {
17019 	mblk_t **mpp;
17020 
17021 	ASSERT(IAM_WRITER_ILL(ill));
17022 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17023 
17024 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17025 		/* Must queue message. Tail insertion */
17026 		mpp = &ill->ill_dlpi_deferred;
17027 		while (*mpp != NULL)
17028 			mpp = &((*mpp)->b_next);
17029 
17030 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17031 		    ill->ill_name));
17032 
17033 		*mpp = mp;
17034 		return;
17035 	}
17036 
17037 	ill_dlpi_dispatch(ill, mp);
17038 }
17039 
17040 /*
17041  * Called when an DLPI control message has been acked or nacked to
17042  * send down the next queued message (if any).
17043  */
17044 void
17045 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17046 {
17047 	mblk_t *mp;
17048 
17049 	ASSERT(IAM_WRITER_ILL(ill));
17050 
17051 	ASSERT(prim != DL_PRIM_INVAL);
17052 	if (ill->ill_dlpi_pending != prim) {
17053 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17054 			(void) mi_strlog(ill->ill_rq, 1,
17055 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17056 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17057 			    dlpi_prim_str(prim), ill->ill_name);
17058 		} else {
17059 			(void) mi_strlog(ill->ill_rq, 1,
17060 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17061 			    "ill_dlpi_done: unexpected ack for %s from %s "
17062 			    "(expecting ack for %s)\n",
17063 			    dlpi_prim_str(prim), ill->ill_name,
17064 			    dlpi_prim_str(ill->ill_dlpi_pending));
17065 		}
17066 		return;
17067 	}
17068 
17069 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17070 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17071 
17072 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17073 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17074 		return;
17075 	}
17076 
17077 	ill->ill_dlpi_deferred = mp->b_next;
17078 	mp->b_next = NULL;
17079 
17080 	ill_dlpi_dispatch(ill, mp);
17081 }
17082 
17083 void
17084 conn_delete_ire(conn_t *connp, caddr_t arg)
17085 {
17086 	ipif_t	*ipif = (ipif_t *)arg;
17087 	ire_t	*ire;
17088 
17089 	/*
17090 	 * Look at the cached ires on conns which has pointers to ipifs.
17091 	 * We just call ire_refrele which clears up the reference
17092 	 * to ire. Called when a conn closes. Also called from ipif_free
17093 	 * to cleanup indirect references to the stale ipif via the cached ire.
17094 	 */
17095 	mutex_enter(&connp->conn_lock);
17096 	ire = connp->conn_ire_cache;
17097 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17098 		connp->conn_ire_cache = NULL;
17099 		mutex_exit(&connp->conn_lock);
17100 		IRE_REFRELE_NOTR(ire);
17101 		return;
17102 	}
17103 	mutex_exit(&connp->conn_lock);
17104 
17105 }
17106 
17107 /*
17108  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17109  * of IREs. Those IREs may have been previously cached in the conn structure.
17110  * This ipcl_walk() walker function releases all references to such IREs based
17111  * on the condemned flag.
17112  */
17113 /* ARGSUSED */
17114 void
17115 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17116 {
17117 	ire_t	*ire;
17118 
17119 	mutex_enter(&connp->conn_lock);
17120 	ire = connp->conn_ire_cache;
17121 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17122 		connp->conn_ire_cache = NULL;
17123 		mutex_exit(&connp->conn_lock);
17124 		IRE_REFRELE_NOTR(ire);
17125 		return;
17126 	}
17127 	mutex_exit(&connp->conn_lock);
17128 }
17129 
17130 /*
17131  * Take down a specific interface, but don't lose any information about it.
17132  * Also delete interface from its interface group (ifgrp).
17133  * (Always called as writer.)
17134  * This function goes through the down sequence even if the interface is
17135  * already down. There are 2 reasons.
17136  * a. Currently we permit interface routes that depend on down interfaces
17137  *    to be added. This behaviour itself is questionable. However it appears
17138  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17139  *    time. We go thru the cleanup in order to remove these routes.
17140  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17141  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17142  *    down, but we need to cleanup i.e. do ill_dl_down and
17143  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17144  *
17145  * IP-MT notes:
17146  *
17147  * Model of reference to interfaces.
17148  *
17149  * The following members in ipif_t track references to the ipif.
17150  *	int     ipif_refcnt;    Active reference count
17151  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17152  * The following members in ill_t track references to the ill.
17153  *	int             ill_refcnt;     active refcnt
17154  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17155  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17156  *
17157  * Reference to an ipif or ill can be obtained in any of the following ways.
17158  *
17159  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17160  * Pointers to ipif / ill from other data structures viz ire and conn.
17161  * Implicit reference to the ipif / ill by holding a reference to the ire.
17162  *
17163  * The ipif/ill lookup functions return a reference held ipif / ill.
17164  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17165  * This is a purely dynamic reference count associated with threads holding
17166  * references to the ipif / ill. Pointers from other structures do not
17167  * count towards this reference count.
17168  *
17169  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17170  * ipif/ill. This is incremented whenever a new ire is created referencing the
17171  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17172  * actually added to the ire hash table. The count is decremented in
17173  * ire_inactive where the ire is destroyed.
17174  *
17175  * nce's reference ill's thru nce_ill and the count of nce's associated with
17176  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17177  * ndp_add() where the nce is actually added to the table. Similarly it is
17178  * decremented in ndp_inactive where the nce is destroyed.
17179  *
17180  * Flow of ioctls involving interface down/up
17181  *
17182  * The following is the sequence of an attempt to set some critical flags on an
17183  * up interface.
17184  * ip_sioctl_flags
17185  * ipif_down
17186  * wait for ipif to be quiescent
17187  * ipif_down_tail
17188  * ip_sioctl_flags_tail
17189  *
17190  * All set ioctls that involve down/up sequence would have a skeleton similar
17191  * to the above. All the *tail functions are called after the refcounts have
17192  * dropped to the appropriate values.
17193  *
17194  * The mechanism to quiesce an ipif is as follows.
17195  *
17196  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17197  * on the ipif. Callers either pass a flag requesting wait or the lookup
17198  *  functions will return NULL.
17199  *
17200  * Delete all ires referencing this ipif
17201  *
17202  * Any thread attempting to do an ipif_refhold on an ipif that has been
17203  * obtained thru a cached pointer will first make sure that
17204  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17205  * increment the refcount.
17206  *
17207  * The above guarantees that the ipif refcount will eventually come down to
17208  * zero and the ipif will quiesce, once all threads that currently hold a
17209  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17210  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17211  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17212  * drop to zero.
17213  *
17214  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17215  *
17216  * Threads trying to lookup an ipif or ill can pass a flag requesting
17217  * wait and restart if the ipif / ill cannot be looked up currently.
17218  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17219  * failure if the ipif is currently undergoing an exclusive operation, and
17220  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17221  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17222  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17223  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17224  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17225  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17226  * until we release the ipsq_lock, even though the the ill/ipif state flags
17227  * can change after we drop the ill_lock.
17228  *
17229  * An attempt to send out a packet using an ipif that is currently
17230  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17231  * operation and restart it later when the exclusive condition on the ipif ends.
17232  * This is an example of not passing the wait flag to the lookup functions. For
17233  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17234  * out a multicast packet on that ipif will fail while the ipif is
17235  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17236  * currently IPIF_CHANGING will also fail.
17237  */
17238 int
17239 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17240 {
17241 	ill_t		*ill = ipif->ipif_ill;
17242 	phyint_t	*phyi;
17243 	conn_t		*connp;
17244 	boolean_t	success;
17245 	boolean_t	ipif_was_up = B_FALSE;
17246 
17247 	ASSERT(IAM_WRITER_IPIF(ipif));
17248 
17249 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17250 
17251 	if (ipif->ipif_flags & IPIF_UP) {
17252 		mutex_enter(&ill->ill_lock);
17253 		ipif->ipif_flags &= ~IPIF_UP;
17254 		ASSERT(ill->ill_ipif_up_count > 0);
17255 		--ill->ill_ipif_up_count;
17256 		mutex_exit(&ill->ill_lock);
17257 		ipif_was_up = B_TRUE;
17258 		/* Update status in SCTP's list */
17259 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17260 	}
17261 
17262 	/*
17263 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17264 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17265 	 * know not to rejoin when the interface is brought back up).
17266 	 */
17267 	if (ipif->ipif_isv6)
17268 		ipif_multicast_down(ipif);
17269 	/*
17270 	 * Remove from the mapping for __sin6_src_id. We insert only
17271 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17272 	 * stored as mapped addresses, we need to check for mapped
17273 	 * INADDR_ANY also.
17274 	 */
17275 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17276 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17277 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17278 		int err;
17279 
17280 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17281 		    ipif->ipif_zoneid);
17282 		if (err != 0) {
17283 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17284 		}
17285 	}
17286 
17287 	/*
17288 	 * Before we delete the ill from the group (if any), we need
17289 	 * to make sure that we delete all the routes dependent on
17290 	 * this and also any ipifs dependent on this ipif for
17291 	 * source address. We need to do before we delete from
17292 	 * the group because
17293 	 *
17294 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17295 	 *
17296 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17297 	 *    for re-doing source address selection. Note that
17298 	 *    ipif_select_source[_v6] called from
17299 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17300 	 *    because we have already marked down here i.e cleared
17301 	 *    IPIF_UP.
17302 	 */
17303 	if (ipif->ipif_isv6)
17304 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17305 	else
17306 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17307 
17308 	/*
17309 	 * Need to add these also to be saved and restored when the
17310 	 * ipif is brought down and up
17311 	 */
17312 	mutex_enter(&ire_mrtun_lock);
17313 	if (ire_mrtun_count != 0) {
17314 		mutex_exit(&ire_mrtun_lock);
17315 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17316 		    (char *)ipif, NULL);
17317 	} else {
17318 		mutex_exit(&ire_mrtun_lock);
17319 	}
17320 
17321 	mutex_enter(&ire_srcif_table_lock);
17322 	if (ire_srcif_table_count > 0) {
17323 		mutex_exit(&ire_srcif_table_lock);
17324 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17325 	} else {
17326 		mutex_exit(&ire_srcif_table_lock);
17327 	}
17328 
17329 	/*
17330 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17331 	 * ires have been deleted above. Otherwise a thread could end up
17332 	 * caching an ire in a conn after we have finished the cleanup of the
17333 	 * conn. The caching is done after making sure that the ire is not yet
17334 	 * condemned. Also documented in the block comment above ip_output
17335 	 */
17336 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17337 	/* Also, delete the ires cached in SCTP */
17338 	sctp_ire_cache_flush(ipif);
17339 
17340 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17341 	nattymod_clean_ipif(ipif);
17342 
17343 	/*
17344 	 * Update any other ipifs which have used "our" local address as
17345 	 * a source address. This entails removing and recreating IRE_INTERFACE
17346 	 * entries for such ipifs.
17347 	 */
17348 	if (ipif->ipif_isv6)
17349 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17350 	else
17351 		ipif_update_other_ipifs(ipif, ill->ill_group);
17352 
17353 	if (ipif_was_up) {
17354 		/*
17355 		 * Check whether it is last ipif to leave this group.
17356 		 * If this is the last ipif to leave, we should remove
17357 		 * this ill from the group as ipif_select_source will not
17358 		 * be able to find any useful ipifs if this ill is selected
17359 		 * for load balancing.
17360 		 *
17361 		 * For nameless groups, we should call ifgrp_delete if this
17362 		 * belongs to some group. As this ipif is going down, we may
17363 		 * need to reconstruct groups.
17364 		 */
17365 		phyi = ill->ill_phyint;
17366 		/*
17367 		 * If the phyint_groupname_len is 0, it may or may not
17368 		 * be in the nameless group. If the phyint_groupname_len is
17369 		 * not 0, then this ill should be part of some group.
17370 		 * As we always insert this ill in the group if
17371 		 * phyint_groupname_len is not zero when the first ipif
17372 		 * comes up (in ipif_up_done), it should be in a group
17373 		 * when the namelen is not 0.
17374 		 *
17375 		 * NOTE : When we delete the ill from the group,it will
17376 		 * blow away all the IRE_CACHES pointing either at this ipif or
17377 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17378 		 * should be pointing at this ill.
17379 		 */
17380 		ASSERT(phyi->phyint_groupname_len == 0 ||
17381 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17382 
17383 		if (phyi->phyint_groupname_len != 0) {
17384 			if (ill->ill_ipif_up_count == 0)
17385 				illgrp_delete(ill);
17386 		}
17387 
17388 		/*
17389 		 * If we have deleted some of the broadcast ires associated
17390 		 * with this ipif, we need to re-nominate somebody else if
17391 		 * the ires that we deleted were the nominated ones.
17392 		 */
17393 		if (ill->ill_group != NULL && !ill->ill_isv6)
17394 			ipif_renominate_bcast(ipif);
17395 	}
17396 
17397 	if (ipif->ipif_isv6)
17398 		ipif_ndp_down(ipif);
17399 
17400 	/*
17401 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17402 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17403 	 * and ill_delete -> ipif_free -> ipif_down
17404 	 */
17405 	if (mp == NULL) {
17406 		ASSERT(q == NULL);
17407 		return (0);
17408 	}
17409 
17410 	if (CONN_Q(q)) {
17411 		connp = Q_TO_CONN(q);
17412 		mutex_enter(&connp->conn_lock);
17413 	} else {
17414 		connp = NULL;
17415 	}
17416 	mutex_enter(&ill->ill_lock);
17417 	/*
17418 	 * Are there any ire's pointing to this ipif that are still active ?
17419 	 * If this is the last ipif going down, are there any ire's pointing
17420 	 * to this ill that are still active ?
17421 	 */
17422 	if (ipif_is_quiescent(ipif)) {
17423 		mutex_exit(&ill->ill_lock);
17424 		if (connp != NULL)
17425 			mutex_exit(&connp->conn_lock);
17426 		return (0);
17427 	}
17428 
17429 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17430 	    ill->ill_name, (void *)ill));
17431 	/*
17432 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17433 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17434 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17435 	 */
17436 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17437 	if (!success) {
17438 		/* The conn is closing. So just return */
17439 		ASSERT(connp != NULL);
17440 		mutex_exit(&ill->ill_lock);
17441 		mutex_exit(&connp->conn_lock);
17442 		return (EINTR);
17443 	}
17444 
17445 	mutex_exit(&ill->ill_lock);
17446 	if (connp != NULL)
17447 		mutex_exit(&connp->conn_lock);
17448 	return (EINPROGRESS);
17449 }
17450 
17451 static void
17452 ipif_down_tail(ipif_t *ipif)
17453 {
17454 	ill_t	*ill = ipif->ipif_ill;
17455 
17456 	/*
17457 	 * Skip any loopback interface (null wq).
17458 	 * If this is the last logical interface on the ill
17459 	 * have ill_dl_down tell the driver we are gone (unbind)
17460 	 * Note that lun 0 can ipif_down even though
17461 	 * there are other logical units that are up.
17462 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17463 	 */
17464 	if (ipif->ipif_ill->ill_wq != NULL) {
17465 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17466 		    ill->ill_dl_up) {
17467 			ill_dl_down(ill);
17468 		}
17469 	}
17470 	ill->ill_logical_down = 0;
17471 
17472 	/*
17473 	 * Have to be after removing the routes in ipif_down_delete_ire.
17474 	 */
17475 	if (ipif->ipif_isv6) {
17476 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17477 			ipif_arp_down(ipif);
17478 	} else {
17479 		ipif_arp_down(ipif);
17480 	}
17481 
17482 	ip_rts_ifmsg(ipif);
17483 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17484 }
17485 
17486 /*
17487  * Bring interface logically down without bringing the physical interface
17488  * down e.g. when the netmask is changed. This avoids long lasting link
17489  * negotiations between an ethernet interface and a certain switches.
17490  */
17491 static int
17492 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17493 {
17494 	/*
17495 	 * The ill_logical_down flag is a transient flag. It is set here
17496 	 * and is cleared once the down has completed in ipif_down_tail.
17497 	 * This flag does not indicate whether the ill stream is in the
17498 	 * DL_BOUND state with the driver. Instead this flag is used by
17499 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17500 	 * the driver. The state of the ill stream i.e. whether it is
17501 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17502 	 */
17503 	ipif->ipif_ill->ill_logical_down = 1;
17504 	return (ipif_down(ipif, q, mp));
17505 }
17506 
17507 /*
17508  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17509  * If the usesrc client ILL is already part of a usesrc group or not,
17510  * in either case a ire_stq with the matching usesrc client ILL will
17511  * locate the IRE's that need to be deleted. We want IREs to be created
17512  * with the new source address.
17513  */
17514 static void
17515 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17516 {
17517 	ill_t	*ucill = (ill_t *)ill_arg;
17518 
17519 	ASSERT(IAM_WRITER_ILL(ucill));
17520 
17521 	if (ire->ire_stq == NULL)
17522 		return;
17523 
17524 	if ((ire->ire_type == IRE_CACHE) &&
17525 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17526 		ire_delete(ire);
17527 }
17528 
17529 /*
17530  * ire_walk routine to delete every IRE dependent on the interface
17531  * address that is going down.	(Always called as writer.)
17532  * Works for both v4 and v6.
17533  * In addition for checking for ire_ipif matches it also checks for
17534  * IRE_CACHE entries which have the same source address as the
17535  * disappearing ipif since ipif_select_source might have picked
17536  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17537  * care of any IRE_INTERFACE with the disappearing source address.
17538  */
17539 static void
17540 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17541 {
17542 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17543 	ill_t *ire_ill;
17544 	ill_t *ipif_ill;
17545 
17546 	ASSERT(IAM_WRITER_IPIF(ipif));
17547 	if (ire->ire_ipif == NULL)
17548 		return;
17549 
17550 	/*
17551 	 * For IPv4, we derive source addresses for an IRE from ipif's
17552 	 * belonging to the same IPMP group as the IRE's outgoing
17553 	 * interface.  If an IRE's outgoing interface isn't in the
17554 	 * same IPMP group as a particular ipif, then that ipif
17555 	 * couldn't have been used as a source address for this IRE.
17556 	 *
17557 	 * For IPv6, source addresses are only restricted to the IPMP group
17558 	 * if the IRE is for a link-local address or a multicast address.
17559 	 * Otherwise, source addresses for an IRE can be chosen from
17560 	 * interfaces other than the the outgoing interface for that IRE.
17561 	 *
17562 	 * For source address selection details, see ipif_select_source()
17563 	 * and ipif_select_source_v6().
17564 	 */
17565 	if (ire->ire_ipversion == IPV4_VERSION ||
17566 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17567 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17568 		ire_ill = ire->ire_ipif->ipif_ill;
17569 		ipif_ill = ipif->ipif_ill;
17570 
17571 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17572 			return;
17573 		}
17574 	}
17575 
17576 
17577 	if (ire->ire_ipif != ipif) {
17578 		/*
17579 		 * Look for a matching source address.
17580 		 */
17581 		if (ire->ire_type != IRE_CACHE)
17582 			return;
17583 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17584 			return;
17585 
17586 		if (ire->ire_ipversion == IPV4_VERSION) {
17587 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17588 				return;
17589 		} else {
17590 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17591 			    &ipif->ipif_v6lcl_addr))
17592 				return;
17593 		}
17594 		ire_delete(ire);
17595 		return;
17596 	}
17597 	/*
17598 	 * ire_delete() will do an ire_flush_cache which will delete
17599 	 * all ire_ipif matches
17600 	 */
17601 	ire_delete(ire);
17602 }
17603 
17604 /*
17605  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17606  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17607  * 2) when an interface is brought up or down (on that ill).
17608  * This ensures that the IRE_CACHE entries don't retain stale source
17609  * address selection results.
17610  */
17611 void
17612 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17613 {
17614 	ill_t	*ill = (ill_t *)ill_arg;
17615 	ill_t	*ipif_ill;
17616 
17617 	ASSERT(IAM_WRITER_ILL(ill));
17618 	/*
17619 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17620 	 * Hence this should be IRE_CACHE.
17621 	 */
17622 	ASSERT(ire->ire_type == IRE_CACHE);
17623 
17624 	/*
17625 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17626 	 * We are only interested in IRE_CACHES that has borrowed
17627 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17628 	 * for which we need to look at ire_ipif->ipif_ill match
17629 	 * with ill.
17630 	 */
17631 	ASSERT(ire->ire_ipif != NULL);
17632 	ipif_ill = ire->ire_ipif->ipif_ill;
17633 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17634 	    ipif_ill->ill_group == ill->ill_group)) {
17635 		ire_delete(ire);
17636 	}
17637 }
17638 
17639 /*
17640  * Delete all the ire whose stq references ill_arg.
17641  */
17642 static void
17643 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17644 {
17645 	ill_t	*ill = (ill_t *)ill_arg;
17646 	ill_t	*ire_ill;
17647 
17648 	ASSERT(IAM_WRITER_ILL(ill));
17649 	/*
17650 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17651 	 * Hence this should be IRE_CACHE.
17652 	 */
17653 	ASSERT(ire->ire_type == IRE_CACHE);
17654 
17655 	/*
17656 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17657 	 * matches ill. We are only interested in IRE_CACHES that
17658 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17659 	 * filtering here.
17660 	 */
17661 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17662 
17663 	if (ire_ill == ill)
17664 		ire_delete(ire);
17665 }
17666 
17667 /*
17668  * This is called when an ill leaves the group. We want to delete
17669  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17670  * pointing at ill.
17671  */
17672 static void
17673 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17674 {
17675 	ill_t	*ill = (ill_t *)ill_arg;
17676 
17677 	ASSERT(IAM_WRITER_ILL(ill));
17678 	ASSERT(ill->ill_group == NULL);
17679 	/*
17680 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17681 	 * Hence this should be IRE_CACHE.
17682 	 */
17683 	ASSERT(ire->ire_type == IRE_CACHE);
17684 	/*
17685 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17686 	 * matches ill. We are interested in both.
17687 	 */
17688 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
17689 	    (ire->ire_ipif->ipif_ill == ill));
17690 
17691 	ire_delete(ire);
17692 }
17693 
17694 /*
17695  * Initiate deallocate of an IPIF. Always called as writer. Called by
17696  * ill_delete or ip_sioctl_removeif.
17697  */
17698 static void
17699 ipif_free(ipif_t *ipif)
17700 {
17701 	ASSERT(IAM_WRITER_IPIF(ipif));
17702 
17703 	/* Remove conn references */
17704 	reset_conn_ipif(ipif);
17705 
17706 	/*
17707 	 * Make sure we have valid net and subnet broadcast ire's for the
17708 	 * other ipif's which share them with this ipif.
17709 	 */
17710 	if (!ipif->ipif_isv6)
17711 		ipif_check_bcast_ires(ipif);
17712 
17713 	/*
17714 	 * Take down the interface. We can be called either from ill_delete
17715 	 * or from ip_sioctl_removeif.
17716 	 */
17717 	(void) ipif_down(ipif, NULL, NULL);
17718 
17719 	rw_enter(&ill_g_lock, RW_WRITER);
17720 	/* Remove pointers to this ill in the multicast routing tables */
17721 	reset_mrt_vif_ipif(ipif);
17722 	rw_exit(&ill_g_lock);
17723 }
17724 
17725 static void
17726 ipif_free_tail(ipif_t *ipif)
17727 {
17728 	mblk_t	*mp;
17729 	ipif_t	**ipifp;
17730 
17731 	/*
17732 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
17733 	 */
17734 	mutex_enter(&ipif->ipif_saved_ire_lock);
17735 	mp = ipif->ipif_saved_ire_mp;
17736 	ipif->ipif_saved_ire_mp = NULL;
17737 	mutex_exit(&ipif->ipif_saved_ire_lock);
17738 	freemsg(mp);
17739 
17740 	/*
17741 	 * Need to hold both ill_g_lock and ill_lock while
17742 	 * inserting or removing an ipif from the linked list
17743 	 * of ipifs hanging off the ill.
17744 	 */
17745 	rw_enter(&ill_g_lock, RW_WRITER);
17746 	/*
17747 	 * Remove all multicast memberships on the interface now.
17748 	 * This removes IPv4 multicast memberships joined within
17749 	 * the kernel as ipif_down does not do ipif_multicast_down
17750 	 * for IPv4. IPv6 is not handled here as the multicast memberships
17751 	 * are based on ill and not on ipif.
17752 	 */
17753 	ilm_free(ipif);
17754 
17755 	/*
17756 	 * Since we held the ill_g_lock while doing the ilm_free above,
17757 	 * we can assert the ilms were really deleted and not just marked
17758 	 * ILM_DELETED.
17759 	 */
17760 	ASSERT(ilm_walk_ipif(ipif) == 0);
17761 
17762 
17763 	IPIF_TRACE_CLEANUP(ipif);
17764 
17765 	/* Ask SCTP to take it out of it list */
17766 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
17767 
17768 	mutex_enter(&ipif->ipif_ill->ill_lock);
17769 	/* Get it out of the ILL interface list. */
17770 	ipifp = &ipif->ipif_ill->ill_ipif;
17771 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17772 		if (*ipifp == ipif) {
17773 			*ipifp = ipif->ipif_next;
17774 			break;
17775 		}
17776 	}
17777 
17778 	mutex_exit(&ipif->ipif_ill->ill_lock);
17779 	rw_exit(&ill_g_lock);
17780 
17781 	mutex_destroy(&ipif->ipif_saved_ire_lock);
17782 	/* Free the memory. */
17783 	mi_free((char *)ipif);
17784 }
17785 
17786 /*
17787  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
17788  * "ill_name" otherwise.
17789  */
17790 char *
17791 ipif_get_name(ipif_t *ipif, char *buf, int len)
17792 {
17793 	char	lbuf[32];
17794 	char	*name;
17795 	size_t	name_len;
17796 
17797 	buf[0] = '\0';
17798 	if (!ipif)
17799 		return (buf);
17800 	name = ipif->ipif_ill->ill_name;
17801 	name_len = ipif->ipif_ill->ill_name_length;
17802 	if (ipif->ipif_id != 0) {
17803 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
17804 		    ipif->ipif_id);
17805 		name = lbuf;
17806 		name_len = mi_strlen(name) + 1;
17807 	}
17808 	len -= 1;
17809 	buf[len] = '\0';
17810 	len = MIN(len, name_len);
17811 	bcopy(name, buf, len);
17812 	return (buf);
17813 }
17814 
17815 /*
17816  * Find an IPIF based on the name passed in.  Names can be of the
17817  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
17818  * The <phys> string can have forms like <dev><#> (e.g., le0),
17819  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
17820  * When there is no colon, the implied unit id is zero. <phys> must
17821  * correspond to the name of an ILL.  (May be called as writer.)
17822  */
17823 static ipif_t *
17824 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
17825     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
17826     mblk_t *mp, ipsq_func_t func, int *error)
17827 {
17828 	char	*cp;
17829 	char	*endp;
17830 	long	id;
17831 	ill_t	*ill;
17832 	ipif_t	*ipif;
17833 	uint_t	ire_type;
17834 	boolean_t did_alloc = B_FALSE;
17835 	ipsq_t	*ipsq;
17836 
17837 	if (error != NULL)
17838 		*error = 0;
17839 
17840 	/*
17841 	 * If the caller wants to us to create the ipif, make sure we have a
17842 	 * valid zoneid
17843 	 */
17844 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
17845 
17846 	if (namelen == 0) {
17847 		if (error != NULL)
17848 			*error = ENXIO;
17849 		return (NULL);
17850 	}
17851 
17852 	*exists = B_FALSE;
17853 	/* Look for a colon in the name. */
17854 	endp = &name[namelen];
17855 	for (cp = endp; --cp > name; ) {
17856 		if (*cp == IPIF_SEPARATOR_CHAR)
17857 			break;
17858 	}
17859 
17860 	if (*cp == IPIF_SEPARATOR_CHAR) {
17861 		/*
17862 		 * Reject any non-decimal aliases for logical
17863 		 * interfaces. Aliases with leading zeroes
17864 		 * are also rejected as they introduce ambiguity
17865 		 * in the naming of the interfaces.
17866 		 * In order to confirm with existing semantics,
17867 		 * and to not break any programs/script relying
17868 		 * on that behaviour, if<0>:0 is considered to be
17869 		 * a valid interface.
17870 		 *
17871 		 * If alias has two or more digits and the first
17872 		 * is zero, fail.
17873 		 */
17874 		if (&cp[2] < endp && cp[1] == '0')
17875 			return (NULL);
17876 	}
17877 
17878 	if (cp <= name) {
17879 		cp = endp;
17880 	} else {
17881 		*cp = '\0';
17882 	}
17883 
17884 	/*
17885 	 * Look up the ILL, based on the portion of the name
17886 	 * before the slash. ill_lookup_on_name returns a held ill.
17887 	 * Temporary to check whether ill exists already. If so
17888 	 * ill_lookup_on_name will clear it.
17889 	 */
17890 	ill = ill_lookup_on_name(name, do_alloc, isv6,
17891 	    q, mp, func, error, &did_alloc);
17892 	if (cp != endp)
17893 		*cp = IPIF_SEPARATOR_CHAR;
17894 	if (ill == NULL)
17895 		return (NULL);
17896 
17897 	/* Establish the unit number in the name. */
17898 	id = 0;
17899 	if (cp < endp && *endp == '\0') {
17900 		/* If there was a colon, the unit number follows. */
17901 		cp++;
17902 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
17903 			ill_refrele(ill);
17904 			if (error != NULL)
17905 				*error = ENXIO;
17906 			return (NULL);
17907 		}
17908 	}
17909 
17910 	GRAB_CONN_LOCK(q);
17911 	mutex_enter(&ill->ill_lock);
17912 	/* Now see if there is an IPIF with this unit number. */
17913 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
17914 		if (ipif->ipif_id == id) {
17915 			if (zoneid != ALL_ZONES &&
17916 			    zoneid != ipif->ipif_zoneid) {
17917 				mutex_exit(&ill->ill_lock);
17918 				RELEASE_CONN_LOCK(q);
17919 				ill_refrele(ill);
17920 				if (error != NULL)
17921 					*error = ENXIO;
17922 				return (NULL);
17923 			}
17924 			/*
17925 			 * The block comment at the start of ipif_down
17926 			 * explains the use of the macros used below
17927 			 */
17928 			if (IPIF_CAN_LOOKUP(ipif)) {
17929 				ipif_refhold_locked(ipif);
17930 				mutex_exit(&ill->ill_lock);
17931 				if (!did_alloc)
17932 					*exists = B_TRUE;
17933 				/*
17934 				 * Drop locks before calling ill_refrele
17935 				 * since it can potentially call into
17936 				 * ipif_ill_refrele_tail which can end up
17937 				 * in trying to acquire any lock.
17938 				 */
17939 				RELEASE_CONN_LOCK(q);
17940 				ill_refrele(ill);
17941 				return (ipif);
17942 			} else if (IPIF_CAN_WAIT(ipif, q)) {
17943 				ipsq = ill->ill_phyint->phyint_ipsq;
17944 				mutex_enter(&ipsq->ipsq_lock);
17945 				mutex_exit(&ill->ill_lock);
17946 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
17947 				mutex_exit(&ipsq->ipsq_lock);
17948 				RELEASE_CONN_LOCK(q);
17949 				ill_refrele(ill);
17950 				*error = EINPROGRESS;
17951 				return (NULL);
17952 			}
17953 		}
17954 	}
17955 	RELEASE_CONN_LOCK(q);
17956 
17957 	if (!do_alloc) {
17958 		mutex_exit(&ill->ill_lock);
17959 		ill_refrele(ill);
17960 		if (error != NULL)
17961 			*error = ENXIO;
17962 		return (NULL);
17963 	}
17964 
17965 	/*
17966 	 * If none found, atomically allocate and return a new one.
17967 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
17968 	 * to support "receive only" use of lo0:1 etc. as is still done
17969 	 * below as an initial guess.
17970 	 * However, this is now likely to be overriden later in ipif_up_done()
17971 	 * when we know for sure what address has been configured on the
17972 	 * interface, since we might have more than one loopback interface
17973 	 * with a loopback address, e.g. in the case of zones, and all the
17974 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
17975 	 */
17976 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
17977 		ire_type = IRE_LOOPBACK;
17978 	else
17979 		ire_type = IRE_LOCAL;
17980 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
17981 	if (ipif != NULL)
17982 		ipif_refhold_locked(ipif);
17983 	else if (error != NULL)
17984 		*error = ENOMEM;
17985 	mutex_exit(&ill->ill_lock);
17986 	ill_refrele(ill);
17987 	return (ipif);
17988 }
17989 
17990 /*
17991  * This routine is called whenever a new address comes up on an ipif.  If
17992  * we are configured to respond to address mask requests, then we are supposed
17993  * to broadcast an address mask reply at this time.  This routine is also
17994  * called if we are already up, but a netmask change is made.  This is legal
17995  * but might not make the system manager very popular.	(May be called
17996  * as writer.)
17997  */
17998 static void
17999 ipif_mask_reply(ipif_t *ipif)
18000 {
18001 	icmph_t	*icmph;
18002 	ipha_t	*ipha;
18003 	mblk_t	*mp;
18004 
18005 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18006 
18007 	if (!ip_respond_to_address_mask_broadcast)
18008 		return;
18009 
18010 	/* ICMP mask reply is IPv4 only */
18011 	ASSERT(!ipif->ipif_isv6);
18012 	/* ICMP mask reply is not for a loopback interface */
18013 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18014 
18015 	mp = allocb(REPLY_LEN, BPRI_HI);
18016 	if (mp == NULL)
18017 		return;
18018 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18019 
18020 	ipha = (ipha_t *)mp->b_rptr;
18021 	bzero(ipha, REPLY_LEN);
18022 	*ipha = icmp_ipha;
18023 	ipha->ipha_ttl = ip_broadcast_ttl;
18024 	ipha->ipha_src = ipif->ipif_src_addr;
18025 	ipha->ipha_dst = ipif->ipif_brd_addr;
18026 	ipha->ipha_length = htons(REPLY_LEN);
18027 	ipha->ipha_ident = 0;
18028 
18029 	icmph = (icmph_t *)&ipha[1];
18030 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18031 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18032 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18033 
18034 	put(ipif->ipif_wq, mp);
18035 
18036 #undef	REPLY_LEN
18037 }
18038 
18039 /*
18040  * When the mtu in the ipif changes, we call this routine through ire_walk
18041  * to update all the relevant IREs.
18042  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18043  */
18044 static void
18045 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18046 {
18047 	ipif_t *ipif = (ipif_t *)ipif_arg;
18048 
18049 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18050 		return;
18051 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18052 }
18053 
18054 /*
18055  * When the mtu in the ill changes, we call this routine through ire_walk
18056  * to update all the relevant IREs.
18057  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18058  */
18059 void
18060 ill_mtu_change(ire_t *ire, char *ill_arg)
18061 {
18062 	ill_t	*ill = (ill_t *)ill_arg;
18063 
18064 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18065 		return;
18066 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18067 }
18068 
18069 /*
18070  * Join the ipif specific multicast groups.
18071  * Must be called after a mapping has been set up in the resolver.  (Always
18072  * called as writer.)
18073  */
18074 void
18075 ipif_multicast_up(ipif_t *ipif)
18076 {
18077 	int err, index;
18078 	ill_t *ill;
18079 
18080 	ASSERT(IAM_WRITER_IPIF(ipif));
18081 
18082 	ill = ipif->ipif_ill;
18083 	index = ill->ill_phyint->phyint_ifindex;
18084 
18085 	ip1dbg(("ipif_multicast_up\n"));
18086 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18087 		return;
18088 
18089 	if (ipif->ipif_isv6) {
18090 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18091 			return;
18092 
18093 		/* Join the all hosts multicast address */
18094 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18095 		/*
18096 		 * Passing B_TRUE means we have to join the multicast
18097 		 * membership on this interface even though this is
18098 		 * FAILED. If we join on a different one in the group,
18099 		 * we will not be able to delete the membership later
18100 		 * as we currently don't track where we join when we
18101 		 * join within the kernel unlike applications where
18102 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18103 		 * for more on this.
18104 		 */
18105 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18106 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18107 		if (err != 0) {
18108 			ip0dbg(("ipif_multicast_up: "
18109 			    "all_hosts_mcast failed %d\n",
18110 			    err));
18111 			return;
18112 		}
18113 		/*
18114 		 * Enable multicast for the solicited node multicast address
18115 		 */
18116 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18117 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18118 
18119 			ipv6_multi.s6_addr32[3] |=
18120 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18121 
18122 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18123 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18124 			    NULL);
18125 			if (err != 0) {
18126 				ip0dbg(("ipif_multicast_up: solicited MC"
18127 				    " failed %d\n", err));
18128 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18129 				    ill, ill->ill_phyint->phyint_ifindex,
18130 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18131 				return;
18132 			}
18133 		}
18134 	} else {
18135 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18136 			return;
18137 
18138 		/* Join the all hosts multicast address */
18139 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18140 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18141 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18142 		if (err) {
18143 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18144 			return;
18145 		}
18146 	}
18147 	ipif->ipif_multicast_up = 1;
18148 }
18149 
18150 /*
18151  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18152  * any explicit memberships are blown away in ill_leave_multicast() when the
18153  * ill is brought down.
18154  */
18155 static void
18156 ipif_multicast_down(ipif_t *ipif)
18157 {
18158 	int err;
18159 
18160 	ASSERT(IAM_WRITER_IPIF(ipif));
18161 
18162 	ip1dbg(("ipif_multicast_down\n"));
18163 	if (!ipif->ipif_multicast_up)
18164 		return;
18165 
18166 	ASSERT(ipif->ipif_isv6);
18167 
18168 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18169 
18170 	/*
18171 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18172 	 * we should look for ilms on this ill rather than the ones that have
18173 	 * been failed over here.  They are here temporarily. As
18174 	 * ipif_multicast_up has joined on this ill, we should delete only
18175 	 * from this ill.
18176 	 */
18177 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18178 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18179 	    B_TRUE, B_TRUE);
18180 	if (err != 0) {
18181 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18182 		    err));
18183 	}
18184 	/*
18185 	 * Disable multicast for the solicited node multicast address
18186 	 */
18187 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18188 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18189 
18190 		ipv6_multi.s6_addr32[3] |=
18191 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18192 
18193 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18194 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18195 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18196 
18197 		if (err != 0) {
18198 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18199 			    err));
18200 		}
18201 	}
18202 
18203 	ipif->ipif_multicast_up = 0;
18204 }
18205 
18206 /*
18207  * Used when an interface comes up to recreate any extra routes on this
18208  * interface.
18209  */
18210 static ire_t **
18211 ipif_recover_ire(ipif_t *ipif)
18212 {
18213 	mblk_t	*mp;
18214 	ire_t	**ipif_saved_irep;
18215 	ire_t	**irep;
18216 
18217 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18218 	    ipif->ipif_id));
18219 
18220 	mutex_enter(&ipif->ipif_saved_ire_lock);
18221 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18222 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18223 	if (ipif_saved_irep == NULL) {
18224 		mutex_exit(&ipif->ipif_saved_ire_lock);
18225 		return (NULL);
18226 	}
18227 
18228 	irep = ipif_saved_irep;
18229 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18230 		ire_t		*ire;
18231 		queue_t		*rfq;
18232 		queue_t		*stq;
18233 		ifrt_t		*ifrt;
18234 		uchar_t		*src_addr;
18235 		uchar_t		*gateway_addr;
18236 		mblk_t		*resolver_mp;
18237 		ushort_t	type;
18238 
18239 		/*
18240 		 * When the ire was initially created and then added in
18241 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18242 		 * in the case of a traditional interface route, or as one of
18243 		 * the IRE_OFFSUBNET types (with the exception of
18244 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18245 		 * which we don't need to save or recover).  In the case where
18246 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18247 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18248 		 * to satisfy software like GateD and Sun Cluster which creates
18249 		 * routes using the the loopback interface's address as a
18250 		 * gateway.
18251 		 *
18252 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18253 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18254 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18255 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18256 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18257 		 * the route looks like a traditional interface route (where
18258 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18259 		 * the saved ifrt->ifrt_type.  This means that in the case where
18260 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18261 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18262 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18263 		 */
18264 		ifrt = (ifrt_t *)mp->b_rptr;
18265 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18266 			rfq = NULL;
18267 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18268 			    ? ipif->ipif_rq : ipif->ipif_wq;
18269 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18270 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18271 			    : (uint8_t *)&ipif->ipif_src_addr;
18272 			gateway_addr = NULL;
18273 			resolver_mp = ipif->ipif_resolver_mp;
18274 			type = ipif->ipif_net_type;
18275 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18276 			/* Recover multiroute broadcast IRE. */
18277 			rfq = ipif->ipif_rq;
18278 			stq = ipif->ipif_wq;
18279 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18280 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18281 			    : (uint8_t *)&ipif->ipif_src_addr;
18282 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18283 			resolver_mp = ipif->ipif_bcast_mp;
18284 			type = ifrt->ifrt_type;
18285 		} else {
18286 			rfq = NULL;
18287 			stq = NULL;
18288 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18289 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18290 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18291 			resolver_mp = NULL;
18292 			type = ifrt->ifrt_type;
18293 		}
18294 
18295 		/*
18296 		 * Create a copy of the IRE with the saved address and netmask.
18297 		 */
18298 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18299 		    "0x%x/0x%x\n",
18300 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18301 		    ntohl(ifrt->ifrt_addr),
18302 		    ntohl(ifrt->ifrt_mask)));
18303 		ire = ire_create(
18304 		    (uint8_t *)&ifrt->ifrt_addr,
18305 		    (uint8_t *)&ifrt->ifrt_mask,
18306 		    src_addr,
18307 		    gateway_addr,
18308 		    NULL,
18309 		    &ifrt->ifrt_max_frag,
18310 		    NULL,
18311 		    rfq,
18312 		    stq,
18313 		    type,
18314 		    resolver_mp,
18315 		    ipif,
18316 		    NULL,
18317 		    0,
18318 		    0,
18319 		    0,
18320 		    ifrt->ifrt_flags,
18321 		    &ifrt->ifrt_iulp_info);
18322 
18323 		if (ire == NULL) {
18324 			mutex_exit(&ipif->ipif_saved_ire_lock);
18325 			kmem_free(ipif_saved_irep,
18326 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18327 			return (NULL);
18328 		}
18329 
18330 		/*
18331 		 * Some software (for example, GateD and Sun Cluster) attempts
18332 		 * to create (what amount to) IRE_PREFIX routes with the
18333 		 * loopback address as the gateway.  This is primarily done to
18334 		 * set up prefixes with the RTF_REJECT flag set (for example,
18335 		 * when generating aggregate routes.)
18336 		 *
18337 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18338 		 * IRE_LOOPBACK, then we map the request into a
18339 		 * IRE_IF_NORESOLVER.
18340 		 */
18341 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18342 			ire->ire_type = IRE_IF_NORESOLVER;
18343 		/*
18344 		 * ire held by ire_add, will be refreled' towards the
18345 		 * the end of ipif_up_done
18346 		 */
18347 		(void) ire_add(&ire, NULL, NULL, NULL);
18348 		*irep = ire;
18349 		irep++;
18350 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18351 	}
18352 	mutex_exit(&ipif->ipif_saved_ire_lock);
18353 	return (ipif_saved_irep);
18354 }
18355 
18356 /*
18357  * Used to set the netmask and broadcast address to default values when the
18358  * interface is brought up.  (Always called as writer.)
18359  */
18360 static void
18361 ipif_set_default(ipif_t *ipif)
18362 {
18363 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18364 
18365 	if (!ipif->ipif_isv6) {
18366 		/*
18367 		 * Interface holds an IPv4 address. Default
18368 		 * mask is the natural netmask.
18369 		 */
18370 		if (!ipif->ipif_net_mask) {
18371 			ipaddr_t	v4mask;
18372 
18373 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18374 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18375 		}
18376 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18377 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18378 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18379 		} else {
18380 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18381 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18382 		}
18383 		/*
18384 		 * NOTE: SunOS 4.X does this even if the broadcast address
18385 		 * has been already set thus we do the same here.
18386 		 */
18387 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18388 			ipaddr_t	v4addr;
18389 
18390 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18391 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18392 		}
18393 	} else {
18394 		/*
18395 		 * Interface holds an IPv6-only address.  Default
18396 		 * mask is all-ones.
18397 		 */
18398 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18399 			ipif->ipif_v6net_mask = ipv6_all_ones;
18400 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18401 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18402 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18403 		} else {
18404 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18405 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18406 		}
18407 	}
18408 }
18409 
18410 /*
18411  * Return 0 if this address can be used as local address without causing
18412  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18413  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18414  * Special checks are needed to allow the same IPv6 link-local address
18415  * on different ills.
18416  * TODO: allowing the same site-local address on different ill's.
18417  */
18418 int
18419 ip_addr_availability_check(ipif_t *new_ipif)
18420 {
18421 	in6_addr_t our_v6addr;
18422 	ill_t *ill;
18423 	ipif_t *ipif;
18424 	ill_walk_context_t ctx;
18425 
18426 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18427 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18428 	ASSERT(RW_READ_HELD(&ill_g_lock));
18429 
18430 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18431 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18432 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18433 		return (0);
18434 
18435 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18436 
18437 	if (new_ipif->ipif_isv6)
18438 		ill = ILL_START_WALK_V6(&ctx);
18439 	else
18440 		ill = ILL_START_WALK_V4(&ctx);
18441 
18442 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18443 		for (ipif = ill->ill_ipif; ipif != NULL;
18444 		    ipif = ipif->ipif_next) {
18445 			if ((ipif == new_ipif) ||
18446 			    !(ipif->ipif_flags & IPIF_UP) ||
18447 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18448 				continue;
18449 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18450 			    &our_v6addr)) {
18451 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18452 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18453 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18454 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18455 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18456 				    new_ipif->ipif_ill != ill)
18457 					continue;
18458 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18459 				    new_ipif->ipif_ill != ill)
18460 					continue;
18461 				else if (new_ipif->ipif_zoneid !=
18462 				    ipif->ipif_zoneid &&
18463 				    (ill->ill_phyint->phyint_flags &
18464 				    PHYI_LOOPBACK))
18465 					continue;
18466 				else if (new_ipif->ipif_ill == ill)
18467 					return (EADDRINUSE);
18468 				else
18469 					return (EADDRNOTAVAIL);
18470 			}
18471 		}
18472 	}
18473 
18474 	return (0);
18475 }
18476 
18477 /*
18478  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18479  * IREs for the ipif.
18480  * When the routine returns EINPROGRESS then mp has been consumed and
18481  * the ioctl will be acked from ip_rput_dlpi.
18482  */
18483 static int
18484 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18485 {
18486 	ill_t	*ill = ipif->ipif_ill;
18487 	boolean_t isv6 = ipif->ipif_isv6;
18488 	int	err = 0;
18489 	boolean_t success;
18490 
18491 	ASSERT(IAM_WRITER_IPIF(ipif));
18492 
18493 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18494 
18495 	/* Shouldn't get here if it is already up. */
18496 	if (ipif->ipif_flags & IPIF_UP)
18497 		return (EALREADY);
18498 
18499 	/* Skip arp/ndp for any loopback interface. */
18500 	if (ill->ill_wq != NULL) {
18501 		conn_t *connp = Q_TO_CONN(q);
18502 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18503 
18504 		if (!ill->ill_dl_up) {
18505 			/*
18506 			 * ill_dl_up is not yet set. i.e. we are yet to
18507 			 * DL_BIND with the driver and this is the first
18508 			 * logical interface on the ill to become "up".
18509 			 * Tell the driver to get going (via DL_BIND_REQ).
18510 			 * Note that changing "significant" IFF_ flags
18511 			 * address/netmask etc cause a down/up dance, but
18512 			 * does not cause an unbind (DL_UNBIND) with the driver
18513 			 */
18514 			return (ill_dl_up(ill, ipif, mp, q));
18515 		}
18516 
18517 		/*
18518 		 * ipif_resolver_up may end up sending an
18519 		 * AR_INTERFACE_UP message to ARP, which would, in
18520 		 * turn send a DLPI message to the driver. ioctls are
18521 		 * serialized and so we cannot send more than one
18522 		 * interface up message at a time. If ipif_resolver_up
18523 		 * does send an interface up message to ARP, we get
18524 		 * EINPROGRESS and we will complete in ip_arp_done.
18525 		 */
18526 
18527 		ASSERT(connp != NULL);
18528 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18529 		mutex_enter(&connp->conn_lock);
18530 		mutex_enter(&ill->ill_lock);
18531 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18532 		mutex_exit(&ill->ill_lock);
18533 		mutex_exit(&connp->conn_lock);
18534 		if (!success)
18535 			return (EINTR);
18536 
18537 		/*
18538 		 * Crank up IPv6 neighbor discovery
18539 		 * Unlike ARP, this should complete when
18540 		 * ipif_ndp_up returns. However, for
18541 		 * ILLF_XRESOLV interfaces we also send a
18542 		 * AR_INTERFACE_UP to the external resolver.
18543 		 * That ioctl will complete in ip_rput.
18544 		 */
18545 		if (isv6) {
18546 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18547 			    B_FALSE);
18548 			if (err != 0) {
18549 				mp = ipsq_pending_mp_get(ipsq, &connp);
18550 				return (err);
18551 			}
18552 		}
18553 		/* Now, ARP */
18554 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18555 		    EINPROGRESS) {
18556 			/* We will complete it in ip_arp_done */
18557 			return (err);
18558 		}
18559 		mp = ipsq_pending_mp_get(ipsq, &connp);
18560 		ASSERT(mp != NULL);
18561 		if (err != 0)
18562 			return (err);
18563 	}
18564 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18565 }
18566 
18567 /*
18568  * Perform a bind for the physical device.
18569  * When the routine returns EINPROGRESS then mp has been consumed and
18570  * the ioctl will be acked from ip_rput_dlpi.
18571  * Allocate an unbind message and save it until ipif_down.
18572  */
18573 static int
18574 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18575 {
18576 	mblk_t	*areq_mp = NULL;
18577 	mblk_t	*bind_mp = NULL;
18578 	mblk_t	*unbind_mp = NULL;
18579 	conn_t	*connp;
18580 	boolean_t success;
18581 
18582 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18583 	ASSERT(IAM_WRITER_ILL(ill));
18584 
18585 	ASSERT(mp != NULL);
18586 
18587 	/* Create a resolver cookie for ARP */
18588 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18589 		areq_t		*areq;
18590 		uint16_t	sap_addr;
18591 
18592 		areq_mp = ill_arp_alloc(ill,
18593 			(uchar_t *)&ip_areq_template, 0);
18594 		if (areq_mp == NULL) {
18595 			return (ENOMEM);
18596 		}
18597 		freemsg(ill->ill_resolver_mp);
18598 		ill->ill_resolver_mp = areq_mp;
18599 		areq = (areq_t *)areq_mp->b_rptr;
18600 		sap_addr = ill->ill_sap;
18601 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18602 		/*
18603 		 * Wait till we call ill_pending_mp_add to determine
18604 		 * the success before we free the ill_resolver_mp and
18605 		 * attach areq_mp in it's place.
18606 		 */
18607 	}
18608 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18609 	    DL_BIND_REQ);
18610 	if (bind_mp == NULL)
18611 		goto bad;
18612 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18613 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18614 
18615 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18616 	if (unbind_mp == NULL)
18617 		goto bad;
18618 
18619 	/*
18620 	 * Record state needed to complete this operation when the
18621 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18622 	 */
18623 	if (WR(q)->q_next == NULL) {
18624 		connp = Q_TO_CONN(q);
18625 		mutex_enter(&connp->conn_lock);
18626 	} else {
18627 		connp = NULL;
18628 	}
18629 	mutex_enter(&ipif->ipif_ill->ill_lock);
18630 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18631 	mutex_exit(&ipif->ipif_ill->ill_lock);
18632 	if (connp != NULL)
18633 		mutex_exit(&connp->conn_lock);
18634 	if (!success)
18635 		goto bad;
18636 
18637 	/*
18638 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18639 	 * the interface goes down.
18640 	 */
18641 	ASSERT(ill->ill_unbind_mp == NULL);
18642 	ill->ill_unbind_mp = unbind_mp;
18643 
18644 	ill_dlpi_send(ill, bind_mp);
18645 	/* Send down link-layer capabilities probe if not already done. */
18646 	ill_capability_probe(ill);
18647 
18648 	/*
18649 	 * Sysid used to rely on the fact that netboots set domainname
18650 	 * and the like. Now that miniroot boots aren't strictly netboots
18651 	 * and miniroot network configuration is driven from userland
18652 	 * these things still need to be set. This situation can be detected
18653 	 * by comparing the interface being configured here to the one
18654 	 * dhcack was set to reference by the boot loader. Once sysid is
18655 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18656 	 */
18657 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18658 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18659 	    (strlen(srpc_domain) == 0)) {
18660 		if (dhcpinit() != 0)
18661 			cmn_err(CE_WARN, "no cached dhcp response");
18662 	}
18663 
18664 	/*
18665 	 * This operation will complete in ip_rput_dlpi with either
18666 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18667 	 */
18668 	return (EINPROGRESS);
18669 bad:
18670 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18671 	/*
18672 	 * We don't have to check for possible removal from illgrp
18673 	 * as we have not yet inserted in illgrp. For groups
18674 	 * without names, this ipif is still not UP and hence
18675 	 * this could not have possibly had any influence in forming
18676 	 * groups.
18677 	 */
18678 
18679 	if (bind_mp != NULL)
18680 		freemsg(bind_mp);
18681 	if (unbind_mp != NULL)
18682 		freemsg(unbind_mp);
18683 	return (ENOMEM);
18684 }
18685 
18686 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
18687 
18688 /*
18689  * DLPI and ARP is up.
18690  * Create all the IREs associated with an interface bring up multicast.
18691  * Set the interface flag and finish other initialization
18692  * that potentially had to be differed to after DL_BIND_ACK.
18693  */
18694 int
18695 ipif_up_done(ipif_t *ipif)
18696 {
18697 	ire_t	*ire_array[20];
18698 	ire_t	**irep = ire_array;
18699 	ire_t	**irep1;
18700 	ipaddr_t net_mask = 0;
18701 	ipaddr_t subnet_mask, route_mask;
18702 	ill_t	*ill = ipif->ipif_ill;
18703 	queue_t	*stq;
18704 	ipif_t	 *src_ipif;
18705 	ipif_t   *tmp_ipif;
18706 	boolean_t	flush_ire_cache = B_TRUE;
18707 	int	err = 0;
18708 	phyint_t *phyi;
18709 	ire_t	**ipif_saved_irep = NULL;
18710 	int ipif_saved_ire_cnt;
18711 	int	cnt;
18712 	boolean_t	src_ipif_held = B_FALSE;
18713 	boolean_t	ire_added = B_FALSE;
18714 	boolean_t	loopback = B_FALSE;
18715 
18716 	ip1dbg(("ipif_up_done(%s:%u)\n",
18717 		ipif->ipif_ill->ill_name, ipif->ipif_id));
18718 	/* Check if this is a loopback interface */
18719 	if (ipif->ipif_ill->ill_wq == NULL)
18720 		loopback = B_TRUE;
18721 
18722 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18723 	/*
18724 	 * If all other interfaces for this ill are down or DEPRECATED,
18725 	 * or otherwise unsuitable for source address selection, remove
18726 	 * any IRE_CACHE entries for this ill to make sure source
18727 	 * address selection gets to take this new ipif into account.
18728 	 * No need to hold ill_lock while traversing the ipif list since
18729 	 * we are writer
18730 	 */
18731 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
18732 		tmp_ipif = tmp_ipif->ipif_next) {
18733 		if (((tmp_ipif->ipif_flags &
18734 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
18735 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
18736 		    (tmp_ipif == ipif))
18737 			continue;
18738 		/* first useable pre-existing interface */
18739 		flush_ire_cache = B_FALSE;
18740 		break;
18741 	}
18742 	if (flush_ire_cache)
18743 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
18744 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
18745 
18746 	/*
18747 	 * Figure out which way the send-to queue should go.  Only
18748 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
18749 	 * should show up here.
18750 	 */
18751 	switch (ill->ill_net_type) {
18752 	case IRE_IF_RESOLVER:
18753 		stq = ill->ill_rq;
18754 		break;
18755 	case IRE_IF_NORESOLVER:
18756 	case IRE_LOOPBACK:
18757 		stq = ill->ill_wq;
18758 		break;
18759 	default:
18760 		return (EINVAL);
18761 	}
18762 
18763 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
18764 		/*
18765 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
18766 		 * ipif_lookup_on_name(), but in the case of zones we can have
18767 		 * several loopback addresses on lo0. So all the interfaces with
18768 		 * loopback addresses need to be marked IRE_LOOPBACK.
18769 		 */
18770 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
18771 		    htonl(INADDR_LOOPBACK))
18772 			ipif->ipif_ire_type = IRE_LOOPBACK;
18773 		else
18774 			ipif->ipif_ire_type = IRE_LOCAL;
18775 	}
18776 
18777 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
18778 		/*
18779 		 * Can't use our source address. Select a different
18780 		 * source address for the IRE_INTERFACE and IRE_LOCAL
18781 		 */
18782 		src_ipif = ipif_select_source(ipif->ipif_ill,
18783 		    ipif->ipif_subnet, ipif->ipif_zoneid);
18784 		if (src_ipif == NULL)
18785 			src_ipif = ipif;	/* Last resort */
18786 		else
18787 			src_ipif_held = B_TRUE;
18788 	} else {
18789 		src_ipif = ipif;
18790 	}
18791 
18792 	/* Create all the IREs associated with this interface */
18793 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18794 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18795 		/* Register the source address for __sin6_src_id */
18796 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
18797 		    ipif->ipif_zoneid);
18798 		if (err != 0) {
18799 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
18800 			return (err);
18801 		}
18802 		/* If the interface address is set, create the local IRE. */
18803 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
18804 			(void *)ipif,
18805 			ipif->ipif_ire_type,
18806 			ntohl(ipif->ipif_lcl_addr)));
18807 		*irep++ = ire_create(
18808 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
18809 		    (uchar_t *)&ip_g_all_ones,		/* mask */
18810 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
18811 		    NULL,				/* no gateway */
18812 		    NULL,
18813 		    &ip_loopback_mtuplus,		/* max frag size */
18814 		    NULL,
18815 		    ipif->ipif_rq,			/* recv-from queue */
18816 		    NULL,				/* no send-to queue */
18817 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
18818 		    NULL,
18819 		    ipif,
18820 		    NULL,
18821 		    0,
18822 		    0,
18823 		    0,
18824 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
18825 		    RTF_PRIVATE : 0,
18826 		    &ire_uinfo_null);
18827 	} else {
18828 		ip1dbg((
18829 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
18830 		    ipif->ipif_ire_type,
18831 		    ntohl(ipif->ipif_lcl_addr),
18832 		    (uint_t)ipif->ipif_flags));
18833 	}
18834 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18835 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18836 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
18837 	} else {
18838 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
18839 	}
18840 
18841 	subnet_mask = ipif->ipif_net_mask;
18842 
18843 	/*
18844 	 * If mask was not specified, use natural netmask of
18845 	 * interface address. Also, store this mask back into the
18846 	 * ipif struct.
18847 	 */
18848 	if (subnet_mask == 0) {
18849 		subnet_mask = net_mask;
18850 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
18851 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
18852 		    ipif->ipif_v6subnet);
18853 	}
18854 
18855 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
18856 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
18857 	    ipif->ipif_subnet != INADDR_ANY) {
18858 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18859 
18860 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18861 			route_mask = IP_HOST_MASK;
18862 		} else {
18863 			route_mask = subnet_mask;
18864 		}
18865 
18866 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
18867 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
18868 			(void *)ipif, (void *)ill,
18869 			ill->ill_net_type,
18870 			ntohl(ipif->ipif_subnet)));
18871 		*irep++ = ire_create(
18872 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
18873 		    (uchar_t *)&route_mask,		/* mask */
18874 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
18875 		    NULL,				/* no gateway */
18876 		    NULL,
18877 		    &ipif->ipif_mtu,			/* max frag */
18878 		    NULL,
18879 		    NULL,				/* no recv queue */
18880 		    stq,				/* send-to queue */
18881 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
18882 		    ill->ill_resolver_mp,		/* xmit header */
18883 		    ipif,
18884 		    NULL,
18885 		    0,
18886 		    0,
18887 		    0,
18888 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
18889 		    &ire_uinfo_null);
18890 	}
18891 
18892 	/*
18893 	 * If the interface address is set, create the broadcast IREs.
18894 	 *
18895 	 * ire_create_bcast checks if the proposed new IRE matches
18896 	 * any existing IRE's with the same physical interface (ILL).
18897 	 * This should get rid of duplicates.
18898 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
18899 	 * any broadcast ires.
18900 	 */
18901 	if ((ipif->ipif_subnet != INADDR_ANY) &&
18902 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
18903 		ipaddr_t addr;
18904 
18905 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
18906 		irep = ire_check_and_create_bcast(ipif, 0, irep,
18907 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18908 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
18909 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18910 
18911 		/*
18912 		 * For backward compatibility, we need to create net
18913 		 * broadcast ire's based on the old "IP address class
18914 		 * system."  The reason is that some old machines only
18915 		 * respond to these class derived net broadcast.
18916 		 *
18917 		 * But we should not create these net broadcast ire's if
18918 		 * the subnet_mask is shorter than the IP address class based
18919 		 * derived netmask.  Otherwise, we may create a net
18920 		 * broadcast address which is the same as an IP address
18921 		 * on the subnet.  Then TCP will refuse to talk to that
18922 		 * address.
18923 		 *
18924 		 * Nor do we need IRE_BROADCAST ire's for the interface
18925 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
18926 		 * interface is already created.  Creating these broadcast
18927 		 * ire's will only create confusion as the "addr" is going
18928 		 * to be same as that of the IP address of the interface.
18929 		 */
18930 		if (net_mask < subnet_mask) {
18931 			addr = net_mask & ipif->ipif_subnet;
18932 			irep = ire_check_and_create_bcast(ipif, addr, irep,
18933 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18934 			irep = ire_check_and_create_bcast(ipif,
18935 			    ~net_mask | addr, irep,
18936 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18937 		}
18938 
18939 		if (subnet_mask != 0xFFFFFFFF) {
18940 			addr = ipif->ipif_subnet;
18941 			irep = ire_check_and_create_bcast(ipif, addr, irep,
18942 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18943 			irep = ire_check_and_create_bcast(ipif,
18944 			    ~subnet_mask|addr, irep,
18945 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18946 		}
18947 	}
18948 
18949 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18950 
18951 	/* If an earlier ire_create failed, get out now */
18952 	for (irep1 = irep; irep1 > ire_array; ) {
18953 		irep1--;
18954 		if (*irep1 == NULL) {
18955 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
18956 			err = ENOMEM;
18957 			goto bad;
18958 		}
18959 	}
18960 
18961 	/*
18962 	 * Need to atomically check for ip_addr_availablity_check
18963 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
18964 	 * from group also.The ill_g_lock is grabbed as reader
18965 	 * just to make sure no new ills or new ipifs are being added
18966 	 * to the system while we are checking the uniqueness of addresses.
18967 	 */
18968 	rw_enter(&ill_g_lock, RW_READER);
18969 	mutex_enter(&ip_addr_avail_lock);
18970 	/* Mark it up, and increment counters. */
18971 	ill->ill_ipif_up_count++;
18972 	ipif->ipif_flags |= IPIF_UP;
18973 	err = ip_addr_availability_check(ipif);
18974 	mutex_exit(&ip_addr_avail_lock);
18975 	rw_exit(&ill_g_lock);
18976 
18977 	if (err != 0) {
18978 		/*
18979 		 * Our address may already be up on the same ill. In this case,
18980 		 * the ARP entry for our ipif replaced the one for the other
18981 		 * ipif. So we don't want to delete it (otherwise the other ipif
18982 		 * would be unable to send packets).
18983 		 * ip_addr_availability_check() identifies this case for us and
18984 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
18985 		 * which is the expected error code.
18986 		 */
18987 		if (err == EADDRINUSE) {
18988 			freemsg(ipif->ipif_arp_del_mp);
18989 			ipif->ipif_arp_del_mp = NULL;
18990 			err = EADDRNOTAVAIL;
18991 		}
18992 		ill->ill_ipif_up_count--;
18993 		ipif->ipif_flags &= ~IPIF_UP;
18994 		goto bad;
18995 	}
18996 
18997 	/*
18998 	 * Add in all newly created IREs.  ire_create_bcast() has
18999 	 * already checked for duplicates of the IRE_BROADCAST type.
19000 	 * We want to add before we call ifgrp_insert which wants
19001 	 * to know whether IRE_IF_RESOLVER exists or not.
19002 	 *
19003 	 * NOTE : We refrele the ire though we may branch to "bad"
19004 	 *	  later on where we do ire_delete. This is okay
19005 	 *	  because nobody can delete it as we are running
19006 	 *	  exclusively.
19007 	 */
19008 	for (irep1 = irep; irep1 > ire_array; ) {
19009 		irep1--;
19010 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19011 		/*
19012 		 * refheld by ire_add. refele towards the end of the func
19013 		 */
19014 		(void) ire_add(irep1, NULL, NULL, NULL);
19015 	}
19016 	ire_added = B_TRUE;
19017 	/*
19018 	 * Form groups if possible.
19019 	 *
19020 	 * If we are supposed to be in a ill_group with a name, insert it
19021 	 * now as we know that at least one ipif is UP. Otherwise form
19022 	 * nameless groups.
19023 	 *
19024 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19025 	 * this ipif into the appropriate interface group, or create a
19026 	 * new one. If this is already in a nameless group, we try to form
19027 	 * a bigger group looking at other ills potentially sharing this
19028 	 * ipif's prefix.
19029 	 */
19030 	phyi = ill->ill_phyint;
19031 	if (phyi->phyint_groupname_len != 0) {
19032 		ASSERT(phyi->phyint_groupname != NULL);
19033 		if (ill->ill_ipif_up_count == 1) {
19034 			ASSERT(ill->ill_group == NULL);
19035 			err = illgrp_insert(&illgrp_head_v4, ill,
19036 			    phyi->phyint_groupname, NULL, B_TRUE);
19037 			if (err != 0) {
19038 				ip1dbg(("ipif_up_done: illgrp allocation "
19039 				    "failed, error %d\n", err));
19040 				goto bad;
19041 			}
19042 		}
19043 		ASSERT(ill->ill_group != NULL);
19044 	}
19045 
19046 	/*
19047 	 * When this is part of group, we need to make sure that
19048 	 * any broadcast ires created because of this ipif coming
19049 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19050 	 * so that we don't receive duplicate broadcast packets.
19051 	 */
19052 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19053 		ipif_renominate_bcast(ipif);
19054 
19055 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19056 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19057 	ipif_saved_irep = ipif_recover_ire(ipif);
19058 
19059 	if (!loopback) {
19060 		/*
19061 		 * If the broadcast address has been set, make sure it makes
19062 		 * sense based on the interface address.
19063 		 * Only match on ill since we are sharing broadcast addresses.
19064 		 */
19065 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19066 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19067 			ire_t	*ire;
19068 
19069 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19070 			    IRE_BROADCAST, ipif, ALL_ZONES,
19071 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19072 
19073 			if (ire == NULL) {
19074 				/*
19075 				 * If there isn't a matching broadcast IRE,
19076 				 * revert to the default for this netmask.
19077 				 */
19078 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19079 				mutex_enter(&ipif->ipif_ill->ill_lock);
19080 				ipif_set_default(ipif);
19081 				mutex_exit(&ipif->ipif_ill->ill_lock);
19082 			} else {
19083 				ire_refrele(ire);
19084 			}
19085 		}
19086 
19087 	}
19088 
19089 
19090 	/* This is the first interface on this ill */
19091 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19092 		/*
19093 		 * Need to recover all multicast memberships in the driver.
19094 		 * This had to be deferred until we had attached.
19095 		 */
19096 		ill_recover_multicast(ill);
19097 	}
19098 	/* Join the allhosts multicast address */
19099 	ipif_multicast_up(ipif);
19100 
19101 	if (!loopback) {
19102 		/*
19103 		 * See whether anybody else would benefit from the
19104 		 * new ipif that we added. We call this always rather
19105 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19106 		 * ipif is for the benefit of illgrp_insert (done above)
19107 		 * which does not do source address selection as it does
19108 		 * not want to re-create interface routes that we are
19109 		 * having reference to it here.
19110 		 */
19111 		ill_update_source_selection(ill);
19112 	}
19113 
19114 	for (irep1 = irep; irep1 > ire_array; ) {
19115 		irep1--;
19116 		if (*irep1 != NULL) {
19117 			/* was held in ire_add */
19118 			ire_refrele(*irep1);
19119 		}
19120 	}
19121 
19122 	cnt = ipif_saved_ire_cnt;
19123 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19124 		if (*irep1 != NULL) {
19125 			/* was held in ire_add */
19126 			ire_refrele(*irep1);
19127 		}
19128 	}
19129 
19130 	/*
19131 	 * This had to be deferred until we had bound.
19132 	 * tell routing sockets that this interface is up
19133 	 */
19134 	ip_rts_ifmsg(ipif);
19135 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19136 
19137 	if (!loopback) {
19138 		/* Broadcast an address mask reply. */
19139 		ipif_mask_reply(ipif);
19140 	}
19141 	if (ipif_saved_irep != NULL) {
19142 		kmem_free(ipif_saved_irep,
19143 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19144 	}
19145 	if (src_ipif_held)
19146 		ipif_refrele(src_ipif);
19147 	/* Let SCTP update the status for this ipif */
19148 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19149 	return (0);
19150 
19151 bad:
19152 	ip1dbg(("ipif_up_done: FAILED \n"));
19153 	/*
19154 	 * We don't have to bother removing from ill groups because
19155 	 *
19156 	 * 1) For groups with names, we insert only when the first ipif
19157 	 *    comes up. In that case if it fails, it will not be in any
19158 	 *    group. So, we need not try to remove for that case.
19159 	 *
19160 	 * 2) For groups without names, either we tried to insert ipif_ill
19161 	 *    in a group as singleton or found some other group to become
19162 	 *    a bigger group. For the former, if it fails we don't have
19163 	 *    anything to do as ipif_ill is not in the group and for the
19164 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19165 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19166 	 */
19167 	while (irep > ire_array) {
19168 		irep--;
19169 		if (*irep != NULL) {
19170 			ire_delete(*irep);
19171 			if (ire_added)
19172 				ire_refrele(*irep);
19173 		}
19174 	}
19175 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19176 
19177 	if (ipif_saved_irep != NULL) {
19178 		kmem_free(ipif_saved_irep,
19179 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19180 	}
19181 	if (src_ipif_held)
19182 		ipif_refrele(src_ipif);
19183 
19184 	ipif_arp_down(ipif);
19185 	return (err);
19186 }
19187 
19188 /*
19189  * Turn off the ARP with the ILLF_NOARP flag.
19190  */
19191 static int
19192 ill_arp_off(ill_t *ill)
19193 {
19194 	mblk_t	*arp_off_mp = NULL;
19195 	mblk_t	*arp_on_mp = NULL;
19196 
19197 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19198 
19199 	ASSERT(IAM_WRITER_ILL(ill));
19200 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19201 
19202 	/*
19203 	 * If the on message is still around we've already done
19204 	 * an arp_off without doing an arp_on thus there is no
19205 	 * work needed.
19206 	 */
19207 	if (ill->ill_arp_on_mp != NULL)
19208 		return (0);
19209 
19210 	/*
19211 	 * Allocate an ARP on message (to be saved) and an ARP off message
19212 	 */
19213 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19214 	if (!arp_off_mp)
19215 		return (ENOMEM);
19216 
19217 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19218 	if (!arp_on_mp)
19219 		goto failed;
19220 
19221 	ASSERT(ill->ill_arp_on_mp == NULL);
19222 	ill->ill_arp_on_mp = arp_on_mp;
19223 
19224 	/* Send an AR_INTERFACE_OFF request */
19225 	putnext(ill->ill_rq, arp_off_mp);
19226 	return (0);
19227 failed:
19228 
19229 	if (arp_off_mp)
19230 		freemsg(arp_off_mp);
19231 	return (ENOMEM);
19232 }
19233 
19234 /*
19235  * Turn on ARP by turning off the ILLF_NOARP flag.
19236  */
19237 static int
19238 ill_arp_on(ill_t *ill)
19239 {
19240 	mblk_t	*mp;
19241 
19242 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19243 
19244 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19245 
19246 	ASSERT(IAM_WRITER_ILL(ill));
19247 	/*
19248 	 * Send an AR_INTERFACE_ON request if we have already done
19249 	 * an arp_off (which allocated the message).
19250 	 */
19251 	if (ill->ill_arp_on_mp != NULL) {
19252 		mp = ill->ill_arp_on_mp;
19253 		ill->ill_arp_on_mp = NULL;
19254 		putnext(ill->ill_rq, mp);
19255 	}
19256 	return (0);
19257 }
19258 
19259 /*
19260  * Called after either deleting ill from the group or when setting
19261  * FAILED or STANDBY on the interface.
19262  */
19263 static void
19264 illgrp_reset_schednext(ill_t *ill)
19265 {
19266 	ill_group_t *illgrp;
19267 	ill_t *save_ill;
19268 
19269 	ASSERT(IAM_WRITER_ILL(ill));
19270 	/*
19271 	 * When called from illgrp_delete, ill_group will be non-NULL.
19272 	 * But when called from ip_sioctl_flags, it could be NULL if
19273 	 * somebody is setting FAILED/INACTIVE on some interface which
19274 	 * is not part of a group.
19275 	 */
19276 	illgrp = ill->ill_group;
19277 	if (illgrp == NULL)
19278 		return;
19279 	if (illgrp->illgrp_ill_schednext != ill)
19280 		return;
19281 
19282 	illgrp->illgrp_ill_schednext = NULL;
19283 	save_ill = ill;
19284 	/*
19285 	 * Choose a good ill to be the next one for
19286 	 * outbound traffic. As the flags FAILED/STANDBY is
19287 	 * not yet marked when called from ip_sioctl_flags,
19288 	 * we check for ill separately.
19289 	 */
19290 	for (ill = illgrp->illgrp_ill; ill != NULL;
19291 	    ill = ill->ill_group_next) {
19292 		if ((ill != save_ill) &&
19293 		    !(ill->ill_phyint->phyint_flags &
19294 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19295 			illgrp->illgrp_ill_schednext = ill;
19296 			return;
19297 		}
19298 	}
19299 }
19300 
19301 /*
19302  * Given an ill, find the next ill in the group to be scheduled.
19303  * (This should be called by ip_newroute() before ire_create().)
19304  * The passed in ill may be pulled out of the group, after we have picked
19305  * up a different outgoing ill from the same group. However ire add will
19306  * atomically check this.
19307  */
19308 ill_t *
19309 illgrp_scheduler(ill_t *ill)
19310 {
19311 	ill_t *retill;
19312 	ill_group_t *illgrp;
19313 	int illcnt;
19314 	int i;
19315 	uint64_t flags;
19316 
19317 	/*
19318 	 * We don't use a lock to check for the ill_group. If this ill
19319 	 * is currently being inserted we may end up just returning this
19320 	 * ill itself. That is ok.
19321 	 */
19322 	if (ill->ill_group == NULL) {
19323 		ill_refhold(ill);
19324 		return (ill);
19325 	}
19326 
19327 	/*
19328 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19329 	 * a set of stable ills. No ill can be added or deleted or change
19330 	 * group while we hold the reader lock.
19331 	 */
19332 	rw_enter(&ill_g_lock, RW_READER);
19333 	if ((illgrp = ill->ill_group) == NULL) {
19334 		rw_exit(&ill_g_lock);
19335 		ill_refhold(ill);
19336 		return (ill);
19337 	}
19338 
19339 	illcnt = illgrp->illgrp_ill_count;
19340 	mutex_enter(&illgrp->illgrp_lock);
19341 	retill = illgrp->illgrp_ill_schednext;
19342 
19343 	if (retill == NULL)
19344 		retill = illgrp->illgrp_ill;
19345 
19346 	/*
19347 	 * We do a circular search beginning at illgrp_ill_schednext
19348 	 * or illgrp_ill. We don't check the flags against the ill lock
19349 	 * since it can change anytime. The ire creation will be atomic
19350 	 * and will fail if the ill is FAILED or OFFLINE.
19351 	 */
19352 	for (i = 0; i < illcnt; i++) {
19353 		flags = retill->ill_phyint->phyint_flags;
19354 
19355 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19356 		    ILL_CAN_LOOKUP(retill)) {
19357 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19358 			ill_refhold(retill);
19359 			break;
19360 		}
19361 		retill = retill->ill_group_next;
19362 		if (retill == NULL)
19363 			retill = illgrp->illgrp_ill;
19364 	}
19365 	mutex_exit(&illgrp->illgrp_lock);
19366 	rw_exit(&ill_g_lock);
19367 
19368 	return (i == illcnt ? NULL : retill);
19369 }
19370 
19371 /*
19372  * Checks for availbility of a usable source address (if there is one) when the
19373  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19374  * this selection is done regardless of the destination.
19375  */
19376 boolean_t
19377 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19378 {
19379 	uint_t	ifindex;
19380 	ipif_t	*ipif = NULL;
19381 	ill_t	*uill;
19382 	boolean_t isv6;
19383 
19384 	ASSERT(ill != NULL);
19385 
19386 	isv6 = ill->ill_isv6;
19387 	ifindex = ill->ill_usesrc_ifindex;
19388 	if (ifindex != 0) {
19389 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19390 		    NULL);
19391 		if (uill == NULL)
19392 			return (NULL);
19393 		mutex_enter(&uill->ill_lock);
19394 		for (ipif = uill->ill_ipif; ipif != NULL;
19395 		    ipif = ipif->ipif_next) {
19396 			if (!IPIF_CAN_LOOKUP(ipif))
19397 				continue;
19398 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19399 				continue;
19400 			if (!(ipif->ipif_flags & IPIF_UP))
19401 				continue;
19402 			if (ipif->ipif_zoneid != zoneid)
19403 				continue;
19404 			if ((isv6 &&
19405 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19406 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19407 				continue;
19408 			mutex_exit(&uill->ill_lock);
19409 			ill_refrele(uill);
19410 			return (B_TRUE);
19411 		}
19412 		mutex_exit(&uill->ill_lock);
19413 		ill_refrele(uill);
19414 	}
19415 	return (B_FALSE);
19416 }
19417 
19418 /*
19419  * Determine the best source address given a destination address and an ill.
19420  * Prefers non-deprecated over deprecated but will return a deprecated
19421  * address if there is no other choice. If there is a usable source address
19422  * on the interface pointed to by ill_usesrc_ifindex then that is given
19423  * first preference.
19424  *
19425  * Returns NULL if there is no suitable source address for the ill.
19426  * This only occurs when there is no valid source address for the ill.
19427  */
19428 ipif_t *
19429 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19430 {
19431 	ipif_t *ipif;
19432 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19433 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19434 	int index = 0;
19435 	boolean_t wrapped = B_FALSE;
19436 	boolean_t same_subnet_only = B_FALSE;
19437 	boolean_t ipif_same_found, ipif_other_found;
19438 	ill_t	*till, *usill = NULL;
19439 
19440 	if (ill->ill_usesrc_ifindex != 0) {
19441 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19442 		    NULL, NULL, NULL, NULL);
19443 		if (usill != NULL)
19444 			ill = usill;	/* Select source from usesrc ILL */
19445 		else
19446 			return (NULL);
19447 	}
19448 
19449 	/*
19450 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19451 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19452 	 * After selecting the right ipif, under ill_lock make sure ipif is
19453 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19454 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19455 	 * but not under a lock.
19456 	 */
19457 	rw_enter(&ill_g_lock, RW_READER);
19458 
19459 retry:
19460 	till = ill;
19461 	ipif_arr[0] = NULL;
19462 
19463 	if (till->ill_group != NULL)
19464 		till = till->ill_group->illgrp_ill;
19465 
19466 	/*
19467 	 * Choose one good source address from each ill across the group.
19468 	 * If possible choose a source address in the same subnet as
19469 	 * the destination address.
19470 	 *
19471 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19472 	 * This is okay because of the following.
19473 	 *
19474 	 *    If PHYI_FAILED is set and we still have non-deprecated
19475 	 *    addresses, it means the addresses have not yet been
19476 	 *    failed over to a different interface. We potentially
19477 	 *    select them to create IRE_CACHES, which will be later
19478 	 *    flushed when the addresses move over.
19479 	 *
19480 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19481 	 *    addresses, it means either the user has configured them
19482 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19483 	 *    been moved over. For the former, in.mpathd does a failover
19484 	 *    when the interface becomes INACTIVE and hence we should
19485 	 *    not find them. Once INACTIVE is set, we don't allow them
19486 	 *    to create logical interfaces anymore. For the latter, a
19487 	 *    flush will happen when INACTIVE is cleared which will
19488 	 *    flush the IRE_CACHES.
19489 	 *
19490 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19491 	 *    over soon. We potentially select them to create IRE_CACHEs,
19492 	 *    which will be later flushed when the addresses move over.
19493 	 *
19494 	 * NOTE : As ipif_select_source is called to borrow source address
19495 	 * for an ipif that is part of a group, source address selection
19496 	 * will be re-done whenever the group changes i.e either an
19497 	 * insertion/deletion in the group.
19498 	 *
19499 	 * Fill ipif_arr[] with source addresses, using these rules:
19500 	 *
19501 	 *	1. At most one source address from a given ill ends up
19502 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19503 	 *	   associated with a given ill ends up in ipif_arr[].
19504 	 *
19505 	 *	2. If there is at least one non-deprecated ipif in the
19506 	 *	   IPMP group with a source address on the same subnet as
19507 	 *	   our destination, then fill ipif_arr[] only with
19508 	 *	   source addresses on the same subnet as our destination.
19509 	 *	   Note that because of (1), only the first
19510 	 *	   non-deprecated ipif found with a source address
19511 	 *	   matching the destination ends up in ipif_arr[].
19512 	 *
19513 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19514 	 *	   addresses not in the same subnet as our destination.
19515 	 *	   Again, because of (1), only the first off-subnet source
19516 	 *	   address will be chosen.
19517 	 *
19518 	 *	4. If there are no non-deprecated ipifs, then just use
19519 	 *	   the source address associated with the last deprecated
19520 	 *	   one we find that happens to be on the same subnet,
19521 	 *	   otherwise the first one not in the same subnet.
19522 	 */
19523 	for (; till != NULL; till = till->ill_group_next) {
19524 		ipif_same_found = B_FALSE;
19525 		ipif_other_found = B_FALSE;
19526 		for (ipif = till->ill_ipif; ipif != NULL;
19527 		    ipif = ipif->ipif_next) {
19528 			if (!IPIF_CAN_LOOKUP(ipif))
19529 				continue;
19530 			/* Always skip NOLOCAL and ANYCAST interfaces */
19531 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19532 				continue;
19533 			if (!(ipif->ipif_flags & IPIF_UP))
19534 				continue;
19535 			if (ipif->ipif_zoneid != zoneid)
19536 				continue;
19537 			/*
19538 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19539 			 * but are not valid as source addresses.
19540 			 */
19541 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19542 				continue;
19543 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19544 				if (ipif_dep == NULL ||
19545 				    (ipif->ipif_net_mask & dst) ==
19546 				    ipif->ipif_subnet)
19547 					ipif_dep = ipif;
19548 				continue;
19549 			}
19550 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19551 				/* found a source address in the same subnet */
19552 				if (same_subnet_only == B_FALSE) {
19553 					same_subnet_only = B_TRUE;
19554 					index = 0;
19555 				}
19556 				ipif_same_found = B_TRUE;
19557 			} else {
19558 				if (same_subnet_only == B_TRUE ||
19559 				    ipif_other_found == B_TRUE)
19560 					continue;
19561 				ipif_other_found = B_TRUE;
19562 			}
19563 			ipif_arr[index++] = ipif;
19564 			if (index == MAX_IPIF_SELECT_SOURCE) {
19565 				wrapped = B_TRUE;
19566 				index = 0;
19567 			}
19568 			if (ipif_same_found == B_TRUE)
19569 				break;
19570 		}
19571 	}
19572 
19573 	if (ipif_arr[0] == NULL) {
19574 		ipif = ipif_dep;
19575 	} else {
19576 		if (wrapped)
19577 			index = MAX_IPIF_SELECT_SOURCE;
19578 		ipif = ipif_arr[ipif_rand() % index];
19579 		ASSERT(ipif != NULL);
19580 	}
19581 
19582 	if (ipif != NULL) {
19583 		mutex_enter(&ipif->ipif_ill->ill_lock);
19584 		if (!IPIF_CAN_LOOKUP(ipif)) {
19585 			mutex_exit(&ipif->ipif_ill->ill_lock);
19586 			goto retry;
19587 		}
19588 		ipif_refhold_locked(ipif);
19589 		mutex_exit(&ipif->ipif_ill->ill_lock);
19590 	}
19591 
19592 	rw_exit(&ill_g_lock);
19593 	if (usill != NULL)
19594 		ill_refrele(usill);
19595 
19596 #ifdef DEBUG
19597 	if (ipif == NULL) {
19598 		char buf1[INET6_ADDRSTRLEN];
19599 
19600 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
19601 		    ill->ill_name,
19602 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
19603 	} else {
19604 		char buf1[INET6_ADDRSTRLEN];
19605 		char buf2[INET6_ADDRSTRLEN];
19606 
19607 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
19608 		    ipif->ipif_ill->ill_name,
19609 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
19610 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
19611 		    buf2, sizeof (buf2))));
19612 	}
19613 #endif /* DEBUG */
19614 	return (ipif);
19615 }
19616 
19617 
19618 /*
19619  * If old_ipif is not NULL, see if ipif was derived from old
19620  * ipif and if so, recreate the interface route by re-doing
19621  * source address selection. This happens when ipif_down ->
19622  * ipif_update_other_ipifs calls us.
19623  *
19624  * If old_ipif is NULL, just redo the source address selection
19625  * if needed. This happens when illgrp_insert or ipif_up_done
19626  * calls us.
19627  */
19628 static void
19629 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
19630 {
19631 	ire_t *ire;
19632 	ire_t *ipif_ire;
19633 	queue_t *stq;
19634 	ipif_t *nipif;
19635 	ill_t *ill;
19636 	boolean_t need_rele = B_FALSE;
19637 
19638 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
19639 	ASSERT(IAM_WRITER_IPIF(ipif));
19640 
19641 	ill = ipif->ipif_ill;
19642 	if (!(ipif->ipif_flags &
19643 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
19644 		/*
19645 		 * Can't possibly have borrowed the source
19646 		 * from old_ipif.
19647 		 */
19648 		return;
19649 	}
19650 
19651 	/*
19652 	 * Is there any work to be done? No work if the address
19653 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
19654 	 * ipif_select_source() does not borrow addresses from
19655 	 * NOLOCAL and ANYCAST interfaces).
19656 	 */
19657 	if ((old_ipif != NULL) &&
19658 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
19659 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
19660 	    (old_ipif->ipif_flags &
19661 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
19662 		return;
19663 	}
19664 
19665 	/*
19666 	 * Perform the same checks as when creating the
19667 	 * IRE_INTERFACE in ipif_up_done.
19668 	 */
19669 	if (!(ipif->ipif_flags & IPIF_UP))
19670 		return;
19671 
19672 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
19673 	    (ipif->ipif_subnet == INADDR_ANY))
19674 		return;
19675 
19676 	ipif_ire = ipif_to_ire(ipif);
19677 	if (ipif_ire == NULL)
19678 		return;
19679 
19680 	/*
19681 	 * We know that ipif uses some other source for its
19682 	 * IRE_INTERFACE. Is it using the source of this
19683 	 * old_ipif?
19684 	 */
19685 	if (old_ipif != NULL &&
19686 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
19687 		ire_refrele(ipif_ire);
19688 		return;
19689 	}
19690 	if (ip_debug > 2) {
19691 		/* ip1dbg */
19692 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
19693 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
19694 	}
19695 
19696 	stq = ipif_ire->ire_stq;
19697 
19698 	/*
19699 	 * Can't use our source address. Select a different
19700 	 * source address for the IRE_INTERFACE.
19701 	 */
19702 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
19703 	if (nipif == NULL) {
19704 		/* Last resort - all ipif's have IPIF_NOLOCAL */
19705 		nipif = ipif;
19706 	} else {
19707 		need_rele = B_TRUE;
19708 	}
19709 
19710 	ire = ire_create(
19711 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
19712 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
19713 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
19714 	    NULL,				/* no gateway */
19715 	    NULL,
19716 	    &ipif->ipif_mtu,			/* max frag */
19717 	    NULL,				/* fast path header */
19718 	    NULL,				/* no recv from queue */
19719 	    stq,				/* send-to queue */
19720 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19721 	    ill->ill_resolver_mp,		/* xmit header */
19722 	    ipif,
19723 	    NULL,
19724 	    0,
19725 	    0,
19726 	    0,
19727 	    0,
19728 	    &ire_uinfo_null);
19729 
19730 	if (ire != NULL) {
19731 		ire_t *ret_ire;
19732 		int error;
19733 
19734 		/*
19735 		 * We don't need ipif_ire anymore. We need to delete
19736 		 * before we add so that ire_add does not detect
19737 		 * duplicates.
19738 		 */
19739 		ire_delete(ipif_ire);
19740 		ret_ire = ire;
19741 		error = ire_add(&ret_ire, NULL, NULL, NULL);
19742 		ASSERT(error == 0);
19743 		ASSERT(ire == ret_ire);
19744 		/* Held in ire_add */
19745 		ire_refrele(ret_ire);
19746 	}
19747 	/*
19748 	 * Either we are falling through from above or could not
19749 	 * allocate a replacement.
19750 	 */
19751 	ire_refrele(ipif_ire);
19752 	if (need_rele)
19753 		ipif_refrele(nipif);
19754 }
19755 
19756 /*
19757  * This old_ipif is going away.
19758  *
19759  * Determine if any other ipif's is using our address as
19760  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
19761  * IPIF_DEPRECATED).
19762  * Find the IRE_INTERFACE for such ipifs and recreate them
19763  * to use an different source address following the rules in
19764  * ipif_up_done.
19765  *
19766  * This function takes an illgrp as an argument so that illgrp_delete
19767  * can call this to update source address even after deleting the
19768  * old_ipif->ipif_ill from the ill group.
19769  */
19770 static void
19771 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
19772 {
19773 	ipif_t *ipif;
19774 	ill_t *ill;
19775 	char	buf[INET6_ADDRSTRLEN];
19776 
19777 	ASSERT(IAM_WRITER_IPIF(old_ipif));
19778 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
19779 
19780 	ill = old_ipif->ipif_ill;
19781 
19782 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
19783 	    ill->ill_name,
19784 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
19785 	    buf, sizeof (buf))));
19786 	/*
19787 	 * If this part of a group, look at all ills as ipif_select_source
19788 	 * borrows source address across all the ills in the group.
19789 	 */
19790 	if (illgrp != NULL)
19791 		ill = illgrp->illgrp_ill;
19792 
19793 	for (; ill != NULL; ill = ill->ill_group_next) {
19794 		for (ipif = ill->ill_ipif; ipif != NULL;
19795 		    ipif = ipif->ipif_next) {
19796 
19797 			if (ipif == old_ipif)
19798 				continue;
19799 
19800 			ipif_recreate_interface_routes(old_ipif, ipif);
19801 		}
19802 	}
19803 }
19804 
19805 /* ARGSUSED */
19806 int
19807 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19808 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19809 {
19810 	/*
19811 	 * ill_phyint_reinit merged the v4 and v6 into a single
19812 	 * ipsq. Could also have become part of a ipmp group in the
19813 	 * process, and we might not have been able to complete the
19814 	 * operation in ipif_set_values, if we could not become
19815 	 * exclusive.  If so restart it here.
19816 	 */
19817 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
19818 }
19819 
19820 
19821 /* ARGSUSED */
19822 int
19823 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19824     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19825 {
19826 	queue_t		*q1 = q;
19827 	char 		*cp;
19828 	char		interf_name[LIFNAMSIZ];
19829 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
19830 
19831 	if (!q->q_next) {
19832 		ip1dbg((
19833 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
19834 		return (EINVAL);
19835 	}
19836 
19837 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
19838 		return (EALREADY);
19839 
19840 	do {
19841 		q1 = q1->q_next;
19842 	} while (q1->q_next);
19843 	cp = q1->q_qinfo->qi_minfo->mi_idname;
19844 	(void) sprintf(interf_name, "%s%d", cp, ppa);
19845 
19846 	/*
19847 	 * Here we are not going to delay the ioack until after
19848 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
19849 	 * original ioctl message before sending the requests.
19850 	 */
19851 	return (ipif_set_values(q, mp, interf_name, &ppa));
19852 }
19853 
19854 /* ARGSUSED */
19855 int
19856 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19857     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19858 {
19859 	return (ENXIO);
19860 }
19861 
19862 /*
19863  * Net and subnet broadcast ire's are now specific to the particular
19864  * physical interface (ill) and not to any one locigal interface (ipif).
19865  * However, if a particular logical interface is being taken down, it's
19866  * associated ire's will be taken down as well.  Hence, when we go to
19867  * take down or change the local address, broadcast address or netmask
19868  * of a specific logical interface, we must check to make sure that we
19869  * have valid net and subnet broadcast ire's for the other logical
19870  * interfaces which may have been shared with the logical interface
19871  * being brought down or changed.
19872  *
19873  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
19874  * is tied to the first interface coming UP. If that ipif is going down,
19875  * we need to recreate them on the next valid ipif.
19876  *
19877  * Note: assume that the ipif passed in is still up so that it's IRE
19878  * entries are still valid.
19879  */
19880 static void
19881 ipif_check_bcast_ires(ipif_t *test_ipif)
19882 {
19883 	ipif_t	*ipif;
19884 	ire_t	*test_subnet_ire, *test_net_ire;
19885 	ire_t	*test_allzero_ire, *test_allone_ire;
19886 	ire_t	*ire_array[12];
19887 	ire_t	**irep = &ire_array[0];
19888 	ire_t	**irep1;
19889 
19890 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
19891 	ipaddr_t test_net_addr, test_subnet_addr;
19892 	ipaddr_t test_net_mask, test_subnet_mask;
19893 	boolean_t need_net_bcast_ire = B_FALSE;
19894 	boolean_t need_subnet_bcast_ire = B_FALSE;
19895 	boolean_t allzero_bcast_ire_created = B_FALSE;
19896 	boolean_t allone_bcast_ire_created = B_FALSE;
19897 	boolean_t net_bcast_ire_created = B_FALSE;
19898 	boolean_t subnet_bcast_ire_created = B_FALSE;
19899 
19900 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
19901 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
19902 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
19903 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
19904 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
19905 
19906 	ASSERT(!test_ipif->ipif_isv6);
19907 	ASSERT(IAM_WRITER_IPIF(test_ipif));
19908 
19909 	/*
19910 	 * No broadcast IREs for the LOOPBACK interface
19911 	 * or others such as point to point and IPIF_NOXMIT.
19912 	 */
19913 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
19914 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
19915 		return;
19916 
19917 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
19918 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19919 
19920 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
19921 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19922 
19923 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
19924 	test_subnet_mask = test_ipif->ipif_net_mask;
19925 
19926 	/*
19927 	 * If no net mask set, assume the default based on net class.
19928 	 */
19929 	if (test_subnet_mask == 0)
19930 		test_subnet_mask = test_net_mask;
19931 
19932 	/*
19933 	 * Check if there is a network broadcast ire associated with this ipif
19934 	 */
19935 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
19936 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
19937 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19938 
19939 	/*
19940 	 * Check if there is a subnet broadcast IRE associated with this ipif
19941 	 */
19942 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
19943 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
19944 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19945 
19946 	/*
19947 	 * No broadcast ire's associated with this ipif.
19948 	 */
19949 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
19950 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
19951 		return;
19952 	}
19953 
19954 	/*
19955 	 * We have established which bcast ires have to be replaced.
19956 	 * Next we try to locate ipifs that match there ires.
19957 	 * The rules are simple: If we find an ipif that matches on the subnet
19958 	 * address it will also match on the net address, the allzeros and
19959 	 * allones address. Any ipif that matches only on the net address will
19960 	 * also match the allzeros and allones addresses.
19961 	 * The other criterion is the ipif_flags. We look for non-deprecated
19962 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
19963 	 * ipifs with check_flags matching (deprecated, etc) are used only
19964 	 * if good ipifs are not available. While looping, we save existing
19965 	 * deprecated ipifs as backup_ipif.
19966 	 * We loop through all the ipifs for this ill looking for ipifs
19967 	 * whose broadcast addr match the ipif passed in, but do not have
19968 	 * their own broadcast ires. For creating 0.0.0.0 and
19969 	 * 255.255.255.255 we just need an ipif on this ill to create.
19970 	 */
19971 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
19972 	    ipif = ipif->ipif_next) {
19973 
19974 		ASSERT(!ipif->ipif_isv6);
19975 		/*
19976 		 * Already checked the ipif passed in.
19977 		 */
19978 		if (ipif == test_ipif) {
19979 			continue;
19980 		}
19981 
19982 		/*
19983 		 * We only need to recreate broadcast ires if another ipif in
19984 		 * the same zone uses them. The new ires must be created in the
19985 		 * same zone.
19986 		 */
19987 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
19988 			continue;
19989 		}
19990 
19991 		/*
19992 		 * Only interested in logical interfaces with valid local
19993 		 * addresses or with the ability to broadcast.
19994 		 */
19995 		if ((ipif->ipif_subnet == 0) ||
19996 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
19997 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
19998 		    !(ipif->ipif_flags & IPIF_UP)) {
19999 			continue;
20000 		}
20001 		/*
20002 		 * Check if there is a net broadcast ire for this
20003 		 * net address.  If it turns out that the ipif we are
20004 		 * about to take down owns this ire, we must make a
20005 		 * new one because it is potentially going away.
20006 		 */
20007 		if (test_net_ire && (!net_bcast_ire_created)) {
20008 			net_mask = ip_net_mask(ipif->ipif_subnet);
20009 			net_addr = net_mask & ipif->ipif_subnet;
20010 			if (net_addr == test_net_addr) {
20011 				need_net_bcast_ire = B_TRUE;
20012 				/*
20013 				 * Use DEPRECATED ipif only if no good
20014 				 * ires are available. subnet_addr is
20015 				 * a better match than net_addr.
20016 				 */
20017 				if ((ipif->ipif_flags & check_flags) &&
20018 				    (backup_ipif_net == NULL)) {
20019 					backup_ipif_net = ipif;
20020 				}
20021 			}
20022 		}
20023 		/*
20024 		 * Check if there is a subnet broadcast ire for this
20025 		 * net address.  If it turns out that the ipif we are
20026 		 * about to take down owns this ire, we must make a
20027 		 * new one because it is potentially going away.
20028 		 */
20029 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20030 			subnet_mask = ipif->ipif_net_mask;
20031 			subnet_addr = ipif->ipif_subnet;
20032 			if (subnet_addr == test_subnet_addr) {
20033 				need_subnet_bcast_ire = B_TRUE;
20034 				if ((ipif->ipif_flags & check_flags) &&
20035 				    (backup_ipif_subnet == NULL)) {
20036 					backup_ipif_subnet = ipif;
20037 				}
20038 			}
20039 		}
20040 
20041 
20042 		/* Short circuit here if this ipif is deprecated */
20043 		if (ipif->ipif_flags & check_flags) {
20044 			if ((test_allzero_ire != NULL) &&
20045 			    (!allzero_bcast_ire_created) &&
20046 			    (backup_ipif_allzeros == NULL)) {
20047 				backup_ipif_allzeros = ipif;
20048 			}
20049 			if ((test_allone_ire != NULL) &&
20050 			    (!allone_bcast_ire_created) &&
20051 			    (backup_ipif_allones == NULL)) {
20052 				backup_ipif_allones = ipif;
20053 			}
20054 			continue;
20055 		}
20056 
20057 		/*
20058 		 * Found an ipif which has the same broadcast ire as the
20059 		 * ipif passed in and the ipif passed in "owns" the ire.
20060 		 * Create new broadcast ire's for this broadcast addr.
20061 		 */
20062 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20063 			irep = ire_create_bcast(ipif, net_addr, irep);
20064 			irep = ire_create_bcast(ipif,
20065 			    ~net_mask | net_addr, irep);
20066 			net_bcast_ire_created = B_TRUE;
20067 		}
20068 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20069 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20070 			irep = ire_create_bcast(ipif,
20071 			    ~subnet_mask | subnet_addr, irep);
20072 			subnet_bcast_ire_created = B_TRUE;
20073 		}
20074 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20075 			irep = ire_create_bcast(ipif, 0, irep);
20076 			allzero_bcast_ire_created = B_TRUE;
20077 		}
20078 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20079 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20080 			allone_bcast_ire_created = B_TRUE;
20081 		}
20082 		/*
20083 		 * Once we have created all the appropriate ires, we
20084 		 * just break out of this loop to add what we have created.
20085 		 * This has been indented similar to ire_match_args for
20086 		 * readability.
20087 		 */
20088 		if (((test_net_ire == NULL) ||
20089 			(net_bcast_ire_created)) &&
20090 		    ((test_subnet_ire == NULL) ||
20091 			(subnet_bcast_ire_created)) &&
20092 		    ((test_allzero_ire == NULL) ||
20093 			(allzero_bcast_ire_created)) &&
20094 		    ((test_allone_ire == NULL) ||
20095 			(allone_bcast_ire_created))) {
20096 			break;
20097 		}
20098 	}
20099 
20100 	/*
20101 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20102 	 * exist. 6 pairs of bcast ires are needed.
20103 	 * Note - the old ires are deleted in ipif_down.
20104 	 */
20105 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20106 		ipif = backup_ipif_net;
20107 		irep = ire_create_bcast(ipif, net_addr, irep);
20108 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20109 		net_bcast_ire_created = B_TRUE;
20110 	}
20111 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20112 	    backup_ipif_subnet) {
20113 		ipif = backup_ipif_subnet;
20114 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20115 		irep = ire_create_bcast(ipif,
20116 		    ~subnet_mask | subnet_addr, irep);
20117 		subnet_bcast_ire_created = B_TRUE;
20118 	}
20119 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20120 	    backup_ipif_allzeros) {
20121 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20122 		allzero_bcast_ire_created = B_TRUE;
20123 	}
20124 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20125 	    backup_ipif_allones) {
20126 		irep = ire_create_bcast(backup_ipif_allones,
20127 		    INADDR_BROADCAST, irep);
20128 		allone_bcast_ire_created = B_TRUE;
20129 	}
20130 
20131 	/*
20132 	 * If we can't create all of them, don't add any of them.
20133 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20134 	 * always have a non-loopback copy and loopback copy
20135 	 * for a given address.
20136 	 */
20137 	for (irep1 = irep; irep1 > ire_array; ) {
20138 		irep1--;
20139 		if (*irep1 == NULL) {
20140 			ip0dbg(("ipif_check_bcast_ires: can't create "
20141 			    "IRE_BROADCAST, memory allocation failure\n"));
20142 			while (irep > ire_array) {
20143 				irep--;
20144 				if (*irep != NULL)
20145 					ire_delete(*irep);
20146 			}
20147 			goto bad;
20148 		}
20149 	}
20150 	for (irep1 = irep; irep1 > ire_array; ) {
20151 		int error;
20152 
20153 		irep1--;
20154 		error = ire_add(irep1, NULL, NULL, NULL);
20155 		if (error == 0) {
20156 			ire_refrele(*irep1);		/* Held in ire_add */
20157 		}
20158 	}
20159 bad:
20160 	if (test_allzero_ire != NULL)
20161 		ire_refrele(test_allzero_ire);
20162 	if (test_allone_ire != NULL)
20163 		ire_refrele(test_allone_ire);
20164 	if (test_net_ire != NULL)
20165 		ire_refrele(test_net_ire);
20166 	if (test_subnet_ire != NULL)
20167 		ire_refrele(test_subnet_ire);
20168 }
20169 
20170 /*
20171  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20172  * from lifr_flags and the name from lifr_name.
20173  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20174  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20175  * Returns EINPROGRESS when mp has been consumed by queueing it on
20176  * ill_pending_mp and the ioctl will complete in ip_rput.
20177  */
20178 /* ARGSUSED */
20179 int
20180 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20181     ip_ioctl_cmd_t *ipip, void *if_req)
20182 {
20183 	int	err;
20184 	ill_t	*ill;
20185 	struct lifreq *lifr = (struct lifreq *)if_req;
20186 
20187 	ASSERT(ipif != NULL);
20188 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20189 	ASSERT(q->q_next != NULL);
20190 
20191 	ill = (ill_t *)q->q_ptr;
20192 	/*
20193 	 * If we are not writer on 'q' then this interface exists already
20194 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20195 	 * So return EALREADY
20196 	 */
20197 	if (ill != ipif->ipif_ill)
20198 		return (EALREADY);
20199 
20200 	if (ill->ill_name[0] != '\0')
20201 		return (EALREADY);
20202 
20203 	/*
20204 	 * Set all the flags. Allows all kinds of override. Provide some
20205 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20206 	 * unless there is either multicast/broadcast support in the driver
20207 	 * or it is a pt-pt link.
20208 	 */
20209 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20210 		/* Meaningless to IP thus don't allow them to be set. */
20211 		ip1dbg(("ip_setname: EINVAL 1\n"));
20212 		return (EINVAL);
20213 	}
20214 	/*
20215 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20216 	 * ill_bcast_addr_length info.
20217 	 */
20218 	if (!ill->ill_needs_attach &&
20219 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20220 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20221 	    ill->ill_bcast_addr_length == 0)) {
20222 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20223 		ip1dbg(("ip_setname: EINVAL 2\n"));
20224 		return (EINVAL);
20225 	}
20226 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20227 	    ((lifr->lifr_flags & IFF_IPV6) ||
20228 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20229 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20230 		ip1dbg(("ip_setname: EINVAL 3\n"));
20231 		return (EINVAL);
20232 	}
20233 	if (lifr->lifr_flags & IFF_UP) {
20234 		/* Can only be set with SIOCSLIFFLAGS */
20235 		ip1dbg(("ip_setname: EINVAL 4\n"));
20236 		return (EINVAL);
20237 	}
20238 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20239 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20240 		ip1dbg(("ip_setname: EINVAL 5\n"));
20241 		return (EINVAL);
20242 	}
20243 	/*
20244 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20245 	 */
20246 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20247 	    !(lifr->lifr_flags & IFF_IPV6) &&
20248 	    !(ipif->ipif_isv6)) {
20249 		ip1dbg(("ip_setname: EINVAL 6\n"));
20250 		return (EINVAL);
20251 	}
20252 
20253 	/*
20254 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20255 	 * we have all the flags here. So, we assign rather than we OR.
20256 	 * We can't OR the flags here because we don't want to set
20257 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20258 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20259 	 * on lifr_flags value here.
20260 	 */
20261 	/*
20262 	 * This ill has not been inserted into the global list.
20263 	 * So we are still single threaded and don't need any lock
20264 	 */
20265 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20266 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20267 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20268 
20269 	/* We started off as V4. */
20270 	if (ill->ill_flags & ILLF_IPV6) {
20271 		ill->ill_phyint->phyint_illv6 = ill;
20272 		ill->ill_phyint->phyint_illv4 = NULL;
20273 	}
20274 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20275 	return (err);
20276 }
20277 
20278 /* ARGSUSED */
20279 int
20280 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20281     ip_ioctl_cmd_t *ipip, void *if_req)
20282 {
20283 	/*
20284 	 * ill_phyint_reinit merged the v4 and v6 into a single
20285 	 * ipsq. Could also have become part of a ipmp group in the
20286 	 * process, and we might not have been able to complete the
20287 	 * slifname in ipif_set_values, if we could not become
20288 	 * exclusive.  If so restart it here
20289 	 */
20290 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20291 }
20292 
20293 /*
20294  * Return a pointer to the ipif which matches the index, IP version type and
20295  * zoneid.
20296  */
20297 ipif_t *
20298 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20299     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20300 {
20301 	ill_t	*ill;
20302 	ipsq_t  *ipsq;
20303 	phyint_t *phyi;
20304 	ipif_t	*ipif;
20305 
20306 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20307 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20308 
20309 	if (err != NULL)
20310 		*err = 0;
20311 
20312 	/*
20313 	 * Indexes are stored in the phyint - a common structure
20314 	 * to both IPv4 and IPv6.
20315 	 */
20316 
20317 	rw_enter(&ill_g_lock, RW_READER);
20318 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20319 	    (void *) &index, NULL);
20320 	if (phyi != NULL) {
20321 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20322 		if (ill == NULL) {
20323 			rw_exit(&ill_g_lock);
20324 			if (err != NULL)
20325 				*err = ENXIO;
20326 			return (NULL);
20327 		}
20328 		GRAB_CONN_LOCK(q);
20329 		mutex_enter(&ill->ill_lock);
20330 		if (ILL_CAN_LOOKUP(ill)) {
20331 			for (ipif = ill->ill_ipif; ipif != NULL;
20332 			    ipif = ipif->ipif_next) {
20333 				if (IPIF_CAN_LOOKUP(ipif) &&
20334 				    (zoneid == ALL_ZONES ||
20335 				    zoneid == ipif->ipif_zoneid)) {
20336 					ipif_refhold_locked(ipif);
20337 					mutex_exit(&ill->ill_lock);
20338 					RELEASE_CONN_LOCK(q);
20339 					rw_exit(&ill_g_lock);
20340 					return (ipif);
20341 				}
20342 			}
20343 		} else if (ILL_CAN_WAIT(ill, q)) {
20344 			ipsq = ill->ill_phyint->phyint_ipsq;
20345 			mutex_enter(&ipsq->ipsq_lock);
20346 			rw_exit(&ill_g_lock);
20347 			mutex_exit(&ill->ill_lock);
20348 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20349 			mutex_exit(&ipsq->ipsq_lock);
20350 			RELEASE_CONN_LOCK(q);
20351 			*err = EINPROGRESS;
20352 			return (NULL);
20353 		}
20354 		mutex_exit(&ill->ill_lock);
20355 		RELEASE_CONN_LOCK(q);
20356 	}
20357 	rw_exit(&ill_g_lock);
20358 	if (err != NULL)
20359 		*err = ENXIO;
20360 	return (NULL);
20361 }
20362 
20363 typedef struct conn_change_s {
20364 	uint_t cc_old_ifindex;
20365 	uint_t cc_new_ifindex;
20366 } conn_change_t;
20367 
20368 /*
20369  * ipcl_walk function for changing interface index.
20370  */
20371 static void
20372 conn_change_ifindex(conn_t *connp, caddr_t arg)
20373 {
20374 	conn_change_t *connc;
20375 	uint_t old_ifindex;
20376 	uint_t new_ifindex;
20377 	int i;
20378 	ilg_t *ilg;
20379 
20380 	connc = (conn_change_t *)arg;
20381 	old_ifindex = connc->cc_old_ifindex;
20382 	new_ifindex = connc->cc_new_ifindex;
20383 
20384 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20385 		connp->conn_orig_bound_ifindex = new_ifindex;
20386 
20387 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20388 		connp->conn_orig_multicast_ifindex = new_ifindex;
20389 
20390 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20391 		connp->conn_orig_xmit_ifindex = new_ifindex;
20392 
20393 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20394 		ilg = &connp->conn_ilg[i];
20395 		if (ilg->ilg_orig_ifindex == old_ifindex)
20396 			ilg->ilg_orig_ifindex = new_ifindex;
20397 	}
20398 }
20399 
20400 /*
20401  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20402  * to new_index if it matches the old_index.
20403  *
20404  * Failovers typically happen within a group of ills. But somebody
20405  * can remove an ill from the group after a failover happened. If
20406  * we are setting the ifindex after this, we potentially need to
20407  * look at all the ills rather than just the ones in the group.
20408  * We cut down the work by looking at matching ill_net_types
20409  * and ill_types as we could not possibly grouped them together.
20410  */
20411 static void
20412 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20413 {
20414 	ill_t *ill;
20415 	ipif_t *ipif;
20416 	uint_t old_ifindex;
20417 	uint_t new_ifindex;
20418 	ilm_t *ilm;
20419 	ill_walk_context_t ctx;
20420 
20421 	old_ifindex = connc->cc_old_ifindex;
20422 	new_ifindex = connc->cc_new_ifindex;
20423 
20424 	rw_enter(&ill_g_lock, RW_READER);
20425 	ill = ILL_START_WALK_ALL(&ctx);
20426 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20427 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20428 			(ill_orig->ill_type != ill->ill_type)) {
20429 			continue;
20430 		}
20431 		for (ipif = ill->ill_ipif; ipif != NULL;
20432 				ipif = ipif->ipif_next) {
20433 			if (ipif->ipif_orig_ifindex == old_ifindex)
20434 				ipif->ipif_orig_ifindex = new_ifindex;
20435 		}
20436 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20437 			if (ilm->ilm_orig_ifindex == old_ifindex)
20438 				ilm->ilm_orig_ifindex = new_ifindex;
20439 		}
20440 	}
20441 	rw_exit(&ill_g_lock);
20442 }
20443 
20444 /*
20445  * We first need to ensure that the new index is unique, and
20446  * then carry the change across both v4 and v6 ill representation
20447  * of the physical interface.
20448  */
20449 /* ARGSUSED */
20450 int
20451 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20452     ip_ioctl_cmd_t *ipip, void *ifreq)
20453 {
20454 	ill_t		*ill;
20455 	ill_t		*ill_other;
20456 	phyint_t	*phyi;
20457 	int		old_index;
20458 	conn_change_t	connc;
20459 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20460 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20461 	uint_t	index;
20462 	ill_t	*ill_v4;
20463 	ill_t	*ill_v6;
20464 
20465 	if (ipip->ipi_cmd_type == IF_CMD)
20466 		index = ifr->ifr_index;
20467 	else
20468 		index = lifr->lifr_index;
20469 
20470 	/*
20471 	 * Only allow on physical interface. Also, index zero is illegal.
20472 	 *
20473 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20474 	 *
20475 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20476 	 *    implies a possible failback might have to happen. As failback
20477 	 *    depends on the old index, we should fail setting the index.
20478 	 *
20479 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20480 	 *    any addresses or multicast memberships are failed over to
20481 	 *    a non-STANDBY interface. As failback depends on the old
20482 	 *    index, we should fail setting the index for this case also.
20483 	 *
20484 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20485 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20486 	 */
20487 	ill = ipif->ipif_ill;
20488 	phyi = ill->ill_phyint;
20489 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20490 	    ipif->ipif_id != 0 || index == 0) {
20491 		return (EINVAL);
20492 	}
20493 	old_index = phyi->phyint_ifindex;
20494 
20495 	/* If the index is not changing, no work to do */
20496 	if (old_index == index)
20497 		return (0);
20498 
20499 	/*
20500 	 * Use ill_lookup_on_ifindex to determine if the
20501 	 * new index is unused and if so allow the change.
20502 	 */
20503 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20504 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20505 	if (ill_v6 != NULL || ill_v4 != NULL) {
20506 		if (ill_v4 != NULL)
20507 			ill_refrele(ill_v4);
20508 		if (ill_v6 != NULL)
20509 			ill_refrele(ill_v6);
20510 		return (EBUSY);
20511 	}
20512 
20513 	/*
20514 	 * The new index is unused. Set it in the phyint.
20515 	 * Locate the other ill so that we can send a routing
20516 	 * sockets message.
20517 	 */
20518 	if (ill->ill_isv6) {
20519 		ill_other = phyi->phyint_illv4;
20520 	} else {
20521 		ill_other = phyi->phyint_illv6;
20522 	}
20523 
20524 	phyi->phyint_ifindex = index;
20525 
20526 	connc.cc_old_ifindex = old_index;
20527 	connc.cc_new_ifindex = index;
20528 	ip_change_ifindex(ill, &connc);
20529 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20530 
20531 	/* Send the routing sockets message */
20532 	ip_rts_ifmsg(ipif);
20533 	if (ill_other != NULL)
20534 		ip_rts_ifmsg(ill_other->ill_ipif);
20535 
20536 	return (0);
20537 }
20538 
20539 /* ARGSUSED */
20540 int
20541 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20542     ip_ioctl_cmd_t *ipip, void *ifreq)
20543 {
20544 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20545 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20546 
20547 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20548 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20549 	/* Get the interface index */
20550 	if (ipip->ipi_cmd_type == IF_CMD) {
20551 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20552 	} else {
20553 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20554 	}
20555 	return (0);
20556 }
20557 
20558 /* ARGSUSED */
20559 int
20560 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20561     ip_ioctl_cmd_t *ipip, void *ifreq)
20562 {
20563 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20564 
20565 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20566 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20567 	/* Get the interface zone */
20568 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20569 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20570 	return (0);
20571 }
20572 
20573 /*
20574  * Set the zoneid of an interface.
20575  */
20576 /* ARGSUSED */
20577 int
20578 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20579     ip_ioctl_cmd_t *ipip, void *ifreq)
20580 {
20581 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20582 	int err = 0;
20583 	boolean_t need_up = B_FALSE;
20584 	zone_t *zptr;
20585 	zone_status_t status;
20586 	zoneid_t zoneid;
20587 
20588 	/* cannot assign instance zero to a non-global zone */
20589 	if (ipif->ipif_id == 0)
20590 		return (ENOTSUP);
20591 
20592 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20593 	zoneid = lifr->lifr_zoneid;
20594 
20595 	/*
20596 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
20597 	 * the event of a race with the zone shutdown processing, since IP
20598 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
20599 	 * interface will be cleaned up even if the zone is shut down
20600 	 * immediately after the status check. If the interface can't be brought
20601 	 * down right away, and the zone is shut down before the restart
20602 	 * function is called, we resolve the possible races by rechecking the
20603 	 * zone status in the restart function.
20604 	 */
20605 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
20606 		return (EINVAL);
20607 	status = zone_status_get(zptr);
20608 	zone_rele(zptr);
20609 
20610 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
20611 		return (EINVAL);
20612 
20613 	if (ipif->ipif_flags & IPIF_UP) {
20614 		/*
20615 		 * If the interface is already marked up,
20616 		 * we call ipif_down which will take care
20617 		 * of ditching any IREs that have been set
20618 		 * up based on the old interface address.
20619 		 */
20620 		err = ipif_logical_down(ipif, q, mp);
20621 		if (err == EINPROGRESS)
20622 			return (err);
20623 		ipif_down_tail(ipif);
20624 		need_up = B_TRUE;
20625 	}
20626 
20627 	err = ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, need_up);
20628 	return (err);
20629 }
20630 
20631 static int
20632 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
20633     queue_t *q, mblk_t *mp, boolean_t need_up)
20634 {
20635 	int	err = 0;
20636 
20637 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
20638 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20639 
20640 	/* Set the new zone id. */
20641 	ipif->ipif_zoneid = zoneid;
20642 
20643 	/* Update sctp list */
20644 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
20645 
20646 	if (need_up) {
20647 		/*
20648 		 * Now bring the interface back up.  If this
20649 		 * is the only IPIF for the ILL, ipif_up
20650 		 * will have to re-bind to the device, so
20651 		 * we may get back EINPROGRESS, in which
20652 		 * case, this IOCTL will get completed in
20653 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
20654 		 */
20655 		err = ipif_up(ipif, q, mp);
20656 	}
20657 	return (err);
20658 }
20659 
20660 /* ARGSUSED */
20661 int
20662 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20663     ip_ioctl_cmd_t *ipip, void *if_req)
20664 {
20665 	struct lifreq *lifr = (struct lifreq *)if_req;
20666 	zoneid_t zoneid;
20667 	zone_t *zptr;
20668 	zone_status_t status;
20669 
20670 	ASSERT(ipif->ipif_id != 0);
20671 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20672 	zoneid = lifr->lifr_zoneid;
20673 
20674 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
20675 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20676 
20677 	/*
20678 	 * We recheck the zone status to resolve the following race condition:
20679 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
20680 	 * 2) hme0:1 is up and can't be brought down right away;
20681 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
20682 	 * 3) zone "myzone" is halted; the zone status switches to
20683 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
20684 	 * the interfaces to remove - hme0:1 is not returned because it's not
20685 	 * yet in "myzone", so it won't be removed;
20686 	 * 4) the restart function for SIOCSLIFZONE is called; without the
20687 	 * status check here, we would have hme0:1 in "myzone" after it's been
20688 	 * destroyed.
20689 	 * Note that if the status check fails, we need to bring the interface
20690 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
20691 	 * ipif_up_done[_v6]().
20692 	 */
20693 	status = ZONE_IS_UNINITIALIZED;
20694 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
20695 		status = zone_status_get(zptr);
20696 		zone_rele(zptr);
20697 	}
20698 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
20699 		if (ipif->ipif_isv6) {
20700 			(void) ipif_up_done_v6(ipif);
20701 		} else {
20702 			(void) ipif_up_done(ipif);
20703 		}
20704 		return (EINVAL);
20705 	}
20706 
20707 	ipif_down_tail(ipif);
20708 
20709 	return (ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, B_TRUE));
20710 }
20711 
20712 /* ARGSUSED */
20713 int
20714 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20715 	ip_ioctl_cmd_t *ipip, void *ifreq)
20716 {
20717 	struct lifreq	*lifr = ifreq;
20718 
20719 	ASSERT(q->q_next == NULL);
20720 	ASSERT(CONN_Q(q));
20721 
20722 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
20723 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20724 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
20725 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
20726 
20727 	return (0);
20728 }
20729 
20730 
20731 /* Find the previous ILL in this usesrc group */
20732 static ill_t *
20733 ill_prev_usesrc(ill_t *uill)
20734 {
20735 	ill_t *ill;
20736 
20737 	for (ill = uill->ill_usesrc_grp_next;
20738 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
20739 	    ill = ill->ill_usesrc_grp_next)
20740 		/* do nothing */;
20741 	return (ill);
20742 }
20743 
20744 /*
20745  * Release all members of the usesrc group. This routine is called
20746  * from ill_delete when the interface being unplumbed is the
20747  * group head.
20748  */
20749 static void
20750 ill_disband_usesrc_group(ill_t *uill)
20751 {
20752 	ill_t *next_ill, *tmp_ill;
20753 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
20754 	next_ill = uill->ill_usesrc_grp_next;
20755 
20756 	do {
20757 		ASSERT(next_ill != NULL);
20758 		tmp_ill = next_ill->ill_usesrc_grp_next;
20759 		ASSERT(tmp_ill != NULL);
20760 		next_ill->ill_usesrc_grp_next = NULL;
20761 		next_ill->ill_usesrc_ifindex = 0;
20762 		next_ill = tmp_ill;
20763 	} while (next_ill->ill_usesrc_ifindex != 0);
20764 	uill->ill_usesrc_grp_next = NULL;
20765 }
20766 
20767 /*
20768  * Remove the client usesrc ILL from the list and relink to a new list
20769  */
20770 int
20771 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
20772 {
20773 	ill_t *ill, *tmp_ill;
20774 
20775 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
20776 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
20777 
20778 	/*
20779 	 * Check if the usesrc client ILL passed in is not already
20780 	 * in use as a usesrc ILL i.e one whose source address is
20781 	 * in use OR a usesrc ILL is not already in use as a usesrc
20782 	 * client ILL
20783 	 */
20784 	if ((ucill->ill_usesrc_ifindex == 0) ||
20785 	    (uill->ill_usesrc_ifindex != 0)) {
20786 		return (-1);
20787 	}
20788 
20789 	ill = ill_prev_usesrc(ucill);
20790 	ASSERT(ill->ill_usesrc_grp_next != NULL);
20791 
20792 	/* Remove from the current list */
20793 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
20794 		/* Only two elements in the list */
20795 		ASSERT(ill->ill_usesrc_ifindex == 0);
20796 		ill->ill_usesrc_grp_next = NULL;
20797 	} else {
20798 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
20799 	}
20800 
20801 	if (ifindex == 0) {
20802 		ucill->ill_usesrc_ifindex = 0;
20803 		ucill->ill_usesrc_grp_next = NULL;
20804 		return (0);
20805 	}
20806 
20807 	ucill->ill_usesrc_ifindex = ifindex;
20808 	tmp_ill = uill->ill_usesrc_grp_next;
20809 	uill->ill_usesrc_grp_next = ucill;
20810 	ucill->ill_usesrc_grp_next =
20811 	    (tmp_ill != NULL) ? tmp_ill : uill;
20812 	return (0);
20813 }
20814 
20815 /*
20816  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
20817  * ip.c for locking details.
20818  */
20819 /* ARGSUSED */
20820 int
20821 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20822     ip_ioctl_cmd_t *ipip, void *ifreq)
20823 {
20824 	struct lifreq *lifr = (struct lifreq *)ifreq;
20825 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
20826 	    ill_flag_changed = B_FALSE;
20827 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
20828 	int err = 0, ret;
20829 	uint_t ifindex;
20830 	phyint_t *us_phyint, *us_cli_phyint;
20831 	ipsq_t *ipsq = NULL;
20832 
20833 	ASSERT(IAM_WRITER_IPIF(ipif));
20834 	ASSERT(q->q_next == NULL);
20835 	ASSERT(CONN_Q(q));
20836 
20837 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
20838 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
20839 
20840 	ASSERT(us_cli_phyint != NULL);
20841 
20842 	/*
20843 	 * If the client ILL is being used for IPMP, abort.
20844 	 * Note, this can be done before ipsq_try_enter since we are already
20845 	 * exclusive on this ILL
20846 	 */
20847 	if ((us_cli_phyint->phyint_groupname != NULL) ||
20848 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
20849 		return (EINVAL);
20850 	}
20851 
20852 	ifindex = lifr->lifr_index;
20853 	if (ifindex == 0) {
20854 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
20855 			/* non usesrc group interface, nothing to reset */
20856 			return (0);
20857 		}
20858 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
20859 		/* valid reset request */
20860 		reset_flg = B_TRUE;
20861 	}
20862 
20863 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
20864 	    ip_process_ioctl, &err);
20865 
20866 	if (usesrc_ill == NULL) {
20867 		return (err);
20868 	}
20869 
20870 	/*
20871 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
20872 	 * group nor can either of the interfaces be used for standy. So
20873 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
20874 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
20875 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
20876 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
20877 	 * the usesrc_cli_ill
20878 	 */
20879 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
20880 	    NEW_OP, B_TRUE);
20881 	if (ipsq == NULL) {
20882 		err = EINPROGRESS;
20883 		/* Operation enqueued on the ipsq of the usesrc ILL */
20884 		goto done;
20885 	}
20886 
20887 	/* Check if the usesrc_ill is used for IPMP */
20888 	us_phyint = usesrc_ill->ill_phyint;
20889 	if ((us_phyint->phyint_groupname != NULL) ||
20890 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
20891 		err = EINVAL;
20892 		goto done;
20893 	}
20894 
20895 	/*
20896 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
20897 	 * already a client then return EINVAL
20898 	 */
20899 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
20900 		err = EINVAL;
20901 		goto done;
20902 	}
20903 
20904 	/*
20905 	 * If the ill_usesrc_ifindex field is already set to what it needs to
20906 	 * be then this is a duplicate operation.
20907 	 */
20908 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
20909 		err = 0;
20910 		goto done;
20911 	}
20912 
20913 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
20914 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
20915 	    usesrc_ill->ill_isv6));
20916 
20917 	/*
20918 	 * The next step ensures that no new ires will be created referencing
20919 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
20920 	 * we go through an ire walk deleting all ire caches that reference
20921 	 * the client ill. New ires referencing the client ill that are added
20922 	 * to the ire table before the ILL_CHANGING flag is set, will be
20923 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
20924 	 * the client ill while the ILL_CHANGING flag is set will be failed
20925 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
20926 	 * checks (under the ill_g_usesrc_lock) that the ire being added
20927 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
20928 	 * belong to the same usesrc group.
20929 	 */
20930 	mutex_enter(&usesrc_cli_ill->ill_lock);
20931 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
20932 	mutex_exit(&usesrc_cli_ill->ill_lock);
20933 	ill_flag_changed = B_TRUE;
20934 
20935 	if (ipif->ipif_isv6)
20936 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
20937 		    ALL_ZONES);
20938 	else
20939 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
20940 		    ALL_ZONES);
20941 
20942 	/*
20943 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
20944 	 * and the ill_usesrc_ifindex fields
20945 	 */
20946 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
20947 
20948 	if (reset_flg) {
20949 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
20950 		if (ret != 0) {
20951 			err = EINVAL;
20952 		}
20953 		rw_exit(&ill_g_usesrc_lock);
20954 		goto done;
20955 	}
20956 
20957 	/*
20958 	 * Four possibilities to consider:
20959 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
20960 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
20961 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
20962 	 * 4. Both are part of their respective usesrc groups
20963 	 */
20964 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
20965 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
20966 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
20967 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
20968 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
20969 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
20970 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
20971 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
20972 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
20973 		/* Insert at head of list */
20974 		usesrc_cli_ill->ill_usesrc_grp_next =
20975 		    usesrc_ill->ill_usesrc_grp_next;
20976 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
20977 	} else {
20978 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
20979 		    ifindex);
20980 		if (ret != 0)
20981 			err = EINVAL;
20982 	}
20983 	rw_exit(&ill_g_usesrc_lock);
20984 
20985 done:
20986 	if (ill_flag_changed) {
20987 		mutex_enter(&usesrc_cli_ill->ill_lock);
20988 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
20989 		mutex_exit(&usesrc_cli_ill->ill_lock);
20990 	}
20991 	if (ipsq != NULL)
20992 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
20993 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
20994 	ill_refrele(usesrc_ill);
20995 	return (err);
20996 }
20997 
20998 /*
20999  * comparison function used by avl.
21000  */
21001 static int
21002 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21003 {
21004 
21005 	uint_t index;
21006 
21007 	ASSERT(phyip != NULL && index_ptr != NULL);
21008 
21009 	index = *((uint_t *)index_ptr);
21010 	/*
21011 	 * let the phyint with the lowest index be on top.
21012 	 */
21013 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21014 		return (1);
21015 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21016 		return (-1);
21017 	return (0);
21018 }
21019 
21020 /*
21021  * comparison function used by avl.
21022  */
21023 static int
21024 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21025 {
21026 	ill_t *ill;
21027 	int res = 0;
21028 
21029 	ASSERT(phyip != NULL && name_ptr != NULL);
21030 
21031 	if (((phyint_t *)phyip)->phyint_illv4)
21032 		ill = ((phyint_t *)phyip)->phyint_illv4;
21033 	else
21034 		ill = ((phyint_t *)phyip)->phyint_illv6;
21035 	ASSERT(ill != NULL);
21036 
21037 	res = strcmp(ill->ill_name, (char *)name_ptr);
21038 	if (res > 0)
21039 		return (1);
21040 	else if (res < 0)
21041 		return (-1);
21042 	return (0);
21043 }
21044 /*
21045  * This function is called from ill_delete when the ill is being
21046  * unplumbed. We remove the reference from the phyint and we also
21047  * free the phyint when there are no more references to it.
21048  */
21049 static void
21050 ill_phyint_free(ill_t *ill)
21051 {
21052 	phyint_t *phyi;
21053 	phyint_t *next_phyint;
21054 	ipsq_t *cur_ipsq;
21055 
21056 	ASSERT(ill->ill_phyint != NULL);
21057 
21058 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21059 	phyi = ill->ill_phyint;
21060 	ill->ill_phyint = NULL;
21061 	/*
21062 	 * ill_init allocates a phyint always to store the copy
21063 	 * of flags relevant to phyint. At that point in time, we could
21064 	 * not assign the name and hence phyint_illv4/v6 could not be
21065 	 * initialized. Later in ipif_set_values, we assign the name to
21066 	 * the ill, at which point in time we assign phyint_illv4/v6.
21067 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21068 	 */
21069 	if (ill->ill_flags & ILLF_IPV6) {
21070 		phyi->phyint_illv6 = NULL;
21071 	} else {
21072 		phyi->phyint_illv4 = NULL;
21073 	}
21074 	/*
21075 	 * ipif_down removes it from the group when the last ipif goes
21076 	 * down.
21077 	 */
21078 	ASSERT(ill->ill_group == NULL);
21079 
21080 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21081 		return;
21082 
21083 	/*
21084 	 * Make sure this phyint was put in the list.
21085 	 */
21086 	if (phyi->phyint_ifindex > 0) {
21087 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21088 		    phyi);
21089 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21090 		    phyi);
21091 	}
21092 	/*
21093 	 * remove phyint from the ipsq list.
21094 	 */
21095 	cur_ipsq = phyi->phyint_ipsq;
21096 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21097 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21098 	} else {
21099 		next_phyint = cur_ipsq->ipsq_phyint_list;
21100 		while (next_phyint != NULL) {
21101 			if (next_phyint->phyint_ipsq_next == phyi) {
21102 				next_phyint->phyint_ipsq_next =
21103 					phyi->phyint_ipsq_next;
21104 				break;
21105 			}
21106 			next_phyint = next_phyint->phyint_ipsq_next;
21107 		}
21108 		ASSERT(next_phyint != NULL);
21109 	}
21110 	IPSQ_DEC_REF(cur_ipsq);
21111 
21112 	if (phyi->phyint_groupname_len != 0) {
21113 		ASSERT(phyi->phyint_groupname != NULL);
21114 		mi_free(phyi->phyint_groupname);
21115 	}
21116 	mi_free(phyi);
21117 }
21118 
21119 /*
21120  * Attach the ill to the phyint structure which can be shared by both
21121  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21122  * function is called from ipif_set_values and ill_lookup_on_name (for
21123  * loopback) where we know the name of the ill. We lookup the ill and if
21124  * there is one present already with the name use that phyint. Otherwise
21125  * reuse the one allocated by ill_init.
21126  */
21127 static void
21128 ill_phyint_reinit(ill_t *ill)
21129 {
21130 	boolean_t isv6 = ill->ill_isv6;
21131 	phyint_t *phyi_old;
21132 	phyint_t *phyi;
21133 	avl_index_t where = 0;
21134 	ill_t	*ill_other = NULL;
21135 	ipsq_t	*ipsq;
21136 
21137 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21138 
21139 	phyi_old = ill->ill_phyint;
21140 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21141 	    phyi_old->phyint_illv6 == NULL));
21142 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21143 	    phyi_old->phyint_illv4 == NULL));
21144 	ASSERT(phyi_old->phyint_ifindex == 0);
21145 
21146 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21147 	    ill->ill_name, &where);
21148 
21149 	/*
21150 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21151 	 *    the global list of ills. So no other thread could have located
21152 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21153 	 * 2. Now locate the other protocol instance of this ill.
21154 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21155 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21156 	 *    of neither ill can change.
21157 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21158 	 *    other ill.
21159 	 * 5. Release all locks.
21160 	 */
21161 
21162 	/*
21163 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21164 	 * we are initializing IPv4.
21165 	 */
21166 	if (phyi != NULL) {
21167 		ill_other = (isv6) ? phyi->phyint_illv4 :
21168 		    phyi->phyint_illv6;
21169 		ASSERT(ill_other->ill_phyint != NULL);
21170 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21171 		    (!isv6 && ill_other->ill_isv6));
21172 		GRAB_ILL_LOCKS(ill, ill_other);
21173 		/*
21174 		 * We are potentially throwing away phyint_flags which
21175 		 * could be different from the one that we obtain from
21176 		 * ill_other->ill_phyint. But it is okay as we are assuming
21177 		 * that the state maintained within IP is correct.
21178 		 */
21179 		mutex_enter(&phyi->phyint_lock);
21180 		if (isv6) {
21181 			ASSERT(phyi->phyint_illv6 == NULL);
21182 			phyi->phyint_illv6 = ill;
21183 		} else {
21184 			ASSERT(phyi->phyint_illv4 == NULL);
21185 			phyi->phyint_illv4 = ill;
21186 		}
21187 		/*
21188 		 * This is a new ill, currently undergoing SLIFNAME
21189 		 * So we could not have joined an IPMP group until now.
21190 		 */
21191 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21192 		    phyi_old->phyint_groupname == NULL);
21193 
21194 		/*
21195 		 * This phyi_old is going away. Decref ipsq_refs and
21196 		 * assert it is zero. The ipsq itself will be freed in
21197 		 * ipsq_exit
21198 		 */
21199 		ipsq = phyi_old->phyint_ipsq;
21200 		IPSQ_DEC_REF(ipsq);
21201 		ASSERT(ipsq->ipsq_refs == 0);
21202 		/* Get the singleton phyint out of the ipsq list */
21203 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21204 		ipsq->ipsq_phyint_list = NULL;
21205 		phyi_old->phyint_illv4 = NULL;
21206 		phyi_old->phyint_illv6 = NULL;
21207 		mi_free(phyi_old);
21208 	} else {
21209 		mutex_enter(&ill->ill_lock);
21210 		/*
21211 		 * We don't need to acquire any lock, since
21212 		 * the ill is not yet visible globally  and we
21213 		 * have not yet released the ill_g_lock.
21214 		 */
21215 		phyi = phyi_old;
21216 		mutex_enter(&phyi->phyint_lock);
21217 		/* XXX We need a recovery strategy here. */
21218 		if (!phyint_assign_ifindex(phyi))
21219 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21220 
21221 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21222 		    (void *)phyi, where);
21223 
21224 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21225 		    &phyi->phyint_ifindex, &where);
21226 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21227 		    (void *)phyi, where);
21228 	}
21229 
21230 	/*
21231 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21232 	 * pending mp is not affected because that is per ill basis.
21233 	 */
21234 	ill->ill_phyint = phyi;
21235 
21236 	/*
21237 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21238 	 * We do this here as when the first ipif was allocated,
21239 	 * ipif_allocate does not know the right interface index.
21240 	 */
21241 
21242 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21243 	/*
21244 	 * Now that the phyint's ifindex has been assigned, complete the
21245 	 * remaining
21246 	 */
21247 	if (ill->ill_isv6) {
21248 		ill->ill_ip6_mib->ipv6IfIndex =
21249 		    ill->ill_phyint->phyint_ifindex;
21250 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21251 		    ill->ill_phyint->phyint_ifindex;
21252 	}
21253 
21254 	RELEASE_ILL_LOCKS(ill, ill_other);
21255 	mutex_exit(&phyi->phyint_lock);
21256 }
21257 
21258 /*
21259  * Notify any downstream modules of the name of this interface.
21260  * An M_IOCTL is used even though we don't expect a successful reply.
21261  * Any reply message from the driver (presumably an M_IOCNAK) will
21262  * eventually get discarded somewhere upstream.  The message format is
21263  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21264  * to IP.
21265  */
21266 static void
21267 ip_ifname_notify(ill_t *ill, queue_t *q)
21268 {
21269 	mblk_t *mp1, *mp2;
21270 	struct iocblk *iocp;
21271 	struct lifreq *lifr;
21272 
21273 	mp1 = mkiocb(SIOCSLIFNAME);
21274 	if (mp1 == NULL)
21275 		return;
21276 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21277 	if (mp2 == NULL) {
21278 		freeb(mp1);
21279 		return;
21280 	}
21281 
21282 	mp1->b_cont = mp2;
21283 	iocp = (struct iocblk *)mp1->b_rptr;
21284 	iocp->ioc_count = sizeof (struct lifreq);
21285 
21286 	lifr = (struct lifreq *)mp2->b_rptr;
21287 	mp2->b_wptr += sizeof (struct lifreq);
21288 	bzero(lifr, sizeof (struct lifreq));
21289 
21290 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21291 	lifr->lifr_ppa = ill->ill_ppa;
21292 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21293 
21294 	putnext(q, mp1);
21295 }
21296 
21297 static boolean_t ip_trash_timer_started = B_FALSE;
21298 
21299 static int
21300 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21301 {
21302 	int err;
21303 
21304 	/* Set the obsolete NDD per-interface forwarding name. */
21305 	err = ill_set_ndd_name(ill);
21306 	if (err != 0) {
21307 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21308 		    err);
21309 	}
21310 
21311 	/* Tell downstream modules where they are. */
21312 	ip_ifname_notify(ill, q);
21313 
21314 	/*
21315 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21316 	 * Error cases are ENOMEM ...
21317 	 */
21318 	err = ill_dl_phys(ill, ipif, mp, q);
21319 
21320 	/*
21321 	 * If there is no IRE expiration timer running, get one started.
21322 	 * igmp and mld timers will be triggered by the first multicast
21323 	 */
21324 	if (!ip_trash_timer_started) {
21325 		/*
21326 		 * acquire the lock and check again.
21327 		 */
21328 		mutex_enter(&ip_trash_timer_lock);
21329 		if (!ip_trash_timer_started) {
21330 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21331 			    MSEC_TO_TICK(ip_timer_interval));
21332 			ip_trash_timer_started = B_TRUE;
21333 		}
21334 		mutex_exit(&ip_trash_timer_lock);
21335 	}
21336 
21337 	if (ill->ill_isv6) {
21338 		mutex_enter(&mld_slowtimeout_lock);
21339 		if (mld_slowtimeout_id == 0) {
21340 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21341 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21342 		}
21343 		mutex_exit(&mld_slowtimeout_lock);
21344 	} else {
21345 		mutex_enter(&igmp_slowtimeout_lock);
21346 		if (igmp_slowtimeout_id == 0) {
21347 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21348 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21349 		}
21350 		mutex_exit(&igmp_slowtimeout_lock);
21351 	}
21352 
21353 	return (err);
21354 }
21355 
21356 /*
21357  * Common routine for ppa and ifname setting. Should be called exclusive.
21358  *
21359  * Returns EINPROGRESS when mp has been consumed by queueing it on
21360  * ill_pending_mp and the ioctl will complete in ip_rput.
21361  *
21362  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21363  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21364  * For SLIFNAME, we pass these values back to the userland.
21365  */
21366 static int
21367 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21368 {
21369 	ill_t	*ill;
21370 	ipif_t	*ipif;
21371 	ipsq_t	*ipsq;
21372 	char	*ppa_ptr;
21373 	char	*old_ptr;
21374 	char	old_char;
21375 	int	error;
21376 
21377 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21378 	ASSERT(q->q_next != NULL);
21379 	ASSERT(interf_name != NULL);
21380 
21381 	ill = (ill_t *)q->q_ptr;
21382 
21383 	ASSERT(ill->ill_name[0] == '\0');
21384 	ASSERT(IAM_WRITER_ILL(ill));
21385 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21386 	ASSERT(ill->ill_ppa == UINT_MAX);
21387 
21388 	/* The ppa is sent down by ifconfig or is chosen */
21389 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21390 		return (EINVAL);
21391 	}
21392 
21393 	/*
21394 	 * make sure ppa passed in is same as ppa in the name.
21395 	 * This check is not made when ppa == UINT_MAX in that case ppa
21396 	 * in the name could be anything. System will choose a ppa and
21397 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21398 	 */
21399 	if (*new_ppa_ptr != UINT_MAX) {
21400 		/* stoi changes the pointer */
21401 		old_ptr = ppa_ptr;
21402 		/*
21403 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21404 		 * (they don't have an externally visible ppa).  We assign one
21405 		 * here so that we can manage the interface.  Note that in
21406 		 * the past this value was always 0 for DLPI 1 drivers.
21407 		 */
21408 		if (*new_ppa_ptr == 0)
21409 			*new_ppa_ptr = stoi(&old_ptr);
21410 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21411 			return (EINVAL);
21412 	}
21413 	/*
21414 	 * terminate string before ppa
21415 	 * save char at that location.
21416 	 */
21417 	old_char = ppa_ptr[0];
21418 	ppa_ptr[0] = '\0';
21419 
21420 	ill->ill_ppa = *new_ppa_ptr;
21421 	/*
21422 	 * Finish as much work now as possible before calling ill_glist_insert
21423 	 * which makes the ill globally visible and also merges it with the
21424 	 * other protocol instance of this phyint. The remaining work is
21425 	 * done after entering the ipsq which may happen sometime later.
21426 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21427 	 */
21428 	ipif = ill->ill_ipif;
21429 
21430 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21431 	ipif_assign_seqid(ipif);
21432 
21433 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21434 		ill->ill_flags |= ILLF_IPV4;
21435 
21436 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21437 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21438 
21439 	if (ill->ill_flags & ILLF_IPV6) {
21440 
21441 		ill->ill_isv6 = B_TRUE;
21442 		if (ill->ill_rq != NULL) {
21443 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21444 			ill->ill_wq->q_qinfo = &winit_ipv6;
21445 		}
21446 
21447 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21448 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21449 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21450 		ipif->ipif_v6subnet = ipv6_all_zeros;
21451 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21452 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21453 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21454 		/*
21455 		 * point-to-point or Non-mulicast capable
21456 		 * interfaces won't do NUD unless explicitly
21457 		 * configured to do so.
21458 		 */
21459 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21460 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21461 			ill->ill_flags |= ILLF_NONUD;
21462 		}
21463 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21464 		if (ill->ill_flags & ILLF_NOARP) {
21465 			/*
21466 			 * Note: xresolv interfaces will eventually need
21467 			 * NOARP set here as well, but that will require
21468 			 * those external resolvers to have some
21469 			 * knowledge of that flag and act appropriately.
21470 			 * Not to be changed at present.
21471 			 */
21472 			ill->ill_flags &= ~ILLF_NOARP;
21473 		}
21474 		/*
21475 		 * Set the ILLF_ROUTER flag according to the global
21476 		 * IPv6 forwarding policy.
21477 		 */
21478 		if (ipv6_forward != 0)
21479 			ill->ill_flags |= ILLF_ROUTER;
21480 	} else if (ill->ill_flags & ILLF_IPV4) {
21481 		ill->ill_isv6 = B_FALSE;
21482 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21483 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21484 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21485 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21486 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21487 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21488 		/*
21489 		 * Set the ILLF_ROUTER flag according to the global
21490 		 * IPv4 forwarding policy.
21491 		 */
21492 		if (ip_g_forward != 0)
21493 			ill->ill_flags |= ILLF_ROUTER;
21494 	}
21495 
21496 	ASSERT(ill->ill_phyint != NULL);
21497 
21498 	/*
21499 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21500 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21501 	 */
21502 	if (ill->ill_isv6) {
21503 		/* allocate v6 mib */
21504 		if (!ill_allocate_mibs(ill))
21505 			return (ENOMEM);
21506 	}
21507 
21508 	/*
21509 	 * Pick a default sap until we get the DL_INFO_ACK back from
21510 	 * the driver.
21511 	 */
21512 	if (ill->ill_sap == 0) {
21513 		if (ill->ill_isv6)
21514 			ill->ill_sap  = IP6_DL_SAP;
21515 		else
21516 			ill->ill_sap  = IP_DL_SAP;
21517 	}
21518 
21519 	ill->ill_ifname_pending = 1;
21520 	ill->ill_ifname_pending_err = 0;
21521 
21522 	ill_refhold(ill);
21523 	rw_enter(&ill_g_lock, RW_WRITER);
21524 	if ((error = ill_glist_insert(ill, interf_name,
21525 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21526 		ill->ill_ppa = UINT_MAX;
21527 		ill->ill_name[0] = '\0';
21528 		/*
21529 		 * undo null termination done above.
21530 		 */
21531 		ppa_ptr[0] = old_char;
21532 		rw_exit(&ill_g_lock);
21533 		ill_refrele(ill);
21534 		return (error);
21535 	}
21536 
21537 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21538 
21539 	/*
21540 	 * When we return the buffer pointed to by interf_name should contain
21541 	 * the same name as in ill_name.
21542 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21543 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21544 	 * so copy full name and update the ppa ptr.
21545 	 * When ppa passed in != UINT_MAX all values are correct just undo
21546 	 * null termination, this saves a bcopy.
21547 	 */
21548 	if (*new_ppa_ptr == UINT_MAX) {
21549 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21550 		*new_ppa_ptr = ill->ill_ppa;
21551 	} else {
21552 		/*
21553 		 * undo null termination done above.
21554 		 */
21555 		ppa_ptr[0] = old_char;
21556 	}
21557 
21558 	/* Let SCTP know about this ILL */
21559 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21560 
21561 	/* and also about the first ipif */
21562 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21563 
21564 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21565 	    B_TRUE);
21566 
21567 	rw_exit(&ill_g_lock);
21568 	ill_refrele(ill);
21569 	if (ipsq == NULL)
21570 		return (EINPROGRESS);
21571 
21572 	/*
21573 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21574 	 * due to the phyint merge in ill_phyint_reinit.
21575 	 */
21576 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21577 		ipsq->ipsq_current_ipif == ipif);
21578 	ipsq->ipsq_current_ipif = ipif;
21579 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21580 	error = ipif_set_values_tail(ill, ipif, mp, q);
21581 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21582 	if (error != 0 && error != EINPROGRESS) {
21583 		/*
21584 		 * restore previous values
21585 		 */
21586 		ill->ill_isv6 = B_FALSE;
21587 	}
21588 	return (error);
21589 }
21590 
21591 
21592 extern void (*ip_cleanup_func)(void);
21593 
21594 void
21595 ipif_init(void)
21596 {
21597 	hrtime_t hrt;
21598 	int i;
21599 
21600 	/*
21601 	 * Can't call drv_getparm here as it is too early in the boot.
21602 	 * As we use ipif_src_random just for picking a different
21603 	 * source address everytime, this need not be really random.
21604 	 */
21605 	hrt = gethrtime();
21606 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
21607 
21608 	for (i = 0; i < MAX_G_HEADS; i++) {
21609 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
21610 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
21611 	}
21612 
21613 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
21614 	    ill_phyint_compare_index,
21615 	    sizeof (phyint_t),
21616 	    offsetof(struct phyint, phyint_avl_by_index));
21617 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
21618 	    ill_phyint_compare_name,
21619 	    sizeof (phyint_t),
21620 	    offsetof(struct phyint, phyint_avl_by_name));
21621 
21622 	ip_cleanup_func = ip_thread_exit;
21623 }
21624 
21625 /*
21626  * This is called by ip_rt_add when src_addr value is other than zero.
21627  * src_addr signifies the source address of the incoming packet. For
21628  * reverse tunnel route we need to create a source addr based routing
21629  * table. This routine creates ip_mrtun_table if it's empty and then
21630  * it adds the route entry hashed by source address. It verifies that
21631  * the outgoing interface is always a non-resolver interface (tunnel).
21632  */
21633 int
21634 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
21635     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
21636 {
21637 	ire_t   *ire;
21638 	ire_t	*save_ire;
21639 	ipif_t  *ipif;
21640 	ill_t   *in_ill = NULL;
21641 	ill_t	*out_ill;
21642 	queue_t	*stq;
21643 	mblk_t	*dlureq_mp;
21644 	int	error;
21645 
21646 	if (ire_arg != NULL)
21647 		*ire_arg = NULL;
21648 	ASSERT(in_src_addr != INADDR_ANY);
21649 
21650 	ipif = ipif_arg;
21651 	if (ipif != NULL) {
21652 		out_ill = ipif->ipif_ill;
21653 	} else {
21654 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
21655 		return (EINVAL);
21656 	}
21657 
21658 	if (src_ipif == NULL) {
21659 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
21660 		return (EINVAL);
21661 	}
21662 	in_ill = src_ipif->ipif_ill;
21663 
21664 	/*
21665 	 * Check for duplicates. We don't need to
21666 	 * match out_ill, because the uniqueness of
21667 	 * a route is only dependent on src_addr and
21668 	 * in_ill.
21669 	 */
21670 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
21671 	if (ire != NULL) {
21672 		ire_refrele(ire);
21673 		return (EEXIST);
21674 	}
21675 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
21676 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
21677 		    ipif->ipif_net_type));
21678 		return (EINVAL);
21679 	}
21680 
21681 	stq = ipif->ipif_wq;
21682 	ASSERT(stq != NULL);
21683 
21684 	/*
21685 	 * The outgoing interface must be non-resolver
21686 	 * interface.
21687 	 */
21688 	dlureq_mp = ill_dlur_gen(NULL,
21689 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
21690 	    out_ill->ill_sap_length);
21691 
21692 	if (dlureq_mp == NULL) {
21693 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
21694 		return (ENOMEM);
21695 	}
21696 
21697 	/* Create the IRE. */
21698 
21699 	ire = ire_create(
21700 	    NULL,				/* Zero dst addr */
21701 	    NULL,				/* Zero mask */
21702 	    NULL,				/* Zero gateway addr */
21703 	    NULL,				/* Zero ipif_src addr */
21704 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
21705 	    &ipif->ipif_mtu,
21706 	    NULL,
21707 	    NULL,				/* rfq */
21708 	    stq,
21709 	    IRE_MIPRTUN,
21710 	    dlureq_mp,
21711 	    ipif,
21712 	    in_ill,
21713 	    0,
21714 	    0,
21715 	    0,
21716 	    flags,
21717 	    &ire_uinfo_null);
21718 
21719 	if (ire == NULL)
21720 		return (ENOMEM);
21721 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
21722 	    ire->ire_type));
21723 	save_ire = ire;
21724 	ASSERT(save_ire != NULL);
21725 	error = ire_add_mrtun(&ire, q, mp, func);
21726 	/*
21727 	 * If ire_add_mrtun() failed, the ire passed in was freed
21728 	 * so there is no need to do so here.
21729 	 */
21730 	if (error != 0) {
21731 		return (error);
21732 	}
21733 
21734 	/* Duplicate check */
21735 	if (ire != save_ire) {
21736 		/* route already exists by now */
21737 		ire_refrele(ire);
21738 		return (EEXIST);
21739 	}
21740 
21741 	if (ire_arg != NULL) {
21742 		/*
21743 		 * Store the ire that was just added. the caller
21744 		 * ip_rts_request responsible for doing ire_refrele()
21745 		 * on it.
21746 		 */
21747 		*ire_arg = ire;
21748 	} else {
21749 		ire_refrele(ire);	/* held in ire_add_mrtun */
21750 	}
21751 
21752 	return (0);
21753 }
21754 
21755 /*
21756  * It is called by ip_rt_delete() only when mipagent requests to delete
21757  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
21758  */
21759 
21760 int
21761 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
21762 {
21763 	ire_t   *ire = NULL;
21764 
21765 	if (in_src_addr == INADDR_ANY)
21766 		return (EINVAL);
21767 	if (src_ipif == NULL)
21768 		return (EINVAL);
21769 
21770 	/* search if this route exists in the ip_mrtun_table */
21771 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
21772 	if (ire == NULL) {
21773 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
21774 		return (ESRCH);
21775 	}
21776 	ire_delete(ire);
21777 	ire_refrele(ire);
21778 	return (0);
21779 }
21780 
21781 /*
21782  * Lookup the ipif corresponding to the onlink destination address. For
21783  * point-to-point interfaces, it matches with remote endpoint destination
21784  * address. For point-to-multipoint interfaces it only tries to match the
21785  * destination with the interface's subnet address. The longest, most specific
21786  * match is found to take care of such rare network configurations like -
21787  * le0: 129.146.1.1/16
21788  * le1: 129.146.2.2/24
21789  * It is used only by SO_DONTROUTE at the moment.
21790  */
21791 ipif_t *
21792 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
21793 {
21794 	ipif_t	*ipif, *best_ipif;
21795 	ill_t	*ill;
21796 	ill_walk_context_t ctx;
21797 
21798 	ASSERT(zoneid != ALL_ZONES);
21799 	best_ipif = NULL;
21800 
21801 	rw_enter(&ill_g_lock, RW_READER);
21802 	ill = ILL_START_WALK_V4(&ctx);
21803 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21804 		mutex_enter(&ill->ill_lock);
21805 		for (ipif = ill->ill_ipif; ipif != NULL;
21806 		    ipif = ipif->ipif_next) {
21807 			if (!IPIF_CAN_LOOKUP(ipif))
21808 				continue;
21809 			if (ipif->ipif_zoneid != zoneid)
21810 				continue;
21811 			/*
21812 			 * Point-to-point case. Look for exact match with
21813 			 * destination address.
21814 			 */
21815 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
21816 				if (ipif->ipif_pp_dst_addr == addr) {
21817 					ipif_refhold_locked(ipif);
21818 					mutex_exit(&ill->ill_lock);
21819 					rw_exit(&ill_g_lock);
21820 					if (best_ipif != NULL)
21821 						ipif_refrele(best_ipif);
21822 					return (ipif);
21823 				}
21824 			} else if (ipif->ipif_subnet == (addr &
21825 			    ipif->ipif_net_mask)) {
21826 				/*
21827 				 * Point-to-multipoint case. Looping through to
21828 				 * find the most specific match. If there are
21829 				 * multiple best match ipif's then prefer ipif's
21830 				 * that are UP. If there is only one best match
21831 				 * ipif and it is DOWN we must still return it.
21832 				 */
21833 				if ((best_ipif == NULL) ||
21834 				    (ipif->ipif_net_mask >
21835 				    best_ipif->ipif_net_mask) ||
21836 				    ((ipif->ipif_net_mask ==
21837 				    best_ipif->ipif_net_mask) &&
21838 				    ((ipif->ipif_flags & IPIF_UP) &&
21839 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
21840 					ipif_refhold_locked(ipif);
21841 					mutex_exit(&ill->ill_lock);
21842 					rw_exit(&ill_g_lock);
21843 					if (best_ipif != NULL)
21844 						ipif_refrele(best_ipif);
21845 					best_ipif = ipif;
21846 					rw_enter(&ill_g_lock, RW_READER);
21847 					mutex_enter(&ill->ill_lock);
21848 				}
21849 			}
21850 		}
21851 		mutex_exit(&ill->ill_lock);
21852 	}
21853 	rw_exit(&ill_g_lock);
21854 	return (best_ipif);
21855 }
21856 
21857 
21858 /*
21859  * Save enough information so that we can recreate the IRE if
21860  * the interface goes down and then up.
21861  */
21862 static void
21863 ipif_save_ire(ipif_t *ipif, ire_t *ire)
21864 {
21865 	mblk_t	*save_mp;
21866 
21867 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
21868 	if (save_mp != NULL) {
21869 		ifrt_t	*ifrt;
21870 
21871 		save_mp->b_wptr += sizeof (ifrt_t);
21872 		ifrt = (ifrt_t *)save_mp->b_rptr;
21873 		bzero(ifrt, sizeof (ifrt_t));
21874 		ifrt->ifrt_type = ire->ire_type;
21875 		ifrt->ifrt_addr = ire->ire_addr;
21876 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
21877 		ifrt->ifrt_src_addr = ire->ire_src_addr;
21878 		ifrt->ifrt_mask = ire->ire_mask;
21879 		ifrt->ifrt_flags = ire->ire_flags;
21880 		ifrt->ifrt_max_frag = ire->ire_max_frag;
21881 		mutex_enter(&ipif->ipif_saved_ire_lock);
21882 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
21883 		ipif->ipif_saved_ire_mp = save_mp;
21884 		ipif->ipif_saved_ire_cnt++;
21885 		mutex_exit(&ipif->ipif_saved_ire_lock);
21886 	}
21887 }
21888 
21889 
21890 static void
21891 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
21892 {
21893 	mblk_t	**mpp;
21894 	mblk_t	*mp;
21895 	ifrt_t	*ifrt;
21896 
21897 	/* Remove from ipif_saved_ire_mp list if it is there */
21898 	mutex_enter(&ipif->ipif_saved_ire_lock);
21899 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
21900 	    mpp = &(*mpp)->b_cont) {
21901 		/*
21902 		 * On a given ipif, the triple of address, gateway and
21903 		 * mask is unique for each saved IRE (in the case of
21904 		 * ordinary interface routes, the gateway address is
21905 		 * all-zeroes).
21906 		 */
21907 		mp = *mpp;
21908 		ifrt = (ifrt_t *)mp->b_rptr;
21909 		if (ifrt->ifrt_addr == ire->ire_addr &&
21910 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
21911 		    ifrt->ifrt_mask == ire->ire_mask) {
21912 			*mpp = mp->b_cont;
21913 			ipif->ipif_saved_ire_cnt--;
21914 			freeb(mp);
21915 			break;
21916 		}
21917 	}
21918 	mutex_exit(&ipif->ipif_saved_ire_lock);
21919 }
21920 
21921 
21922 /*
21923  * IP multirouting broadcast routes handling
21924  * Append CGTP broadcast IREs to regular ones created
21925  * at ifconfig time.
21926  */
21927 static void
21928 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
21929 {
21930 	ire_t *ire_prim;
21931 
21932 	ASSERT(ire != NULL);
21933 	ASSERT(ire_dst != NULL);
21934 
21935 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
21936 	    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
21937 	if (ire_prim != NULL) {
21938 		/*
21939 		 * We are in the special case of broadcasts for
21940 		 * CGTP. We add an IRE_BROADCAST that holds
21941 		 * the RTF_MULTIRT flag, the destination
21942 		 * address of ire_dst and the low level
21943 		 * info of ire_prim. In other words, CGTP
21944 		 * broadcast is added to the redundant ipif.
21945 		 */
21946 		ipif_t *ipif_prim;
21947 		ire_t  *bcast_ire;
21948 
21949 		ipif_prim = ire_prim->ire_ipif;
21950 
21951 		ip2dbg(("ip_cgtp_filter_bcast_add: "
21952 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
21953 		    (void *)ire_dst, (void *)ire_prim,
21954 		    (void *)ipif_prim));
21955 
21956 		bcast_ire = ire_create(
21957 		    (uchar_t *)&ire->ire_addr,
21958 		    (uchar_t *)&ip_g_all_ones,
21959 		    (uchar_t *)&ire_dst->ire_src_addr,
21960 		    (uchar_t *)&ire->ire_gateway_addr,
21961 		    NULL,
21962 		    &ipif_prim->ipif_mtu,
21963 		    NULL,
21964 		    ipif_prim->ipif_rq,
21965 		    ipif_prim->ipif_wq,
21966 		    IRE_BROADCAST,
21967 		    ipif_prim->ipif_bcast_mp,
21968 		    ipif_prim,
21969 		    NULL,
21970 		    0,
21971 		    0,
21972 		    0,
21973 		    ire->ire_flags,
21974 		    &ire_uinfo_null);
21975 
21976 		if (bcast_ire != NULL) {
21977 
21978 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
21979 				ip2dbg(("ip_cgtp_filter_bcast_add: "
21980 				    "added bcast_ire %p\n",
21981 				    (void *)bcast_ire));
21982 
21983 				ipif_save_ire(bcast_ire->ire_ipif,
21984 				    bcast_ire);
21985 				ire_refrele(bcast_ire);
21986 			}
21987 		}
21988 		ire_refrele(ire_prim);
21989 	}
21990 }
21991 
21992 
21993 /*
21994  * IP multirouting broadcast routes handling
21995  * Remove the broadcast ire
21996  */
21997 static void
21998 ip_cgtp_bcast_delete(ire_t *ire)
21999 {
22000 	ire_t *ire_dst;
22001 
22002 	ASSERT(ire != NULL);
22003 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22004 	    NULL, NULL, MATCH_IRE_TYPE);
22005 	if (ire_dst != NULL) {
22006 		ire_t *ire_prim;
22007 
22008 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22009 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22010 		if (ire_prim != NULL) {
22011 			ipif_t *ipif_prim;
22012 			ire_t  *bcast_ire;
22013 
22014 			ipif_prim = ire_prim->ire_ipif;
22015 
22016 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22017 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22018 			    (void *)ire_dst, (void *)ire_prim,
22019 			    (void *)ipif_prim));
22020 
22021 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22022 			    ire->ire_gateway_addr,
22023 			    IRE_BROADCAST,
22024 			    ipif_prim,
22025 			    NULL,
22026 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22027 			    MATCH_IRE_MASK);
22028 
22029 			if (bcast_ire != NULL) {
22030 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22031 				    "looked up bcast_ire %p\n",
22032 				    (void *)bcast_ire));
22033 				ipif_remove_ire(bcast_ire->ire_ipif,
22034 					bcast_ire);
22035 				ire_delete(bcast_ire);
22036 			}
22037 			ire_refrele(ire_prim);
22038 		}
22039 		ire_refrele(ire_dst);
22040 	}
22041 }
22042 
22043 /*
22044  * IPsec hardware acceleration capabilities related functions.
22045  */
22046 
22047 /*
22048  * Free a per-ill IPsec capabilities structure.
22049  */
22050 static void
22051 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22052 {
22053 	if (capab->auth_hw_algs != NULL)
22054 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22055 	if (capab->encr_hw_algs != NULL)
22056 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22057 	if (capab->encr_algparm != NULL)
22058 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22059 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22060 }
22061 
22062 /*
22063  * Allocate a new per-ill IPsec capabilities structure. This structure
22064  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22065  * an array which specifies, for each algorithm, whether this algorithm
22066  * is supported by the ill or not.
22067  */
22068 static ill_ipsec_capab_t *
22069 ill_ipsec_capab_alloc(void)
22070 {
22071 	ill_ipsec_capab_t *capab;
22072 	uint_t nelems;
22073 
22074 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22075 	if (capab == NULL)
22076 		return (NULL);
22077 
22078 	/* we need one bit per algorithm */
22079 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22080 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22081 
22082 	/* allocate memory to store algorithm flags */
22083 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22084 	if (capab->encr_hw_algs == NULL)
22085 		goto nomem;
22086 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22087 	if (capab->auth_hw_algs == NULL)
22088 		goto nomem;
22089 	/*
22090 	 * Leave encr_algparm NULL for now since we won't need it half
22091 	 * the time
22092 	 */
22093 	return (capab);
22094 
22095 nomem:
22096 	ill_ipsec_capab_free(capab);
22097 	return (NULL);
22098 }
22099 
22100 /*
22101  * Resize capability array.  Since we're exclusive, this is OK.
22102  */
22103 static boolean_t
22104 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22105 {
22106 	ipsec_capab_algparm_t *nalp, *oalp;
22107 	uint32_t olen, nlen;
22108 
22109 	oalp = capab->encr_algparm;
22110 	olen = capab->encr_algparm_size;
22111 
22112 	if (oalp != NULL) {
22113 		if (algid < capab->encr_algparm_end)
22114 			return (B_TRUE);
22115 	}
22116 
22117 	nlen = (algid + 1) * sizeof (*nalp);
22118 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22119 	if (nalp == NULL)
22120 		return (B_FALSE);
22121 
22122 	if (oalp != NULL) {
22123 		bcopy(oalp, nalp, olen);
22124 		kmem_free(oalp, olen);
22125 	}
22126 	capab->encr_algparm = nalp;
22127 	capab->encr_algparm_size = nlen;
22128 	capab->encr_algparm_end = algid + 1;
22129 
22130 	return (B_TRUE);
22131 }
22132 
22133 /*
22134  * Compare the capabilities of the specified ill with the protocol
22135  * and algorithms specified by the SA passed as argument.
22136  * If they match, returns B_TRUE, B_FALSE if they do not match.
22137  *
22138  * The ill can be passed as a pointer to it, or by specifying its index
22139  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22140  *
22141  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22142  * packet is eligible for hardware acceleration, and by
22143  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22144  * to a particular ill.
22145  */
22146 boolean_t
22147 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22148     ipsa_t *sa)
22149 {
22150 	boolean_t sa_isv6;
22151 	uint_t algid;
22152 	struct ill_ipsec_capab_s *cpp;
22153 	boolean_t need_refrele = B_FALSE;
22154 
22155 	if (ill == NULL) {
22156 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22157 		    NULL, NULL, NULL);
22158 		if (ill == NULL) {
22159 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22160 			return (B_FALSE);
22161 		}
22162 		need_refrele = B_TRUE;
22163 	}
22164 
22165 	/*
22166 	 * Use the address length specified by the SA to determine
22167 	 * if it corresponds to a IPv6 address, and fail the matching
22168 	 * if the isv6 flag passed as argument does not match.
22169 	 * Note: this check is used for SADB capability checking before
22170 	 * sending SA information to an ill.
22171 	 */
22172 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22173 	if (sa_isv6 != ill_isv6)
22174 		/* protocol mismatch */
22175 		goto done;
22176 
22177 	/*
22178 	 * Check if the ill supports the protocol, algorithm(s) and
22179 	 * key size(s) specified by the SA, and get the pointers to
22180 	 * the algorithms supported by the ill.
22181 	 */
22182 	switch (sa->ipsa_type) {
22183 
22184 	case SADB_SATYPE_ESP:
22185 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22186 			/* ill does not support ESP acceleration */
22187 			goto done;
22188 		cpp = ill->ill_ipsec_capab_esp;
22189 		algid = sa->ipsa_auth_alg;
22190 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22191 			goto done;
22192 		algid = sa->ipsa_encr_alg;
22193 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22194 			goto done;
22195 		if (algid < cpp->encr_algparm_end) {
22196 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22197 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22198 				goto done;
22199 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22200 				goto done;
22201 		}
22202 		break;
22203 
22204 	case SADB_SATYPE_AH:
22205 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22206 			/* ill does not support AH acceleration */
22207 			goto done;
22208 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22209 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22210 			goto done;
22211 		break;
22212 	}
22213 
22214 	if (need_refrele)
22215 		ill_refrele(ill);
22216 	return (B_TRUE);
22217 done:
22218 	if (need_refrele)
22219 		ill_refrele(ill);
22220 	return (B_FALSE);
22221 }
22222 
22223 
22224 /*
22225  * Add a new ill to the list of IPsec capable ills.
22226  * Called from ill_capability_ipsec_ack() when an ACK was received
22227  * indicating that IPsec hardware processing was enabled for an ill.
22228  *
22229  * ill must point to the ill for which acceleration was enabled.
22230  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22231  */
22232 static void
22233 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22234 {
22235 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22236 	uint_t sa_type;
22237 	uint_t ipproto;
22238 
22239 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22240 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22241 
22242 	switch (dl_cap) {
22243 	case DL_CAPAB_IPSEC_AH:
22244 		sa_type = SADB_SATYPE_AH;
22245 		ills = &ipsec_capab_ills_ah;
22246 		ipproto = IPPROTO_AH;
22247 		break;
22248 	case DL_CAPAB_IPSEC_ESP:
22249 		sa_type = SADB_SATYPE_ESP;
22250 		ills = &ipsec_capab_ills_esp;
22251 		ipproto = IPPROTO_ESP;
22252 		break;
22253 	}
22254 
22255 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22256 
22257 	/*
22258 	 * Add ill index to list of hardware accelerators. If
22259 	 * already in list, do nothing.
22260 	 */
22261 	for (cur_ill = *ills; cur_ill != NULL &&
22262 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22263 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22264 		;
22265 
22266 	if (cur_ill == NULL) {
22267 		/* if this is a new entry for this ill */
22268 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22269 		if (new_ill == NULL) {
22270 			rw_exit(&ipsec_capab_ills_lock);
22271 			return;
22272 		}
22273 
22274 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22275 		new_ill->ill_isv6 = ill->ill_isv6;
22276 		new_ill->next = *ills;
22277 		*ills = new_ill;
22278 	} else if (!sadb_resync) {
22279 		/* not resync'ing SADB and an entry exists for this ill */
22280 		rw_exit(&ipsec_capab_ills_lock);
22281 		return;
22282 	}
22283 
22284 	rw_exit(&ipsec_capab_ills_lock);
22285 
22286 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22287 		/*
22288 		 * IPsec module for protocol loaded, initiate dump
22289 		 * of the SADB to this ill.
22290 		 */
22291 		sadb_ill_download(ill, sa_type);
22292 }
22293 
22294 /*
22295  * Remove an ill from the list of IPsec capable ills.
22296  */
22297 static void
22298 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22299 {
22300 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22301 
22302 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22303 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22304 
22305 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22306 	    &ipsec_capab_ills_esp;
22307 
22308 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22309 
22310 	prev_ill = NULL;
22311 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22312 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22313 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22314 		;
22315 	if (cur_ill == NULL) {
22316 		/* entry not found */
22317 		rw_exit(&ipsec_capab_ills_lock);
22318 		return;
22319 	}
22320 	if (prev_ill == NULL) {
22321 		/* entry at front of list */
22322 		*ills = NULL;
22323 	} else {
22324 		prev_ill->next = cur_ill->next;
22325 	}
22326 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22327 	rw_exit(&ipsec_capab_ills_lock);
22328 }
22329 
22330 
22331 /*
22332  * Handling of DL_CONTROL_REQ messages that must be sent down to
22333  * an ill while having exclusive access.
22334  */
22335 /* ARGSUSED */
22336 static void
22337 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22338 {
22339 	ill_t *ill = (ill_t *)q->q_ptr;
22340 
22341 	ill_dlpi_send(ill, mp);
22342 }
22343 
22344 
22345 /*
22346  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22347  * supporting the specified IPsec protocol acceleration.
22348  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22349  * We free the mblk and, if sa is non-null, release the held referece.
22350  */
22351 void
22352 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22353 {
22354 	ipsec_capab_ill_t *ici, *cur_ici;
22355 	ill_t *ill;
22356 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22357 
22358 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22359 	    ipsec_capab_ills_esp;
22360 
22361 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22362 
22363 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22364 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22365 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22366 
22367 		/*
22368 		 * Handle the case where the ill goes away while the SADB is
22369 		 * attempting to send messages.  If it's going away, it's
22370 		 * nuking its shadow SADB, so we don't care..
22371 		 */
22372 
22373 		if (ill == NULL)
22374 			continue;
22375 
22376 		if (sa != NULL) {
22377 			/*
22378 			 * Make sure capabilities match before
22379 			 * sending SA to ill.
22380 			 */
22381 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22382 			    cur_ici->ill_isv6, sa)) {
22383 				ill_refrele(ill);
22384 				continue;
22385 			}
22386 
22387 			mutex_enter(&sa->ipsa_lock);
22388 			sa->ipsa_flags |= IPSA_F_HW;
22389 			mutex_exit(&sa->ipsa_lock);
22390 		}
22391 
22392 		/*
22393 		 * Copy template message, and add it to the front
22394 		 * of the mblk ship list. We want to avoid holding
22395 		 * the ipsec_capab_ills_lock while sending the
22396 		 * message to the ills.
22397 		 *
22398 		 * The b_next and b_prev are temporarily used
22399 		 * to build a list of mblks to be sent down, and to
22400 		 * save the ill to which they must be sent.
22401 		 */
22402 		nmp = copymsg(mp);
22403 		if (nmp == NULL) {
22404 			ill_refrele(ill);
22405 			continue;
22406 		}
22407 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22408 		nmp->b_next = mp_ship_list;
22409 		mp_ship_list = nmp;
22410 		nmp->b_prev = (mblk_t *)ill;
22411 	}
22412 
22413 	rw_exit(&ipsec_capab_ills_lock);
22414 
22415 	nmp = mp_ship_list;
22416 	while (nmp != NULL) {
22417 		/* restore the mblk to a sane state */
22418 		next_mp = nmp->b_next;
22419 		nmp->b_next = NULL;
22420 		ill = (ill_t *)nmp->b_prev;
22421 		nmp->b_prev = NULL;
22422 
22423 		/*
22424 		 * Ship the mblk to the ill, must be exclusive. Keep the
22425 		 * reference to the ill as qwriter_ip() does a ill_referele().
22426 		 */
22427 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22428 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22429 
22430 		nmp = next_mp;
22431 	}
22432 
22433 	if (sa != NULL)
22434 		IPSA_REFRELE(sa);
22435 	freemsg(mp);
22436 }
22437 
22438 
22439 /*
22440  * Derive an interface id from the link layer address.
22441  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22442  */
22443 static boolean_t
22444 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22445 {
22446 	char		*addr;
22447 
22448 	if (phys_length != ETHERADDRL)
22449 		return (B_FALSE);
22450 
22451 	/* Form EUI-64 like address */
22452 	addr = (char *)&v6addr->s6_addr32[2];
22453 	bcopy((char *)phys_addr, addr, 3);
22454 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22455 	addr[3] = (char)0xff;
22456 	addr[4] = (char)0xfe;
22457 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22458 	return (B_TRUE);
22459 }
22460 
22461 /* ARGSUSED */
22462 static boolean_t
22463 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22464 {
22465 	return (B_FALSE);
22466 }
22467 
22468 /* ARGSUSED */
22469 static boolean_t
22470 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22471     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22472 {
22473 	/*
22474 	 * Multicast address mappings used over Ethernet/802.X.
22475 	 * This address is used as a base for mappings.
22476 	 */
22477 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22478 	    0x00, 0x00, 0x00};
22479 
22480 	/*
22481 	 * Extract low order 32 bits from IPv6 multicast address.
22482 	 * Or that into the link layer address, starting from the
22483 	 * second byte.
22484 	 */
22485 	*hw_start = 2;
22486 	v6_extract_mask->s6_addr32[0] = 0;
22487 	v6_extract_mask->s6_addr32[1] = 0;
22488 	v6_extract_mask->s6_addr32[2] = 0;
22489 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22490 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22491 	return (B_TRUE);
22492 }
22493 
22494 /*
22495  * Indicate by return value whether multicast is supported. If not,
22496  * this code should not touch/change any parameters.
22497  */
22498 /* ARGSUSED */
22499 static boolean_t
22500 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22501     uint32_t *hw_start, ipaddr_t *extract_mask)
22502 {
22503 	/*
22504 	 * Multicast address mappings used over Ethernet/802.X.
22505 	 * This address is used as a base for mappings.
22506 	 */
22507 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22508 	    0x00, 0x00, 0x00 };
22509 
22510 	if (phys_length != ETHERADDRL)
22511 		return (B_FALSE);
22512 
22513 	*extract_mask = htonl(0x007fffff);
22514 	*hw_start = 2;
22515 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22516 	return (B_TRUE);
22517 }
22518 
22519 /*
22520  * Derive IPoIB interface id from the link layer address.
22521  */
22522 static boolean_t
22523 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22524 {
22525 	char		*addr;
22526 
22527 	if (phys_length != 20)
22528 		return (B_FALSE);
22529 	addr = (char *)&v6addr->s6_addr32[2];
22530 	bcopy(phys_addr + 12, addr, 8);
22531 	/*
22532 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22533 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22534 	 * rules. In these cases, the IBA considers these GUIDs to be in
22535 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22536 	 * required; vendors are required not to assign global EUI-64's
22537 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22538 	 * of the interface identifier. Whether the GUID is in modified
22539 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22540 	 * bit set to 1.
22541 	 */
22542 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22543 	return (B_TRUE);
22544 }
22545 
22546 /*
22547  * Note on mapping from multicast IP addresses to IPoIB multicast link
22548  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22549  * The format of an IPoIB multicast address is:
22550  *
22551  *  4 byte QPN      Scope Sign.  Pkey
22552  * +--------------------------------------------+
22553  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22554  * +--------------------------------------------+
22555  *
22556  * The Scope and Pkey components are properties of the IBA port and
22557  * network interface. They can be ascertained from the broadcast address.
22558  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22559  */
22560 
22561 static boolean_t
22562 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22563     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22564 {
22565 	/*
22566 	 * Base IPoIB IPv6 multicast address used for mappings.
22567 	 * Does not contain the IBA scope/Pkey values.
22568 	 */
22569 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22570 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22571 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22572 
22573 	/*
22574 	 * Extract low order 80 bits from IPv6 multicast address.
22575 	 * Or that into the link layer address, starting from the
22576 	 * sixth byte.
22577 	 */
22578 	*hw_start = 6;
22579 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22580 
22581 	/*
22582 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22583 	 */
22584 	*(maddr + 5) = *(bphys_addr + 5);
22585 	*(maddr + 8) = *(bphys_addr + 8);
22586 	*(maddr + 9) = *(bphys_addr + 9);
22587 
22588 	v6_extract_mask->s6_addr32[0] = 0;
22589 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
22590 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
22591 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22592 	return (B_TRUE);
22593 }
22594 
22595 static boolean_t
22596 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22597     uint32_t *hw_start, ipaddr_t *extract_mask)
22598 {
22599 	/*
22600 	 * Base IPoIB IPv4 multicast address used for mappings.
22601 	 * Does not contain the IBA scope/Pkey values.
22602 	 */
22603 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22604 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
22605 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22606 
22607 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
22608 		return (B_FALSE);
22609 
22610 	/*
22611 	 * Extract low order 28 bits from IPv4 multicast address.
22612 	 * Or that into the link layer address, starting from the
22613 	 * sixteenth byte.
22614 	 */
22615 	*extract_mask = htonl(0x0fffffff);
22616 	*hw_start = 16;
22617 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
22618 
22619 	/*
22620 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22621 	 */
22622 	*(maddr + 5) = *(bphys_addr + 5);
22623 	*(maddr + 8) = *(bphys_addr + 8);
22624 	*(maddr + 9) = *(bphys_addr + 9);
22625 	return (B_TRUE);
22626 }
22627 
22628 /*
22629  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
22630  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
22631  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
22632  * the link-local address is preferred.
22633  */
22634 boolean_t
22635 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22636 {
22637 	ipif_t	*ipif;
22638 	ipif_t	*maybe_ipif = NULL;
22639 
22640 	mutex_enter(&ill->ill_lock);
22641 	if (ill->ill_state_flags & ILL_CONDEMNED) {
22642 		mutex_exit(&ill->ill_lock);
22643 		if (ipifp != NULL)
22644 			*ipifp = NULL;
22645 		return (B_FALSE);
22646 	}
22647 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
22648 		if (!IPIF_CAN_LOOKUP(ipif))
22649 			continue;
22650 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid)
22651 			continue;
22652 		if ((ipif->ipif_flags & flags) != flags)
22653 			continue;
22654 
22655 		if (ipifp == NULL) {
22656 			mutex_exit(&ill->ill_lock);
22657 			ASSERT(maybe_ipif == NULL);
22658 			return (B_TRUE);
22659 		}
22660 		if (!ill->ill_isv6 ||
22661 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
22662 			ipif_refhold_locked(ipif);
22663 			mutex_exit(&ill->ill_lock);
22664 			*ipifp = ipif;
22665 			return (B_TRUE);
22666 		}
22667 		if (maybe_ipif == NULL)
22668 			maybe_ipif = ipif;
22669 	}
22670 	if (ipifp != NULL) {
22671 		if (maybe_ipif != NULL)
22672 			ipif_refhold_locked(maybe_ipif);
22673 		*ipifp = maybe_ipif;
22674 	}
22675 	mutex_exit(&ill->ill_lock);
22676 	return (maybe_ipif != NULL);
22677 }
22678 
22679 /*
22680  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
22681  */
22682 boolean_t
22683 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22684 {
22685 	ill_t *illg;
22686 
22687 	/*
22688 	 * We look at the passed-in ill first without grabbing ill_g_lock.
22689 	 */
22690 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
22691 		return (B_TRUE);
22692 	}
22693 	rw_enter(&ill_g_lock, RW_READER);
22694 	if (ill->ill_group == NULL) {
22695 		/* ill not in a group */
22696 		rw_exit(&ill_g_lock);
22697 		return (B_FALSE);
22698 	}
22699 
22700 	/*
22701 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
22702 	 * group. We need to look for an ipif in the zone on all the ills in the
22703 	 * group.
22704 	 */
22705 	illg = ill->ill_group->illgrp_ill;
22706 	do {
22707 		/*
22708 		 * We don't call ipif_lookup_zoneid() on ill as we already know
22709 		 * that it's not there.
22710 		 */
22711 		if (illg != ill &&
22712 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
22713 			break;
22714 		}
22715 	} while ((illg = illg->ill_group_next) != NULL);
22716 	rw_exit(&ill_g_lock);
22717 	return (illg != NULL);
22718 }
22719 
22720 /*
22721  * Heuristic to check if ill has hit the FAILBACK=no case,
22722  * i.e. failover has occured from ill and later interface has recovered,
22723  * but user has configured FAILBACK=no.
22724  * Checks if ill has an usable ipif.
22725  *
22726  * Return values:
22727  *	B_FALSE - ill has no usable ipif, hit FAILBACK=no case
22728  *	B_TRUE	- ill has at least one usable ipif, FAILBACK=no case not hit
22729  */
22730 static boolean_t
22731 ill_has_usable_ipif(ill_t *ill)
22732 {
22733 	ipif_t *ipif;
22734 
22735 	/* Check whether it is in an IPMP group */
22736 	if (ill->ill_phyint->phyint_groupname == NULL)
22737 		return (B_TRUE);
22738 
22739 	if (ill->ill_ipif_up_count == 0)
22740 		return (B_FALSE);
22741 
22742 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
22743 		uint64_t flags = ipif->ipif_flags;
22744 
22745 		/*
22746 		 * This ipif is usable if it is IPIF_UP and not a
22747 		 * dedicated test address.  A dedicated test address
22748 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
22749 		 * (note in particular that V6 test addresses are
22750 		 * link-local data addresses and thus are marked
22751 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
22752 		 */
22753 		if ((flags & IPIF_UP) &&
22754 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
22755 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
22756 			return (B_TRUE);
22757 	}
22758 	return (B_FALSE);
22759 }
22760 
22761 /*
22762  * Check if this ill is only being used to send ICMP probes for IPMP
22763  */
22764 boolean_t
22765 ill_is_probeonly(ill_t *ill)
22766 {
22767 	/*
22768 	 * Check if the interface is FAILED, or INACTIVE
22769 	 * or has hit the FAILBACK=no case.
22770 	 */
22771 	if ((ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE)) ||
22772 	    ill_has_usable_ipif(ill) == B_FALSE) {
22773 		return (B_TRUE);
22774 	}
22775 
22776 	return (B_FALSE);
22777 }
22778