xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 4745263a792e84bbd9e36b3ceb07d1275762cf9b)
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 /*
555  * Reflects value of FAILBACK variable in IPMP config file
556  * /etc/default/mpathd. Default value is B_TRUE.
557  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
558  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
559  */
560 static boolean_t ipmp_enable_failback = B_TRUE;
561 
562 static uint_t
563 ipif_rand(void)
564 {
565 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
566 	return ((ipif_src_random >> 16) & 0x7fff);
567 }
568 
569 /*
570  * Allocate per-interface mibs. Only used for ipv6.
571  * Returns true if ok. False otherwise.
572  *  ipsq  may not yet be allocated (loopback case ).
573  */
574 static boolean_t
575 ill_allocate_mibs(ill_t *ill)
576 {
577 	ASSERT(ill->ill_isv6);
578 
579 	/* Already allocated? */
580 	if (ill->ill_ip6_mib != NULL) {
581 		ASSERT(ill->ill_icmp6_mib != NULL);
582 		return (B_TRUE);
583 	}
584 
585 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
586 	    KM_NOSLEEP);
587 	if (ill->ill_ip6_mib == NULL) {
588 		return (B_FALSE);
589 	}
590 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
591 	    KM_NOSLEEP);
592 	if (ill->ill_icmp6_mib == NULL) {
593 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
594 		ill->ill_ip6_mib = NULL;
595 		return (B_FALSE);
596 	}
597 	/*
598 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
599 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
600 	 * -> ill_phyint_reinit
601 	 */
602 	return (B_TRUE);
603 }
604 
605 /*
606  * Common code for preparation of ARP commands.  Two points to remember:
607  * 	1) The ill_name is tacked on at the end of the allocated space so
608  *	   the templates name_offset field must contain the total space
609  *	   to allocate less the name length.
610  *
611  *	2) The templates name_length field should contain the *template*
612  *	   length.  We use it as a parameter to bcopy() and then write
613  *	   the real ill_name_length into the name_length field of the copy.
614  * (Always called as writer.)
615  */
616 mblk_t *
617 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
618 {
619 	arc_t	*arc = (arc_t *)template;
620 	char	*cp;
621 	int	len;
622 	mblk_t	*mp;
623 	uint_t	name_length = ill->ill_name_length;
624 	uint_t	template_len = arc->arc_name_length;
625 
626 	len = arc->arc_name_offset + name_length;
627 	mp = allocb(len, BPRI_HI);
628 	if (mp == NULL)
629 		return (NULL);
630 	cp = (char *)mp->b_rptr;
631 	mp->b_wptr = (uchar_t *)&cp[len];
632 	if (template_len)
633 		bcopy(template, cp, template_len);
634 	if (len > template_len)
635 		bzero(&cp[template_len], len - template_len);
636 	mp->b_datap->db_type = M_PROTO;
637 
638 	arc = (arc_t *)cp;
639 	arc->arc_name_length = name_length;
640 	cp = (char *)arc + arc->arc_name_offset;
641 	bcopy(ill->ill_name, cp, name_length);
642 
643 	if (addr) {
644 		area_t	*area = (area_t *)mp->b_rptr;
645 
646 		cp = (char *)area + area->area_proto_addr_offset;
647 		bcopy(addr, cp, area->area_proto_addr_length);
648 		if (area->area_cmd == AR_ENTRY_ADD) {
649 			cp = (char *)area;
650 			len = area->area_proto_addr_length;
651 			if (area->area_proto_mask_offset)
652 				cp += area->area_proto_mask_offset;
653 			else
654 				cp += area->area_proto_addr_offset + len;
655 			while (len-- > 0)
656 				*cp++ = (char)~0;
657 		}
658 	}
659 	return (mp);
660 }
661 
662 /*
663  * Completely vaporize a lower level tap and all associated interfaces.
664  * ill_delete is called only out of ip_close when the device control
665  * stream is being closed.
666  */
667 void
668 ill_delete(ill_t *ill)
669 {
670 	ipif_t	*ipif;
671 	ill_t	*prev_ill;
672 
673 	/*
674 	 * ill_delete may be forcibly entering the ipsq. The previous
675 	 * ioctl may not have completed and may need to be aborted.
676 	 * ipsq_flush takes care of it. If we don't need to enter the
677 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
678 	 * ill_delete_tail is sufficient.
679 	 */
680 	ipsq_flush(ill);
681 
682 	/*
683 	 * Nuke all interfaces.  ipif_free will take down the interface,
684 	 * remove it from the list, and free the data structure.
685 	 * Walk down the ipif list and remove the logical interfaces
686 	 * first before removing the main ipif. We can't unplumb
687 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
688 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
689 	 * POINTOPOINT.
690 	 *
691 	 * If ill_ipif was not properly initialized (i.e low on memory),
692 	 * then no interfaces to clean up. In this case just clean up the
693 	 * ill.
694 	 */
695 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
696 		ipif_free(ipif);
697 
698 	/*
699 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
700 	 * So nobody can be using this mp now. Free the mp allocated for
701 	 * honoring ILLF_NOARP
702 	 */
703 	freemsg(ill->ill_arp_on_mp);
704 	ill->ill_arp_on_mp = NULL;
705 
706 	/* Clean up msgs on pending upcalls for mrouted */
707 	reset_mrt_ill(ill);
708 
709 	/*
710 	 * ipif_free -> reset_conn_ipif will remove all multicast
711 	 * references for IPv4. For IPv6, we need to do it here as
712 	 * it points only at ills.
713 	 */
714 	reset_conn_ill(ill);
715 
716 	/*
717 	 * ill_down will arrange to blow off any IRE's dependent on this
718 	 * ILL, and shut down fragmentation reassembly.
719 	 */
720 	ill_down(ill);
721 
722 	/* Let SCTP know, so that it can remove this from its list. */
723 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
724 
725 	/*
726 	 * If an address on this ILL is being used as a source address then
727 	 * clear out the pointers in other ILLs that point to this ILL.
728 	 */
729 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
730 	if (ill->ill_usesrc_grp_next != NULL) {
731 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
732 			ill_disband_usesrc_group(ill);
733 		} else {	/* consumer of the usesrc ILL */
734 			prev_ill = ill_prev_usesrc(ill);
735 			prev_ill->ill_usesrc_grp_next =
736 			    ill->ill_usesrc_grp_next;
737 		}
738 	}
739 	rw_exit(&ill_g_usesrc_lock);
740 }
741 
742 /*
743  * ill_delete_tail is called from ip_modclose after all references
744  * to the closing ill are gone. The wait is done in ip_modclose
745  */
746 void
747 ill_delete_tail(ill_t *ill)
748 {
749 	mblk_t	**mpp;
750 	ipif_t	*ipif;
751 
752 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
753 		ipif_down_tail(ipif);
754 
755 	/*
756 	 * Send the detach if there's one to send (i.e., if we're above a
757 	 * style 2 DLPI driver).
758 	 */
759 	if (ill->ill_detach_mp != NULL) {
760 		ill_dlpi_send(ill, ill->ill_detach_mp);
761 		ill->ill_detach_mp = NULL;
762 	}
763 
764 	/*
765 	 * If polling capability is enabled (which signifies direct
766 	 * upcall into IP and driver has ill saved as a handle),
767 	 * we need to make sure that unbind has completed before we
768 	 * let the ill disappear and driver no longer has any reference
769 	 * to this ill.
770 	 */
771 	mutex_enter(&ill->ill_lock);
772 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
773 		while (!(ill->ill_state_flags & ILL_DL_UNBIND_DONE))
774 			cv_wait(&ill->ill_cv, &ill->ill_lock);
775 	}
776 	mutex_exit(&ill->ill_lock);
777 
778 	if (ill->ill_net_type != IRE_LOOPBACK)
779 		qprocsoff(ill->ill_rq);
780 
781 	/*
782 	 * We do an ipsq_flush once again now. New messages could have
783 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
784 	 * could also have landed up if an ioctl thread had looked up
785 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
786 	 * enqueued the ioctl when we did the ipsq_flush last time.
787 	 */
788 	ipsq_flush(ill);
789 
790 	/*
791 	 * Free capabilities.
792 	 */
793 	if (ill->ill_ipsec_capab_ah != NULL) {
794 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
795 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
796 		ill->ill_ipsec_capab_ah = NULL;
797 	}
798 
799 	if (ill->ill_ipsec_capab_esp != NULL) {
800 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
801 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
802 		ill->ill_ipsec_capab_esp = NULL;
803 	}
804 
805 	if (ill->ill_mdt_capab != NULL) {
806 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
807 		ill->ill_mdt_capab = NULL;
808 	}
809 
810 	if (ill->ill_hcksum_capab != NULL) {
811 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
812 		ill->ill_hcksum_capab = NULL;
813 	}
814 
815 	if (ill->ill_zerocopy_capab != NULL) {
816 		kmem_free(ill->ill_zerocopy_capab,
817 		    sizeof (ill_zerocopy_capab_t));
818 		ill->ill_zerocopy_capab = NULL;
819 	}
820 
821 	/*
822 	 * Clean up polling capabilities
823 	 */
824 	if (ill->ill_capabilities & ILL_CAPAB_POLL)
825 		ipsq_clean_all(ill);
826 
827 	if (ill->ill_poll_capab != NULL) {
828 		CONN_DEC_REF(ill->ill_poll_capab->ill_unbind_conn);
829 		ill->ill_poll_capab->ill_unbind_conn = NULL;
830 		kmem_free(ill->ill_poll_capab,
831 		    sizeof (ill_poll_capab_t) +
832 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
833 		ill->ill_poll_capab = NULL;
834 	}
835 
836 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
837 
838 	while (ill->ill_ipif != NULL)
839 		ipif_free_tail(ill->ill_ipif);
840 
841 	ill_down_tail(ill);
842 
843 	/*
844 	 * We have removed all references to ilm from conn and the ones joined
845 	 * within the kernel.
846 	 *
847 	 * We don't walk conns, mrts and ires because
848 	 *
849 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
850 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
851 	 *    ill references.
852 	 */
853 	ASSERT(ilm_walk_ill(ill) == 0);
854 	/*
855 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
856 	 * could free the phyint. No more reference to the phyint after this
857 	 * point.
858 	 */
859 	(void) ill_glist_delete(ill);
860 
861 	rw_enter(&ip_g_nd_lock, RW_WRITER);
862 	if (ill->ill_ndd_name != NULL)
863 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
864 	rw_exit(&ip_g_nd_lock);
865 
866 
867 	if (ill->ill_frag_ptr != NULL) {
868 		uint_t count;
869 
870 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
871 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
872 		}
873 		mi_free(ill->ill_frag_ptr);
874 		ill->ill_frag_ptr = NULL;
875 		ill->ill_frag_hash_tbl = NULL;
876 	}
877 	if (ill->ill_nd_lla_mp != NULL)
878 		freemsg(ill->ill_nd_lla_mp);
879 	/* Free all retained control messages. */
880 	mpp = &ill->ill_first_mp_to_free;
881 	do {
882 		while (mpp[0]) {
883 			mblk_t  *mp;
884 			mblk_t  *mp1;
885 
886 			mp = mpp[0];
887 			mpp[0] = mp->b_next;
888 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
889 				mp1->b_next = NULL;
890 				mp1->b_prev = NULL;
891 			}
892 			freemsg(mp);
893 		}
894 	} while (mpp++ != &ill->ill_last_mp_to_free);
895 
896 	ill_free_mib(ill);
897 	ILL_TRACE_CLEANUP(ill);
898 }
899 
900 static void
901 ill_free_mib(ill_t *ill)
902 {
903 	if (ill->ill_ip6_mib != NULL) {
904 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
905 		ill->ill_ip6_mib = NULL;
906 	}
907 	if (ill->ill_icmp6_mib != NULL) {
908 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
909 		ill->ill_icmp6_mib = NULL;
910 	}
911 }
912 
913 /*
914  * Concatenate together a physical address and a sap.
915  *
916  * Sap_lengths are interpreted as follows:
917  *   sap_length == 0	==>	no sap
918  *   sap_length > 0	==>	sap is at the head of the dlpi address
919  *   sap_length < 0	==>	sap is at the tail of the dlpi address
920  */
921 static void
922 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
923     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
924 {
925 	uint16_t sap_addr = (uint16_t)sap_src;
926 
927 	if (sap_length == 0) {
928 		if (phys_src == NULL)
929 			bzero(dst, phys_length);
930 		else
931 			bcopy(phys_src, dst, phys_length);
932 	} else if (sap_length < 0) {
933 		if (phys_src == NULL)
934 			bzero(dst, phys_length);
935 		else
936 			bcopy(phys_src, dst, phys_length);
937 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
938 	} else {
939 		bcopy(&sap_addr, dst, sizeof (sap_addr));
940 		if (phys_src == NULL)
941 			bzero((char *)dst + sap_length, phys_length);
942 		else
943 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
944 	}
945 }
946 
947 /*
948  * Generate a dl_unitdata_req mblk for the device and address given.
949  * addr_length is the length of the physical portion of the address.
950  * If addr is NULL include an all zero address of the specified length.
951  * TRUE? In any case, addr_length is taken to be the entire length of the
952  * dlpi address, including the absolute value of sap_length.
953  */
954 mblk_t *
955 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
956 		t_scalar_t sap_length)
957 {
958 	dl_unitdata_req_t *dlur;
959 	mblk_t	*mp;
960 	t_scalar_t	abs_sap_length;		/* absolute value */
961 
962 	abs_sap_length = ABS(sap_length);
963 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
964 		DL_UNITDATA_REQ);
965 	if (mp == NULL)
966 		return (NULL);
967 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
968 	/* HACK: accomodate incompatible DLPI drivers */
969 	if (addr_length == 8)
970 		addr_length = 6;
971 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
972 	dlur->dl_dest_addr_offset = sizeof (*dlur);
973 	dlur->dl_priority.dl_min = 0;
974 	dlur->dl_priority.dl_max = 0;
975 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
976 	    (uchar_t *)&dlur[1]);
977 	return (mp);
978 }
979 
980 /*
981  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
982  * Return an error if we already have 1 or more ioctls in progress.
983  * This is used only for non-exclusive ioctls. Currently this is used
984  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
985  * and thus need to use ipsq_pending_mp_add.
986  */
987 boolean_t
988 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
989 {
990 	ASSERT(MUTEX_HELD(&ill->ill_lock));
991 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
992 	/*
993 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
994 	 */
995 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
996 	    (add_mp->b_datap->db_type == M_IOCTL));
997 
998 	ASSERT(MUTEX_HELD(&connp->conn_lock));
999 	/*
1000 	 * Return error if the conn has started closing. The conn
1001 	 * could have finished cleaning up the pending mp list,
1002 	 * If so we should not add another mp to the list negating
1003 	 * the cleanup.
1004 	 */
1005 	if (connp->conn_state_flags & CONN_CLOSING)
1006 		return (B_FALSE);
1007 	/*
1008 	 * Add the pending mp to the head of the list, chained by b_next.
1009 	 * Note down the conn on which the ioctl request came, in b_prev.
1010 	 * This will be used to later get the conn, when we get a response
1011 	 * on the ill queue, from some other module (typically arp)
1012 	 */
1013 	add_mp->b_next = (void *)ill->ill_pending_mp;
1014 	add_mp->b_queue = CONNP_TO_WQ(connp);
1015 	ill->ill_pending_mp = add_mp;
1016 	if (connp != NULL)
1017 		connp->conn_oper_pending_ill = ill;
1018 	return (B_TRUE);
1019 }
1020 
1021 /*
1022  * Retrieve the ill_pending_mp and return it. We have to walk the list
1023  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1024  */
1025 mblk_t *
1026 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1027 {
1028 	mblk_t	*prev = NULL;
1029 	mblk_t	*curr = NULL;
1030 	uint_t	id;
1031 	conn_t	*connp;
1032 
1033 	/*
1034 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1035 	 * up the pending mp, but it does not know the ioc_id and
1036 	 * passes in a zero for it.
1037 	 */
1038 	mutex_enter(&ill->ill_lock);
1039 	if (ioc_id != 0)
1040 		*connpp = NULL;
1041 
1042 	/* Search the list for the appropriate ioctl based on ioc_id */
1043 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1044 	    prev = curr, curr = curr->b_next) {
1045 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1046 		connp = Q_TO_CONN(curr->b_queue);
1047 		/* Match based on the ioc_id or based on the conn */
1048 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1049 			break;
1050 	}
1051 
1052 	if (curr != NULL) {
1053 		/* Unlink the mblk from the pending mp list */
1054 		if (prev != NULL) {
1055 			prev->b_next = curr->b_next;
1056 		} else {
1057 			ASSERT(ill->ill_pending_mp == curr);
1058 			ill->ill_pending_mp = curr->b_next;
1059 		}
1060 
1061 		/*
1062 		 * conn refcnt must have been bumped up at the start of
1063 		 * the ioctl. So we can safely access the conn.
1064 		 */
1065 		ASSERT(CONN_Q(curr->b_queue));
1066 		*connpp = Q_TO_CONN(curr->b_queue);
1067 		curr->b_next = NULL;
1068 		curr->b_queue = NULL;
1069 	}
1070 
1071 	mutex_exit(&ill->ill_lock);
1072 
1073 	return (curr);
1074 }
1075 
1076 /*
1077  * Add the pending mp to the list. There can be only 1 pending mp
1078  * in the list. Any exclusive ioctl that needs to wait for a response
1079  * from another module or driver needs to use this function to set
1080  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1081  * the other module/driver. This is also used while waiting for the
1082  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1083  */
1084 boolean_t
1085 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1086     int waitfor)
1087 {
1088 	ipsq_t	*ipsq;
1089 
1090 	ASSERT(IAM_WRITER_IPIF(ipif));
1091 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1092 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1093 	/*
1094 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1095 	 * M_ERROR/M_HANGUP from driver
1096 	 */
1097 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1098 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1099 
1100 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1101 	if (connp != NULL) {
1102 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1103 		/*
1104 		 * Return error if the conn has started closing. The conn
1105 		 * could have finished cleaning up the pending mp list,
1106 		 * If so we should not add another mp to the list negating
1107 		 * the cleanup.
1108 		 */
1109 		if (connp->conn_state_flags & CONN_CLOSING)
1110 			return (B_FALSE);
1111 	}
1112 	mutex_enter(&ipsq->ipsq_lock);
1113 	ipsq->ipsq_pending_ipif = ipif;
1114 	/*
1115 	 * Note down the queue in b_queue. This will be returned by
1116 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1117 	 * the processing
1118 	 */
1119 	add_mp->b_next = NULL;
1120 	add_mp->b_queue = q;
1121 	ipsq->ipsq_pending_mp = add_mp;
1122 	ipsq->ipsq_waitfor = waitfor;
1123 	/*
1124 	 * ipsq_current_ipif is needed to restart the operation from
1125 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1126 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1127 	 * been set until now.
1128 	 */
1129 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1130 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1131 		ipsq->ipsq_current_ipif = ipif;
1132 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1133 	}
1134 	if (connp != NULL)
1135 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1136 	mutex_exit(&ipsq->ipsq_lock);
1137 	return (B_TRUE);
1138 }
1139 
1140 /*
1141  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1142  * queued in the list.
1143  */
1144 mblk_t *
1145 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1146 {
1147 	mblk_t	*curr = NULL;
1148 
1149 	mutex_enter(&ipsq->ipsq_lock);
1150 	*connpp = NULL;
1151 	if (ipsq->ipsq_pending_mp == NULL) {
1152 		mutex_exit(&ipsq->ipsq_lock);
1153 		return (NULL);
1154 	}
1155 
1156 	/* There can be only 1 such excl message */
1157 	curr = ipsq->ipsq_pending_mp;
1158 	ASSERT(curr != NULL && curr->b_next == NULL);
1159 	ipsq->ipsq_pending_ipif = NULL;
1160 	ipsq->ipsq_pending_mp = NULL;
1161 	ipsq->ipsq_waitfor = 0;
1162 	mutex_exit(&ipsq->ipsq_lock);
1163 
1164 	if (CONN_Q(curr->b_queue)) {
1165 		/*
1166 		 * This mp did a refhold on the conn, at the start of the ioctl.
1167 		 * So we can safely return a pointer to the conn to the caller.
1168 		 */
1169 		*connpp = Q_TO_CONN(curr->b_queue);
1170 	} else {
1171 		*connpp = NULL;
1172 	}
1173 	curr->b_next = NULL;
1174 	curr->b_prev = NULL;
1175 	return (curr);
1176 }
1177 
1178 /*
1179  * Cleanup the ioctl mp queued in ipsq_pending_mp
1180  * - Called in the ill_delete path
1181  * - Called in the M_ERROR or M_HANGUP path on the ill.
1182  * - Called in the conn close path.
1183  */
1184 boolean_t
1185 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1186 {
1187 	mblk_t	*mp;
1188 	ipsq_t	*ipsq;
1189 	queue_t	*q;
1190 	ipif_t	*ipif;
1191 
1192 	ASSERT(IAM_WRITER_ILL(ill));
1193 	ipsq = ill->ill_phyint->phyint_ipsq;
1194 	mutex_enter(&ipsq->ipsq_lock);
1195 	/*
1196 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1197 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1198 	 * even if it is meant for another ill, since we have to enqueue
1199 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1200 	 * If connp is non-null we are called from the conn close path.
1201 	 */
1202 	mp = ipsq->ipsq_pending_mp;
1203 	if (mp == NULL || (connp != NULL &&
1204 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1205 		mutex_exit(&ipsq->ipsq_lock);
1206 		return (B_FALSE);
1207 	}
1208 	/* Now remove from the ipsq_pending_mp */
1209 	ipsq->ipsq_pending_mp = NULL;
1210 	q = mp->b_queue;
1211 	mp->b_next = NULL;
1212 	mp->b_prev = NULL;
1213 	mp->b_queue = NULL;
1214 
1215 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1216 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1217 	if (ill->ill_move_in_progress) {
1218 		ILL_CLEAR_MOVE(ill);
1219 	} else if (ill->ill_up_ipifs) {
1220 		ill_group_cleanup(ill);
1221 	}
1222 
1223 	ipif = ipsq->ipsq_pending_ipif;
1224 	ipsq->ipsq_pending_ipif = NULL;
1225 	ipsq->ipsq_waitfor = 0;
1226 	ipsq->ipsq_current_ipif = NULL;
1227 	mutex_exit(&ipsq->ipsq_lock);
1228 
1229 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1230 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1231 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1232 	} else {
1233 		/*
1234 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1235 		 * be just ip_ioctl_freemsg. we have to restart it
1236 		 * otherwise the thread will be stuck.
1237 		 */
1238 		ip_ioctl_freemsg(mp);
1239 	}
1240 	return (B_TRUE);
1241 }
1242 
1243 /*
1244  * The ill is closing. Cleanup all the pending mps. Called exclusively
1245  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1246  * knows this ill, and hence nobody can add an mp to this list
1247  */
1248 static void
1249 ill_pending_mp_cleanup(ill_t *ill)
1250 {
1251 	mblk_t	*mp;
1252 	queue_t	*q;
1253 
1254 	ASSERT(IAM_WRITER_ILL(ill));
1255 
1256 	mutex_enter(&ill->ill_lock);
1257 	/*
1258 	 * Every mp on the pending mp list originating from an ioctl
1259 	 * added 1 to the conn refcnt, at the start of the ioctl.
1260 	 * So bump it down now.  See comments in ip_wput_nondata()
1261 	 */
1262 	while (ill->ill_pending_mp != NULL) {
1263 		mp = ill->ill_pending_mp;
1264 		ill->ill_pending_mp = mp->b_next;
1265 		mutex_exit(&ill->ill_lock);
1266 
1267 		q = mp->b_queue;
1268 		ASSERT(CONN_Q(q));
1269 		mp->b_next = NULL;
1270 		mp->b_prev = NULL;
1271 		mp->b_queue = NULL;
1272 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1273 		mutex_enter(&ill->ill_lock);
1274 	}
1275 	ill->ill_pending_ipif = NULL;
1276 
1277 	mutex_exit(&ill->ill_lock);
1278 }
1279 
1280 /*
1281  * Called in the conn close path and ill delete path
1282  */
1283 static void
1284 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1285 {
1286 	ipsq_t	*ipsq;
1287 	mblk_t	*prev;
1288 	mblk_t	*curr;
1289 	mblk_t	*next;
1290 	queue_t	*q;
1291 	mblk_t	*tmp_list = NULL;
1292 
1293 	ASSERT(IAM_WRITER_ILL(ill));
1294 	if (connp != NULL)
1295 		q = CONNP_TO_WQ(connp);
1296 	else
1297 		q = ill->ill_wq;
1298 
1299 	ipsq = ill->ill_phyint->phyint_ipsq;
1300 	/*
1301 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1302 	 * In the case of ioctl from a conn, there can be only 1 mp
1303 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1304 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1305 	 * ioctls meant for this ill form conn's are not flushed. They will
1306 	 * be processed during ipsq_exit and will not find the ill and will
1307 	 * return error.
1308 	 */
1309 	mutex_enter(&ipsq->ipsq_lock);
1310 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1311 	    curr = next) {
1312 		next = curr->b_next;
1313 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1314 			/* Unlink the mblk from the pending mp list */
1315 			if (prev != NULL) {
1316 				prev->b_next = curr->b_next;
1317 			} else {
1318 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1319 				ipsq->ipsq_xopq_mphead = curr->b_next;
1320 			}
1321 			if (ipsq->ipsq_xopq_mptail == curr)
1322 				ipsq->ipsq_xopq_mptail = prev;
1323 			/*
1324 			 * Create a temporary list and release the ipsq lock
1325 			 * New elements are added to the head of the tmp_list
1326 			 */
1327 			curr->b_next = tmp_list;
1328 			tmp_list = curr;
1329 		} else {
1330 			prev = curr;
1331 		}
1332 	}
1333 	mutex_exit(&ipsq->ipsq_lock);
1334 
1335 	while (tmp_list != NULL) {
1336 		curr = tmp_list;
1337 		tmp_list = curr->b_next;
1338 		curr->b_next = NULL;
1339 		curr->b_prev = NULL;
1340 		curr->b_queue = NULL;
1341 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1342 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1343 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1344 		} else {
1345 			/*
1346 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1347 			 * this can't be just ip_ioctl_freemsg. we have to
1348 			 * restart it otherwise the thread will be stuck.
1349 			 */
1350 			ip_ioctl_freemsg(curr);
1351 		}
1352 	}
1353 }
1354 
1355 /*
1356  * This conn has started closing. Cleanup any pending ioctl from this conn.
1357  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1358  */
1359 void
1360 conn_ioctl_cleanup(conn_t *connp)
1361 {
1362 	mblk_t *curr;
1363 	ipsq_t	*ipsq;
1364 	ill_t	*ill;
1365 	boolean_t refheld;
1366 
1367 	/*
1368 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1369 	 * ioctl has not yet started, the mp is pending in the list headed by
1370 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1371 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1372 	 * is currently executing now the mp is not queued anywhere but
1373 	 * conn_oper_pending_ill is null. The conn close will wait
1374 	 * till the conn_ref drops to zero.
1375 	 */
1376 	mutex_enter(&connp->conn_lock);
1377 	ill = connp->conn_oper_pending_ill;
1378 	if (ill == NULL) {
1379 		mutex_exit(&connp->conn_lock);
1380 		return;
1381 	}
1382 
1383 	curr = ill_pending_mp_get(ill, &connp, 0);
1384 	if (curr != NULL) {
1385 		mutex_exit(&connp->conn_lock);
1386 		CONN_DEC_REF(connp);
1387 		ip_ioctl_freemsg(curr);
1388 		return;
1389 	}
1390 	/*
1391 	 * We may not be able to refhold the ill if the ill/ipif
1392 	 * is changing. But we need to make sure that the ill will
1393 	 * not vanish. So we just bump up the ill_waiter count.
1394 	 */
1395 	refheld = ill_waiter_inc(ill);
1396 	mutex_exit(&connp->conn_lock);
1397 	if (refheld) {
1398 		if (ipsq_enter(ill, B_TRUE)) {
1399 			ill_waiter_dcr(ill);
1400 			/*
1401 			 * Check whether this ioctl has started and is
1402 			 * pending now in ipsq_pending_mp. If it is not
1403 			 * found there then check whether this ioctl has
1404 			 * not even started and is in the ipsq_xopq list.
1405 			 */
1406 			if (!ipsq_pending_mp_cleanup(ill, connp))
1407 				ipsq_xopq_mp_cleanup(ill, connp);
1408 			ipsq = ill->ill_phyint->phyint_ipsq;
1409 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1410 			return;
1411 		}
1412 	}
1413 
1414 	/*
1415 	 * The ill is also closing and we could not bump up the
1416 	 * ill_waiter_count or we could not enter the ipsq. Leave
1417 	 * the cleanup to ill_delete
1418 	 */
1419 	mutex_enter(&connp->conn_lock);
1420 	while (connp->conn_oper_pending_ill != NULL)
1421 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1422 	mutex_exit(&connp->conn_lock);
1423 	if (refheld)
1424 		ill_waiter_dcr(ill);
1425 }
1426 
1427 /*
1428  * ipcl_walk function for cleaning up conn_*_ill fields.
1429  */
1430 static void
1431 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1432 {
1433 	ill_t	*ill = (ill_t *)arg;
1434 	ire_t	*ire;
1435 
1436 	mutex_enter(&connp->conn_lock);
1437 	if (connp->conn_multicast_ill == ill) {
1438 		/* Revert to late binding */
1439 		connp->conn_multicast_ill = NULL;
1440 		connp->conn_orig_multicast_ifindex = 0;
1441 	}
1442 	if (connp->conn_incoming_ill == ill)
1443 		connp->conn_incoming_ill = NULL;
1444 	if (connp->conn_outgoing_ill == ill)
1445 		connp->conn_outgoing_ill = NULL;
1446 	if (connp->conn_outgoing_pill == ill)
1447 		connp->conn_outgoing_pill = NULL;
1448 	if (connp->conn_nofailover_ill == ill)
1449 		connp->conn_nofailover_ill = NULL;
1450 	if (connp->conn_xmit_if_ill == ill)
1451 		connp->conn_xmit_if_ill = NULL;
1452 	if (connp->conn_ire_cache != NULL) {
1453 		ire = connp->conn_ire_cache;
1454 		/*
1455 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1456 		 * interface X and ipif coming from interface Y, if interface
1457 		 * X and Y are part of the same IPMPgroup. Thus whenever
1458 		 * interface X goes down, remove all references to it by
1459 		 * checking both on ire_ipif and ire_stq.
1460 		 */
1461 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1462 		    (ire->ire_type == IRE_CACHE &&
1463 		    ire->ire_stq == ill->ill_wq)) {
1464 			connp->conn_ire_cache = NULL;
1465 			mutex_exit(&connp->conn_lock);
1466 			ire_refrele_notr(ire);
1467 			return;
1468 		}
1469 	}
1470 	mutex_exit(&connp->conn_lock);
1471 
1472 }
1473 
1474 /* ARGSUSED */
1475 void
1476 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1477 {
1478 	ill_t	*ill = q->q_ptr;
1479 	ipif_t	*ipif;
1480 
1481 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1482 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1483 		ipif_down_tail(ipif);
1484 	ill_down_tail(ill);
1485 	freemsg(mp);
1486 	ipsq->ipsq_current_ipif = NULL;
1487 }
1488 
1489 /*
1490  * ill_down_start is called when we want to down this ill and bring it up again
1491  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1492  * all interfaces, but don't tear down any plumbing.
1493  */
1494 boolean_t
1495 ill_down_start(queue_t *q, mblk_t *mp)
1496 {
1497 	ill_t	*ill;
1498 	ipif_t	*ipif;
1499 
1500 	ill = q->q_ptr;
1501 
1502 	ASSERT(IAM_WRITER_ILL(ill));
1503 
1504 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1505 		(void) ipif_down(ipif, NULL, NULL);
1506 
1507 	ill_down(ill);
1508 
1509 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1510 	mutex_enter(&ill->ill_lock);
1511 	/*
1512 	 * Atomically test and add the pending mp if references are
1513 	 * still active.
1514 	 */
1515 	if (!ill_is_quiescent(ill)) {
1516 		/*
1517 		 * Get rid of any pending mps and cleanup. Call will
1518 		 * not fail since we are passing a null connp.
1519 		 */
1520 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1521 		    mp, ILL_DOWN);
1522 		mutex_exit(&ill->ill_lock);
1523 		return (B_FALSE);
1524 	}
1525 	mutex_exit(&ill->ill_lock);
1526 	return (B_TRUE);
1527 }
1528 
1529 static void
1530 ill_down(ill_t *ill)
1531 {
1532 	/* Blow off any IREs dependent on this ILL. */
1533 	ire_walk(ill_downi, (char *)ill);
1534 
1535 	mutex_enter(&ire_mrtun_lock);
1536 	if (ire_mrtun_count != 0) {
1537 		mutex_exit(&ire_mrtun_lock);
1538 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1539 		    (char *)ill, NULL);
1540 	} else {
1541 		mutex_exit(&ire_mrtun_lock);
1542 	}
1543 
1544 	/*
1545 	 * If any interface based forwarding table exists
1546 	 * Blow off the ires there dependent on this ill
1547 	 */
1548 	mutex_enter(&ire_srcif_table_lock);
1549 	if (ire_srcif_table_count > 0) {
1550 		mutex_exit(&ire_srcif_table_lock);
1551 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1552 	} else {
1553 		mutex_exit(&ire_srcif_table_lock);
1554 	}
1555 
1556 	/* Remove any conn_*_ill depending on this ill */
1557 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1558 
1559 	if (ill->ill_group != NULL) {
1560 		illgrp_delete(ill);
1561 	}
1562 
1563 }
1564 
1565 static void
1566 ill_down_tail(ill_t *ill)
1567 {
1568 	int	i;
1569 
1570 	/* Destroy ill_srcif_table if it exists */
1571 	/* Lock not reqd really because nobody should be able to access */
1572 	mutex_enter(&ill->ill_lock);
1573 	if (ill->ill_srcif_table != NULL) {
1574 		ill->ill_srcif_refcnt = 0;
1575 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1576 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1577 		}
1578 		kmem_free(ill->ill_srcif_table,
1579 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1580 		ill->ill_srcif_table = NULL;
1581 		ill->ill_srcif_refcnt = 0;
1582 		ill->ill_mrtun_refcnt = 0;
1583 	}
1584 	mutex_exit(&ill->ill_lock);
1585 }
1586 
1587 /*
1588  * ire_walk routine used to delete every IRE that depends on queues
1589  * associated with 'ill'.  (Always called as writer.)
1590  */
1591 static void
1592 ill_downi(ire_t *ire, char *ill_arg)
1593 {
1594 	ill_t	*ill = (ill_t *)ill_arg;
1595 
1596 	/*
1597 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1598 	 * interface X and ipif coming from interface Y, if interface
1599 	 * X and Y are part of the same IPMP group. Thus whenever interface
1600 	 * X goes down, remove all references to it by checking both
1601 	 * on ire_ipif and ire_stq.
1602 	 */
1603 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1604 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1605 		ire_delete(ire);
1606 	}
1607 }
1608 
1609 /*
1610  * A seperate routine for deleting revtun and srcif based routes
1611  * are needed because the ires only deleted when the interface
1612  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1613  * we want to keep mobile IP specific code separate.
1614  */
1615 static void
1616 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1617 {
1618 	ill_t   *ill = (ill_t *)ill_arg;
1619 
1620 	ASSERT(ire->ire_in_ill != NULL);
1621 
1622 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1623 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1624 		ire_delete(ire);
1625 	}
1626 }
1627 
1628 /*
1629  * Remove ire/nce from the fastpath list.
1630  */
1631 void
1632 ill_fastpath_nack(ill_t *ill)
1633 {
1634 	if (ill->ill_isv6) {
1635 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1636 	} else {
1637 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1638 	}
1639 }
1640 
1641 /* Consume an M_IOCACK of the fastpath probe. */
1642 void
1643 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1644 {
1645 	mblk_t	*mp1 = mp;
1646 
1647 	/*
1648 	 * If this was the first attempt turn on the fastpath probing.
1649 	 */
1650 	mutex_enter(&ill->ill_lock);
1651 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1652 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1653 	mutex_exit(&ill->ill_lock);
1654 
1655 	/* Free the M_IOCACK mblk, hold on to the data */
1656 	mp = mp->b_cont;
1657 	freeb(mp1);
1658 	if (mp == NULL)
1659 		return;
1660 	if (mp->b_cont != NULL) {
1661 		/*
1662 		 * Update all IRE's or NCE's that are waiting for
1663 		 * fastpath update.
1664 		 */
1665 		if (ill->ill_isv6) {
1666 			/*
1667 			 * update nce's in the fastpath list.
1668 			 */
1669 			nce_fastpath_list_dispatch(ill,
1670 			    ndp_fastpath_update, mp);
1671 		} else {
1672 
1673 			/*
1674 			 * update ire's in the fastpath list.
1675 			 */
1676 			ire_fastpath_list_dispatch(ill,
1677 			    ire_fastpath_update, mp);
1678 			/*
1679 			 * Check if we need to traverse reverse tunnel table.
1680 			 * Since there is only single ire_type (IRE_MIPRTUN)
1681 			 * in the table, we don't need to match on ire_type.
1682 			 * We have to check ire_mrtun_count and not the
1683 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1684 			 * on the incoming ill and here we are dealing with
1685 			 * outgoing ill.
1686 			 */
1687 			mutex_enter(&ire_mrtun_lock);
1688 			if (ire_mrtun_count != 0) {
1689 				mutex_exit(&ire_mrtun_lock);
1690 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1691 				    (void (*)(ire_t *, void *))
1692 					ire_fastpath_update, mp, ill);
1693 			} else {
1694 				mutex_exit(&ire_mrtun_lock);
1695 			}
1696 		}
1697 		mp1 = mp->b_cont;
1698 		freeb(mp);
1699 		mp = mp1;
1700 	} else {
1701 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1702 	}
1703 
1704 	freeb(mp);
1705 }
1706 
1707 /*
1708  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1709  * The data portion of the request is a dl_unitdata_req_t template for
1710  * what we would send downstream in the absence of a fastpath confirmation.
1711  */
1712 int
1713 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1714 {
1715 	struct iocblk	*ioc;
1716 	mblk_t	*mp;
1717 
1718 	if (dlur_mp == NULL)
1719 		return (EINVAL);
1720 
1721 	mutex_enter(&ill->ill_lock);
1722 	switch (ill->ill_dlpi_fastpath_state) {
1723 	case IDMS_FAILED:
1724 		/*
1725 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1726 		 * support it.
1727 		 */
1728 		mutex_exit(&ill->ill_lock);
1729 		return (ENOTSUP);
1730 	case IDMS_UNKNOWN:
1731 		/* This is the first probe */
1732 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1733 		break;
1734 	default:
1735 		break;
1736 	}
1737 	mutex_exit(&ill->ill_lock);
1738 
1739 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1740 		return (EAGAIN);
1741 
1742 	mp->b_cont = copyb(dlur_mp);
1743 	if (mp->b_cont == NULL) {
1744 		freeb(mp);
1745 		return (EAGAIN);
1746 	}
1747 
1748 	ioc = (struct iocblk *)mp->b_rptr;
1749 	ioc->ioc_count = msgdsize(mp->b_cont);
1750 
1751 	putnext(ill->ill_wq, mp);
1752 	return (0);
1753 }
1754 
1755 void
1756 ill_capability_probe(ill_t *ill)
1757 {
1758 	/*
1759 	 * Do so only if negotiation is enabled, capabilities are unknown,
1760 	 * and a capability negotiation is not already in progress.
1761 	 */
1762 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1763 	    ill->ill_capab_state != IDMS_RENEG)
1764 		return;
1765 
1766 	ill->ill_capab_state = IDMS_INPROGRESS;
1767 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1768 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1769 }
1770 
1771 void
1772 ill_capability_reset(ill_t *ill)
1773 {
1774 	mblk_t *sc_mp = NULL;
1775 	mblk_t *tmp;
1776 
1777 	/*
1778 	 * Note here that we reset the state to UNKNOWN, and later send
1779 	 * down the DL_CAPABILITY_REQ without first setting the state to
1780 	 * INPROGRESS.  We do this in order to distinguish the
1781 	 * DL_CAPABILITY_ACK response which may come back in response to
1782 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1783 	 * also handle the case where the driver doesn't send us back
1784 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1785 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1786 	 * features are turned off until the state reaches IDMS_OK.
1787 	 */
1788 	ill->ill_capab_state = IDMS_UNKNOWN;
1789 
1790 	/*
1791 	 * Disable sub-capabilities and request a list of sub-capability
1792 	 * messages which will be sent down to the driver.  Each handler
1793 	 * allocates the corresponding dl_capability_sub_t inside an
1794 	 * mblk, and links it to the existing sc_mp mblk, or return it
1795 	 * as sc_mp if it's the first sub-capability (the passed in
1796 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1797 	 * sc_mp will be pulled-up, before passing it downstream.
1798 	 */
1799 	ill_capability_mdt_reset(ill, &sc_mp);
1800 	ill_capability_hcksum_reset(ill, &sc_mp);
1801 	ill_capability_zerocopy_reset(ill, &sc_mp);
1802 	ill_capability_ipsec_reset(ill, &sc_mp);
1803 	ill_capability_poll_reset(ill, &sc_mp);
1804 
1805 	/* Nothing to send down in order to disable the capabilities? */
1806 	if (sc_mp == NULL)
1807 		return;
1808 
1809 	tmp = msgpullup(sc_mp, -1);
1810 	freemsg(sc_mp);
1811 	if ((sc_mp = tmp) == NULL) {
1812 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1813 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1814 		return;
1815 	}
1816 
1817 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1818 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1819 }
1820 
1821 /*
1822  * Request or set new-style hardware capabilities supported by DLS provider.
1823  */
1824 static void
1825 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1826 {
1827 	mblk_t *mp;
1828 	dl_capability_req_t *capb;
1829 	size_t size = 0;
1830 	uint8_t *ptr;
1831 
1832 	if (reqp != NULL)
1833 		size = MBLKL(reqp);
1834 
1835 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1836 	if (mp == NULL) {
1837 		freemsg(reqp);
1838 		return;
1839 	}
1840 	ptr = mp->b_rptr;
1841 
1842 	capb = (dl_capability_req_t *)ptr;
1843 	ptr += sizeof (dl_capability_req_t);
1844 
1845 	if (reqp != NULL) {
1846 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1847 		capb->dl_sub_length = size;
1848 		bcopy(reqp->b_rptr, ptr, size);
1849 		ptr += size;
1850 		mp->b_cont = reqp->b_cont;
1851 		freeb(reqp);
1852 	}
1853 	ASSERT(ptr == mp->b_wptr);
1854 
1855 	ill_dlpi_send(ill, mp);
1856 }
1857 
1858 static void
1859 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1860 {
1861 	dl_capab_id_t *id_ic;
1862 	uint_t sub_dl_cap = outers->dl_cap;
1863 	dl_capability_sub_t *inners;
1864 	uint8_t *capend;
1865 
1866 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1867 
1868 	/*
1869 	 * Note: range checks here are not absolutely sufficient to
1870 	 * make us robust against malformed messages sent by drivers;
1871 	 * this is in keeping with the rest of IP's dlpi handling.
1872 	 * (Remember, it's coming from something else in the kernel
1873 	 * address space)
1874 	 */
1875 
1876 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1877 	if (capend > mp->b_wptr) {
1878 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1879 		    "malformed sub-capability too long for mblk");
1880 		return;
1881 	}
1882 
1883 	id_ic = (dl_capab_id_t *)(outers + 1);
1884 
1885 	if (outers->dl_length < sizeof (*id_ic) ||
1886 	    (inners = &id_ic->id_subcap,
1887 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1888 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1889 		    "encapsulated capab type %d too long for mblk",
1890 		    inners->dl_cap);
1891 		return;
1892 	}
1893 
1894 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1895 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1896 		    "isn't as expected; pass-thru module(s) detected, "
1897 		    "discarding capability\n", inners->dl_cap));
1898 		return;
1899 	}
1900 
1901 	/* Process the encapsulated sub-capability */
1902 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1903 }
1904 
1905 /*
1906  * Process Multidata Transmit capability negotiation ack received from a
1907  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1908  * DL_CAPABILITY_ACK message.
1909  */
1910 static void
1911 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1912 {
1913 	mblk_t *nmp = NULL;
1914 	dl_capability_req_t *oc;
1915 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1916 	ill_mdt_capab_t **ill_mdt_capab;
1917 	uint_t sub_dl_cap = isub->dl_cap;
1918 	uint8_t *capend;
1919 
1920 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1921 
1922 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1923 
1924 	/*
1925 	 * Note: range checks here are not absolutely sufficient to
1926 	 * make us robust against malformed messages sent by drivers;
1927 	 * this is in keeping with the rest of IP's dlpi handling.
1928 	 * (Remember, it's coming from something else in the kernel
1929 	 * address space)
1930 	 */
1931 
1932 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1933 	if (capend > mp->b_wptr) {
1934 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1935 		    "malformed sub-capability too long for mblk");
1936 		return;
1937 	}
1938 
1939 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1940 
1941 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1942 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1943 		    "unsupported MDT sub-capability (version %d, expected %d)",
1944 		    mdt_ic->mdt_version, MDT_VERSION_2);
1945 		return;
1946 	}
1947 
1948 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1949 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1950 		    "capability isn't as expected; pass-thru module(s) "
1951 		    "detected, discarding capability\n"));
1952 		return;
1953 	}
1954 
1955 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1956 
1957 		if (*ill_mdt_capab == NULL) {
1958 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1959 			    KM_NOSLEEP);
1960 
1961 			if (*ill_mdt_capab == NULL) {
1962 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1963 				    "could not enable MDT version %d "
1964 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1965 				    ill->ill_name);
1966 				return;
1967 			}
1968 		}
1969 
1970 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1971 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1972 		    "header spaces, %d max pld bufs, %d span limit)\n",
1973 		    ill->ill_name, MDT_VERSION_2,
1974 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1975 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1976 
1977 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1978 		(*ill_mdt_capab)->ill_mdt_on = 1;
1979 		/*
1980 		 * Round the following values to the nearest 32-bit; ULP
1981 		 * may further adjust them to accomodate for additional
1982 		 * protocol headers.  We pass these values to ULP during
1983 		 * bind time.
1984 		 */
1985 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1986 		    roundup(mdt_ic->mdt_hdr_head, 4);
1987 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1988 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1989 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1990 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1991 
1992 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1993 	} else {
1994 		uint_t size;
1995 		uchar_t *rptr;
1996 
1997 		size = sizeof (dl_capability_req_t) +
1998 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1999 
2000 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2001 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2002 			    "could not enable MDT for %s (ENOMEM)\n",
2003 			    ill->ill_name);
2004 			return;
2005 		}
2006 
2007 		rptr = nmp->b_rptr;
2008 		/* initialize dl_capability_req_t */
2009 		oc = (dl_capability_req_t *)nmp->b_rptr;
2010 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2011 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2012 		    sizeof (dl_capab_mdt_t);
2013 		nmp->b_rptr += sizeof (dl_capability_req_t);
2014 
2015 		/* initialize dl_capability_sub_t */
2016 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2017 		nmp->b_rptr += sizeof (*isub);
2018 
2019 		/* initialize dl_capab_mdt_t */
2020 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2021 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2022 
2023 		nmp->b_rptr = rptr;
2024 
2025 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2026 		    "to enable MDT version %d\n", ill->ill_name,
2027 		    MDT_VERSION_2));
2028 
2029 		/* set ENABLE flag */
2030 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2031 
2032 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2033 		ill_dlpi_send(ill, nmp);
2034 	}
2035 }
2036 
2037 static void
2038 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2039 {
2040 	mblk_t *mp;
2041 	dl_capab_mdt_t *mdt_subcap;
2042 	dl_capability_sub_t *dl_subcap;
2043 	int size;
2044 
2045 	if (!(ill->ill_capabilities & ILL_CAPAB_MDT))
2046 		return;
2047 
2048 	ASSERT(ill->ill_mdt_capab != NULL);
2049 	/*
2050 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2051 	 * structure since it's possible that another thread is still
2052 	 * referring to it.  The structure only gets deallocated when
2053 	 * we destroy the ill.
2054 	 */
2055 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2056 
2057 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2058 
2059 	mp = allocb(size, BPRI_HI);
2060 	if (mp == NULL) {
2061 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2062 		    "request to disable MDT\n"));
2063 		return;
2064 	}
2065 
2066 	mp->b_wptr = mp->b_rptr + size;
2067 
2068 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2069 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2070 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2071 
2072 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2073 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2074 	mdt_subcap->mdt_flags = 0;
2075 	mdt_subcap->mdt_hdr_head = 0;
2076 	mdt_subcap->mdt_hdr_tail = 0;
2077 
2078 	if (*sc_mp != NULL)
2079 		linkb(*sc_mp, mp);
2080 	else
2081 		*sc_mp = mp;
2082 }
2083 
2084 /*
2085  * Send a DL_NOTIFY_REQ to the specified ill to enable
2086  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2087  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2088  * acceleration.
2089  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2090  */
2091 static boolean_t
2092 ill_enable_promisc_notify(ill_t *ill)
2093 {
2094 	mblk_t *mp;
2095 	dl_notify_req_t *req;
2096 
2097 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2098 
2099 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2100 	if (mp == NULL)
2101 		return (B_FALSE);
2102 
2103 	req = (dl_notify_req_t *)mp->b_rptr;
2104 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2105 	    DL_NOTE_PROMISC_OFF_PHYS;
2106 
2107 	ill_dlpi_send(ill, mp);
2108 
2109 	return (B_TRUE);
2110 }
2111 
2112 
2113 /*
2114  * Allocate an IPsec capability request which will be filled by our
2115  * caller to turn on support for one or more algorithms.
2116  */
2117 static mblk_t *
2118 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2119 {
2120 	mblk_t *nmp;
2121 	dl_capability_req_t	*ocap;
2122 	dl_capab_ipsec_t	*ocip;
2123 	dl_capab_ipsec_t	*icip;
2124 	uint8_t			*ptr;
2125 	icip = (dl_capab_ipsec_t *)(isub + 1);
2126 
2127 	/*
2128 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2129 	 * PROMISC_ON/OFF notification from the provider. We need to
2130 	 * do this before enabling the algorithms to avoid leakage of
2131 	 * cleartext packets.
2132 	 */
2133 
2134 	if (!ill_enable_promisc_notify(ill))
2135 		return (NULL);
2136 
2137 	/*
2138 	 * Allocate new mblk which will contain a new capability
2139 	 * request to enable the capabilities.
2140 	 */
2141 
2142 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2143 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2144 	if (nmp == NULL)
2145 		return (NULL);
2146 
2147 	ptr = nmp->b_rptr;
2148 
2149 	/* initialize dl_capability_req_t */
2150 	ocap = (dl_capability_req_t *)ptr;
2151 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2152 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2153 	ptr += sizeof (dl_capability_req_t);
2154 
2155 	/* initialize dl_capability_sub_t */
2156 	bcopy(isub, ptr, sizeof (*isub));
2157 	ptr += sizeof (*isub);
2158 
2159 	/* initialize dl_capab_ipsec_t */
2160 	ocip = (dl_capab_ipsec_t *)ptr;
2161 	bcopy(icip, ocip, sizeof (*icip));
2162 
2163 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2164 	return (nmp);
2165 }
2166 
2167 /*
2168  * Process an IPsec capability negotiation ack received from a DLS Provider.
2169  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2170  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2171  */
2172 static void
2173 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2174 {
2175 	dl_capab_ipsec_t	*icip;
2176 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2177 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2178 	uint_t cipher, nciphers;
2179 	mblk_t *nmp;
2180 	uint_t alg_len;
2181 	boolean_t need_sadb_dump;
2182 	uint_t sub_dl_cap = isub->dl_cap;
2183 	ill_ipsec_capab_t **ill_capab;
2184 	uint64_t ill_capab_flag;
2185 	uint8_t *capend, *ciphend;
2186 	boolean_t sadb_resync;
2187 
2188 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2189 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2190 
2191 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2192 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2193 		ill_capab_flag = ILL_CAPAB_AH;
2194 	} else {
2195 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2196 		ill_capab_flag = ILL_CAPAB_ESP;
2197 	}
2198 
2199 	/*
2200 	 * If the ill capability structure exists, then this incoming
2201 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2202 	 * If this is so, then we'd need to resynchronize the SADB
2203 	 * after re-enabling the offloaded ciphers.
2204 	 */
2205 	sadb_resync = (*ill_capab != NULL);
2206 
2207 	/*
2208 	 * Note: range checks here are not absolutely sufficient to
2209 	 * make us robust against malformed messages sent by drivers;
2210 	 * this is in keeping with the rest of IP's dlpi handling.
2211 	 * (Remember, it's coming from something else in the kernel
2212 	 * address space)
2213 	 */
2214 
2215 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2216 	if (capend > mp->b_wptr) {
2217 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2218 		    "malformed sub-capability too long for mblk");
2219 		return;
2220 	}
2221 
2222 	/*
2223 	 * There are two types of acks we process here:
2224 	 * 1. acks in reply to a (first form) generic capability req
2225 	 *    (no ENABLE flag set)
2226 	 * 2. acks in reply to a ENABLE capability req.
2227 	 *    (ENABLE flag set)
2228 	 *
2229 	 * We process the subcapability passed as argument as follows:
2230 	 * 1 do initializations
2231 	 *   1.1 initialize nmp = NULL
2232 	 *   1.2 set need_sadb_dump to B_FALSE
2233 	 * 2 for each cipher in subcapability:
2234 	 *   2.1 if ENABLE flag is set:
2235 	 *	2.1.1 update per-ill ipsec capabilities info
2236 	 *	2.1.2 set need_sadb_dump to B_TRUE
2237 	 *   2.2 if ENABLE flag is not set:
2238 	 *	2.2.1 if nmp is NULL:
2239 	 *		2.2.1.1 allocate and initialize nmp
2240 	 *		2.2.1.2 init current pos in nmp
2241 	 *	2.2.2 copy current cipher to current pos in nmp
2242 	 *	2.2.3 set ENABLE flag in nmp
2243 	 *	2.2.4 update current pos
2244 	 * 3 if nmp is not equal to NULL, send enable request
2245 	 *   3.1 send capability request
2246 	 * 4 if need_sadb_dump is B_TRUE
2247 	 *   4.1 enable promiscuous on/off notifications
2248 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2249 	 *	AH or ESP SA's to interface.
2250 	 */
2251 
2252 	nmp = NULL;
2253 	oalg = NULL;
2254 	need_sadb_dump = B_FALSE;
2255 	icip = (dl_capab_ipsec_t *)(isub + 1);
2256 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2257 
2258 	nciphers = icip->cip_nciphers;
2259 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2260 
2261 	if (ciphend > capend) {
2262 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2263 		    "too many ciphers for sub-capability len");
2264 		return;
2265 	}
2266 
2267 	for (cipher = 0; cipher < nciphers; cipher++) {
2268 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2269 
2270 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2271 			/*
2272 			 * TBD: when we provide a way to disable capabilities
2273 			 * from above, need to manage the request-pending state
2274 			 * and fail if we were not expecting this ACK.
2275 			 */
2276 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2277 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2278 
2279 			/*
2280 			 * Update IPsec capabilities for this ill
2281 			 */
2282 
2283 			if (*ill_capab == NULL) {
2284 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2285 				    ("ill_capability_ipsec_ack: "
2286 					"allocating ipsec_capab for ill\n"));
2287 				*ill_capab = ill_ipsec_capab_alloc();
2288 
2289 				if (*ill_capab == NULL) {
2290 					cmn_err(CE_WARN,
2291 					    "ill_capability_ipsec_ack: "
2292 					    "could not enable IPsec Hardware "
2293 					    "acceleration for %s (ENOMEM)\n",
2294 					    ill->ill_name);
2295 					return;
2296 				}
2297 			}
2298 
2299 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2300 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2301 
2302 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2303 				cmn_err(CE_WARN,
2304 				    "ill_capability_ipsec_ack: "
2305 				    "malformed IPsec algorithm id %d",
2306 				    ialg->alg_prim);
2307 				continue;
2308 			}
2309 
2310 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2311 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2312 				    ialg->alg_prim);
2313 			} else {
2314 				ipsec_capab_algparm_t *alp;
2315 
2316 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2317 				    ialg->alg_prim);
2318 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2319 				    ialg->alg_prim)) {
2320 					cmn_err(CE_WARN,
2321 					    "ill_capability_ipsec_ack: "
2322 					    "no space for IPsec alg id %d",
2323 					    ialg->alg_prim);
2324 					continue;
2325 				}
2326 				alp = &((*ill_capab)->encr_algparm[
2327 						ialg->alg_prim]);
2328 				alp->minkeylen = ialg->alg_minbits;
2329 				alp->maxkeylen = ialg->alg_maxbits;
2330 			}
2331 			ill->ill_capabilities |= ill_capab_flag;
2332 			/*
2333 			 * indicate that a capability was enabled, which
2334 			 * will be used below to kick off a SADB dump
2335 			 * to the ill.
2336 			 */
2337 			need_sadb_dump = B_TRUE;
2338 		} else {
2339 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2340 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2341 				ialg->alg_prim));
2342 
2343 			if (nmp == NULL) {
2344 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2345 				if (nmp == NULL) {
2346 					/*
2347 					 * Sending the PROMISC_ON/OFF
2348 					 * notification request failed.
2349 					 * We cannot enable the algorithms
2350 					 * since the Provider will not
2351 					 * notify IP of promiscous mode
2352 					 * changes, which could lead
2353 					 * to leakage of packets.
2354 					 */
2355 					cmn_err(CE_WARN,
2356 					    "ill_capability_ipsec_ack: "
2357 					    "could not enable IPsec Hardware "
2358 					    "acceleration for %s (ENOMEM)\n",
2359 					    ill->ill_name);
2360 					return;
2361 				}
2362 				/* ptr to current output alg specifier */
2363 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2364 			}
2365 
2366 			/*
2367 			 * Copy current alg specifier, set ENABLE
2368 			 * flag, and advance to next output alg.
2369 			 * For now we enable all IPsec capabilities.
2370 			 */
2371 			ASSERT(oalg != NULL);
2372 			bcopy(ialg, oalg, alg_len);
2373 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2374 			nmp->b_wptr += alg_len;
2375 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2376 		}
2377 
2378 		/* move to next input algorithm specifier */
2379 		ialg = (dl_capab_ipsec_alg_t *)
2380 		    ((char *)ialg + alg_len);
2381 	}
2382 
2383 	if (nmp != NULL)
2384 		/*
2385 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2386 		 * IPsec hardware acceleration.
2387 		 */
2388 		ill_dlpi_send(ill, nmp);
2389 
2390 	if (need_sadb_dump)
2391 		/*
2392 		 * An acknowledgement corresponding to a request to
2393 		 * enable acceleration was received, notify SADB.
2394 		 */
2395 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2396 }
2397 
2398 /*
2399  * Given an mblk with enough space in it, create sub-capability entries for
2400  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2401  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2402  * in preparation for the reset the DL_CAPABILITY_REQ message.
2403  */
2404 static void
2405 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2406     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2407 {
2408 	dl_capab_ipsec_t *oipsec;
2409 	dl_capab_ipsec_alg_t *oalg;
2410 	dl_capability_sub_t *dl_subcap;
2411 	int i, k;
2412 
2413 	ASSERT(nciphers > 0);
2414 	ASSERT(ill_cap != NULL);
2415 	ASSERT(mp != NULL);
2416 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2417 
2418 	/* dl_capability_sub_t for "stype" */
2419 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2420 	dl_subcap->dl_cap = stype;
2421 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2422 	mp->b_wptr += sizeof (dl_capability_sub_t);
2423 
2424 	/* dl_capab_ipsec_t for "stype" */
2425 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2426 	oipsec->cip_version = 1;
2427 	oipsec->cip_nciphers = nciphers;
2428 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2429 
2430 	/* create entries for "stype" AUTH ciphers */
2431 	for (i = 0; i < ill_cap->algs_size; i++) {
2432 		for (k = 0; k < BITSPERBYTE; k++) {
2433 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2434 				continue;
2435 
2436 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2437 			bzero((void *)oalg, sizeof (*oalg));
2438 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2439 			oalg->alg_prim = k + (BITSPERBYTE * i);
2440 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2441 		}
2442 	}
2443 	/* create entries for "stype" ENCR ciphers */
2444 	for (i = 0; i < ill_cap->algs_size; i++) {
2445 		for (k = 0; k < BITSPERBYTE; k++) {
2446 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2447 				continue;
2448 
2449 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2450 			bzero((void *)oalg, sizeof (*oalg));
2451 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2452 			oalg->alg_prim = k + (BITSPERBYTE * i);
2453 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2454 		}
2455 	}
2456 }
2457 
2458 /*
2459  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2460  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2461  * POPC instruction, but our macro is more flexible for an arbitrary length
2462  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2463  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2464  * stays that way, we can reduce the number of iterations required.
2465  */
2466 #define	COUNT_1S(val, sum) {					\
2467 	uint8_t x = val & 0xff;					\
2468 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2469 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2470 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2471 }
2472 
2473 /* ARGSUSED */
2474 static void
2475 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2476 {
2477 	mblk_t *mp;
2478 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2479 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2480 	uint64_t ill_capabilities = ill->ill_capabilities;
2481 	int ah_cnt = 0, esp_cnt = 0;
2482 	int ah_len = 0, esp_len = 0;
2483 	int i, size = 0;
2484 
2485 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2486 		return;
2487 
2488 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2489 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2490 
2491 	/* Find out the number of ciphers for AH */
2492 	if (cap_ah != NULL) {
2493 		for (i = 0; i < cap_ah->algs_size; i++) {
2494 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2495 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2496 		}
2497 		if (ah_cnt > 0) {
2498 			size += sizeof (dl_capability_sub_t) +
2499 			    sizeof (dl_capab_ipsec_t);
2500 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2501 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2502 			size += ah_len;
2503 		}
2504 	}
2505 
2506 	/* Find out the number of ciphers for ESP */
2507 	if (cap_esp != NULL) {
2508 		for (i = 0; i < cap_esp->algs_size; i++) {
2509 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2510 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2511 		}
2512 		if (esp_cnt > 0) {
2513 			size += sizeof (dl_capability_sub_t) +
2514 			    sizeof (dl_capab_ipsec_t);
2515 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2516 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2517 			size += esp_len;
2518 		}
2519 	}
2520 
2521 	if (size == 0) {
2522 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2523 		    "there's nothing to reset\n"));
2524 		return;
2525 	}
2526 
2527 	mp = allocb(size, BPRI_HI);
2528 	if (mp == NULL) {
2529 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2530 		    "request to disable IPSEC Hardware Acceleration\n"));
2531 		return;
2532 	}
2533 
2534 	/*
2535 	 * Clear the capability flags for IPSec HA but retain the ill
2536 	 * capability structures since it's possible that another thread
2537 	 * is still referring to them.  The structures only get deallocated
2538 	 * when we destroy the ill.
2539 	 *
2540 	 * Various places check the flags to see if the ill is capable of
2541 	 * hardware acceleration, and by clearing them we ensure that new
2542 	 * outbound IPSec packets are sent down encrypted.
2543 	 */
2544 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2545 
2546 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2547 	if (ah_cnt > 0) {
2548 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2549 		    cap_ah, mp);
2550 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2551 	}
2552 
2553 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2554 	if (esp_cnt > 0) {
2555 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2556 		    cap_esp, mp);
2557 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2558 	}
2559 
2560 	/*
2561 	 * At this point we've composed a bunch of sub-capabilities to be
2562 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2563 	 * by the caller.  Upon receiving this reset message, the driver
2564 	 * must stop inbound decryption (by destroying all inbound SAs)
2565 	 * and let the corresponding packets come in encrypted.
2566 	 */
2567 
2568 	if (*sc_mp != NULL)
2569 		linkb(*sc_mp, mp);
2570 	else
2571 		*sc_mp = mp;
2572 }
2573 
2574 static void
2575 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2576     boolean_t encapsulated)
2577 {
2578 	boolean_t legacy = B_FALSE;
2579 
2580 	/*
2581 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2582 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2583 	 * instructed the driver to disable its advertised capabilities,
2584 	 * so there's no point in accepting any response at this moment.
2585 	 */
2586 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2587 		return;
2588 
2589 	/*
2590 	 * Note that only the following two sub-capabilities may be
2591 	 * considered as "legacy", since their original definitions
2592 	 * do not incorporate the dl_mid_t module ID token, and hence
2593 	 * may require the use of the wrapper sub-capability.
2594 	 */
2595 	switch (subp->dl_cap) {
2596 	case DL_CAPAB_IPSEC_AH:
2597 	case DL_CAPAB_IPSEC_ESP:
2598 		legacy = B_TRUE;
2599 		break;
2600 	}
2601 
2602 	/*
2603 	 * For legacy sub-capabilities which don't incorporate a queue_t
2604 	 * pointer in their structures, discard them if we detect that
2605 	 * there are intermediate modules in between IP and the driver.
2606 	 */
2607 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2608 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2609 		    "%d discarded; %d module(s) present below IP\n",
2610 		    subp->dl_cap, ill->ill_lmod_cnt));
2611 		return;
2612 	}
2613 
2614 	switch (subp->dl_cap) {
2615 	case DL_CAPAB_IPSEC_AH:
2616 	case DL_CAPAB_IPSEC_ESP:
2617 		ill_capability_ipsec_ack(ill, mp, subp);
2618 		break;
2619 	case DL_CAPAB_MDT:
2620 		ill_capability_mdt_ack(ill, mp, subp);
2621 		break;
2622 	case DL_CAPAB_HCKSUM:
2623 		ill_capability_hcksum_ack(ill, mp, subp);
2624 		break;
2625 	case DL_CAPAB_ZEROCOPY:
2626 		ill_capability_zerocopy_ack(ill, mp, subp);
2627 		break;
2628 	case DL_CAPAB_POLL:
2629 		ill_capability_poll_ack(ill, mp, subp);
2630 		break;
2631 	default:
2632 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2633 		    subp->dl_cap));
2634 	}
2635 }
2636 
2637 /*
2638  * As part of negotiating polling capability, the driver tells us
2639  * the default (or normal) blanking interval and packet threshold
2640  * (the receive timer fires if blanking interval is reached or
2641  * the packet threshold is reached).
2642  *
2643  * As part of manipulating the polling interval, we always use our
2644  * estimated interval (avg service time * number of packets queued
2645  * on the squeue) but we try to blank for a minimum of
2646  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2647  * packet threshold during this time. When we are not in polling mode
2648  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2649  * rr_min_blank_ratio but up the packet cnt by a ratio of
2650  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2651  * possible although for a shorter interval.
2652  */
2653 #define	RR_MAX_BLANK_RATIO	20
2654 #define	RR_MIN_BLANK_RATIO	10
2655 #define	RR_MAX_PKT_CNT_RATIO	3
2656 #define	RR_MIN_PKT_CNT_RATIO	3
2657 
2658 /*
2659  * These can be tuned via /etc/system.
2660  */
2661 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2662 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2663 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2664 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2665 
2666 static mac_resource_handle_t
2667 ill_ring_add(void *arg, mac_resource_t *mrp)
2668 {
2669 	ill_t			*ill = (ill_t *)arg;
2670 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2671 	ill_rx_ring_t		*rx_ring;
2672 	int			ip_rx_index;
2673 
2674 	if (mrp->mr_type != MAC_RX_FIFO) {
2675 		return (NULL);
2676 	}
2677 	ASSERT(ill != NULL);
2678 	ASSERT(ill->ill_poll_capab != NULL);
2679 	ASSERT(mrp != NULL);
2680 
2681 	mutex_enter(&ill->ill_lock);
2682 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2683 		rx_ring = &ill->ill_poll_capab->ill_ring_tbl[ip_rx_index];
2684 		ASSERT(rx_ring != NULL);
2685 
2686 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2687 			time_t normal_blank_time =
2688 			    mrfp->mrf_normal_blank_time;
2689 			uint_t normal_pkt_cnt =
2690 			    mrfp->mrf_normal_pkt_count;
2691 
2692 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2693 
2694 			rx_ring->rr_blank = mrfp->mrf_blank;
2695 			rx_ring->rr_handle = mrfp->mrf_arg;
2696 			rx_ring->rr_ill = ill;
2697 			rx_ring->rr_normal_blank_time = normal_blank_time;
2698 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2699 
2700 			rx_ring->rr_max_blank_time =
2701 			    normal_blank_time * rr_max_blank_ratio;
2702 			rx_ring->rr_min_blank_time =
2703 			    normal_blank_time * rr_min_blank_ratio;
2704 			rx_ring->rr_max_pkt_cnt =
2705 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2706 			rx_ring->rr_min_pkt_cnt =
2707 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2708 
2709 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2710 			mutex_exit(&ill->ill_lock);
2711 
2712 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2713 			    (int), ip_rx_index);
2714 			return ((mac_resource_handle_t)rx_ring);
2715 		}
2716 	}
2717 
2718 	/*
2719 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2720 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2721 	 * should be made configurable. Meanwhile it cause no panic because
2722 	 * driver will pass ip_input a NULL handle which will make
2723 	 * IP allocate the default squeue and Polling mode will not
2724 	 * be used for this ring.
2725 	 */
2726 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2727 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2728 
2729 	mutex_exit(&ill->ill_lock);
2730 	return (NULL);
2731 }
2732 
2733 static boolean_t
2734 ill_capability_poll_init(ill_t *ill)
2735 {
2736 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2737 	conn_t 			*connp;
2738 	size_t			sz;
2739 
2740 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2741 		if (ill_poll == NULL) {
2742 			cmn_err(CE_PANIC, "ill_capability_poll_init: "
2743 			    "polling enabled for ill=%s (%p) but data "
2744 			    "structs uninitialized\n", ill->ill_name,
2745 			    (void *)ill);
2746 		}
2747 		return (B_TRUE);
2748 	}
2749 
2750 	if (ill_poll != NULL) {
2751 		ill_rx_ring_t 	*rx_ring = ill_poll->ill_ring_tbl;
2752 		/* Polling is being re-enabled */
2753 
2754 		connp = ill_poll->ill_unbind_conn;
2755 		ASSERT(rx_ring != NULL);
2756 		bzero((void *)ill_poll, sizeof (ill_poll_capab_t));
2757 		bzero((void *)rx_ring,
2758 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2759 		ill_poll->ill_ring_tbl = rx_ring;
2760 		ill_poll->ill_unbind_conn = connp;
2761 		return (B_TRUE);
2762 	}
2763 
2764 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2765 		return (B_FALSE);
2766 
2767 	sz = sizeof (ill_poll_capab_t);
2768 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2769 
2770 	ill_poll = kmem_zalloc(sz, KM_NOSLEEP);
2771 	if (ill_poll == NULL) {
2772 		cmn_err(CE_WARN, "ill_capability_poll_init: could not "
2773 		    "allocate poll_capab for %s (%p)\n", ill->ill_name,
2774 		    (void *)ill);
2775 		CONN_DEC_REF(connp);
2776 		return (B_FALSE);
2777 	}
2778 
2779 	/* Allocate space to hold ring table */
2780 	ill_poll->ill_ring_tbl = (ill_rx_ring_t *)&ill_poll[1];
2781 	ill->ill_poll_capab = ill_poll;
2782 	ill_poll->ill_unbind_conn = connp;
2783 	return (B_TRUE);
2784 }
2785 
2786 /*
2787  * ill_capability_poll_disable: disable polling capability. Since
2788  * any of the rings might already be in use, need to call ipsq_clean_all()
2789  * which gets behind the squeue to disable direct calls if necessary.
2790  * Clean up the direct tx function pointers as well.
2791  */
2792 static void
2793 ill_capability_poll_disable(ill_t *ill)
2794 {
2795 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2796 
2797 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2798 		ipsq_clean_all(ill);
2799 		ill_poll->ill_tx = NULL;
2800 		ill_poll->ill_tx_handle = NULL;
2801 	}
2802 
2803 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
2804 }
2805 
2806 static void
2807 ill_capability_poll_capable(ill_t *ill, dl_capab_poll_t *ipoll,
2808     dl_capability_sub_t *isub)
2809 {
2810 	uint_t			size;
2811 	uchar_t			*rptr;
2812 	dl_capab_poll_t		poll, *opoll;
2813 	ill_poll_capab_t	*ill_poll;
2814 	mblk_t			*nmp = NULL;
2815 	dl_capability_req_t	*ocap;
2816 
2817 	if (!ill_capability_poll_init(ill))
2818 		return;
2819 	ill_poll = ill->ill_poll_capab;
2820 
2821 	/* Copy locally to get the members aligned */
2822 	bcopy((void *)ipoll, (void *)&poll, sizeof (dl_capab_poll_t));
2823 
2824 	/* Get the tx function and handle from dld */
2825 	ill_poll->ill_tx = (ip_dld_tx_t)poll.poll_tx;
2826 	ill_poll->ill_tx_handle = (void *)poll.poll_tx_handle;
2827 
2828 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2829 	    isub->dl_length;
2830 
2831 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2832 		cmn_err(CE_WARN, "ill_capability_poll_ack: could not allocate "
2833 		    "memory for CAPAB_REQ for %s (%p)\n", ill->ill_name,
2834 		    (void *)ill);
2835 		return;
2836 	}
2837 
2838 	/* initialize dl_capability_req_t */
2839 	rptr = nmp->b_rptr;
2840 	ocap = (dl_capability_req_t *)rptr;
2841 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2842 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2843 	rptr += sizeof (dl_capability_req_t);
2844 
2845 	/* initialize dl_capability_sub_t */
2846 	bcopy(isub, rptr, sizeof (*isub));
2847 	rptr += sizeof (*isub);
2848 
2849 	opoll = (dl_capab_poll_t *)rptr;
2850 	rptr += sizeof (dl_capab_poll_t);
2851 
2852 	/* initialize dl_capab_poll_t to be sent down */
2853 	poll.poll_rx_handle = (uintptr_t)ill;
2854 	poll.poll_rx = (uintptr_t)ip_input;
2855 	poll.poll_ring_add = (uintptr_t)ill_ring_add;
2856 	poll.poll_flags = POLL_ENABLE;
2857 	bcopy((void *)&poll, (void *)opoll, sizeof (dl_capab_poll_t));
2858 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2859 
2860 	/* nmp points to a DL_CAPABILITY_REQ message to enable polling */
2861 	ill_dlpi_send(ill, nmp);
2862 }
2863 
2864 
2865 /*
2866  * Process a polling capability negotiation ack received
2867  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_POLL)
2868  * of a DL_CAPABILITY_ACK message.
2869  */
2870 static void
2871 ill_capability_poll_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2872 {
2873 	dl_capab_poll_t		*ipoll;
2874 	uint_t			sub_dl_cap = isub->dl_cap;
2875 	uint8_t			*capend;
2876 
2877 
2878 	ASSERT(sub_dl_cap == DL_CAPAB_POLL);
2879 
2880 	/*
2881 	 * Don't enable polling for ipv6 ill's
2882 	 */
2883 	if (ill->ill_isv6) {
2884 		return;
2885 	}
2886 
2887 	/*
2888 	 * Note: range checks here are not absolutely sufficient to
2889 	 * make us robust against malformed messages sent by drivers;
2890 	 * this is in keeping with the rest of IP's dlpi handling.
2891 	 * (Remember, it's coming from something else in the kernel
2892 	 * address space)
2893 	 */
2894 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2895 	if (capend > mp->b_wptr) {
2896 		cmn_err(CE_WARN, "ill_capability_poll_ack: "
2897 		    "malformed sub-capability too long for mblk");
2898 		return;
2899 	}
2900 
2901 	/*
2902 	 * There are two types of acks we process here:
2903 	 * 1. acks in reply to a (first form) generic capability req
2904 	 *    (poll_flag will be set to POLL_CAPABLE)
2905 	 * 2. acks in reply to a POLL_ENABLE capability req.
2906 	 *    (POLL_ENABLE flag set)
2907 	 */
2908 	ipoll = (dl_capab_poll_t *)(isub + 1);
2909 
2910 	if (!dlcapabcheckqid(&ipoll->poll_mid, ill->ill_lmod_rq)) {
2911 		ip1dbg(("ill_capability_poll_ack: mid token for polling "
2912 		    "capability isn't as expected; pass-thru "
2913 		    "module(s) detected, discarding capability\n"));
2914 		if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2915 			/*
2916 			 * This is a capability renegotitation case.
2917 			 * The interface better be unusable at this
2918 			 * point other wise bad things will happen
2919 			 * if we disable direct calls on a running
2920 			 * and up interface.
2921 			 */
2922 			ill_capability_poll_disable(ill);
2923 		}
2924 		return;
2925 	}
2926 
2927 	switch (ipoll->poll_flags) {
2928 	default:
2929 		/* Disable if unknown flag */
2930 	case POLL_DISABLE:
2931 		ill_capability_poll_disable(ill);
2932 		break;
2933 	case POLL_CAPABLE:
2934 		/*
2935 		 * If the capability was already enabled, its safe
2936 		 * to disable it first to get rid of stale information
2937 		 * and then start enabling it again.
2938 		 */
2939 		ill_capability_poll_disable(ill);
2940 		ill_capability_poll_capable(ill, ipoll, isub);
2941 		break;
2942 	case POLL_ENABLE:
2943 		if (!(ill->ill_capabilities & ILL_CAPAB_POLL)) {
2944 			ASSERT(ill->ill_poll_capab != NULL);
2945 			ill->ill_capabilities |= ILL_CAPAB_POLL;
2946 		}
2947 		break;
2948 	}
2949 }
2950 
2951 static void
2952 ill_capability_poll_reset(ill_t *ill, mblk_t **sc_mp)
2953 {
2954 	mblk_t *mp;
2955 	dl_capab_poll_t *ipoll;
2956 	dl_capability_sub_t *dl_subcap;
2957 	int size;
2958 
2959 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
2960 		return;
2961 
2962 	ASSERT(ill->ill_poll_capab != NULL);
2963 
2964 	/*
2965 	 * Disable polling capability
2966 	 */
2967 	ill_capability_poll_disable(ill);
2968 
2969 	size = sizeof (*dl_subcap) + sizeof (*ipoll);
2970 
2971 	mp = allocb(size, BPRI_HI);
2972 	if (mp == NULL) {
2973 		ip1dbg(("ill_capability_poll_reset: unable to allocate "
2974 		    "request to disable polling\n"));
2975 		return;
2976 	}
2977 
2978 	mp->b_wptr = mp->b_rptr + size;
2979 
2980 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2981 	dl_subcap->dl_cap = DL_CAPAB_POLL;
2982 	dl_subcap->dl_length = sizeof (*ipoll);
2983 
2984 	ipoll = (dl_capab_poll_t *)(dl_subcap + 1);
2985 	ipoll->poll_flags = POLL_DISABLE;
2986 
2987 	if (*sc_mp != NULL)
2988 		linkb(*sc_mp, mp);
2989 	else
2990 		*sc_mp = mp;
2991 }
2992 
2993 
2994 /*
2995  * Process a hardware checksum offload capability negotiation ack received
2996  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2997  * of a DL_CAPABILITY_ACK message.
2998  */
2999 static void
3000 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3001 {
3002 	dl_capability_req_t	*ocap;
3003 	dl_capab_hcksum_t	*ihck, *ohck;
3004 	ill_hcksum_capab_t	**ill_hcksum;
3005 	mblk_t			*nmp = NULL;
3006 	uint_t			sub_dl_cap = isub->dl_cap;
3007 	uint8_t			*capend;
3008 
3009 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3010 
3011 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3012 
3013 	/*
3014 	 * Note: range checks here are not absolutely sufficient to
3015 	 * make us robust against malformed messages sent by drivers;
3016 	 * this is in keeping with the rest of IP's dlpi handling.
3017 	 * (Remember, it's coming from something else in the kernel
3018 	 * address space)
3019 	 */
3020 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3021 	if (capend > mp->b_wptr) {
3022 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3023 		    "malformed sub-capability too long for mblk");
3024 		return;
3025 	}
3026 
3027 	/*
3028 	 * There are two types of acks we process here:
3029 	 * 1. acks in reply to a (first form) generic capability req
3030 	 *    (no ENABLE flag set)
3031 	 * 2. acks in reply to a ENABLE capability req.
3032 	 *    (ENABLE flag set)
3033 	 */
3034 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3035 
3036 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3037 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3038 		    "unsupported hardware checksum "
3039 		    "sub-capability (version %d, expected %d)",
3040 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3041 		return;
3042 	}
3043 
3044 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3045 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3046 		    "checksum capability isn't as expected; pass-thru "
3047 		    "module(s) detected, discarding capability\n"));
3048 		return;
3049 	}
3050 
3051 #define	CURR_HCKSUM_CAPAB \
3052 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM)
3053 
3054 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3055 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3056 		/* do ENABLE processing */
3057 		if (*ill_hcksum == NULL) {
3058 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3059 			    KM_NOSLEEP);
3060 
3061 			if (*ill_hcksum == NULL) {
3062 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3063 				    "could not enable hcksum version %d "
3064 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3065 				    ill->ill_name);
3066 				return;
3067 			}
3068 		}
3069 
3070 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3071 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3072 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3073 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3074 		    "has enabled hardware checksumming\n ",
3075 		    ill->ill_name));
3076 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3077 		/*
3078 		 * Enabling hardware checksum offload
3079 		 * Currently IP supports {TCP,UDP}/IPv4
3080 		 * partial and full cksum offload and
3081 		 * IPv4 header checksum offload.
3082 		 * Allocate new mblk which will
3083 		 * contain a new capability request
3084 		 * to enable hardware checksum offload.
3085 		 */
3086 		uint_t	size;
3087 		uchar_t	*rptr;
3088 
3089 		size = sizeof (dl_capability_req_t) +
3090 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3091 
3092 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3093 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3094 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3095 			    ill->ill_name);
3096 			return;
3097 		}
3098 
3099 		rptr = nmp->b_rptr;
3100 		/* initialize dl_capability_req_t */
3101 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3102 		ocap->dl_sub_offset =
3103 		    sizeof (dl_capability_req_t);
3104 		ocap->dl_sub_length =
3105 		    sizeof (dl_capability_sub_t) +
3106 		    isub->dl_length;
3107 		nmp->b_rptr += sizeof (dl_capability_req_t);
3108 
3109 		/* initialize dl_capability_sub_t */
3110 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3111 		nmp->b_rptr += sizeof (*isub);
3112 
3113 		/* initialize dl_capab_hcksum_t */
3114 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3115 		bcopy(ihck, ohck, sizeof (*ihck));
3116 
3117 		nmp->b_rptr = rptr;
3118 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3119 
3120 		/* Set ENABLE flag */
3121 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3122 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3123 
3124 		/*
3125 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3126 		 * hardware checksum acceleration.
3127 		 */
3128 		ill_dlpi_send(ill, nmp);
3129 	} else
3130 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3131 		    "advertised %x hardware checksum capability flags\n",
3132 		    ill->ill_name, ihck->hcksum_txflags));
3133 }
3134 
3135 static void
3136 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3137 {
3138 	mblk_t *mp;
3139 	dl_capab_hcksum_t *hck_subcap;
3140 	dl_capability_sub_t *dl_subcap;
3141 	int size;
3142 
3143 	if (!(ill->ill_capabilities & ILL_CAPAB_HCKSUM))
3144 		return;
3145 
3146 	ASSERT(ill->ill_hcksum_capab != NULL);
3147 	/*
3148 	 * Clear the capability flag for hardware checksum offload but
3149 	 * retain the ill_hcksum_capab structure since it's possible that
3150 	 * another thread is still referring to it.  The structure only
3151 	 * gets deallocated when we destroy the ill.
3152 	 */
3153 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3154 
3155 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3156 
3157 	mp = allocb(size, BPRI_HI);
3158 	if (mp == NULL) {
3159 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3160 		    "request to disable hardware checksum offload\n"));
3161 		return;
3162 	}
3163 
3164 	mp->b_wptr = mp->b_rptr + size;
3165 
3166 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3167 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3168 	dl_subcap->dl_length = sizeof (*hck_subcap);
3169 
3170 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3171 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3172 	hck_subcap->hcksum_txflags = 0;
3173 
3174 	if (*sc_mp != NULL)
3175 		linkb(*sc_mp, mp);
3176 	else
3177 		*sc_mp = mp;
3178 }
3179 
3180 static void
3181 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3182 {
3183 	mblk_t *nmp = NULL;
3184 	dl_capability_req_t *oc;
3185 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3186 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3187 	uint_t sub_dl_cap = isub->dl_cap;
3188 	uint8_t *capend;
3189 
3190 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3191 
3192 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3193 
3194 	/*
3195 	 * Note: range checks here are not absolutely sufficient to
3196 	 * make us robust against malformed messages sent by drivers;
3197 	 * this is in keeping with the rest of IP's dlpi handling.
3198 	 * (Remember, it's coming from something else in the kernel
3199 	 * address space)
3200 	 */
3201 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3202 	if (capend > mp->b_wptr) {
3203 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3204 		    "malformed sub-capability too long for mblk");
3205 		return;
3206 	}
3207 
3208 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3209 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3210 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3211 		    "unsupported ZEROCOPY sub-capability (version %d, "
3212 		    "expected %d)", zc_ic->zerocopy_version,
3213 		    ZEROCOPY_VERSION_1);
3214 		return;
3215 	}
3216 
3217 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3218 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3219 		    "capability isn't as expected; pass-thru module(s) "
3220 		    "detected, discarding capability\n"));
3221 		return;
3222 	}
3223 
3224 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3225 		if (*ill_zerocopy_capab == NULL) {
3226 			*ill_zerocopy_capab =
3227 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3228 			    KM_NOSLEEP);
3229 
3230 			if (*ill_zerocopy_capab == NULL) {
3231 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3232 				    "could not enable Zero-copy version %d "
3233 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3234 				    ill->ill_name);
3235 				return;
3236 			}
3237 		}
3238 
3239 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3240 		    "supports Zero-copy version %d\n", ill->ill_name,
3241 		    ZEROCOPY_VERSION_1));
3242 
3243 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3244 		    zc_ic->zerocopy_version;
3245 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3246 		    zc_ic->zerocopy_flags;
3247 
3248 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3249 	} else {
3250 		uint_t size;
3251 		uchar_t *rptr;
3252 
3253 		size = sizeof (dl_capability_req_t) +
3254 		    sizeof (dl_capability_sub_t) +
3255 		    sizeof (dl_capab_zerocopy_t);
3256 
3257 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3258 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3259 			    "could not enable zerocopy for %s (ENOMEM)\n",
3260 			    ill->ill_name);
3261 			return;
3262 		}
3263 
3264 		rptr = nmp->b_rptr;
3265 		/* initialize dl_capability_req_t */
3266 		oc = (dl_capability_req_t *)rptr;
3267 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3268 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3269 		    sizeof (dl_capab_zerocopy_t);
3270 		rptr += sizeof (dl_capability_req_t);
3271 
3272 		/* initialize dl_capability_sub_t */
3273 		bcopy(isub, rptr, sizeof (*isub));
3274 		rptr += sizeof (*isub);
3275 
3276 		/* initialize dl_capab_zerocopy_t */
3277 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3278 		*zc_oc = *zc_ic;
3279 
3280 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3281 		    "to enable zero-copy version %d\n", ill->ill_name,
3282 		    ZEROCOPY_VERSION_1));
3283 
3284 		/* set VMSAFE_MEM flag */
3285 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3286 
3287 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3288 		ill_dlpi_send(ill, nmp);
3289 	}
3290 }
3291 
3292 static void
3293 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3294 {
3295 	mblk_t *mp;
3296 	dl_capab_zerocopy_t *zerocopy_subcap;
3297 	dl_capability_sub_t *dl_subcap;
3298 	int size;
3299 
3300 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3301 		return;
3302 
3303 	ASSERT(ill->ill_zerocopy_capab != NULL);
3304 	/*
3305 	 * Clear the capability flag for Zero-copy but retain the
3306 	 * ill_zerocopy_capab structure since it's possible that another
3307 	 * thread is still referring to it.  The structure only gets
3308 	 * deallocated when we destroy the ill.
3309 	 */
3310 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3311 
3312 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3313 
3314 	mp = allocb(size, BPRI_HI);
3315 	if (mp == NULL) {
3316 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3317 		    "request to disable Zero-copy\n"));
3318 		return;
3319 	}
3320 
3321 	mp->b_wptr = mp->b_rptr + size;
3322 
3323 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3324 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3325 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3326 
3327 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3328 	zerocopy_subcap->zerocopy_version =
3329 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3330 	zerocopy_subcap->zerocopy_flags = 0;
3331 
3332 	if (*sc_mp != NULL)
3333 		linkb(*sc_mp, mp);
3334 	else
3335 		*sc_mp = mp;
3336 }
3337 
3338 /*
3339  * Consume a new-style hardware capabilities negotiation ack.
3340  * Called from ip_rput_dlpi_writer().
3341  */
3342 void
3343 ill_capability_ack(ill_t *ill, mblk_t *mp)
3344 {
3345 	dl_capability_ack_t *capp;
3346 	dl_capability_sub_t *subp, *endp;
3347 
3348 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3349 		ill->ill_capab_state = IDMS_OK;
3350 
3351 	capp = (dl_capability_ack_t *)mp->b_rptr;
3352 
3353 	if (capp->dl_sub_length == 0)
3354 		/* no new-style capabilities */
3355 		return;
3356 
3357 	/* make sure the driver supplied correct dl_sub_length */
3358 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3359 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3360 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3361 		return;
3362 	}
3363 
3364 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3365 	/*
3366 	 * There are sub-capabilities. Process the ones we know about.
3367 	 * Loop until we don't have room for another sub-cap header..
3368 	 */
3369 	for (subp = SC(capp, capp->dl_sub_offset),
3370 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3371 	    subp <= endp;
3372 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3373 
3374 		switch (subp->dl_cap) {
3375 		case DL_CAPAB_ID_WRAPPER:
3376 			ill_capability_id_ack(ill, mp, subp);
3377 			break;
3378 		default:
3379 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3380 			break;
3381 		}
3382 	}
3383 #undef SC
3384 }
3385 
3386 /*
3387  * This routine is called to scan the fragmentation reassembly table for
3388  * the specified ILL for any packets that are starting to smell.
3389  * dead_interval is the maximum time in seconds that will be tolerated.  It
3390  * will either be the value specified in ip_g_frag_timeout, or zero if the
3391  * ILL is shutting down and it is time to blow everything off.
3392  *
3393  * It returns the number of seconds (as a time_t) that the next frag timer
3394  * should be scheduled for, 0 meaning that the timer doesn't need to be
3395  * re-started.  Note that the method of calculating next_timeout isn't
3396  * entirely accurate since time will flow between the time we grab
3397  * current_time and the time we schedule the next timeout.  This isn't a
3398  * big problem since this is the timer for sending an ICMP reassembly time
3399  * exceeded messages, and it doesn't have to be exactly accurate.
3400  *
3401  * This function is
3402  * sometimes called as writer, although this is not required.
3403  */
3404 time_t
3405 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3406 {
3407 	ipfb_t	*ipfb;
3408 	ipfb_t	*endp;
3409 	ipf_t	*ipf;
3410 	ipf_t	*ipfnext;
3411 	mblk_t	*mp;
3412 	time_t	current_time = gethrestime_sec();
3413 	time_t	next_timeout = 0;
3414 	uint32_t	hdr_length;
3415 	mblk_t	*send_icmp_head;
3416 	mblk_t	*send_icmp_head_v6;
3417 
3418 	ipfb = ill->ill_frag_hash_tbl;
3419 	if (ipfb == NULL)
3420 		return (B_FALSE);
3421 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3422 	/* Walk the frag hash table. */
3423 	for (; ipfb < endp; ipfb++) {
3424 		send_icmp_head = NULL;
3425 		send_icmp_head_v6 = NULL;
3426 		mutex_enter(&ipfb->ipfb_lock);
3427 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3428 			time_t frag_time = current_time - ipf->ipf_timestamp;
3429 			time_t frag_timeout;
3430 
3431 			if (frag_time < dead_interval) {
3432 				/*
3433 				 * There are some outstanding fragments
3434 				 * that will timeout later.  Make note of
3435 				 * the time so that we can reschedule the
3436 				 * next timeout appropriately.
3437 				 */
3438 				frag_timeout = dead_interval - frag_time;
3439 				if (next_timeout == 0 ||
3440 				    frag_timeout < next_timeout) {
3441 					next_timeout = frag_timeout;
3442 				}
3443 				break;
3444 			}
3445 			/* Time's up.  Get it out of here. */
3446 			hdr_length = ipf->ipf_nf_hdr_len;
3447 			ipfnext = ipf->ipf_hash_next;
3448 			if (ipfnext)
3449 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3450 			*ipf->ipf_ptphn = ipfnext;
3451 			mp = ipf->ipf_mp->b_cont;
3452 			for (; mp; mp = mp->b_cont) {
3453 				/* Extra points for neatness. */
3454 				IP_REASS_SET_START(mp, 0);
3455 				IP_REASS_SET_END(mp, 0);
3456 			}
3457 			mp = ipf->ipf_mp->b_cont;
3458 			ill->ill_frag_count -= ipf->ipf_count;
3459 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3460 			ipfb->ipfb_count -= ipf->ipf_count;
3461 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3462 			ipfb->ipfb_frag_pkts--;
3463 			/*
3464 			 * We do not send any icmp message from here because
3465 			 * we currently are holding the ipfb_lock for this
3466 			 * hash chain. If we try and send any icmp messages
3467 			 * from here we may end up via a put back into ip
3468 			 * trying to get the same lock, causing a recursive
3469 			 * mutex panic. Instead we build a list and send all
3470 			 * the icmp messages after we have dropped the lock.
3471 			 */
3472 			if (ill->ill_isv6) {
3473 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3474 				if (hdr_length != 0) {
3475 					mp->b_next = send_icmp_head_v6;
3476 					send_icmp_head_v6 = mp;
3477 				} else {
3478 					freemsg(mp);
3479 				}
3480 			} else {
3481 				BUMP_MIB(&ip_mib, ipReasmFails);
3482 				if (hdr_length != 0) {
3483 					mp->b_next = send_icmp_head;
3484 					send_icmp_head = mp;
3485 				} else {
3486 					freemsg(mp);
3487 				}
3488 			}
3489 			freeb(ipf->ipf_mp);
3490 		}
3491 		mutex_exit(&ipfb->ipfb_lock);
3492 		/*
3493 		 * Now need to send any icmp messages that we delayed from
3494 		 * above.
3495 		 */
3496 		while (send_icmp_head_v6 != NULL) {
3497 			mp = send_icmp_head_v6;
3498 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3499 			mp->b_next = NULL;
3500 			icmp_time_exceeded_v6(ill->ill_wq, mp,
3501 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, B_FALSE);
3502 		}
3503 		while (send_icmp_head != NULL) {
3504 			mp = send_icmp_head;
3505 			send_icmp_head = send_icmp_head->b_next;
3506 			mp->b_next = NULL;
3507 			icmp_time_exceeded(ill->ill_wq, mp,
3508 			    ICMP_REASSEMBLY_TIME_EXCEEDED);
3509 		}
3510 	}
3511 	/*
3512 	 * A non-dying ILL will use the return value to decide whether to
3513 	 * restart the frag timer, and for how long.
3514 	 */
3515 	return (next_timeout);
3516 }
3517 
3518 /*
3519  * This routine is called when the approximate count of mblk memory used
3520  * for the specified ILL has exceeded max_count.
3521  */
3522 void
3523 ill_frag_prune(ill_t *ill, uint_t max_count)
3524 {
3525 	ipfb_t	*ipfb;
3526 	ipf_t	*ipf;
3527 	size_t	count;
3528 
3529 	/*
3530 	 * If we are here within ip_min_frag_prune_time msecs remove
3531 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3532 	 * ill_frag_free_num_pkts.
3533 	 */
3534 	mutex_enter(&ill->ill_lock);
3535 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3536 	    (ip_min_frag_prune_time != 0 ?
3537 	    ip_min_frag_prune_time : msec_per_tick)) {
3538 
3539 		ill->ill_frag_free_num_pkts++;
3540 
3541 	} else {
3542 		ill->ill_frag_free_num_pkts = 0;
3543 	}
3544 	ill->ill_last_frag_clean_time = lbolt;
3545 	mutex_exit(&ill->ill_lock);
3546 
3547 	/*
3548 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3549 	 */
3550 	if (ill->ill_frag_free_num_pkts != 0) {
3551 		int ix;
3552 
3553 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3554 			ipfb = &ill->ill_frag_hash_tbl[ix];
3555 			mutex_enter(&ipfb->ipfb_lock);
3556 			if (ipfb->ipfb_ipf != NULL) {
3557 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3558 				    ill->ill_frag_free_num_pkts);
3559 			}
3560 			mutex_exit(&ipfb->ipfb_lock);
3561 		}
3562 	}
3563 	/*
3564 	 * While the reassembly list for this ILL is too big, prune a fragment
3565 	 * queue by age, oldest first.  Note that the per ILL count is
3566 	 * approximate, while the per frag hash bucket counts are accurate.
3567 	 */
3568 	while (ill->ill_frag_count > max_count) {
3569 		int	ix;
3570 		ipfb_t	*oipfb = NULL;
3571 		uint_t	oldest = UINT_MAX;
3572 
3573 		count = 0;
3574 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3575 			ipfb = &ill->ill_frag_hash_tbl[ix];
3576 			mutex_enter(&ipfb->ipfb_lock);
3577 			ipf = ipfb->ipfb_ipf;
3578 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3579 				oldest = ipf->ipf_gen;
3580 				oipfb = ipfb;
3581 			}
3582 			count += ipfb->ipfb_count;
3583 			mutex_exit(&ipfb->ipfb_lock);
3584 		}
3585 		/* Refresh the per ILL count */
3586 		ill->ill_frag_count = count;
3587 		if (oipfb == NULL) {
3588 			ill->ill_frag_count = 0;
3589 			break;
3590 		}
3591 		if (count <= max_count)
3592 			return;	/* Somebody beat us to it, nothing to do */
3593 		mutex_enter(&oipfb->ipfb_lock);
3594 		ipf = oipfb->ipfb_ipf;
3595 		if (ipf != NULL) {
3596 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3597 		}
3598 		mutex_exit(&oipfb->ipfb_lock);
3599 	}
3600 }
3601 
3602 /*
3603  * free 'free_cnt' fragmented packets starting at ipf.
3604  */
3605 void
3606 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3607 {
3608 	size_t	count;
3609 	mblk_t	*mp;
3610 	mblk_t	*tmp;
3611 	ipf_t **ipfp = ipf->ipf_ptphn;
3612 
3613 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3614 	ASSERT(ipfp != NULL);
3615 	ASSERT(ipf != NULL);
3616 
3617 	while (ipf != NULL && free_cnt-- > 0) {
3618 		count = ipf->ipf_count;
3619 		mp = ipf->ipf_mp;
3620 		ipf = ipf->ipf_hash_next;
3621 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3622 			IP_REASS_SET_START(tmp, 0);
3623 			IP_REASS_SET_END(tmp, 0);
3624 		}
3625 		ill->ill_frag_count -= count;
3626 		ASSERT(ipfb->ipfb_count >= count);
3627 		ipfb->ipfb_count -= count;
3628 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3629 		ipfb->ipfb_frag_pkts--;
3630 		freemsg(mp);
3631 		BUMP_MIB(&ip_mib, ipReasmFails);
3632 	}
3633 
3634 	if (ipf)
3635 		ipf->ipf_ptphn = ipfp;
3636 	ipfp[0] = ipf;
3637 }
3638 
3639 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3640 	"obsolete and may be removed in a future release of Solaris.  Use " \
3641 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3642 
3643 /*
3644  * For obsolete per-interface forwarding configuration;
3645  * called in response to ND_GET.
3646  */
3647 /* ARGSUSED */
3648 static int
3649 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3650 {
3651 	ill_t *ill = (ill_t *)cp;
3652 
3653 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3654 
3655 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3656 	return (0);
3657 }
3658 
3659 /*
3660  * For obsolete per-interface forwarding configuration;
3661  * called in response to ND_SET.
3662  */
3663 /* ARGSUSED */
3664 static int
3665 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3666     cred_t *ioc_cr)
3667 {
3668 	long value;
3669 	int retval;
3670 
3671 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3672 
3673 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3674 	    value < 0 || value > 1) {
3675 		return (EINVAL);
3676 	}
3677 
3678 	rw_enter(&ill_g_lock, RW_READER);
3679 	retval = ill_forward_set(q, mp, (value != 0), cp);
3680 	rw_exit(&ill_g_lock);
3681 	return (retval);
3682 }
3683 
3684 /*
3685  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3686  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3687  * up RTS_IFINFO routing socket messages for each interface whose flags we
3688  * change.
3689  */
3690 /* ARGSUSED */
3691 int
3692 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3693 {
3694 	ill_t *ill = (ill_t *)cp;
3695 	ill_group_t *illgrp;
3696 
3697 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3698 
3699 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3700 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3701 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3702 		return (EINVAL);
3703 
3704 	/*
3705 	 * If the ill is in an IPMP group, set the forwarding policy on all
3706 	 * members of the group to the same value.
3707 	 */
3708 	illgrp = ill->ill_group;
3709 	if (illgrp != NULL) {
3710 		ill_t *tmp_ill;
3711 
3712 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3713 		    tmp_ill = tmp_ill->ill_group_next) {
3714 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3715 			    (enable ? "Enabling" : "Disabling"),
3716 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3717 			    tmp_ill->ill_name));
3718 			mutex_enter(&tmp_ill->ill_lock);
3719 			if (enable)
3720 				tmp_ill->ill_flags |= ILLF_ROUTER;
3721 			else
3722 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3723 			mutex_exit(&tmp_ill->ill_lock);
3724 			if (tmp_ill->ill_isv6)
3725 				ill_set_nce_router_flags(tmp_ill, enable);
3726 			/* Notify routing socket listeners of this change. */
3727 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3728 		}
3729 	} else {
3730 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3731 		    (enable ? "Enabling" : "Disabling"),
3732 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3733 		mutex_enter(&ill->ill_lock);
3734 		if (enable)
3735 			ill->ill_flags |= ILLF_ROUTER;
3736 		else
3737 			ill->ill_flags &= ~ILLF_ROUTER;
3738 		mutex_exit(&ill->ill_lock);
3739 		if (ill->ill_isv6)
3740 			ill_set_nce_router_flags(ill, enable);
3741 		/* Notify routing socket listeners of this change. */
3742 		ip_rts_ifmsg(ill->ill_ipif);
3743 	}
3744 
3745 	return (0);
3746 }
3747 
3748 /*
3749  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3750  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3751  * set or clear.
3752  */
3753 static void
3754 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3755 {
3756 	ipif_t *ipif;
3757 	nce_t *nce;
3758 
3759 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3760 		nce = ndp_lookup(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3761 		if (nce != NULL) {
3762 			mutex_enter(&nce->nce_lock);
3763 			if (enable)
3764 				nce->nce_flags |= NCE_F_ISROUTER;
3765 			else
3766 				nce->nce_flags &= ~NCE_F_ISROUTER;
3767 			mutex_exit(&nce->nce_lock);
3768 			NCE_REFRELE(nce);
3769 		}
3770 	}
3771 }
3772 
3773 /*
3774  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3775  * for this ill.  Make sure the v6/v4 question has been answered about this
3776  * ill.  The creation of this ndd variable is only for backwards compatibility.
3777  * The preferred way to control per-interface IP forwarding is through the
3778  * ILLF_ROUTER interface flag.
3779  */
3780 static int
3781 ill_set_ndd_name(ill_t *ill)
3782 {
3783 	char *suffix;
3784 
3785 	ASSERT(IAM_WRITER_ILL(ill));
3786 
3787 	if (ill->ill_isv6)
3788 		suffix = ipv6_forward_suffix;
3789 	else
3790 		suffix = ipv4_forward_suffix;
3791 
3792 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3793 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3794 	/*
3795 	 * Copies over the '\0'.
3796 	 * Note that strlen(suffix) is always bounded.
3797 	 */
3798 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3799 	    strlen(suffix) + 1);
3800 
3801 	/*
3802 	 * Use of the nd table requires holding the reader lock.
3803 	 * Modifying the nd table thru nd_load/nd_unload requires
3804 	 * the writer lock.
3805 	 */
3806 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3807 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3808 	    nd_ill_forward_set, (caddr_t)ill)) {
3809 		/*
3810 		 * If the nd_load failed, it only meant that it could not
3811 		 * allocate a new bunch of room for further NDD expansion.
3812 		 * Because of that, the ill_ndd_name will be set to 0, and
3813 		 * this interface is at the mercy of the global ip_forwarding
3814 		 * variable.
3815 		 */
3816 		rw_exit(&ip_g_nd_lock);
3817 		ill->ill_ndd_name = NULL;
3818 		return (ENOMEM);
3819 	}
3820 	rw_exit(&ip_g_nd_lock);
3821 	return (0);
3822 }
3823 
3824 /*
3825  * Intializes the context structure and returns the first ill in the list
3826  * cuurently start_list and end_list can have values:
3827  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3828  * IP_V4_G_HEAD		Traverse IPV4 list only.
3829  * IP_V6_G_HEAD		Traverse IPV6 list only.
3830  */
3831 
3832 /*
3833  * We don't check for CONDEMNED ills here. Caller must do that if
3834  * necessary under the ill lock.
3835  */
3836 ill_t *
3837 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3838 {
3839 	ill_if_t *ifp;
3840 	ill_t *ill;
3841 	avl_tree_t *avl_tree;
3842 
3843 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3844 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3845 
3846 	/*
3847 	 * setup the lists to search
3848 	 */
3849 	if (end_list != MAX_G_HEADS) {
3850 		ctx->ctx_current_list = start_list;
3851 		ctx->ctx_last_list = end_list;
3852 	} else {
3853 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3854 		ctx->ctx_current_list = 0;
3855 	}
3856 
3857 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3858 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3859 		if (ifp != (ill_if_t *)
3860 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3861 			avl_tree = &ifp->illif_avl_by_ppa;
3862 			ill = avl_first(avl_tree);
3863 			/*
3864 			 * ill is guaranteed to be non NULL or ifp should have
3865 			 * not existed.
3866 			 */
3867 			ASSERT(ill != NULL);
3868 			return (ill);
3869 		}
3870 		ctx->ctx_current_list++;
3871 	}
3872 
3873 	return (NULL);
3874 }
3875 
3876 /*
3877  * returns the next ill in the list. ill_first() must have been called
3878  * before calling ill_next() or bad things will happen.
3879  */
3880 
3881 /*
3882  * We don't check for CONDEMNED ills here. Caller must do that if
3883  * necessary under the ill lock.
3884  */
3885 ill_t *
3886 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3887 {
3888 	ill_if_t *ifp;
3889 	ill_t *ill;
3890 
3891 
3892 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3893 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3894 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
3895 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3896 	    AVL_AFTER)) != NULL) {
3897 		return (ill);
3898 	}
3899 
3900 	/* goto next ill_ifp in the list. */
3901 	ifp = lastill->ill_ifptr->illif_next;
3902 
3903 	/* make sure not at end of circular list */
3904 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3905 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3906 			return (NULL);
3907 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3908 	}
3909 
3910 	return (avl_first(&ifp->illif_avl_by_ppa));
3911 }
3912 
3913 /*
3914  * Check interface name for correct format which is name+ppa.
3915  * name can contain characters and digits, the right most digits
3916  * make up the ppa number. use of octal is not allowed, name must contain
3917  * a ppa, return pointer to the start of ppa.
3918  * In case of error return NULL.
3919  */
3920 static char *
3921 ill_get_ppa_ptr(char *name)
3922 {
3923 	int namelen = mi_strlen(name);
3924 
3925 	int len = namelen;
3926 
3927 	name += len;
3928 	while (len > 0) {
3929 		name--;
3930 		if (*name < '0' || *name > '9')
3931 			break;
3932 		len--;
3933 	}
3934 
3935 	/* empty string, all digits, or no trailing digits */
3936 	if (len == 0 || len == (int)namelen)
3937 		return (NULL);
3938 
3939 	name++;
3940 	/* check for attempted use of octal */
3941 	if (*name == '0' && len != (int)namelen - 1)
3942 		return (NULL);
3943 	return (name);
3944 }
3945 
3946 /*
3947  * use avl tree to locate the ill.
3948  */
3949 static ill_t *
3950 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3951     ipsq_func_t func, int *error)
3952 {
3953 	char *ppa_ptr = NULL;
3954 	int len;
3955 	uint_t ppa;
3956 	ill_t *ill = NULL;
3957 	ill_if_t *ifp;
3958 	int list;
3959 	ipsq_t *ipsq;
3960 
3961 	if (error != NULL)
3962 		*error = 0;
3963 
3964 	/*
3965 	 * get ppa ptr
3966 	 */
3967 	if (isv6)
3968 		list = IP_V6_G_HEAD;
3969 	else
3970 		list = IP_V4_G_HEAD;
3971 
3972 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3973 		if (error != NULL)
3974 			*error = ENXIO;
3975 		return (NULL);
3976 	}
3977 
3978 	len = ppa_ptr - name + 1;
3979 
3980 	ppa = stoi(&ppa_ptr);
3981 
3982 	ifp = IP_VX_ILL_G_LIST(list);
3983 
3984 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
3985 		/*
3986 		 * match is done on len - 1 as the name is not null
3987 		 * terminated it contains ppa in addition to the interface
3988 		 * name.
3989 		 */
3990 		if ((ifp->illif_name_len == len) &&
3991 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
3992 			break;
3993 		} else {
3994 			ifp = ifp->illif_next;
3995 		}
3996 	}
3997 
3998 
3999 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4000 		/*
4001 		 * Even the interface type does not exist.
4002 		 */
4003 		if (error != NULL)
4004 			*error = ENXIO;
4005 		return (NULL);
4006 	}
4007 
4008 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4009 	if (ill != NULL) {
4010 		/*
4011 		 * The block comment at the start of ipif_down
4012 		 * explains the use of the macros used below
4013 		 */
4014 		GRAB_CONN_LOCK(q);
4015 		mutex_enter(&ill->ill_lock);
4016 		if (ILL_CAN_LOOKUP(ill)) {
4017 			ill_refhold_locked(ill);
4018 			mutex_exit(&ill->ill_lock);
4019 			RELEASE_CONN_LOCK(q);
4020 			return (ill);
4021 		} else if (ILL_CAN_WAIT(ill, q)) {
4022 			ipsq = ill->ill_phyint->phyint_ipsq;
4023 			mutex_enter(&ipsq->ipsq_lock);
4024 			mutex_exit(&ill->ill_lock);
4025 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4026 			mutex_exit(&ipsq->ipsq_lock);
4027 			RELEASE_CONN_LOCK(q);
4028 			*error = EINPROGRESS;
4029 			return (NULL);
4030 		}
4031 		mutex_exit(&ill->ill_lock);
4032 		RELEASE_CONN_LOCK(q);
4033 	}
4034 	if (error != NULL)
4035 		*error = ENXIO;
4036 	return (NULL);
4037 }
4038 
4039 /*
4040  * comparison function for use with avl.
4041  */
4042 static int
4043 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4044 {
4045 	uint_t ppa;
4046 	uint_t ill_ppa;
4047 
4048 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4049 
4050 	ppa = *((uint_t *)ppa_ptr);
4051 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4052 	/*
4053 	 * We want the ill with the lowest ppa to be on the
4054 	 * top.
4055 	 */
4056 	if (ill_ppa < ppa)
4057 		return (1);
4058 	if (ill_ppa > ppa)
4059 		return (-1);
4060 	return (0);
4061 }
4062 
4063 /*
4064  * remove an interface type from the global list.
4065  */
4066 static void
4067 ill_delete_interface_type(ill_if_t *interface)
4068 {
4069 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4070 
4071 	ASSERT(interface != NULL);
4072 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4073 
4074 	avl_destroy(&interface->illif_avl_by_ppa);
4075 	if (interface->illif_ppa_arena != NULL)
4076 		vmem_destroy(interface->illif_ppa_arena);
4077 
4078 	remque(interface);
4079 
4080 	mi_free(interface);
4081 }
4082 
4083 /*
4084  * remove ill from the global list.
4085  */
4086 static void
4087 ill_glist_delete(ill_t *ill)
4088 {
4089 	if (ill == NULL)
4090 		return;
4091 
4092 	rw_enter(&ill_g_lock, RW_WRITER);
4093 	/*
4094 	 * If the ill was never inserted into the AVL tree
4095 	 * we skip the if branch.
4096 	 */
4097 	if (ill->ill_ifptr != NULL) {
4098 		/*
4099 		 * remove from AVL tree and free ppa number
4100 		 */
4101 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4102 
4103 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4104 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4105 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4106 		}
4107 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4108 			ill_delete_interface_type(ill->ill_ifptr);
4109 		}
4110 
4111 		/*
4112 		 * Indicate ill is no longer in the list.
4113 		 */
4114 		ill->ill_ifptr = NULL;
4115 		ill->ill_name_length = 0;
4116 		ill->ill_name[0] = '\0';
4117 		ill->ill_ppa = UINT_MAX;
4118 	}
4119 	ill_phyint_free(ill);
4120 	rw_exit(&ill_g_lock);
4121 }
4122 
4123 /*
4124  * allocate a ppa, if the number of plumbed interfaces of this type are
4125  * less than ill_no_arena do a linear search to find a unused ppa.
4126  * When the number goes beyond ill_no_arena switch to using an arena.
4127  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4128  * is the return value for an error condition, so allocation starts at one
4129  * and is decremented by one.
4130  */
4131 static int
4132 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4133 {
4134 	ill_t *tmp_ill;
4135 	uint_t start, end;
4136 	int ppa;
4137 
4138 	if (ifp->illif_ppa_arena == NULL &&
4139 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4140 		/*
4141 		 * Create an arena.
4142 		 */
4143 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4144 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4145 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4146 			/* allocate what has already been assigned */
4147 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4148 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4149 		    tmp_ill, AVL_AFTER)) {
4150 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4151 			    1,		/* size */
4152 			    1,		/* align/quantum */
4153 			    0,		/* phase */
4154 			    0,		/* nocross */
4155 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4156 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4157 			    VM_NOSLEEP|VM_FIRSTFIT);
4158 			if (ppa == 0) {
4159 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4160 				    " failed while switching"));
4161 				vmem_destroy(ifp->illif_ppa_arena);
4162 				ifp->illif_ppa_arena = NULL;
4163 				break;
4164 			}
4165 		}
4166 	}
4167 
4168 	if (ifp->illif_ppa_arena != NULL) {
4169 		if (ill->ill_ppa == UINT_MAX) {
4170 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4171 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4172 			if (ppa == 0)
4173 				return (EAGAIN);
4174 			ill->ill_ppa = --ppa;
4175 		} else {
4176 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4177 			    1, 		/* size */
4178 			    1, 		/* align/quantum */
4179 			    0, 		/* phase */
4180 			    0, 		/* nocross */
4181 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4182 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4183 			    VM_NOSLEEP|VM_FIRSTFIT);
4184 			/*
4185 			 * Most likely the allocation failed because
4186 			 * the requested ppa was in use.
4187 			 */
4188 			if (ppa == 0)
4189 				return (EEXIST);
4190 		}
4191 		return (0);
4192 	}
4193 
4194 	/*
4195 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4196 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4197 	 */
4198 	if (ill->ill_ppa == UINT_MAX) {
4199 		end = UINT_MAX - 1;
4200 		start = 0;
4201 	} else {
4202 		end = start = ill->ill_ppa;
4203 	}
4204 
4205 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4206 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4207 		if (start++ >= end) {
4208 			if (ill->ill_ppa == UINT_MAX)
4209 				return (EAGAIN);
4210 			else
4211 				return (EEXIST);
4212 		}
4213 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4214 	}
4215 	ill->ill_ppa = start;
4216 	return (0);
4217 }
4218 
4219 /*
4220  * Insert ill into the list of configured ill's. Once this function completes,
4221  * the ill is globally visible and is available through lookups. More precisely
4222  * this happens after the caller drops the ill_g_lock.
4223  */
4224 static int
4225 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4226 {
4227 	ill_if_t *ill_interface;
4228 	avl_index_t where = 0;
4229 	int error;
4230 	int name_length;
4231 	int index;
4232 	boolean_t check_length = B_FALSE;
4233 
4234 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4235 
4236 	name_length = mi_strlen(name) + 1;
4237 
4238 	if (isv6)
4239 		index = IP_V6_G_HEAD;
4240 	else
4241 		index = IP_V4_G_HEAD;
4242 
4243 	ill_interface = IP_VX_ILL_G_LIST(index);
4244 	/*
4245 	 * Search for interface type based on name
4246 	 */
4247 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4248 		if ((ill_interface->illif_name_len == name_length) &&
4249 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4250 			break;
4251 		}
4252 		ill_interface = ill_interface->illif_next;
4253 	}
4254 
4255 	/*
4256 	 * Interface type not found, create one.
4257 	 */
4258 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4259 
4260 		ill_g_head_t ghead;
4261 
4262 		/*
4263 		 * allocate ill_if_t structure
4264 		 */
4265 
4266 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4267 		if (ill_interface == NULL) {
4268 			return (ENOMEM);
4269 		}
4270 
4271 
4272 
4273 		(void) strcpy(ill_interface->illif_name, name);
4274 		ill_interface->illif_name_len = name_length;
4275 
4276 		avl_create(&ill_interface->illif_avl_by_ppa,
4277 		    ill_compare_ppa, sizeof (ill_t),
4278 		    offsetof(struct ill_s, ill_avl_byppa));
4279 
4280 		/*
4281 		 * link the structure in the back to maintain order
4282 		 * of configuration for ifconfig output.
4283 		 */
4284 		ghead = ill_g_heads[index];
4285 		insque(ill_interface, ghead.ill_g_list_tail);
4286 
4287 	}
4288 
4289 	if (ill->ill_ppa == UINT_MAX)
4290 		check_length = B_TRUE;
4291 
4292 	error = ill_alloc_ppa(ill_interface, ill);
4293 	if (error != 0) {
4294 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4295 			ill_delete_interface_type(ill->ill_ifptr);
4296 		return (error);
4297 	}
4298 
4299 	/*
4300 	 * When the ppa is choosen by the system, check that there is
4301 	 * enough space to insert ppa. if a specific ppa was passed in this
4302 	 * check is not required as the interface name passed in will have
4303 	 * the right ppa in it.
4304 	 */
4305 	if (check_length) {
4306 		/*
4307 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4308 		 */
4309 		char buf[sizeof (uint_t) * 3];
4310 
4311 		/*
4312 		 * convert ppa to string to calculate the amount of space
4313 		 * required for it in the name.
4314 		 */
4315 		numtos(ill->ill_ppa, buf);
4316 
4317 		/* Do we have enough space to insert ppa ? */
4318 
4319 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4320 			/* Free ppa and interface type struct */
4321 			if (ill_interface->illif_ppa_arena != NULL) {
4322 				vmem_free(ill_interface->illif_ppa_arena,
4323 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4324 			}
4325 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4326 			    0) {
4327 				ill_delete_interface_type(ill->ill_ifptr);
4328 			}
4329 
4330 			return (EINVAL);
4331 		}
4332 	}
4333 
4334 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4335 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4336 
4337 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4338 	    &where);
4339 	ill->ill_ifptr = ill_interface;
4340 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4341 
4342 	ill_phyint_reinit(ill);
4343 	return (0);
4344 }
4345 
4346 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4347 static boolean_t
4348 ipsq_init(ill_t *ill)
4349 {
4350 	ipsq_t  *ipsq;
4351 
4352 	/* Init the ipsq and impicitly enter as writer */
4353 	ill->ill_phyint->phyint_ipsq =
4354 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4355 	if (ill->ill_phyint->phyint_ipsq == NULL)
4356 		return (B_FALSE);
4357 	ipsq = ill->ill_phyint->phyint_ipsq;
4358 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4359 	ill->ill_phyint->phyint_ipsq_next = NULL;
4360 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4361 	ipsq->ipsq_refs = 1;
4362 	ipsq->ipsq_writer = curthread;
4363 	ipsq->ipsq_reentry_cnt = 1;
4364 #ifdef ILL_DEBUG
4365 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4366 #endif
4367 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4368 	return (B_TRUE);
4369 }
4370 
4371 /*
4372  * ill_init is called by ip_open when a device control stream is opened.
4373  * It does a few initializations, and shoots a DL_INFO_REQ message down
4374  * to the driver.  The response is later picked up in ip_rput_dlpi and
4375  * used to set up default mechanisms for talking to the driver.  (Always
4376  * called as writer.)
4377  *
4378  * If this function returns error, ip_open will call ip_close which in
4379  * turn will call ill_delete to clean up any memory allocated here that
4380  * is not yet freed.
4381  */
4382 int
4383 ill_init(queue_t *q, ill_t *ill)
4384 {
4385 	int	count;
4386 	dl_info_req_t	*dlir;
4387 	mblk_t	*info_mp;
4388 	uchar_t *frag_ptr;
4389 
4390 	/*
4391 	 * The ill is initialized to zero by mi_alloc*(). In addition
4392 	 * some fields already contain valid values, initialized in
4393 	 * ip_open(), before we reach here.
4394 	 */
4395 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4396 
4397 	ill->ill_rq = q;
4398 	ill->ill_wq = WR(q);
4399 
4400 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4401 	    BPRI_HI);
4402 	if (info_mp == NULL)
4403 		return (ENOMEM);
4404 
4405 	/*
4406 	 * Allocate sufficient space to contain our fragment hash table and
4407 	 * the device name.
4408 	 */
4409 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4410 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4411 	if (frag_ptr == NULL) {
4412 		freemsg(info_mp);
4413 		return (ENOMEM);
4414 	}
4415 	ill->ill_frag_ptr = frag_ptr;
4416 	ill->ill_frag_free_num_pkts = 0;
4417 	ill->ill_last_frag_clean_time = 0;
4418 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4419 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4420 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4421 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4422 		    NULL, MUTEX_DEFAULT, NULL);
4423 	}
4424 
4425 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4426 	if (ill->ill_phyint == NULL) {
4427 		freemsg(info_mp);
4428 		mi_free(frag_ptr);
4429 		return (ENOMEM);
4430 	}
4431 
4432 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4433 	/*
4434 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4435 	 * at this point because of the following reason. If we can't
4436 	 * enter the ipsq at some point and cv_wait, the writer that
4437 	 * wakes us up tries to locate us using the list of all phyints
4438 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4439 	 * If we don't set it now, we risk a missed wakeup.
4440 	 */
4441 	ill->ill_phyint->phyint_illv4 = ill;
4442 	ill->ill_ppa = UINT_MAX;
4443 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4444 
4445 	if (!ipsq_init(ill)) {
4446 		freemsg(info_mp);
4447 		mi_free(frag_ptr);
4448 		mi_free(ill->ill_phyint);
4449 		return (ENOMEM);
4450 	}
4451 
4452 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4453 
4454 
4455 	/* Frag queue limit stuff */
4456 	ill->ill_frag_count = 0;
4457 	ill->ill_ipf_gen = 0;
4458 
4459 	ill->ill_global_timer = INFINITY;
4460 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4461 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4462 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4463 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4464 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4465 
4466 	/*
4467 	 * Initialize IPv6 configuration variables.  The IP module is always
4468 	 * opened as an IPv4 module.  Instead tracking down the cases where
4469 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4470 	 * here for convenience, this has no effect until the ill is set to do
4471 	 * IPv6.
4472 	 */
4473 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4474 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4475 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4476 	ill->ill_max_buf = ND_MAX_Q;
4477 	ill->ill_refcnt = 0;
4478 
4479 	/* Send down the Info Request to the driver. */
4480 	info_mp->b_datap->db_type = M_PCPROTO;
4481 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4482 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4483 	dlir->dl_primitive = DL_INFO_REQ;
4484 
4485 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4486 
4487 	qprocson(q);
4488 	ill_dlpi_send(ill, info_mp);
4489 
4490 	return (0);
4491 }
4492 
4493 /*
4494  * ill_dls_info
4495  * creates datalink socket info from the device.
4496  */
4497 int
4498 ill_dls_info(struct sockaddr_dl *sdl, ipif_t *ipif)
4499 {
4500 	size_t	length;
4501 	ill_t	*ill = ipif->ipif_ill;
4502 
4503 	sdl->sdl_family = AF_LINK;
4504 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4505 	sdl->sdl_type = ipif->ipif_type;
4506 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4507 	length = mi_strlen(sdl->sdl_data);
4508 	ASSERT(length < 256);
4509 	sdl->sdl_nlen = (uchar_t)length;
4510 	sdl->sdl_alen = ill->ill_phys_addr_length;
4511 	mutex_enter(&ill->ill_lock);
4512 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4513 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4514 		    ill->ill_phys_addr_length);
4515 	}
4516 	mutex_exit(&ill->ill_lock);
4517 	sdl->sdl_slen = 0;
4518 	return (sizeof (struct sockaddr_dl));
4519 }
4520 
4521 /*
4522  * ill_xarp_info
4523  * creates xarp info from the device.
4524  */
4525 static int
4526 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4527 {
4528 	sdl->sdl_family = AF_LINK;
4529 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4530 	sdl->sdl_type = ill->ill_type;
4531 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4532 	    sizeof (sdl->sdl_data));
4533 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4534 	sdl->sdl_alen = ill->ill_phys_addr_length;
4535 	sdl->sdl_slen = 0;
4536 	return (sdl->sdl_nlen);
4537 }
4538 
4539 static int
4540 loopback_kstat_update(kstat_t *ksp, int rw)
4541 {
4542 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4543 
4544 	if (rw == KSTAT_WRITE)
4545 		return (EACCES);
4546 	kn[0].value.ui32 = loopback_packets;
4547 	kn[1].value.ui32 = loopback_packets;
4548 	return (0);
4549 }
4550 
4551 
4552 /*
4553  * Has ifindex been plumbed already.
4554  */
4555 static boolean_t
4556 phyint_exists(uint_t index)
4557 {
4558 	phyint_t *phyi;
4559 
4560 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4561 	/*
4562 	 * Indexes are stored in the phyint - a common structure
4563 	 * to both IPv4 and IPv6.
4564 	 */
4565 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4566 	    (void *) &index, NULL);
4567 	return (phyi != NULL);
4568 }
4569 
4570 /*
4571  * Assign a unique interface index for the phyint.
4572  */
4573 static boolean_t
4574 phyint_assign_ifindex(phyint_t *phyi)
4575 {
4576 	uint_t starting_index;
4577 
4578 	ASSERT(phyi->phyint_ifindex == 0);
4579 	if (!ill_index_wrap) {
4580 		phyi->phyint_ifindex = ill_index++;
4581 		if (ill_index == 0) {
4582 			/* Reached the uint_t limit Next time wrap  */
4583 			ill_index_wrap = B_TRUE;
4584 		}
4585 		return (B_TRUE);
4586 	}
4587 
4588 	/*
4589 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4590 	 * at this point and don't want to call any function that attempts
4591 	 * to get the lock again.
4592 	 */
4593 	starting_index = ill_index++;
4594 	for (; ill_index != starting_index; ill_index++) {
4595 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4596 			/* found unused index - use it */
4597 			phyi->phyint_ifindex = ill_index;
4598 			return (B_TRUE);
4599 		}
4600 	}
4601 
4602 	/*
4603 	 * all interface indicies are inuse.
4604 	 */
4605 	return (B_FALSE);
4606 }
4607 
4608 /*
4609  * Return a pointer to the ill which matches the supplied name.  Note that
4610  * the ill name length includes the null termination character.  (May be
4611  * called as writer.)
4612  * If do_alloc and the interface is "lo0" it will be automatically created.
4613  * Cannot bump up reference on condemned ills. So dup detect can't be done
4614  * using this func.
4615  */
4616 ill_t *
4617 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4618     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4619 {
4620 	ill_t	*ill;
4621 	ipif_t	*ipif;
4622 	kstat_named_t	*kn;
4623 	boolean_t isloopback;
4624 	ipsq_t *old_ipsq;
4625 
4626 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4627 
4628 	rw_enter(&ill_g_lock, RW_READER);
4629 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4630 	rw_exit(&ill_g_lock);
4631 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4632 		return (ill);
4633 
4634 	/*
4635 	 * Couldn't find it.  Does this happen to be a lookup for the
4636 	 * loopback device and are we allowed to allocate it?
4637 	 */
4638 	if (!isloopback || !do_alloc)
4639 		return (NULL);
4640 
4641 	rw_enter(&ill_g_lock, RW_WRITER);
4642 
4643 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4644 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4645 		rw_exit(&ill_g_lock);
4646 		return (ill);
4647 	}
4648 
4649 	/* Create the loopback device on demand */
4650 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4651 	    sizeof (ipif_loopback_name), BPRI_MED));
4652 	if (ill == NULL)
4653 		goto done;
4654 
4655 	*ill = ill_null;
4656 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4657 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4658 	if (ill->ill_phyint == NULL)
4659 		goto done;
4660 
4661 	if (isv6)
4662 		ill->ill_phyint->phyint_illv6 = ill;
4663 	else
4664 		ill->ill_phyint->phyint_illv4 = ill;
4665 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4666 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4667 	/* Add room for tcp+ip headers */
4668 	if (isv6) {
4669 		ill->ill_isv6 = B_TRUE;
4670 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4671 		if (!ill_allocate_mibs(ill))
4672 			goto done;
4673 	} else {
4674 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4675 	}
4676 	ill->ill_max_mtu = ill->ill_max_frag;
4677 	/*
4678 	 * ipif_loopback_name can't be pointed at directly because its used
4679 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4680 	 * from the glist, ill_glist_delete() sets the first character of
4681 	 * ill_name to '\0'.
4682 	 */
4683 	ill->ill_name = (char *)ill + sizeof (*ill);
4684 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4685 	ill->ill_name_length = sizeof (ipif_loopback_name);
4686 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4687 
4688 	ill->ill_global_timer = INFINITY;
4689 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4690 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4691 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4692 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4693 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4694 
4695 	/* No resolver here. */
4696 	ill->ill_net_type = IRE_LOOPBACK;
4697 
4698 	/* Initialize the ipsq */
4699 	if (!ipsq_init(ill))
4700 		goto done;
4701 
4702 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4703 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4704 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4705 #ifdef ILL_DEBUG
4706 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4707 #endif
4708 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4709 	if (ipif == NULL)
4710 		goto done;
4711 
4712 	ill->ill_flags = ILLF_MULTICAST;
4713 
4714 	/* Set up default loopback address and mask. */
4715 	if (!isv6) {
4716 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4717 
4718 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4719 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4720 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4721 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4722 		    ipif->ipif_v6subnet);
4723 		ill->ill_flags |= ILLF_IPV4;
4724 	} else {
4725 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4726 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4727 		ipif->ipif_v6net_mask = ipv6_all_ones;
4728 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4729 		    ipif->ipif_v6subnet);
4730 		ill->ill_flags |= ILLF_IPV6;
4731 	}
4732 
4733 	/*
4734 	 * Chain us in at the end of the ill list. hold the ill
4735 	 * before we make it globally visible. 1 for the lookup.
4736 	 */
4737 	ill->ill_refcnt = 0;
4738 	ill_refhold(ill);
4739 
4740 	ill->ill_frag_count = 0;
4741 	ill->ill_frag_free_num_pkts = 0;
4742 	ill->ill_last_frag_clean_time = 0;
4743 
4744 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4745 
4746 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4747 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4748 
4749 	/* Let SCTP know so that it can add this to its list */
4750 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4751 
4752 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4753 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4754 
4755 	/*
4756 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4757 	 */
4758 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4759 		/* Loopback ills aren't in any IPMP group */
4760 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4761 		ipsq_delete(old_ipsq);
4762 	}
4763 
4764 	/*
4765 	 * Delay this till the ipif is allocated as ipif_allocate
4766 	 * de-references ill_phyint for getting the ifindex. We
4767 	 * can't do this before ipif_allocate because ill_phyint_reinit
4768 	 * -> phyint_assign_ifindex expects ipif to be present.
4769 	 */
4770 	mutex_enter(&ill->ill_phyint->phyint_lock);
4771 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4772 	mutex_exit(&ill->ill_phyint->phyint_lock);
4773 
4774 	if (loopback_ksp == NULL) {
4775 		/* Export loopback interface statistics */
4776 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4777 		    KSTAT_TYPE_NAMED, 2, 0);
4778 		if (loopback_ksp != NULL) {
4779 			loopback_ksp->ks_update = loopback_kstat_update;
4780 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4781 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4782 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4783 			kstat_install(loopback_ksp);
4784 		}
4785 	}
4786 
4787 	if (error != NULL)
4788 		*error = 0;
4789 	*did_alloc = B_TRUE;
4790 	rw_exit(&ill_g_lock);
4791 	return (ill);
4792 done:
4793 	if (ill != NULL) {
4794 		if (ill->ill_phyint != NULL) {
4795 			ipsq_t	*ipsq;
4796 
4797 			ipsq = ill->ill_phyint->phyint_ipsq;
4798 			if (ipsq != NULL)
4799 				kmem_free(ipsq, sizeof (ipsq_t));
4800 			mi_free(ill->ill_phyint);
4801 		}
4802 		ill_free_mib(ill);
4803 		mi_free(ill);
4804 	}
4805 	rw_exit(&ill_g_lock);
4806 	if (error != NULL)
4807 		*error = ENOMEM;
4808 	return (NULL);
4809 }
4810 
4811 /*
4812  * Return a pointer to the ill which matches the index and IP version type.
4813  */
4814 ill_t *
4815 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4816     ipsq_func_t func, int *err)
4817 {
4818 	ill_t	*ill;
4819 	ipsq_t  *ipsq;
4820 	phyint_t *phyi;
4821 
4822 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4823 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4824 
4825 	if (err != NULL)
4826 		*err = 0;
4827 
4828 	/*
4829 	 * Indexes are stored in the phyint - a common structure
4830 	 * to both IPv4 and IPv6.
4831 	 */
4832 	rw_enter(&ill_g_lock, RW_READER);
4833 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4834 	    (void *) &index, NULL);
4835 	if (phyi != NULL) {
4836 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4837 		if (ill != NULL) {
4838 			/*
4839 			 * The block comment at the start of ipif_down
4840 			 * explains the use of the macros used below
4841 			 */
4842 			GRAB_CONN_LOCK(q);
4843 			mutex_enter(&ill->ill_lock);
4844 			if (ILL_CAN_LOOKUP(ill)) {
4845 				ill_refhold_locked(ill);
4846 				mutex_exit(&ill->ill_lock);
4847 				RELEASE_CONN_LOCK(q);
4848 				rw_exit(&ill_g_lock);
4849 				return (ill);
4850 			} else if (ILL_CAN_WAIT(ill, q)) {
4851 				ipsq = ill->ill_phyint->phyint_ipsq;
4852 				mutex_enter(&ipsq->ipsq_lock);
4853 				rw_exit(&ill_g_lock);
4854 				mutex_exit(&ill->ill_lock);
4855 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4856 				mutex_exit(&ipsq->ipsq_lock);
4857 				RELEASE_CONN_LOCK(q);
4858 				*err = EINPROGRESS;
4859 				return (NULL);
4860 			}
4861 			RELEASE_CONN_LOCK(q);
4862 			mutex_exit(&ill->ill_lock);
4863 		}
4864 	}
4865 	rw_exit(&ill_g_lock);
4866 	if (err != NULL)
4867 		*err = ENXIO;
4868 	return (NULL);
4869 }
4870 
4871 /*
4872  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4873  * that gives a running thread a reference to the ill. This reference must be
4874  * released by the thread when it is done accessing the ill and related
4875  * objects. ill_refcnt can not be used to account for static references
4876  * such as other structures pointing to an ill. Callers must generally
4877  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4878  * or be sure that the ill is not being deleted or changing state before
4879  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4880  * ill won't change any of its critical state such as address, netmask etc.
4881  */
4882 void
4883 ill_refhold(ill_t *ill)
4884 {
4885 	mutex_enter(&ill->ill_lock);
4886 	ill->ill_refcnt++;
4887 	ILL_TRACE_REF(ill);
4888 	mutex_exit(&ill->ill_lock);
4889 }
4890 
4891 void
4892 ill_refhold_locked(ill_t *ill)
4893 {
4894 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4895 	ill->ill_refcnt++;
4896 	ILL_TRACE_REF(ill);
4897 }
4898 
4899 int
4900 ill_check_and_refhold(ill_t *ill)
4901 {
4902 	mutex_enter(&ill->ill_lock);
4903 	if (ILL_CAN_LOOKUP(ill)) {
4904 		ill_refhold_locked(ill);
4905 		mutex_exit(&ill->ill_lock);
4906 		return (0);
4907 	}
4908 	mutex_exit(&ill->ill_lock);
4909 	return (ILL_LOOKUP_FAILED);
4910 }
4911 
4912 /*
4913  * Must not be called while holding any locks. Otherwise if this is
4914  * the last reference to be released, there is a chance of recursive mutex
4915  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4916  * to restart an ioctl.
4917  */
4918 void
4919 ill_refrele(ill_t *ill)
4920 {
4921 	mutex_enter(&ill->ill_lock);
4922 	ASSERT(ill->ill_refcnt != 0);
4923 	ill->ill_refcnt--;
4924 	ILL_UNTRACE_REF(ill);
4925 	if (ill->ill_refcnt != 0) {
4926 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4927 		mutex_exit(&ill->ill_lock);
4928 		return;
4929 	}
4930 
4931 	/* Drops the ill_lock */
4932 	ipif_ill_refrele_tail(ill);
4933 }
4934 
4935 /*
4936  * Obtain a weak reference count on the ill. This reference ensures the
4937  * ill won't be freed, but the ill may change any of its critical state
4938  * such as netmask, address etc. Returns an error if the ill has started
4939  * closing.
4940  */
4941 boolean_t
4942 ill_waiter_inc(ill_t *ill)
4943 {
4944 	mutex_enter(&ill->ill_lock);
4945 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4946 		mutex_exit(&ill->ill_lock);
4947 		return (B_FALSE);
4948 	}
4949 	ill->ill_waiters++;
4950 	mutex_exit(&ill->ill_lock);
4951 	return (B_TRUE);
4952 }
4953 
4954 void
4955 ill_waiter_dcr(ill_t *ill)
4956 {
4957 	mutex_enter(&ill->ill_lock);
4958 	ill->ill_waiters--;
4959 	if (ill->ill_waiters == 0)
4960 		cv_broadcast(&ill->ill_cv);
4961 	mutex_exit(&ill->ill_lock);
4962 }
4963 
4964 /*
4965  * Named Dispatch routine to produce a formatted report on all ILLs.
4966  * This report is accessed by using the ndd utility to "get" ND variable
4967  * "ip_ill_status".
4968  */
4969 /* ARGSUSED */
4970 int
4971 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
4972 {
4973 	ill_t		*ill;
4974 	ill_walk_context_t ctx;
4975 
4976 	(void) mi_mpprintf(mp,
4977 	    "ILL      " MI_COL_HDRPAD_STR
4978 	/*   01234567[89ABCDEF] */
4979 	    "rq       " MI_COL_HDRPAD_STR
4980 	/*   01234567[89ABCDEF] */
4981 	    "wq       " MI_COL_HDRPAD_STR
4982 	/*   01234567[89ABCDEF] */
4983 	    "upcnt mxfrg err name");
4984 	/*   12345 12345 123 xxxxxxxx  */
4985 
4986 	rw_enter(&ill_g_lock, RW_READER);
4987 	ill = ILL_START_WALK_ALL(&ctx);
4988 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4989 		(void) mi_mpprintf(mp,
4990 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
4991 		    "%05u %05u %03d %s",
4992 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
4993 		    ill->ill_ipif_up_count,
4994 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
4995 	}
4996 	rw_exit(&ill_g_lock);
4997 
4998 	return (0);
4999 }
5000 
5001 /*
5002  * Named Dispatch routine to produce a formatted report on all IPIFs.
5003  * This report is accessed by using the ndd utility to "get" ND variable
5004  * "ip_ipif_status".
5005  */
5006 /* ARGSUSED */
5007 int
5008 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5009 {
5010 	char	buf1[INET6_ADDRSTRLEN];
5011 	char	buf2[INET6_ADDRSTRLEN];
5012 	char	buf3[INET6_ADDRSTRLEN];
5013 	char	buf4[INET6_ADDRSTRLEN];
5014 	char	buf5[INET6_ADDRSTRLEN];
5015 	char	buf6[INET6_ADDRSTRLEN];
5016 	char	buf[LIFNAMSIZ];
5017 	ill_t	*ill;
5018 	ipif_t	*ipif;
5019 	nv_t	*nvp;
5020 	uint64_t flags;
5021 	zoneid_t zoneid;
5022 	ill_walk_context_t ctx;
5023 
5024 	(void) mi_mpprintf(mp,
5025 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5026 	    "\tlocal address\n"
5027 	    "\tsrc address\n"
5028 	    "\tsubnet\n"
5029 	    "\tmask\n"
5030 	    "\tbroadcast\n"
5031 	    "\tp-p-dst");
5032 
5033 	ASSERT(q->q_next == NULL);
5034 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5035 
5036 	rw_enter(&ill_g_lock, RW_READER);
5037 	ill = ILL_START_WALK_ALL(&ctx);
5038 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5039 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
5040 			if (zoneid != GLOBAL_ZONEID &&
5041 			    zoneid != ipif->ipif_zoneid)
5042 				continue;
5043 			(void) mi_mpprintf(mp,
5044 			    MI_COL_PTRFMT_STR
5045 			    "%04u %05u %u/%u/%u %s %d",
5046 			    (void *)ipif,
5047 			    ipif->ipif_metric, ipif->ipif_mtu,
5048 			    ipif->ipif_ib_pkt_count,
5049 			    ipif->ipif_ob_pkt_count,
5050 			    ipif->ipif_fo_pkt_count,
5051 			    ipif_get_name(ipif, buf, sizeof (buf)),
5052 			    ipif->ipif_zoneid);
5053 
5054 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5055 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5056 
5057 		/* Tack on text strings for any flags. */
5058 		nvp = ipif_nv_tbl;
5059 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5060 			if (nvp->nv_value & flags)
5061 				(void) mi_mpprintf_nr(mp, " %s",
5062 				    nvp->nv_name);
5063 		}
5064 		(void) mi_mpprintf(mp,
5065 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5066 		    inet_ntop(AF_INET6,
5067 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5068 		    inet_ntop(AF_INET6,
5069 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5070 		    inet_ntop(AF_INET6,
5071 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5072 		    inet_ntop(AF_INET6,
5073 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5074 		    inet_ntop(AF_INET6,
5075 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5076 		    inet_ntop(AF_INET6,
5077 			&ipif->ipif_v6pp_dst_addr,
5078 			buf6, sizeof (buf6)));
5079 		}
5080 	}
5081 	rw_exit(&ill_g_lock);
5082 	return (0);
5083 }
5084 
5085 /*
5086  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5087  * driver.  We construct best guess defaults for lower level information that
5088  * we need.  If an interface is brought up without injection of any overriding
5089  * information from outside, we have to be ready to go with these defaults.
5090  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5091  * we primarely want the dl_provider_style.
5092  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5093  * at which point we assume the other part of the information is valid.
5094  */
5095 void
5096 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5097 {
5098 	uchar_t		*brdcst_addr;
5099 	uint_t		brdcst_addr_length, phys_addr_length;
5100 	t_scalar_t	sap_length;
5101 	dl_info_ack_t	*dlia;
5102 	ip_m_t		*ipm;
5103 	dl_qos_cl_sel1_t *sel1;
5104 
5105 	ASSERT(IAM_WRITER_ILL(ill));
5106 
5107 	/*
5108 	 * Till the ill is fully up ILL_CHANGING will be set and
5109 	 * the ill is not globally visible. So no need for a lock.
5110 	 */
5111 	dlia = (dl_info_ack_t *)mp->b_rptr;
5112 	ill->ill_mactype = dlia->dl_mac_type;
5113 
5114 	ipm = ip_m_lookup(dlia->dl_mac_type);
5115 	if (ipm == NULL) {
5116 		ipm = ip_m_lookup(DL_OTHER);
5117 		ASSERT(ipm != NULL);
5118 	}
5119 	ill->ill_media = ipm;
5120 
5121 	/*
5122 	 * When the new DLPI stuff is ready we'll pull lengths
5123 	 * from dlia.
5124 	 */
5125 	if (dlia->dl_version == DL_VERSION_2) {
5126 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5127 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5128 		    brdcst_addr_length);
5129 		if (brdcst_addr == NULL) {
5130 			brdcst_addr_length = 0;
5131 		}
5132 		sap_length = dlia->dl_sap_length;
5133 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5134 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5135 		    brdcst_addr_length, sap_length, phys_addr_length));
5136 	} else {
5137 		brdcst_addr_length = 6;
5138 		brdcst_addr = ip_six_byte_all_ones;
5139 		sap_length = -2;
5140 		phys_addr_length = brdcst_addr_length;
5141 	}
5142 
5143 	ill->ill_bcast_addr_length = brdcst_addr_length;
5144 	ill->ill_phys_addr_length = phys_addr_length;
5145 	ill->ill_sap_length = sap_length;
5146 	ill->ill_max_frag = dlia->dl_max_sdu;
5147 	ill->ill_max_mtu = ill->ill_max_frag;
5148 
5149 	ill->ill_type = ipm->ip_m_type;
5150 
5151 	if (!ill->ill_dlpi_style_set) {
5152 		if (dlia->dl_provider_style == DL_STYLE2)
5153 			ill->ill_needs_attach = 1;
5154 
5155 		/*
5156 		 * Allocate the first ipif on this ill. We don't delay it
5157 		 * further as ioctl handling assumes atleast one ipif to
5158 		 * be present.
5159 		 *
5160 		 * At this point we don't know whether the ill is v4 or v6.
5161 		 * We will know this whan the SIOCSLIFNAME happens and
5162 		 * the correct value for ill_isv6 will be assigned in
5163 		 * ipif_set_values(). We need to hold the ill lock and
5164 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5165 		 * the wakeup.
5166 		 */
5167 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5168 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5169 		mutex_enter(&ill->ill_lock);
5170 		ASSERT(ill->ill_dlpi_style_set == 0);
5171 		ill->ill_dlpi_style_set = 1;
5172 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5173 		cv_broadcast(&ill->ill_cv);
5174 		mutex_exit(&ill->ill_lock);
5175 		freemsg(mp);
5176 		return;
5177 	}
5178 	ASSERT(ill->ill_ipif != NULL);
5179 	/*
5180 	 * We know whether it is IPv4 or IPv6 now, as this is the
5181 	 * second DL_INFO_ACK we are recieving in response to the
5182 	 * DL_INFO_REQ sent in ipif_set_values.
5183 	 */
5184 	if (ill->ill_isv6)
5185 		ill->ill_sap = IP6_DL_SAP;
5186 	else
5187 		ill->ill_sap = IP_DL_SAP;
5188 	/*
5189 	 * Set ipif_mtu which is used to set the IRE's
5190 	 * ire_max_frag value. The driver could have sent
5191 	 * a different mtu from what it sent last time. No
5192 	 * need to call ipif_mtu_change because IREs have
5193 	 * not yet been created.
5194 	 */
5195 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5196 	/*
5197 	 * Clear all the flags that were set based on ill_bcast_addr_length
5198 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5199 	 * changed now and we need to re-evaluate.
5200 	 */
5201 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5202 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5203 
5204 	/*
5205 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5206 	 * changed now.
5207 	 */
5208 	if (ill->ill_bcast_addr_length == 0) {
5209 		if (ill->ill_resolver_mp != NULL)
5210 			freemsg(ill->ill_resolver_mp);
5211 		if (ill->ill_bcast_mp != NULL)
5212 			freemsg(ill->ill_bcast_mp);
5213 		if (ill->ill_flags & ILLF_XRESOLV)
5214 			ill->ill_net_type = IRE_IF_RESOLVER;
5215 		else
5216 			ill->ill_net_type = IRE_IF_NORESOLVER;
5217 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5218 		    ill->ill_phys_addr_length,
5219 		    ill->ill_sap,
5220 		    ill->ill_sap_length);
5221 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5222 
5223 		if (ill->ill_isv6)
5224 			/*
5225 			 * Note: xresolv interfaces will eventually need NOARP
5226 			 * set here as well, but that will require those
5227 			 * external resolvers to have some knowledge of
5228 			 * that flag and act appropriately. Not to be changed
5229 			 * at present.
5230 			 */
5231 			ill->ill_flags |= ILLF_NONUD;
5232 		else
5233 			ill->ill_flags |= ILLF_NOARP;
5234 
5235 		if (ill->ill_phys_addr_length == 0) {
5236 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5237 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5238 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5239 			} else {
5240 				/* pt-pt supports multicast. */
5241 				ill->ill_flags |= ILLF_MULTICAST;
5242 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5243 			}
5244 		}
5245 	} else {
5246 		ill->ill_net_type = IRE_IF_RESOLVER;
5247 		if (ill->ill_bcast_mp != NULL)
5248 			freemsg(ill->ill_bcast_mp);
5249 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5250 		    ill->ill_bcast_addr_length, ill->ill_sap,
5251 		    ill->ill_sap_length);
5252 		/*
5253 		 * Later detect lack of DLPI driver multicast
5254 		 * capability by catching DL_ENABMULTI errors in
5255 		 * ip_rput_dlpi.
5256 		 */
5257 		ill->ill_flags |= ILLF_MULTICAST;
5258 		if (!ill->ill_isv6)
5259 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5260 	}
5261 	/* By default an interface does not support any CoS marking */
5262 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5263 
5264 	/*
5265 	 * If we get QoS information in DL_INFO_ACK, the device supports
5266 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5267 	 */
5268 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5269 	    dlia->dl_qos_length);
5270 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5271 		ill->ill_flags |= ILLF_COS_ENABLED;
5272 	}
5273 
5274 	/* Clear any previous error indication. */
5275 	ill->ill_error = 0;
5276 	freemsg(mp);
5277 }
5278 
5279 /*
5280  * Perform various checks to verify that an address would make sense as a
5281  * local, remote, or subnet interface address.
5282  */
5283 static boolean_t
5284 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5285 {
5286 	ipaddr_t	net_mask;
5287 
5288 	/*
5289 	 * Don't allow all zeroes, all ones or experimental address, but allow
5290 	 * all ones netmask.
5291 	 */
5292 	if ((net_mask = ip_net_mask(addr)) == 0)
5293 		return (B_FALSE);
5294 	/* A given netmask overrides the "guess" netmask */
5295 	if (subnet_mask != 0)
5296 		net_mask = subnet_mask;
5297 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5298 	    (addr == (addr | ~net_mask)))) {
5299 		return (B_FALSE);
5300 	}
5301 	if (CLASSD(addr))
5302 		return (B_FALSE);
5303 
5304 	return (B_TRUE);
5305 }
5306 
5307 /*
5308  * ipif_lookup_group
5309  * Returns held ipif
5310  */
5311 ipif_t *
5312 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5313 {
5314 	ire_t	*ire;
5315 	ipif_t	*ipif;
5316 
5317 	ire = ire_lookup_multi(group, zoneid);
5318 	if (ire == NULL)
5319 		return (NULL);
5320 	ipif = ire->ire_ipif;
5321 	ipif_refhold(ipif);
5322 	ire_refrele(ire);
5323 	return (ipif);
5324 }
5325 
5326 /*
5327  * Look for an ipif with the specified interface address and destination.
5328  * The destination address is used only for matching point-to-point interfaces.
5329  */
5330 ipif_t *
5331 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5332     ipsq_func_t func, int *error)
5333 {
5334 	ipif_t	*ipif;
5335 	ill_t	*ill;
5336 	ill_walk_context_t ctx;
5337 	ipsq_t	*ipsq;
5338 
5339 	if (error != NULL)
5340 		*error = 0;
5341 
5342 	/*
5343 	 * First match all the point-to-point interfaces
5344 	 * before looking at non-point-to-point interfaces.
5345 	 * This is done to avoid returning non-point-to-point
5346 	 * ipif instead of unnumbered point-to-point ipif.
5347 	 */
5348 	rw_enter(&ill_g_lock, RW_READER);
5349 	ill = ILL_START_WALK_V4(&ctx);
5350 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5351 		GRAB_CONN_LOCK(q);
5352 		mutex_enter(&ill->ill_lock);
5353 		for (ipif = ill->ill_ipif; ipif != NULL;
5354 		    ipif = ipif->ipif_next) {
5355 			/* Allow the ipif to be down */
5356 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5357 			    (ipif->ipif_lcl_addr == if_addr) &&
5358 			    (ipif->ipif_pp_dst_addr == dst)) {
5359 				/*
5360 				 * The block comment at the start of ipif_down
5361 				 * explains the use of the macros used below
5362 				 */
5363 				if (IPIF_CAN_LOOKUP(ipif)) {
5364 					ipif_refhold_locked(ipif);
5365 					mutex_exit(&ill->ill_lock);
5366 					RELEASE_CONN_LOCK(q);
5367 					rw_exit(&ill_g_lock);
5368 					return (ipif);
5369 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5370 					ipsq = ill->ill_phyint->phyint_ipsq;
5371 					mutex_enter(&ipsq->ipsq_lock);
5372 					mutex_exit(&ill->ill_lock);
5373 					rw_exit(&ill_g_lock);
5374 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5375 						ill);
5376 					mutex_exit(&ipsq->ipsq_lock);
5377 					RELEASE_CONN_LOCK(q);
5378 					*error = EINPROGRESS;
5379 					return (NULL);
5380 				}
5381 			}
5382 		}
5383 		mutex_exit(&ill->ill_lock);
5384 		RELEASE_CONN_LOCK(q);
5385 	}
5386 	rw_exit(&ill_g_lock);
5387 
5388 	/* lookup the ipif based on interface address */
5389 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5390 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5391 	return (ipif);
5392 }
5393 
5394 /*
5395  * Look for an ipif with the specified address. For point-point links
5396  * we look for matches on either the destination address and the local
5397  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5398  * is set.
5399  * Matches on a specific ill if match_ill is set.
5400  */
5401 ipif_t *
5402 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5403     mblk_t *mp, ipsq_func_t func, int *error)
5404 {
5405 	ipif_t  *ipif;
5406 	ill_t   *ill;
5407 	boolean_t ptp = B_FALSE;
5408 	ipsq_t	*ipsq;
5409 	ill_walk_context_t	ctx;
5410 
5411 	if (error != NULL)
5412 		*error = 0;
5413 
5414 	rw_enter(&ill_g_lock, RW_READER);
5415 	/*
5416 	 * Repeat twice, first based on local addresses and
5417 	 * next time for pointopoint.
5418 	 */
5419 repeat:
5420 	ill = ILL_START_WALK_V4(&ctx);
5421 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5422 		if (match_ill != NULL && ill != match_ill) {
5423 			continue;
5424 		}
5425 		GRAB_CONN_LOCK(q);
5426 		mutex_enter(&ill->ill_lock);
5427 		for (ipif = ill->ill_ipif; ipif != NULL;
5428 		    ipif = ipif->ipif_next) {
5429 			if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5430 				continue;
5431 			/* Allow the ipif to be down */
5432 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5433 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5434 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5435 			    (ipif->ipif_pp_dst_addr == addr))) {
5436 				/*
5437 				 * The block comment at the start of ipif_down
5438 				 * explains the use of the macros used below
5439 				 */
5440 				if (IPIF_CAN_LOOKUP(ipif)) {
5441 					ipif_refhold_locked(ipif);
5442 					mutex_exit(&ill->ill_lock);
5443 					RELEASE_CONN_LOCK(q);
5444 					rw_exit(&ill_g_lock);
5445 					return (ipif);
5446 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5447 					ipsq = ill->ill_phyint->phyint_ipsq;
5448 					mutex_enter(&ipsq->ipsq_lock);
5449 					mutex_exit(&ill->ill_lock);
5450 					rw_exit(&ill_g_lock);
5451 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5452 						ill);
5453 					mutex_exit(&ipsq->ipsq_lock);
5454 					RELEASE_CONN_LOCK(q);
5455 					*error = EINPROGRESS;
5456 					return (NULL);
5457 				}
5458 			}
5459 		}
5460 		mutex_exit(&ill->ill_lock);
5461 		RELEASE_CONN_LOCK(q);
5462 	}
5463 
5464 	/* Now try the ptp case */
5465 	if (ptp) {
5466 		rw_exit(&ill_g_lock);
5467 		if (error != NULL)
5468 			*error = ENXIO;
5469 		return (NULL);
5470 	}
5471 	ptp = B_TRUE;
5472 	goto repeat;
5473 }
5474 
5475 /*
5476  * Look for an ipif that matches the specified remote address i.e. the
5477  * ipif that would receive the specified packet.
5478  * First look for directly connected interfaces and then do a recursive
5479  * IRE lookup and pick the first ipif corresponding to the source address in the
5480  * ire.
5481  * Returns: held ipif
5482  */
5483 ipif_t *
5484 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5485 {
5486 	ipif_t	*ipif;
5487 	ire_t	*ire;
5488 
5489 	ASSERT(!ill->ill_isv6);
5490 
5491 	/*
5492 	 * Someone could be changing this ipif currently or change it
5493 	 * after we return this. Thus  a few packets could use the old
5494 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5495 	 * will atomically be updated or cleaned up with the new value
5496 	 * Thus we don't need a lock to check the flags or other attrs below.
5497 	 */
5498 	mutex_enter(&ill->ill_lock);
5499 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5500 		if (!IPIF_CAN_LOOKUP(ipif))
5501 			continue;
5502 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5503 			continue;
5504 		/* Allow the ipif to be down */
5505 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5506 			if ((ipif->ipif_pp_dst_addr == addr) ||
5507 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5508 			    ipif->ipif_lcl_addr == addr)) {
5509 				ipif_refhold_locked(ipif);
5510 				mutex_exit(&ill->ill_lock);
5511 				return (ipif);
5512 			}
5513 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5514 			ipif_refhold_locked(ipif);
5515 			mutex_exit(&ill->ill_lock);
5516 			return (ipif);
5517 		}
5518 	}
5519 	mutex_exit(&ill->ill_lock);
5520 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5521 	    MATCH_IRE_RECURSIVE);
5522 	if (ire != NULL) {
5523 		/*
5524 		 * The callers of this function wants to know the
5525 		 * interface on which they have to send the replies
5526 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5527 		 * derived from different ills, we really don't care
5528 		 * what we return here.
5529 		 */
5530 		ipif = ire->ire_ipif;
5531 		if (ipif != NULL) {
5532 			ipif_refhold(ipif);
5533 			ire_refrele(ire);
5534 			return (ipif);
5535 		}
5536 		ire_refrele(ire);
5537 	}
5538 	/* Pick the first interface */
5539 	ipif = ipif_get_next_ipif(NULL, ill);
5540 	return (ipif);
5541 }
5542 
5543 /*
5544  * This func does not prevent refcnt from increasing. But if
5545  * the caller has taken steps to that effect, then this func
5546  * can be used to determine whether the ill has become quiescent
5547  */
5548 boolean_t
5549 ill_is_quiescent(ill_t *ill)
5550 {
5551 	ipif_t	*ipif;
5552 
5553 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5554 
5555 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5556 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5557 			return (B_FALSE);
5558 	}
5559 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5560 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5561 	    ill->ill_mrtun_refcnt != 0)
5562 		return (B_FALSE);
5563 	return (B_TRUE);
5564 }
5565 
5566 /*
5567  * This func does not prevent refcnt from increasing. But if
5568  * the caller has taken steps to that effect, then this func
5569  * can be used to determine whether the ipif has become quiescent
5570  */
5571 static boolean_t
5572 ipif_is_quiescent(ipif_t *ipif)
5573 {
5574 	ill_t *ill;
5575 
5576 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5577 
5578 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5579 		return (B_FALSE);
5580 
5581 	ill = ipif->ipif_ill;
5582 	if (ill->ill_ipif_up_count != 0 || ill->ill_logical_down)
5583 		return (B_TRUE);
5584 
5585 	/* This is the last ipif going down or being deleted on this ill */
5586 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0)
5587 		return (B_FALSE);
5588 
5589 	return (B_TRUE);
5590 }
5591 
5592 /*
5593  * This func does not prevent refcnt from increasing. But if
5594  * the caller has taken steps to that effect, then this func
5595  * can be used to determine whether the ipifs marked with IPIF_MOVING
5596  * have become quiescent and can be moved in a failover/failback.
5597  */
5598 static ipif_t *
5599 ill_quiescent_to_move(ill_t *ill)
5600 {
5601 	ipif_t  *ipif;
5602 
5603 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5604 
5605 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5606 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5607 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5608 				return (ipif);
5609 			}
5610 		}
5611 	}
5612 	return (NULL);
5613 }
5614 
5615 /*
5616  * The ipif/ill/ire has been refreled. Do the tail processing.
5617  * Determine if the ipif or ill in question has become quiescent and if so
5618  * wakeup close and/or restart any queued pending ioctl that is waiting
5619  * for the ipif_down (or ill_down)
5620  */
5621 void
5622 ipif_ill_refrele_tail(ill_t *ill)
5623 {
5624 	mblk_t	*mp;
5625 	conn_t	*connp;
5626 	ipsq_t	*ipsq;
5627 	ipif_t	*ipif;
5628 
5629 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5630 
5631 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5632 	    ill_is_quiescent(ill)) {
5633 		/* ill_close may be waiting */
5634 		cv_broadcast(&ill->ill_cv);
5635 	}
5636 
5637 	/* ipsq can't change because ill_lock  is held */
5638 	ipsq = ill->ill_phyint->phyint_ipsq;
5639 	if (ipsq->ipsq_waitfor == 0) {
5640 		/* Not waiting for anything, just return. */
5641 		mutex_exit(&ill->ill_lock);
5642 		return;
5643 	}
5644 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5645 		ipsq->ipsq_pending_ipif != NULL);
5646 	/*
5647 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5648 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5649 	 * be zero for restarting an ioctl that ends up downing the ill.
5650 	 */
5651 	ipif = ipsq->ipsq_pending_ipif;
5652 	if (ipif->ipif_ill != ill) {
5653 		/* The ioctl is pending on some other ill. */
5654 		mutex_exit(&ill->ill_lock);
5655 		return;
5656 	}
5657 
5658 	switch (ipsq->ipsq_waitfor) {
5659 	case IPIF_DOWN:
5660 	case IPIF_FREE:
5661 		if (!ipif_is_quiescent(ipif)) {
5662 			mutex_exit(&ill->ill_lock);
5663 			return;
5664 		}
5665 		break;
5666 
5667 	case ILL_DOWN:
5668 	case ILL_FREE:
5669 		/*
5670 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5671 		 * waits synchronously in ip_close, and no message is queued in
5672 		 * ipsq_pending_mp at all in this case
5673 		 */
5674 		if (!ill_is_quiescent(ill)) {
5675 			mutex_exit(&ill->ill_lock);
5676 			return;
5677 		}
5678 
5679 		break;
5680 
5681 	case ILL_MOVE_OK:
5682 		if (ill_quiescent_to_move(ill) != NULL) {
5683 			mutex_exit(&ill->ill_lock);
5684 			return;
5685 		}
5686 
5687 		break;
5688 	default:
5689 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5690 		    (void *)ipsq, ipsq->ipsq_waitfor);
5691 	}
5692 
5693 	/*
5694 	 * Incr refcnt for the qwriter_ip call below which
5695 	 * does a refrele
5696 	 */
5697 	ill_refhold_locked(ill);
5698 	mutex_exit(&ill->ill_lock);
5699 
5700 	mp = ipsq_pending_mp_get(ipsq, &connp);
5701 	ASSERT(mp != NULL);
5702 
5703 	switch (mp->b_datap->db_type) {
5704 	case M_ERROR:
5705 	case M_HANGUP:
5706 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5707 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5708 		return;
5709 
5710 	case M_IOCTL:
5711 	case M_IOCDATA:
5712 		(void) qwriter_ip(NULL, ill,
5713 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5714 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5715 		return;
5716 
5717 	default:
5718 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5719 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5720 	}
5721 }
5722 
5723 #ifdef ILL_DEBUG
5724 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5725 void
5726 th_trace_rrecord(th_trace_t *th_trace)
5727 {
5728 	tr_buf_t *tr_buf;
5729 	uint_t lastref;
5730 
5731 	lastref = th_trace->th_trace_lastref;
5732 	lastref++;
5733 	if (lastref == TR_BUF_MAX)
5734 		lastref = 0;
5735 	th_trace->th_trace_lastref = lastref;
5736 	tr_buf = &th_trace->th_trbuf[lastref];
5737 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5738 }
5739 
5740 th_trace_t *
5741 th_trace_ipif_lookup(ipif_t *ipif)
5742 {
5743 	int bucket_id;
5744 	th_trace_t *th_trace;
5745 
5746 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5747 
5748 	bucket_id = IP_TR_HASH(curthread);
5749 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5750 
5751 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5752 	    th_trace = th_trace->th_next) {
5753 		if (th_trace->th_id == curthread)
5754 			return (th_trace);
5755 	}
5756 	return (NULL);
5757 }
5758 
5759 void
5760 ipif_trace_ref(ipif_t *ipif)
5761 {
5762 	int bucket_id;
5763 	th_trace_t *th_trace;
5764 
5765 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5766 
5767 	if (ipif->ipif_trace_disable)
5768 		return;
5769 
5770 	/*
5771 	 * Attempt to locate the trace buffer for the curthread.
5772 	 * If it does not exist, then allocate a new trace buffer
5773 	 * and link it in list of trace bufs for this ipif, at the head
5774 	 */
5775 	th_trace = th_trace_ipif_lookup(ipif);
5776 	if (th_trace == NULL) {
5777 		bucket_id = IP_TR_HASH(curthread);
5778 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5779 		    KM_NOSLEEP);
5780 		if (th_trace == NULL) {
5781 			ipif->ipif_trace_disable = B_TRUE;
5782 			ipif_trace_cleanup(ipif);
5783 			return;
5784 		}
5785 		th_trace->th_id = curthread;
5786 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5787 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5788 		if (th_trace->th_next != NULL)
5789 			th_trace->th_next->th_prev = &th_trace->th_next;
5790 		ipif->ipif_trace[bucket_id] = th_trace;
5791 	}
5792 	ASSERT(th_trace->th_refcnt >= 0 &&
5793 		th_trace->th_refcnt < TR_BUF_MAX -1);
5794 	th_trace->th_refcnt++;
5795 	th_trace_rrecord(th_trace);
5796 }
5797 
5798 void
5799 ipif_untrace_ref(ipif_t *ipif)
5800 {
5801 	th_trace_t *th_trace;
5802 
5803 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5804 
5805 	if (ipif->ipif_trace_disable)
5806 		return;
5807 	th_trace = th_trace_ipif_lookup(ipif);
5808 	ASSERT(th_trace != NULL);
5809 	ASSERT(th_trace->th_refcnt > 0);
5810 
5811 	th_trace->th_refcnt--;
5812 	th_trace_rrecord(th_trace);
5813 }
5814 
5815 th_trace_t *
5816 th_trace_ill_lookup(ill_t *ill)
5817 {
5818 	th_trace_t *th_trace;
5819 	int bucket_id;
5820 
5821 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5822 
5823 	bucket_id = IP_TR_HASH(curthread);
5824 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5825 
5826 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
5827 	    th_trace = th_trace->th_next) {
5828 		if (th_trace->th_id == curthread)
5829 			return (th_trace);
5830 	}
5831 	return (NULL);
5832 }
5833 
5834 void
5835 ill_trace_ref(ill_t *ill)
5836 {
5837 	int bucket_id;
5838 	th_trace_t *th_trace;
5839 
5840 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5841 	if (ill->ill_trace_disable)
5842 		return;
5843 	/*
5844 	 * Attempt to locate the trace buffer for the curthread.
5845 	 * If it does not exist, then allocate a new trace buffer
5846 	 * and link it in list of trace bufs for this ill, at the head
5847 	 */
5848 	th_trace = th_trace_ill_lookup(ill);
5849 	if (th_trace == NULL) {
5850 		bucket_id = IP_TR_HASH(curthread);
5851 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5852 		    KM_NOSLEEP);
5853 		if (th_trace == NULL) {
5854 			ill->ill_trace_disable = B_TRUE;
5855 			ill_trace_cleanup(ill);
5856 			return;
5857 		}
5858 		th_trace->th_id = curthread;
5859 		th_trace->th_next = ill->ill_trace[bucket_id];
5860 		th_trace->th_prev = &ill->ill_trace[bucket_id];
5861 		if (th_trace->th_next != NULL)
5862 			th_trace->th_next->th_prev = &th_trace->th_next;
5863 		ill->ill_trace[bucket_id] = th_trace;
5864 	}
5865 	ASSERT(th_trace->th_refcnt >= 0 &&
5866 		th_trace->th_refcnt < TR_BUF_MAX - 1);
5867 
5868 	th_trace->th_refcnt++;
5869 	th_trace_rrecord(th_trace);
5870 }
5871 
5872 void
5873 ill_untrace_ref(ill_t *ill)
5874 {
5875 	th_trace_t *th_trace;
5876 
5877 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5878 
5879 	if (ill->ill_trace_disable)
5880 		return;
5881 	th_trace = th_trace_ill_lookup(ill);
5882 	ASSERT(th_trace != NULL);
5883 	ASSERT(th_trace->th_refcnt > 0);
5884 
5885 	th_trace->th_refcnt--;
5886 	th_trace_rrecord(th_trace);
5887 }
5888 
5889 /*
5890  * Verify that this thread has no refs to the ipif and free
5891  * the trace buffers
5892  */
5893 /* ARGSUSED */
5894 void
5895 ipif_thread_exit(ipif_t *ipif, void *dummy)
5896 {
5897 	th_trace_t *th_trace;
5898 
5899 	mutex_enter(&ipif->ipif_ill->ill_lock);
5900 
5901 	th_trace = th_trace_ipif_lookup(ipif);
5902 	if (th_trace == NULL) {
5903 		mutex_exit(&ipif->ipif_ill->ill_lock);
5904 		return;
5905 	}
5906 	ASSERT(th_trace->th_refcnt == 0);
5907 	/* unlink th_trace and free it */
5908 	*th_trace->th_prev = th_trace->th_next;
5909 	if (th_trace->th_next != NULL)
5910 		th_trace->th_next->th_prev = th_trace->th_prev;
5911 	th_trace->th_next = NULL;
5912 	th_trace->th_prev = NULL;
5913 	kmem_free(th_trace, sizeof (th_trace_t));
5914 
5915 	mutex_exit(&ipif->ipif_ill->ill_lock);
5916 }
5917 
5918 /*
5919  * Verify that this thread has no refs to the ill and free
5920  * the trace buffers
5921  */
5922 /* ARGSUSED */
5923 void
5924 ill_thread_exit(ill_t *ill, void *dummy)
5925 {
5926 	th_trace_t *th_trace;
5927 
5928 	mutex_enter(&ill->ill_lock);
5929 
5930 	th_trace = th_trace_ill_lookup(ill);
5931 	if (th_trace == NULL) {
5932 		mutex_exit(&ill->ill_lock);
5933 		return;
5934 	}
5935 	ASSERT(th_trace->th_refcnt == 0);
5936 	/* unlink th_trace and free it */
5937 	*th_trace->th_prev = th_trace->th_next;
5938 	if (th_trace->th_next != NULL)
5939 		th_trace->th_next->th_prev = th_trace->th_prev;
5940 	th_trace->th_next = NULL;
5941 	th_trace->th_prev = NULL;
5942 	kmem_free(th_trace, sizeof (th_trace_t));
5943 
5944 	mutex_exit(&ill->ill_lock);
5945 }
5946 #endif
5947 
5948 #ifdef ILL_DEBUG
5949 void
5950 ip_thread_exit(void)
5951 {
5952 	ill_t	*ill;
5953 	ipif_t	*ipif;
5954 	ill_walk_context_t	ctx;
5955 
5956 	rw_enter(&ill_g_lock, RW_READER);
5957 	ill = ILL_START_WALK_ALL(&ctx);
5958 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5959 		for (ipif = ill->ill_ipif; ipif != NULL;
5960 		    ipif = ipif->ipif_next) {
5961 			ipif_thread_exit(ipif, NULL);
5962 		}
5963 		ill_thread_exit(ill, NULL);
5964 	}
5965 	rw_exit(&ill_g_lock);
5966 
5967 	ire_walk(ire_thread_exit, NULL);
5968 	ndp_walk_impl(NULL, nce_thread_exit, NULL, B_FALSE);
5969 }
5970 
5971 /*
5972  * Called when ipif is unplumbed or when memory alloc fails
5973  */
5974 void
5975 ipif_trace_cleanup(ipif_t *ipif)
5976 {
5977 	int	i;
5978 	th_trace_t	*th_trace;
5979 	th_trace_t	*th_trace_next;
5980 
5981 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
5982 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
5983 		    th_trace = th_trace_next) {
5984 			th_trace_next = th_trace->th_next;
5985 			kmem_free(th_trace, sizeof (th_trace_t));
5986 		}
5987 		ipif->ipif_trace[i] = NULL;
5988 	}
5989 }
5990 
5991 /*
5992  * Called when ill is unplumbed or when memory alloc fails
5993  */
5994 void
5995 ill_trace_cleanup(ill_t *ill)
5996 {
5997 	int	i;
5998 	th_trace_t	*th_trace;
5999 	th_trace_t	*th_trace_next;
6000 
6001 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6002 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6003 		    th_trace = th_trace_next) {
6004 			th_trace_next = th_trace->th_next;
6005 			kmem_free(th_trace, sizeof (th_trace_t));
6006 		}
6007 		ill->ill_trace[i] = NULL;
6008 	}
6009 }
6010 
6011 #else
6012 void ip_thread_exit(void) {}
6013 #endif
6014 
6015 void
6016 ipif_refhold_locked(ipif_t *ipif)
6017 {
6018 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6019 	ipif->ipif_refcnt++;
6020 	IPIF_TRACE_REF(ipif);
6021 }
6022 
6023 void
6024 ipif_refhold(ipif_t *ipif)
6025 {
6026 	ill_t	*ill;
6027 
6028 	ill = ipif->ipif_ill;
6029 	mutex_enter(&ill->ill_lock);
6030 	ipif->ipif_refcnt++;
6031 	IPIF_TRACE_REF(ipif);
6032 	mutex_exit(&ill->ill_lock);
6033 }
6034 
6035 /*
6036  * Must not be called while holding any locks. Otherwise if this is
6037  * the last reference to be released there is a chance of recursive mutex
6038  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6039  * to restart an ioctl.
6040  */
6041 void
6042 ipif_refrele(ipif_t *ipif)
6043 {
6044 	ill_t	*ill;
6045 
6046 	ill = ipif->ipif_ill;
6047 
6048 	mutex_enter(&ill->ill_lock);
6049 	ASSERT(ipif->ipif_refcnt != 0);
6050 	ipif->ipif_refcnt--;
6051 	IPIF_UNTRACE_REF(ipif);
6052 	if (ipif->ipif_refcnt != 0) {
6053 		mutex_exit(&ill->ill_lock);
6054 		return;
6055 	}
6056 
6057 	/* Drops the ill_lock */
6058 	ipif_ill_refrele_tail(ill);
6059 }
6060 
6061 ipif_t *
6062 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6063 {
6064 	ipif_t	*ipif;
6065 
6066 	mutex_enter(&ill->ill_lock);
6067 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6068 	    ipif != NULL; ipif = ipif->ipif_next) {
6069 		if (!IPIF_CAN_LOOKUP(ipif))
6070 			continue;
6071 		ipif_refhold_locked(ipif);
6072 		mutex_exit(&ill->ill_lock);
6073 		return (ipif);
6074 	}
6075 	mutex_exit(&ill->ill_lock);
6076 	return (NULL);
6077 }
6078 
6079 /*
6080  * TODO: make this table extendible at run time
6081  * Return a pointer to the mac type info for 'mac_type'
6082  */
6083 static ip_m_t *
6084 ip_m_lookup(t_uscalar_t mac_type)
6085 {
6086 	ip_m_t	*ipm;
6087 
6088 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6089 		if (ipm->ip_m_mac_type == mac_type)
6090 			return (ipm);
6091 	return (NULL);
6092 }
6093 
6094 /*
6095  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6096  * ipif_arg is passed in to associate it with the correct interface.
6097  * We may need to restart this operation if the ipif cannot be looked up
6098  * due to an exclusive operation that is currently in progress. The restart
6099  * entry point is specified by 'func'
6100  */
6101 int
6102 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6103     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6104     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6105     ipsq_func_t func)
6106 {
6107 	ire_t	*ire;
6108 	ire_t	*gw_ire = NULL;
6109 	ipif_t	*ipif = NULL;
6110 	boolean_t ipif_refheld = B_FALSE;
6111 	uint_t	type;
6112 	int	match_flags = MATCH_IRE_TYPE;
6113 	int	error;
6114 
6115 	ip1dbg(("ip_rt_add:"));
6116 
6117 	if (ire_arg != NULL)
6118 		*ire_arg = NULL;
6119 
6120 	/*
6121 	 * If this is the case of RTF_HOST being set, then we set the netmask
6122 	 * to all ones (regardless if one was supplied).
6123 	 */
6124 	if (flags & RTF_HOST)
6125 		mask = IP_HOST_MASK;
6126 
6127 	/*
6128 	 * Prevent routes with a zero gateway from being created (since
6129 	 * interfaces can currently be plumbed and brought up no assigned
6130 	 * address).
6131 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6132 	 */
6133 	if (gw_addr == 0 && src_ipif == NULL)
6134 		return (ENETUNREACH);
6135 	/*
6136 	 * Get the ipif, if any, corresponding to the gw_addr
6137 	 */
6138 	if (gw_addr != 0) {
6139 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6140 		    &error);
6141 		if (ipif != NULL) {
6142 			if (IS_VNI(ipif->ipif_ill)) {
6143 				ipif_refrele(ipif);
6144 				return (EINVAL);
6145 			}
6146 			ipif_refheld = B_TRUE;
6147 		} else if (error == EINPROGRESS) {
6148 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6149 			return (EINPROGRESS);
6150 		} else {
6151 			error = 0;
6152 		}
6153 	}
6154 
6155 	if (ipif != NULL) {
6156 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6157 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6158 	} else {
6159 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6160 	}
6161 
6162 	/*
6163 	 * GateD will attempt to create routes with a loopback interface
6164 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6165 	 * these routes to be added, but create them as interface routes
6166 	 * since the gateway is an interface address.
6167 	 */
6168 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK))
6169 		flags &= ~RTF_GATEWAY;
6170 
6171 	/*
6172 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6173 	 * and the gateway address provided is one of the system's interface
6174 	 * addresses.  By using the routing socket interface and supplying an
6175 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6176 	 * specifying an interface route to be created is available which uses
6177 	 * the interface index that specifies the outgoing interface rather than
6178 	 * the address of an outgoing interface (which may not be able to
6179 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6180 	 * flag, routes can be specified which not only specify the next-hop to
6181 	 * be used when routing to a certain prefix, but also which outgoing
6182 	 * interface should be used.
6183 	 *
6184 	 * Previously, interfaces would have unique addresses assigned to them
6185 	 * and so the address assigned to a particular interface could be used
6186 	 * to identify a particular interface.  One exception to this was the
6187 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6188 	 *
6189 	 * With the advent of IPv6 and its link-local addresses, this
6190 	 * restriction was relaxed and interfaces could share addresses between
6191 	 * themselves.  In fact, typically all of the link-local interfaces on
6192 	 * an IPv6 node or router will have the same link-local address.  In
6193 	 * order to differentiate between these interfaces, the use of an
6194 	 * interface index is necessary and this index can be carried inside a
6195 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6196 	 * of using the interface index, however, is that all of the ipif's that
6197 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6198 	 * cannot be used to differentiate between ipif's (or logical
6199 	 * interfaces) that belong to the same ill (physical interface).
6200 	 *
6201 	 * For example, in the following case involving IPv4 interfaces and
6202 	 * logical interfaces
6203 	 *
6204 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6205 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6206 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6207 	 *
6208 	 * the ipif's corresponding to each of these interface routes can be
6209 	 * uniquely identified by the "gateway" (actually interface address).
6210 	 *
6211 	 * In this case involving multiple IPv6 default routes to a particular
6212 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6213 	 * default route is of interest:
6214 	 *
6215 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6216 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6217 	 */
6218 
6219 	/* RTF_GATEWAY not set */
6220 	if (!(flags & RTF_GATEWAY)) {
6221 		queue_t	*stq;
6222 		queue_t	*rfq = NULL;
6223 		ill_t	*in_ill = NULL;
6224 
6225 		/*
6226 		 * As the interface index specified with the RTA_IFP sockaddr is
6227 		 * the same for all ipif's off of an ill, the matching logic
6228 		 * below uses MATCH_IRE_ILL if such an index was specified.
6229 		 * This means that routes sharing the same prefix when added
6230 		 * using a RTA_IFP sockaddr must have distinct interface
6231 		 * indices (namely, they must be on distinct ill's).
6232 		 *
6233 		 * On the other hand, since the gateway address will usually be
6234 		 * different for each ipif on the system, the matching logic
6235 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6236 		 * route.  This means that interface routes for the same prefix
6237 		 * can be created if they belong to distinct ipif's and if a
6238 		 * RTA_IFP sockaddr is not present.
6239 		 */
6240 		if (ipif_arg != NULL) {
6241 			if (ipif_refheld)  {
6242 				ipif_refrele(ipif);
6243 				ipif_refheld = B_FALSE;
6244 			}
6245 			ipif = ipif_arg;
6246 			match_flags |= MATCH_IRE_ILL;
6247 		} else {
6248 			/*
6249 			 * Check the ipif corresponding to the gw_addr
6250 			 */
6251 			if (ipif == NULL)
6252 				return (ENETUNREACH);
6253 			match_flags |= MATCH_IRE_IPIF;
6254 		}
6255 		ASSERT(ipif != NULL);
6256 		/*
6257 		 * If src_ipif is not NULL, we have to create
6258 		 * an ire with non-null ire_in_ill value
6259 		 */
6260 		if (src_ipif != NULL) {
6261 			in_ill = src_ipif->ipif_ill;
6262 		}
6263 
6264 		/*
6265 		 * We check for an existing entry at this point.
6266 		 *
6267 		 * Since a netmask isn't passed in via the ioctl interface
6268 		 * (SIOCADDRT), we don't check for a matching netmask in that
6269 		 * case.
6270 		 */
6271 		if (!ioctl_msg)
6272 			match_flags |= MATCH_IRE_MASK;
6273 		if (src_ipif != NULL) {
6274 			/* Look up in the special table */
6275 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6276 			    ipif, src_ipif->ipif_ill, match_flags);
6277 		} else {
6278 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6279 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6280 			    match_flags);
6281 		}
6282 		if (ire != NULL) {
6283 			ire_refrele(ire);
6284 			if (ipif_refheld)
6285 				ipif_refrele(ipif);
6286 			return (EEXIST);
6287 		}
6288 
6289 		if (src_ipif != NULL) {
6290 			/*
6291 			 * Create the special ire for the IRE table
6292 			 * which hangs out of ire_in_ill. This ire
6293 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6294 			 * Thus rfq is non-NULL.
6295 			 */
6296 			rfq = ipif->ipif_rq;
6297 		}
6298 		/* Create the usual interface ires */
6299 
6300 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6301 		    ? ipif->ipif_rq : ipif->ipif_wq;
6302 
6303 		/*
6304 		 * Create a copy of the IRE_LOOPBACK,
6305 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6306 		 * the modified address and netmask.
6307 		 */
6308 		ire = ire_create(
6309 		    (uchar_t *)&dst_addr,
6310 		    (uint8_t *)&mask,
6311 		    (uint8_t *)&ipif->ipif_src_addr,
6312 		    NULL,
6313 		    NULL,
6314 		    &ipif->ipif_mtu,
6315 		    NULL,
6316 		    rfq,
6317 		    stq,
6318 		    ipif->ipif_net_type,
6319 		    ipif->ipif_resolver_mp,
6320 		    ipif,
6321 		    in_ill,
6322 		    0,
6323 		    0,
6324 		    0,
6325 		    flags,
6326 		    &ire_uinfo_null);
6327 		if (ire == NULL) {
6328 			if (ipif_refheld)
6329 				ipif_refrele(ipif);
6330 			return (ENOMEM);
6331 		}
6332 
6333 		/*
6334 		 * Some software (for example, GateD and Sun Cluster) attempts
6335 		 * to create (what amount to) IRE_PREFIX routes with the
6336 		 * loopback address as the gateway.  This is primarily done to
6337 		 * set up prefixes with the RTF_REJECT flag set (for example,
6338 		 * when generating aggregate routes.)
6339 		 *
6340 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6341 		 * IRE_LOOPBACK, then we map the request into a
6342 		 * IRE_IF_NORESOLVER.
6343 		 *
6344 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6345 		 * routine, but rather using ire_create() directly.
6346 		 */
6347 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6348 			ire->ire_type = IRE_IF_NORESOLVER;
6349 		error = ire_add(&ire, q, mp, func);
6350 		if (error == 0)
6351 			goto save_ire;
6352 
6353 		/*
6354 		 * In the result of failure, ire_add() will have already
6355 		 * deleted the ire in question, so there is no need to
6356 		 * do that here.
6357 		 */
6358 		if (ipif_refheld)
6359 			ipif_refrele(ipif);
6360 		return (error);
6361 	}
6362 	if (ipif_refheld) {
6363 		ipif_refrele(ipif);
6364 		ipif_refheld = B_FALSE;
6365 	}
6366 
6367 	if (src_ipif != NULL) {
6368 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6369 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6370 		return (EINVAL);
6371 	}
6372 	/*
6373 	 * Get an interface IRE for the specified gateway.
6374 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6375 	 * gateway, it is currently unreachable and we fail the request
6376 	 * accordingly.
6377 	 */
6378 	ipif = ipif_arg;
6379 	if (ipif_arg != NULL)
6380 		match_flags |= MATCH_IRE_ILL;
6381 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6382 	    ALL_ZONES, 0, match_flags);
6383 	if (gw_ire == NULL)
6384 		return (ENETUNREACH);
6385 
6386 	/*
6387 	 * We create one of three types of IREs as a result of this request
6388 	 * based on the netmask.  A netmask of all ones (which is automatically
6389 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6390 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6391 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6392 	 * destination prefix.
6393 	 */
6394 	if (mask == IP_HOST_MASK)
6395 		type = IRE_HOST;
6396 	else if (mask == 0)
6397 		type = IRE_DEFAULT;
6398 	else
6399 		type = IRE_PREFIX;
6400 
6401 	/* check for a duplicate entry */
6402 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6403 	    NULL, ALL_ZONES, 0, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6404 	if (ire != NULL) {
6405 		ire_refrele(gw_ire);
6406 		ire_refrele(ire);
6407 		return (EEXIST);
6408 	}
6409 
6410 	/* Create the IRE. */
6411 	ire = ire_create(
6412 	    (uchar_t *)&dst_addr,		/* dest address */
6413 	    (uchar_t *)&mask,			/* mask */
6414 	    /* src address assigned by the caller? */
6415 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6416 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6417 	    (uchar_t *)&gw_addr,		/* gateway address */
6418 	    NULL,				/* no in-srcaddress */
6419 	    &gw_ire->ire_max_frag,
6420 	    NULL,				/* no Fast Path header */
6421 	    NULL,				/* no recv-from queue */
6422 	    NULL,				/* no send-to queue */
6423 	    (ushort_t)type,			/* IRE type */
6424 	    NULL,
6425 	    ipif_arg,
6426 	    NULL,
6427 	    0,
6428 	    0,
6429 	    0,
6430 	    flags,
6431 	    &gw_ire->ire_uinfo);		/* Inherit ULP info from gw */
6432 	if (ire == NULL) {
6433 		ire_refrele(gw_ire);
6434 		return (ENOMEM);
6435 	}
6436 
6437 	/*
6438 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6439 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6440 	 */
6441 
6442 	/* Add the new IRE. */
6443 	error = ire_add(&ire, q, mp, func);
6444 	if (error != 0) {
6445 		/*
6446 		 * In the result of failure, ire_add() will have already
6447 		 * deleted the ire in question, so there is no need to
6448 		 * do that here.
6449 		 */
6450 		ire_refrele(gw_ire);
6451 		return (error);
6452 	}
6453 
6454 	if (flags & RTF_MULTIRT) {
6455 		/*
6456 		 * Invoke the CGTP (multirouting) filtering module
6457 		 * to add the dst address in the filtering database.
6458 		 * Replicated inbound packets coming from that address
6459 		 * will be filtered to discard the duplicates.
6460 		 * It is not necessary to call the CGTP filter hook
6461 		 * when the dst address is a broadcast or multicast,
6462 		 * because an IP source address cannot be a broadcast
6463 		 * or a multicast.
6464 		 */
6465 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6466 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
6467 		if (ire_dst != NULL) {
6468 			ip_cgtp_bcast_add(ire, ire_dst);
6469 			ire_refrele(ire_dst);
6470 			goto save_ire;
6471 		}
6472 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6473 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6474 			    ire->ire_addr,
6475 			    ire->ire_gateway_addr,
6476 			    ire->ire_src_addr,
6477 			    gw_ire->ire_src_addr);
6478 			if (res != 0) {
6479 				ire_refrele(gw_ire);
6480 				ire_delete(ire);
6481 				return (res);
6482 			}
6483 		}
6484 	}
6485 
6486 save_ire:
6487 	if (gw_ire != NULL) {
6488 		ire_refrele(gw_ire);
6489 	}
6490 	/*
6491 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6492 	 * flag. This route is only added and deleted by mipagent.
6493 	 * So, for simplicity of design, we refrain from saving
6494 	 * ires that are created with srcif value. This may change
6495 	 * in future if we find more usage of srcifp feature.
6496 	 */
6497 	if (ipif != NULL && src_ipif == NULL) {
6498 		/*
6499 		 * Save enough information so that we can recreate the IRE if
6500 		 * the interface goes down and then up.  The metrics associated
6501 		 * with the route will be saved as well when rts_setmetrics() is
6502 		 * called after the IRE has been created.  In the case where
6503 		 * memory cannot be allocated, none of this information will be
6504 		 * saved.
6505 		 */
6506 		ipif_save_ire(ipif, ire);
6507 	}
6508 	if (ioctl_msg)
6509 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6510 	if (ire_arg != NULL) {
6511 		/*
6512 		 * Store the ire that was successfully added into where ire_arg
6513 		 * points to so that callers don't have to look it up
6514 		 * themselves (but they are responsible for ire_refrele()ing
6515 		 * the ire when they are finished with it).
6516 		 */
6517 		*ire_arg = ire;
6518 	} else {
6519 		ire_refrele(ire);		/* Held in ire_add */
6520 	}
6521 	if (ipif_refheld)
6522 		ipif_refrele(ipif);
6523 	return (0);
6524 }
6525 
6526 /*
6527  * ip_rt_delete is called to delete an IPv4 route.
6528  * ipif_arg is passed in to associate it with the correct interface.
6529  * src_ipif is passed to associate the incoming interface of the packet.
6530  * We may need to restart this operation if the ipif cannot be looked up
6531  * due to an exclusive operation that is currently in progress. The restart
6532  * entry point is specified by 'func'
6533  */
6534 /* ARGSUSED4 */
6535 int
6536 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6537     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6538     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6539 {
6540 	ire_t	*ire = NULL;
6541 	ipif_t	*ipif;
6542 	boolean_t ipif_refheld = B_FALSE;
6543 	uint_t	type;
6544 	uint_t	match_flags = MATCH_IRE_TYPE;
6545 	int	err = 0;
6546 
6547 	ip1dbg(("ip_rt_delete:"));
6548 	/*
6549 	 * If this is the case of RTF_HOST being set, then we set the netmask
6550 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6551 	 */
6552 	if (flags & RTF_HOST) {
6553 		mask = IP_HOST_MASK;
6554 		match_flags |= MATCH_IRE_MASK;
6555 	} else if (rtm_addrs & RTA_NETMASK) {
6556 		match_flags |= MATCH_IRE_MASK;
6557 	}
6558 
6559 	/*
6560 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6561 	 * we check if the gateway address is one of our interfaces first,
6562 	 * and fall back on RTF_GATEWAY routes.
6563 	 *
6564 	 * This makes it possible to delete an original
6565 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6566 	 *
6567 	 * As the interface index specified with the RTA_IFP sockaddr is the
6568 	 * same for all ipif's off of an ill, the matching logic below uses
6569 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6570 	 * sharing the same prefix and interface index as the the route
6571 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6572 	 * is specified in the request.
6573 	 *
6574 	 * On the other hand, since the gateway address will usually be
6575 	 * different for each ipif on the system, the matching logic
6576 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6577 	 * route.  This means that interface routes for the same prefix can be
6578 	 * uniquely identified if they belong to distinct ipif's and if a
6579 	 * RTA_IFP sockaddr is not present.
6580 	 *
6581 	 * For more detail on specifying routes by gateway address and by
6582 	 * interface index, see the comments in ip_rt_add().
6583 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6584 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6585 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6586 	 * succeed.
6587 	 */
6588 	if (src_ipif != NULL) {
6589 		if (ipif_arg == NULL && gw_addr != 0) {
6590 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6591 			    q, mp, func, &err);
6592 			if (ipif_arg != NULL)
6593 				ipif_refheld = B_TRUE;
6594 		}
6595 		if (ipif_arg == NULL) {
6596 			err = (err == EINPROGRESS) ? err : ESRCH;
6597 			return (err);
6598 		}
6599 		ipif = ipif_arg;
6600 	} else {
6601 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6602 			    q, mp, func, &err);
6603 		if (ipif != NULL)
6604 			ipif_refheld = B_TRUE;
6605 		else if (err == EINPROGRESS)
6606 			return (err);
6607 		else
6608 			err = 0;
6609 	}
6610 	if (ipif != NULL) {
6611 		if (ipif_arg != NULL) {
6612 			if (ipif_refheld) {
6613 				ipif_refrele(ipif);
6614 				ipif_refheld = B_FALSE;
6615 			}
6616 			ipif = ipif_arg;
6617 			match_flags |= MATCH_IRE_ILL;
6618 		} else {
6619 			match_flags |= MATCH_IRE_IPIF;
6620 		}
6621 		if (src_ipif != NULL) {
6622 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6623 			    ipif, src_ipif->ipif_ill, match_flags);
6624 		} else {
6625 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6626 				ire = ire_ctable_lookup(dst_addr, 0,
6627 				    IRE_LOOPBACK, ipif, ALL_ZONES, match_flags);
6628 			}
6629 			if (ire == NULL) {
6630 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6631 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6632 				    match_flags);
6633 			}
6634 		}
6635 	}
6636 
6637 	if (ire == NULL) {
6638 		/*
6639 		 * At this point, the gateway address is not one of our own
6640 		 * addresses or a matching interface route was not found.  We
6641 		 * set the IRE type to lookup based on whether
6642 		 * this is a host route, a default route or just a prefix.
6643 		 *
6644 		 * If an ipif_arg was passed in, then the lookup is based on an
6645 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6646 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6647 		 * set as the route being looked up is not a traditional
6648 		 * interface route.
6649 		 * Since we do not add gateway route with srcipif, we don't
6650 		 * expect to find it either.
6651 		 */
6652 		if (src_ipif != NULL) {
6653 			if (ipif_refheld)
6654 				ipif_refrele(ipif);
6655 			return (ESRCH);
6656 		} else {
6657 			match_flags &= ~MATCH_IRE_IPIF;
6658 			match_flags |= MATCH_IRE_GW;
6659 			if (ipif_arg != NULL)
6660 				match_flags |= MATCH_IRE_ILL;
6661 			if (mask == IP_HOST_MASK)
6662 				type = IRE_HOST;
6663 			else if (mask == 0)
6664 				type = IRE_DEFAULT;
6665 			else
6666 				type = IRE_PREFIX;
6667 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6668 			    ipif_arg, NULL, ALL_ZONES, 0, match_flags);
6669 			if (ire == NULL && type == IRE_HOST) {
6670 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6671 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6672 				    ALL_ZONES, 0, match_flags);
6673 			}
6674 		}
6675 	}
6676 
6677 	if (ipif_refheld)
6678 		ipif_refrele(ipif);
6679 
6680 	/* ipif is not refheld anymore */
6681 	if (ire == NULL)
6682 		return (ESRCH);
6683 
6684 	if (ire->ire_flags & RTF_MULTIRT) {
6685 		/*
6686 		 * Invoke the CGTP (multirouting) filtering module
6687 		 * to remove the dst address from the filtering database.
6688 		 * Packets coming from that address will no longer be
6689 		 * filtered to remove duplicates.
6690 		 */
6691 		if (ip_cgtp_filter_ops != NULL) {
6692 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6693 			    ire->ire_gateway_addr);
6694 		}
6695 		ip_cgtp_bcast_delete(ire);
6696 	}
6697 
6698 	ipif = ire->ire_ipif;
6699 	/*
6700 	 * Removing from ipif_saved_ire_mp is not necessary
6701 	 * when src_ipif being non-NULL. ip_rt_add does not
6702 	 * save the ires which src_ipif being non-NULL.
6703 	 */
6704 	if (ipif != NULL && src_ipif == NULL) {
6705 		ipif_remove_ire(ipif, ire);
6706 	}
6707 	if (ioctl_msg)
6708 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6709 	ire_delete(ire);
6710 	ire_refrele(ire);
6711 	return (err);
6712 }
6713 
6714 /*
6715  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6716  */
6717 /* ARGSUSED */
6718 int
6719 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6720     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6721 {
6722 	ipaddr_t dst_addr;
6723 	ipaddr_t gw_addr;
6724 	ipaddr_t mask;
6725 	int error = 0;
6726 	mblk_t *mp1;
6727 	struct rtentry *rt;
6728 	ipif_t *ipif = NULL;
6729 
6730 	ip1dbg(("ip_siocaddrt:"));
6731 	/* Existence of mp1 verified in ip_wput_nondata */
6732 	mp1 = mp->b_cont->b_cont;
6733 	rt = (struct rtentry *)mp1->b_rptr;
6734 
6735 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6736 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6737 
6738 	/*
6739 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6740 	 * to a particular host address.  In this case, we set the netmask to
6741 	 * all ones for the particular destination address.  Otherwise,
6742 	 * determine the netmask to be used based on dst_addr and the interfaces
6743 	 * in use.
6744 	 */
6745 	if (rt->rt_flags & RTF_HOST) {
6746 		mask = IP_HOST_MASK;
6747 	} else {
6748 		/*
6749 		 * Note that ip_subnet_mask returns a zero mask in the case of
6750 		 * default (an all-zeroes address).
6751 		 */
6752 		mask = ip_subnet_mask(dst_addr, &ipif);
6753 	}
6754 
6755 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags,
6756 	    NULL, NULL, NULL, B_TRUE, q, mp, ip_process_ioctl);
6757 	if (ipif != NULL)
6758 		ipif_refrele(ipif);
6759 	return (error);
6760 }
6761 
6762 /*
6763  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6764  */
6765 /* ARGSUSED */
6766 int
6767 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6768     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6769 {
6770 	ipaddr_t dst_addr;
6771 	ipaddr_t gw_addr;
6772 	ipaddr_t mask;
6773 	int error;
6774 	mblk_t *mp1;
6775 	struct rtentry *rt;
6776 	ipif_t *ipif = NULL;
6777 
6778 	ip1dbg(("ip_siocdelrt:"));
6779 	/* Existence of mp1 verified in ip_wput_nondata */
6780 	mp1 = mp->b_cont->b_cont;
6781 	rt = (struct rtentry *)mp1->b_rptr;
6782 
6783 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6784 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6785 
6786 	/*
6787 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6788 	 * to a particular host address.  In this case, we set the netmask to
6789 	 * all ones for the particular destination address.  Otherwise,
6790 	 * determine the netmask to be used based on dst_addr and the interfaces
6791 	 * in use.
6792 	 */
6793 	if (rt->rt_flags & RTF_HOST) {
6794 		mask = IP_HOST_MASK;
6795 	} else {
6796 		/*
6797 		 * Note that ip_subnet_mask returns a zero mask in the case of
6798 		 * default (an all-zeroes address).
6799 		 */
6800 		mask = ip_subnet_mask(dst_addr, &ipif);
6801 	}
6802 
6803 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6804 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6805 	    B_TRUE, q, mp, ip_process_ioctl);
6806 	if (ipif != NULL)
6807 		ipif_refrele(ipif);
6808 	return (error);
6809 }
6810 
6811 /*
6812  * Enqueue the mp onto the ipsq, chained by b_next.
6813  * b_prev stores the function to be executed later, and b_queue the queue
6814  * where this mp originated.
6815  */
6816 void
6817 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6818     ill_t *pending_ill)
6819 {
6820 	conn_t	*connp = NULL;
6821 
6822 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6823 	ASSERT(func != NULL);
6824 
6825 	mp->b_queue = q;
6826 	mp->b_prev = (void *)func;
6827 	mp->b_next = NULL;
6828 
6829 	switch (type) {
6830 	case CUR_OP:
6831 		if (ipsq->ipsq_mptail != NULL) {
6832 			ASSERT(ipsq->ipsq_mphead != NULL);
6833 			ipsq->ipsq_mptail->b_next = mp;
6834 		} else {
6835 			ASSERT(ipsq->ipsq_mphead == NULL);
6836 			ipsq->ipsq_mphead = mp;
6837 		}
6838 		ipsq->ipsq_mptail = mp;
6839 		break;
6840 
6841 	case NEW_OP:
6842 		if (ipsq->ipsq_xopq_mptail != NULL) {
6843 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6844 			ipsq->ipsq_xopq_mptail->b_next = mp;
6845 		} else {
6846 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6847 			ipsq->ipsq_xopq_mphead = mp;
6848 		}
6849 		ipsq->ipsq_xopq_mptail = mp;
6850 		break;
6851 	default:
6852 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6853 	}
6854 
6855 	if (CONN_Q(q) && pending_ill != NULL) {
6856 		connp = Q_TO_CONN(q);
6857 
6858 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6859 		connp->conn_oper_pending_ill = pending_ill;
6860 	}
6861 }
6862 
6863 /*
6864  * Return the mp at the head of the ipsq. After emptying the ipsq
6865  * look at the next ioctl, if this ioctl is complete. Otherwise
6866  * return, we will resume when we complete the current ioctl.
6867  * The current ioctl will wait till it gets a response from the
6868  * driver below.
6869  */
6870 static mblk_t *
6871 ipsq_dq(ipsq_t *ipsq)
6872 {
6873 	mblk_t	*mp;
6874 
6875 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6876 
6877 	mp = ipsq->ipsq_mphead;
6878 	if (mp != NULL) {
6879 		ipsq->ipsq_mphead = mp->b_next;
6880 		if (ipsq->ipsq_mphead == NULL)
6881 			ipsq->ipsq_mptail = NULL;
6882 		mp->b_next = NULL;
6883 		return (mp);
6884 	}
6885 	if (ipsq->ipsq_current_ipif != NULL)
6886 		return (NULL);
6887 	mp = ipsq->ipsq_xopq_mphead;
6888 	if (mp != NULL) {
6889 		ipsq->ipsq_xopq_mphead = mp->b_next;
6890 		if (ipsq->ipsq_xopq_mphead == NULL)
6891 			ipsq->ipsq_xopq_mptail = NULL;
6892 		mp->b_next = NULL;
6893 		return (mp);
6894 	}
6895 	return (NULL);
6896 }
6897 
6898 /*
6899  * Enter the ipsq corresponding to ill, by waiting synchronously till
6900  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6901  * will have to drain completely before ipsq_enter returns success.
6902  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
6903  * and the ipsq_exit logic will start the next enqueued ioctl after
6904  * completion of the current ioctl. If 'force' is used, we don't wait
6905  * for the enqueued ioctls. This is needed when a conn_close wants to
6906  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6907  * of an ill can also use this option. But we dont' use it currently.
6908  */
6909 #define	ENTER_SQ_WAIT_TICKS 100
6910 boolean_t
6911 ipsq_enter(ill_t *ill, boolean_t force)
6912 {
6913 	ipsq_t	*ipsq;
6914 	boolean_t waited_enough = B_FALSE;
6915 
6916 	/*
6917 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
6918 	 * Since the <ill-ipsq> assocs could change while we wait for the
6919 	 * writer, it is easier to wait on a fixed global rather than try to
6920 	 * cv_wait on a changing ipsq.
6921 	 */
6922 	mutex_enter(&ill->ill_lock);
6923 	for (;;) {
6924 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6925 			mutex_exit(&ill->ill_lock);
6926 			return (B_FALSE);
6927 		}
6928 
6929 		ipsq = ill->ill_phyint->phyint_ipsq;
6930 		mutex_enter(&ipsq->ipsq_lock);
6931 		if (ipsq->ipsq_writer == NULL &&
6932 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
6933 			break;
6934 		} else if (ipsq->ipsq_writer != NULL) {
6935 			mutex_exit(&ipsq->ipsq_lock);
6936 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6937 		} else {
6938 			mutex_exit(&ipsq->ipsq_lock);
6939 			if (force) {
6940 				(void) cv_timedwait(&ill->ill_cv,
6941 				    &ill->ill_lock,
6942 				    lbolt + ENTER_SQ_WAIT_TICKS);
6943 				waited_enough = B_TRUE;
6944 				continue;
6945 			} else {
6946 				cv_wait(&ill->ill_cv, &ill->ill_lock);
6947 			}
6948 		}
6949 	}
6950 
6951 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
6952 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
6953 	ipsq->ipsq_writer = curthread;
6954 	ipsq->ipsq_reentry_cnt++;
6955 #ifdef ILL_DEBUG
6956 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
6957 #endif
6958 	mutex_exit(&ipsq->ipsq_lock);
6959 	mutex_exit(&ill->ill_lock);
6960 	return (B_TRUE);
6961 }
6962 
6963 /*
6964  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6965  * certain critical operations like plumbing (i.e. most set ioctls),
6966  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
6967  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
6968  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
6969  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
6970  * threads executing in the ipsq. Responses from the driver pertain to the
6971  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
6972  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
6973  *
6974  * If a thread does not want to reenter the ipsq when it is already writer,
6975  * it must make sure that the specified reentry point to be called later
6976  * when the ipsq is empty, nor any code path starting from the specified reentry
6977  * point must never ever try to enter the ipsq again. Otherwise it can lead
6978  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6979  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6980  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
6981  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
6982  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6983  * ioctl if the current ioctl has completed. If the current ioctl is still
6984  * in progress it simply returns. The current ioctl could be waiting for
6985  * a response from another module (arp_ or the driver or could be waiting for
6986  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
6987  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
6988  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6989  * ipsq_current_ipif is clear which happens only on ioctl completion.
6990  */
6991 
6992 /*
6993  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
6994  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
6995  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
6996  * completion.
6997  */
6998 ipsq_t *
6999 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7000     ipsq_func_t func, int type, boolean_t reentry_ok)
7001 {
7002 	ipsq_t	*ipsq;
7003 
7004 	/* Only 1 of ipif or ill can be specified */
7005 	ASSERT((ipif != NULL) ^ (ill != NULL));
7006 	if (ipif != NULL)
7007 		ill = ipif->ipif_ill;
7008 
7009 	/*
7010 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7011 	 * ipsq of an ill can't change when ill_lock is held.
7012 	 */
7013 	GRAB_CONN_LOCK(q);
7014 	mutex_enter(&ill->ill_lock);
7015 	ipsq = ill->ill_phyint->phyint_ipsq;
7016 	mutex_enter(&ipsq->ipsq_lock);
7017 
7018 	/*
7019 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7020 	 *    (Note: If the caller does not specify reentry_ok then neither
7021 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7022 	 *    again. Otherwise it can lead to an infinite loop
7023 	 * 2. Enter the ipsq if there is no current writer and this attempted
7024 	 *    entry is part of the current ioctl or operation
7025 	 * 3. Enter the ipsq if there is no current writer and this is a new
7026 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7027 	 *    empty and there is no ioctl (or operation) currently in progress
7028 	 */
7029 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7030 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7031 	    ipsq->ipsq_current_ipif == NULL))) ||
7032 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7033 		/* Success. */
7034 		ipsq->ipsq_reentry_cnt++;
7035 		ipsq->ipsq_writer = curthread;
7036 		mutex_exit(&ipsq->ipsq_lock);
7037 		mutex_exit(&ill->ill_lock);
7038 		RELEASE_CONN_LOCK(q);
7039 #ifdef ILL_DEBUG
7040 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7041 #endif
7042 		return (ipsq);
7043 	}
7044 
7045 	ipsq_enq(ipsq, q, mp, func, type, ill);
7046 
7047 	mutex_exit(&ipsq->ipsq_lock);
7048 	mutex_exit(&ill->ill_lock);
7049 	RELEASE_CONN_LOCK(q);
7050 	return (NULL);
7051 }
7052 
7053 /*
7054  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7055  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7056  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7057  * completion.
7058  *
7059  * This function does a refrele on the ipif/ill.
7060  */
7061 void
7062 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7063     ipsq_func_t func, int type, boolean_t reentry_ok)
7064 {
7065 	ipsq_t	*ipsq;
7066 
7067 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7068 	/*
7069 	 * Caller must have done a refhold on the ipif. ipif_refrele
7070 	 * happens on the passed ipif. We can do this since we are
7071 	 * already exclusive, or we won't access ipif henceforth, Both
7072 	 * this func and caller will just return if we ipsq_try_enter
7073 	 * fails above. This is needed because func needs to
7074 	 * see the correct refcount. Eg. removeif can work only then.
7075 	 */
7076 	if (ipif != NULL)
7077 		ipif_refrele(ipif);
7078 	else
7079 		ill_refrele(ill);
7080 	if (ipsq != NULL) {
7081 		(*func)(ipsq, q, mp, NULL);
7082 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7083 	}
7084 }
7085 
7086 /*
7087  * If there are more than ILL_GRP_CNT ills in a group,
7088  * we use kmem alloc'd buffers, else use the stack
7089  */
7090 #define	ILL_GRP_CNT	14
7091 /*
7092  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7093  * Called by a thread that is currently exclusive on this ipsq.
7094  */
7095 void
7096 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7097 {
7098 	queue_t	*q;
7099 	mblk_t	*mp;
7100 	ipsq_func_t	func;
7101 	int	next;
7102 	ill_t	**ill_list = NULL;
7103 	size_t	ill_list_size = 0;
7104 	int	cnt = 0;
7105 	boolean_t need_ipsq_free = B_FALSE;
7106 
7107 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7108 	mutex_enter(&ipsq->ipsq_lock);
7109 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7110 	if (ipsq->ipsq_reentry_cnt != 1) {
7111 		ipsq->ipsq_reentry_cnt--;
7112 		mutex_exit(&ipsq->ipsq_lock);
7113 		return;
7114 	}
7115 
7116 	mp = ipsq_dq(ipsq);
7117 	while (mp != NULL) {
7118 again:
7119 		mutex_exit(&ipsq->ipsq_lock);
7120 		func = (ipsq_func_t)mp->b_prev;
7121 		q = (queue_t *)mp->b_queue;
7122 		mp->b_prev = NULL;
7123 		mp->b_queue = NULL;
7124 
7125 		/*
7126 		 * If 'q' is an conn queue, it is valid, since we did a
7127 		 * a refhold on the connp, at the start of the ioctl.
7128 		 * If 'q' is an ill queue, it is valid, since close of an
7129 		 * ill will clean up the 'ipsq'.
7130 		 */
7131 		(*func)(ipsq, q, mp, NULL);
7132 
7133 		mutex_enter(&ipsq->ipsq_lock);
7134 		mp = ipsq_dq(ipsq);
7135 	}
7136 
7137 	mutex_exit(&ipsq->ipsq_lock);
7138 
7139 	/*
7140 	 * Need to grab the locks in the right order. Need to
7141 	 * atomically check (under ipsq_lock) that there are no
7142 	 * messages before relinquishing the ipsq. Also need to
7143 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7144 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7145 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7146 	 * to grab ill_g_lock as writer.
7147 	 */
7148 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7149 
7150 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7151 	if (ipsq->ipsq_refs != 0) {
7152 		/* At most 2 ills v4/v6 per phyint */
7153 		cnt = ipsq->ipsq_refs << 1;
7154 		ill_list_size = cnt * sizeof (ill_t *);
7155 		/*
7156 		 * If memory allocation fails, we will do the split
7157 		 * the next time ipsq_exit is called for whatever reason.
7158 		 * As long as the ipsq_split flag is set the need to
7159 		 * split is remembered.
7160 		 */
7161 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7162 		if (ill_list != NULL)
7163 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7164 	}
7165 	mutex_enter(&ipsq->ipsq_lock);
7166 	mp = ipsq_dq(ipsq);
7167 	if (mp != NULL) {
7168 		/* oops, some message has landed up, we can't get out */
7169 		if (ill_list != NULL)
7170 			ill_unlock_ills(ill_list, cnt);
7171 		rw_exit(&ill_g_lock);
7172 		if (ill_list != NULL)
7173 			kmem_free(ill_list, ill_list_size);
7174 		ill_list = NULL;
7175 		ill_list_size = 0;
7176 		cnt = 0;
7177 		goto again;
7178 	}
7179 
7180 	/*
7181 	 * Split only if no ioctl is pending and if memory alloc succeeded
7182 	 * above.
7183 	 */
7184 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7185 		ill_list != NULL) {
7186 		/*
7187 		 * No new ill can join this ipsq since we are holding the
7188 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7189 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7190 		 * If so we will retry on the next ipsq_exit.
7191 		 */
7192 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7193 	}
7194 
7195 	/*
7196 	 * We are holding the ipsq lock, hence no new messages can
7197 	 * land up on the ipsq, and there are no messages currently.
7198 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7199 	 * atomically while holding ill locks.
7200 	 */
7201 	ipsq->ipsq_writer = NULL;
7202 	ipsq->ipsq_reentry_cnt--;
7203 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7204 #ifdef ILL_DEBUG
7205 	ipsq->ipsq_depth = 0;
7206 #endif
7207 	mutex_exit(&ipsq->ipsq_lock);
7208 	/*
7209 	 * For IPMP this should wake up all ills in this ipsq.
7210 	 * We need to hold the ill_lock while waking up waiters to
7211 	 * avoid missed wakeups. But there is no need to acquire all
7212 	 * the ill locks and then wakeup. If we have not acquired all
7213 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7214 	 * wakes up ills one at a time after getting the right ill_lock
7215 	 */
7216 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7217 	if (ill_list != NULL)
7218 		ill_unlock_ills(ill_list, cnt);
7219 	if (ipsq->ipsq_refs == 0)
7220 		need_ipsq_free = B_TRUE;
7221 	rw_exit(&ill_g_lock);
7222 	if (ill_list != 0)
7223 		kmem_free(ill_list, ill_list_size);
7224 
7225 	if (need_ipsq_free) {
7226 		/*
7227 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7228 		 * looked up. ipsq can be looked up only thru ill or phyint
7229 		 * and there are no ills/phyint on this ipsq.
7230 		 */
7231 		ipsq_delete(ipsq);
7232 	}
7233 	/*
7234 	 * Now start any igmp or mld timers that could not be started
7235 	 * while inside the ipsq. The timers can't be started while inside
7236 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7237 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7238 	 * there could be a deadlock since the timeout handlers
7239 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7240 	 * wait in ipsq_enter() trying to get the ipsq.
7241 	 *
7242 	 * However there is one exception to the above. If this thread is
7243 	 * itself the igmp/mld timeout handler thread, then we don't want
7244 	 * to start any new timer until the current handler is done. The
7245 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7246 	 * all others pass B_TRUE.
7247 	 */
7248 	if (start_igmp_timer) {
7249 		mutex_enter(&igmp_timer_lock);
7250 		next = igmp_deferred_next;
7251 		igmp_deferred_next = INFINITY;
7252 		mutex_exit(&igmp_timer_lock);
7253 
7254 		if (next != INFINITY)
7255 			igmp_start_timers(next);
7256 	}
7257 
7258 	if (start_mld_timer) {
7259 		mutex_enter(&mld_timer_lock);
7260 		next = mld_deferred_next;
7261 		mld_deferred_next = INFINITY;
7262 		mutex_exit(&mld_timer_lock);
7263 
7264 		if (next != INFINITY)
7265 			mld_start_timers(next);
7266 	}
7267 }
7268 
7269 /*
7270  * The ill is closing. Flush all messages on the ipsq that originated
7271  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7272  * for this ill since ipsq_enter could not have entered until then.
7273  * New messages can't be queued since the CONDEMNED flag is set.
7274  */
7275 static void
7276 ipsq_flush(ill_t *ill)
7277 {
7278 	queue_t	*q;
7279 	mblk_t	*prev;
7280 	mblk_t	*mp;
7281 	mblk_t	*mp_next;
7282 	ipsq_t	*ipsq;
7283 
7284 	ASSERT(IAM_WRITER_ILL(ill));
7285 	ipsq = ill->ill_phyint->phyint_ipsq;
7286 	/*
7287 	 * Flush any messages sent up by the driver.
7288 	 */
7289 	mutex_enter(&ipsq->ipsq_lock);
7290 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7291 		mp_next = mp->b_next;
7292 		q = mp->b_queue;
7293 		if (q == ill->ill_rq || q == ill->ill_wq) {
7294 			/* Remove the mp from the ipsq */
7295 			if (prev == NULL)
7296 				ipsq->ipsq_mphead = mp->b_next;
7297 			else
7298 				prev->b_next = mp->b_next;
7299 			if (ipsq->ipsq_mptail == mp) {
7300 				ASSERT(mp_next == NULL);
7301 				ipsq->ipsq_mptail = prev;
7302 			}
7303 			ip_ioctl_freemsg(mp);
7304 		} else {
7305 			prev = mp;
7306 		}
7307 	}
7308 	mutex_exit(&ipsq->ipsq_lock);
7309 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7310 	ipsq_xopq_mp_cleanup(ill, NULL);
7311 	ill_pending_mp_cleanup(ill);
7312 }
7313 
7314 /*
7315  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7316  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7317  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7318  * time (possible with one port going down for aggr and someone tearing down the
7319  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7320  * to indicate when the cleanup has started (1 ref) and when the cleanup
7321  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7322  * putting 2 ref on ill_inuse_ref.
7323  */
7324 static void
7325 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7326 {
7327 	conn_t *connp;
7328 	squeue_t *sqp;
7329 	mblk_t *mp;
7330 
7331 	ASSERT(rx_ring != NULL);
7332 
7333 	/* Just clean one squeue */
7334 	mutex_enter(&ill->ill_lock);
7335 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7336 		/* Some operations pending on the ring. Wait */
7337 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7338 
7339 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7340 		/*
7341 		 * Someone already trying to clean
7342 		 * this squeue or its already been cleaned.
7343 		 */
7344 		mutex_exit(&ill->ill_lock);
7345 		return;
7346 	}
7347 	sqp = rx_ring->rr_sqp;
7348 
7349 	if (sqp == NULL) {
7350 		/*
7351 		 * The rx_ring never had a squeue assigned to it.
7352 		 * We are under ill_lock so we can clean it up
7353 		 * here itself since no one can get to it.
7354 		 */
7355 		rx_ring->rr_blank = NULL;
7356 		rx_ring->rr_handle = NULL;
7357 		rx_ring->rr_sqp = NULL;
7358 		rx_ring->rr_ring_state = ILL_RING_FREE;
7359 		mutex_exit(&ill->ill_lock);
7360 		return;
7361 	}
7362 
7363 	/* Set the state that its being cleaned */
7364 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7365 	ASSERT(sqp != NULL);
7366 	mutex_exit(&ill->ill_lock);
7367 
7368 	/*
7369 	 * Use the preallocated ill_unbind_conn for this purpose
7370 	 */
7371 	connp = ill->ill_poll_capab->ill_unbind_conn;
7372 	mp = &connp->conn_tcp->tcp_closemp;
7373 	CONN_INC_REF(connp);
7374 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7375 
7376 	mutex_enter(&ill->ill_lock);
7377 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7378 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7379 
7380 	mutex_exit(&ill->ill_lock);
7381 }
7382 
7383 static void
7384 ipsq_clean_all(ill_t *ill)
7385 {
7386 	int idx;
7387 
7388 	/*
7389 	 * No need to clean if poll_capab isn't set for this ill
7390 	 */
7391 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
7392 		return;
7393 
7394 	ill->ill_capabilities &= ~ILL_CAPAB_POLL;
7395 
7396 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7397 		ill_rx_ring_t *ipr = &ill->ill_poll_capab->ill_ring_tbl[idx];
7398 		ipsq_clean_ring(ill, ipr);
7399 	}
7400 }
7401 
7402 /* ARGSUSED */
7403 int
7404 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7405     ip_ioctl_cmd_t *ipip, void *ifreq)
7406 {
7407 	ill_t	*ill;
7408 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7409 	boolean_t isv6;
7410 	conn_t	*connp;
7411 
7412 	connp = Q_TO_CONN(q);
7413 	isv6 = connp->conn_af_isv6;
7414 	/*
7415 	 * Set original index.
7416 	 * Failover and failback move logical interfaces
7417 	 * from one physical interface to another.  The
7418 	 * original index indicates the parent of a logical
7419 	 * interface, in other words, the physical interface
7420 	 * the logical interface will be moved back to on
7421 	 * failback.
7422 	 */
7423 
7424 	/*
7425 	 * Don't allow the original index to be changed
7426 	 * for non-failover addresses, autoconfigured
7427 	 * addresses, or IPv6 link local addresses.
7428 	 */
7429 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7430 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7431 		return (EINVAL);
7432 	}
7433 	/*
7434 	 * The new original index must be in use by some
7435 	 * physical interface.
7436 	 */
7437 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7438 	    NULL, NULL);
7439 	if (ill == NULL)
7440 		return (ENXIO);
7441 	ill_refrele(ill);
7442 
7443 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7444 	/*
7445 	 * When this ipif gets failed back, don't
7446 	 * preserve the original id, as it is no
7447 	 * longer applicable.
7448 	 */
7449 	ipif->ipif_orig_ipifid = 0;
7450 	/*
7451 	 * For IPv4, change the original index of any
7452 	 * multicast addresses associated with the
7453 	 * ipif to the new value.
7454 	 */
7455 	if (!isv6) {
7456 		ilm_t *ilm;
7457 
7458 		mutex_enter(&ipif->ipif_ill->ill_lock);
7459 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7460 		    ilm = ilm->ilm_next) {
7461 			if (ilm->ilm_ipif == ipif) {
7462 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7463 			}
7464 		}
7465 		mutex_exit(&ipif->ipif_ill->ill_lock);
7466 	}
7467 	return (0);
7468 }
7469 
7470 /* ARGSUSED */
7471 int
7472 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7473     ip_ioctl_cmd_t *ipip, void *ifreq)
7474 {
7475 	struct lifreq *lifr = (struct lifreq *)ifreq;
7476 
7477 	/*
7478 	 * Get the original interface index i.e the one
7479 	 * before FAILOVER if it ever happened.
7480 	 */
7481 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7482 	return (0);
7483 }
7484 
7485 /*
7486  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7487  * refhold and return the associated ipif
7488  */
7489 int
7490 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7491 {
7492 	boolean_t exists;
7493 	struct iftun_req *ta;
7494 	ipif_t	*ipif;
7495 	ill_t	*ill;
7496 	boolean_t isv6;
7497 	mblk_t	*mp1;
7498 	int	error;
7499 	conn_t	*connp;
7500 
7501 	/* Existence verified in ip_wput_nondata */
7502 	mp1 = mp->b_cont->b_cont;
7503 	ta = (struct iftun_req *)mp1->b_rptr;
7504 	/*
7505 	 * Null terminate the string to protect against buffer
7506 	 * overrun. String was generated by user code and may not
7507 	 * be trusted.
7508 	 */
7509 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7510 
7511 	connp = Q_TO_CONN(q);
7512 	isv6 = connp->conn_af_isv6;
7513 
7514 	/* Disallows implicit create */
7515 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7516 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7517 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7518 	if (ipif == NULL)
7519 		return (error);
7520 
7521 	if (ipif->ipif_id != 0) {
7522 		/*
7523 		 * We really don't want to set/get tunnel parameters
7524 		 * on virtual tunnel interfaces.  Only allow the
7525 		 * base tunnel to do these.
7526 		 */
7527 		ipif_refrele(ipif);
7528 		return (EINVAL);
7529 	}
7530 
7531 	/*
7532 	 * Send down to tunnel mod for ioctl processing.
7533 	 * Will finish ioctl in ip_rput_other().
7534 	 */
7535 	ill = ipif->ipif_ill;
7536 	if (ill->ill_net_type == IRE_LOOPBACK) {
7537 		ipif_refrele(ipif);
7538 		return (EOPNOTSUPP);
7539 	}
7540 
7541 	if (ill->ill_wq == NULL) {
7542 		ipif_refrele(ipif);
7543 		return (ENXIO);
7544 	}
7545 	/*
7546 	 * Mark the ioctl as coming from an IPv6 interface for
7547 	 * tun's convenience.
7548 	 */
7549 	if (ill->ill_isv6)
7550 		ta->ifta_flags |= 0x80000000;
7551 	*ipifp = ipif;
7552 	return (0);
7553 }
7554 
7555 /*
7556  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7557  * and return the associated ipif.
7558  * Return value:
7559  *	Non zero: An error has occurred. ci may not be filled out.
7560  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7561  *	a held ipif in ci.ci_ipif.
7562  */
7563 int
7564 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7565     cmd_info_t *ci, ipsq_func_t func)
7566 {
7567 	sin_t		*sin;
7568 	sin6_t		*sin6;
7569 	char		*name;
7570 	struct ifreq    *ifr;
7571 	struct lifreq    *lifr;
7572 	ipif_t		*ipif = NULL;
7573 	ill_t		*ill;
7574 	conn_t		*connp;
7575 	boolean_t	isv6;
7576 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7577 	boolean_t	exists;
7578 	int		err;
7579 	mblk_t		*mp1;
7580 	zoneid_t	zoneid;
7581 
7582 	if (q->q_next != NULL) {
7583 		ill = (ill_t *)q->q_ptr;
7584 		isv6 = ill->ill_isv6;
7585 		connp = NULL;
7586 		zoneid = ALL_ZONES;
7587 	} else {
7588 		ill = NULL;
7589 		connp = Q_TO_CONN(q);
7590 		isv6 = connp->conn_af_isv6;
7591 		zoneid = connp->conn_zoneid;
7592 		if (zoneid == GLOBAL_ZONEID) {
7593 			/* global zone can access ipifs in all zones */
7594 			zoneid = ALL_ZONES;
7595 		}
7596 	}
7597 
7598 	/* Has been checked in ip_wput_nondata */
7599 	mp1 = mp->b_cont->b_cont;
7600 
7601 
7602 	if (cmd_type == IF_CMD) {
7603 		/* This a old style SIOC[GS]IF* command */
7604 		ifr = (struct ifreq *)mp1->b_rptr;
7605 		/*
7606 		 * Null terminate the string to protect against buffer
7607 		 * overrun. String was generated by user code and may not
7608 		 * be trusted.
7609 		 */
7610 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7611 		sin = (sin_t *)&ifr->ifr_addr;
7612 		name = ifr->ifr_name;
7613 		ci->ci_sin = sin;
7614 		ci->ci_sin6 = NULL;
7615 		ci->ci_lifr = (struct lifreq *)ifr;
7616 	} else {
7617 		/* This a new style SIOC[GS]LIF* command */
7618 		ASSERT(cmd_type == LIF_CMD);
7619 		lifr = (struct lifreq *)mp1->b_rptr;
7620 		/*
7621 		 * Null terminate the string to protect against buffer
7622 		 * overrun. String was generated by user code and may not
7623 		 * be trusted.
7624 		 */
7625 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7626 		name = lifr->lifr_name;
7627 		sin = (sin_t *)&lifr->lifr_addr;
7628 		sin6 = (sin6_t *)&lifr->lifr_addr;
7629 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7630 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7631 			    LIFNAMSIZ);
7632 		}
7633 		ci->ci_sin = sin;
7634 		ci->ci_sin6 = sin6;
7635 		ci->ci_lifr = lifr;
7636 	}
7637 
7638 
7639 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7640 		/*
7641 		 * The ioctl will be failed if the ioctl comes down
7642 		 * an conn stream
7643 		 */
7644 		if (ill == NULL) {
7645 			/*
7646 			 * Not an ill queue, return EINVAL same as the
7647 			 * old error code.
7648 			 */
7649 			return (ENXIO);
7650 		}
7651 		ipif = ill->ill_ipif;
7652 		ipif_refhold(ipif);
7653 	} else {
7654 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7655 		    &exists, isv6, zoneid,
7656 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7657 		if (ipif == NULL) {
7658 			if (err == EINPROGRESS)
7659 				return (err);
7660 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7661 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7662 				/*
7663 				 * Need to try both v4 and v6 since this
7664 				 * ioctl can come down either v4 or v6
7665 				 * socket. The lifreq.lifr_family passed
7666 				 * down by this ioctl is AF_UNSPEC.
7667 				 */
7668 				ipif = ipif_lookup_on_name(name,
7669 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7670 				    zoneid, (connp == NULL) ? q :
7671 				    CONNP_TO_WQ(connp), mp, func, &err);
7672 				if (err == EINPROGRESS)
7673 					return (err);
7674 			}
7675 			err = 0;	/* Ensure we don't use it below */
7676 		}
7677 	}
7678 
7679 	/*
7680 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7681 	 */
7682 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7683 		ipif_refrele(ipif);
7684 		return (ENXIO);
7685 	}
7686 
7687 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7688 	    name[0] == '\0') {
7689 		/*
7690 		 * Handle a or a SIOC?IF* with a null name
7691 		 * during plumb (on the ill queue before the I_PLINK).
7692 		 */
7693 		ipif = ill->ill_ipif;
7694 		ipif_refhold(ipif);
7695 	}
7696 
7697 	if (ipif == NULL)
7698 		return (ENXIO);
7699 
7700 	/*
7701 	 * Allow only GET operations if this ipif has been created
7702 	 * temporarily due to a MOVE operation.
7703 	 */
7704 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7705 		ipif_refrele(ipif);
7706 		return (EINVAL);
7707 	}
7708 
7709 	ci->ci_ipif = ipif;
7710 	return (0);
7711 }
7712 
7713 /*
7714  * Return the total number of ipifs.
7715  */
7716 static uint_t
7717 ip_get_numifs(zoneid_t zoneid)
7718 {
7719 	uint_t numifs = 0;
7720 	ill_t	*ill;
7721 	ill_walk_context_t	ctx;
7722 	ipif_t	*ipif;
7723 
7724 	rw_enter(&ill_g_lock, RW_READER);
7725 	ill = ILL_START_WALK_V4(&ctx);
7726 
7727 	while (ill != NULL) {
7728 		for (ipif = ill->ill_ipif; ipif != NULL;
7729 		    ipif = ipif->ipif_next) {
7730 			if (ipif->ipif_zoneid == zoneid)
7731 				numifs++;
7732 		}
7733 		ill = ill_next(&ctx, ill);
7734 	}
7735 	rw_exit(&ill_g_lock);
7736 	return (numifs);
7737 }
7738 
7739 /*
7740  * Return the total number of ipifs.
7741  */
7742 static uint_t
7743 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7744 {
7745 	uint_t numifs = 0;
7746 	ill_t	*ill;
7747 	ipif_t	*ipif;
7748 	ill_walk_context_t	ctx;
7749 
7750 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7751 
7752 	rw_enter(&ill_g_lock, RW_READER);
7753 	if (family == AF_INET)
7754 		ill = ILL_START_WALK_V4(&ctx);
7755 	else if (family == AF_INET6)
7756 		ill = ILL_START_WALK_V6(&ctx);
7757 	else
7758 		ill = ILL_START_WALK_ALL(&ctx);
7759 
7760 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7761 		for (ipif = ill->ill_ipif; ipif != NULL;
7762 		    ipif = ipif->ipif_next) {
7763 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7764 			    !(lifn_flags & LIFC_NOXMIT))
7765 				continue;
7766 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7767 			    !(lifn_flags & LIFC_TEMPORARY))
7768 				continue;
7769 			if (((ipif->ipif_flags &
7770 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7771 			    IPIF_DEPRECATED)) ||
7772 			    (ill->ill_phyint->phyint_flags &
7773 			    PHYI_LOOPBACK) ||
7774 			    !(ipif->ipif_flags & IPIF_UP)) &&
7775 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7776 				continue;
7777 
7778 			if (zoneid != ipif->ipif_zoneid &&
7779 			    (zoneid != GLOBAL_ZONEID ||
7780 			    !(lifn_flags & LIFC_ALLZONES)))
7781 				continue;
7782 
7783 			numifs++;
7784 		}
7785 	}
7786 	rw_exit(&ill_g_lock);
7787 	return (numifs);
7788 }
7789 
7790 uint_t
7791 ip_get_lifsrcofnum(ill_t *ill)
7792 {
7793 	uint_t numifs = 0;
7794 	ill_t	*ill_head = ill;
7795 
7796 	/*
7797 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7798 	 * other thread may be trying to relink the ILLs in this usesrc group
7799 	 * and adjusting the ill_usesrc_grp_next pointers
7800 	 */
7801 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7802 	if ((ill->ill_usesrc_ifindex == 0) &&
7803 	    (ill->ill_usesrc_grp_next != NULL)) {
7804 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7805 		    ill = ill->ill_usesrc_grp_next)
7806 			numifs++;
7807 	}
7808 	rw_exit(&ill_g_usesrc_lock);
7809 
7810 	return (numifs);
7811 }
7812 
7813 /* Null values are passed in for ipif, sin, and ifreq */
7814 /* ARGSUSED */
7815 int
7816 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7817     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7818 {
7819 	int *nump;
7820 
7821 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7822 
7823 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7824 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7825 
7826 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
7827 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7828 	return (0);
7829 }
7830 
7831 /* Null values are passed in for ipif, sin, and ifreq */
7832 /* ARGSUSED */
7833 int
7834 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7835     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7836 {
7837 	struct lifnum *lifn;
7838 	mblk_t	*mp1;
7839 
7840 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7841 
7842 	/* Existence checked in ip_wput_nondata */
7843 	mp1 = mp->b_cont->b_cont;
7844 
7845 	lifn = (struct lifnum *)mp1->b_rptr;
7846 	switch (lifn->lifn_family) {
7847 	case AF_UNSPEC:
7848 	case AF_INET:
7849 	case AF_INET6:
7850 		break;
7851 	default:
7852 		return (EAFNOSUPPORT);
7853 	}
7854 
7855 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7856 	    Q_TO_CONN(q)->conn_zoneid);
7857 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7858 	return (0);
7859 }
7860 
7861 /* ARGSUSED */
7862 int
7863 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7864     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7865 {
7866 	STRUCT_HANDLE(ifconf, ifc);
7867 	mblk_t *mp1;
7868 	struct iocblk *iocp;
7869 	struct ifreq *ifr;
7870 	ill_walk_context_t	ctx;
7871 	ill_t	*ill;
7872 	ipif_t	*ipif;
7873 	struct sockaddr_in *sin;
7874 	int32_t	ifclen;
7875 	zoneid_t zoneid;
7876 
7877 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7878 
7879 	ip1dbg(("ip_sioctl_get_ifconf"));
7880 	/* Existence verified in ip_wput_nondata */
7881 	mp1 = mp->b_cont->b_cont;
7882 	iocp = (struct iocblk *)mp->b_rptr;
7883 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7884 
7885 	/*
7886 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7887 	 * the user buffer address and length into which the list of struct
7888 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7889 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7890 	 * the SIOCGIFCONF operation was redefined to simply provide
7891 	 * a large output buffer into which we are supposed to jam the ifreq
7892 	 * array.  The same ioctl command code was used, despite the fact that
7893 	 * both the applications and the kernel code had to change, thus making
7894 	 * it impossible to support both interfaces.
7895 	 *
7896 	 * For reasons not good enough to try to explain, the following
7897 	 * algorithm is used for deciding what to do with one of these:
7898 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7899 	 * form with the output buffer coming down as the continuation message.
7900 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7901 	 * and we have to copy in the ifconf structure to find out how big the
7902 	 * output buffer is and where to copy out to.  Sure no problem...
7903 	 *
7904 	 */
7905 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7906 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7907 		int numifs = 0;
7908 		size_t ifc_bufsize;
7909 
7910 		/*
7911 		 * Must be (better be!) continuation of a TRANSPARENT
7912 		 * IOCTL.  We just copied in the ifconf structure.
7913 		 */
7914 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7915 		    (struct ifconf *)mp1->b_rptr);
7916 
7917 		/*
7918 		 * Allocate a buffer to hold requested information.
7919 		 *
7920 		 * If ifc_len is larger than what is needed, we only
7921 		 * allocate what we will use.
7922 		 *
7923 		 * If ifc_len is smaller than what is needed, return
7924 		 * EINVAL.
7925 		 *
7926 		 * XXX: the ill_t structure can hava 2 counters, for
7927 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7928 		 * number of interfaces for a device, so we don't need
7929 		 * to count them here...
7930 		 */
7931 		numifs = ip_get_numifs(zoneid);
7932 
7933 		ifclen = STRUCT_FGET(ifc, ifc_len);
7934 		ifc_bufsize = numifs * sizeof (struct ifreq);
7935 		if (ifc_bufsize > ifclen) {
7936 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7937 				/* old behaviour */
7938 				return (EINVAL);
7939 			} else {
7940 				ifc_bufsize = ifclen;
7941 			}
7942 		}
7943 
7944 		mp1 = mi_copyout_alloc(q, mp,
7945 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7946 		if (mp1 == NULL)
7947 			return (ENOMEM);
7948 
7949 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7950 	}
7951 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7952 	/*
7953 	 * the SIOCGIFCONF ioctl only knows about
7954 	 * IPv4 addresses, so don't try to tell
7955 	 * it about interfaces with IPv6-only
7956 	 * addresses. (Last parm 'isv6' is B_FALSE)
7957 	 */
7958 
7959 	ifr = (struct ifreq *)mp1->b_rptr;
7960 
7961 	rw_enter(&ill_g_lock, RW_READER);
7962 	ill = ILL_START_WALK_V4(&ctx);
7963 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7964 		for (ipif = ill->ill_ipif; ipif;
7965 		    ipif = ipif->ipif_next) {
7966 			if (zoneid != ipif->ipif_zoneid)
7967 				continue;
7968 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7969 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7970 					/* old behaviour */
7971 					rw_exit(&ill_g_lock);
7972 					return (EINVAL);
7973 				} else {
7974 					goto if_copydone;
7975 				}
7976 			}
7977 			(void) ipif_get_name(ipif,
7978 			    ifr->ifr_name,
7979 			    sizeof (ifr->ifr_name));
7980 			sin = (sin_t *)&ifr->ifr_addr;
7981 			*sin = sin_null;
7982 			sin->sin_family = AF_INET;
7983 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7984 			ifr++;
7985 		}
7986 	}
7987 if_copydone:
7988 	rw_exit(&ill_g_lock);
7989 	mp1->b_wptr = (uchar_t *)ifr;
7990 
7991 	if (STRUCT_BUF(ifc) != NULL) {
7992 		STRUCT_FSET(ifc, ifc_len,
7993 			(int)((uchar_t *)ifr - mp1->b_rptr));
7994 	}
7995 	return (0);
7996 }
7997 
7998 /*
7999  * Get the interfaces using the address hosted on the interface passed in,
8000  * as a source adddress
8001  */
8002 /* ARGSUSED */
8003 int
8004 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8005     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8006 {
8007 	mblk_t *mp1;
8008 	ill_t	*ill, *ill_head;
8009 	ipif_t	*ipif, *orig_ipif;
8010 	int	numlifs = 0;
8011 	size_t	lifs_bufsize, lifsmaxlen;
8012 	struct	lifreq *lifr;
8013 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8014 	uint_t	ifindex;
8015 	zoneid_t zoneid;
8016 	int err = 0;
8017 	boolean_t isv6 = B_FALSE;
8018 	struct	sockaddr_in	*sin;
8019 	struct	sockaddr_in6	*sin6;
8020 
8021 	STRUCT_HANDLE(lifsrcof, lifs);
8022 
8023 	ASSERT(q->q_next == NULL);
8024 
8025 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8026 
8027 	/* Existence verified in ip_wput_nondata */
8028 	mp1 = mp->b_cont->b_cont;
8029 
8030 	/*
8031 	 * Must be (better be!) continuation of a TRANSPARENT
8032 	 * IOCTL.  We just copied in the lifsrcof structure.
8033 	 */
8034 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8035 	    (struct lifsrcof *)mp1->b_rptr);
8036 
8037 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8038 		return (EINVAL);
8039 
8040 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8041 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8042 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8043 	    ip_process_ioctl, &err);
8044 	if (ipif == NULL) {
8045 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8046 		    ifindex));
8047 		return (err);
8048 	}
8049 
8050 
8051 	/* Allocate a buffer to hold requested information */
8052 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8053 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8054 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8055 	/* The actual size needed is always returned in lifs_len */
8056 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8057 
8058 	/* If the amount we need is more than what is passed in, abort */
8059 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8060 		ipif_refrele(ipif);
8061 		return (0);
8062 	}
8063 
8064 	mp1 = mi_copyout_alloc(q, mp,
8065 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8066 	if (mp1 == NULL) {
8067 		ipif_refrele(ipif);
8068 		return (ENOMEM);
8069 	}
8070 
8071 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8072 	bzero(mp1->b_rptr, lifs_bufsize);
8073 
8074 	lifr = (struct lifreq *)mp1->b_rptr;
8075 
8076 	ill = ill_head = ipif->ipif_ill;
8077 	orig_ipif = ipif;
8078 
8079 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8080 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8081 	rw_enter(&ill_g_lock, RW_READER);
8082 
8083 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8084 	for (; (ill != NULL) && (ill != ill_head);
8085 	    ill = ill->ill_usesrc_grp_next) {
8086 
8087 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8088 			break;
8089 
8090 		ipif = ill->ill_ipif;
8091 		(void) ipif_get_name(ipif,
8092 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8093 		if (ipif->ipif_isv6) {
8094 			sin6 = (sin6_t *)&lifr->lifr_addr;
8095 			*sin6 = sin6_null;
8096 			sin6->sin6_family = AF_INET6;
8097 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8098 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8099 			    &ipif->ipif_v6net_mask);
8100 		} else {
8101 			sin = (sin_t *)&lifr->lifr_addr;
8102 			*sin = sin_null;
8103 			sin->sin_family = AF_INET;
8104 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8105 			lifr->lifr_addrlen = ip_mask_to_plen(
8106 			    ipif->ipif_net_mask);
8107 		}
8108 		lifr++;
8109 	}
8110 	rw_exit(&ill_g_usesrc_lock);
8111 	rw_exit(&ill_g_lock);
8112 	ipif_refrele(orig_ipif);
8113 	mp1->b_wptr = (uchar_t *)lifr;
8114 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8115 
8116 	return (0);
8117 }
8118 
8119 /* ARGSUSED */
8120 int
8121 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8122     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8123 {
8124 	mblk_t *mp1;
8125 	int	list;
8126 	ill_t	*ill;
8127 	ipif_t	*ipif;
8128 	int	flags;
8129 	int	numlifs = 0;
8130 	size_t	lifc_bufsize;
8131 	struct	lifreq *lifr;
8132 	sa_family_t	family;
8133 	struct	sockaddr_in	*sin;
8134 	struct	sockaddr_in6	*sin6;
8135 	ill_walk_context_t	ctx;
8136 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8137 	int32_t	lifclen;
8138 	zoneid_t zoneid;
8139 	STRUCT_HANDLE(lifconf, lifc);
8140 
8141 	ip1dbg(("ip_sioctl_get_lifconf"));
8142 
8143 	ASSERT(q->q_next == NULL);
8144 
8145 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8146 
8147 	/* Existence verified in ip_wput_nondata */
8148 	mp1 = mp->b_cont->b_cont;
8149 
8150 	/*
8151 	 * An extended version of SIOCGIFCONF that takes an
8152 	 * additional address family and flags field.
8153 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8154 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8155 	 * interfaces are omitted.
8156 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8157 	 * unless LIFC_TEMPORARY is specified.
8158 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8159 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8160 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8161 	 * has priority over LIFC_NOXMIT.
8162 	 */
8163 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8164 
8165 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8166 		return (EINVAL);
8167 
8168 	/*
8169 	 * Must be (better be!) continuation of a TRANSPARENT
8170 	 * IOCTL.  We just copied in the lifconf structure.
8171 	 */
8172 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8173 
8174 	family = STRUCT_FGET(lifc, lifc_family);
8175 	flags = STRUCT_FGET(lifc, lifc_flags);
8176 
8177 	switch (family) {
8178 	case AF_UNSPEC:
8179 		/*
8180 		 * walk all ILL's.
8181 		 */
8182 		list = MAX_G_HEADS;
8183 		break;
8184 	case AF_INET:
8185 		/*
8186 		 * walk only IPV4 ILL's.
8187 		 */
8188 		list = IP_V4_G_HEAD;
8189 		break;
8190 	case AF_INET6:
8191 		/*
8192 		 * walk only IPV6 ILL's.
8193 		 */
8194 		list = IP_V6_G_HEAD;
8195 		break;
8196 	default:
8197 		return (EAFNOSUPPORT);
8198 	}
8199 
8200 	/*
8201 	 * Allocate a buffer to hold requested information.
8202 	 *
8203 	 * If lifc_len is larger than what is needed, we only
8204 	 * allocate what we will use.
8205 	 *
8206 	 * If lifc_len is smaller than what is needed, return
8207 	 * EINVAL.
8208 	 */
8209 	numlifs = ip_get_numlifs(family, flags, zoneid);
8210 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8211 	lifclen = STRUCT_FGET(lifc, lifc_len);
8212 	if (lifc_bufsize > lifclen) {
8213 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8214 			return (EINVAL);
8215 		else
8216 			lifc_bufsize = lifclen;
8217 	}
8218 
8219 	mp1 = mi_copyout_alloc(q, mp,
8220 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8221 	if (mp1 == NULL)
8222 		return (ENOMEM);
8223 
8224 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8225 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8226 
8227 	lifr = (struct lifreq *)mp1->b_rptr;
8228 
8229 	rw_enter(&ill_g_lock, RW_READER);
8230 	ill = ill_first(list, list, &ctx);
8231 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8232 		for (ipif = ill->ill_ipif; ipif != NULL;
8233 		    ipif = ipif->ipif_next) {
8234 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8235 			    !(flags & LIFC_NOXMIT))
8236 				continue;
8237 
8238 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8239 			    !(flags & LIFC_TEMPORARY))
8240 				continue;
8241 
8242 			if (((ipif->ipif_flags &
8243 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8244 			    IPIF_DEPRECATED)) ||
8245 			    (ill->ill_phyint->phyint_flags &
8246 			    PHYI_LOOPBACK) ||
8247 			    !(ipif->ipif_flags & IPIF_UP)) &&
8248 			    (flags & LIFC_EXTERNAL_SOURCE))
8249 				continue;
8250 
8251 			if (zoneid != ipif->ipif_zoneid &&
8252 			    (zoneid != GLOBAL_ZONEID ||
8253 			    !(flags & LIFC_ALLZONES)))
8254 				continue;
8255 
8256 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8257 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8258 					rw_exit(&ill_g_lock);
8259 					return (EINVAL);
8260 				} else {
8261 					goto lif_copydone;
8262 				}
8263 			}
8264 
8265 			(void) ipif_get_name(ipif,
8266 				lifr->lifr_name,
8267 				sizeof (lifr->lifr_name));
8268 			if (ipif->ipif_isv6) {
8269 				sin6 = (sin6_t *)&lifr->lifr_addr;
8270 				*sin6 = sin6_null;
8271 				sin6->sin6_family = AF_INET6;
8272 				sin6->sin6_addr =
8273 				ipif->ipif_v6lcl_addr;
8274 				lifr->lifr_addrlen =
8275 				ip_mask_to_plen_v6(
8276 				    &ipif->ipif_v6net_mask);
8277 			} else {
8278 				sin = (sin_t *)&lifr->lifr_addr;
8279 				*sin = sin_null;
8280 				sin->sin_family = AF_INET;
8281 				sin->sin_addr.s_addr =
8282 				    ipif->ipif_lcl_addr;
8283 				lifr->lifr_addrlen =
8284 				    ip_mask_to_plen(
8285 				    ipif->ipif_net_mask);
8286 			}
8287 			lifr++;
8288 		}
8289 	}
8290 lif_copydone:
8291 	rw_exit(&ill_g_lock);
8292 
8293 	mp1->b_wptr = (uchar_t *)lifr;
8294 	if (STRUCT_BUF(lifc) != NULL) {
8295 		STRUCT_FSET(lifc, lifc_len,
8296 			(int)((uchar_t *)lifr - mp1->b_rptr));
8297 	}
8298 	return (0);
8299 }
8300 
8301 /* ARGSUSED */
8302 int
8303 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8304     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8305 {
8306 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8307 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8308 	return (0);
8309 }
8310 
8311 static void
8312 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8313 {
8314 	ip6_asp_t *table;
8315 	size_t table_size;
8316 	mblk_t *data_mp;
8317 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8318 
8319 	/* These two ioctls are I_STR only */
8320 	if (iocp->ioc_count == TRANSPARENT) {
8321 		miocnak(q, mp, 0, EINVAL);
8322 		return;
8323 	}
8324 
8325 	data_mp = mp->b_cont;
8326 	if (data_mp == NULL) {
8327 		/* The user passed us a NULL argument */
8328 		table = NULL;
8329 		table_size = iocp->ioc_count;
8330 	} else {
8331 		/*
8332 		 * The user provided a table.  The stream head
8333 		 * may have copied in the user data in chunks,
8334 		 * so make sure everything is pulled up
8335 		 * properly.
8336 		 */
8337 		if (MBLKL(data_mp) < iocp->ioc_count) {
8338 			mblk_t *new_data_mp;
8339 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8340 			    NULL) {
8341 				miocnak(q, mp, 0, ENOMEM);
8342 				return;
8343 			}
8344 			freemsg(data_mp);
8345 			data_mp = new_data_mp;
8346 			mp->b_cont = data_mp;
8347 		}
8348 		table = (ip6_asp_t *)data_mp->b_rptr;
8349 		table_size = iocp->ioc_count;
8350 	}
8351 
8352 	switch (iocp->ioc_cmd) {
8353 	case SIOCGIP6ADDRPOLICY:
8354 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8355 		if (iocp->ioc_rval == -1)
8356 			iocp->ioc_error = EINVAL;
8357 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8358 		else if (table != NULL &&
8359 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8360 			ip6_asp_t *src = table;
8361 			ip6_asp32_t *dst = (void *)table;
8362 			int count = table_size / sizeof (ip6_asp_t);
8363 			int i;
8364 
8365 			/*
8366 			 * We need to do an in-place shrink of the array
8367 			 * to match the alignment attributes of the
8368 			 * 32-bit ABI looking at it.
8369 			 */
8370 			/* LINTED: logical expression always true: op "||" */
8371 			ASSERT(sizeof (*src) > sizeof (*dst));
8372 			for (i = 1; i < count; i++)
8373 				bcopy(src + i, dst + i, sizeof (*dst));
8374 		}
8375 #endif
8376 		break;
8377 
8378 	case SIOCSIP6ADDRPOLICY:
8379 		ASSERT(mp->b_prev == NULL);
8380 		mp->b_prev = (void *)q;
8381 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8382 		/*
8383 		 * We pass in the datamodel here so that the ip6_asp_replace()
8384 		 * routine can handle converting from 32-bit to native formats
8385 		 * where necessary.
8386 		 *
8387 		 * A better way to handle this might be to convert the inbound
8388 		 * data structure here, and hang it off a new 'mp'; thus the
8389 		 * ip6_asp_replace() logic would always be dealing with native
8390 		 * format data structures..
8391 		 *
8392 		 * (An even simpler way to handle these ioctls is to just
8393 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8394 		 * and just recompile everything that depends on it.)
8395 		 */
8396 #endif
8397 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8398 		    iocp->ioc_flag & IOC_MODELS);
8399 		return;
8400 	}
8401 
8402 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8403 	qreply(q, mp);
8404 }
8405 
8406 static void
8407 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8408 {
8409 	mblk_t 		*data_mp;
8410 	struct dstinforeq	*dir;
8411 	uint8_t		*end, *cur;
8412 	in6_addr_t	*daddr, *saddr;
8413 	ipaddr_t	v4daddr;
8414 	ire_t		*ire;
8415 	char		*slabel, *dlabel;
8416 	boolean_t	isipv4;
8417 	int		match_ire;
8418 	ill_t		*dst_ill;
8419 	ipif_t		*src_ipif, *ire_ipif;
8420 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8421 	zoneid_t	zoneid;
8422 
8423 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8424 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8425 
8426 	/*
8427 	 * This ioctl is I_STR only, and must have a
8428 	 * data mblk following the M_IOCTL mblk.
8429 	 */
8430 	data_mp = mp->b_cont;
8431 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8432 		miocnak(q, mp, 0, EINVAL);
8433 		return;
8434 	}
8435 
8436 	if (MBLKL(data_mp) < iocp->ioc_count) {
8437 		mblk_t *new_data_mp;
8438 
8439 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8440 			miocnak(q, mp, 0, ENOMEM);
8441 			return;
8442 		}
8443 		freemsg(data_mp);
8444 		data_mp = new_data_mp;
8445 		mp->b_cont = data_mp;
8446 	}
8447 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8448 
8449 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8450 	    end - cur >= sizeof (struct dstinforeq);
8451 	    cur += sizeof (struct dstinforeq)) {
8452 		dir = (struct dstinforeq *)cur;
8453 		daddr = &dir->dir_daddr;
8454 		saddr = &dir->dir_saddr;
8455 
8456 		/*
8457 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8458 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8459 		 * and ipif_select_source[_v6]() do not.
8460 		 */
8461 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8462 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8463 
8464 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8465 		if (isipv4) {
8466 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8467 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8468 			    0, NULL, NULL, zoneid, 0, match_ire);
8469 		} else {
8470 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8471 			    0, NULL, NULL, zoneid, 0, match_ire);
8472 		}
8473 		if (ire == NULL) {
8474 			dir->dir_dreachable = 0;
8475 
8476 			/* move on to next dst addr */
8477 			continue;
8478 		}
8479 		dir->dir_dreachable = 1;
8480 
8481 		ire_ipif = ire->ire_ipif;
8482 		if (ire_ipif == NULL)
8483 			goto next_dst;
8484 
8485 		/*
8486 		 * We expect to get back an interface ire or a
8487 		 * gateway ire cache entry.  For both types, the
8488 		 * output interface is ire_ipif->ipif_ill.
8489 		 */
8490 		dst_ill = ire_ipif->ipif_ill;
8491 		dir->dir_dmactype = dst_ill->ill_mactype;
8492 
8493 		if (isipv4) {
8494 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8495 		} else {
8496 			src_ipif = ipif_select_source_v6(dst_ill,
8497 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8498 			    zoneid);
8499 		}
8500 		if (src_ipif == NULL)
8501 			goto next_dst;
8502 
8503 		*saddr = src_ipif->ipif_v6lcl_addr;
8504 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8505 		slabel = ip6_asp_lookup(saddr, NULL);
8506 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8507 		dir->dir_sdeprecated =
8508 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8509 		ipif_refrele(src_ipif);
8510 next_dst:
8511 		ire_refrele(ire);
8512 	}
8513 	miocack(q, mp, iocp->ioc_count, 0);
8514 }
8515 
8516 
8517 /*
8518  * Check if this is an address assigned to this machine.
8519  * Skips interfaces that are down by using ire checks.
8520  * Translates mapped addresses to v4 addresses and then
8521  * treats them as such, returning true if the v4 address
8522  * associated with this mapped address is configured.
8523  * Note: Applications will have to be careful what they do
8524  * with the response; use of mapped addresses limits
8525  * what can be done with the socket, especially with
8526  * respect to socket options and ioctls - neither IPv4
8527  * options nor IPv6 sticky options/ancillary data options
8528  * may be used.
8529  */
8530 /* ARGSUSED */
8531 int
8532 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8533     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8534 {
8535 	struct sioc_addrreq *sia;
8536 	sin_t *sin;
8537 	ire_t *ire;
8538 	mblk_t *mp1;
8539 	zoneid_t zoneid;
8540 
8541 	ip1dbg(("ip_sioctl_tmyaddr"));
8542 
8543 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8544 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8545 
8546 	/* Existence verified in ip_wput_nondata */
8547 	mp1 = mp->b_cont->b_cont;
8548 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8549 	sin = (sin_t *)&sia->sa_addr;
8550 	switch (sin->sin_family) {
8551 	case AF_INET6: {
8552 		sin6_t *sin6 = (sin6_t *)sin;
8553 
8554 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8555 			ipaddr_t v4_addr;
8556 
8557 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8558 			    v4_addr);
8559 			ire = ire_ctable_lookup(v4_addr, 0,
8560 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8561 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8562 		} else {
8563 			in6_addr_t v6addr;
8564 
8565 			v6addr = sin6->sin6_addr;
8566 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8567 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8568 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8569 		}
8570 		break;
8571 	}
8572 	case AF_INET: {
8573 		ipaddr_t v4addr;
8574 
8575 		v4addr = sin->sin_addr.s_addr;
8576 		ire = ire_ctable_lookup(v4addr, 0,
8577 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8578 		    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8579 		break;
8580 	}
8581 	default:
8582 		return (EAFNOSUPPORT);
8583 	}
8584 	if (ire != NULL) {
8585 		sia->sa_res = 1;
8586 		ire_refrele(ire);
8587 	} else {
8588 		sia->sa_res = 0;
8589 	}
8590 	return (0);
8591 }
8592 
8593 /*
8594  * Check if this is an address assigned on-link i.e. neighbor,
8595  * and makes sure it's reachable from the current zone.
8596  * Returns true for my addresses as well.
8597  * Translates mapped addresses to v4 addresses and then
8598  * treats them as such, returning true if the v4 address
8599  * associated with this mapped address is configured.
8600  * Note: Applications will have to be careful what they do
8601  * with the response; use of mapped addresses limits
8602  * what can be done with the socket, especially with
8603  * respect to socket options and ioctls - neither IPv4
8604  * options nor IPv6 sticky options/ancillary data options
8605  * may be used.
8606  */
8607 /* ARGSUSED */
8608 int
8609 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8610     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8611 {
8612 	struct sioc_addrreq *sia;
8613 	sin_t *sin;
8614 	mblk_t	*mp1;
8615 	ire_t *ire = NULL;
8616 	zoneid_t zoneid;
8617 
8618 	ip1dbg(("ip_sioctl_tonlink"));
8619 
8620 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8621 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8622 
8623 	/* Existence verified in ip_wput_nondata */
8624 	mp1 = mp->b_cont->b_cont;
8625 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8626 	sin = (sin_t *)&sia->sa_addr;
8627 
8628 	/*
8629 	 * Match addresses with a zero gateway field to avoid
8630 	 * routes going through a router.
8631 	 * Exclude broadcast and multicast addresses.
8632 	 */
8633 	switch (sin->sin_family) {
8634 	case AF_INET6: {
8635 		sin6_t *sin6 = (sin6_t *)sin;
8636 
8637 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8638 			ipaddr_t v4_addr;
8639 
8640 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8641 			    v4_addr);
8642 			if (!CLASSD(v4_addr)) {
8643 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8644 				    NULL, NULL, zoneid, MATCH_IRE_GW);
8645 			}
8646 		} else {
8647 			in6_addr_t v6addr;
8648 			in6_addr_t v6gw;
8649 
8650 			v6addr = sin6->sin6_addr;
8651 			v6gw = ipv6_all_zeros;
8652 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8653 				ire = ire_route_lookup_v6(&v6addr, 0,
8654 				    &v6gw, 0, NULL, NULL, zoneid,
8655 				    MATCH_IRE_GW);
8656 			}
8657 		}
8658 		break;
8659 	}
8660 	case AF_INET: {
8661 		ipaddr_t v4addr;
8662 
8663 		v4addr = sin->sin_addr.s_addr;
8664 		if (!CLASSD(v4addr)) {
8665 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8666 			    NULL, NULL, zoneid, MATCH_IRE_GW);
8667 		}
8668 		break;
8669 	}
8670 	default:
8671 		return (EAFNOSUPPORT);
8672 	}
8673 	sia->sa_res = 0;
8674 	if (ire != NULL) {
8675 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8676 		    IRE_LOCAL|IRE_LOOPBACK)) {
8677 			sia->sa_res = 1;
8678 		}
8679 		ire_refrele(ire);
8680 	}
8681 	return (0);
8682 }
8683 
8684 /*
8685  * TBD: implement when kernel maintaines a list of site prefixes.
8686  */
8687 /* ARGSUSED */
8688 int
8689 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8690     ip_ioctl_cmd_t *ipip, void *ifreq)
8691 {
8692 	return (ENXIO);
8693 }
8694 
8695 /* ARGSUSED */
8696 int
8697 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8698     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8699 {
8700 	ill_t  		*ill;
8701 	mblk_t		*mp1;
8702 	conn_t		*connp;
8703 	boolean_t	success;
8704 
8705 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8706 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8707 	/* ioctl comes down on an conn */
8708 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8709 	connp = Q_TO_CONN(q);
8710 
8711 	mp->b_datap->db_type = M_IOCTL;
8712 
8713 	/*
8714 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8715 	 * The original mp contains contaminated b_next values due to 'mi',
8716 	 * which is needed to do the mi_copy_done. Unfortunately if we
8717 	 * send down the original mblk itself and if we are popped due to an
8718 	 * an unplumb before the response comes back from tunnel,
8719 	 * the streamhead (which does a freemsg) will see this contaminated
8720 	 * message and the assertion in freemsg about non-null b_next/b_prev
8721 	 * will panic a DEBUG kernel.
8722 	 */
8723 	mp1 = copymsg(mp);
8724 	if (mp1 == NULL)
8725 		return (ENOMEM);
8726 
8727 	ill = ipif->ipif_ill;
8728 	mutex_enter(&connp->conn_lock);
8729 	mutex_enter(&ill->ill_lock);
8730 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8731 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8732 		    mp, 0);
8733 	} else {
8734 		success = ill_pending_mp_add(ill, connp, mp);
8735 	}
8736 	mutex_exit(&ill->ill_lock);
8737 	mutex_exit(&connp->conn_lock);
8738 
8739 	if (success) {
8740 		ip1dbg(("sending down tunparam request "));
8741 		putnext(ill->ill_wq, mp1);
8742 		return (EINPROGRESS);
8743 	} else {
8744 		/* The conn has started closing */
8745 		freemsg(mp1);
8746 		return (EINTR);
8747 	}
8748 }
8749 
8750 static int
8751 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8752     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8753 {
8754 	mblk_t *mp1;
8755 	mblk_t *mp2;
8756 	mblk_t *pending_mp;
8757 	ipaddr_t ipaddr;
8758 	area_t *area;
8759 	struct iocblk *iocp;
8760 	conn_t *connp;
8761 	struct arpreq *ar;
8762 	struct xarpreq *xar;
8763 	boolean_t success;
8764 	int flags, alength;
8765 	char *lladdr;
8766 
8767 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8768 	connp = Q_TO_CONN(q);
8769 
8770 	iocp = (struct iocblk *)mp->b_rptr;
8771 	/*
8772 	 * ill has already been set depending on whether
8773 	 * bsd style or interface style ioctl.
8774 	 */
8775 	ASSERT(ill != NULL);
8776 
8777 	/*
8778 	 * Is this one of the new SIOC*XARP ioctls?
8779 	 */
8780 	if (x_arp_ioctl) {
8781 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8782 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8783 		ar = NULL;
8784 
8785 		flags = xar->xarp_flags;
8786 		lladdr = LLADDR(&xar->xarp_ha);
8787 		/*
8788 		 * Validate against user's link layer address length
8789 		 * input and name and addr length limits.
8790 		 */
8791 		alength = ill->ill_phys_addr_length;
8792 		if (iocp->ioc_cmd == SIOCSXARP) {
8793 			if (alength != xar->xarp_ha.sdl_alen ||
8794 			    (alength + xar->xarp_ha.sdl_nlen >
8795 			    sizeof (xar->xarp_ha.sdl_data)))
8796 				return (EINVAL);
8797 		}
8798 	} else {
8799 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8800 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8801 		xar = NULL;
8802 
8803 		flags = ar->arp_flags;
8804 		lladdr = ar->arp_ha.sa_data;
8805 		/*
8806 		 * Theoretically, the sa_family could tell us what link
8807 		 * layer type this operation is trying to deal with. By
8808 		 * common usage AF_UNSPEC means ethernet. We'll assume
8809 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8810 		 * for now. Our new SIOC*XARP ioctls can be used more
8811 		 * generally.
8812 		 *
8813 		 * If the underlying media happens to have a non 6 byte
8814 		 * address, arp module will fail set/get, but the del
8815 		 * operation will succeed.
8816 		 */
8817 		alength = 6;
8818 		if ((iocp->ioc_cmd != SIOCDARP) &&
8819 		    (alength != ill->ill_phys_addr_length)) {
8820 			return (EINVAL);
8821 		}
8822 	}
8823 
8824 	/*
8825 	 * We are going to pass up to ARP a packet chain that looks
8826 	 * like:
8827 	 *
8828 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
8829 	 *
8830 	 * Get a copy of the original IOCTL mblk to head the chain,
8831 	 * to be sent up (in mp1). Also get another copy to store
8832 	 * in the ill_pending_mp list, for matching the response
8833 	 * when it comes back from ARP.
8834 	 */
8835 	mp1 = copyb(mp);
8836 	pending_mp = copymsg(mp);
8837 	if (mp1 == NULL || pending_mp == NULL) {
8838 		if (mp1 != NULL)
8839 			freeb(mp1);
8840 		if (pending_mp != NULL)
8841 			ip_ioctl_freemsg(pending_mp);
8842 		return (ENOMEM);
8843 	}
8844 
8845 	ipaddr = sin->sin_addr.s_addr;
8846 
8847 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
8848 	    (caddr_t)&ipaddr);
8849 	if (mp2 == NULL) {
8850 		freeb(mp1);
8851 		ip_ioctl_freemsg(pending_mp);
8852 		return (ENOMEM);
8853 	}
8854 	/* Put together the chain. */
8855 	mp1->b_cont = mp2;
8856 	mp1->b_datap->db_type = M_IOCTL;
8857 	mp2->b_cont = mp;
8858 	mp2->b_datap->db_type = M_DATA;
8859 
8860 	iocp = (struct iocblk *)mp1->b_rptr;
8861 
8862 	/*
8863 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
8864 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
8865 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
8866 	 * ioc_count field; set ioc_count to be correct.
8867 	 */
8868 	iocp->ioc_count = MBLKL(mp1->b_cont);
8869 
8870 	/*
8871 	 * Set the proper command in the ARP message.
8872 	 * Convert the SIOC{G|S|D}ARP calls into our
8873 	 * AR_ENTRY_xxx calls.
8874 	 */
8875 	area = (area_t *)mp2->b_rptr;
8876 	switch (iocp->ioc_cmd) {
8877 	case SIOCDARP:
8878 	case SIOCDXARP:
8879 		/*
8880 		 * We defer deleting the corresponding IRE until
8881 		 * we return from arp.
8882 		 */
8883 		area->area_cmd = AR_ENTRY_DELETE;
8884 		area->area_proto_mask_offset = 0;
8885 		break;
8886 	case SIOCGARP:
8887 	case SIOCGXARP:
8888 		area->area_cmd = AR_ENTRY_SQUERY;
8889 		area->area_proto_mask_offset = 0;
8890 		break;
8891 	case SIOCSARP:
8892 	case SIOCSXARP: {
8893 		/*
8894 		 * Delete the corresponding ire to make sure IP will
8895 		 * pick up any change from arp.
8896 		 */
8897 		if (!if_arp_ioctl) {
8898 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
8899 			break;
8900 		} else {
8901 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8902 			if (ipif != NULL) {
8903 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
8904 				ipif_refrele(ipif);
8905 			}
8906 			break;
8907 		}
8908 	}
8909 	}
8910 	iocp->ioc_cmd = area->area_cmd;
8911 
8912 	/*
8913 	 * Before sending 'mp' to ARP, we have to clear the b_next
8914 	 * and b_prev. Otherwise if STREAMS encounters such a message
8915 	 * in freemsg(), (because ARP can close any time) it can cause
8916 	 * a panic. But mi code needs the b_next and b_prev values of
8917 	 * mp->b_cont, to complete the ioctl. So we store it here
8918 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
8919 	 * when the response comes down from ARP.
8920 	 */
8921 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
8922 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
8923 	mp->b_cont->b_next = NULL;
8924 	mp->b_cont->b_prev = NULL;
8925 
8926 	mutex_enter(&connp->conn_lock);
8927 	mutex_enter(&ill->ill_lock);
8928 	/* conn has not yet started closing, hence this can't fail */
8929 	success = ill_pending_mp_add(ill, connp, pending_mp);
8930 	ASSERT(success);
8931 	mutex_exit(&ill->ill_lock);
8932 	mutex_exit(&connp->conn_lock);
8933 
8934 	/*
8935 	 * Fill in the rest of the ARP operation fields.
8936 	 */
8937 	area->area_hw_addr_length = alength;
8938 	bcopy(lladdr,
8939 	    (char *)area + area->area_hw_addr_offset,
8940 	    area->area_hw_addr_length);
8941 	/* Translate the flags. */
8942 	if (flags & ATF_PERM)
8943 		area->area_flags |= ACE_F_PERMANENT;
8944 	if (flags & ATF_PUBL)
8945 		area->area_flags |= ACE_F_PUBLISH;
8946 
8947 	/*
8948 	 * Up to ARP it goes.  The response will come
8949 	 * back in ip_wput as an M_IOCACK message, and
8950 	 * will be handed to ip_sioctl_iocack for
8951 	 * completion.
8952 	 */
8953 	putnext(ill->ill_rq, mp1);
8954 	return (EINPROGRESS);
8955 }
8956 
8957 /* ARGSUSED */
8958 int
8959 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8960     ip_ioctl_cmd_t *ipip, void *ifreq)
8961 {
8962 	struct xarpreq *xar;
8963 	boolean_t isv6;
8964 	mblk_t	*mp1;
8965 	int	err;
8966 	conn_t	*connp;
8967 	int ifnamelen;
8968 	ire_t	*ire = NULL;
8969 	ill_t	*ill = NULL;
8970 	struct sockaddr_in *sin;
8971 	boolean_t if_arp_ioctl = B_FALSE;
8972 
8973 	/* ioctl comes down on an conn */
8974 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8975 	connp = Q_TO_CONN(q);
8976 	isv6 = connp->conn_af_isv6;
8977 
8978 	/* Existance verified in ip_wput_nondata */
8979 	mp1 = mp->b_cont->b_cont;
8980 
8981 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
8982 	xar = (struct xarpreq *)mp1->b_rptr;
8983 	sin = (sin_t *)&xar->xarp_pa;
8984 
8985 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
8986 	    (xar->xarp_pa.ss_family != AF_INET))
8987 		return (ENXIO);
8988 
8989 	ifnamelen = xar->xarp_ha.sdl_nlen;
8990 	if (ifnamelen != 0) {
8991 		char	*cptr, cval;
8992 
8993 		if (ifnamelen >= LIFNAMSIZ)
8994 			return (EINVAL);
8995 
8996 		/*
8997 		 * Instead of bcopying a bunch of bytes,
8998 		 * null-terminate the string in-situ.
8999 		 */
9000 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9001 		cval = *cptr;
9002 		*cptr = '\0';
9003 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9004 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9005 		    &err, NULL);
9006 		*cptr = cval;
9007 		if (ill == NULL)
9008 			return (err);
9009 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9010 			ill_refrele(ill);
9011 			return (ENXIO);
9012 		}
9013 
9014 		if_arp_ioctl = B_TRUE;
9015 	} else {
9016 		/*
9017 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9018 		 * as an extended BSD ioctl. The kernel uses the IP address
9019 		 * to figure out the network interface.
9020 		 */
9021 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9022 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9023 		    ((ill = ire_to_ill(ire)) == NULL)) {
9024 			if (ire != NULL)
9025 				ire_refrele(ire);
9026 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9027 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9028 			    MATCH_IRE_TYPE);
9029 			if ((ire == NULL) ||
9030 			    ((ill = ire_to_ill(ire)) == NULL)) {
9031 				if (ire != NULL)
9032 					ire_refrele(ire);
9033 				return (ENXIO);
9034 			}
9035 		}
9036 		ASSERT(ire != NULL && ill != NULL);
9037 	}
9038 
9039 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9040 	if (if_arp_ioctl)
9041 		ill_refrele(ill);
9042 	if (ire != NULL)
9043 		ire_refrele(ire);
9044 
9045 	return (err);
9046 }
9047 
9048 /*
9049  * ARP IOCTLs.
9050  * How does IP get in the business of fronting ARP configuration/queries?
9051  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9052  * are by tradition passed in through a datagram socket.  That lands in IP.
9053  * As it happens, this is just as well since the interface is quite crude in
9054  * that it passes in no information about protocol or hardware types, or
9055  * interface association.  After making the protocol assumption, IP is in
9056  * the position to look up the name of the ILL, which ARP will need, and
9057  * format a request that can be handled by ARP.	 The request is passed up
9058  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9059  * back a response.  ARP supports its own set of more general IOCTLs, in
9060  * case anyone is interested.
9061  */
9062 /* ARGSUSED */
9063 int
9064 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9065     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9066 {
9067 	struct arpreq *ar;
9068 	struct sockaddr_in *sin;
9069 	ire_t	*ire;
9070 	boolean_t isv6;
9071 	mblk_t	*mp1;
9072 	int	err;
9073 	conn_t	*connp;
9074 	ill_t	*ill;
9075 
9076 	/* ioctl comes down on an conn */
9077 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9078 	connp = Q_TO_CONN(q);
9079 	isv6 = connp->conn_af_isv6;
9080 	if (isv6)
9081 		return (ENXIO);
9082 
9083 	/* Existance verified in ip_wput_nondata */
9084 	mp1 = mp->b_cont->b_cont;
9085 
9086 	ar = (struct arpreq *)mp1->b_rptr;
9087 	sin = (sin_t *)&ar->arp_pa;
9088 
9089 	/*
9090 	 * We need to let ARP know on which interface the IP
9091 	 * address has an ARP mapping. In the IPMP case, a
9092 	 * simple forwarding table lookup will return the
9093 	 * IRE_IF_RESOLVER for the first interface in the group,
9094 	 * which might not be the interface on which the
9095 	 * requested IP address was resolved due to the ill
9096 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9097 	 * So we do a cache table lookup first: if the IRE cache
9098 	 * entry for the IP address is still there, it will
9099 	 * contain the ill pointer for the right interface, so
9100 	 * we use that. If the cache entry has been flushed, we
9101 	 * fall back to the forwarding table lookup. This should
9102 	 * be rare enough since IRE cache entries have a longer
9103 	 * life expectancy than ARP cache entries.
9104 	 */
9105 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9106 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9107 	    ((ill = ire_to_ill(ire)) == NULL)) {
9108 		if (ire != NULL)
9109 			ire_refrele(ire);
9110 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9111 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9112 		    MATCH_IRE_TYPE);
9113 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9114 			if (ire != NULL)
9115 				ire_refrele(ire);
9116 			return (ENXIO);
9117 		}
9118 	}
9119 	ASSERT(ire != NULL && ill != NULL);
9120 
9121 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9122 	ire_refrele(ire);
9123 	return (err);
9124 }
9125 
9126 /*
9127  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9128  * atomically set/clear the muxids. Also complete the ioctl by acking or
9129  * naking it.  Note that the code is structured such that the link type,
9130  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9131  * its clones use the persistent link, while pppd(1M) and perhaps many
9132  * other daemons may use non-persistent link.  When combined with some
9133  * ill_t states, linking and unlinking lower streams may be used as
9134  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9135  */
9136 /* ARGSUSED */
9137 void
9138 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9139 {
9140 	mblk_t *mp1;
9141 	mblk_t *mp2;
9142 	struct linkblk *li;
9143 	queue_t	*ipwq;
9144 	char	*name;
9145 	struct qinit *qinfo;
9146 	struct ipmx_s *ipmxp;
9147 	ill_t	*ill = NULL;
9148 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9149 	int	err = 0;
9150 	boolean_t	entered_ipsq = B_FALSE;
9151 	boolean_t islink;
9152 	queue_t *dwq = NULL;
9153 
9154 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9155 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9156 
9157 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9158 	    B_TRUE : B_FALSE;
9159 
9160 	mp1 = mp->b_cont;	/* This is the linkblk info */
9161 	li = (struct linkblk *)mp1->b_rptr;
9162 
9163 	/*
9164 	 * ARP has added this special mblk, and the utility is asking us
9165 	 * to perform consistency checks, and also atomically set the
9166 	 * muxid. Ifconfig is an example.  It achieves this by using
9167 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9168 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9169 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9170 	 * and other comments in this routine for more details.
9171 	 */
9172 	mp2 = mp1->b_cont;	/* This is added by ARP */
9173 
9174 	/*
9175 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9176 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9177 	 * get the special mblk above.  For backward compatibility, we just
9178 	 * return success.  The utility will use SIOCSLIFMUXID to store
9179 	 * the muxids.  This is not atomic, and can leave the streams
9180 	 * unplumbable if the utility is interrrupted, before it does the
9181 	 * SIOCSLIFMUXID.
9182 	 */
9183 	if (mp2 == NULL) {
9184 		/*
9185 		 * At this point we don't know whether or not this is the
9186 		 * IP module stream or the ARP device stream.  We need to
9187 		 * walk the lower stream in order to find this out, since
9188 		 * the capability negotiation is done only on the IP module
9189 		 * stream.  IP module instance is identified by the module
9190 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9191 		 * STREAMS ensures that the lower stream (l_qbot) will not
9192 		 * vanish until this ioctl completes. So we can safely walk
9193 		 * the stream or refer to the q_ptr.
9194 		 */
9195 		ipwq = li->l_qbot;
9196 		while (ipwq != NULL) {
9197 			qinfo = ipwq->q_qinfo;
9198 			name = qinfo->qi_minfo->mi_idname;
9199 			if (name != NULL && name[0] != NULL &&
9200 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9201 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9202 			    (ipwq->q_next != NULL)) {
9203 				break;
9204 			}
9205 			ipwq = ipwq->q_next;
9206 		}
9207 		/*
9208 		 * This looks like an IP module stream, so trigger
9209 		 * the capability reset or re-negotiation if necessary.
9210 		 */
9211 		if (ipwq != NULL) {
9212 			ill = ipwq->q_ptr;
9213 			ASSERT(ill != NULL);
9214 
9215 			if (ipsq == NULL) {
9216 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9217 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9218 				if (ipsq == NULL)
9219 					return;
9220 				entered_ipsq = B_TRUE;
9221 			}
9222 			ASSERT(IAM_WRITER_ILL(ill));
9223 			/*
9224 			 * Store the upper read queue of the module
9225 			 * immediately below IP, and count the total
9226 			 * number of lower modules.  Do this only
9227 			 * for I_PLINK or I_LINK event.
9228 			 */
9229 			ill->ill_lmod_rq = NULL;
9230 			ill->ill_lmod_cnt = 0;
9231 			if (islink && (dwq = ipwq->q_next) != NULL) {
9232 				ill->ill_lmod_rq = RD(dwq);
9233 
9234 				while (dwq != NULL) {
9235 					ill->ill_lmod_cnt++;
9236 					dwq = dwq->q_next;
9237 				}
9238 			}
9239 			/*
9240 			 * There's no point in resetting or re-negotiating if
9241 			 * we are not bound to the driver, so only do this if
9242 			 * the DLPI state is idle (up); we assume such state
9243 			 * since ill_ipif_up_count gets incremented in
9244 			 * ipif_up_done(), which is after we are bound to the
9245 			 * driver.  Note that in the case of logical
9246 			 * interfaces, IP won't rebind to the driver unless
9247 			 * the ill_ipif_up_count is 0, meaning that all other
9248 			 * IP interfaces (including the main ipif) are in the
9249 			 * down state.  Because of this, we use such counter
9250 			 * as an indicator, instead of relying on the IPIF_UP
9251 			 * flag, which is per ipif instance.
9252 			 */
9253 			if (ill->ill_ipif_up_count > 0) {
9254 				if (islink)
9255 					ill_capability_probe(ill);
9256 				else
9257 					ill_capability_reset(ill);
9258 			}
9259 		}
9260 		goto done;
9261 	}
9262 
9263 	/*
9264 	 * This is an I_{P}LINK sent down by ifconfig on
9265 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9266 	 * giving more info. STREAMS ensures that the lower
9267 	 * stream (l_qbot) will not vanish until this ioctl
9268 	 * completes. So we can safely walk the stream or refer
9269 	 * to the q_ptr.
9270 	 */
9271 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9272 	if (ipmxp->ipmx_arpdev_stream) {
9273 		/*
9274 		 * The operation is occuring on the arp-device
9275 		 * stream.
9276 		 */
9277 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9278 		    q, mp, ip_sioctl_plink, &err, NULL);
9279 		if (ill == NULL) {
9280 			if (err == EINPROGRESS) {
9281 				return;
9282 			} else {
9283 				err = EINVAL;
9284 				goto done;
9285 			}
9286 		}
9287 
9288 		if (ipsq == NULL) {
9289 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9290 			    NEW_OP, B_TRUE);
9291 			if (ipsq == NULL) {
9292 				ill_refrele(ill);
9293 				return;
9294 			}
9295 			entered_ipsq = B_TRUE;
9296 		}
9297 		ASSERT(IAM_WRITER_ILL(ill));
9298 		ill_refrele(ill);
9299 		/*
9300 		 * To ensure consistency between IP and ARP,
9301 		 * the following LIFO scheme is used in
9302 		 * plink/punlink. (IP first, ARP last).
9303 		 * This is because the muxid's are stored
9304 		 * in the IP stream on the ill.
9305 		 *
9306 		 * I_{P}LINK: ifconfig plinks the IP stream before
9307 		 * plinking the ARP stream. On an arp-dev
9308 		 * stream, IP checks that it is not yet
9309 		 * plinked, and it also checks that the
9310 		 * corresponding IP stream is already plinked.
9311 		 *
9312 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9313 		 * before punlinking the IP stream. IP does
9314 		 * not allow punlink of the IP stream unless
9315 		 * the arp stream has been punlinked.
9316 		 *
9317 		 */
9318 		if ((islink &&
9319 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9320 		    (!islink &&
9321 		    ill->ill_arp_muxid != li->l_index)) {
9322 			err = EINVAL;
9323 			goto done;
9324 		}
9325 		if (islink) {
9326 			ill->ill_arp_muxid = li->l_index;
9327 		} else {
9328 			ill->ill_arp_muxid = 0;
9329 		}
9330 	} else {
9331 		/*
9332 		 * This must be the IP module stream with or
9333 		 * without arp. Walk the stream and locate the
9334 		 * IP module. An IP module instance is
9335 		 * identified by the module name IP, non-null
9336 		 * q_next, and it's wput not being ip_lwput.
9337 		 */
9338 		ipwq = li->l_qbot;
9339 		while (ipwq != NULL) {
9340 			qinfo = ipwq->q_qinfo;
9341 			name = qinfo->qi_minfo->mi_idname;
9342 			if (name != NULL && name[0] != NULL &&
9343 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9344 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9345 			    (ipwq->q_next != NULL)) {
9346 				break;
9347 			}
9348 			ipwq = ipwq->q_next;
9349 		}
9350 		if (ipwq != NULL) {
9351 			ill = ipwq->q_ptr;
9352 			ASSERT(ill != NULL);
9353 
9354 			if (ipsq == NULL) {
9355 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9356 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9357 				if (ipsq == NULL)
9358 					return;
9359 				entered_ipsq = B_TRUE;
9360 			}
9361 			ASSERT(IAM_WRITER_ILL(ill));
9362 			/*
9363 			 * Return error if the ip_mux_id is
9364 			 * non-zero and command is I_{P}LINK.
9365 			 * If command is I_{P}UNLINK, return
9366 			 * error if the arp-devstr is not
9367 			 * yet punlinked.
9368 			 */
9369 			if ((islink && ill->ill_ip_muxid != 0) ||
9370 			    (!islink && ill->ill_arp_muxid != 0)) {
9371 				err = EINVAL;
9372 				goto done;
9373 			}
9374 			ill->ill_lmod_rq = NULL;
9375 			ill->ill_lmod_cnt = 0;
9376 			if (islink) {
9377 				/*
9378 				 * Store the upper read queue of the module
9379 				 * immediately below IP, and count the total
9380 				 * number of lower modules.
9381 				 */
9382 				if ((dwq = ipwq->q_next) != NULL) {
9383 					ill->ill_lmod_rq = RD(dwq);
9384 
9385 					while (dwq != NULL) {
9386 						ill->ill_lmod_cnt++;
9387 						dwq = dwq->q_next;
9388 					}
9389 				}
9390 				ill->ill_ip_muxid = li->l_index;
9391 			} else {
9392 				ill->ill_ip_muxid = 0;
9393 			}
9394 
9395 			/*
9396 			 * See comments above about resetting/re-
9397 			 * negotiating driver sub-capabilities.
9398 			 */
9399 			if (ill->ill_ipif_up_count > 0) {
9400 				if (islink)
9401 					ill_capability_probe(ill);
9402 				else
9403 					ill_capability_reset(ill);
9404 			}
9405 		}
9406 	}
9407 done:
9408 	iocp->ioc_count = 0;
9409 	iocp->ioc_error = err;
9410 	if (err == 0)
9411 		mp->b_datap->db_type = M_IOCACK;
9412 	else
9413 		mp->b_datap->db_type = M_IOCNAK;
9414 	qreply(q, mp);
9415 
9416 	/* Conn was refheld in ip_sioctl_copyin_setup */
9417 	if (CONN_Q(q))
9418 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9419 	if (entered_ipsq)
9420 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9421 }
9422 
9423 /*
9424  * Search the ioctl command in the ioctl tables and return a pointer
9425  * to the ioctl command information. The ioctl command tables are
9426  * static and fully populated at compile time.
9427  */
9428 ip_ioctl_cmd_t *
9429 ip_sioctl_lookup(int ioc_cmd)
9430 {
9431 	int index;
9432 	ip_ioctl_cmd_t *ipip;
9433 	ip_ioctl_cmd_t *ipip_end;
9434 
9435 	if (ioc_cmd == IPI_DONTCARE)
9436 		return (NULL);
9437 
9438 	/*
9439 	 * Do a 2 step search. First search the indexed table
9440 	 * based on the least significant byte of the ioctl cmd.
9441 	 * If we don't find a match, then search the misc table
9442 	 * serially.
9443 	 */
9444 	index = ioc_cmd & 0xFF;
9445 	if (index < ip_ndx_ioctl_count) {
9446 		ipip = &ip_ndx_ioctl_table[index];
9447 		if (ipip->ipi_cmd == ioc_cmd) {
9448 			/* Found a match in the ndx table */
9449 			return (ipip);
9450 		}
9451 	}
9452 
9453 	/* Search the misc table */
9454 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9455 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9456 		if (ipip->ipi_cmd == ioc_cmd)
9457 			/* Found a match in the misc table */
9458 			return (ipip);
9459 	}
9460 
9461 	return (NULL);
9462 }
9463 
9464 /*
9465  * Wrapper function for resuming deferred ioctl processing
9466  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9467  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9468  */
9469 /* ARGSUSED */
9470 void
9471 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9472     void *dummy_arg)
9473 {
9474 	ip_sioctl_copyin_setup(q, mp);
9475 }
9476 
9477 /*
9478  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9479  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9480  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9481  * We establish here the size of the block to be copied in.  mi_copyin
9482  * arranges for this to happen, an processing continues in ip_wput with
9483  * an M_IOCDATA message.
9484  */
9485 void
9486 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9487 {
9488 	int	copyin_size;
9489 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9490 	ip_ioctl_cmd_t *ipip;
9491 	cred_t *cr;
9492 
9493 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9494 	if (ipip == NULL) {
9495 		/*
9496 		 * The ioctl is not one we understand or own.
9497 		 * Pass it along to be processed down stream,
9498 		 * if this is a module instance of IP, else nak
9499 		 * the ioctl.
9500 		 */
9501 		if (q->q_next == NULL) {
9502 			goto nak;
9503 		} else {
9504 			putnext(q, mp);
9505 			return;
9506 		}
9507 	}
9508 
9509 	/*
9510 	 * If this is deferred, then we will do all the checks when we
9511 	 * come back.
9512 	 */
9513 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9514 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9515 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9516 		return;
9517 	}
9518 
9519 	/*
9520 	 * Only allow a very small subset of IP ioctls on this stream if
9521 	 * IP is a module and not a driver. Allowing ioctls to be processed
9522 	 * in this case may cause assert failures or data corruption.
9523 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9524 	 * ioctls allowed on an IP module stream, after which this stream
9525 	 * normally becomes a multiplexor (at which time the stream head
9526 	 * will fail all ioctls).
9527 	 */
9528 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9529 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9530 			/*
9531 			 * Pass common Streams ioctls which the IP
9532 			 * module does not own or consume along to
9533 			 * be processed down stream.
9534 			 */
9535 			putnext(q, mp);
9536 			return;
9537 		} else {
9538 			goto nak;
9539 		}
9540 	}
9541 
9542 	/* Make sure we have ioctl data to process. */
9543 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9544 		goto nak;
9545 
9546 	/*
9547 	 * Prefer dblk credential over ioctl credential; some synthesized
9548 	 * ioctls have kcred set because there's no way to crhold()
9549 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9550 	 * the framework; the caller of ioctl needs to hold the reference
9551 	 * for the duration of the call).
9552 	 */
9553 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9554 
9555 	/* Make sure normal users don't send down privileged ioctls */
9556 	if ((ipip->ipi_flags & IPI_PRIV) &&
9557 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9558 		/* We checked the privilege earlier but log it here */
9559 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9560 		return;
9561 	}
9562 
9563 	/*
9564 	 * The ioctl command tables can only encode fixed length
9565 	 * ioctl data. If the length is variable, the table will
9566 	 * encode the length as zero. Such special cases are handled
9567 	 * below in the switch.
9568 	 */
9569 	if (ipip->ipi_copyin_size != 0) {
9570 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9571 		return;
9572 	}
9573 
9574 	switch (iocp->ioc_cmd) {
9575 	case O_SIOCGIFCONF:
9576 	case SIOCGIFCONF:
9577 		/*
9578 		 * This IOCTL is hilarious.  See comments in
9579 		 * ip_sioctl_get_ifconf for the story.
9580 		 */
9581 		if (iocp->ioc_count == TRANSPARENT)
9582 			copyin_size = SIZEOF_STRUCT(ifconf,
9583 			    iocp->ioc_flag);
9584 		else
9585 			copyin_size = iocp->ioc_count;
9586 		mi_copyin(q, mp, NULL, copyin_size);
9587 		return;
9588 
9589 	case O_SIOCGLIFCONF:
9590 	case SIOCGLIFCONF:
9591 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9592 		mi_copyin(q, mp, NULL, copyin_size);
9593 		return;
9594 
9595 	case SIOCGLIFSRCOF:
9596 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9597 		mi_copyin(q, mp, NULL, copyin_size);
9598 		return;
9599 	case SIOCGIP6ADDRPOLICY:
9600 		ip_sioctl_ip6addrpolicy(q, mp);
9601 		ip6_asp_table_refrele();
9602 		return;
9603 
9604 	case SIOCSIP6ADDRPOLICY:
9605 		ip_sioctl_ip6addrpolicy(q, mp);
9606 		return;
9607 
9608 	case SIOCGDSTINFO:
9609 		ip_sioctl_dstinfo(q, mp);
9610 		ip6_asp_table_refrele();
9611 		return;
9612 
9613 	case I_PLINK:
9614 	case I_PUNLINK:
9615 	case I_LINK:
9616 	case I_UNLINK:
9617 		/*
9618 		 * We treat non-persistent link similarly as the persistent
9619 		 * link case, in terms of plumbing/unplumbing, as well as
9620 		 * dynamic re-plumbing events indicator.  See comments
9621 		 * in ip_sioctl_plink() for more.
9622 		 *
9623 		 * Request can be enqueued in the 'ipsq' while waiting
9624 		 * to become exclusive. So bump up the conn ref.
9625 		 */
9626 		if (CONN_Q(q))
9627 			CONN_INC_REF(Q_TO_CONN(q));
9628 		ip_sioctl_plink(NULL, q, mp, NULL);
9629 		return;
9630 
9631 	case ND_GET:
9632 	case ND_SET:
9633 		/*
9634 		 * Use of the nd table requires holding the reader lock.
9635 		 * Modifying the nd table thru nd_load/nd_unload requires
9636 		 * the writer lock.
9637 		 */
9638 		rw_enter(&ip_g_nd_lock, RW_READER);
9639 		if (nd_getset(q, ip_g_nd, mp)) {
9640 			rw_exit(&ip_g_nd_lock);
9641 
9642 			if (iocp->ioc_error)
9643 				iocp->ioc_count = 0;
9644 			mp->b_datap->db_type = M_IOCACK;
9645 			qreply(q, mp);
9646 			return;
9647 		}
9648 		rw_exit(&ip_g_nd_lock);
9649 		/*
9650 		 * We don't understand this subioctl of ND_GET / ND_SET.
9651 		 * Maybe intended for some driver / module below us
9652 		 */
9653 		if (q->q_next) {
9654 			putnext(q, mp);
9655 		} else {
9656 			iocp->ioc_error = ENOENT;
9657 			mp->b_datap->db_type = M_IOCNAK;
9658 			iocp->ioc_count = 0;
9659 			qreply(q, mp);
9660 		}
9661 		return;
9662 
9663 	case IP_IOCTL:
9664 		ip_wput_ioctl(q, mp);
9665 		return;
9666 	default:
9667 		cmn_err(CE_PANIC, "should not happen ");
9668 	}
9669 nak:
9670 	if (mp->b_cont != NULL) {
9671 		freemsg(mp->b_cont);
9672 		mp->b_cont = NULL;
9673 	}
9674 	iocp->ioc_error = EINVAL;
9675 	mp->b_datap->db_type = M_IOCNAK;
9676 	iocp->ioc_count = 0;
9677 	qreply(q, mp);
9678 }
9679 
9680 /* ip_wput hands off ARP IOCTL responses to us */
9681 void
9682 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9683 {
9684 	struct arpreq *ar;
9685 	struct xarpreq *xar;
9686 	area_t	*area;
9687 	mblk_t	*area_mp;
9688 	struct iocblk *iocp;
9689 	mblk_t	*orig_ioc_mp, *tmp;
9690 	struct iocblk	*orig_iocp;
9691 	ill_t *ill;
9692 	conn_t *connp = NULL;
9693 	uint_t ioc_id;
9694 	mblk_t *pending_mp;
9695 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9696 	int *flagsp;
9697 	char *storage = NULL;
9698 	sin_t *sin;
9699 	ipaddr_t addr;
9700 	int err;
9701 
9702 	ill = q->q_ptr;
9703 	ASSERT(ill != NULL);
9704 
9705 	/*
9706 	 * We should get back from ARP a packet chain that looks like:
9707 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9708 	 */
9709 	if (!(area_mp = mp->b_cont) ||
9710 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9711 	    !(orig_ioc_mp = area_mp->b_cont) ||
9712 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9713 		freemsg(mp);
9714 		return;
9715 	}
9716 
9717 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9718 
9719 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9720 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9721 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9722 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9723 		x_arp_ioctl = B_TRUE;
9724 		xar = (struct xarpreq *)tmp->b_rptr;
9725 		sin = (sin_t *)&xar->xarp_pa;
9726 		flagsp = &xar->xarp_flags;
9727 		storage = xar->xarp_ha.sdl_data;
9728 		if (xar->xarp_ha.sdl_nlen != 0)
9729 			ifx_arp_ioctl = B_TRUE;
9730 	} else {
9731 		ar = (struct arpreq *)tmp->b_rptr;
9732 		sin = (sin_t *)&ar->arp_pa;
9733 		flagsp = &ar->arp_flags;
9734 		storage = ar->arp_ha.sa_data;
9735 	}
9736 
9737 	iocp = (struct iocblk *)mp->b_rptr;
9738 
9739 	/*
9740 	 * Pick out the originating queue based on the ioc_id.
9741 	 */
9742 	ioc_id = iocp->ioc_id;
9743 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9744 	if (pending_mp == NULL) {
9745 		ASSERT(connp == NULL);
9746 		ip_ioctl_freemsg(mp);
9747 		return;
9748 	}
9749 	ASSERT(connp != NULL);
9750 	q = CONNP_TO_WQ(connp);
9751 
9752 	/* Uncouple the internally generated IOCTL from the original one */
9753 	area = (area_t *)area_mp->b_rptr;
9754 	area_mp->b_cont = NULL;
9755 
9756 	/*
9757 	 * Restore the b_next and b_prev used by mi code. This is needed
9758 	 * to complete the ioctl using mi* functions. We stored them in
9759 	 * the pending mp prior to sending the request to ARP.
9760 	 */
9761 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9762 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9763 	ip_ioctl_freemsg(pending_mp);
9764 
9765 	/*
9766 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9767 	 * Catch the case where there is an IRE_CACHE by no entry in the
9768 	 * arp table.
9769 	 */
9770 	addr = sin->sin_addr.s_addr;
9771 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9772 		ire_t			*ire;
9773 		dl_unitdata_req_t	*dlup;
9774 		mblk_t			*llmp;
9775 		int			addr_len;
9776 		ill_t			*ipsqill = NULL;
9777 
9778 		if (ifx_arp_ioctl) {
9779 			/*
9780 			 * There's no need to lookup the ill, since
9781 			 * we've already done that when we started
9782 			 * processing the ioctl and sent the message
9783 			 * to ARP on that ill.  So use the ill that
9784 			 * is stored in q->q_ptr.
9785 			 */
9786 			ipsqill = ill;
9787 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9788 			    ipsqill->ill_ipif, ALL_ZONES,
9789 			    MATCH_IRE_TYPE | MATCH_IRE_ILL);
9790 		} else {
9791 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9792 			    NULL, ALL_ZONES, MATCH_IRE_TYPE);
9793 			if (ire != NULL)
9794 				ipsqill = ire_to_ill(ire);
9795 		}
9796 
9797 		if ((x_arp_ioctl) && (ipsqill != NULL))
9798 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9799 
9800 		if (ire != NULL) {
9801 			*flagsp = ATF_INUSE;
9802 			llmp = ire->ire_dlureq_mp;
9803 			if (llmp != NULL && ipsqill != NULL) {
9804 				uchar_t *macaddr;
9805 
9806 				addr_len = ipsqill->ill_phys_addr_length;
9807 				if (x_arp_ioctl && ((addr_len +
9808 				    ipsqill->ill_name_length) >
9809 				    sizeof (xar->xarp_ha.sdl_data))) {
9810 					ire_refrele(ire);
9811 					freemsg(mp);
9812 					ip_ioctl_finish(q, orig_ioc_mp,
9813 					    EINVAL, NO_COPYOUT, NULL, NULL);
9814 					return;
9815 				}
9816 				*flagsp |= ATF_COM;
9817 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
9818 				if (ipsqill->ill_sap_length < 0)
9819 					macaddr = llmp->b_rptr +
9820 					    dlup->dl_dest_addr_offset;
9821 				else
9822 					macaddr = llmp->b_rptr +
9823 					    dlup->dl_dest_addr_offset +
9824 					    ipsqill->ill_sap_length;
9825 				/*
9826 				 * For SIOCGARP, MAC address length
9827 				 * validation has already been done
9828 				 * before the ioctl was issued to ARP to
9829 				 * allow it to progress only on 6 byte
9830 				 * addressable (ethernet like) media. Thus
9831 				 * the mac address copying can not overwrite
9832 				 * the sa_data area below.
9833 				 */
9834 				bcopy(macaddr, storage, addr_len);
9835 			}
9836 			/* Ditch the internal IOCTL. */
9837 			freemsg(mp);
9838 			ire_refrele(ire);
9839 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9840 			return;
9841 		}
9842 	}
9843 
9844 	/*
9845 	 * Delete the coresponding IRE_CACHE if any.
9846 	 * Reset the error if there was one (in case there was no entry
9847 	 * in arp.)
9848 	 */
9849 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
9850 		ipif_t *ipintf = NULL;
9851 
9852 		if (ifx_arp_ioctl) {
9853 			/*
9854 			 * There's no need to lookup the ill, since
9855 			 * we've already done that when we started
9856 			 * processing the ioctl and sent the message
9857 			 * to ARP on that ill.  So use the ill that
9858 			 * is stored in q->q_ptr.
9859 			 */
9860 			ipintf = ill->ill_ipif;
9861 		}
9862 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
9863 			/*
9864 			 * The address in "addr" may be an entry for a
9865 			 * router. If that's true, then any off-net
9866 			 * IRE_CACHE entries that go through the router
9867 			 * with address "addr" must be clobbered. Use
9868 			 * ire_walk to achieve this goal.
9869 			 */
9870 			if (ifx_arp_ioctl)
9871 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
9872 				    ire_delete_cache_gw, (char *)&addr, ill);
9873 			else
9874 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
9875 				    ALL_ZONES);
9876 			iocp->ioc_error = 0;
9877 		}
9878 	}
9879 
9880 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
9881 		err = iocp->ioc_error;
9882 		freemsg(mp);
9883 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
9884 		return;
9885 	}
9886 
9887 	/*
9888 	 * Completion of an SIOCG{X}ARP.  Translate the information from
9889 	 * the area_t into the struct {x}arpreq.
9890 	 */
9891 	if (x_arp_ioctl) {
9892 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9893 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9894 		    sizeof (xar->xarp_ha.sdl_data)) {
9895 			freemsg(mp);
9896 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
9897 			    NO_COPYOUT, NULL, NULL);
9898 			return;
9899 		}
9900 	}
9901 	*flagsp = ATF_INUSE;
9902 	if (area->area_flags & ACE_F_PERMANENT)
9903 		*flagsp |= ATF_PERM;
9904 	if (area->area_flags & ACE_F_PUBLISH)
9905 		*flagsp |= ATF_PUBL;
9906 	if (area->area_hw_addr_length != 0) {
9907 		*flagsp |= ATF_COM;
9908 		/*
9909 		 * For SIOCGARP, MAC address length validation has
9910 		 * already been done before the ioctl was issued to ARP
9911 		 * to allow it to progress only on 6 byte addressable
9912 		 * (ethernet like) media. Thus the mac address copying
9913 		 * can not overwrite the sa_data area below.
9914 		 */
9915 		bcopy((char *)area + area->area_hw_addr_offset,
9916 		    storage, area->area_hw_addr_length);
9917 	}
9918 
9919 	/* Ditch the internal IOCTL. */
9920 	freemsg(mp);
9921 	/* Complete the original. */
9922 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9923 }
9924 
9925 /*
9926  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9927  * interface) create the next available logical interface for this
9928  * physical interface.
9929  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9930  * ipif with the specified name.
9931  *
9932  * If the address family is not AF_UNSPEC then set the address as well.
9933  *
9934  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9935  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9936  *
9937  * Executed as a writer on the ill or ill group.
9938  * So no lock is needed to traverse the ipif chain, or examine the
9939  * phyint flags.
9940  */
9941 /* ARGSUSED */
9942 int
9943 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9944     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9945 {
9946 	mblk_t	*mp1;
9947 	struct lifreq *lifr;
9948 	boolean_t	isv6;
9949 	boolean_t	exists;
9950 	char 	*name;
9951 	char	*endp;
9952 	char	*cp;
9953 	int	namelen;
9954 	ipif_t	*ipif;
9955 	long	id;
9956 	ipsq_t	*ipsq;
9957 	ill_t	*ill;
9958 	sin_t	*sin;
9959 	int	err = 0;
9960 	boolean_t found_sep = B_FALSE;
9961 	conn_t	*connp;
9962 	zoneid_t zoneid;
9963 	int	orig_ifindex = 0;
9964 
9965 	ip1dbg(("ip_sioctl_addif\n"));
9966 	/* Existence of mp1 has been checked in ip_wput_nondata */
9967 	mp1 = mp->b_cont->b_cont;
9968 	/*
9969 	 * Null terminate the string to protect against buffer
9970 	 * overrun. String was generated by user code and may not
9971 	 * be trusted.
9972 	 */
9973 	lifr = (struct lifreq *)mp1->b_rptr;
9974 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9975 	name = lifr->lifr_name;
9976 	ASSERT(CONN_Q(q));
9977 	connp = Q_TO_CONN(q);
9978 	isv6 = connp->conn_af_isv6;
9979 	zoneid = connp->conn_zoneid;
9980 	namelen = mi_strlen(name);
9981 	if (namelen == 0)
9982 		return (EINVAL);
9983 
9984 	exists = B_FALSE;
9985 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9986 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9987 		/*
9988 		 * Allow creating lo0 using SIOCLIFADDIF.
9989 		 * can't be any other writer thread. So can pass null below
9990 		 * for the last 4 args to ipif_lookup_name.
9991 		 */
9992 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
9993 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
9994 		/* Prevent any further action */
9995 		if (ipif == NULL) {
9996 			return (ENOBUFS);
9997 		} else if (!exists) {
9998 			/* We created the ipif now and as writer */
9999 			ipif_refrele(ipif);
10000 			return (0);
10001 		} else {
10002 			ill = ipif->ipif_ill;
10003 			ill_refhold(ill);
10004 			ipif_refrele(ipif);
10005 		}
10006 	} else {
10007 		/* Look for a colon in the name. */
10008 		endp = &name[namelen];
10009 		for (cp = endp; --cp > name; ) {
10010 			if (*cp == IPIF_SEPARATOR_CHAR) {
10011 				found_sep = B_TRUE;
10012 				/*
10013 				 * Reject any non-decimal aliases for plumbing
10014 				 * of logical interfaces. Aliases with leading
10015 				 * zeroes are also rejected as they introduce
10016 				 * ambiguity in the naming of the interfaces.
10017 				 * Comparing with "0" takes care of all such
10018 				 * cases.
10019 				 */
10020 				if ((strncmp("0", cp+1, 1)) == 0)
10021 					return (EINVAL);
10022 
10023 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10024 				    id <= 0 || *endp != '\0') {
10025 					return (EINVAL);
10026 				}
10027 				*cp = '\0';
10028 				break;
10029 			}
10030 		}
10031 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10032 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10033 		if (found_sep)
10034 			*cp = IPIF_SEPARATOR_CHAR;
10035 		if (ill == NULL)
10036 			return (err);
10037 	}
10038 
10039 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10040 	    B_TRUE);
10041 
10042 	/*
10043 	 * Release the refhold due to the lookup, now that we are excl
10044 	 * or we are just returning
10045 	 */
10046 	ill_refrele(ill);
10047 
10048 	if (ipsq == NULL)
10049 		return (EINPROGRESS);
10050 
10051 	/*
10052 	 * If the interface is failed, inactive or offlined, look for a working
10053 	 * interface in the ill group and create the ipif there. If we can't
10054 	 * find a good interface, create the ipif anyway so that in.mpathd can
10055 	 * move it to the first repaired interface.
10056 	 */
10057 	if ((ill->ill_phyint->phyint_flags &
10058 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10059 	    ill->ill_phyint->phyint_groupname_len != 0) {
10060 		phyint_t *phyi;
10061 		char *groupname = ill->ill_phyint->phyint_groupname;
10062 
10063 		/*
10064 		 * We're looking for a working interface, but it doesn't matter
10065 		 * if it's up or down; so instead of following the group lists,
10066 		 * we look at each physical interface and compare the groupname.
10067 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10068 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10069 		 * Otherwise we create the ipif on the failed interface.
10070 		 */
10071 		rw_enter(&ill_g_lock, RW_READER);
10072 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10073 		for (; phyi != NULL;
10074 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10075 		    phyi, AVL_AFTER)) {
10076 			if (phyi->phyint_groupname_len == 0)
10077 				continue;
10078 			ASSERT(phyi->phyint_groupname != NULL);
10079 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10080 			    !(phyi->phyint_flags &
10081 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10082 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10083 			    (phyi->phyint_illv4 != NULL))) {
10084 				break;
10085 			}
10086 		}
10087 		rw_exit(&ill_g_lock);
10088 
10089 		if (phyi != NULL) {
10090 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10091 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10092 			    phyi->phyint_illv4);
10093 		}
10094 	}
10095 
10096 	/*
10097 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10098 	 * before or after us.
10099 	 */
10100 	ASSERT(IAM_WRITER_ILL(ill));
10101 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10102 
10103 	if (found_sep && orig_ifindex == 0) {
10104 		/* Now see if there is an IPIF with this unit number. */
10105 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10106 			if (ipif->ipif_id == id) {
10107 				err = EEXIST;
10108 				goto done;
10109 			}
10110 		}
10111 	}
10112 
10113 	/*
10114 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10115 	 * of lo0. We never come here when we plumb lo0:0. It
10116 	 * happens in ipif_lookup_on_name.
10117 	 * The specified unit number is ignored when we create the ipif on a
10118 	 * different interface. However, we save it in ipif_orig_ipifid below so
10119 	 * that the ipif fails back to the right position.
10120 	 */
10121 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10122 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10123 		err = ENOBUFS;
10124 		goto done;
10125 	}
10126 
10127 	/* Return created name with ioctl */
10128 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10129 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10130 	ip1dbg(("created %s\n", lifr->lifr_name));
10131 
10132 	/* Set address */
10133 	sin = (sin_t *)&lifr->lifr_addr;
10134 	if (sin->sin_family != AF_UNSPEC) {
10135 		err = ip_sioctl_addr(ipif, sin, q, mp,
10136 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10137 	}
10138 
10139 	/* Set ifindex and unit number for failback */
10140 	if (err == 0 && orig_ifindex != 0) {
10141 		ipif->ipif_orig_ifindex = orig_ifindex;
10142 		if (found_sep) {
10143 			ipif->ipif_orig_ipifid = id;
10144 		}
10145 	}
10146 
10147 done:
10148 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10149 	return (err);
10150 }
10151 
10152 /*
10153  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10154  * interface) delete it based on the IP address (on this physical interface).
10155  * Otherwise delete it based on the ipif_id.
10156  * Also, special handling to allow a removeif of lo0.
10157  */
10158 /* ARGSUSED */
10159 int
10160 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10161     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10162 {
10163 	conn_t		*connp;
10164 	ill_t		*ill = ipif->ipif_ill;
10165 	boolean_t	 success;
10166 
10167 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10168 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10169 	ASSERT(IAM_WRITER_IPIF(ipif));
10170 
10171 	connp = Q_TO_CONN(q);
10172 	/*
10173 	 * Special case for unplumbing lo0 (the loopback physical interface).
10174 	 * If unplumbing lo0, the incoming address structure has been
10175 	 * initialized to all zeros. When unplumbing lo0, all its logical
10176 	 * interfaces must be removed too.
10177 	 *
10178 	 * Note that this interface may be called to remove a specific
10179 	 * loopback logical interface (eg, lo0:1). But in that case
10180 	 * ipif->ipif_id != 0 so that the code path for that case is the
10181 	 * same as any other interface (meaning it skips the code directly
10182 	 * below).
10183 	 */
10184 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10185 		if (sin->sin_family == AF_UNSPEC &&
10186 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10187 			/*
10188 			 * Mark it condemned. No new ref. will be made to ill.
10189 			 */
10190 			mutex_enter(&ill->ill_lock);
10191 			ill->ill_state_flags |= ILL_CONDEMNED;
10192 			for (ipif = ill->ill_ipif; ipif != NULL;
10193 			    ipif = ipif->ipif_next) {
10194 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10195 			}
10196 			mutex_exit(&ill->ill_lock);
10197 
10198 			ipif = ill->ill_ipif;
10199 			/* unplumb the loopback interface */
10200 			ill_delete(ill);
10201 			mutex_enter(&connp->conn_lock);
10202 			mutex_enter(&ill->ill_lock);
10203 			ASSERT(ill->ill_group == NULL);
10204 
10205 			/* Are any references to this ill active */
10206 			if (ill_is_quiescent(ill)) {
10207 				mutex_exit(&ill->ill_lock);
10208 				mutex_exit(&connp->conn_lock);
10209 				ill_delete_tail(ill);
10210 				return (0);
10211 			}
10212 			success = ipsq_pending_mp_add(connp, ipif,
10213 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10214 			mutex_exit(&connp->conn_lock);
10215 			mutex_exit(&ill->ill_lock);
10216 			if (success)
10217 				return (EINPROGRESS);
10218 			else
10219 				return (EINTR);
10220 		}
10221 	}
10222 
10223 	/*
10224 	 * We are exclusive on the ipsq, so an ill move will be serialized
10225 	 * before or after us.
10226 	 */
10227 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10228 
10229 	if (ipif->ipif_id == 0) {
10230 		/* Find based on address */
10231 		if (ipif->ipif_isv6) {
10232 			sin6_t *sin6;
10233 
10234 			if (sin->sin_family != AF_INET6)
10235 				return (EAFNOSUPPORT);
10236 
10237 			sin6 = (sin6_t *)sin;
10238 			/* We are a writer, so we should be able to lookup */
10239 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10240 			    ill, ALL_ZONES, NULL, 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_v6(&sin6->sin6_addr,
10247 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10248 			}
10249 		} else {
10250 			ipaddr_t addr;
10251 
10252 			if (sin->sin_family != AF_INET)
10253 				return (EAFNOSUPPORT);
10254 
10255 			addr = sin->sin_addr.s_addr;
10256 			/* We are a writer, so we should be able to lookup */
10257 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10258 			    NULL, NULL, NULL);
10259 			if (ipif == NULL) {
10260 				/*
10261 				 * Maybe the address in on another interface in
10262 				 * the same IPMP group? We check this below.
10263 				 */
10264 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10265 				    NULL, NULL, NULL, NULL);
10266 			}
10267 		}
10268 		if (ipif == NULL) {
10269 			return (EADDRNOTAVAIL);
10270 		}
10271 		/*
10272 		 * When the address to be removed is hosted on a different
10273 		 * interface, we check if the interface is in the same IPMP
10274 		 * group as the specified one; if so we proceed with the
10275 		 * removal.
10276 		 * ill->ill_group is NULL when the ill is down, so we have to
10277 		 * compare the group names instead.
10278 		 */
10279 		if (ipif->ipif_ill != ill &&
10280 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10281 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10282 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10283 		    ill->ill_phyint->phyint_groupname) != 0)) {
10284 			ipif_refrele(ipif);
10285 			return (EADDRNOTAVAIL);
10286 		}
10287 
10288 		/* This is a writer */
10289 		ipif_refrele(ipif);
10290 	}
10291 
10292 	/*
10293 	 * Can not delete instance zero since it is tied to the ill.
10294 	 */
10295 	if (ipif->ipif_id == 0)
10296 		return (EBUSY);
10297 
10298 	mutex_enter(&ill->ill_lock);
10299 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10300 	mutex_exit(&ill->ill_lock);
10301 
10302 	ipif_free(ipif);
10303 
10304 	mutex_enter(&connp->conn_lock);
10305 	mutex_enter(&ill->ill_lock);
10306 
10307 	/* Are any references to this ipif active */
10308 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10309 		mutex_exit(&ill->ill_lock);
10310 		mutex_exit(&connp->conn_lock);
10311 		ipif_down_tail(ipif);
10312 		ipif_free_tail(ipif);
10313 		return (0);
10314 	    }
10315 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10316 	    IPIF_FREE);
10317 	mutex_exit(&ill->ill_lock);
10318 	mutex_exit(&connp->conn_lock);
10319 	if (success)
10320 		return (EINPROGRESS);
10321 	else
10322 		return (EINTR);
10323 }
10324 
10325 /*
10326  * Restart the removeif ioctl. The refcnt has gone down to 0.
10327  * The ipif is already condemned. So can't find it thru lookups.
10328  */
10329 /* ARGSUSED */
10330 int
10331 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10332     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10333 {
10334 	ill_t *ill;
10335 
10336 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10337 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10338 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10339 		ill = ipif->ipif_ill;
10340 		ASSERT(IAM_WRITER_ILL(ill));
10341 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10342 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10343 		ill_delete_tail(ill);
10344 		return (0);
10345 	}
10346 
10347 	ill = ipif->ipif_ill;
10348 	ASSERT(IAM_WRITER_IPIF(ipif));
10349 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10350 
10351 	ipif_down_tail(ipif);
10352 	ipif_free_tail(ipif);
10353 
10354 	ILL_UNMARK_CHANGING(ill);
10355 	return (0);
10356 }
10357 
10358 /*
10359  * Set the local interface address.
10360  * Allow an address of all zero when the interface is down.
10361  */
10362 /* ARGSUSED */
10363 int
10364 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10365     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10366 {
10367 	int err = 0;
10368 	in6_addr_t v6addr;
10369 	boolean_t need_up = B_FALSE;
10370 
10371 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10372 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10373 
10374 	ASSERT(IAM_WRITER_IPIF(ipif));
10375 
10376 	if (ipif->ipif_isv6) {
10377 		sin6_t *sin6;
10378 		ill_t *ill;
10379 		phyint_t *phyi;
10380 
10381 		if (sin->sin_family != AF_INET6)
10382 			return (EAFNOSUPPORT);
10383 
10384 		sin6 = (sin6_t *)sin;
10385 		v6addr = sin6->sin6_addr;
10386 		ill = ipif->ipif_ill;
10387 		phyi = ill->ill_phyint;
10388 
10389 		/*
10390 		 * Enforce that true multicast interfaces have a link-local
10391 		 * address for logical unit 0.
10392 		 */
10393 		if (ipif->ipif_id == 0 &&
10394 		    (ill->ill_flags & ILLF_MULTICAST) &&
10395 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10396 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10397 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10398 			return (EADDRNOTAVAIL);
10399 		}
10400 
10401 		/*
10402 		 * up interfaces shouldn't have the unspecified address
10403 		 * unless they also have the IPIF_NOLOCAL flags set and
10404 		 * have a subnet assigned.
10405 		 */
10406 		if ((ipif->ipif_flags & IPIF_UP) &&
10407 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10408 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10409 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10410 			return (EADDRNOTAVAIL);
10411 		}
10412 
10413 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10414 			return (EADDRNOTAVAIL);
10415 	} else {
10416 		ipaddr_t addr;
10417 
10418 		if (sin->sin_family != AF_INET)
10419 			return (EAFNOSUPPORT);
10420 
10421 		addr = sin->sin_addr.s_addr;
10422 
10423 		/* Allow 0 as the local address. */
10424 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10425 			return (EADDRNOTAVAIL);
10426 
10427 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10428 	}
10429 
10430 
10431 	/*
10432 	 * Even if there is no change we redo things just to rerun
10433 	 * ipif_set_default.
10434 	 */
10435 	if (ipif->ipif_flags & IPIF_UP) {
10436 		/*
10437 		 * Setting a new local address, make sure
10438 		 * we have net and subnet bcast ire's for
10439 		 * the old address if we need them.
10440 		 */
10441 		if (!ipif->ipif_isv6)
10442 			ipif_check_bcast_ires(ipif);
10443 		/*
10444 		 * If the interface is already marked up,
10445 		 * we call ipif_down which will take care
10446 		 * of ditching any IREs that have been set
10447 		 * up based on the old interface address.
10448 		 */
10449 		err = ipif_logical_down(ipif, q, mp);
10450 		if (err == EINPROGRESS)
10451 			return (err);
10452 		ipif_down_tail(ipif);
10453 		need_up = 1;
10454 	}
10455 
10456 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10457 	return (err);
10458 }
10459 
10460 int
10461 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10462     boolean_t need_up)
10463 {
10464 	in6_addr_t v6addr;
10465 	ipaddr_t addr;
10466 	sin6_t	*sin6;
10467 	int	err = 0;
10468 
10469 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10470 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10471 	ASSERT(IAM_WRITER_IPIF(ipif));
10472 	if (ipif->ipif_isv6) {
10473 		sin6 = (sin6_t *)sin;
10474 		v6addr = sin6->sin6_addr;
10475 	} else {
10476 		addr = sin->sin_addr.s_addr;
10477 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10478 	}
10479 	mutex_enter(&ipif->ipif_ill->ill_lock);
10480 	ipif->ipif_v6lcl_addr = v6addr;
10481 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10482 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10483 	} else {
10484 		ipif->ipif_v6src_addr = v6addr;
10485 	}
10486 
10487 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10488 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10489 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10490 
10491 		/*
10492 		 * The local address of this interface is a 6to4 address,
10493 		 * check if this interface is in fact a 6to4 tunnel or just
10494 		 * an interface configured with a 6to4 address.  We are only
10495 		 * interested in the former.
10496 		 */
10497 		if (wqp != NULL) {
10498 			while ((wqp->q_next != NULL) &&
10499 			    (wqp->q_next->q_qinfo != NULL) &&
10500 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10501 
10502 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10503 				    == TUN6TO4_MODID) {
10504 					/* set for use in IP */
10505 					ipif->ipif_ill->ill_is_6to4tun = 1;
10506 					break;
10507 				}
10508 				wqp = wqp->q_next;
10509 			}
10510 		}
10511 	}
10512 
10513 	ipif_set_default(ipif);
10514 	mutex_exit(&ipif->ipif_ill->ill_lock);
10515 
10516 	if (need_up) {
10517 		/*
10518 		 * Now bring the interface back up.  If this
10519 		 * is the only IPIF for the ILL, ipif_up
10520 		 * will have to re-bind to the device, so
10521 		 * we may get back EINPROGRESS, in which
10522 		 * case, this IOCTL will get completed in
10523 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10524 		 */
10525 		err = ipif_up(ipif, q, mp);
10526 	} else {
10527 		/*
10528 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10529 		 * if need_up is true.
10530 		 */
10531 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10532 	}
10533 
10534 	return (err);
10535 }
10536 
10537 
10538 /*
10539  * Restart entry point to restart the address set operation after the
10540  * refcounts have dropped to zero.
10541  */
10542 /* ARGSUSED */
10543 int
10544 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10545     ip_ioctl_cmd_t *ipip, void *ifreq)
10546 {
10547 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10548 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10549 	ASSERT(IAM_WRITER_IPIF(ipif));
10550 	ipif_down_tail(ipif);
10551 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10552 }
10553 
10554 /* ARGSUSED */
10555 int
10556 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10557     ip_ioctl_cmd_t *ipip, void *if_req)
10558 {
10559 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10560 	struct lifreq *lifr = (struct lifreq *)if_req;
10561 
10562 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10563 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10564 	/*
10565 	 * The net mask and address can't change since we have a
10566 	 * reference to the ipif. So no lock is necessary.
10567 	 */
10568 	if (ipif->ipif_isv6) {
10569 		*sin6 = sin6_null;
10570 		sin6->sin6_family = AF_INET6;
10571 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10572 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10573 		lifr->lifr_addrlen =
10574 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10575 	} else {
10576 		*sin = sin_null;
10577 		sin->sin_family = AF_INET;
10578 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10579 		if (ipip->ipi_cmd_type == LIF_CMD) {
10580 			lifr->lifr_addrlen =
10581 			    ip_mask_to_plen(ipif->ipif_net_mask);
10582 		}
10583 	}
10584 	return (0);
10585 }
10586 
10587 /*
10588  * Set the destination address for a pt-pt interface.
10589  */
10590 /* ARGSUSED */
10591 int
10592 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10593     ip_ioctl_cmd_t *ipip, void *if_req)
10594 {
10595 	int err = 0;
10596 	in6_addr_t v6addr;
10597 	boolean_t need_up = B_FALSE;
10598 
10599 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10600 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10601 	ASSERT(IAM_WRITER_IPIF(ipif));
10602 
10603 	if (ipif->ipif_isv6) {
10604 		sin6_t *sin6;
10605 
10606 		if (sin->sin_family != AF_INET6)
10607 			return (EAFNOSUPPORT);
10608 
10609 		sin6 = (sin6_t *)sin;
10610 		v6addr = sin6->sin6_addr;
10611 
10612 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10613 			return (EADDRNOTAVAIL);
10614 	} else {
10615 		ipaddr_t addr;
10616 
10617 		if (sin->sin_family != AF_INET)
10618 			return (EAFNOSUPPORT);
10619 
10620 		addr = sin->sin_addr.s_addr;
10621 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10622 			return (EADDRNOTAVAIL);
10623 
10624 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10625 	}
10626 
10627 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10628 		return (0);	/* No change */
10629 
10630 	if (ipif->ipif_flags & IPIF_UP) {
10631 		/*
10632 		 * If the interface is already marked up,
10633 		 * we call ipif_down which will take care
10634 		 * of ditching any IREs that have been set
10635 		 * up based on the old pp dst address.
10636 		 */
10637 		err = ipif_logical_down(ipif, q, mp);
10638 		if (err == EINPROGRESS)
10639 			return (err);
10640 		ipif_down_tail(ipif);
10641 		need_up = B_TRUE;
10642 	}
10643 	/*
10644 	 * could return EINPROGRESS. If so ioctl will complete in
10645 	 * ip_rput_dlpi_writer
10646 	 */
10647 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10648 	return (err);
10649 }
10650 
10651 static int
10652 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10653     boolean_t need_up)
10654 {
10655 	in6_addr_t v6addr;
10656 	ill_t	*ill = ipif->ipif_ill;
10657 	int	err = 0;
10658 
10659 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10660 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10661 	if (ipif->ipif_isv6) {
10662 		sin6_t *sin6;
10663 
10664 		sin6 = (sin6_t *)sin;
10665 		v6addr = sin6->sin6_addr;
10666 	} else {
10667 		ipaddr_t addr;
10668 
10669 		addr = sin->sin_addr.s_addr;
10670 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10671 	}
10672 	mutex_enter(&ill->ill_lock);
10673 	/* Set point to point destination address. */
10674 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10675 		/*
10676 		 * Allow this as a means of creating logical
10677 		 * pt-pt interfaces on top of e.g. an Ethernet.
10678 		 * XXX Undocumented HACK for testing.
10679 		 * pt-pt interfaces are created with NUD disabled.
10680 		 */
10681 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10682 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10683 		if (ipif->ipif_isv6)
10684 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10685 	}
10686 
10687 	/* Set the new address. */
10688 	ipif->ipif_v6pp_dst_addr = v6addr;
10689 	/* Make sure subnet tracks pp_dst */
10690 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10691 	mutex_exit(&ill->ill_lock);
10692 
10693 	if (need_up) {
10694 		/*
10695 		 * Now bring the interface back up.  If this
10696 		 * is the only IPIF for the ILL, ipif_up
10697 		 * will have to re-bind to the device, so
10698 		 * we may get back EINPROGRESS, in which
10699 		 * case, this IOCTL will get completed in
10700 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10701 		 */
10702 		err = ipif_up(ipif, q, mp);
10703 	}
10704 	return (err);
10705 }
10706 
10707 /*
10708  * Restart entry point to restart the dstaddress set operation after the
10709  * refcounts have dropped to zero.
10710  */
10711 /* ARGSUSED */
10712 int
10713 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10714     ip_ioctl_cmd_t *ipip, void *ifreq)
10715 {
10716 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10717 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10718 	ipif_down_tail(ipif);
10719 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10720 }
10721 
10722 /* ARGSUSED */
10723 int
10724 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10725     ip_ioctl_cmd_t *ipip, void *if_req)
10726 {
10727 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10728 
10729 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10730 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10731 	/*
10732 	 * Get point to point destination address. The addresses can't
10733 	 * change since we hold a reference to the ipif.
10734 	 */
10735 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10736 		return (EADDRNOTAVAIL);
10737 
10738 	if (ipif->ipif_isv6) {
10739 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10740 		*sin6 = sin6_null;
10741 		sin6->sin6_family = AF_INET6;
10742 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10743 	} else {
10744 		*sin = sin_null;
10745 		sin->sin_family = AF_INET;
10746 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10747 	}
10748 	return (0);
10749 }
10750 
10751 /*
10752  * part of ipmp, make this func return the active/inactive state and
10753  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10754  */
10755 /*
10756  * This function either sets or clears the IFF_INACTIVE flag.
10757  *
10758  * As long as there are some addresses or multicast memberships on the
10759  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10760  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10761  * will be used for outbound packets.
10762  *
10763  * Caller needs to verify the validity of setting IFF_INACTIVE.
10764  */
10765 static void
10766 phyint_inactive(phyint_t *phyi)
10767 {
10768 	ill_t *ill_v4;
10769 	ill_t *ill_v6;
10770 	ipif_t *ipif;
10771 	ilm_t *ilm;
10772 
10773 	ill_v4 = phyi->phyint_illv4;
10774 	ill_v6 = phyi->phyint_illv6;
10775 
10776 	/*
10777 	 * No need for a lock while traversing the list since iam
10778 	 * a writer
10779 	 */
10780 	if (ill_v4 != NULL) {
10781 		ASSERT(IAM_WRITER_ILL(ill_v4));
10782 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10783 		    ipif = ipif->ipif_next) {
10784 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10785 				mutex_enter(&phyi->phyint_lock);
10786 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10787 				mutex_exit(&phyi->phyint_lock);
10788 				return;
10789 			}
10790 		}
10791 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10792 		    ilm = ilm->ilm_next) {
10793 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10794 				mutex_enter(&phyi->phyint_lock);
10795 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10796 				mutex_exit(&phyi->phyint_lock);
10797 				return;
10798 			}
10799 		}
10800 	}
10801 	if (ill_v6 != NULL) {
10802 		ill_v6 = phyi->phyint_illv6;
10803 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
10804 		    ipif = ipif->ipif_next) {
10805 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10806 				mutex_enter(&phyi->phyint_lock);
10807 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10808 				mutex_exit(&phyi->phyint_lock);
10809 				return;
10810 			}
10811 		}
10812 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
10813 		    ilm = ilm->ilm_next) {
10814 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10815 				mutex_enter(&phyi->phyint_lock);
10816 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10817 				mutex_exit(&phyi->phyint_lock);
10818 				return;
10819 			}
10820 		}
10821 	}
10822 	mutex_enter(&phyi->phyint_lock);
10823 	phyi->phyint_flags |= PHYI_INACTIVE;
10824 	mutex_exit(&phyi->phyint_lock);
10825 }
10826 
10827 /*
10828  * This function is called only when the phyint flags change. Currently
10829  * called from ip_sioctl_flags. We re-do the broadcast nomination so
10830  * that we can select a good ill.
10831  */
10832 static void
10833 ip_redo_nomination(phyint_t *phyi)
10834 {
10835 	ill_t *ill_v4;
10836 
10837 	ill_v4 = phyi->phyint_illv4;
10838 
10839 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
10840 		ASSERT(IAM_WRITER_ILL(ill_v4));
10841 		if (ill_v4->ill_group->illgrp_ill_count > 1)
10842 			ill_nominate_bcast_rcv(ill_v4->ill_group);
10843 	}
10844 }
10845 
10846 /*
10847  * Heuristic to check if ill is INACTIVE.
10848  * Checks if ill has an ipif with an usable ip address.
10849  *
10850  * Return values:
10851  *	B_TRUE	- ill is INACTIVE; has no usable ipif
10852  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
10853  */
10854 static boolean_t
10855 ill_is_inactive(ill_t *ill)
10856 {
10857 	ipif_t *ipif;
10858 
10859 	/* Check whether it is in an IPMP group */
10860 	if (ill->ill_phyint->phyint_groupname == NULL)
10861 		return (B_FALSE);
10862 
10863 	if (ill->ill_ipif_up_count == 0)
10864 		return (B_TRUE);
10865 
10866 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
10867 		uint64_t flags = ipif->ipif_flags;
10868 
10869 		/*
10870 		 * This ipif is usable if it is IPIF_UP and not a
10871 		 * dedicated test address.  A dedicated test address
10872 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
10873 		 * (note in particular that V6 test addresses are
10874 		 * link-local data addresses and thus are marked
10875 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
10876 		 */
10877 		if ((flags & IPIF_UP) &&
10878 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
10879 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
10880 			return (B_FALSE);
10881 	}
10882 	return (B_TRUE);
10883 }
10884 
10885 /*
10886  * Set interface flags.
10887  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
10888  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
10889  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
10890  *
10891  * NOTE : We really don't enforce that ipif_id zero should be used
10892  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10893  *	  is because applications generally does SICGLIFFLAGS and
10894  *	  ORs in the new flags (that affects the logical) and does a
10895  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10896  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10897  *	  flags that will be turned on is correct with respect to
10898  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10899  */
10900 /* ARGSUSED */
10901 int
10902 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10903     ip_ioctl_cmd_t *ipip, void *if_req)
10904 {
10905 	uint64_t turn_on;
10906 	uint64_t turn_off;
10907 	int	err;
10908 	boolean_t need_up = B_FALSE;
10909 	phyint_t *phyi;
10910 	ill_t *ill;
10911 	uint64_t intf_flags;
10912 	boolean_t phyint_flags_modified = B_FALSE;
10913 	uint64_t flags;
10914 	struct ifreq *ifr;
10915 	struct lifreq *lifr;
10916 	boolean_t set_linklocal = B_FALSE;
10917 	boolean_t zero_source = B_FALSE;
10918 
10919 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10920 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10921 
10922 	ASSERT(IAM_WRITER_IPIF(ipif));
10923 
10924 	ill = ipif->ipif_ill;
10925 	phyi = ill->ill_phyint;
10926 
10927 	if (ipip->ipi_cmd_type == IF_CMD) {
10928 		ifr = (struct ifreq *)if_req;
10929 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10930 	} else {
10931 		lifr = (struct lifreq *)if_req;
10932 		flags = lifr->lifr_flags;
10933 	}
10934 
10935 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10936 
10937 	/*
10938 	 * Has the flags been set correctly till now ?
10939 	 */
10940 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10941 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10942 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10943 	/*
10944 	 * Compare the new flags to the old, and partition
10945 	 * into those coming on and those going off.
10946 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10947 	 */
10948 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10949 		flags |= intf_flags & ~0xFFFF;
10950 
10951 	/*
10952 	 * First check which bits will change and then which will
10953 	 * go on and off
10954 	 */
10955 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
10956 	if (!turn_on)
10957 		return (0);	/* No change */
10958 
10959 	turn_off = intf_flags & turn_on;
10960 	turn_on ^= turn_off;
10961 	err = 0;
10962 
10963 	/*
10964 	 * Don't allow any bits belonging to the logical interface
10965 	 * to be set or cleared on the replacement ipif that was
10966 	 * created temporarily during a MOVE.
10967 	 */
10968 	if (ipif->ipif_replace_zero &&
10969 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
10970 		return (EINVAL);
10971 	}
10972 
10973 	/*
10974 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
10975 	 * IPv6 interfaces.
10976 	 */
10977 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
10978 		return (EINVAL);
10979 
10980 	/*
10981 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10982 	 * interfaces.  It makes no sense in that context.
10983 	 */
10984 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10985 		return (EINVAL);
10986 
10987 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
10988 		zero_source = B_TRUE;
10989 
10990 	/*
10991 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10992 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10993 	 * If the link local address isn't set, and can be set, it will get
10994 	 * set later on in this function.
10995 	 */
10996 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10997 	    (flags & IFF_UP) && !zero_source &&
10998 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10999 		if (ipif_cant_setlinklocal(ipif))
11000 			return (EINVAL);
11001 		set_linklocal = B_TRUE;
11002 	}
11003 
11004 	/*
11005 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11006 	 * same time. No need to grab ill_g_usesrc_lock here, see
11007 	 * synchronization notes in ip.c
11008 	 */
11009 	if (turn_on & PHYI_STANDBY &&
11010 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11011 		return (EINVAL);
11012 	}
11013 
11014 	/*
11015 	 * If we modify physical interface flags, we'll potentially need to
11016 	 * send up two routing socket messages for the changes (one for the
11017 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11018 	 */
11019 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11020 		phyint_flags_modified = B_TRUE;
11021 
11022 	/*
11023 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11024 	 * we need to flush the IRE_CACHES belonging to this ill.
11025 	 * We handle this case here without doing the DOWN/UP dance
11026 	 * like it is done for other flags. If some other flags are
11027 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11028 	 * below will handle it by bringing it down and then
11029 	 * bringing it UP.
11030 	 */
11031 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11032 		ill_t *ill_v4, *ill_v6;
11033 
11034 		ill_v4 = phyi->phyint_illv4;
11035 		ill_v6 = phyi->phyint_illv6;
11036 
11037 		/*
11038 		 * First set the INACTIVE flag if needed. Then delete the ires.
11039 		 * ire_add will atomically prevent creating new IRE_CACHEs
11040 		 * unless hidden flag is set.
11041 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11042 		 */
11043 		if ((turn_on & PHYI_FAILED) &&
11044 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11045 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11046 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11047 		}
11048 		if ((turn_off & PHYI_FAILED) &&
11049 		    ((intf_flags & PHYI_STANDBY) ||
11050 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11051 			phyint_inactive(phyi);
11052 		}
11053 
11054 		if (turn_on & PHYI_STANDBY) {
11055 			/*
11056 			 * We implicitly set INACTIVE only when STANDBY is set.
11057 			 * INACTIVE is also set on non-STANDBY phyint when user
11058 			 * disables FAILBACK using configuration file.
11059 			 * Do not allow STANDBY to be set on such INACTIVE
11060 			 * phyint
11061 			 */
11062 			if (phyi->phyint_flags & PHYI_INACTIVE)
11063 				return (EINVAL);
11064 			if (!(phyi->phyint_flags & PHYI_FAILED))
11065 				phyint_inactive(phyi);
11066 		}
11067 		if (turn_off & PHYI_STANDBY) {
11068 			if (ipmp_enable_failback) {
11069 				/*
11070 				 * Reset PHYI_INACTIVE.
11071 				 */
11072 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11073 			} else if (ill_is_inactive(ill) &&
11074 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11075 				/*
11076 				 * Need to set INACTIVE, when user sets
11077 				 * STANDBY on a non-STANDBY phyint and
11078 				 * later resets STANDBY
11079 				 */
11080 				phyint_inactive(phyi);
11081 			}
11082 		}
11083 		/*
11084 		 * We should always send up a message so that the
11085 		 * daemons come to know of it. Note that the zeroth
11086 		 * interface can be down and the check below for IPIF_UP
11087 		 * will not make sense as we are actually setting
11088 		 * a phyint flag here. We assume that the ipif used
11089 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11090 		 * send up any message for non-zero ipifs).
11091 		 */
11092 		phyint_flags_modified = B_TRUE;
11093 
11094 		if (ill_v4 != NULL) {
11095 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11096 			    IRE_CACHE, ill_stq_cache_delete,
11097 			    (char *)ill_v4, ill_v4);
11098 			illgrp_reset_schednext(ill_v4);
11099 		}
11100 		if (ill_v6 != NULL) {
11101 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11102 			    IRE_CACHE, ill_stq_cache_delete,
11103 			    (char *)ill_v6, ill_v6);
11104 			illgrp_reset_schednext(ill_v6);
11105 		}
11106 	}
11107 
11108 	/*
11109 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11110 	 * status of the interface and, if the interface is part of an IPMP
11111 	 * group, all other interfaces that are part of the same IPMP
11112 	 * group.
11113 	 */
11114 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11115 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11116 		    (caddr_t)ill);
11117 	}
11118 
11119 	/*
11120 	 * If the interface is not UP and we are not going to
11121 	 * bring it UP, record the flags and return. When the
11122 	 * interface comes UP later, the right actions will be
11123 	 * taken.
11124 	 */
11125 	if (!(ipif->ipif_flags & IPIF_UP) &&
11126 	    !(turn_on & IPIF_UP)) {
11127 		/* Record new flags in their respective places. */
11128 		mutex_enter(&ill->ill_lock);
11129 		mutex_enter(&ill->ill_phyint->phyint_lock);
11130 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11131 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11132 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11133 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11134 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11135 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11136 		mutex_exit(&ill->ill_lock);
11137 		mutex_exit(&ill->ill_phyint->phyint_lock);
11138 
11139 		/*
11140 		 * We do the broadcast and nomination here rather
11141 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11142 		 * the case of FAILBACK from INACTIVE standby to the
11143 		 * interface that has been repaired, PHYI_FAILED has not
11144 		 * been cleared yet. If there are only two interfaces in
11145 		 * that group, all we have is a FAILED and INACTIVE
11146 		 * interface. If we do the nomination soon after a failback,
11147 		 * the broadcast nomination code would select the
11148 		 * INACTIVE interface for receiving broadcasts as FAILED is
11149 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11150 		 * receive broadcast packets, we need to redo nomination
11151 		 * when the FAILED is cleared here. Thus, in general we
11152 		 * always do the nomination here for FAILED, STANDBY
11153 		 * and OFFLINE.
11154 		 */
11155 		if (((turn_on | turn_off) &
11156 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11157 			ip_redo_nomination(phyi);
11158 		}
11159 		if (phyint_flags_modified) {
11160 			if (phyi->phyint_illv4 != NULL) {
11161 				ip_rts_ifmsg(phyi->phyint_illv4->
11162 				    ill_ipif);
11163 			}
11164 			if (phyi->phyint_illv6 != NULL) {
11165 				ip_rts_ifmsg(phyi->phyint_illv6->
11166 				    ill_ipif);
11167 			}
11168 		}
11169 		return (0);
11170 	} else if (set_linklocal || zero_source) {
11171 		mutex_enter(&ill->ill_lock);
11172 		if (set_linklocal)
11173 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11174 		if (zero_source)
11175 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11176 		mutex_exit(&ill->ill_lock);
11177 	}
11178 
11179 	/*
11180 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11181 	 * or point-to-point interfaces with an unspecified destination. We do
11182 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11183 	 * have a subnet assigned, which is how in.ndpd currently manages its
11184 	 * onlink prefix list when no addresses are configured with those
11185 	 * prefixes.
11186 	 */
11187 	if (ipif->ipif_isv6 &&
11188 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11189 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11190 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11191 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11192 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11193 		return (EINVAL);
11194 	}
11195 
11196 	/*
11197 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11198 	 * from being brought up.
11199 	 */
11200 	if (!ipif->ipif_isv6 &&
11201 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11202 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11203 		return (EINVAL);
11204 	}
11205 
11206 	/*
11207 	 * The only flag changes that we currently take specific action on
11208 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11209 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11210 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11211 	 * the flags and bringing it back up again.
11212 	 */
11213 	if ((turn_on|turn_off) &
11214 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11215 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11216 		/*
11217 		 * Taking this ipif down, make sure we have
11218 		 * valid net and subnet bcast ire's for other
11219 		 * logical interfaces, if we need them.
11220 		 */
11221 		if (!ipif->ipif_isv6)
11222 			ipif_check_bcast_ires(ipif);
11223 
11224 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11225 		    !(turn_off & IPIF_UP)) {
11226 			need_up = B_TRUE;
11227 			if (ipif->ipif_flags & IPIF_UP)
11228 				ill->ill_logical_down = 1;
11229 			turn_on &= ~IPIF_UP;
11230 		}
11231 		err = ipif_down(ipif, q, mp);
11232 		ip1dbg(("ipif_down returns %d err ", err));
11233 		if (err == EINPROGRESS)
11234 			return (err);
11235 		ipif_down_tail(ipif);
11236 	}
11237 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11238 }
11239 
11240 static int
11241 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11242     boolean_t need_up)
11243 {
11244 	ill_t	*ill;
11245 	phyint_t *phyi;
11246 	uint64_t turn_on;
11247 	uint64_t turn_off;
11248 	uint64_t intf_flags;
11249 	boolean_t phyint_flags_modified = B_FALSE;
11250 	int	err = 0;
11251 	boolean_t set_linklocal = B_FALSE;
11252 	boolean_t zero_source = B_FALSE;
11253 
11254 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11255 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11256 
11257 	ASSERT(IAM_WRITER_IPIF(ipif));
11258 
11259 	ill = ipif->ipif_ill;
11260 	phyi = ill->ill_phyint;
11261 
11262 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11263 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11264 
11265 	turn_off = intf_flags & turn_on;
11266 	turn_on ^= turn_off;
11267 
11268 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11269 		phyint_flags_modified = B_TRUE;
11270 
11271 	/*
11272 	 * Now we change the flags. Track current value of
11273 	 * other flags in their respective places.
11274 	 */
11275 	mutex_enter(&ill->ill_lock);
11276 	mutex_enter(&phyi->phyint_lock);
11277 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11278 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11279 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11280 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11281 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11282 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11283 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11284 		set_linklocal = B_TRUE;
11285 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11286 	}
11287 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11288 		zero_source = B_TRUE;
11289 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11290 	}
11291 	mutex_exit(&ill->ill_lock);
11292 	mutex_exit(&phyi->phyint_lock);
11293 
11294 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11295 		ip_redo_nomination(phyi);
11296 
11297 	if (set_linklocal)
11298 		(void) ipif_setlinklocal(ipif);
11299 
11300 	if (zero_source)
11301 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11302 	else
11303 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11304 
11305 	if (need_up) {
11306 		/*
11307 		 * XXX ipif_up really does not know whether a phyint flags
11308 		 * was modified or not. So, it sends up information on
11309 		 * only one routing sockets message. As we don't bring up
11310 		 * the interface and also set STANDBY/FAILED simultaneously
11311 		 * it should be okay.
11312 		 */
11313 		err = ipif_up(ipif, q, mp);
11314 	} else {
11315 		/*
11316 		 * Make sure routing socket sees all changes to the flags.
11317 		 * ipif_up_done* handles this when we use ipif_up.
11318 		 */
11319 		if (phyint_flags_modified) {
11320 			if (phyi->phyint_illv4 != NULL) {
11321 				ip_rts_ifmsg(phyi->phyint_illv4->
11322 				    ill_ipif);
11323 			}
11324 			if (phyi->phyint_illv6 != NULL) {
11325 				ip_rts_ifmsg(phyi->phyint_illv6->
11326 				    ill_ipif);
11327 			}
11328 		} else {
11329 			ip_rts_ifmsg(ipif);
11330 		}
11331 	}
11332 	return (err);
11333 }
11334 
11335 /*
11336  * Restart entry point to restart the flags restart operation after the
11337  * refcounts have dropped to zero.
11338  */
11339 /* ARGSUSED */
11340 int
11341 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11342     ip_ioctl_cmd_t *ipip, void *if_req)
11343 {
11344 	int	err;
11345 	struct ifreq *ifr = (struct ifreq *)if_req;
11346 	struct lifreq *lifr = (struct lifreq *)if_req;
11347 
11348 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11349 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11350 
11351 	ipif_down_tail(ipif);
11352 	if (ipip->ipi_cmd_type == IF_CMD) {
11353 		/*
11354 		 * Since ip_sioctl_flags expects an int and ifr_flags
11355 		 * is a short we need to cast ifr_flags into an int
11356 		 * to avoid having sign extension cause bits to get
11357 		 * set that should not be.
11358 		 */
11359 		err = ip_sioctl_flags_tail(ipif,
11360 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11361 		    q, mp, B_TRUE);
11362 	} else {
11363 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11364 		    q, mp, B_TRUE);
11365 	}
11366 	return (err);
11367 }
11368 
11369 /* ARGSUSED */
11370 int
11371 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11372     ip_ioctl_cmd_t *ipip, void *if_req)
11373 {
11374 	/*
11375 	 * Has the flags been set correctly till now ?
11376 	 */
11377 	ill_t *ill = ipif->ipif_ill;
11378 	phyint_t *phyi = ill->ill_phyint;
11379 
11380 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11381 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11382 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11383 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11384 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11385 
11386 	/*
11387 	 * Need a lock since some flags can be set even when there are
11388 	 * references to the ipif.
11389 	 */
11390 	mutex_enter(&ill->ill_lock);
11391 	if (ipip->ipi_cmd_type == IF_CMD) {
11392 		struct ifreq *ifr = (struct ifreq *)if_req;
11393 
11394 		/* Get interface flags (low 16 only). */
11395 		ifr->ifr_flags = ((ipif->ipif_flags |
11396 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11397 	} else {
11398 		struct lifreq *lifr = (struct lifreq *)if_req;
11399 
11400 		/* Get interface flags. */
11401 		lifr->lifr_flags = ipif->ipif_flags |
11402 		    ill->ill_flags | phyi->phyint_flags;
11403 	}
11404 	mutex_exit(&ill->ill_lock);
11405 	return (0);
11406 }
11407 
11408 /* ARGSUSED */
11409 int
11410 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11411     ip_ioctl_cmd_t *ipip, void *if_req)
11412 {
11413 	int mtu;
11414 	int ip_min_mtu;
11415 	struct ifreq	*ifr;
11416 	struct lifreq *lifr;
11417 	ire_t	*ire;
11418 
11419 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11420 	    ipif->ipif_id, (void *)ipif));
11421 	if (ipip->ipi_cmd_type == IF_CMD) {
11422 		ifr = (struct ifreq *)if_req;
11423 		mtu = ifr->ifr_metric;
11424 	} else {
11425 		lifr = (struct lifreq *)if_req;
11426 		mtu = lifr->lifr_mtu;
11427 	}
11428 
11429 	if (ipif->ipif_isv6)
11430 		ip_min_mtu = IPV6_MIN_MTU;
11431 	else
11432 		ip_min_mtu = IP_MIN_MTU;
11433 
11434 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11435 		return (EINVAL);
11436 
11437 	/*
11438 	 * Change the MTU size in all relevant ire's.
11439 	 * Mtu change Vs. new ire creation - protocol below.
11440 	 * First change ipif_mtu and the ire_max_frag of the
11441 	 * interface ire. Then do an ire walk and change the
11442 	 * ire_max_frag of all affected ires. During ire_add
11443 	 * under the bucket lock, set the ire_max_frag of the
11444 	 * new ire being created from the ipif/ire from which
11445 	 * it is being derived. If an mtu change happens after
11446 	 * the ire is added, the new ire will be cleaned up.
11447 	 * Conversely if the mtu change happens before the ire
11448 	 * is added, ire_add will see the new value of the mtu.
11449 	 */
11450 	ipif->ipif_mtu = mtu;
11451 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11452 
11453 	if (ipif->ipif_isv6)
11454 		ire = ipif_to_ire_v6(ipif);
11455 	else
11456 		ire = ipif_to_ire(ipif);
11457 	if (ire != NULL) {
11458 		ire->ire_max_frag = ipif->ipif_mtu;
11459 		ire_refrele(ire);
11460 	}
11461 	if (ipif->ipif_flags & IPIF_UP) {
11462 		if (ipif->ipif_isv6)
11463 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11464 		else
11465 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11466 	}
11467 	/* Update the MTU in SCTP's list */
11468 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11469 	return (0);
11470 }
11471 
11472 /* Get interface MTU. */
11473 /* ARGSUSED */
11474 int
11475 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11476 	ip_ioctl_cmd_t *ipip, void *if_req)
11477 {
11478 	struct ifreq	*ifr;
11479 	struct lifreq	*lifr;
11480 
11481 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11482 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11483 	if (ipip->ipi_cmd_type == IF_CMD) {
11484 		ifr = (struct ifreq *)if_req;
11485 		ifr->ifr_metric = ipif->ipif_mtu;
11486 	} else {
11487 		lifr = (struct lifreq *)if_req;
11488 		lifr->lifr_mtu = ipif->ipif_mtu;
11489 	}
11490 	return (0);
11491 }
11492 
11493 /* Set interface broadcast address. */
11494 /* ARGSUSED2 */
11495 int
11496 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11497 	ip_ioctl_cmd_t *ipip, void *if_req)
11498 {
11499 	ipaddr_t addr;
11500 	ire_t	*ire;
11501 
11502 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11503 	    ipif->ipif_id));
11504 
11505 	ASSERT(IAM_WRITER_IPIF(ipif));
11506 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11507 		return (EADDRNOTAVAIL);
11508 
11509 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11510 
11511 	if (sin->sin_family != AF_INET)
11512 		return (EAFNOSUPPORT);
11513 
11514 	addr = sin->sin_addr.s_addr;
11515 	if (ipif->ipif_flags & IPIF_UP) {
11516 		/*
11517 		 * If we are already up, make sure the new
11518 		 * broadcast address makes sense.  If it does,
11519 		 * there should be an IRE for it already.
11520 		 * Don't match on ipif, only on the ill
11521 		 * since we are sharing these now. Don't use
11522 		 * MATCH_IRE_ILL_GROUP as we are looking for
11523 		 * the broadcast ire on this ill and each ill
11524 		 * in the group has its own broadcast ire.
11525 		 */
11526 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11527 		    ipif, ALL_ZONES, (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11528 		if (ire == NULL) {
11529 			return (EINVAL);
11530 		} else {
11531 			ire_refrele(ire);
11532 		}
11533 	}
11534 	/*
11535 	 * Changing the broadcast addr for this ipif.
11536 	 * Make sure we have valid net and subnet bcast
11537 	 * ire's for other logical interfaces, if needed.
11538 	 */
11539 	if (addr != ipif->ipif_brd_addr)
11540 		ipif_check_bcast_ires(ipif);
11541 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11542 	return (0);
11543 }
11544 
11545 /* Get interface broadcast address. */
11546 /* ARGSUSED */
11547 int
11548 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11549     ip_ioctl_cmd_t *ipip, void *if_req)
11550 {
11551 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11552 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11553 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11554 		return (EADDRNOTAVAIL);
11555 
11556 	/* IPIF_BROADCAST not possible with IPv6 */
11557 	ASSERT(!ipif->ipif_isv6);
11558 	*sin = sin_null;
11559 	sin->sin_family = AF_INET;
11560 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11561 	return (0);
11562 }
11563 
11564 /*
11565  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11566  */
11567 /* ARGSUSED */
11568 int
11569 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11570     ip_ioctl_cmd_t *ipip, void *if_req)
11571 {
11572 	int err = 0;
11573 	in6_addr_t v6mask;
11574 
11575 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11576 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11577 
11578 	ASSERT(IAM_WRITER_IPIF(ipif));
11579 
11580 	if (ipif->ipif_isv6) {
11581 		sin6_t *sin6;
11582 
11583 		if (sin->sin_family != AF_INET6)
11584 			return (EAFNOSUPPORT);
11585 
11586 		sin6 = (sin6_t *)sin;
11587 		v6mask = sin6->sin6_addr;
11588 	} else {
11589 		ipaddr_t mask;
11590 
11591 		if (sin->sin_family != AF_INET)
11592 			return (EAFNOSUPPORT);
11593 
11594 		mask = sin->sin_addr.s_addr;
11595 		V4MASK_TO_V6(mask, v6mask);
11596 	}
11597 
11598 	/*
11599 	 * No big deal if the interface isn't already up, or the mask
11600 	 * isn't really changing, or this is pt-pt.
11601 	 */
11602 	if (!(ipif->ipif_flags & IPIF_UP) ||
11603 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11604 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11605 		ipif->ipif_v6net_mask = v6mask;
11606 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11607 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11608 			    ipif->ipif_v6net_mask,
11609 			    ipif->ipif_v6subnet);
11610 		}
11611 		return (0);
11612 	}
11613 	/*
11614 	 * Make sure we have valid net and subnet broadcast ire's
11615 	 * for the old netmask, if needed by other logical interfaces.
11616 	 */
11617 	if (!ipif->ipif_isv6)
11618 		ipif_check_bcast_ires(ipif);
11619 
11620 	err = ipif_logical_down(ipif, q, mp);
11621 	if (err == EINPROGRESS)
11622 		return (err);
11623 	ipif_down_tail(ipif);
11624 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11625 	return (err);
11626 }
11627 
11628 static int
11629 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11630 {
11631 	in6_addr_t v6mask;
11632 	int err = 0;
11633 
11634 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11635 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11636 
11637 	if (ipif->ipif_isv6) {
11638 		sin6_t *sin6;
11639 
11640 		sin6 = (sin6_t *)sin;
11641 		v6mask = sin6->sin6_addr;
11642 	} else {
11643 		ipaddr_t mask;
11644 
11645 		mask = sin->sin_addr.s_addr;
11646 		V4MASK_TO_V6(mask, v6mask);
11647 	}
11648 
11649 	ipif->ipif_v6net_mask = v6mask;
11650 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11651 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11652 		    ipif->ipif_v6subnet);
11653 	}
11654 	err = ipif_up(ipif, q, mp);
11655 
11656 	if (err == 0 || err == EINPROGRESS) {
11657 		/*
11658 		 * The interface must be DL_BOUND if this packet has to
11659 		 * go out on the wire. Since we only go through a logical
11660 		 * down and are bound with the driver during an internal
11661 		 * down/up that is satisfied.
11662 		 */
11663 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11664 			/* Potentially broadcast an address mask reply. */
11665 			ipif_mask_reply(ipif);
11666 		}
11667 	}
11668 	return (err);
11669 }
11670 
11671 /* ARGSUSED */
11672 int
11673 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11674     ip_ioctl_cmd_t *ipip, void *if_req)
11675 {
11676 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11677 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11678 	ipif_down_tail(ipif);
11679 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11680 }
11681 
11682 /* Get interface net mask. */
11683 /* ARGSUSED */
11684 int
11685 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11686     ip_ioctl_cmd_t *ipip, void *if_req)
11687 {
11688 	struct lifreq *lifr = (struct lifreq *)if_req;
11689 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11690 
11691 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11692 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11693 
11694 	/*
11695 	 * net mask can't change since we have a reference to the ipif.
11696 	 */
11697 	if (ipif->ipif_isv6) {
11698 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11699 		*sin6 = sin6_null;
11700 		sin6->sin6_family = AF_INET6;
11701 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11702 		lifr->lifr_addrlen =
11703 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11704 	} else {
11705 		*sin = sin_null;
11706 		sin->sin_family = AF_INET;
11707 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11708 		if (ipip->ipi_cmd_type == LIF_CMD) {
11709 			lifr->lifr_addrlen =
11710 			    ip_mask_to_plen(ipif->ipif_net_mask);
11711 		}
11712 	}
11713 	return (0);
11714 }
11715 
11716 /* ARGSUSED */
11717 int
11718 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11719     ip_ioctl_cmd_t *ipip, void *if_req)
11720 {
11721 
11722 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11723 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11724 	/*
11725 	 * Set interface metric.  We don't use this for
11726 	 * anything but we keep track of it in case it is
11727 	 * important to routing applications or such.
11728 	 */
11729 	if (ipip->ipi_cmd_type == IF_CMD) {
11730 		struct ifreq    *ifr;
11731 
11732 		ifr = (struct ifreq *)if_req;
11733 		ipif->ipif_metric = ifr->ifr_metric;
11734 	} else {
11735 		struct lifreq   *lifr;
11736 
11737 		lifr = (struct lifreq *)if_req;
11738 		ipif->ipif_metric = lifr->lifr_metric;
11739 	}
11740 	return (0);
11741 }
11742 
11743 
11744 /* ARGSUSED */
11745 int
11746 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11747     ip_ioctl_cmd_t *ipip, void *if_req)
11748 {
11749 
11750 	/* Get interface metric. */
11751 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11752 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11753 	if (ipip->ipi_cmd_type == IF_CMD) {
11754 		struct ifreq    *ifr;
11755 
11756 		ifr = (struct ifreq *)if_req;
11757 		ifr->ifr_metric = ipif->ipif_metric;
11758 	} else {
11759 		struct lifreq   *lifr;
11760 
11761 		lifr = (struct lifreq *)if_req;
11762 		lifr->lifr_metric = ipif->ipif_metric;
11763 	}
11764 
11765 	return (0);
11766 }
11767 
11768 /* ARGSUSED */
11769 int
11770 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11771     ip_ioctl_cmd_t *ipip, void *if_req)
11772 {
11773 
11774 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11775 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11776 	/*
11777 	 * Set the muxid returned from I_PLINK.
11778 	 */
11779 	if (ipip->ipi_cmd_type == IF_CMD) {
11780 		struct ifreq *ifr = (struct ifreq *)if_req;
11781 
11782 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11783 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11784 	} else {
11785 		struct lifreq *lifr = (struct lifreq *)if_req;
11786 
11787 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11788 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11789 	}
11790 	return (0);
11791 }
11792 
11793 /* ARGSUSED */
11794 int
11795 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11796     ip_ioctl_cmd_t *ipip, void *if_req)
11797 {
11798 
11799 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11800 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11801 	/*
11802 	 * Get the muxid saved in ill for I_PUNLINK.
11803 	 */
11804 	if (ipip->ipi_cmd_type == IF_CMD) {
11805 		struct ifreq *ifr = (struct ifreq *)if_req;
11806 
11807 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11808 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11809 	} else {
11810 		struct lifreq *lifr = (struct lifreq *)if_req;
11811 
11812 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11813 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11814 	}
11815 	return (0);
11816 }
11817 
11818 /*
11819  * Set the subnet prefix. Does not modify the broadcast address.
11820  */
11821 /* ARGSUSED */
11822 int
11823 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11824     ip_ioctl_cmd_t *ipip, void *if_req)
11825 {
11826 	int err = 0;
11827 	in6_addr_t v6addr;
11828 	in6_addr_t v6mask;
11829 	boolean_t need_up = B_FALSE;
11830 	int addrlen;
11831 
11832 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11833 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11834 
11835 	ASSERT(IAM_WRITER_IPIF(ipif));
11836 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11837 
11838 	if (ipif->ipif_isv6) {
11839 		sin6_t *sin6;
11840 
11841 		if (sin->sin_family != AF_INET6)
11842 			return (EAFNOSUPPORT);
11843 
11844 		sin6 = (sin6_t *)sin;
11845 		v6addr = sin6->sin6_addr;
11846 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11847 			return (EADDRNOTAVAIL);
11848 	} else {
11849 		ipaddr_t addr;
11850 
11851 		if (sin->sin_family != AF_INET)
11852 			return (EAFNOSUPPORT);
11853 
11854 		addr = sin->sin_addr.s_addr;
11855 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11856 			return (EADDRNOTAVAIL);
11857 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11858 		/* Add 96 bits */
11859 		addrlen += IPV6_ABITS - IP_ABITS;
11860 	}
11861 
11862 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11863 		return (EINVAL);
11864 
11865 	/* Check if bits in the address is set past the mask */
11866 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11867 		return (EINVAL);
11868 
11869 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11870 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11871 		return (0);	/* No change */
11872 
11873 	if (ipif->ipif_flags & IPIF_UP) {
11874 		/*
11875 		 * If the interface is already marked up,
11876 		 * we call ipif_down which will take care
11877 		 * of ditching any IREs that have been set
11878 		 * up based on the old interface address.
11879 		 */
11880 		err = ipif_logical_down(ipif, q, mp);
11881 		if (err == EINPROGRESS)
11882 			return (err);
11883 		ipif_down_tail(ipif);
11884 		need_up = B_TRUE;
11885 	}
11886 
11887 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11888 	return (err);
11889 }
11890 
11891 static int
11892 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11893     queue_t *q, mblk_t *mp, boolean_t need_up)
11894 {
11895 	ill_t	*ill = ipif->ipif_ill;
11896 	int	err = 0;
11897 
11898 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11899 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11900 
11901 	/* Set the new address. */
11902 	mutex_enter(&ill->ill_lock);
11903 	ipif->ipif_v6net_mask = v6mask;
11904 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11905 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11906 		    ipif->ipif_v6subnet);
11907 	}
11908 	mutex_exit(&ill->ill_lock);
11909 
11910 	if (need_up) {
11911 		/*
11912 		 * Now bring the interface back up.  If this
11913 		 * is the only IPIF for the ILL, ipif_up
11914 		 * will have to re-bind to the device, so
11915 		 * we may get back EINPROGRESS, in which
11916 		 * case, this IOCTL will get completed in
11917 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11918 		 */
11919 		err = ipif_up(ipif, q, mp);
11920 		if (err == EINPROGRESS)
11921 			return (err);
11922 	}
11923 	return (err);
11924 }
11925 
11926 /* ARGSUSED */
11927 int
11928 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11929     ip_ioctl_cmd_t *ipip, void *if_req)
11930 {
11931 	int	addrlen;
11932 	in6_addr_t v6addr;
11933 	in6_addr_t v6mask;
11934 	struct lifreq *lifr = (struct lifreq *)if_req;
11935 
11936 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11937 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11938 	ipif_down_tail(ipif);
11939 
11940 	addrlen = lifr->lifr_addrlen;
11941 	if (ipif->ipif_isv6) {
11942 		sin6_t *sin6;
11943 
11944 		sin6 = (sin6_t *)sin;
11945 		v6addr = sin6->sin6_addr;
11946 	} else {
11947 		ipaddr_t addr;
11948 
11949 		addr = sin->sin_addr.s_addr;
11950 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11951 		addrlen += IPV6_ABITS - IP_ABITS;
11952 	}
11953 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11954 
11955 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11956 }
11957 
11958 /* ARGSUSED */
11959 int
11960 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11961     ip_ioctl_cmd_t *ipip, void *if_req)
11962 {
11963 	struct lifreq *lifr = (struct lifreq *)if_req;
11964 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11965 
11966 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11967 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11968 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11969 
11970 	if (ipif->ipif_isv6) {
11971 		*sin6 = sin6_null;
11972 		sin6->sin6_family = AF_INET6;
11973 		sin6->sin6_addr = ipif->ipif_v6subnet;
11974 		lifr->lifr_addrlen =
11975 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11976 	} else {
11977 		*sin = sin_null;
11978 		sin->sin_family = AF_INET;
11979 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11980 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11981 	}
11982 	return (0);
11983 }
11984 
11985 /*
11986  * Set the IPv6 address token.
11987  */
11988 /* ARGSUSED */
11989 int
11990 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11991     ip_ioctl_cmd_t *ipi, void *if_req)
11992 {
11993 	ill_t *ill = ipif->ipif_ill;
11994 	int err;
11995 	in6_addr_t v6addr;
11996 	in6_addr_t v6mask;
11997 	boolean_t need_up = B_FALSE;
11998 	int i;
11999 	sin6_t *sin6 = (sin6_t *)sin;
12000 	struct lifreq *lifr = (struct lifreq *)if_req;
12001 	int addrlen;
12002 
12003 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12004 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12005 	ASSERT(IAM_WRITER_IPIF(ipif));
12006 
12007 	addrlen = lifr->lifr_addrlen;
12008 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12009 	if (ipif->ipif_id != 0)
12010 		return (EINVAL);
12011 
12012 	if (!ipif->ipif_isv6)
12013 		return (EINVAL);
12014 
12015 	if (addrlen > IPV6_ABITS)
12016 		return (EINVAL);
12017 
12018 	v6addr = sin6->sin6_addr;
12019 
12020 	/*
12021 	 * The length of the token is the length from the end.  To get
12022 	 * the proper mask for this, compute the mask of the bits not
12023 	 * in the token; ie. the prefix, and then xor to get the mask.
12024 	 */
12025 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12026 		return (EINVAL);
12027 	for (i = 0; i < 4; i++) {
12028 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12029 	}
12030 
12031 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12032 	    ill->ill_token_length == addrlen)
12033 		return (0);	/* No change */
12034 
12035 	if (ipif->ipif_flags & IPIF_UP) {
12036 		err = ipif_logical_down(ipif, q, mp);
12037 		if (err == EINPROGRESS)
12038 			return (err);
12039 		ipif_down_tail(ipif);
12040 		need_up = B_TRUE;
12041 	}
12042 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12043 	return (err);
12044 }
12045 
12046 static int
12047 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12048     mblk_t *mp, boolean_t need_up)
12049 {
12050 	in6_addr_t v6addr;
12051 	in6_addr_t v6mask;
12052 	ill_t	*ill = ipif->ipif_ill;
12053 	int	i;
12054 	int	err = 0;
12055 
12056 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12057 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12058 	v6addr = sin6->sin6_addr;
12059 	/*
12060 	 * The length of the token is the length from the end.  To get
12061 	 * the proper mask for this, compute the mask of the bits not
12062 	 * in the token; ie. the prefix, and then xor to get the mask.
12063 	 */
12064 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12065 	for (i = 0; i < 4; i++)
12066 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12067 
12068 	mutex_enter(&ill->ill_lock);
12069 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12070 	ill->ill_token_length = addrlen;
12071 	mutex_exit(&ill->ill_lock);
12072 
12073 	if (need_up) {
12074 		/*
12075 		 * Now bring the interface back up.  If this
12076 		 * is the only IPIF for the ILL, ipif_up
12077 		 * will have to re-bind to the device, so
12078 		 * we may get back EINPROGRESS, in which
12079 		 * case, this IOCTL will get completed in
12080 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12081 		 */
12082 		err = ipif_up(ipif, q, mp);
12083 		if (err == EINPROGRESS)
12084 			return (err);
12085 	}
12086 	return (err);
12087 }
12088 
12089 /* ARGSUSED */
12090 int
12091 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12092     ip_ioctl_cmd_t *ipi, void *if_req)
12093 {
12094 	ill_t *ill;
12095 	sin6_t *sin6 = (sin6_t *)sin;
12096 	struct lifreq *lifr = (struct lifreq *)if_req;
12097 
12098 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12099 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12100 	if (ipif->ipif_id != 0)
12101 		return (EINVAL);
12102 
12103 	ill = ipif->ipif_ill;
12104 	if (!ill->ill_isv6)
12105 		return (ENXIO);
12106 
12107 	*sin6 = sin6_null;
12108 	sin6->sin6_family = AF_INET6;
12109 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12110 	sin6->sin6_addr = ill->ill_token;
12111 	lifr->lifr_addrlen = ill->ill_token_length;
12112 	return (0);
12113 }
12114 
12115 /*
12116  * Set (hardware) link specific information that might override
12117  * what was acquired through the DL_INFO_ACK.
12118  * The logic is as follows.
12119  *
12120  * become exclusive
12121  * set CHANGING flag
12122  * change mtu on affected IREs
12123  * clear CHANGING flag
12124  *
12125  * An ire add that occurs before the CHANGING flag is set will have its mtu
12126  * changed by the ip_sioctl_lnkinfo.
12127  *
12128  * During the time the CHANGING flag is set, no new ires will be added to the
12129  * bucket, and ire add will fail (due the CHANGING flag).
12130  *
12131  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12132  * before it is added to the bucket.
12133  *
12134  * Obviously only 1 thread can set the CHANGING flag and we need to become
12135  * exclusive to set the flag.
12136  */
12137 /* ARGSUSED */
12138 int
12139 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12140     ip_ioctl_cmd_t *ipi, void *if_req)
12141 {
12142 	ill_t		*ill = ipif->ipif_ill;
12143 	ipif_t		*nipif;
12144 	int		ip_min_mtu;
12145 	boolean_t	mtu_walk = B_FALSE;
12146 	struct lifreq	*lifr = (struct lifreq *)if_req;
12147 	lif_ifinfo_req_t *lir;
12148 	ire_t		*ire;
12149 
12150 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12151 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12152 	lir = &lifr->lifr_ifinfo;
12153 	ASSERT(IAM_WRITER_IPIF(ipif));
12154 
12155 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12156 	if (ipif->ipif_id != 0)
12157 		return (EINVAL);
12158 
12159 	/* Set interface MTU. */
12160 	if (ipif->ipif_isv6)
12161 		ip_min_mtu = IPV6_MIN_MTU;
12162 	else
12163 		ip_min_mtu = IP_MIN_MTU;
12164 
12165 	/*
12166 	 * Verify values before we set anything. Allow zero to
12167 	 * mean unspecified.
12168 	 */
12169 	if (lir->lir_maxmtu != 0 &&
12170 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12171 	    lir->lir_maxmtu < ip_min_mtu))
12172 		return (EINVAL);
12173 	if (lir->lir_reachtime != 0 &&
12174 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12175 		return (EINVAL);
12176 	if (lir->lir_reachretrans != 0 &&
12177 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12178 		return (EINVAL);
12179 
12180 	mutex_enter(&ill->ill_lock);
12181 	ill->ill_state_flags |= ILL_CHANGING;
12182 	for (nipif = ill->ill_ipif; nipif != NULL;
12183 	    nipif = nipif->ipif_next) {
12184 		nipif->ipif_state_flags |= IPIF_CHANGING;
12185 	}
12186 
12187 	mutex_exit(&ill->ill_lock);
12188 
12189 	if (lir->lir_maxmtu != 0) {
12190 		ill->ill_max_mtu = lir->lir_maxmtu;
12191 		ill->ill_mtu_userspecified = 1;
12192 		mtu_walk = B_TRUE;
12193 	}
12194 
12195 	if (lir->lir_reachtime != 0)
12196 		ill->ill_reachable_time = lir->lir_reachtime;
12197 
12198 	if (lir->lir_reachretrans != 0)
12199 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12200 
12201 	ill->ill_max_hops = lir->lir_maxhops;
12202 
12203 	ill->ill_max_buf = ND_MAX_Q;
12204 
12205 	if (mtu_walk) {
12206 		/*
12207 		 * Set the MTU on all ipifs associated with this ill except
12208 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12209 		 */
12210 		for (nipif = ill->ill_ipif; nipif != NULL;
12211 		    nipif = nipif->ipif_next) {
12212 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12213 				continue;
12214 
12215 			nipif->ipif_mtu = ill->ill_max_mtu;
12216 
12217 			if (!(nipif->ipif_flags & IPIF_UP))
12218 				continue;
12219 
12220 			if (nipif->ipif_isv6)
12221 				ire = ipif_to_ire_v6(nipif);
12222 			else
12223 				ire = ipif_to_ire(nipif);
12224 			if (ire != NULL) {
12225 				ire->ire_max_frag = ipif->ipif_mtu;
12226 				ire_refrele(ire);
12227 			}
12228 			if (ill->ill_isv6) {
12229 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12230 				    ipif_mtu_change, (char *)nipif,
12231 				    ill);
12232 			} else {
12233 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12234 				    ipif_mtu_change, (char *)nipif,
12235 				    ill);
12236 			}
12237 		}
12238 	}
12239 
12240 	mutex_enter(&ill->ill_lock);
12241 	for (nipif = ill->ill_ipif; nipif != NULL;
12242 	    nipif = nipif->ipif_next) {
12243 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12244 	}
12245 	ILL_UNMARK_CHANGING(ill);
12246 	mutex_exit(&ill->ill_lock);
12247 
12248 	return (0);
12249 }
12250 
12251 /* ARGSUSED */
12252 int
12253 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12254     ip_ioctl_cmd_t *ipi, void *if_req)
12255 {
12256 	struct lif_ifinfo_req *lir;
12257 	ill_t *ill = ipif->ipif_ill;
12258 
12259 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12260 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12261 	if (ipif->ipif_id != 0)
12262 		return (EINVAL);
12263 
12264 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12265 	lir->lir_maxhops = ill->ill_max_hops;
12266 	lir->lir_reachtime = ill->ill_reachable_time;
12267 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12268 	lir->lir_maxmtu = ill->ill_max_mtu;
12269 
12270 	return (0);
12271 }
12272 
12273 /*
12274  * Return best guess as to the subnet mask for the specified address.
12275  * Based on the subnet masks for all the configured interfaces.
12276  *
12277  * We end up returning a zero mask in the case of default, multicast or
12278  * experimental.
12279  */
12280 static ipaddr_t
12281 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12282 {
12283 	ipaddr_t net_mask;
12284 	ill_t	*ill;
12285 	ipif_t	*ipif;
12286 	ill_walk_context_t ctx;
12287 	ipif_t	*fallback_ipif = NULL;
12288 
12289 	net_mask = ip_net_mask(addr);
12290 	if (net_mask == 0) {
12291 		*ipifp = NULL;
12292 		return (0);
12293 	}
12294 
12295 	/* Let's check to see if this is maybe a local subnet route. */
12296 	/* this function only applies to IPv4 interfaces */
12297 	rw_enter(&ill_g_lock, RW_READER);
12298 	ill = ILL_START_WALK_V4(&ctx);
12299 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12300 		mutex_enter(&ill->ill_lock);
12301 		for (ipif = ill->ill_ipif; ipif != NULL;
12302 		    ipif = ipif->ipif_next) {
12303 			if (!IPIF_CAN_LOOKUP(ipif))
12304 				continue;
12305 			if (!(ipif->ipif_flags & IPIF_UP))
12306 				continue;
12307 			if ((ipif->ipif_subnet & net_mask) ==
12308 			    (addr & net_mask)) {
12309 				/*
12310 				 * Don't trust pt-pt interfaces if there are
12311 				 * other interfaces.
12312 				 */
12313 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12314 					if (fallback_ipif == NULL) {
12315 						ipif_refhold_locked(ipif);
12316 						fallback_ipif = ipif;
12317 					}
12318 					continue;
12319 				}
12320 
12321 				/*
12322 				 * Fine. Just assume the same net mask as the
12323 				 * directly attached subnet interface is using.
12324 				 */
12325 				ipif_refhold_locked(ipif);
12326 				mutex_exit(&ill->ill_lock);
12327 				rw_exit(&ill_g_lock);
12328 				if (fallback_ipif != NULL)
12329 					ipif_refrele(fallback_ipif);
12330 				*ipifp = ipif;
12331 				return (ipif->ipif_net_mask);
12332 			}
12333 		}
12334 		mutex_exit(&ill->ill_lock);
12335 	}
12336 	rw_exit(&ill_g_lock);
12337 
12338 	*ipifp = fallback_ipif;
12339 	return ((fallback_ipif != NULL) ?
12340 	    fallback_ipif->ipif_net_mask : net_mask);
12341 }
12342 
12343 /*
12344  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12345  */
12346 static void
12347 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12348 {
12349 	IOCP	iocp;
12350 	ipft_t	*ipft;
12351 	ipllc_t	*ipllc;
12352 	mblk_t	*mp1;
12353 	cred_t	*cr;
12354 	int	error = 0;
12355 	conn_t	*connp;
12356 
12357 	ip1dbg(("ip_wput_ioctl"));
12358 	iocp = (IOCP)mp->b_rptr;
12359 	mp1 = mp->b_cont;
12360 	if (mp1 == NULL) {
12361 		iocp->ioc_error = EINVAL;
12362 		mp->b_datap->db_type = M_IOCNAK;
12363 		iocp->ioc_count = 0;
12364 		qreply(q, mp);
12365 		return;
12366 	}
12367 
12368 	/*
12369 	 * These IOCTLs provide various control capabilities to
12370 	 * upstream agents such as ULPs and processes.	There
12371 	 * are currently two such IOCTLs implemented.  They
12372 	 * are used by TCP to provide update information for
12373 	 * existing IREs and to forcibly delete an IRE for a
12374 	 * host that is not responding, thereby forcing an
12375 	 * attempt at a new route.
12376 	 */
12377 	iocp->ioc_error = EINVAL;
12378 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12379 		goto done;
12380 
12381 	ipllc = (ipllc_t *)mp1->b_rptr;
12382 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12383 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12384 			break;
12385 	}
12386 	/*
12387 	 * prefer credential from mblk over ioctl;
12388 	 * see ip_sioctl_copyin_setup
12389 	 */
12390 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12391 
12392 	/*
12393 	 * Refhold the conn in case the request gets queued up in some lookup
12394 	 */
12395 	ASSERT(CONN_Q(q));
12396 	connp = Q_TO_CONN(q);
12397 	CONN_INC_REF(connp);
12398 	if (ipft->ipft_pfi &&
12399 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12400 		pullupmsg(mp1, ipft->ipft_min_size))) {
12401 		error = (*ipft->ipft_pfi)(q,
12402 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12403 	}
12404 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12405 		/*
12406 		 * CONN_OPER_PENDING_DONE happens in the function called
12407 		 * through ipft_pfi above.
12408 		 */
12409 		return;
12410 	}
12411 
12412 	CONN_OPER_PENDING_DONE(connp);
12413 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12414 		freemsg(mp);
12415 		return;
12416 	}
12417 	iocp->ioc_error = error;
12418 
12419 done:
12420 	mp->b_datap->db_type = M_IOCACK;
12421 	if (iocp->ioc_error)
12422 		iocp->ioc_count = 0;
12423 	qreply(q, mp);
12424 }
12425 
12426 /*
12427  * Lookup an ipif using the sequence id (ipif_seqid)
12428  */
12429 ipif_t *
12430 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12431 {
12432 	ipif_t *ipif;
12433 
12434 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12435 
12436 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12437 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12438 			return (ipif);
12439 	}
12440 	return (NULL);
12441 }
12442 
12443 uint64_t ipif_g_seqid;
12444 
12445 /*
12446  * Assign a unique id for the ipif. This is used later when we send
12447  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12448  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12449  * IRE is added, we verify that ipif has not disappeared.
12450  */
12451 
12452 static void
12453 ipif_assign_seqid(ipif_t *ipif)
12454 {
12455 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12456 }
12457 
12458 /*
12459  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12460  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12461  * be inserted into the first space available in the list. The value of
12462  * ipif_id will then be set to the appropriate value for its position.
12463  */
12464 static int
12465 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12466 {
12467 	ill_t *ill;
12468 	ipif_t *tipif;
12469 	ipif_t **tipifp;
12470 	int id;
12471 
12472 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12473 	    IAM_WRITER_IPIF(ipif));
12474 
12475 	ill = ipif->ipif_ill;
12476 	ASSERT(ill != NULL);
12477 
12478 	/*
12479 	 * In the case of lo0:0 we already hold the ill_g_lock.
12480 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12481 	 * ipif_insert. Another such caller is ipif_move.
12482 	 */
12483 	if (acquire_g_lock)
12484 		rw_enter(&ill_g_lock, RW_WRITER);
12485 	if (acquire_ill_lock)
12486 		mutex_enter(&ill->ill_lock);
12487 	id = ipif->ipif_id;
12488 	tipifp = &(ill->ill_ipif);
12489 	if (id == -1) {	/* need to find a real id */
12490 		id = 0;
12491 		while ((tipif = *tipifp) != NULL) {
12492 			ASSERT(tipif->ipif_id >= id);
12493 			if (tipif->ipif_id != id)
12494 				break; /* non-consecutive id */
12495 			id++;
12496 			tipifp = &(tipif->ipif_next);
12497 		}
12498 		/* limit number of logical interfaces */
12499 		if (id >= ip_addrs_per_if) {
12500 			if (acquire_ill_lock)
12501 				mutex_exit(&ill->ill_lock);
12502 			if (acquire_g_lock)
12503 				rw_exit(&ill_g_lock);
12504 			return (-1);
12505 		}
12506 		ipif->ipif_id = id; /* assign new id */
12507 	} else if (id < ip_addrs_per_if) {
12508 		/* we have a real id; insert ipif in the right place */
12509 		while ((tipif = *tipifp) != NULL) {
12510 			ASSERT(tipif->ipif_id != id);
12511 			if (tipif->ipif_id > id)
12512 				break; /* found correct location */
12513 			tipifp = &(tipif->ipif_next);
12514 		}
12515 	} else {
12516 		if (acquire_ill_lock)
12517 			mutex_exit(&ill->ill_lock);
12518 		if (acquire_g_lock)
12519 			rw_exit(&ill_g_lock);
12520 		return (-1);
12521 	}
12522 
12523 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12524 
12525 	ipif->ipif_next = tipif;
12526 	*tipifp = ipif;
12527 	if (acquire_ill_lock)
12528 		mutex_exit(&ill->ill_lock);
12529 	if (acquire_g_lock)
12530 		rw_exit(&ill_g_lock);
12531 	return (0);
12532 }
12533 
12534 /*
12535  * Allocate and initialize a new interface control structure.  (Always
12536  * called as writer.)
12537  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12538  * is not part of the global linked list of ills. ipif_seqid is unique
12539  * in the system and to preserve the uniqueness, it is assigned only
12540  * when ill becomes part of the global list. At that point ill will
12541  * have a name. If it doesn't get assigned here, it will get assigned
12542  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12543  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12544  * the interface flags or any other information from the DL_INFO_ACK for
12545  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12546  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12547  * second DL_INFO_ACK comes in from the driver.
12548  */
12549 static ipif_t *
12550 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12551 {
12552 	ipif_t	*ipif;
12553 	phyint_t *phyi;
12554 
12555 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12556 	    ill->ill_name, id, (void *)ill));
12557 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12558 
12559 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12560 		return (NULL);
12561 	*ipif = ipif_zero;	/* start clean */
12562 
12563 	ipif->ipif_ill = ill;
12564 	ipif->ipif_id = id;	/* could be -1 */
12565 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12566 
12567 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12568 
12569 	ipif->ipif_refcnt = 0;
12570 	ipif->ipif_saved_ire_cnt = 0;
12571 
12572 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12573 		mi_free(ipif);
12574 		return (NULL);
12575 	}
12576 	/* -1 id should have been replaced by real id */
12577 	id = ipif->ipif_id;
12578 	ASSERT(id >= 0);
12579 
12580 	if (ill->ill_name[0] != '\0') {
12581 		ipif_assign_seqid(ipif);
12582 		if (ill->ill_phyint->phyint_ifindex != 0)
12583 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12584 	}
12585 	/*
12586 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12587 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12588 	 * ioctl sets ipif_orig_ipifid to zero.
12589 	 */
12590 	ipif->ipif_orig_ipifid = id;
12591 
12592 	/*
12593 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12594 	 * The ipif is still not up and can't be looked up until the
12595 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12596 	 */
12597 	mutex_enter(&ill->ill_lock);
12598 	mutex_enter(&ill->ill_phyint->phyint_lock);
12599 	/*
12600 	 * Set the running flag when logical interface zero is created.
12601 	 * For subsequent logical interfaces, a DLPI link down
12602 	 * notification message may have cleared the running flag to
12603 	 * indicate the link is down, so we shouldn't just blindly set it.
12604 	 */
12605 	if (id == 0)
12606 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12607 	ipif->ipif_ire_type = ire_type;
12608 	phyi = ill->ill_phyint;
12609 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12610 
12611 	if (ipif->ipif_isv6) {
12612 		ill->ill_flags |= ILLF_IPV6;
12613 	} else {
12614 		ipaddr_t inaddr_any = INADDR_ANY;
12615 
12616 		ill->ill_flags |= ILLF_IPV4;
12617 
12618 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12619 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12620 		    &ipif->ipif_v6lcl_addr);
12621 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12622 		    &ipif->ipif_v6src_addr);
12623 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12624 		    &ipif->ipif_v6subnet);
12625 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12626 		    &ipif->ipif_v6net_mask);
12627 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12628 		    &ipif->ipif_v6brd_addr);
12629 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12630 		    &ipif->ipif_v6pp_dst_addr);
12631 	}
12632 
12633 	/*
12634 	 * Don't set the interface flags etc. now, will do it in
12635 	 * ip_ll_subnet_defaults.
12636 	 */
12637 	if (!initialize) {
12638 		mutex_exit(&ill->ill_lock);
12639 		mutex_exit(&ill->ill_phyint->phyint_lock);
12640 		return (ipif);
12641 	}
12642 	ipif->ipif_mtu = ill->ill_max_mtu;
12643 
12644 	if (ill->ill_bcast_addr_length != 0) {
12645 		/*
12646 		 * Later detect lack of DLPI driver multicast
12647 		 * capability by catching DL_ENABMULTI errors in
12648 		 * ip_rput_dlpi.
12649 		 */
12650 		ill->ill_flags |= ILLF_MULTICAST;
12651 		if (!ipif->ipif_isv6)
12652 			ipif->ipif_flags |= IPIF_BROADCAST;
12653 	} else {
12654 		if (ill->ill_net_type != IRE_LOOPBACK) {
12655 			if (ipif->ipif_isv6)
12656 				/*
12657 				 * Note: xresolv interfaces will eventually need
12658 				 * NOARP set here as well, but that will require
12659 				 * those external resolvers to have some
12660 				 * knowledge of that flag and act appropriately.
12661 				 * Not to be changed at present.
12662 				 */
12663 				ill->ill_flags |= ILLF_NONUD;
12664 			else
12665 				ill->ill_flags |= ILLF_NOARP;
12666 		}
12667 		if (ill->ill_phys_addr_length == 0) {
12668 			if (ill->ill_media &&
12669 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12670 				ipif->ipif_flags |= IPIF_NOXMIT;
12671 				phyi->phyint_flags |= PHYI_VIRTUAL;
12672 			} else {
12673 				/* pt-pt supports multicast. */
12674 				ill->ill_flags |= ILLF_MULTICAST;
12675 				if (ill->ill_net_type == IRE_LOOPBACK) {
12676 					phyi->phyint_flags |=
12677 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12678 				} else {
12679 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12680 				}
12681 			}
12682 		}
12683 	}
12684 	mutex_exit(&ill->ill_lock);
12685 	mutex_exit(&ill->ill_phyint->phyint_lock);
12686 	return (ipif);
12687 }
12688 
12689 /*
12690  * If appropriate, send a message up to the resolver delete the entry
12691  * for the address of this interface which is going out of business.
12692  * (Always called as writer).
12693  *
12694  * NOTE : We need to check for NULL mps as some of the fields are
12695  *	  initialized only for some interface types. See ipif_resolver_up()
12696  *	  for details.
12697  */
12698 void
12699 ipif_arp_down(ipif_t *ipif)
12700 {
12701 	mblk_t	*mp;
12702 
12703 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12704 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12705 	ASSERT(IAM_WRITER_IPIF(ipif));
12706 
12707 	/* Delete the mapping for the local address */
12708 	mp = ipif->ipif_arp_del_mp;
12709 	if (mp != NULL) {
12710 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12711 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12712 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12713 		putnext(ipif->ipif_ill->ill_rq, mp);
12714 		ipif->ipif_arp_del_mp = NULL;
12715 	}
12716 
12717 	/*
12718 	 * If this is the last ipif that is going down, we need
12719 	 * to clean up ARP completely.
12720 	 */
12721 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12722 
12723 		/* Send up AR_INTERFACE_DOWN message */
12724 		mp = ipif->ipif_ill->ill_arp_down_mp;
12725 		if (mp != NULL) {
12726 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12727 			    dlpi_prim_str(*(int *)mp->b_rptr),
12728 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12729 			    ipif->ipif_id));
12730 			putnext(ipif->ipif_ill->ill_rq, mp);
12731 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12732 		}
12733 
12734 		/* Tell ARP to delete the multicast mappings */
12735 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12736 		if (mp != NULL) {
12737 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12738 			    dlpi_prim_str(*(int *)mp->b_rptr),
12739 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12740 			    ipif->ipif_id));
12741 			putnext(ipif->ipif_ill->ill_rq, mp);
12742 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12743 		}
12744 	}
12745 }
12746 
12747 /*
12748  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12749  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12750  * that it wants the add_mp allocated in this function to be returned
12751  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12752  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12753  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12754  * as it does a ipif_arp_down after calling this function - which will
12755  * remove what we add here.
12756  *
12757  * Returns -1 on failures and 0 on success.
12758  */
12759 int
12760 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12761 {
12762 	mblk_t	*del_mp = NULL;
12763 	mblk_t *add_mp = NULL;
12764 	mblk_t *mp;
12765 	ill_t	*ill = ipif->ipif_ill;
12766 	phyint_t *phyi = ill->ill_phyint;
12767 	ipaddr_t addr, mask, extract_mask = 0;
12768 	arma_t	*arma;
12769 	uint8_t *maddr, *bphys_addr;
12770 	uint32_t hw_start;
12771 	dl_unitdata_req_t *dlur;
12772 
12773 	ASSERT(IAM_WRITER_IPIF(ipif));
12774 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12775 		return (0);
12776 
12777 	/*
12778 	 * Delete the existing mapping from ARP. Normally ipif_down
12779 	 * -> ipif_arp_down should send this up to ARP. The only
12780 	 * reason we would find this when we are switching from
12781 	 * Multicast to Broadcast where we did not do a down.
12782 	 */
12783 	mp = ill->ill_arp_del_mapping_mp;
12784 	if (mp != NULL) {
12785 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12786 		    dlpi_prim_str(*(int *)mp->b_rptr),
12787 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12788 		putnext(ill->ill_rq, mp);
12789 		ill->ill_arp_del_mapping_mp = NULL;
12790 	}
12791 
12792 	if (arp_add_mapping_mp != NULL)
12793 		*arp_add_mapping_mp = NULL;
12794 
12795 	/*
12796 	 * Check that the address is not to long for the constant
12797 	 * length reserved in the template arma_t.
12798 	 */
12799 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12800 		return (-1);
12801 
12802 	/* Add mapping mblk */
12803 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
12804 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
12805 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
12806 	    (caddr_t)&addr);
12807 	if (add_mp == NULL)
12808 		return (-1);
12809 	arma = (arma_t *)add_mp->b_rptr;
12810 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
12811 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
12812 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
12813 
12814 	/*
12815 	 * Determine the broadcast address.
12816 	 */
12817 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
12818 	if (ill->ill_sap_length < 0)
12819 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
12820 	else
12821 		bphys_addr = (uchar_t *)dlur +
12822 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
12823 	/*
12824 	 * Check PHYI_MULTI_BCAST and length of physical
12825 	 * address to determine if we use the mapping or the
12826 	 * broadcast address.
12827 	 */
12828 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
12829 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
12830 		    bphys_addr, maddr, &hw_start, &extract_mask))
12831 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
12832 
12833 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
12834 	    (ill->ill_flags & ILLF_MULTICAST)) {
12835 		/* Make sure this will not match the "exact" entry. */
12836 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
12837 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
12838 		    (caddr_t)&addr);
12839 		if (del_mp == NULL) {
12840 			freemsg(add_mp);
12841 			return (-1);
12842 		}
12843 		bcopy(&extract_mask, (char *)arma +
12844 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
12845 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
12846 			/* Use link-layer broadcast address for MULTI_BCAST */
12847 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
12848 			ip2dbg(("ipif_arp_setup_multicast: adding"
12849 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
12850 		} else {
12851 			arma->arma_hw_mapping_start = hw_start;
12852 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
12853 			    " ARP setup for %s\n", ill->ill_name));
12854 		}
12855 	} else {
12856 		freemsg(add_mp);
12857 		ASSERT(del_mp == NULL);
12858 		/* It is neither MULTICAST nor MULTI_BCAST */
12859 		return (0);
12860 	}
12861 	ASSERT(add_mp != NULL && del_mp != NULL);
12862 	ill->ill_arp_del_mapping_mp = del_mp;
12863 	if (arp_add_mapping_mp != NULL) {
12864 		/* The caller just wants the mblks allocated */
12865 		*arp_add_mapping_mp = add_mp;
12866 	} else {
12867 		/* The caller wants us to send it to arp */
12868 		putnext(ill->ill_rq, add_mp);
12869 	}
12870 	return (0);
12871 }
12872 
12873 /*
12874  * Get the resolver set up for a new interface address.
12875  * (Always called as writer.)
12876  * Called both for IPv4 and IPv6 interfaces,
12877  * though it only sets up the resolver for v6
12878  * if it's an xresolv interface (one using an external resolver).
12879  * Honors ILLF_NOARP.
12880  * The boolean value arp_just_publish, if B_TRUE, indicates that
12881  * it only needs to send an AR_ENTRY_ADD message up to ARP for
12882  * IPv4 interfaces. Currently, B_TRUE is only set when this
12883  * function is called by ip_rput_dlpi_writer() to handle
12884  * asynchronous hardware address change notification.
12885  * Returns error on failure.
12886  */
12887 int
12888 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
12889 {
12890 	caddr_t	addr;
12891 	mblk_t	*arp_up_mp = NULL;
12892 	mblk_t	*arp_down_mp = NULL;
12893 	mblk_t	*arp_add_mp = NULL;
12894 	mblk_t	*arp_del_mp = NULL;
12895 	mblk_t	*arp_add_mapping_mp = NULL;
12896 	mblk_t	*arp_del_mapping_mp = NULL;
12897 	ill_t	*ill = ipif->ipif_ill;
12898 	uchar_t	*area_p = NULL;
12899 	uchar_t	*ared_p = NULL;
12900 	int	err = ENOMEM;
12901 
12902 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12903 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
12904 	    (uint_t)ipif->ipif_flags));
12905 	ASSERT(IAM_WRITER_IPIF(ipif));
12906 
12907 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
12908 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
12909 		return (0);
12910 	}
12911 
12912 	if (ill->ill_isv6) {
12913 		/*
12914 		 * External resolver for IPv6
12915 		 */
12916 		ASSERT(!arp_just_publish);
12917 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12918 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
12919 			area_p = (uchar_t *)&ip6_area_template;
12920 			ared_p = (uchar_t *)&ip6_ared_template;
12921 		}
12922 	} else {
12923 		/*
12924 		 * IPv4 arp case. If the ARP stream has already started
12925 		 * closing, fail this request for ARP bringup. Else
12926 		 * record the fact that an ARP bringup is pending.
12927 		 */
12928 		mutex_enter(&ill->ill_lock);
12929 		if (ill->ill_arp_closing) {
12930 			mutex_exit(&ill->ill_lock);
12931 			err = EINVAL;
12932 			goto failed;
12933 		} else {
12934 			if (ill->ill_ipif_up_count == 0)
12935 				ill->ill_arp_bringup_pending = 1;
12936 			mutex_exit(&ill->ill_lock);
12937 		}
12938 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
12939 			addr = (caddr_t)&ipif->ipif_lcl_addr;
12940 			area_p = (uchar_t *)&ip_area_template;
12941 			ared_p = (uchar_t *)&ip_ared_template;
12942 		}
12943 	}
12944 
12945 	/*
12946 	 * Add an entry for the local address in ARP only if it
12947 	 * is not UNNUMBERED and the address is not INADDR_ANY.
12948 	 */
12949 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
12950 		/* Now ask ARP to publish our address. */
12951 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
12952 		if (arp_add_mp == NULL)
12953 			goto failed;
12954 		if (arp_just_publish) {
12955 			/*
12956 			 * Copy the new hardware address and length into
12957 			 * arp_add_mp to be sent to ARP.
12958 			 */
12959 			area_t *area = (area_t *)arp_add_mp->b_rptr;
12960 			area->area_hw_addr_length =
12961 			    ill->ill_phys_addr_length;
12962 			bcopy((char *)ill->ill_phys_addr,
12963 			    ((char *)area + area->area_hw_addr_offset),
12964 			    area->area_hw_addr_length);
12965 		}
12966 
12967 		((area_t *)arp_add_mp->b_rptr)->area_flags =
12968 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
12969 
12970 		if (arp_just_publish)
12971 			goto arp_setup_multicast;
12972 
12973 		/*
12974 		 * Allocate an ARP deletion message so we know we can tell ARP
12975 		 * when the interface goes down.
12976 		 */
12977 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
12978 		if (arp_del_mp == NULL)
12979 			goto failed;
12980 
12981 	} else {
12982 		if (arp_just_publish)
12983 			goto done;
12984 	}
12985 	/*
12986 	 * Need to bring up ARP or setup multicast mapping only
12987 	 * when the first interface is coming UP.
12988 	 */
12989 	if (ill->ill_ipif_up_count != 0)
12990 		goto done;
12991 
12992 	/*
12993 	 * Allocate an ARP down message (to be saved) and an ARP up
12994 	 * message.
12995 	 */
12996 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
12997 	if (arp_down_mp == NULL)
12998 		goto failed;
12999 
13000 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13001 	if (arp_up_mp == NULL)
13002 		goto failed;
13003 
13004 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13005 		goto done;
13006 
13007 arp_setup_multicast:
13008 	/*
13009 	 * Setup the multicast mappings. This function initializes
13010 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13011 	 * IPv6.
13012 	 */
13013 	if (!ill->ill_isv6) {
13014 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13015 		if (err != 0)
13016 			goto failed;
13017 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13018 		ASSERT(arp_add_mapping_mp != NULL);
13019 	}
13020 
13021 done:;
13022 	if (arp_del_mp != NULL) {
13023 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13024 		ipif->ipif_arp_del_mp = arp_del_mp;
13025 	}
13026 	if (arp_down_mp != NULL) {
13027 		ASSERT(ill->ill_arp_down_mp == NULL);
13028 		ill->ill_arp_down_mp = arp_down_mp;
13029 	}
13030 	if (arp_del_mapping_mp != NULL) {
13031 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13032 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13033 	}
13034 	if (arp_up_mp != NULL) {
13035 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13036 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13037 		putnext(ill->ill_rq, arp_up_mp);
13038 	}
13039 	if (arp_add_mp != NULL) {
13040 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13041 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13042 		putnext(ill->ill_rq, arp_add_mp);
13043 	}
13044 	if (arp_add_mapping_mp != NULL) {
13045 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13046 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13047 		putnext(ill->ill_rq, arp_add_mapping_mp);
13048 	}
13049 	if (arp_just_publish)
13050 		return (0);
13051 
13052 	if (ill->ill_flags & ILLF_NOARP)
13053 		err = ill_arp_off(ill);
13054 	else
13055 		err = ill_arp_on(ill);
13056 	if (err) {
13057 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13058 		freemsg(ipif->ipif_arp_del_mp);
13059 		if (arp_down_mp != NULL)
13060 			freemsg(ill->ill_arp_down_mp);
13061 		if (ill->ill_arp_del_mapping_mp != NULL)
13062 			freemsg(ill->ill_arp_del_mapping_mp);
13063 		ipif->ipif_arp_del_mp = NULL;
13064 		ill->ill_arp_down_mp = NULL;
13065 		ill->ill_arp_del_mapping_mp = NULL;
13066 		return (err);
13067 	}
13068 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
13069 
13070 failed:;
13071 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13072 	freemsg(arp_add_mp);
13073 	freemsg(arp_del_mp);
13074 	freemsg(arp_add_mapping_mp);
13075 	freemsg(arp_up_mp);
13076 	freemsg(arp_down_mp);
13077 	ill->ill_arp_bringup_pending = 0;
13078 	return (err);
13079 }
13080 
13081 /*
13082  * Wakeup all threads waiting to enter the ipsq, and sleeping
13083  * on any of the ills in this ipsq. The ill_lock of the ill
13084  * must be held so that waiters don't miss wakeups
13085  */
13086 static void
13087 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13088 {
13089 	phyint_t *phyint;
13090 
13091 	phyint = ipsq->ipsq_phyint_list;
13092 	while (phyint != NULL) {
13093 		if (phyint->phyint_illv4) {
13094 			if (!caller_holds_lock)
13095 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13096 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13097 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13098 			if (!caller_holds_lock)
13099 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13100 		}
13101 		if (phyint->phyint_illv6) {
13102 			if (!caller_holds_lock)
13103 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13104 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13105 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13106 			if (!caller_holds_lock)
13107 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13108 		}
13109 		phyint = phyint->phyint_ipsq_next;
13110 	}
13111 }
13112 
13113 static ipsq_t *
13114 ipsq_create(char *groupname)
13115 {
13116 	ipsq_t	*ipsq;
13117 
13118 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13119 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13120 	if (ipsq == NULL) {
13121 		return (NULL);
13122 	}
13123 
13124 	if (groupname != NULL)
13125 		(void) strcpy(ipsq->ipsq_name, groupname);
13126 	else
13127 		ipsq->ipsq_name[0] = '\0';
13128 
13129 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13130 	ipsq->ipsq_flags |= IPSQ_GROUP;
13131 	ipsq->ipsq_next = ipsq_g_head;
13132 	ipsq_g_head = ipsq;
13133 	return (ipsq);
13134 }
13135 
13136 /*
13137  * Return an ipsq correspoding to the groupname. If 'create' is true
13138  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13139  * uniquely with an IPMP group. However during IPMP groupname operations,
13140  * multiple IPMP groups may be associated with a single ipsq. But no
13141  * IPMP group can be associated with more than 1 ipsq at any time.
13142  * For example
13143  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13144  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13145  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13146  *
13147  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13148  * status shown below during the execution of the above command.
13149  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13150  *
13151  * After the completion of the above groupname command we return to the stable
13152  * state shown below.
13153  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13154  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13155  *
13156  * Because of the above, we don't search based on the ipsq_name since that
13157  * would miss the correct ipsq during certain windows as shown above.
13158  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13159  * natural state.
13160  */
13161 static ipsq_t *
13162 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13163 {
13164 	ipsq_t	*ipsq;
13165 	int	group_len;
13166 	phyint_t *phyint;
13167 
13168 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13169 
13170 	group_len = strlen(groupname);
13171 	ASSERT(group_len != 0);
13172 	group_len++;
13173 
13174 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13175 		/*
13176 		 * When an ipsq is being split, and ill_split_ipsq
13177 		 * calls this function, we exclude it from being considered.
13178 		 */
13179 		if (ipsq == exclude_ipsq)
13180 			continue;
13181 
13182 		/*
13183 		 * Compare against the ipsq_name. The groupname change happens
13184 		 * in 2 phases. The 1st phase merges the from group into
13185 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13186 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13187 		 * ipsq_name. At this point the phyint_groupname has not been
13188 		 * updated.
13189 		 */
13190 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13191 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13192 			/*
13193 			 * Verify that an ipmp groupname is exactly
13194 			 * part of 1 ipsq and is not found in any other
13195 			 * ipsq.
13196 			 */
13197 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13198 			    NULL);
13199 			return (ipsq);
13200 		}
13201 
13202 		/*
13203 		 * Comparison against ipsq_name alone is not sufficient.
13204 		 * In the case when groups are currently being
13205 		 * merged, the ipsq could hold other IPMP groups temporarily.
13206 		 * so we walk the phyint list and compare against the
13207 		 * phyint_groupname as well.
13208 		 */
13209 		phyint = ipsq->ipsq_phyint_list;
13210 		while (phyint != NULL) {
13211 			if ((group_len == phyint->phyint_groupname_len) &&
13212 			    (bcmp(phyint->phyint_groupname, groupname,
13213 			    group_len) == 0)) {
13214 				/*
13215 				 * Verify that an ipmp groupname is exactly
13216 				 * part of 1 ipsq and is not found in any other
13217 				 * ipsq.
13218 				 */
13219 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13220 					== NULL);
13221 				return (ipsq);
13222 			}
13223 			phyint = phyint->phyint_ipsq_next;
13224 		}
13225 	}
13226 	if (create)
13227 		ipsq = ipsq_create(groupname);
13228 	return (ipsq);
13229 }
13230 
13231 static void
13232 ipsq_delete(ipsq_t *ipsq)
13233 {
13234 	ipsq_t *nipsq;
13235 	ipsq_t *pipsq = NULL;
13236 
13237 	/*
13238 	 * We don't hold the ipsq lock, but we are sure no new
13239 	 * messages can land up, since the ipsq_refs is zero.
13240 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13241 	 * is associated with this ipsq. (Lookups are based on ill_name
13242 	 * or phyint_group_name)
13243 	 */
13244 	ASSERT(ipsq->ipsq_refs == 0);
13245 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13246 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13247 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13248 		/*
13249 		 * This is not the ipsq of an IPMP group.
13250 		 */
13251 		kmem_free(ipsq, sizeof (ipsq_t));
13252 		return;
13253 	}
13254 
13255 	rw_enter(&ill_g_lock, RW_WRITER);
13256 
13257 	/*
13258 	 * Locate the ipsq  before we can remove it from
13259 	 * the singly linked list of ipsq's.
13260 	 */
13261 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13262 		if (nipsq == ipsq) {
13263 			break;
13264 		}
13265 		pipsq = nipsq;
13266 	}
13267 
13268 	ASSERT(nipsq == ipsq);
13269 
13270 	/* unlink ipsq from the list */
13271 	if (pipsq != NULL)
13272 		pipsq->ipsq_next = ipsq->ipsq_next;
13273 	else
13274 		ipsq_g_head = ipsq->ipsq_next;
13275 	kmem_free(ipsq, sizeof (ipsq_t));
13276 	rw_exit(&ill_g_lock);
13277 }
13278 
13279 static void
13280 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13281     queue_t *q)
13282 
13283 {
13284 
13285 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13286 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13287 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13288 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13289 	ASSERT(current_mp != NULL);
13290 
13291 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13292 		NEW_OP, NULL);
13293 
13294 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13295 	    new_ipsq->ipsq_xopq_mphead != NULL);
13296 
13297 	/*
13298 	 * move from old ipsq to the new ipsq.
13299 	 */
13300 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13301 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13302 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13303 
13304 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13305 }
13306 
13307 void
13308 ill_group_cleanup(ill_t *ill)
13309 {
13310 	ill_t *ill_v4;
13311 	ill_t *ill_v6;
13312 	ipif_t *ipif;
13313 
13314 	ill_v4 = ill->ill_phyint->phyint_illv4;
13315 	ill_v6 = ill->ill_phyint->phyint_illv6;
13316 
13317 	if (ill_v4 != NULL) {
13318 		mutex_enter(&ill_v4->ill_lock);
13319 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13320 		    ipif = ipif->ipif_next) {
13321 			IPIF_UNMARK_MOVING(ipif);
13322 		}
13323 		ill_v4->ill_up_ipifs = B_FALSE;
13324 		mutex_exit(&ill_v4->ill_lock);
13325 	}
13326 
13327 	if (ill_v6 != NULL) {
13328 		mutex_enter(&ill_v6->ill_lock);
13329 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13330 		    ipif = ipif->ipif_next) {
13331 			IPIF_UNMARK_MOVING(ipif);
13332 		}
13333 		ill_v6->ill_up_ipifs = B_FALSE;
13334 		mutex_exit(&ill_v6->ill_lock);
13335 	}
13336 }
13337 /*
13338  * This function is called when an ill has had a change in its group status
13339  * to bring up all the ipifs that were up before the change.
13340  */
13341 int
13342 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13343 {
13344 	ipif_t *ipif;
13345 	ill_t *ill_v4;
13346 	ill_t *ill_v6;
13347 	ill_t *from_ill;
13348 	int err = 0;
13349 
13350 
13351 	ASSERT(IAM_WRITER_ILL(ill));
13352 
13353 	/*
13354 	 * Except for ipif_state_flags and ill_state_flags the other
13355 	 * fields of the ipif/ill that are modified below are protected
13356 	 * implicitly since we are a writer. We would have tried to down
13357 	 * even an ipif that was already down, in ill_down_ipifs. So we
13358 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13359 	 */
13360 	ill_v4 = ill->ill_phyint->phyint_illv4;
13361 	ill_v6 = ill->ill_phyint->phyint_illv6;
13362 	if (ill_v4 != NULL) {
13363 		ill_v4->ill_up_ipifs = B_TRUE;
13364 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13365 		    ipif = ipif->ipif_next) {
13366 			mutex_enter(&ill_v4->ill_lock);
13367 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13368 			IPIF_UNMARK_MOVING(ipif);
13369 			mutex_exit(&ill_v4->ill_lock);
13370 			if (ipif->ipif_was_up) {
13371 				if (!(ipif->ipif_flags & IPIF_UP))
13372 					err = ipif_up(ipif, q, mp);
13373 				ipif->ipif_was_up = B_FALSE;
13374 				if (err != 0) {
13375 					/*
13376 					 * Can there be any other error ?
13377 					 */
13378 					ASSERT(err == EINPROGRESS);
13379 					return (err);
13380 				}
13381 			}
13382 		}
13383 		mutex_enter(&ill_v4->ill_lock);
13384 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13385 		mutex_exit(&ill_v4->ill_lock);
13386 		ill_v4->ill_up_ipifs = B_FALSE;
13387 		if (ill_v4->ill_move_in_progress) {
13388 			ASSERT(ill_v4->ill_move_peer != NULL);
13389 			ill_v4->ill_move_in_progress = B_FALSE;
13390 			from_ill = ill_v4->ill_move_peer;
13391 			from_ill->ill_move_in_progress = B_FALSE;
13392 			from_ill->ill_move_peer = NULL;
13393 			mutex_enter(&from_ill->ill_lock);
13394 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13395 			mutex_exit(&from_ill->ill_lock);
13396 			if (ill_v6 == NULL) {
13397 				if (from_ill->ill_phyint->phyint_flags &
13398 				    PHYI_STANDBY) {
13399 					phyint_inactive(from_ill->ill_phyint);
13400 				}
13401 				if (ill_v4->ill_phyint->phyint_flags &
13402 				    PHYI_STANDBY) {
13403 					phyint_inactive(ill_v4->ill_phyint);
13404 				}
13405 			}
13406 			ill_v4->ill_move_peer = NULL;
13407 		}
13408 	}
13409 
13410 	if (ill_v6 != NULL) {
13411 		ill_v6->ill_up_ipifs = B_TRUE;
13412 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13413 		    ipif = ipif->ipif_next) {
13414 			mutex_enter(&ill_v6->ill_lock);
13415 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13416 			IPIF_UNMARK_MOVING(ipif);
13417 			mutex_exit(&ill_v6->ill_lock);
13418 			if (ipif->ipif_was_up) {
13419 				if (!(ipif->ipif_flags & IPIF_UP))
13420 					err = ipif_up(ipif, q, mp);
13421 				ipif->ipif_was_up = B_FALSE;
13422 				if (err != 0) {
13423 					/*
13424 					 * Can there be any other error ?
13425 					 */
13426 					ASSERT(err == EINPROGRESS);
13427 					return (err);
13428 				}
13429 			}
13430 		}
13431 		mutex_enter(&ill_v6->ill_lock);
13432 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13433 		mutex_exit(&ill_v6->ill_lock);
13434 		ill_v6->ill_up_ipifs = B_FALSE;
13435 		if (ill_v6->ill_move_in_progress) {
13436 			ASSERT(ill_v6->ill_move_peer != NULL);
13437 			ill_v6->ill_move_in_progress = B_FALSE;
13438 			from_ill = ill_v6->ill_move_peer;
13439 			from_ill->ill_move_in_progress = B_FALSE;
13440 			from_ill->ill_move_peer = NULL;
13441 			mutex_enter(&from_ill->ill_lock);
13442 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13443 			mutex_exit(&from_ill->ill_lock);
13444 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13445 				phyint_inactive(from_ill->ill_phyint);
13446 			}
13447 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13448 				phyint_inactive(ill_v6->ill_phyint);
13449 			}
13450 			ill_v6->ill_move_peer = NULL;
13451 		}
13452 	}
13453 	return (0);
13454 }
13455 
13456 /*
13457  * bring down all the approriate ipifs.
13458  */
13459 /* ARGSUSED */
13460 static void
13461 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13462 {
13463 	ipif_t *ipif;
13464 
13465 	ASSERT(IAM_WRITER_ILL(ill));
13466 
13467 	/*
13468 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13469 	 * are modified below are protected implicitly since we are a writer
13470 	 */
13471 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13472 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13473 			continue;
13474 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13475 			/*
13476 			 * We go through the ipif_down logic even if the ipif
13477 			 * is already down, since routes can be added based
13478 			 * on down ipifs. Going through ipif_down once again
13479 			 * will delete any IREs created based on these routes.
13480 			 */
13481 			if (ipif->ipif_flags & IPIF_UP)
13482 				ipif->ipif_was_up = B_TRUE;
13483 			/*
13484 			 * If called with chk_nofailover true ipif is moving.
13485 			 */
13486 			mutex_enter(&ill->ill_lock);
13487 			if (chk_nofailover) {
13488 				ipif->ipif_state_flags |=
13489 					IPIF_MOVING | IPIF_CHANGING;
13490 			} else {
13491 				ipif->ipif_state_flags |= IPIF_CHANGING;
13492 			}
13493 			mutex_exit(&ill->ill_lock);
13494 			/*
13495 			 * Need to re-create net/subnet bcast ires if
13496 			 * they are dependent on ipif.
13497 			 */
13498 			if (!ipif->ipif_isv6)
13499 				ipif_check_bcast_ires(ipif);
13500 			(void) ipif_logical_down(ipif, NULL, NULL);
13501 			ipif_down_tail(ipif);
13502 			/*
13503 			 * We don't do ipif_multicast_down for IPv4 in
13504 			 * ipif_down. We need to set this so that
13505 			 * ipif_multicast_up will join the
13506 			 * ALLHOSTS_GROUP on to_ill.
13507 			 */
13508 			ipif->ipif_multicast_up = B_FALSE;
13509 		}
13510 	}
13511 }
13512 
13513 #define	IPSQ_INC_REF(ipsq)	{			\
13514 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13515 	(ipsq)->ipsq_refs++;				\
13516 }
13517 
13518 #define	IPSQ_DEC_REF(ipsq)	{			\
13519 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13520 	(ipsq)->ipsq_refs--;				\
13521 	if ((ipsq)->ipsq_refs == 0)				\
13522 		(ipsq)->ipsq_name[0] = '\0'; 		\
13523 }
13524 
13525 /*
13526  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13527  * new_ipsq.
13528  */
13529 static void
13530 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13531 {
13532 	phyint_t *phyint;
13533 	phyint_t *next_phyint;
13534 
13535 	/*
13536 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13537 	 * writer and the ill_lock of the ill in question. Also the dest
13538 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13539 	 */
13540 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13541 
13542 	phyint = cur_ipsq->ipsq_phyint_list;
13543 	cur_ipsq->ipsq_phyint_list = NULL;
13544 	while (phyint != NULL) {
13545 		next_phyint = phyint->phyint_ipsq_next;
13546 		IPSQ_DEC_REF(cur_ipsq);
13547 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13548 		new_ipsq->ipsq_phyint_list = phyint;
13549 		IPSQ_INC_REF(new_ipsq);
13550 		phyint->phyint_ipsq = new_ipsq;
13551 		phyint = next_phyint;
13552 	}
13553 }
13554 
13555 #define	SPLIT_SUCCESS		0
13556 #define	SPLIT_NOT_NEEDED	1
13557 #define	SPLIT_FAILED		2
13558 
13559 int
13560 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13561 {
13562 	ipsq_t *newipsq = NULL;
13563 
13564 	/*
13565 	 * Assertions denote pre-requisites for changing the ipsq of
13566 	 * a phyint
13567 	 */
13568 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13569 	/*
13570 	 * <ill-phyint> assocs can't change while ill_g_lock
13571 	 * is held as writer. See ill_phyint_reinit()
13572 	 */
13573 	ASSERT(phyint->phyint_illv4 == NULL ||
13574 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13575 	ASSERT(phyint->phyint_illv6 == NULL ||
13576 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13577 
13578 	if ((phyint->phyint_groupname_len !=
13579 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13580 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13581 	    phyint->phyint_groupname_len) != 0)) {
13582 		/*
13583 		 * Once we fail in creating a new ipsq due to memory shortage,
13584 		 * don't attempt to create new ipsq again, based on another
13585 		 * phyint, since we want all phyints belonging to an IPMP group
13586 		 * to be in the same ipsq even in the event of mem alloc fails.
13587 		 */
13588 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13589 		    cur_ipsq);
13590 		if (newipsq == NULL) {
13591 			/* Memory allocation failure */
13592 			return (SPLIT_FAILED);
13593 		} else {
13594 			/* ipsq_refs protected by ill_g_lock (writer) */
13595 			IPSQ_DEC_REF(cur_ipsq);
13596 			phyint->phyint_ipsq = newipsq;
13597 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13598 			newipsq->ipsq_phyint_list = phyint;
13599 			IPSQ_INC_REF(newipsq);
13600 			return (SPLIT_SUCCESS);
13601 		}
13602 	}
13603 	return (SPLIT_NOT_NEEDED);
13604 }
13605 
13606 /*
13607  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13608  * to do this split
13609  */
13610 static int
13611 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13612 {
13613 	ipsq_t *newipsq;
13614 
13615 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13616 	/*
13617 	 * <ill-phyint> assocs can't change while ill_g_lock
13618 	 * is held as writer. See ill_phyint_reinit()
13619 	 */
13620 
13621 	ASSERT(phyint->phyint_illv4 == NULL ||
13622 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13623 	ASSERT(phyint->phyint_illv6 == NULL ||
13624 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13625 
13626 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13627 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13628 		/*
13629 		 * ipsq_init failed due to no memory
13630 		 * caller will use the same ipsq
13631 		 */
13632 		return (SPLIT_FAILED);
13633 	}
13634 
13635 	/* ipsq_ref is protected by ill_g_lock (writer) */
13636 	IPSQ_DEC_REF(cur_ipsq);
13637 
13638 	/*
13639 	 * This is a new ipsq that is unknown to the world.
13640 	 * So we don't need to hold ipsq_lock,
13641 	 */
13642 	newipsq = phyint->phyint_ipsq;
13643 	newipsq->ipsq_writer = NULL;
13644 	newipsq->ipsq_reentry_cnt--;
13645 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13646 #ifdef ILL_DEBUG
13647 	newipsq->ipsq_depth = 0;
13648 #endif
13649 
13650 	return (SPLIT_SUCCESS);
13651 }
13652 
13653 /*
13654  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13655  * ipsq's representing their individual groups or themselves. Return
13656  * whether split needs to be retried again later.
13657  */
13658 static boolean_t
13659 ill_split_ipsq(ipsq_t *cur_ipsq)
13660 {
13661 	phyint_t *phyint;
13662 	phyint_t *next_phyint;
13663 	int	error;
13664 	boolean_t need_retry = B_FALSE;
13665 
13666 	phyint = cur_ipsq->ipsq_phyint_list;
13667 	cur_ipsq->ipsq_phyint_list = NULL;
13668 	while (phyint != NULL) {
13669 		next_phyint = phyint->phyint_ipsq_next;
13670 		/*
13671 		 * 'created' will tell us whether the callee actually
13672 		 * created an ipsq. Lack of memory may force the callee
13673 		 * to return without creating an ipsq.
13674 		 */
13675 		if (phyint->phyint_groupname == NULL) {
13676 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13677 		} else {
13678 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13679 					need_retry);
13680 		}
13681 
13682 		switch (error) {
13683 		case SPLIT_FAILED:
13684 			need_retry = B_TRUE;
13685 			/* FALLTHRU */
13686 		case SPLIT_NOT_NEEDED:
13687 			/*
13688 			 * Keep it on the list.
13689 			 */
13690 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13691 			cur_ipsq->ipsq_phyint_list = phyint;
13692 			break;
13693 		case SPLIT_SUCCESS:
13694 			break;
13695 		default:
13696 			ASSERT(0);
13697 		}
13698 
13699 		phyint = next_phyint;
13700 	}
13701 	return (need_retry);
13702 }
13703 
13704 /*
13705  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13706  * and return the ills in the list. This list will be
13707  * needed to unlock all the ills later on by the caller.
13708  * The <ill-ipsq> associations could change between the
13709  * lock and unlock. Hence the unlock can't traverse the
13710  * ipsq to get the list of ills.
13711  */
13712 static int
13713 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13714 {
13715 	int	cnt = 0;
13716 	phyint_t	*phyint;
13717 
13718 	/*
13719 	 * The caller holds ill_g_lock to ensure that the ill memberships
13720 	 * of the ipsq don't change
13721 	 */
13722 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13723 
13724 	phyint = ipsq->ipsq_phyint_list;
13725 	while (phyint != NULL) {
13726 		if (phyint->phyint_illv4 != NULL) {
13727 			ASSERT(cnt < list_max);
13728 			list[cnt++] = phyint->phyint_illv4;
13729 		}
13730 		if (phyint->phyint_illv6 != NULL) {
13731 			ASSERT(cnt < list_max);
13732 			list[cnt++] = phyint->phyint_illv6;
13733 		}
13734 		phyint = phyint->phyint_ipsq_next;
13735 	}
13736 	ill_lock_ills(list, cnt);
13737 	return (cnt);
13738 }
13739 
13740 void
13741 ill_lock_ills(ill_t **list, int cnt)
13742 {
13743 	int	i;
13744 
13745 	if (cnt > 1) {
13746 		boolean_t try_again;
13747 		do {
13748 			try_again = B_FALSE;
13749 			for (i = 0; i < cnt - 1; i++) {
13750 				if (list[i] < list[i + 1]) {
13751 					ill_t	*tmp;
13752 
13753 					/* swap the elements */
13754 					tmp = list[i];
13755 					list[i] = list[i + 1];
13756 					list[i + 1] = tmp;
13757 					try_again = B_TRUE;
13758 				}
13759 			}
13760 		} while (try_again);
13761 	}
13762 
13763 	for (i = 0; i < cnt; i++) {
13764 		if (i == 0) {
13765 			if (list[i] != NULL)
13766 				mutex_enter(&list[i]->ill_lock);
13767 			else
13768 				return;
13769 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13770 			mutex_enter(&list[i]->ill_lock);
13771 		}
13772 	}
13773 }
13774 
13775 void
13776 ill_unlock_ills(ill_t **list, int cnt)
13777 {
13778 	int	i;
13779 
13780 	for (i = 0; i < cnt; i++) {
13781 		if ((i == 0) && (list[i] != NULL)) {
13782 			mutex_exit(&list[i]->ill_lock);
13783 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13784 			mutex_exit(&list[i]->ill_lock);
13785 		}
13786 	}
13787 }
13788 
13789 /*
13790  * Merge all the ills from 1 ipsq group into another ipsq group.
13791  * The source ipsq group is specified by the ipsq associated with
13792  * 'from_ill'. The destination ipsq group is specified by the ipsq
13793  * associated with 'to_ill' or 'groupname' respectively.
13794  * Note that ipsq itself does not have a reference count mechanism
13795  * and functions don't look up an ipsq and pass it around. Instead
13796  * functions pass around an ill or groupname, and the ipsq is looked
13797  * up from the ill or groupname and the required operation performed
13798  * atomically with the lookup on the ipsq.
13799  */
13800 static int
13801 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13802     queue_t *q)
13803 {
13804 	ipsq_t *old_ipsq;
13805 	ipsq_t *new_ipsq;
13806 	ill_t	**ill_list;
13807 	int	cnt;
13808 	size_t	ill_list_size;
13809 	boolean_t became_writer_on_new_sq = B_FALSE;
13810 
13811 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
13812 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
13813 
13814 	/*
13815 	 * Need to hold ill_g_lock as writer and also the ill_lock to
13816 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
13817 	 * ipsq_lock to prevent new messages from landing on an ipsq.
13818 	 */
13819 	rw_enter(&ill_g_lock, RW_WRITER);
13820 
13821 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
13822 	if (groupname != NULL)
13823 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
13824 	else {
13825 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
13826 	}
13827 
13828 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
13829 
13830 	/*
13831 	 * both groups are on the same ipsq.
13832 	 */
13833 	if (old_ipsq == new_ipsq) {
13834 		rw_exit(&ill_g_lock);
13835 		return (0);
13836 	}
13837 
13838 	cnt = old_ipsq->ipsq_refs << 1;
13839 	ill_list_size = cnt * sizeof (ill_t *);
13840 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
13841 	if (ill_list == NULL) {
13842 		rw_exit(&ill_g_lock);
13843 		return (ENOMEM);
13844 	}
13845 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
13846 
13847 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
13848 	mutex_enter(&new_ipsq->ipsq_lock);
13849 	if ((new_ipsq->ipsq_writer == NULL &&
13850 		new_ipsq->ipsq_current_ipif == NULL) ||
13851 	    (new_ipsq->ipsq_writer == curthread)) {
13852 		new_ipsq->ipsq_writer = curthread;
13853 		new_ipsq->ipsq_reentry_cnt++;
13854 		became_writer_on_new_sq = B_TRUE;
13855 	}
13856 
13857 	/*
13858 	 * We are holding ill_g_lock as writer and all the ill locks of
13859 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
13860 	 * message can land up on the old ipsq even though we don't hold the
13861 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
13862 	 */
13863 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
13864 
13865 	/*
13866 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
13867 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
13868 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
13869 	 */
13870 	ill_merge_ipsq(old_ipsq, new_ipsq);
13871 
13872 	/*
13873 	 * Mark the new ipsq as needing a split since it is currently
13874 	 * being shared by more than 1 IPMP group. The split will
13875 	 * occur at the end of ipsq_exit
13876 	 */
13877 	new_ipsq->ipsq_split = B_TRUE;
13878 
13879 	/* Now release all the locks */
13880 	mutex_exit(&new_ipsq->ipsq_lock);
13881 	ill_unlock_ills(ill_list, cnt);
13882 	rw_exit(&ill_g_lock);
13883 
13884 	kmem_free(ill_list, ill_list_size);
13885 
13886 	/*
13887 	 * If we succeeded in becoming writer on the new ipsq, then
13888 	 * drain the new ipsq and start processing  all enqueued messages
13889 	 * including the current ioctl we are processing which is either
13890 	 * a set groupname or failover/failback.
13891 	 */
13892 	if (became_writer_on_new_sq)
13893 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
13894 
13895 	/*
13896 	 * syncq has been changed and all the messages have been moved.
13897 	 */
13898 	mutex_enter(&old_ipsq->ipsq_lock);
13899 	old_ipsq->ipsq_current_ipif = NULL;
13900 	mutex_exit(&old_ipsq->ipsq_lock);
13901 	return (EINPROGRESS);
13902 }
13903 
13904 /*
13905  * Delete and add the loopback copy and non-loopback copy of
13906  * the BROADCAST ire corresponding to ill and addr. Used to
13907  * group broadcast ires together when ill becomes part of
13908  * a group.
13909  *
13910  * This function is also called when ill is leaving the group
13911  * so that the ires belonging to the group gets re-grouped.
13912  */
13913 static void
13914 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
13915 {
13916 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
13917 	ire_t **ire_ptpn = &ire_head;
13918 
13919 	/*
13920 	 * The loopback and non-loopback IREs are inserted in the order in which
13921 	 * they're found, on the basis that they are correctly ordered (loopback
13922 	 * first).
13923 	 */
13924 	for (;;) {
13925 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
13926 		    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
13927 		if (ire == NULL)
13928 			break;
13929 
13930 		/*
13931 		 * we are passing in KM_SLEEP because it is not easy to
13932 		 * go back to a sane state in case of memory failure.
13933 		 */
13934 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
13935 		ASSERT(nire != NULL);
13936 		bzero(nire, sizeof (ire_t));
13937 		/*
13938 		 * Don't use ire_max_frag directly since we don't
13939 		 * hold on to 'ire' until we add the new ire 'nire' and
13940 		 * we don't want the new ire to have a dangling reference
13941 		 * to 'ire'. The ire_max_frag of a broadcast ire must
13942 		 * be in sync with the ipif_mtu of the associate ipif.
13943 		 * For eg. this happens as a result of SIOCSLIFNAME,
13944 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
13945 		 * the driver. A change in ire_max_frag triggered as
13946 		 * as a result of path mtu discovery, or due to an
13947 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
13948 		 * route change -mtu command does not apply to broadcast ires.
13949 		 *
13950 		 * XXX We need a recovery strategy here if ire_init fails
13951 		 */
13952 		if (ire_init(nire,
13953 		    (uchar_t *)&ire->ire_addr,
13954 		    (uchar_t *)&ire->ire_mask,
13955 		    (uchar_t *)&ire->ire_src_addr,
13956 		    (uchar_t *)&ire->ire_gateway_addr,
13957 		    (uchar_t *)&ire->ire_in_src_addr,
13958 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
13959 			&ire->ire_ipif->ipif_mtu,
13960 		    ire->ire_fp_mp,
13961 		    ire->ire_rfq,
13962 		    ire->ire_stq,
13963 		    ire->ire_type,
13964 		    ire->ire_dlureq_mp,
13965 		    ire->ire_ipif,
13966 		    ire->ire_in_ill,
13967 		    ire->ire_cmask,
13968 		    ire->ire_phandle,
13969 		    ire->ire_ihandle,
13970 		    ire->ire_flags,
13971 		    &ire->ire_uinfo) == NULL) {
13972 			cmn_err(CE_PANIC, "ire_init() failed");
13973 		}
13974 		ire_delete(ire);
13975 		ire_refrele(ire);
13976 
13977 		/*
13978 		 * The newly created IREs are inserted at the tail of the list
13979 		 * starting with ire_head. As we've just allocated them no one
13980 		 * knows about them so it's safe.
13981 		 */
13982 		*ire_ptpn = nire;
13983 		ire_ptpn = &nire->ire_next;
13984 	}
13985 
13986 	for (nire = ire_head; nire != NULL; nire = nire_next) {
13987 		int error;
13988 		ire_t *oire;
13989 		/* unlink the IRE from our list before calling ire_add() */
13990 		nire_next = nire->ire_next;
13991 		nire->ire_next = NULL;
13992 
13993 		/* ire_add adds the ire at the right place in the list */
13994 		oire = nire;
13995 		error = ire_add(&nire, NULL, NULL, NULL);
13996 		ASSERT(error == 0);
13997 		ASSERT(oire == nire);
13998 		ire_refrele(nire);	/* Held in ire_add */
13999 	}
14000 }
14001 
14002 /*
14003  * This function is usually called when an ill is inserted in
14004  * a group and all the ipifs are already UP. As all the ipifs
14005  * are already UP, the broadcast ires have already been created
14006  * and been inserted. But, ire_add_v4 would not have grouped properly.
14007  * We need to re-group for the benefit of ip_wput_ire which
14008  * expects BROADCAST ires to be grouped properly to avoid sending
14009  * more than one copy of the broadcast packet per group.
14010  *
14011  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14012  *	  because when ipif_up_done ends up calling this, ires have
14013  *        already been added before illgrp_insert i.e before ill_group
14014  *	  has been initialized.
14015  */
14016 static void
14017 ill_group_bcast_for_xmit(ill_t *ill)
14018 {
14019 	ill_group_t *illgrp;
14020 	ipif_t *ipif;
14021 	ipaddr_t addr;
14022 	ipaddr_t net_mask;
14023 	ipaddr_t subnet_netmask;
14024 
14025 	illgrp = ill->ill_group;
14026 
14027 	/*
14028 	 * This function is called even when an ill is deleted from
14029 	 * the group. Hence, illgrp could be null.
14030 	 */
14031 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14032 		return;
14033 
14034 	/*
14035 	 * Delete all the BROADCAST ires matching this ill and add
14036 	 * them back. This time, ire_add_v4 should take care of
14037 	 * grouping them with others because ill is part of the
14038 	 * group.
14039 	 */
14040 	ill_bcast_delete_and_add(ill, 0);
14041 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14042 
14043 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14044 
14045 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14046 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14047 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14048 		} else {
14049 			net_mask = htonl(IN_CLASSA_NET);
14050 		}
14051 		addr = net_mask & ipif->ipif_subnet;
14052 		ill_bcast_delete_and_add(ill, addr);
14053 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14054 
14055 		subnet_netmask = ipif->ipif_net_mask;
14056 		addr = ipif->ipif_subnet;
14057 		ill_bcast_delete_and_add(ill, addr);
14058 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14059 	}
14060 }
14061 
14062 /*
14063  * This function is called from illgrp_delete when ill is being deleted
14064  * from the group.
14065  *
14066  * As ill is not there in the group anymore, any address belonging
14067  * to this ill should be cleared of IRE_MARK_NORECV.
14068  */
14069 static void
14070 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14071 {
14072 	ire_t *ire;
14073 	irb_t *irb;
14074 
14075 	ASSERT(ill->ill_group == NULL);
14076 
14077 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14078 	    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14079 
14080 	if (ire != NULL) {
14081 		/*
14082 		 * IPMP and plumbing operations are serialized on the ipsq, so
14083 		 * no one will insert or delete a broadcast ire under our feet.
14084 		 */
14085 		irb = ire->ire_bucket;
14086 		rw_enter(&irb->irb_lock, RW_READER);
14087 		ire_refrele(ire);
14088 
14089 		for (; ire != NULL; ire = ire->ire_next) {
14090 			if (ire->ire_addr != addr)
14091 				break;
14092 			if (ire_to_ill(ire) != ill)
14093 				continue;
14094 
14095 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14096 			ire->ire_marks &= ~IRE_MARK_NORECV;
14097 		}
14098 		rw_exit(&irb->irb_lock);
14099 	}
14100 }
14101 
14102 /*
14103  * This function must be called only after the broadcast ires
14104  * have been grouped together. For a given address addr, nominate
14105  * only one of the ires whose interface is not FAILED or OFFLINE.
14106  *
14107  * This is also called when an ipif goes down, so that we can nominate
14108  * a different ire with the same address for receiving.
14109  */
14110 static void
14111 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14112 {
14113 	irb_t *irb;
14114 	ire_t *ire;
14115 	ire_t *ire1;
14116 	ire_t *save_ire;
14117 	ire_t **irep = NULL;
14118 	boolean_t first = B_TRUE;
14119 	ire_t *clear_ire = NULL;
14120 	ire_t *start_ire = NULL;
14121 	ire_t	*new_lb_ire;
14122 	ire_t	*new_nlb_ire;
14123 	boolean_t new_lb_ire_used = B_FALSE;
14124 	boolean_t new_nlb_ire_used = B_FALSE;
14125 	uint64_t match_flags;
14126 	uint64_t phyi_flags;
14127 	boolean_t fallback = B_FALSE;
14128 
14129 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14130 	    MATCH_IRE_TYPE);
14131 	/*
14132 	 * We may not be able to find some ires if a previous
14133 	 * ire_create failed. This happens when an ipif goes
14134 	 * down and we are unable to create BROADCAST ires due
14135 	 * to memory failure. Thus, we have to check for NULL
14136 	 * below. This should handle the case for LOOPBACK,
14137 	 * POINTOPOINT and interfaces with some POINTOPOINT
14138 	 * logicals for which there are no BROADCAST ires.
14139 	 */
14140 	if (ire == NULL)
14141 		return;
14142 	/*
14143 	 * Currently IRE_BROADCASTS are deleted when an ipif
14144 	 * goes down which runs exclusively. Thus, setting
14145 	 * IRE_MARK_RCVD should not race with ire_delete marking
14146 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14147 	 * be consistent with other parts of the code that walks
14148 	 * a given bucket.
14149 	 */
14150 	save_ire = ire;
14151 	irb = ire->ire_bucket;
14152 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14153 	if (new_lb_ire == NULL) {
14154 		ire_refrele(ire);
14155 		return;
14156 	}
14157 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14158 	if (new_nlb_ire == NULL) {
14159 		ire_refrele(ire);
14160 		kmem_cache_free(ire_cache, new_lb_ire);
14161 		return;
14162 	}
14163 	IRB_REFHOLD(irb);
14164 	rw_enter(&irb->irb_lock, RW_WRITER);
14165 	/*
14166 	 * Get to the first ire matching the address and the
14167 	 * group. If the address does not match we are done
14168 	 * as we could not find the IRE. If the address matches
14169 	 * we should get to the first one matching the group.
14170 	 */
14171 	while (ire != NULL) {
14172 		if (ire->ire_addr != addr ||
14173 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14174 			break;
14175 		}
14176 		ire = ire->ire_next;
14177 	}
14178 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14179 	start_ire = ire;
14180 redo:
14181 	while (ire != NULL && ire->ire_addr == addr &&
14182 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14183 		/*
14184 		 * The first ire for any address within a group
14185 		 * should always be the one with IRE_MARK_NORECV cleared
14186 		 * so that ip_wput_ire can avoid searching for one.
14187 		 * Note down the insertion point which will be used
14188 		 * later.
14189 		 */
14190 		if (first && (irep == NULL))
14191 			irep = ire->ire_ptpn;
14192 		/*
14193 		 * PHYI_FAILED is set when the interface fails.
14194 		 * This interface might have become good, but the
14195 		 * daemon has not yet detected. We should still
14196 		 * not receive on this. PHYI_OFFLINE should never
14197 		 * be picked as this has been offlined and soon
14198 		 * be removed.
14199 		 */
14200 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14201 		if (phyi_flags & PHYI_OFFLINE) {
14202 			ire->ire_marks |= IRE_MARK_NORECV;
14203 			ire = ire->ire_next;
14204 			continue;
14205 		}
14206 		if (phyi_flags & match_flags) {
14207 			ire->ire_marks |= IRE_MARK_NORECV;
14208 			ire = ire->ire_next;
14209 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14210 			    PHYI_INACTIVE) {
14211 				fallback = B_TRUE;
14212 			}
14213 			continue;
14214 		}
14215 		if (first) {
14216 			/*
14217 			 * We will move this to the front of the list later
14218 			 * on.
14219 			 */
14220 			clear_ire = ire;
14221 			ire->ire_marks &= ~IRE_MARK_NORECV;
14222 		} else {
14223 			ire->ire_marks |= IRE_MARK_NORECV;
14224 		}
14225 		first = B_FALSE;
14226 		ire = ire->ire_next;
14227 	}
14228 	/*
14229 	 * If we never nominated anybody, try nominating at least
14230 	 * an INACTIVE, if we found one. Do it only once though.
14231 	 */
14232 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14233 	    fallback) {
14234 		match_flags = PHYI_FAILED;
14235 		ire = start_ire;
14236 		irep = NULL;
14237 		goto redo;
14238 	}
14239 	ire_refrele(save_ire);
14240 
14241 	/*
14242 	 * irep non-NULL indicates that we entered the while loop
14243 	 * above. If clear_ire is at the insertion point, we don't
14244 	 * have to do anything. clear_ire will be NULL if all the
14245 	 * interfaces are failed.
14246 	 *
14247 	 * We cannot unlink and reinsert the ire at the right place
14248 	 * in the list since there can be other walkers of this bucket.
14249 	 * Instead we delete and recreate the ire
14250 	 */
14251 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14252 		ire_t *clear_ire_stq = NULL;
14253 		bzero(new_lb_ire, sizeof (ire_t));
14254 		/* XXX We need a recovery strategy here. */
14255 		if (ire_init(new_lb_ire,
14256 		    (uchar_t *)&clear_ire->ire_addr,
14257 		    (uchar_t *)&clear_ire->ire_mask,
14258 		    (uchar_t *)&clear_ire->ire_src_addr,
14259 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14260 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14261 		    &clear_ire->ire_max_frag,
14262 		    clear_ire->ire_fp_mp,
14263 		    clear_ire->ire_rfq,
14264 		    clear_ire->ire_stq,
14265 		    clear_ire->ire_type,
14266 		    clear_ire->ire_dlureq_mp,
14267 		    clear_ire->ire_ipif,
14268 		    clear_ire->ire_in_ill,
14269 		    clear_ire->ire_cmask,
14270 		    clear_ire->ire_phandle,
14271 		    clear_ire->ire_ihandle,
14272 		    clear_ire->ire_flags,
14273 		    &clear_ire->ire_uinfo) == NULL)
14274 			cmn_err(CE_PANIC, "ire_init() failed");
14275 		if (clear_ire->ire_stq == NULL) {
14276 			ire_t *ire_next = clear_ire->ire_next;
14277 			if (ire_next != NULL &&
14278 			    ire_next->ire_stq != NULL &&
14279 			    ire_next->ire_addr == clear_ire->ire_addr &&
14280 			    ire_next->ire_ipif->ipif_ill ==
14281 			    clear_ire->ire_ipif->ipif_ill) {
14282 				clear_ire_stq = ire_next;
14283 
14284 				bzero(new_nlb_ire, sizeof (ire_t));
14285 				/* XXX We need a recovery strategy here. */
14286 				if (ire_init(new_nlb_ire,
14287 				    (uchar_t *)&clear_ire_stq->ire_addr,
14288 				    (uchar_t *)&clear_ire_stq->ire_mask,
14289 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14290 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14291 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14292 				    &clear_ire_stq->ire_max_frag,
14293 				    clear_ire_stq->ire_fp_mp,
14294 				    clear_ire_stq->ire_rfq,
14295 				    clear_ire_stq->ire_stq,
14296 				    clear_ire_stq->ire_type,
14297 				    clear_ire_stq->ire_dlureq_mp,
14298 				    clear_ire_stq->ire_ipif,
14299 				    clear_ire_stq->ire_in_ill,
14300 				    clear_ire_stq->ire_cmask,
14301 				    clear_ire_stq->ire_phandle,
14302 				    clear_ire_stq->ire_ihandle,
14303 				    clear_ire_stq->ire_flags,
14304 				    &clear_ire_stq->ire_uinfo) == NULL)
14305 					cmn_err(CE_PANIC, "ire_init() failed");
14306 			}
14307 		}
14308 
14309 		/*
14310 		 * Delete the ire. We can't call ire_delete() since
14311 		 * we are holding the bucket lock. We can't release the
14312 		 * bucket lock since we can't allow irep to change. So just
14313 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14314 		 * ire from the list and do the refrele.
14315 		 */
14316 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14317 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14318 
14319 		if (clear_ire_stq != NULL) {
14320 			ire_fastpath_list_delete(
14321 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14322 			    clear_ire_stq);
14323 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14324 		}
14325 
14326 		/*
14327 		 * Also take care of otherfields like ib/ob pkt count
14328 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14329 		 */
14330 
14331 		/* Add the new ire's. Insert at *irep */
14332 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14333 		ire1 = *irep;
14334 		if (ire1 != NULL)
14335 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14336 		new_lb_ire->ire_next = ire1;
14337 		/* Link the new one in. */
14338 		new_lb_ire->ire_ptpn = irep;
14339 		membar_producer();
14340 		*irep = new_lb_ire;
14341 		new_lb_ire_used = B_TRUE;
14342 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14343 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14344 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14345 
14346 		if (clear_ire_stq != NULL) {
14347 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14348 			irep = &new_lb_ire->ire_next;
14349 			/* Add the new ire. Insert at *irep */
14350 			ire1 = *irep;
14351 			if (ire1 != NULL)
14352 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14353 			new_nlb_ire->ire_next = ire1;
14354 			/* Link the new one in. */
14355 			new_nlb_ire->ire_ptpn = irep;
14356 			membar_producer();
14357 			*irep = new_nlb_ire;
14358 			new_nlb_ire_used = B_TRUE;
14359 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14360 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14361 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14362 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14363 		}
14364 	}
14365 	rw_exit(&irb->irb_lock);
14366 	if (!new_lb_ire_used)
14367 		kmem_cache_free(ire_cache, new_lb_ire);
14368 	if (!new_nlb_ire_used)
14369 		kmem_cache_free(ire_cache, new_nlb_ire);
14370 	IRB_REFRELE(irb);
14371 }
14372 
14373 /*
14374  * Whenever an ipif goes down we have to renominate a different
14375  * broadcast ire to receive. Whenever an ipif comes up, we need
14376  * to make sure that we have only one nominated to receive.
14377  */
14378 static void
14379 ipif_renominate_bcast(ipif_t *ipif)
14380 {
14381 	ill_t *ill = ipif->ipif_ill;
14382 	ipaddr_t subnet_addr;
14383 	ipaddr_t net_addr;
14384 	ipaddr_t net_mask = 0;
14385 	ipaddr_t subnet_netmask;
14386 	ipaddr_t addr;
14387 	ill_group_t *illgrp;
14388 
14389 	illgrp = ill->ill_group;
14390 	/*
14391 	 * If this is the last ipif going down, it might take
14392 	 * the ill out of the group. In that case ipif_down ->
14393 	 * illgrp_delete takes care of doing the nomination.
14394 	 * ipif_down does not call for this case.
14395 	 */
14396 	ASSERT(illgrp != NULL);
14397 
14398 	/* There could not have been any ires associated with this */
14399 	if (ipif->ipif_subnet == 0)
14400 		return;
14401 
14402 	ill_mark_bcast(illgrp, 0);
14403 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14404 
14405 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14406 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14407 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14408 	} else {
14409 		net_mask = htonl(IN_CLASSA_NET);
14410 	}
14411 	addr = net_mask & ipif->ipif_subnet;
14412 	ill_mark_bcast(illgrp, addr);
14413 
14414 	net_addr = ~net_mask | addr;
14415 	ill_mark_bcast(illgrp, net_addr);
14416 
14417 	subnet_netmask = ipif->ipif_net_mask;
14418 	addr = ipif->ipif_subnet;
14419 	ill_mark_bcast(illgrp, addr);
14420 
14421 	subnet_addr = ~subnet_netmask | addr;
14422 	ill_mark_bcast(illgrp, subnet_addr);
14423 }
14424 
14425 /*
14426  * Whenever we form or delete ill groups, we need to nominate one set of
14427  * BROADCAST ires for receiving in the group.
14428  *
14429  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14430  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14431  *    for ill_ipif_up_count to be non-zero. This is the only case where
14432  *    ill_ipif_up_count is zero and we would still find the ires.
14433  *
14434  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14435  *    ipif is UP and we just have to do the nomination.
14436  *
14437  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14438  *    from the group. So, we have to do the nomination.
14439  *
14440  * Because of (3), there could be just one ill in the group. But we have
14441  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14442  * Thus, this function does not optimize when there is only one ill as
14443  * it is not correct for (3).
14444  */
14445 static void
14446 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14447 {
14448 	ill_t *ill;
14449 	ipif_t *ipif;
14450 	ipaddr_t subnet_addr;
14451 	ipaddr_t prev_subnet_addr = 0;
14452 	ipaddr_t net_addr;
14453 	ipaddr_t prev_net_addr = 0;
14454 	ipaddr_t net_mask = 0;
14455 	ipaddr_t subnet_netmask;
14456 	ipaddr_t addr;
14457 
14458 	/*
14459 	 * When the last memeber is leaving, there is nothing to
14460 	 * nominate.
14461 	 */
14462 	if (illgrp->illgrp_ill_count == 0) {
14463 		ASSERT(illgrp->illgrp_ill == NULL);
14464 		return;
14465 	}
14466 
14467 	ill = illgrp->illgrp_ill;
14468 	ASSERT(!ill->ill_isv6);
14469 	/*
14470 	 * We assume that ires with same address and belonging to the
14471 	 * same group, has been grouped together. Nominating a *single*
14472 	 * ill in the group for sending and receiving broadcast is done
14473 	 * by making sure that the first BROADCAST ire (which will be
14474 	 * the one returned by ire_ctable_lookup for ip_rput and the
14475 	 * one that will be used in ip_wput_ire) will be the one that
14476 	 * will not have IRE_MARK_NORECV set.
14477 	 *
14478 	 * 1) ip_rput checks and discards packets received on ires marked
14479 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14480 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14481 	 *    first ire in the group for every broadcast address in the group.
14482 	 *    ip_rput will accept packets only on the first ire i.e only
14483 	 *    one copy of the ill.
14484 	 *
14485 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14486 	 *    packet for the whole group. It needs to send out on the ill
14487 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14488 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14489 	 *    the copy echoed back on other port where the ire is not marked
14490 	 *    with IRE_MARK_NORECV.
14491 	 *
14492 	 * Note that we just need to have the first IRE either loopback or
14493 	 * non-loopback (either of them may not exist if ire_create failed
14494 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14495 	 * always hit the first one and hence will always accept one copy.
14496 	 *
14497 	 * We have a broadcast ire per ill for all the unique prefixes
14498 	 * hosted on that ill. As we don't have a way of knowing the
14499 	 * unique prefixes on a given ill and hence in the whole group,
14500 	 * we just call ill_mark_bcast on all the prefixes that exist
14501 	 * in the group. For the common case of one prefix, the code
14502 	 * below optimizes by remebering the last address used for
14503 	 * markng. In the case of multiple prefixes, this will still
14504 	 * optimize depending the order of prefixes.
14505 	 *
14506 	 * The only unique address across the whole group is 0.0.0.0 and
14507 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14508 	 * the first ire in the bucket for receiving and disables the
14509 	 * others.
14510 	 */
14511 	ill_mark_bcast(illgrp, 0);
14512 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14513 	for (; ill != NULL; ill = ill->ill_group_next) {
14514 
14515 		for (ipif = ill->ill_ipif; ipif != NULL;
14516 		    ipif = ipif->ipif_next) {
14517 
14518 			if (!(ipif->ipif_flags & IPIF_UP) ||
14519 			    ipif->ipif_subnet == 0) {
14520 				continue;
14521 			}
14522 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14523 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14524 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14525 			} else {
14526 				net_mask = htonl(IN_CLASSA_NET);
14527 			}
14528 			addr = net_mask & ipif->ipif_subnet;
14529 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14530 				ill_mark_bcast(illgrp, addr);
14531 				net_addr = ~net_mask | addr;
14532 				ill_mark_bcast(illgrp, net_addr);
14533 			}
14534 			prev_net_addr = addr;
14535 
14536 			subnet_netmask = ipif->ipif_net_mask;
14537 			addr = ipif->ipif_subnet;
14538 			if (prev_subnet_addr == 0 ||
14539 			    prev_subnet_addr != addr) {
14540 				ill_mark_bcast(illgrp, addr);
14541 				subnet_addr = ~subnet_netmask | addr;
14542 				ill_mark_bcast(illgrp, subnet_addr);
14543 			}
14544 			prev_subnet_addr = addr;
14545 		}
14546 	}
14547 }
14548 
14549 /*
14550  * This function is called while forming ill groups.
14551  *
14552  * Currently, we handle only allmulti groups. We want to join
14553  * allmulti on only one of the ills in the groups. In future,
14554  * when we have link aggregation, we may have to join normal
14555  * multicast groups on multiple ills as switch does inbound load
14556  * balancing. Following are the functions that calls this
14557  * function :
14558  *
14559  * 1) ill_recover_multicast : Interface is coming back UP.
14560  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14561  *    will call ill_recover_multicast to recover all the multicast
14562  *    groups. We need to make sure that only one member is joined
14563  *    in the ill group.
14564  *
14565  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14566  *    Somebody is joining allmulti. We need to make sure that only one
14567  *    member is joined in the group.
14568  *
14569  * 3) illgrp_insert : If allmulti has already joined, we need to make
14570  *    sure that only one member is joined in the group.
14571  *
14572  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14573  *    allmulti who we have nominated. We need to pick someother ill.
14574  *
14575  * 5) illgrp_delete : The ill we nominated is leaving the group,
14576  *    we need to pick a new ill to join the group.
14577  *
14578  * For (1), (2), (5) - we just have to check whether there is
14579  * a good ill joined in the group. If we could not find any ills
14580  * joined the group, we should join.
14581  *
14582  * For (4), the one that was nominated to receive, left the group.
14583  * There could be nobody joined in the group when this function is
14584  * called.
14585  *
14586  * For (3) - we need to explicitly check whether there are multiple
14587  * ills joined in the group.
14588  *
14589  * For simplicity, we don't differentiate any of the above cases. We
14590  * just leave the group if it is joined on any of them and join on
14591  * the first good ill.
14592  */
14593 int
14594 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14595 {
14596 	ilm_t *ilm;
14597 	ill_t *ill;
14598 	ill_t *fallback_inactive_ill = NULL;
14599 	ill_t *fallback_failed_ill = NULL;
14600 	int ret = 0;
14601 
14602 	/*
14603 	 * Leave the allmulti on all the ills and start fresh.
14604 	 */
14605 	for (ill = illgrp->illgrp_ill; ill != NULL;
14606 	    ill = ill->ill_group_next) {
14607 		if (ill->ill_join_allmulti)
14608 			(void) ip_leave_allmulti(ill->ill_ipif);
14609 	}
14610 
14611 	/*
14612 	 * Choose a good ill. Fallback to inactive or failed if
14613 	 * none available. We need to fallback to FAILED in the
14614 	 * case where we have 2 interfaces in a group - where
14615 	 * one of them is failed and another is a good one and
14616 	 * the good one (not marked inactive) is leaving the group.
14617 	 */
14618 	ret = 0;
14619 	for (ill = illgrp->illgrp_ill; ill != NULL;
14620 	    ill = ill->ill_group_next) {
14621 		/* Never pick an offline interface */
14622 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14623 			continue;
14624 
14625 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14626 			fallback_failed_ill = ill;
14627 			continue;
14628 		}
14629 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14630 			fallback_inactive_ill = ill;
14631 			continue;
14632 		}
14633 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14634 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14635 				ret = ip_join_allmulti(ill->ill_ipif);
14636 				/*
14637 				 * ip_join_allmulti can fail because of memory
14638 				 * failures. So, make sure we join at least
14639 				 * on one ill.
14640 				 */
14641 				if (ill->ill_join_allmulti)
14642 					return (0);
14643 			}
14644 		}
14645 	}
14646 	if (ret != 0) {
14647 		/*
14648 		 * If we tried nominating above and failed to do so,
14649 		 * return error. We might have tried multiple times.
14650 		 * But, return the latest error.
14651 		 */
14652 		return (ret);
14653 	}
14654 	if ((ill = fallback_inactive_ill) != NULL) {
14655 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14656 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14657 				ret = ip_join_allmulti(ill->ill_ipif);
14658 				return (ret);
14659 			}
14660 		}
14661 	} else if ((ill = fallback_failed_ill) != NULL) {
14662 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14663 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14664 				ret = ip_join_allmulti(ill->ill_ipif);
14665 				return (ret);
14666 			}
14667 		}
14668 	}
14669 	return (0);
14670 }
14671 
14672 /*
14673  * This function is called from illgrp_delete after it is
14674  * deleted from the group to reschedule responsibilities
14675  * to a different ill.
14676  */
14677 static void
14678 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14679 {
14680 	ilm_t	*ilm;
14681 	ipif_t	*ipif;
14682 	ipaddr_t subnet_addr;
14683 	ipaddr_t net_addr;
14684 	ipaddr_t net_mask = 0;
14685 	ipaddr_t subnet_netmask;
14686 	ipaddr_t addr;
14687 
14688 	ASSERT(ill->ill_group == NULL);
14689 	/*
14690 	 * Broadcast Responsibility:
14691 	 *
14692 	 * 1. If this ill has been nominated for receiving broadcast
14693 	 * packets, we need to find a new one. Before we find a new
14694 	 * one, we need to re-group the ires that are part of this new
14695 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14696 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14697 	 * thing for us.
14698 	 *
14699 	 * 2. If this ill was not nominated for receiving broadcast
14700 	 * packets, we need to clear the IRE_MARK_NORECV flag
14701 	 * so that we continue to send up broadcast packets.
14702 	 */
14703 	if (!ill->ill_isv6) {
14704 		/*
14705 		 * Case 1 above : No optimization here. Just redo the
14706 		 * nomination.
14707 		 */
14708 		ill_group_bcast_for_xmit(ill);
14709 		ill_nominate_bcast_rcv(illgrp);
14710 
14711 		/*
14712 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14713 		 */
14714 		ill_clear_bcast_mark(ill, 0);
14715 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14716 
14717 		for (ipif = ill->ill_ipif; ipif != NULL;
14718 		    ipif = ipif->ipif_next) {
14719 
14720 			if (!(ipif->ipif_flags & IPIF_UP) ||
14721 			    ipif->ipif_subnet == 0) {
14722 				continue;
14723 			}
14724 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14725 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14726 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14727 			} else {
14728 				net_mask = htonl(IN_CLASSA_NET);
14729 			}
14730 			addr = net_mask & ipif->ipif_subnet;
14731 			ill_clear_bcast_mark(ill, addr);
14732 
14733 			net_addr = ~net_mask | addr;
14734 			ill_clear_bcast_mark(ill, net_addr);
14735 
14736 			subnet_netmask = ipif->ipif_net_mask;
14737 			addr = ipif->ipif_subnet;
14738 			ill_clear_bcast_mark(ill, addr);
14739 
14740 			subnet_addr = ~subnet_netmask | addr;
14741 			ill_clear_bcast_mark(ill, subnet_addr);
14742 		}
14743 	}
14744 
14745 	/*
14746 	 * Multicast Responsibility.
14747 	 *
14748 	 * If we have joined allmulti on this one, find a new member
14749 	 * in the group to join allmulti. As this ill is already part
14750 	 * of allmulti, we don't have to join on this one.
14751 	 *
14752 	 * If we have not joined allmulti on this one, there is no
14753 	 * responsibility to handoff. But we need to take new
14754 	 * responsibility i.e, join allmulti on this one if we need
14755 	 * to.
14756 	 */
14757 	if (ill->ill_join_allmulti) {
14758 		(void) ill_nominate_mcast_rcv(illgrp);
14759 	} else {
14760 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14761 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14762 				(void) ip_join_allmulti(ill->ill_ipif);
14763 				break;
14764 			}
14765 		}
14766 	}
14767 
14768 	/*
14769 	 * We intentionally do the flushing of IRE_CACHES only matching
14770 	 * on the ill and not on groups. Note that we are already deleted
14771 	 * from the group.
14772 	 *
14773 	 * This will make sure that all IRE_CACHES whose stq is pointing
14774 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14775 	 * deleted and IRE_CACHES that are not pointing at this ill will
14776 	 * be left alone.
14777 	 */
14778 	if (ill->ill_isv6) {
14779 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14780 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14781 	} else {
14782 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14783 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14784 	}
14785 
14786 	/*
14787 	 * Some conn may have cached one of the IREs deleted above. By removing
14788 	 * the ire reference, we clean up the extra reference to the ill held in
14789 	 * ire->ire_stq.
14790 	 */
14791 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14792 
14793 	/*
14794 	 * Re-do source address selection for all the members in the
14795 	 * group, if they borrowed source address from one of the ipifs
14796 	 * in this ill.
14797 	 */
14798 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14799 		if (ill->ill_isv6) {
14800 			ipif_update_other_ipifs_v6(ipif, illgrp);
14801 		} else {
14802 			ipif_update_other_ipifs(ipif, illgrp);
14803 		}
14804 	}
14805 }
14806 
14807 /*
14808  * Delete the ill from the group. The caller makes sure that it is
14809  * in a group and it okay to delete from the group. So, we always
14810  * delete here.
14811  */
14812 static void
14813 illgrp_delete(ill_t *ill)
14814 {
14815 	ill_group_t *illgrp;
14816 	ill_group_t *tmpg;
14817 	ill_t *tmp_ill;
14818 
14819 	/*
14820 	 * Reset illgrp_ill_schednext if it was pointing at us.
14821 	 * We need to do this before we set ill_group to NULL.
14822 	 */
14823 	rw_enter(&ill_g_lock, RW_WRITER);
14824 	mutex_enter(&ill->ill_lock);
14825 
14826 	illgrp_reset_schednext(ill);
14827 
14828 	illgrp = ill->ill_group;
14829 
14830 	/* Delete the ill from illgrp. */
14831 	if (illgrp->illgrp_ill == ill) {
14832 		illgrp->illgrp_ill = ill->ill_group_next;
14833 	} else {
14834 		tmp_ill = illgrp->illgrp_ill;
14835 		while (tmp_ill->ill_group_next != ill) {
14836 			tmp_ill = tmp_ill->ill_group_next;
14837 			ASSERT(tmp_ill != NULL);
14838 		}
14839 		tmp_ill->ill_group_next = ill->ill_group_next;
14840 	}
14841 	ill->ill_group = NULL;
14842 	ill->ill_group_next = NULL;
14843 
14844 	illgrp->illgrp_ill_count--;
14845 	mutex_exit(&ill->ill_lock);
14846 	rw_exit(&ill_g_lock);
14847 
14848 	/*
14849 	 * As this ill is leaving the group, we need to hand off
14850 	 * the responsibilities to the other ills in the group, if
14851 	 * this ill had some responsibilities.
14852 	 */
14853 
14854 	ill_handoff_responsibility(ill, illgrp);
14855 
14856 	rw_enter(&ill_g_lock, RW_WRITER);
14857 
14858 	if (illgrp->illgrp_ill_count == 0) {
14859 
14860 		ASSERT(illgrp->illgrp_ill == NULL);
14861 		if (ill->ill_isv6) {
14862 			if (illgrp == illgrp_head_v6) {
14863 				illgrp_head_v6 = illgrp->illgrp_next;
14864 			} else {
14865 				tmpg = illgrp_head_v6;
14866 				while (tmpg->illgrp_next != illgrp) {
14867 					tmpg = tmpg->illgrp_next;
14868 					ASSERT(tmpg != NULL);
14869 				}
14870 				tmpg->illgrp_next = illgrp->illgrp_next;
14871 			}
14872 		} else {
14873 			if (illgrp == illgrp_head_v4) {
14874 				illgrp_head_v4 = illgrp->illgrp_next;
14875 			} else {
14876 				tmpg = illgrp_head_v4;
14877 				while (tmpg->illgrp_next != illgrp) {
14878 					tmpg = tmpg->illgrp_next;
14879 					ASSERT(tmpg != NULL);
14880 				}
14881 				tmpg->illgrp_next = illgrp->illgrp_next;
14882 			}
14883 		}
14884 		mutex_destroy(&illgrp->illgrp_lock);
14885 		mi_free(illgrp);
14886 	}
14887 	rw_exit(&ill_g_lock);
14888 
14889 	/*
14890 	 * Even though the ill is out of the group its not necessary
14891 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
14892 	 * We will split the ipsq when phyint_groupname is set to NULL.
14893 	 */
14894 
14895 	/*
14896 	 * Send a routing sockets message if we are deleting from
14897 	 * groups with names.
14898 	 */
14899 	if (ill->ill_phyint->phyint_groupname_len != 0)
14900 		ip_rts_ifmsg(ill->ill_ipif);
14901 }
14902 
14903 /*
14904  * Re-do source address selection. This is normally called when
14905  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
14906  * ipif comes up.
14907  */
14908 void
14909 ill_update_source_selection(ill_t *ill)
14910 {
14911 	ipif_t *ipif;
14912 
14913 	ASSERT(IAM_WRITER_ILL(ill));
14914 
14915 	if (ill->ill_group != NULL)
14916 		ill = ill->ill_group->illgrp_ill;
14917 
14918 	for (; ill != NULL; ill = ill->ill_group_next) {
14919 		for (ipif = ill->ill_ipif; ipif != NULL;
14920 		    ipif = ipif->ipif_next) {
14921 			if (ill->ill_isv6)
14922 				ipif_recreate_interface_routes_v6(NULL, ipif);
14923 			else
14924 				ipif_recreate_interface_routes(NULL, ipif);
14925 		}
14926 	}
14927 }
14928 
14929 /*
14930  * Insert ill in a group headed by illgrp_head. The caller can either
14931  * pass a groupname in which case we search for a group with the
14932  * same name to insert in or pass a group to insert in. This function
14933  * would only search groups with names.
14934  *
14935  * NOTE : The caller should make sure that there is at least one ipif
14936  *	  UP on this ill so that illgrp_scheduler can pick this ill
14937  *	  for outbound packets. If ill_ipif_up_count is zero, we have
14938  *	  already sent a DL_UNBIND to the driver and we don't want to
14939  *	  send anymore packets. We don't assert for ipif_up_count
14940  *	  to be greater than zero, because ipif_up_done wants to call
14941  *	  this function before bumping up the ipif_up_count. See
14942  *	  ipif_up_done() for details.
14943  */
14944 int
14945 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
14946     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
14947 {
14948 	ill_group_t *illgrp;
14949 	ill_t *prev_ill;
14950 	phyint_t *phyi;
14951 
14952 	ASSERT(ill->ill_group == NULL);
14953 
14954 	rw_enter(&ill_g_lock, RW_WRITER);
14955 	mutex_enter(&ill->ill_lock);
14956 
14957 	if (groupname != NULL) {
14958 		/*
14959 		 * Look for a group with a matching groupname to insert.
14960 		 */
14961 		for (illgrp = *illgrp_head; illgrp != NULL;
14962 		    illgrp = illgrp->illgrp_next) {
14963 
14964 			ill_t *tmp_ill;
14965 
14966 			tmp_ill = illgrp->illgrp_ill;
14967 			ASSERT(tmp_ill != NULL && tmp_ill->ill_phyint != NULL);
14968 			phyi = tmp_ill->ill_phyint;
14969 			/*
14970 			 * Look at groups which has names only.
14971 			 */
14972 			if (phyi->phyint_groupname_len == 0)
14973 				continue;
14974 			/*
14975 			 * Names are stored in the phyint common to both
14976 			 * IPv4 and IPv6.
14977 			 */
14978 			if (mi_strcmp(phyi->phyint_groupname,
14979 			    groupname) == 0) {
14980 				break;
14981 			}
14982 		}
14983 	} else {
14984 		/*
14985 		 * If the caller passes in a NULL "grp_to_insert", we
14986 		 * allocate one below and insert this singleton.
14987 		 */
14988 		illgrp = grp_to_insert;
14989 	}
14990 
14991 	ill->ill_group_next = NULL;
14992 
14993 	if (illgrp == NULL) {
14994 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
14995 		if (illgrp == NULL) {
14996 			return (ENOMEM);
14997 		}
14998 		illgrp->illgrp_next = *illgrp_head;
14999 		*illgrp_head = illgrp;
15000 		illgrp->illgrp_ill = ill;
15001 		illgrp->illgrp_ill_count = 1;
15002 		ill->ill_group = illgrp;
15003 		/*
15004 		 * Used in illgrp_scheduler to protect multiple threads
15005 		 * from traversing the list.
15006 		 */
15007 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15008 	} else {
15009 		ASSERT(ill->ill_net_type ==
15010 		    illgrp->illgrp_ill->ill_net_type);
15011 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15012 
15013 		/* Insert ill at tail of this group */
15014 		prev_ill = illgrp->illgrp_ill;
15015 		while (prev_ill->ill_group_next != NULL)
15016 			prev_ill = prev_ill->ill_group_next;
15017 		prev_ill->ill_group_next = ill;
15018 		ill->ill_group = illgrp;
15019 		illgrp->illgrp_ill_count++;
15020 		/*
15021 		 * Inherit group properties. Currently only forwarding
15022 		 * is the property we try to keep the same with all the
15023 		 * ills. When there are more, we will abstract this into
15024 		 * a function.
15025 		 */
15026 		ill->ill_flags &= ~ILLF_ROUTER;
15027 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15028 	}
15029 	mutex_exit(&ill->ill_lock);
15030 	rw_exit(&ill_g_lock);
15031 
15032 	/*
15033 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15034 	 *    may be zero as it has not yet been bumped. But the ires
15035 	 *    have already been added. So, we do the nomination here
15036 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15037 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15038 	 *    ill_ipif_up_count here while nominating broadcast ires for
15039 	 *    receive.
15040 	 *
15041 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15042 	 *    to group them properly as ire_add() has already happened
15043 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15044 	 *    case, we need to do it here anyway.
15045 	 */
15046 	if (!ill->ill_isv6) {
15047 		ill_group_bcast_for_xmit(ill);
15048 		ill_nominate_bcast_rcv(illgrp);
15049 	}
15050 
15051 	if (!ipif_is_coming_up) {
15052 		/*
15053 		 * When ipif_up_done() calls this function, the multicast
15054 		 * groups have not been joined yet. So, there is no point in
15055 		 * nomination. ip_join_allmulti will handle groups when
15056 		 * ill_recover_multicast is called from ipif_up_done() later.
15057 		 */
15058 		(void) ill_nominate_mcast_rcv(illgrp);
15059 		/*
15060 		 * ipif_up_done calls ill_update_source_selection
15061 		 * anyway. Moreover, we don't want to re-create
15062 		 * interface routes while ipif_up_done() still has reference
15063 		 * to them. Refer to ipif_up_done() for more details.
15064 		 */
15065 		ill_update_source_selection(ill);
15066 	}
15067 
15068 	/*
15069 	 * Send a routing sockets message if we are inserting into
15070 	 * groups with names.
15071 	 */
15072 	if (groupname != NULL)
15073 		ip_rts_ifmsg(ill->ill_ipif);
15074 	return (0);
15075 }
15076 
15077 /*
15078  * Return the first phyint matching the groupname. There could
15079  * be more than one when there are ill groups.
15080  *
15081  * Needs work: called only from ip_sioctl_groupname
15082  */
15083 static phyint_t *
15084 phyint_lookup_group(char *groupname)
15085 {
15086 	phyint_t *phyi;
15087 
15088 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15089 	/*
15090 	 * Group names are stored in the phyint - a common structure
15091 	 * to both IPv4 and IPv6.
15092 	 */
15093 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15094 	for (; phyi != NULL;
15095 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15096 	    phyi, AVL_AFTER)) {
15097 		if (phyi->phyint_groupname_len == 0)
15098 			continue;
15099 		ASSERT(phyi->phyint_groupname != NULL);
15100 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15101 			return (phyi);
15102 	}
15103 	return (NULL);
15104 }
15105 
15106 
15107 
15108 /*
15109  * MT notes on creation and deletion of IPMP groups
15110  *
15111  * Creation and deletion of IPMP groups introduce the need to merge or
15112  * split the associated serialization objects i.e the ipsq's. Normally all
15113  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15114  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15115  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15116  * is a need to change the <ill-ipsq> association and we have to operate on both
15117  * the source and destination IPMP groups. For eg. attempting to set the
15118  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15119  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15120  * source or destination IPMP group are mapped to a single ipsq for executing
15121  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15122  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15123  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15124  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15125  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15126  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15127  *
15128  * In the above example the ioctl handling code locates the current ipsq of hme0
15129  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15130  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15131  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15132  * the destination ipsq. If the destination ipsq is not busy, it also enters
15133  * the destination ipsq exclusively. Now the actual groupname setting operation
15134  * can proceed. If the destination ipsq is busy, the operation is enqueued
15135  * on the destination (merged) ipsq and will be handled in the unwind from
15136  * ipsq_exit.
15137  *
15138  * To prevent other threads accessing the ill while the group name change is
15139  * in progres, we bring down the ipifs which also removes the ill from the
15140  * group. The group is changed in phyint and when the first ipif on the ill
15141  * is brought up, the ill is inserted into the right IPMP group by
15142  * illgrp_insert.
15143  */
15144 /* ARGSUSED */
15145 int
15146 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15147     ip_ioctl_cmd_t *ipip, void *ifreq)
15148 {
15149 	int i;
15150 	char *tmp;
15151 	int namelen;
15152 	ill_t *ill = ipif->ipif_ill;
15153 	ill_t *ill_v4, *ill_v6;
15154 	int err = 0;
15155 	phyint_t *phyi;
15156 	phyint_t *phyi_tmp;
15157 	struct lifreq *lifr;
15158 	mblk_t	*mp1;
15159 	char *groupname;
15160 	ipsq_t *ipsq;
15161 
15162 	ASSERT(IAM_WRITER_IPIF(ipif));
15163 
15164 	/* Existance verified in ip_wput_nondata */
15165 	mp1 = mp->b_cont->b_cont;
15166 	lifr = (struct lifreq *)mp1->b_rptr;
15167 	groupname = lifr->lifr_groupname;
15168 
15169 	if (ipif->ipif_id != 0)
15170 		return (EINVAL);
15171 
15172 	phyi = ill->ill_phyint;
15173 	ASSERT(phyi != NULL);
15174 
15175 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15176 		return (EINVAL);
15177 
15178 	tmp = groupname;
15179 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15180 		;
15181 
15182 	if (i == LIFNAMSIZ) {
15183 		/* no null termination */
15184 		return (EINVAL);
15185 	}
15186 
15187 	/*
15188 	 * Calculate the namelen exclusive of the null
15189 	 * termination character.
15190 	 */
15191 	namelen = tmp - groupname;
15192 
15193 	ill_v4 = phyi->phyint_illv4;
15194 	ill_v6 = phyi->phyint_illv6;
15195 
15196 	/*
15197 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15198 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15199 	 * synchronization notes in ip.c
15200 	 */
15201 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15202 		return (EINVAL);
15203 	}
15204 
15205 	/*
15206 	 * mark the ill as changing.
15207 	 * this should queue all new requests on the syncq.
15208 	 */
15209 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15210 
15211 	if (ill_v4 != NULL)
15212 		ill_v4->ill_state_flags |= ILL_CHANGING;
15213 	if (ill_v6 != NULL)
15214 		ill_v6->ill_state_flags |= ILL_CHANGING;
15215 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15216 
15217 	if (namelen == 0) {
15218 		/*
15219 		 * Null string means remove this interface from the
15220 		 * existing group.
15221 		 */
15222 		if (phyi->phyint_groupname_len == 0) {
15223 			/*
15224 			 * Never was in a group.
15225 			 */
15226 			err = 0;
15227 			goto done;
15228 		}
15229 
15230 		/*
15231 		 * IPv4 or IPv6 may be temporarily out of the group when all
15232 		 * the ipifs are down. Thus, we need to check for ill_group to
15233 		 * be non-NULL.
15234 		 */
15235 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15236 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15237 			mutex_enter(&ill_v4->ill_lock);
15238 			if (!ill_is_quiescent(ill_v4)) {
15239 				/*
15240 				 * ipsq_pending_mp_add will not fail since
15241 				 * connp is NULL
15242 				 */
15243 				(void) ipsq_pending_mp_add(NULL,
15244 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15245 				mutex_exit(&ill_v4->ill_lock);
15246 				err = EINPROGRESS;
15247 				goto done;
15248 			}
15249 			mutex_exit(&ill_v4->ill_lock);
15250 		}
15251 
15252 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15253 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15254 			mutex_enter(&ill_v6->ill_lock);
15255 			if (!ill_is_quiescent(ill_v6)) {
15256 				(void) ipsq_pending_mp_add(NULL,
15257 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15258 				mutex_exit(&ill_v6->ill_lock);
15259 				err = EINPROGRESS;
15260 				goto done;
15261 			}
15262 			mutex_exit(&ill_v6->ill_lock);
15263 		}
15264 
15265 		rw_enter(&ill_g_lock, RW_WRITER);
15266 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15267 		mutex_enter(&phyi->phyint_lock);
15268 		ASSERT(phyi->phyint_groupname != NULL);
15269 		mi_free(phyi->phyint_groupname);
15270 		phyi->phyint_groupname = NULL;
15271 		phyi->phyint_groupname_len = 0;
15272 		mutex_exit(&phyi->phyint_lock);
15273 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15274 		rw_exit(&ill_g_lock);
15275 		err = ill_up_ipifs(ill, q, mp);
15276 
15277 		/*
15278 		 * set the split flag so that the ipsq can be split
15279 		 */
15280 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15281 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15282 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15283 
15284 	} else {
15285 		if (phyi->phyint_groupname_len != 0) {
15286 			ASSERT(phyi->phyint_groupname != NULL);
15287 			/* Are we inserting in the same group ? */
15288 			if (mi_strcmp(groupname,
15289 			    phyi->phyint_groupname) == 0) {
15290 				err = 0;
15291 				goto done;
15292 			}
15293 		}
15294 
15295 		rw_enter(&ill_g_lock, RW_READER);
15296 		/*
15297 		 * Merge ipsq for the group's.
15298 		 * This check is here as multiple groups/ills might be
15299 		 * sharing the same ipsq.
15300 		 * If we have to merege than the operation is restarted
15301 		 * on the new ipsq.
15302 		 */
15303 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15304 		if (phyi->phyint_ipsq != ipsq) {
15305 			rw_exit(&ill_g_lock);
15306 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15307 			goto done;
15308 		}
15309 		/*
15310 		 * Running exclusive on new ipsq.
15311 		 */
15312 
15313 		ASSERT(ipsq != NULL);
15314 		ASSERT(ipsq->ipsq_writer == curthread);
15315 
15316 		/*
15317 		 * Check whether the ill_type and ill_net_type matches before
15318 		 * we allocate any memory so that the cleanup is easier.
15319 		 *
15320 		 * We can't group dissimilar ones as we can't load spread
15321 		 * packets across the group because of potential link-level
15322 		 * header differences.
15323 		 */
15324 		phyi_tmp = phyint_lookup_group(groupname);
15325 		if (phyi_tmp != NULL) {
15326 			if ((ill_v4 != NULL &&
15327 			    phyi_tmp->phyint_illv4 != NULL) &&
15328 			    ((ill_v4->ill_net_type !=
15329 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15330 			    (ill_v4->ill_type !=
15331 			    phyi_tmp->phyint_illv4->ill_type))) {
15332 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15333 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15334 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15335 				rw_exit(&ill_g_lock);
15336 				return (EINVAL);
15337 			}
15338 			if ((ill_v6 != NULL &&
15339 			    phyi_tmp->phyint_illv6 != NULL) &&
15340 			    ((ill_v6->ill_net_type !=
15341 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15342 			    (ill_v6->ill_type !=
15343 			    phyi_tmp->phyint_illv6->ill_type))) {
15344 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15345 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15346 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15347 				rw_exit(&ill_g_lock);
15348 				return (EINVAL);
15349 			}
15350 		}
15351 
15352 		rw_exit(&ill_g_lock);
15353 
15354 		/*
15355 		 * bring down all v4 ipifs.
15356 		 */
15357 		if (ill_v4 != NULL) {
15358 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15359 		}
15360 
15361 		/*
15362 		 * bring down all v6 ipifs.
15363 		 */
15364 		if (ill_v6 != NULL) {
15365 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15366 		}
15367 
15368 		/*
15369 		 * make sure all ipifs are down and there are no active
15370 		 * references. Call to ipsq_pending_mp_add will not fail
15371 		 * since connp is NULL.
15372 		 */
15373 		if (ill_v4 != NULL) {
15374 			mutex_enter(&ill_v4->ill_lock);
15375 			if (!ill_is_quiescent(ill_v4)) {
15376 				(void) ipsq_pending_mp_add(NULL,
15377 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15378 				mutex_exit(&ill_v4->ill_lock);
15379 				err = EINPROGRESS;
15380 				goto done;
15381 			}
15382 			mutex_exit(&ill_v4->ill_lock);
15383 		}
15384 
15385 		if (ill_v6 != NULL) {
15386 			mutex_enter(&ill_v6->ill_lock);
15387 			if (!ill_is_quiescent(ill_v6)) {
15388 				(void) ipsq_pending_mp_add(NULL,
15389 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15390 				mutex_exit(&ill_v6->ill_lock);
15391 				err = EINPROGRESS;
15392 				goto done;
15393 			}
15394 			mutex_exit(&ill_v6->ill_lock);
15395 		}
15396 
15397 		/*
15398 		 * allocate including space for null terminator
15399 		 * before we insert.
15400 		 */
15401 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15402 		if (tmp == NULL)
15403 			return (ENOMEM);
15404 
15405 		rw_enter(&ill_g_lock, RW_WRITER);
15406 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15407 		mutex_enter(&phyi->phyint_lock);
15408 		if (phyi->phyint_groupname_len != 0) {
15409 			ASSERT(phyi->phyint_groupname != NULL);
15410 			mi_free(phyi->phyint_groupname);
15411 		}
15412 
15413 		/*
15414 		 * setup the new group name.
15415 		 */
15416 		phyi->phyint_groupname = tmp;
15417 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15418 		phyi->phyint_groupname_len = namelen + 1;
15419 		mutex_exit(&phyi->phyint_lock);
15420 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15421 		rw_exit(&ill_g_lock);
15422 
15423 		err = ill_up_ipifs(ill, q, mp);
15424 	}
15425 
15426 done:
15427 	/*
15428 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15429 	 */
15430 	if (err != EINPROGRESS) {
15431 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15432 		if (ill_v4 != NULL)
15433 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15434 		if (ill_v6 != NULL)
15435 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15436 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15437 	}
15438 	return (err);
15439 }
15440 
15441 /* ARGSUSED */
15442 int
15443 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15444     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15445 {
15446 	ill_t *ill;
15447 	phyint_t *phyi;
15448 	struct lifreq *lifr;
15449 	mblk_t	*mp1;
15450 
15451 	/* Existence verified in ip_wput_nondata */
15452 	mp1 = mp->b_cont->b_cont;
15453 	lifr = (struct lifreq *)mp1->b_rptr;
15454 	ill = ipif->ipif_ill;
15455 	phyi = ill->ill_phyint;
15456 
15457 	lifr->lifr_groupname[0] = '\0';
15458 	/*
15459 	 * ill_group may be null if all the interfaces
15460 	 * are down. But still, the phyint should always
15461 	 * hold the name.
15462 	 */
15463 	if (phyi->phyint_groupname_len != 0) {
15464 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15465 		    phyi->phyint_groupname_len);
15466 	}
15467 
15468 	return (0);
15469 }
15470 
15471 
15472 typedef struct conn_move_s {
15473 	ill_t	*cm_from_ill;
15474 	ill_t	*cm_to_ill;
15475 	int	cm_ifindex;
15476 } conn_move_t;
15477 
15478 /*
15479  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15480  */
15481 static void
15482 conn_move(conn_t *connp, caddr_t arg)
15483 {
15484 	conn_move_t *connm;
15485 	int ifindex;
15486 	int i;
15487 	ill_t *from_ill;
15488 	ill_t *to_ill;
15489 	ilg_t *ilg;
15490 	ilm_t *ret_ilm;
15491 
15492 	connm = (conn_move_t *)arg;
15493 	ifindex = connm->cm_ifindex;
15494 	from_ill = connm->cm_from_ill;
15495 	to_ill = connm->cm_to_ill;
15496 
15497 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15498 
15499 	/* All multicast fields protected by conn_lock */
15500 	mutex_enter(&connp->conn_lock);
15501 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15502 	if ((connp->conn_outgoing_ill == from_ill) &&
15503 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15504 		connp->conn_outgoing_ill = to_ill;
15505 		connp->conn_incoming_ill = to_ill;
15506 	}
15507 
15508 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15509 
15510 	if ((connp->conn_multicast_ill == from_ill) &&
15511 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15512 		connp->conn_multicast_ill = connm->cm_to_ill;
15513 	}
15514 
15515 	/* Change IP_XMIT_IF associations */
15516 	if ((connp->conn_xmit_if_ill == from_ill) &&
15517 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15518 		connp->conn_xmit_if_ill = to_ill;
15519 	}
15520 	/*
15521 	 * Change the ilg_ill to point to the new one. This assumes
15522 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15523 	 * has been told to receive packets on this interface.
15524 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15525 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15526 	 * some ilms may not have moved. We check to see whether
15527 	 * the ilms have moved to to_ill. We can't check on from_ill
15528 	 * as in the process of moving, we could have split an ilm
15529 	 * in to two - which has the same orig_ifindex and v6group.
15530 	 *
15531 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15532 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15533 	 */
15534 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15535 		ilg = &connp->conn_ilg[i];
15536 		if ((ilg->ilg_ill == from_ill) &&
15537 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15538 			/* ifindex != 0 indicates failback */
15539 			if (ifindex != 0) {
15540 				connp->conn_ilg[i].ilg_ill = to_ill;
15541 				continue;
15542 			}
15543 
15544 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15545 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15546 			    connp->conn_zoneid);
15547 
15548 			if (ret_ilm != NULL)
15549 				connp->conn_ilg[i].ilg_ill = to_ill;
15550 		}
15551 	}
15552 	mutex_exit(&connp->conn_lock);
15553 }
15554 
15555 static void
15556 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15557 {
15558 	conn_move_t connm;
15559 
15560 	connm.cm_from_ill = from_ill;
15561 	connm.cm_to_ill = to_ill;
15562 	connm.cm_ifindex = ifindex;
15563 
15564 	ipcl_walk(conn_move, (caddr_t)&connm);
15565 }
15566 
15567 /*
15568  * ilm has been moved from from_ill to to_ill.
15569  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15570  * appropriately.
15571  *
15572  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15573  *	  the code there de-references ipif_ill to get the ill to
15574  *	  send multicast requests. It does not work as ipif is on its
15575  *	  move and already moved when this function is called.
15576  *	  Thus, we need to use from_ill and to_ill send down multicast
15577  *	  requests.
15578  */
15579 static void
15580 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15581 {
15582 	ipif_t *ipif;
15583 	ilm_t *ilm;
15584 
15585 	/*
15586 	 * See whether we need to send down DL_ENABMULTI_REQ on
15587 	 * to_ill as ilm has just been added.
15588 	 */
15589 	ASSERT(IAM_WRITER_ILL(to_ill));
15590 	ASSERT(IAM_WRITER_ILL(from_ill));
15591 
15592 	ILM_WALKER_HOLD(to_ill);
15593 	for (ilm = to_ill->ill_ilm; ilm != NULL && ilm->ilm_is_new &&
15594 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm->ilm_next) {
15595 
15596 		/*
15597 		 * no locks held, ill/ipif cannot dissappear as long
15598 		 * as we are writer.
15599 		 */
15600 		ipif = to_ill->ill_ipif;
15601 		/*
15602 		 * No need to hold any lock as we are the writer and this
15603 		 * can only be changed by a writer.
15604 		 */
15605 		ilm->ilm_is_new = B_FALSE;
15606 
15607 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15608 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15609 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15610 			    "resolver\n"));
15611 			continue;		/* Must be IRE_IF_NORESOLVER */
15612 		}
15613 
15614 
15615 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15616 			ip1dbg(("ilm_send_multicast_reqs: "
15617 			    "to_ill MULTI_BCAST\n"));
15618 			ilm->ilm_join_mld = B_FALSE;
15619 			goto from;
15620 		}
15621 
15622 		if (ilm->ilm_join_mld) {
15623 			ASSERT(to_ill->ill_isv6);
15624 			mld_joingroup(ilm);
15625 		}
15626 
15627 		ilm->ilm_join_mld = B_FALSE;
15628 
15629 		if (to_ill->ill_ipif_up_count == 0) {
15630 			/*
15631 			 * Nobody there. All multicast addresses will be
15632 			 * re-joined when we get the DL_BIND_ACK bringing the
15633 			 * interface up.
15634 			 */
15635 			ilm->ilm_notify_driver = B_FALSE;
15636 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15637 			goto from;
15638 		}
15639 
15640 		/*
15641 		 * For allmulti address, we want to join on only one interface.
15642 		 * Checking for ilm_numentries_v6 is not correct as you may
15643 		 * find an ilm with zero address on to_ill, but we may not
15644 		 * have nominated to_ill for receiving. Thus, if we have
15645 		 * nominated from_ill (ill_join_allmulti is set), nominate
15646 		 * only if to_ill is not already nominated (to_ill normally
15647 		 * should not have been nominated if "from_ill" has already
15648 		 * been nominated. As we don't prevent failovers from happening
15649 		 * across groups, we don't assert).
15650 		 */
15651 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15652 			/*
15653 			 * There is no need to hold ill locks as we are
15654 			 * writer on both ills and when ill_join_allmulti
15655 			 * is changed the thread is always a writer.
15656 			 */
15657 			if (from_ill->ill_join_allmulti &&
15658 			    !to_ill->ill_join_allmulti) {
15659 				(void) ip_join_allmulti(to_ill->ill_ipif);
15660 			}
15661 		} else if (ilm->ilm_notify_driver) {
15662 
15663 			/*
15664 			 * This is a newly moved ilm so we need to tell the
15665 			 * driver about the new group. There can be more than
15666 			 * one ilm's for the same group in the list each with a
15667 			 * different orig_ifindex. We have to inform the driver
15668 			 * once. In ilm_move_v[4,6] we only set the flag
15669 			 * ilm_notify_driver for the first ilm.
15670 			 */
15671 
15672 			(void) ip_ll_send_enabmulti_req(to_ill,
15673 			    &ilm->ilm_v6addr);
15674 		}
15675 
15676 		ilm->ilm_notify_driver = B_FALSE;
15677 
15678 		/*
15679 		 * See whether we need to send down DL_DISABMULTI_REQ on
15680 		 * from_ill as ilm has just been removed.
15681 		 */
15682 from:
15683 		ipif = from_ill->ill_ipif;
15684 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15685 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15686 			ip1dbg(("ilm_send_multicast_reqs: "
15687 			    "from_ill not resolver\n"));
15688 			continue;		/* Must be IRE_IF_NORESOLVER */
15689 		}
15690 
15691 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15692 			ip1dbg(("ilm_send_multicast_reqs: "
15693 			    "from_ill MULTI_BCAST\n"));
15694 			continue;
15695 		}
15696 
15697 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15698 			if (from_ill->ill_join_allmulti)
15699 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15700 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15701 			(void) ip_ll_send_disabmulti_req(from_ill,
15702 		    &ilm->ilm_v6addr);
15703 		}
15704 	}
15705 	ILM_WALKER_RELE(to_ill);
15706 }
15707 
15708 /*
15709  * This function is called when all multicast memberships needs
15710  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15711  * called only once unlike the IPv4 counterpart where it is called after
15712  * every logical interface is moved. The reason is due to multicast
15713  * memberships are joined using an interface address in IPv4 while in
15714  * IPv6, interface index is used.
15715  */
15716 static void
15717 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15718 {
15719 	ilm_t	*ilm;
15720 	ilm_t	*ilm_next;
15721 	ilm_t	*new_ilm;
15722 	ilm_t	**ilmp;
15723 	int	count;
15724 	char buf[INET6_ADDRSTRLEN];
15725 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15726 
15727 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15728 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15729 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15730 
15731 	if (ifindex == 0) {
15732 		/*
15733 		 * Form the solicited node mcast address which is used later.
15734 		 */
15735 		ipif_t *ipif;
15736 
15737 		ipif = from_ill->ill_ipif;
15738 		ASSERT(ipif->ipif_id == 0);
15739 
15740 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15741 	}
15742 
15743 	ilmp = &from_ill->ill_ilm;
15744 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
15745 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
15746 		ilm_next = ilm->ilm_next;
15747 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15748 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15749 		ASSERT(ilm->ilm_orig_ifindex != 0);
15750 		if (ilm->ilm_orig_ifindex == ifindex) {
15751 			/*
15752 			 * We are failing back multicast memberships.
15753 			 * If the same ilm exists in to_ill, it means somebody
15754 			 * has joined the same group there e.g. ff02::1
15755 			 * is joined within the kernel when the interfaces
15756 			 * came UP.
15757 			 */
15758 			ASSERT(ilm->ilm_ipif == NULL);
15759 			if (new_ilm != NULL) {
15760 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15761 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15762 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15763 					new_ilm->ilm_join_mld = B_TRUE;
15764 				}
15765 			} else {
15766 				/*
15767 				 * check if we can just move the ilm
15768 				 */
15769 				if (from_ill->ill_ilm_walker_cnt != 0) {
15770 					/*
15771 					 * We have walkers we cannot move
15772 					 * the ilm, so allocate a new ilm,
15773 					 * this (old) ilm will be marked
15774 					 * ILM_DELETED at the end of the loop
15775 					 * and will be freed when the
15776 					 * last walker exits.
15777 					 */
15778 					new_ilm = (ilm_t *)mi_zalloc
15779 					    (sizeof (ilm_t));
15780 					if (new_ilm == NULL) {
15781 						ip0dbg(("ilm_move_v6: "
15782 						    "FAILBACK of IPv6"
15783 						    " multicast address %s : "
15784 						    "from %s to"
15785 						    " %s failed : ENOMEM \n",
15786 						    inet_ntop(AF_INET6,
15787 						    &ilm->ilm_v6addr, buf,
15788 						    sizeof (buf)),
15789 						    from_ill->ill_name,
15790 						    to_ill->ill_name));
15791 
15792 							ilmp = &ilm->ilm_next;
15793 							continue;
15794 					}
15795 					*new_ilm = *ilm;
15796 					/*
15797 					 * we don't want new_ilm linked to
15798 					 * ilm's filter list.
15799 					 */
15800 					new_ilm->ilm_filter = NULL;
15801 				} else {
15802 					/*
15803 					 * No walkers we can move the ilm.
15804 					 * lets take it out of the list.
15805 					 */
15806 					*ilmp = ilm->ilm_next;
15807 					ilm->ilm_next = NULL;
15808 					new_ilm = ilm;
15809 				}
15810 
15811 				new_ilm->ilm_ill = to_ill;
15812 				/* Add to the to_ill's list */
15813 				new_ilm->ilm_next = to_ill->ill_ilm;
15814 				to_ill->ill_ilm = new_ilm;
15815 				/*
15816 				 * set the flag so that mld_joingroup is
15817 				 * called in ilm_send_multicast_reqs().
15818 				 */
15819 				new_ilm->ilm_join_mld = B_TRUE;
15820 				/*
15821 				 * if this is the first ilm for the group
15822 				 * set ilm_notify_driver so that we notify the
15823 				 * driver in ilm_send_multicast_reqs.
15824 				 */
15825 				if (ilm_lookup_ill_v6(to_ill,
15826 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15827 					new_ilm->ilm_notify_driver = B_TRUE;
15828 			}
15829 			goto bottom;
15830 		} else if (ifindex != 0) {
15831 			/*
15832 			 * If this is FAILBACK (ifindex != 0) and the ifindex
15833 			 * has not matched above, look at the next ilm.
15834 			 */
15835 			ilmp = &ilm->ilm_next;
15836 			continue;
15837 		}
15838 		/*
15839 		 * If we are here, it means ifindex is 0. Failover
15840 		 * everything.
15841 		 *
15842 		 * We need to handle solicited node mcast address
15843 		 * and all_nodes mcast address differently as they
15844 		 * are joined witin the kenrel (ipif_multicast_up)
15845 		 * and potentially from the userland. We are called
15846 		 * after the ipifs of from_ill has been moved.
15847 		 * If we still find ilms on ill with solicited node
15848 		 * mcast address or all_nodes mcast address, it must
15849 		 * belong to the UP interface that has not moved e.g.
15850 		 * ipif_id 0 with the link local prefix does not move.
15851 		 * We join this on the new ill accounting for all the
15852 		 * userland memberships so that applications don't
15853 		 * see any failure.
15854 		 *
15855 		 * We need to make sure that we account only for the
15856 		 * solicited node and all node multicast addresses
15857 		 * that was brought UP on these. In the case of
15858 		 * a failover from A to B, we might have ilms belonging
15859 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
15860 		 * for the membership from the userland. If we are failing
15861 		 * over from B to C now, we will find the ones belonging
15862 		 * to A on B. These don't account for the ill_ipif_up_count.
15863 		 * They just move from B to C. The check below on
15864 		 * ilm_orig_ifindex ensures that.
15865 		 */
15866 		if ((ilm->ilm_orig_ifindex ==
15867 		    from_ill->ill_phyint->phyint_ifindex) &&
15868 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
15869 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
15870 		    &ilm->ilm_v6addr))) {
15871 			ASSERT(ilm->ilm_refcnt > 0);
15872 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
15873 			/*
15874 			 * For indentation reasons, we are not using a
15875 			 * "else" here.
15876 			 */
15877 			if (count == 0) {
15878 				ilmp = &ilm->ilm_next;
15879 				continue;
15880 			}
15881 			ilm->ilm_refcnt -= count;
15882 			if (new_ilm != NULL) {
15883 				/*
15884 				 * Can find one with the same
15885 				 * ilm_orig_ifindex, if we are failing
15886 				 * over to a STANDBY. This happens
15887 				 * when somebody wants to join a group
15888 				 * on a STANDBY interface and we
15889 				 * internally join on a different one.
15890 				 * If we had joined on from_ill then, a
15891 				 * failover now will find a new ilm
15892 				 * with this index.
15893 				 */
15894 				ip1dbg(("ilm_move_v6: FAILOVER, found"
15895 				    " new ilm on %s, group address %s\n",
15896 				    to_ill->ill_name,
15897 				    inet_ntop(AF_INET6,
15898 				    &ilm->ilm_v6addr, buf,
15899 				    sizeof (buf))));
15900 				new_ilm->ilm_refcnt += count;
15901 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15902 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15903 					new_ilm->ilm_join_mld = B_TRUE;
15904 				}
15905 			} else {
15906 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
15907 				if (new_ilm == NULL) {
15908 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
15909 					    " multicast address %s : from %s to"
15910 					    " %s failed : ENOMEM \n",
15911 					    inet_ntop(AF_INET6,
15912 					    &ilm->ilm_v6addr, buf,
15913 					    sizeof (buf)), from_ill->ill_name,
15914 					    to_ill->ill_name));
15915 					ilmp = &ilm->ilm_next;
15916 					continue;
15917 				}
15918 				*new_ilm = *ilm;
15919 				new_ilm->ilm_filter = NULL;
15920 				new_ilm->ilm_refcnt = count;
15921 				new_ilm->ilm_ill = to_ill;
15922 				new_ilm->ilm_timer = INFINITY;
15923 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
15924 				new_ilm->ilm_join_mld = B_TRUE;
15925 				/* Add to the to_ill's list */
15926 				new_ilm->ilm_next = to_ill->ill_ilm;
15927 				to_ill->ill_ilm = new_ilm;
15928 				/*
15929 				 * If the to_ill has not joined this
15930 				 * group we need to tell the driver in
15931 				 * ill_send_multicast_reqs.
15932 				 */
15933 				if (ilm_lookup_ill_v6(to_ill,
15934 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15935 					new_ilm->ilm_notify_driver = B_TRUE;
15936 				ASSERT(new_ilm->ilm_ipif == NULL);
15937 			}
15938 			if (ilm->ilm_refcnt == 0) {
15939 				goto bottom;
15940 			} else {
15941 				new_ilm->ilm_is_new = B_TRUE;
15942 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
15943 				CLEAR_SLIST(new_ilm->ilm_filter);
15944 				ilmp = &ilm->ilm_next;
15945 			}
15946 			continue;
15947 		} else {
15948 			/*
15949 			 * ifindex = 0 means, move everything pointing at
15950 			 * from_ill. We are doing this becuase ill has
15951 			 * either FAILED or became INACTIVE.
15952 			 *
15953 			 * As we would like to move things later back to
15954 			 * from_ill, we want to retain the identity of this
15955 			 * ilm. Thus, we don't blindly increment the reference
15956 			 * count on the ilms matching the address alone. We
15957 			 * need to match on the ilm_orig_index also. new_ilm
15958 			 * was obtained by matching ilm_orig_index also.
15959 			 */
15960 			if (new_ilm != NULL) {
15961 				/*
15962 				 * This is possible only if a previous restore
15963 				 * was incomplete i.e restore to
15964 				 * ilm_orig_ifindex left some ilms because
15965 				 * of some failures. Thus when we are failing
15966 				 * again, we might find our old friends there.
15967 				 */
15968 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
15969 				    " on %s, group address %s\n",
15970 				    to_ill->ill_name,
15971 				    inet_ntop(AF_INET6,
15972 				    &ilm->ilm_v6addr, buf,
15973 				    sizeof (buf))));
15974 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15975 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15976 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15977 					new_ilm->ilm_join_mld = B_TRUE;
15978 				}
15979 			} else {
15980 				if (from_ill->ill_ilm_walker_cnt != 0) {
15981 					new_ilm = (ilm_t *)
15982 					    mi_zalloc(sizeof (ilm_t));
15983 					if (new_ilm == NULL) {
15984 						ip0dbg(("ilm_move_v6: "
15985 						    "FAILOVER of IPv6"
15986 						    " multicast address %s : "
15987 						    "from %s to"
15988 						    " %s failed : ENOMEM \n",
15989 						    inet_ntop(AF_INET6,
15990 						    &ilm->ilm_v6addr, buf,
15991 						    sizeof (buf)),
15992 						    from_ill->ill_name,
15993 						    to_ill->ill_name));
15994 
15995 							ilmp = &ilm->ilm_next;
15996 							continue;
15997 					}
15998 					*new_ilm = *ilm;
15999 					new_ilm->ilm_filter = NULL;
16000 				} else {
16001 					*ilmp = ilm->ilm_next;
16002 					new_ilm = ilm;
16003 				}
16004 				/* Add to the to_ill's list */
16005 				new_ilm->ilm_next = to_ill->ill_ilm;
16006 				to_ill->ill_ilm = new_ilm;
16007 				ASSERT(ilm->ilm_ipif == NULL);
16008 				new_ilm->ilm_ill = to_ill;
16009 				new_ilm->ilm_join_mld = B_TRUE;
16010 				/*
16011 				 * If the to_ill has not joined this
16012 				 * group we need to tell the driver in
16013 				 * ill_send_multicast_reqs.
16014 				 */
16015 				if (ilm_lookup_ill_v6(to_ill,
16016 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16017 					new_ilm->ilm_notify_driver = B_TRUE;
16018 			}
16019 
16020 		}
16021 
16022 bottom:
16023 		/*
16024 		 * set ilm_send_multicast_reqs so that we inform the
16025 		 * driver about the multicast group.
16026 		 */
16027 		new_ilm->ilm_is_new = B_TRUE;
16028 		/*
16029 		 * Revert multicast filter state to (EXCLUDE, NULL).
16030 		 * new_ilm->ilm_join_mld should already be set if needed.
16031 		 */
16032 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16033 		CLEAR_SLIST(new_ilm->ilm_filter);
16034 		/*
16035 		 * We allocated/got a new ilm, free the old one.
16036 		 */
16037 		if (new_ilm != ilm) {
16038 			if (from_ill->ill_ilm_walker_cnt == 0) {
16039 				*ilmp = ilm->ilm_next;
16040 				ilm->ilm_next = NULL;
16041 				FREE_SLIST(ilm->ilm_filter);
16042 				FREE_SLIST(ilm->ilm_pendsrcs);
16043 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16044 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16045 				mi_free((char *)ilm);
16046 			} else {
16047 				ilm->ilm_flags |= ILM_DELETED;
16048 				from_ill->ill_ilm_cleanup_reqd = 1;
16049 				ilmp = &ilm->ilm_next;
16050 			}
16051 		}
16052 	}
16053 }
16054 
16055 /*
16056  * Move all the multicast memberships to to_ill. Called when
16057  * an ipif moves from "from_ill" to "to_ill". This function is slightly
16058  * different from IPv6 counterpart as multicast memberships are associated
16059  * with ills in IPv6. This function is called after every ipif is moved
16060  * unlike IPv6, where it is moved only once.
16061  */
16062 static void
16063 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
16064 {
16065 	ilm_t	*ilm;
16066 	ilm_t	*ilm_next;
16067 	ilm_t	*new_ilm;
16068 	ilm_t	**ilmp;
16069 
16070 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16071 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16072 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16073 
16074 	ilmp = &from_ill->ill_ilm;
16075 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
16076 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
16077 		ilm_next = ilm->ilm_next;
16078 		ASSERT(ilm->ilm_ipif != NULL);
16079 
16080 		if (ilm->ilm_ipif != ipif) {
16081 			ilmp = &ilm->ilm_next;
16082 			continue;
16083 		}
16084 
16085 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16086 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16087 			/*
16088 			 * We joined this in ipif_multicast_up
16089 			 * and we never did an ipif_multicast_down
16090 			 * for IPv4. If nobody else from the userland
16091 			 * has reference, we free the ilm, and later
16092 			 * when this ipif comes up on the new ill,
16093 			 * we will join this again.
16094 			 */
16095 			if (--ilm->ilm_refcnt == 0)
16096 				goto delete_ilm;
16097 
16098 			new_ilm = ilm_lookup_ipif(ipif,
16099 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16100 			if (new_ilm != NULL) {
16101 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16102 				/*
16103 				 * We still need to deal with the from_ill.
16104 				 */
16105 				new_ilm->ilm_is_new = B_TRUE;
16106 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16107 				CLEAR_SLIST(new_ilm->ilm_filter);
16108 				goto delete_ilm;
16109 			}
16110 			/*
16111 			 * If we could not find one e.g. ipif is
16112 			 * still down on to_ill, we add this ilm
16113 			 * on ill_new to preserve the reference
16114 			 * count.
16115 			 */
16116 		}
16117 		/*
16118 		 * When ipifs move, ilms always move with it
16119 		 * to the NEW ill. Thus we should never be
16120 		 * able to find ilm till we really move it here.
16121 		 */
16122 		ASSERT(ilm_lookup_ipif(ipif,
16123 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16124 
16125 		if (from_ill->ill_ilm_walker_cnt != 0) {
16126 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16127 			if (new_ilm == NULL) {
16128 				char buf[INET6_ADDRSTRLEN];
16129 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16130 				    " multicast address %s : "
16131 				    "from %s to"
16132 				    " %s failed : ENOMEM \n",
16133 				    inet_ntop(AF_INET,
16134 				    &ilm->ilm_v6addr, buf,
16135 				    sizeof (buf)),
16136 				    from_ill->ill_name,
16137 				    to_ill->ill_name));
16138 
16139 				ilmp = &ilm->ilm_next;
16140 				continue;
16141 			}
16142 			*new_ilm = *ilm;
16143 			/* We don't want new_ilm linked to ilm's filter list */
16144 			new_ilm->ilm_filter = NULL;
16145 		} else {
16146 			/* Remove from the list */
16147 			*ilmp = ilm->ilm_next;
16148 			new_ilm = ilm;
16149 		}
16150 
16151 		/*
16152 		 * If we have never joined this group on the to_ill
16153 		 * make sure we tell the driver.
16154 		 */
16155 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16156 		    ALL_ZONES) == NULL)
16157 			new_ilm->ilm_notify_driver = B_TRUE;
16158 
16159 		/* Add to the to_ill's list */
16160 		new_ilm->ilm_next = to_ill->ill_ilm;
16161 		to_ill->ill_ilm = new_ilm;
16162 		new_ilm->ilm_is_new = B_TRUE;
16163 
16164 		/*
16165 		 * Revert multicast filter state to (EXCLUDE, NULL)
16166 		 */
16167 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16168 		CLEAR_SLIST(new_ilm->ilm_filter);
16169 
16170 		/*
16171 		 * Delete only if we have allocated a new ilm.
16172 		 */
16173 		if (new_ilm != ilm) {
16174 delete_ilm:
16175 			if (from_ill->ill_ilm_walker_cnt == 0) {
16176 				/* Remove from the list */
16177 				*ilmp = ilm->ilm_next;
16178 				ilm->ilm_next = NULL;
16179 				FREE_SLIST(ilm->ilm_filter);
16180 				FREE_SLIST(ilm->ilm_pendsrcs);
16181 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16182 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16183 				mi_free((char *)ilm);
16184 			} else {
16185 				ilm->ilm_flags |= ILM_DELETED;
16186 				from_ill->ill_ilm_cleanup_reqd = 1;
16187 				ilmp = &ilm->ilm_next;
16188 			}
16189 		}
16190 	}
16191 }
16192 
16193 static uint_t
16194 ipif_get_id(ill_t *ill, uint_t id)
16195 {
16196 	uint_t	unit;
16197 	ipif_t	*tipif;
16198 	boolean_t found = B_FALSE;
16199 
16200 	/*
16201 	 * During failback, we want to go back to the same id
16202 	 * instead of the smallest id so that the original
16203 	 * configuration is maintained. id is non-zero in that
16204 	 * case.
16205 	 */
16206 	if (id != 0) {
16207 		/*
16208 		 * While failing back, if we still have an ipif with
16209 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16210 		 * as soon as we return from this function. It was
16211 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16212 		 * we can choose the smallest id. Thus we return zero
16213 		 * in that case ignoring the hint.
16214 		 */
16215 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16216 			return (0);
16217 		for (tipif = ill->ill_ipif; tipif != NULL;
16218 		    tipif = tipif->ipif_next) {
16219 			if (tipif->ipif_id == id) {
16220 				found = B_TRUE;
16221 				break;
16222 			}
16223 		}
16224 		/*
16225 		 * If somebody already plumbed another logical
16226 		 * with the same id, we won't be able to find it.
16227 		 */
16228 		if (!found)
16229 			return (id);
16230 	}
16231 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16232 		found = B_FALSE;
16233 		for (tipif = ill->ill_ipif; tipif != NULL;
16234 		    tipif = tipif->ipif_next) {
16235 			if (tipif->ipif_id == unit) {
16236 				found = B_TRUE;
16237 				break;
16238 			}
16239 		}
16240 		if (!found)
16241 			break;
16242 	}
16243 	return (unit);
16244 }
16245 
16246 /* ARGSUSED */
16247 static int
16248 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16249     ipif_t **rep_ipif_ptr)
16250 {
16251 	ill_t	*from_ill;
16252 	ipif_t	*rep_ipif;
16253 	ipif_t	**ipifp;
16254 	uint_t	unit;
16255 	int err = 0;
16256 	ipif_t	*to_ipif;
16257 	struct iocblk	*iocp;
16258 	boolean_t failback_cmd;
16259 	boolean_t remove_ipif;
16260 	int	rc;
16261 
16262 	ASSERT(IAM_WRITER_ILL(to_ill));
16263 	ASSERT(IAM_WRITER_IPIF(ipif));
16264 
16265 	iocp = (struct iocblk *)mp->b_rptr;
16266 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16267 	remove_ipif = B_FALSE;
16268 
16269 	from_ill = ipif->ipif_ill;
16270 
16271 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16272 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16273 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16274 
16275 	/*
16276 	 * Don't move LINK LOCAL addresses as they are tied to
16277 	 * physical interface.
16278 	 */
16279 	if (from_ill->ill_isv6 &&
16280 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16281 		ipif->ipif_was_up = B_FALSE;
16282 		IPIF_UNMARK_MOVING(ipif);
16283 		return (0);
16284 	}
16285 
16286 	/*
16287 	 * We set the ipif_id to maximum so that the search for
16288 	 * ipif_id will pick the lowest number i.e 0 in the
16289 	 * following 2 cases :
16290 	 *
16291 	 * 1) We have a replacement ipif at the head of to_ill.
16292 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16293 	 *    on to_ill and hence the MOVE might fail. We want to
16294 	 *    remove it only if we could move the ipif. Thus, by
16295 	 *    setting it to the MAX value, we make the search in
16296 	 *    ipif_get_id return the zeroth id.
16297 	 *
16298 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16299 	 *    we might just have a zero address plumbed on the ipif
16300 	 *    with zero id in the case of IPv4. We remove that while
16301 	 *    doing the failback. We want to remove it only if we
16302 	 *    could move the ipif. Thus, by setting it to the MAX
16303 	 *    value, we make the search in ipif_get_id return the
16304 	 *    zeroth id.
16305 	 *
16306 	 * Both (1) and (2) are done only when when we are moving
16307 	 * an ipif (either due to failover/failback) which originally
16308 	 * belonged to this interface i.e the ipif_orig_ifindex is
16309 	 * the same as to_ill's ifindex. This is needed so that
16310 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16311 	 * from B -> A (B is being removed from the group) and
16312 	 * FAILBACK from A -> B restores the original configuration.
16313 	 * Without the check for orig_ifindex, the second FAILOVER
16314 	 * could make the ipif belonging to B replace the A's zeroth
16315 	 * ipif and the subsequent failback re-creating the replacement
16316 	 * ipif again.
16317 	 *
16318 	 * NOTE : We created the replacement ipif when we did a
16319 	 * FAILOVER (See below). We could check for FAILBACK and
16320 	 * then look for replacement ipif to be removed. But we don't
16321 	 * want to do that because we wan't to allow the possibility
16322 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16323 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16324 	 * from B -> A.
16325 	 */
16326 	to_ipif = to_ill->ill_ipif;
16327 	if ((to_ill->ill_phyint->phyint_ifindex ==
16328 	    ipif->ipif_orig_ifindex) &&
16329 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16330 		ASSERT(to_ipif->ipif_id == 0);
16331 		remove_ipif = B_TRUE;
16332 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16333 	}
16334 	/*
16335 	 * Find the lowest logical unit number on the to_ill.
16336 	 * If we are failing back, try to get the original id
16337 	 * rather than the lowest one so that the original
16338 	 * configuration is maintained.
16339 	 *
16340 	 * XXX need a better scheme for this.
16341 	 */
16342 	if (failback_cmd) {
16343 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16344 	} else {
16345 		unit = ipif_get_id(to_ill, 0);
16346 	}
16347 
16348 	/* Reset back to zero in case we fail below */
16349 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16350 		to_ipif->ipif_id = 0;
16351 
16352 	if (unit == ip_addrs_per_if) {
16353 		ipif->ipif_was_up = B_FALSE;
16354 		IPIF_UNMARK_MOVING(ipif);
16355 		return (EINVAL);
16356 	}
16357 
16358 	/*
16359 	 * ipif is ready to move from "from_ill" to "to_ill".
16360 	 *
16361 	 * 1) If we are moving ipif with id zero, create a
16362 	 *    replacement ipif for this ipif on from_ill. If this fails
16363 	 *    fail the MOVE operation.
16364 	 *
16365 	 * 2) Remove the replacement ipif on to_ill if any.
16366 	 *    We could remove the replacement ipif when we are moving
16367 	 *    the ipif with id zero. But what if somebody already
16368 	 *    unplumbed it ? Thus we always remove it if it is present.
16369 	 *    We want to do it only if we are sure we are going to
16370 	 *    move the ipif to to_ill which is why there are no
16371 	 *    returns due to error till ipif is linked to to_ill.
16372 	 *    Note that the first ipif that we failback will always
16373 	 *    be zero if it is present.
16374 	 */
16375 	if (ipif->ipif_id == 0) {
16376 		ipaddr_t inaddr_any = INADDR_ANY;
16377 
16378 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16379 		if (rep_ipif == NULL) {
16380 			ipif->ipif_was_up = B_FALSE;
16381 			IPIF_UNMARK_MOVING(ipif);
16382 			return (ENOMEM);
16383 		}
16384 		*rep_ipif = ipif_zero;
16385 		/*
16386 		 * Before we put the ipif on the list, store the addresses
16387 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16388 		 * assumes so. This logic is not any different from what
16389 		 * ipif_allocate does.
16390 		 */
16391 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16392 		    &rep_ipif->ipif_v6lcl_addr);
16393 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16394 		    &rep_ipif->ipif_v6src_addr);
16395 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16396 		    &rep_ipif->ipif_v6subnet);
16397 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16398 		    &rep_ipif->ipif_v6net_mask);
16399 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16400 		    &rep_ipif->ipif_v6brd_addr);
16401 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16402 		    &rep_ipif->ipif_v6pp_dst_addr);
16403 		/*
16404 		 * We mark IPIF_NOFAILOVER so that this can never
16405 		 * move.
16406 		 */
16407 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16408 		rep_ipif->ipif_flags &= ~IPIF_UP;
16409 		rep_ipif->ipif_replace_zero = B_TRUE;
16410 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16411 		    MUTEX_DEFAULT, NULL);
16412 		rep_ipif->ipif_id = 0;
16413 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16414 		rep_ipif->ipif_ill = from_ill;
16415 		rep_ipif->ipif_orig_ifindex =
16416 		    from_ill->ill_phyint->phyint_ifindex;
16417 		/* Insert at head */
16418 		rep_ipif->ipif_next = from_ill->ill_ipif;
16419 		from_ill->ill_ipif = rep_ipif;
16420 		/*
16421 		 * We don't really care to let apps know about
16422 		 * this interface.
16423 		 */
16424 	}
16425 
16426 	if (remove_ipif) {
16427 		/*
16428 		 * We set to a max value above for this case to get
16429 		 * id zero. ASSERT that we did get one.
16430 		 */
16431 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16432 		rep_ipif = to_ipif;
16433 		to_ill->ill_ipif = rep_ipif->ipif_next;
16434 		rep_ipif->ipif_next = NULL;
16435 		/*
16436 		 * If some apps scanned and find this interface,
16437 		 * it is time to let them know, so that they can
16438 		 * delete it.
16439 		 */
16440 
16441 		*rep_ipif_ptr = rep_ipif;
16442 	}
16443 
16444 	/* Get it out of the ILL interface list. */
16445 	ipifp = &ipif->ipif_ill->ill_ipif;
16446 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16447 		if (*ipifp == ipif) {
16448 			*ipifp = ipif->ipif_next;
16449 			break;
16450 		}
16451 	}
16452 
16453 	/* Assign the new ill */
16454 	ipif->ipif_ill = to_ill;
16455 	ipif->ipif_id = unit;
16456 	/* id has already been checked */
16457 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16458 	ASSERT(rc == 0);
16459 	/* Let SCTP update its list */
16460 	sctp_move_ipif(ipif, from_ill, to_ill);
16461 	/*
16462 	 * Handle the failover and failback of ipif_t between
16463 	 * ill_t that have differing maximum mtu values.
16464 	 */
16465 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16466 		if (ipif->ipif_saved_mtu == 0) {
16467 			/*
16468 			 * As this ipif_t is moving to an ill_t
16469 			 * that has a lower ill_max_mtu, its
16470 			 * ipif_mtu needs to be saved so it can
16471 			 * be restored during failback or during
16472 			 * failover to an ill_t which has a
16473 			 * higher ill_max_mtu.
16474 			 */
16475 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16476 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16477 		} else {
16478 			/*
16479 			 * The ipif_t is, once again, moving to
16480 			 * an ill_t that has a lower maximum mtu
16481 			 * value.
16482 			 */
16483 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16484 		}
16485 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16486 	    ipif->ipif_saved_mtu != 0) {
16487 		/*
16488 		 * The mtu of this ipif_t had to be reduced
16489 		 * during an earlier failover; this is an
16490 		 * opportunity for it to be increased (either as
16491 		 * part of another failover or a failback).
16492 		 */
16493 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16494 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16495 			ipif->ipif_saved_mtu = 0;
16496 		} else {
16497 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16498 		}
16499 	}
16500 
16501 	/*
16502 	 * We preserve all the other fields of the ipif including
16503 	 * ipif_saved_ire_mp. The routes that are saved here will
16504 	 * be recreated on the new interface and back on the old
16505 	 * interface when we move back.
16506 	 */
16507 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16508 
16509 	return (err);
16510 }
16511 
16512 static int
16513 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16514     int ifindex, ipif_t **rep_ipif_ptr)
16515 {
16516 	ipif_t *mipif;
16517 	ipif_t *ipif_next;
16518 	int err;
16519 
16520 	/*
16521 	 * We don't really try to MOVE back things if some of the
16522 	 * operations fail. The daemon will take care of moving again
16523 	 * later on.
16524 	 */
16525 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16526 		ipif_next = mipif->ipif_next;
16527 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16528 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16529 
16530 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16531 
16532 			/*
16533 			 * When the MOVE fails, it is the job of the
16534 			 * application to take care of this properly
16535 			 * i.e try again if it is ENOMEM.
16536 			 */
16537 			if (mipif->ipif_ill != from_ill) {
16538 				/*
16539 				 * ipif has moved.
16540 				 *
16541 				 * Move the multicast memberships associated
16542 				 * with this ipif to the new ill. For IPv6, we
16543 				 * do it once after all the ipifs are moved
16544 				 * (in ill_move) as they are not associated
16545 				 * with ipifs.
16546 				 *
16547 				 * We need to move the ilms as the ipif has
16548 				 * already been moved to a new ill even
16549 				 * in the case of errors. Neither
16550 				 * ilm_free(ipif) will find the ilm
16551 				 * when somebody unplumbs this ipif nor
16552 				 * ilm_delete(ilm) will be able to find the
16553 				 * ilm, if we don't move now.
16554 				 */
16555 				if (!from_ill->ill_isv6)
16556 					ilm_move_v4(from_ill, to_ill, mipif);
16557 			}
16558 
16559 			if (err != 0)
16560 				return (err);
16561 		}
16562 	}
16563 	return (0);
16564 }
16565 
16566 static int
16567 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16568 {
16569 	int ifindex;
16570 	int err;
16571 	struct iocblk	*iocp;
16572 	ipif_t	*ipif;
16573 	ipif_t *rep_ipif_ptr = NULL;
16574 	ipif_t	*from_ipif = NULL;
16575 	boolean_t check_rep_if = B_FALSE;
16576 
16577 	iocp = (struct iocblk *)mp->b_rptr;
16578 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16579 		/*
16580 		 * Move everything pointing at from_ill to to_ill.
16581 		 * We acheive this by passing in 0 as ifindex.
16582 		 */
16583 		ifindex = 0;
16584 	} else {
16585 		/*
16586 		 * Move everything pointing at from_ill whose original
16587 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16588 		 * We acheive this by passing in ifindex rather than 0.
16589 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16590 		 */
16591 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16592 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16593 	}
16594 
16595 	/*
16596 	 * Determine if there is at least one ipif that would move from
16597 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16598 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16599 	 * the move, in which case we need to quiesce the replacement ipif also.
16600 	 */
16601 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16602 	    from_ipif = from_ipif->ipif_next) {
16603 		if (((ifindex == 0) ||
16604 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16605 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16606 			check_rep_if = B_TRUE;
16607 			break;
16608 		}
16609 	}
16610 
16611 
16612 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16613 
16614 	GRAB_ILL_LOCKS(from_ill, to_ill);
16615 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16616 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16617 		    mp, ILL_MOVE_OK);
16618 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16619 		return (EINPROGRESS);
16620 	}
16621 
16622 	/* Check if the replacement ipif is quiescent to delete */
16623 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16624 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16625 		to_ill->ill_ipif->ipif_state_flags |=
16626 		    IPIF_MOVING | IPIF_CHANGING;
16627 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16628 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16629 			    mp, ILL_MOVE_OK);
16630 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16631 			return (EINPROGRESS);
16632 		}
16633 	}
16634 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16635 
16636 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16637 	rw_enter(&ill_g_lock, RW_WRITER);
16638 	GRAB_ILL_LOCKS(from_ill, to_ill);
16639 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16640 
16641 	/* ilm_move is done inside ipif_move for IPv4 */
16642 	if (err == 0 && from_ill->ill_isv6)
16643 		ilm_move_v6(from_ill, to_ill, ifindex);
16644 
16645 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16646 	rw_exit(&ill_g_lock);
16647 
16648 	/*
16649 	 * send rts messages and multicast messages.
16650 	 */
16651 	if (rep_ipif_ptr != NULL) {
16652 		ip_rts_ifmsg(rep_ipif_ptr);
16653 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16654 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16655 		mi_free(rep_ipif_ptr);
16656 	}
16657 
16658 	ilm_send_multicast_reqs(from_ill, to_ill);
16659 
16660 	conn_move_ill(from_ill, to_ill, ifindex);
16661 
16662 	return (err);
16663 }
16664 
16665 /*
16666  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16667  * Also checks for the validity of the arguments.
16668  * Note: We are already exclusive inside the from group.
16669  * It is upto the caller to release refcnt on the to_ill's.
16670  */
16671 static int
16672 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16673     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16674 {
16675 	int dst_index;
16676 	ipif_t *ipif_v4, *ipif_v6;
16677 	struct lifreq *lifr;
16678 	mblk_t *mp1;
16679 	boolean_t exists;
16680 	sin_t	*sin;
16681 	int	err = 0;
16682 
16683 	if ((mp1 = mp->b_cont) == NULL)
16684 		return (EPROTO);
16685 
16686 	if ((mp1 = mp1->b_cont) == NULL)
16687 		return (EPROTO);
16688 
16689 	lifr = (struct lifreq *)mp1->b_rptr;
16690 	sin = (sin_t *)&lifr->lifr_addr;
16691 
16692 	/*
16693 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16694 	 * specific operations.
16695 	 */
16696 	if (sin->sin_family != AF_UNSPEC)
16697 		return (EINVAL);
16698 
16699 	/*
16700 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16701 	 * NULLs for the last 4 args and we know the lookup won't fail
16702 	 * with EINPROGRESS.
16703 	 */
16704 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16705 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16706 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16707 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16708 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16709 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16710 
16711 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16712 		return (ENXIO);
16713 
16714 	if (ipif_v4 != NULL) {
16715 		ASSERT(ipif_v4->ipif_refcnt != 0);
16716 		if (ipif_v4->ipif_id != 0) {
16717 			err = EINVAL;
16718 			goto done;
16719 		}
16720 
16721 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16722 		*ill_from_v4 = ipif_v4->ipif_ill;
16723 	}
16724 
16725 	if (ipif_v6 != NULL) {
16726 		ASSERT(ipif_v6->ipif_refcnt != 0);
16727 		if (ipif_v6->ipif_id != 0) {
16728 			err = EINVAL;
16729 			goto done;
16730 		}
16731 
16732 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16733 		*ill_from_v6 = ipif_v6->ipif_ill;
16734 	}
16735 
16736 	err = 0;
16737 	dst_index = lifr->lifr_movetoindex;
16738 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16739 	    q, mp, ip_process_ioctl, &err);
16740 	if (err != 0) {
16741 		/*
16742 		 * There could be only v6.
16743 		 */
16744 		if (err != ENXIO)
16745 			goto done;
16746 		err = 0;
16747 	}
16748 
16749 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16750 	    q, mp, ip_process_ioctl, &err);
16751 	if (err != 0) {
16752 		if (err != ENXIO)
16753 			goto done;
16754 		if (*ill_to_v4 == NULL) {
16755 			err = ENXIO;
16756 			goto done;
16757 		}
16758 		err = 0;
16759 	}
16760 
16761 	/*
16762 	 * If we have something to MOVE i.e "from" not NULL,
16763 	 * "to" should be non-NULL.
16764 	 */
16765 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16766 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16767 		err = EINVAL;
16768 	}
16769 
16770 done:
16771 	if (ipif_v4 != NULL)
16772 		ipif_refrele(ipif_v4);
16773 	if (ipif_v6 != NULL)
16774 		ipif_refrele(ipif_v6);
16775 	return (err);
16776 }
16777 
16778 /*
16779  * FAILOVER and FAILBACK are modelled as MOVE operations.
16780  *
16781  * We don't check whether the MOVE is within the same group or
16782  * not, because this ioctl can be used as a generic mechanism
16783  * to failover from interface A to B, though things will function
16784  * only if they are really part of the same group. Moreover,
16785  * all ipifs may be down and hence temporarily out of the group.
16786  *
16787  * ipif's that need to be moved are first brought down; V4 ipifs are brought
16788  * down first and then V6.  For each we wait for the ipif's to become quiescent.
16789  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
16790  * have been deleted and there are no active references. Once quiescent the
16791  * ipif's are moved and brought up on the new ill.
16792  *
16793  * Normally the source ill and destination ill belong to the same IPMP group
16794  * and hence the same ipsq_t. In the event they don't belong to the same
16795  * same group the two ipsq's are first merged into one ipsq - that of the
16796  * to_ill. The multicast memberships on the source and destination ill cannot
16797  * change during the move operation since multicast joins/leaves also have to
16798  * execute on the same ipsq and are hence serialized.
16799  */
16800 /* ARGSUSED */
16801 int
16802 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16803     ip_ioctl_cmd_t *ipip, void *ifreq)
16804 {
16805 	ill_t *ill_to_v4 = NULL;
16806 	ill_t *ill_to_v6 = NULL;
16807 	ill_t *ill_from_v4 = NULL;
16808 	ill_t *ill_from_v6 = NULL;
16809 	int err = 0;
16810 
16811 	/*
16812 	 * setup from and to ill's, we can get EINPROGRESS only for
16813 	 * to_ill's.
16814 	 */
16815 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
16816 	    &ill_to_v4, &ill_to_v6);
16817 
16818 	if (err != 0) {
16819 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
16820 		goto done;
16821 	}
16822 
16823 	/*
16824 	 * nothing to do.
16825 	 */
16826 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
16827 		goto done;
16828 	}
16829 
16830 	/*
16831 	 * nothing to do.
16832 	 */
16833 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
16834 		goto done;
16835 	}
16836 
16837 	/*
16838 	 * Mark the ill as changing.
16839 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
16840 	 * in ill_up_ipifs in case of error they are cleared below.
16841 	 */
16842 
16843 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16844 	if (ill_from_v4 != NULL)
16845 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
16846 	if (ill_from_v6 != NULL)
16847 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
16848 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16849 
16850 	/*
16851 	 * Make sure that both src and dst are
16852 	 * in the same syncq group. If not make it happen.
16853 	 * We are not holding any locks because we are the writer
16854 	 * on the from_ipsq and we will hold locks in ill_merge_groups
16855 	 * to protect to_ipsq against changing.
16856 	 */
16857 	if (ill_from_v4 != NULL) {
16858 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
16859 		    ill_to_v4->ill_phyint->phyint_ipsq) {
16860 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
16861 			    NULL, mp, q);
16862 			goto err_ret;
16863 
16864 		}
16865 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
16866 	} else {
16867 
16868 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
16869 		    ill_to_v6->ill_phyint->phyint_ipsq) {
16870 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
16871 			    NULL, mp, q);
16872 			goto err_ret;
16873 
16874 		}
16875 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
16876 	}
16877 
16878 	/*
16879 	 * Now that the ipsq's have been merged and we are the writer
16880 	 * lets mark to_ill as changing as well.
16881 	 */
16882 
16883 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16884 	if (ill_to_v4 != NULL)
16885 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
16886 	if (ill_to_v6 != NULL)
16887 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
16888 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16889 
16890 	/*
16891 	 * Its ok for us to proceed with the move even if
16892 	 * ill_pending_mp is non null on one of the from ill's as the reply
16893 	 * should not be looking at the ipif, it should only care about the
16894 	 * ill itself.
16895 	 */
16896 
16897 	/*
16898 	 * lets move ipv4 first.
16899 	 */
16900 	if (ill_from_v4 != NULL) {
16901 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
16902 		ill_from_v4->ill_move_in_progress = B_TRUE;
16903 		ill_to_v4->ill_move_in_progress = B_TRUE;
16904 		ill_to_v4->ill_move_peer = ill_from_v4;
16905 		ill_from_v4->ill_move_peer = ill_to_v4;
16906 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
16907 	}
16908 
16909 	/*
16910 	 * Now lets move ipv6.
16911 	 */
16912 	if (err == 0 && ill_from_v6 != NULL) {
16913 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
16914 		ill_from_v6->ill_move_in_progress = B_TRUE;
16915 		ill_to_v6->ill_move_in_progress = B_TRUE;
16916 		ill_to_v6->ill_move_peer = ill_from_v6;
16917 		ill_from_v6->ill_move_peer = ill_to_v6;
16918 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
16919 	}
16920 
16921 err_ret:
16922 	/*
16923 	 * EINPROGRESS means we are waiting for the ipif's that need to be
16924 	 * moved to become quiescent.
16925 	 */
16926 	if (err == EINPROGRESS) {
16927 		goto done;
16928 	}
16929 
16930 	/*
16931 	 * if err is set ill_up_ipifs will not be called
16932 	 * lets clear the flags.
16933 	 */
16934 
16935 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16936 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16937 	/*
16938 	 * Some of the clearing may be redundant. But it is simple
16939 	 * not making any extra checks.
16940 	 */
16941 	if (ill_from_v6 != NULL) {
16942 		ill_from_v6->ill_move_in_progress = B_FALSE;
16943 		ill_from_v6->ill_move_peer = NULL;
16944 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
16945 	}
16946 	if (ill_from_v4 != NULL) {
16947 		ill_from_v4->ill_move_in_progress = B_FALSE;
16948 		ill_from_v4->ill_move_peer = NULL;
16949 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
16950 	}
16951 	if (ill_to_v6 != NULL) {
16952 		ill_to_v6->ill_move_in_progress = B_FALSE;
16953 		ill_to_v6->ill_move_peer = NULL;
16954 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
16955 	}
16956 	if (ill_to_v4 != NULL) {
16957 		ill_to_v4->ill_move_in_progress = B_FALSE;
16958 		ill_to_v4->ill_move_peer = NULL;
16959 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
16960 	}
16961 
16962 	/*
16963 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
16964 	 * Do this always to maintain proper state i.e even in case of errors.
16965 	 * As phyint_inactive looks at both v4 and v6 interfaces,
16966 	 * we need not call on both v4 and v6 interfaces.
16967 	 */
16968 	if (ill_from_v4 != NULL) {
16969 		if ((ill_from_v4->ill_phyint->phyint_flags &
16970 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
16971 			phyint_inactive(ill_from_v4->ill_phyint);
16972 		}
16973 	} else if (ill_from_v6 != NULL) {
16974 		if ((ill_from_v6->ill_phyint->phyint_flags &
16975 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
16976 			phyint_inactive(ill_from_v6->ill_phyint);
16977 		}
16978 	}
16979 
16980 	if (ill_to_v4 != NULL) {
16981 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
16982 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
16983 		}
16984 	} else if (ill_to_v6 != NULL) {
16985 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
16986 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
16987 		}
16988 	}
16989 
16990 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16991 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16992 
16993 no_err:
16994 	/*
16995 	 * lets bring the interfaces up on the to_ill.
16996 	 */
16997 	if (err == 0) {
16998 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
16999 		    q, mp);
17000 	}
17001 done:
17002 
17003 	if (ill_to_v4 != NULL) {
17004 		ill_refrele(ill_to_v4);
17005 	}
17006 	if (ill_to_v6 != NULL) {
17007 		ill_refrele(ill_to_v6);
17008 	}
17009 
17010 	return (err);
17011 }
17012 
17013 static void
17014 ill_dl_down(ill_t *ill)
17015 {
17016 	/*
17017 	 * The ill is down; unbind but stay attached since we're still
17018 	 * associated with a PPA.
17019 	 */
17020 	mblk_t	*mp = ill->ill_unbind_mp;
17021 
17022 	ill->ill_unbind_mp = NULL;
17023 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
17024 	if (mp != NULL) {
17025 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
17026 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
17027 		    ill->ill_name));
17028 		ill_dlpi_send(ill, mp);
17029 	}
17030 
17031 	/*
17032 	 * Toss all of our multicast memberships.  We could keep them, but
17033 	 * then we'd have to do bookkeeping of any joins and leaves performed
17034 	 * by the application while the the interface is down (we can't just
17035 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
17036 	 * on a downed interface).
17037 	 */
17038 	ill_leave_multicast(ill);
17039 
17040 	mutex_enter(&ill->ill_lock);
17041 	ill->ill_dl_up = 0;
17042 	mutex_exit(&ill->ill_lock);
17043 }
17044 
17045 void
17046 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
17047 {
17048 	union DL_primitives *dlp;
17049 	t_uscalar_t prim;
17050 
17051 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17052 
17053 	dlp = (union DL_primitives *)mp->b_rptr;
17054 	prim = dlp->dl_primitive;
17055 
17056 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
17057 		dlpi_prim_str(prim), prim, ill->ill_name));
17058 
17059 	switch (prim) {
17060 	case DL_PHYS_ADDR_REQ:
17061 	{
17062 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
17063 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
17064 		break;
17065 	}
17066 	case DL_BIND_REQ:
17067 		mutex_enter(&ill->ill_lock);
17068 		ill->ill_state_flags &= ~ILL_DL_UNBIND_DONE;
17069 		mutex_exit(&ill->ill_lock);
17070 		break;
17071 	}
17072 
17073 	ill->ill_dlpi_pending = prim;
17074 
17075 	/*
17076 	 * Some drivers send M_FLUSH up to IP as part of unbind
17077 	 * request.  When this M_FLUSH is sent back to the driver,
17078 	 * this can go after we send the detach request if the
17079 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17080 	 * to the M_FLUSH in ip_rput and locally generate another
17081 	 * M_FLUSH for the correctness.  This will get freed in
17082 	 * ip_wput_nondata.
17083 	 */
17084 	if (prim == DL_UNBIND_REQ)
17085 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17086 
17087 	putnext(ill->ill_wq, mp);
17088 }
17089 
17090 /*
17091  * Send a DLPI control message to the driver but make sure there
17092  * is only one outstanding message. Uses ill_dlpi_pending to tell
17093  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17094  * when an ACK or a NAK is received to process the next queued message.
17095  *
17096  * We don't protect ill_dlpi_pending with any lock. This is okay as
17097  * every place where its accessed, ip is exclusive while accessing
17098  * ill_dlpi_pending except when this function is called from ill_init()
17099  */
17100 void
17101 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17102 {
17103 	mblk_t **mpp;
17104 
17105 	ASSERT(IAM_WRITER_ILL(ill));
17106 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17107 
17108 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17109 		/* Must queue message. Tail insertion */
17110 		mpp = &ill->ill_dlpi_deferred;
17111 		while (*mpp != NULL)
17112 			mpp = &((*mpp)->b_next);
17113 
17114 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17115 		    ill->ill_name));
17116 
17117 		*mpp = mp;
17118 		return;
17119 	}
17120 
17121 	ill_dlpi_dispatch(ill, mp);
17122 }
17123 
17124 /*
17125  * Called when an DLPI control message has been acked or nacked to
17126  * send down the next queued message (if any).
17127  */
17128 void
17129 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17130 {
17131 	mblk_t *mp;
17132 
17133 	ASSERT(IAM_WRITER_ILL(ill));
17134 
17135 	ASSERT(prim != DL_PRIM_INVAL);
17136 	if (ill->ill_dlpi_pending != prim) {
17137 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17138 			(void) mi_strlog(ill->ill_rq, 1,
17139 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17140 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17141 			    dlpi_prim_str(prim), ill->ill_name);
17142 		} else {
17143 			(void) mi_strlog(ill->ill_rq, 1,
17144 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17145 			    "ill_dlpi_done: unexpected ack for %s from %s "
17146 			    "(expecting ack for %s)\n",
17147 			    dlpi_prim_str(prim), ill->ill_name,
17148 			    dlpi_prim_str(ill->ill_dlpi_pending));
17149 		}
17150 		return;
17151 	}
17152 
17153 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17154 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17155 
17156 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17157 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17158 		return;
17159 	}
17160 
17161 	ill->ill_dlpi_deferred = mp->b_next;
17162 	mp->b_next = NULL;
17163 
17164 	ill_dlpi_dispatch(ill, mp);
17165 }
17166 
17167 void
17168 conn_delete_ire(conn_t *connp, caddr_t arg)
17169 {
17170 	ipif_t	*ipif = (ipif_t *)arg;
17171 	ire_t	*ire;
17172 
17173 	/*
17174 	 * Look at the cached ires on conns which has pointers to ipifs.
17175 	 * We just call ire_refrele which clears up the reference
17176 	 * to ire. Called when a conn closes. Also called from ipif_free
17177 	 * to cleanup indirect references to the stale ipif via the cached ire.
17178 	 */
17179 	mutex_enter(&connp->conn_lock);
17180 	ire = connp->conn_ire_cache;
17181 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17182 		connp->conn_ire_cache = NULL;
17183 		mutex_exit(&connp->conn_lock);
17184 		IRE_REFRELE_NOTR(ire);
17185 		return;
17186 	}
17187 	mutex_exit(&connp->conn_lock);
17188 
17189 }
17190 
17191 /*
17192  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17193  * of IREs. Those IREs may have been previously cached in the conn structure.
17194  * This ipcl_walk() walker function releases all references to such IREs based
17195  * on the condemned flag.
17196  */
17197 /* ARGSUSED */
17198 void
17199 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17200 {
17201 	ire_t	*ire;
17202 
17203 	mutex_enter(&connp->conn_lock);
17204 	ire = connp->conn_ire_cache;
17205 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17206 		connp->conn_ire_cache = NULL;
17207 		mutex_exit(&connp->conn_lock);
17208 		IRE_REFRELE_NOTR(ire);
17209 		return;
17210 	}
17211 	mutex_exit(&connp->conn_lock);
17212 }
17213 
17214 /*
17215  * Take down a specific interface, but don't lose any information about it.
17216  * Also delete interface from its interface group (ifgrp).
17217  * (Always called as writer.)
17218  * This function goes through the down sequence even if the interface is
17219  * already down. There are 2 reasons.
17220  * a. Currently we permit interface routes that depend on down interfaces
17221  *    to be added. This behaviour itself is questionable. However it appears
17222  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17223  *    time. We go thru the cleanup in order to remove these routes.
17224  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17225  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17226  *    down, but we need to cleanup i.e. do ill_dl_down and
17227  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17228  *
17229  * IP-MT notes:
17230  *
17231  * Model of reference to interfaces.
17232  *
17233  * The following members in ipif_t track references to the ipif.
17234  *	int     ipif_refcnt;    Active reference count
17235  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17236  * The following members in ill_t track references to the ill.
17237  *	int             ill_refcnt;     active refcnt
17238  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17239  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17240  *
17241  * Reference to an ipif or ill can be obtained in any of the following ways.
17242  *
17243  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17244  * Pointers to ipif / ill from other data structures viz ire and conn.
17245  * Implicit reference to the ipif / ill by holding a reference to the ire.
17246  *
17247  * The ipif/ill lookup functions return a reference held ipif / ill.
17248  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17249  * This is a purely dynamic reference count associated with threads holding
17250  * references to the ipif / ill. Pointers from other structures do not
17251  * count towards this reference count.
17252  *
17253  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17254  * ipif/ill. This is incremented whenever a new ire is created referencing the
17255  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17256  * actually added to the ire hash table. The count is decremented in
17257  * ire_inactive where the ire is destroyed.
17258  *
17259  * nce's reference ill's thru nce_ill and the count of nce's associated with
17260  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17261  * ndp_add() where the nce is actually added to the table. Similarly it is
17262  * decremented in ndp_inactive where the nce is destroyed.
17263  *
17264  * Flow of ioctls involving interface down/up
17265  *
17266  * The following is the sequence of an attempt to set some critical flags on an
17267  * up interface.
17268  * ip_sioctl_flags
17269  * ipif_down
17270  * wait for ipif to be quiescent
17271  * ipif_down_tail
17272  * ip_sioctl_flags_tail
17273  *
17274  * All set ioctls that involve down/up sequence would have a skeleton similar
17275  * to the above. All the *tail functions are called after the refcounts have
17276  * dropped to the appropriate values.
17277  *
17278  * The mechanism to quiesce an ipif is as follows.
17279  *
17280  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17281  * on the ipif. Callers either pass a flag requesting wait or the lookup
17282  *  functions will return NULL.
17283  *
17284  * Delete all ires referencing this ipif
17285  *
17286  * Any thread attempting to do an ipif_refhold on an ipif that has been
17287  * obtained thru a cached pointer will first make sure that
17288  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17289  * increment the refcount.
17290  *
17291  * The above guarantees that the ipif refcount will eventually come down to
17292  * zero and the ipif will quiesce, once all threads that currently hold a
17293  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17294  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17295  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17296  * drop to zero.
17297  *
17298  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17299  *
17300  * Threads trying to lookup an ipif or ill can pass a flag requesting
17301  * wait and restart if the ipif / ill cannot be looked up currently.
17302  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17303  * failure if the ipif is currently undergoing an exclusive operation, and
17304  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17305  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17306  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17307  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17308  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17309  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17310  * until we release the ipsq_lock, even though the the ill/ipif state flags
17311  * can change after we drop the ill_lock.
17312  *
17313  * An attempt to send out a packet using an ipif that is currently
17314  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17315  * operation and restart it later when the exclusive condition on the ipif ends.
17316  * This is an example of not passing the wait flag to the lookup functions. For
17317  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17318  * out a multicast packet on that ipif will fail while the ipif is
17319  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17320  * currently IPIF_CHANGING will also fail.
17321  */
17322 int
17323 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17324 {
17325 	ill_t		*ill = ipif->ipif_ill;
17326 	phyint_t	*phyi;
17327 	conn_t		*connp;
17328 	boolean_t	success;
17329 	boolean_t	ipif_was_up = B_FALSE;
17330 
17331 	ASSERT(IAM_WRITER_IPIF(ipif));
17332 
17333 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17334 
17335 	if (ipif->ipif_flags & IPIF_UP) {
17336 		mutex_enter(&ill->ill_lock);
17337 		ipif->ipif_flags &= ~IPIF_UP;
17338 		ASSERT(ill->ill_ipif_up_count > 0);
17339 		--ill->ill_ipif_up_count;
17340 		mutex_exit(&ill->ill_lock);
17341 		ipif_was_up = B_TRUE;
17342 		/* Update status in SCTP's list */
17343 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17344 	}
17345 
17346 	/*
17347 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17348 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17349 	 * know not to rejoin when the interface is brought back up).
17350 	 */
17351 	if (ipif->ipif_isv6)
17352 		ipif_multicast_down(ipif);
17353 	/*
17354 	 * Remove from the mapping for __sin6_src_id. We insert only
17355 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17356 	 * stored as mapped addresses, we need to check for mapped
17357 	 * INADDR_ANY also.
17358 	 */
17359 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17360 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17361 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17362 		int err;
17363 
17364 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17365 		    ipif->ipif_zoneid);
17366 		if (err != 0) {
17367 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17368 		}
17369 	}
17370 
17371 	/*
17372 	 * Before we delete the ill from the group (if any), we need
17373 	 * to make sure that we delete all the routes dependent on
17374 	 * this and also any ipifs dependent on this ipif for
17375 	 * source address. We need to do before we delete from
17376 	 * the group because
17377 	 *
17378 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17379 	 *
17380 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17381 	 *    for re-doing source address selection. Note that
17382 	 *    ipif_select_source[_v6] called from
17383 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17384 	 *    because we have already marked down here i.e cleared
17385 	 *    IPIF_UP.
17386 	 */
17387 	if (ipif->ipif_isv6)
17388 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17389 	else
17390 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17391 
17392 	/*
17393 	 * Need to add these also to be saved and restored when the
17394 	 * ipif is brought down and up
17395 	 */
17396 	mutex_enter(&ire_mrtun_lock);
17397 	if (ire_mrtun_count != 0) {
17398 		mutex_exit(&ire_mrtun_lock);
17399 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17400 		    (char *)ipif, NULL);
17401 	} else {
17402 		mutex_exit(&ire_mrtun_lock);
17403 	}
17404 
17405 	mutex_enter(&ire_srcif_table_lock);
17406 	if (ire_srcif_table_count > 0) {
17407 		mutex_exit(&ire_srcif_table_lock);
17408 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17409 	} else {
17410 		mutex_exit(&ire_srcif_table_lock);
17411 	}
17412 
17413 	/*
17414 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17415 	 * ires have been deleted above. Otherwise a thread could end up
17416 	 * caching an ire in a conn after we have finished the cleanup of the
17417 	 * conn. The caching is done after making sure that the ire is not yet
17418 	 * condemned. Also documented in the block comment above ip_output
17419 	 */
17420 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17421 	/* Also, delete the ires cached in SCTP */
17422 	sctp_ire_cache_flush(ipif);
17423 
17424 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17425 	nattymod_clean_ipif(ipif);
17426 
17427 	/*
17428 	 * Update any other ipifs which have used "our" local address as
17429 	 * a source address. This entails removing and recreating IRE_INTERFACE
17430 	 * entries for such ipifs.
17431 	 */
17432 	if (ipif->ipif_isv6)
17433 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17434 	else
17435 		ipif_update_other_ipifs(ipif, ill->ill_group);
17436 
17437 	if (ipif_was_up) {
17438 		/*
17439 		 * Check whether it is last ipif to leave this group.
17440 		 * If this is the last ipif to leave, we should remove
17441 		 * this ill from the group as ipif_select_source will not
17442 		 * be able to find any useful ipifs if this ill is selected
17443 		 * for load balancing.
17444 		 *
17445 		 * For nameless groups, we should call ifgrp_delete if this
17446 		 * belongs to some group. As this ipif is going down, we may
17447 		 * need to reconstruct groups.
17448 		 */
17449 		phyi = ill->ill_phyint;
17450 		/*
17451 		 * If the phyint_groupname_len is 0, it may or may not
17452 		 * be in the nameless group. If the phyint_groupname_len is
17453 		 * not 0, then this ill should be part of some group.
17454 		 * As we always insert this ill in the group if
17455 		 * phyint_groupname_len is not zero when the first ipif
17456 		 * comes up (in ipif_up_done), it should be in a group
17457 		 * when the namelen is not 0.
17458 		 *
17459 		 * NOTE : When we delete the ill from the group,it will
17460 		 * blow away all the IRE_CACHES pointing either at this ipif or
17461 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17462 		 * should be pointing at this ill.
17463 		 */
17464 		ASSERT(phyi->phyint_groupname_len == 0 ||
17465 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17466 
17467 		if (phyi->phyint_groupname_len != 0) {
17468 			if (ill->ill_ipif_up_count == 0)
17469 				illgrp_delete(ill);
17470 		}
17471 
17472 		/*
17473 		 * If we have deleted some of the broadcast ires associated
17474 		 * with this ipif, we need to re-nominate somebody else if
17475 		 * the ires that we deleted were the nominated ones.
17476 		 */
17477 		if (ill->ill_group != NULL && !ill->ill_isv6)
17478 			ipif_renominate_bcast(ipif);
17479 	}
17480 
17481 	if (ipif->ipif_isv6)
17482 		ipif_ndp_down(ipif);
17483 
17484 	/*
17485 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17486 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17487 	 * and ill_delete -> ipif_free -> ipif_down
17488 	 */
17489 	if (mp == NULL) {
17490 		ASSERT(q == NULL);
17491 		return (0);
17492 	}
17493 
17494 	if (CONN_Q(q)) {
17495 		connp = Q_TO_CONN(q);
17496 		mutex_enter(&connp->conn_lock);
17497 	} else {
17498 		connp = NULL;
17499 	}
17500 	mutex_enter(&ill->ill_lock);
17501 	/*
17502 	 * Are there any ire's pointing to this ipif that are still active ?
17503 	 * If this is the last ipif going down, are there any ire's pointing
17504 	 * to this ill that are still active ?
17505 	 */
17506 	if (ipif_is_quiescent(ipif)) {
17507 		mutex_exit(&ill->ill_lock);
17508 		if (connp != NULL)
17509 			mutex_exit(&connp->conn_lock);
17510 		return (0);
17511 	}
17512 
17513 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17514 	    ill->ill_name, (void *)ill));
17515 	/*
17516 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17517 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17518 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17519 	 */
17520 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17521 	if (!success) {
17522 		/* The conn is closing. So just return */
17523 		ASSERT(connp != NULL);
17524 		mutex_exit(&ill->ill_lock);
17525 		mutex_exit(&connp->conn_lock);
17526 		return (EINTR);
17527 	}
17528 
17529 	mutex_exit(&ill->ill_lock);
17530 	if (connp != NULL)
17531 		mutex_exit(&connp->conn_lock);
17532 	return (EINPROGRESS);
17533 }
17534 
17535 static void
17536 ipif_down_tail(ipif_t *ipif)
17537 {
17538 	ill_t	*ill = ipif->ipif_ill;
17539 
17540 	/*
17541 	 * Skip any loopback interface (null wq).
17542 	 * If this is the last logical interface on the ill
17543 	 * have ill_dl_down tell the driver we are gone (unbind)
17544 	 * Note that lun 0 can ipif_down even though
17545 	 * there are other logical units that are up.
17546 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17547 	 */
17548 	if (ipif->ipif_ill->ill_wq != NULL) {
17549 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17550 		    ill->ill_dl_up) {
17551 			ill_dl_down(ill);
17552 		}
17553 	}
17554 	ill->ill_logical_down = 0;
17555 
17556 	/*
17557 	 * Have to be after removing the routes in ipif_down_delete_ire.
17558 	 */
17559 	if (ipif->ipif_isv6) {
17560 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17561 			ipif_arp_down(ipif);
17562 	} else {
17563 		ipif_arp_down(ipif);
17564 	}
17565 
17566 	ip_rts_ifmsg(ipif);
17567 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17568 }
17569 
17570 /*
17571  * Bring interface logically down without bringing the physical interface
17572  * down e.g. when the netmask is changed. This avoids long lasting link
17573  * negotiations between an ethernet interface and a certain switches.
17574  */
17575 static int
17576 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17577 {
17578 	/*
17579 	 * The ill_logical_down flag is a transient flag. It is set here
17580 	 * and is cleared once the down has completed in ipif_down_tail.
17581 	 * This flag does not indicate whether the ill stream is in the
17582 	 * DL_BOUND state with the driver. Instead this flag is used by
17583 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17584 	 * the driver. The state of the ill stream i.e. whether it is
17585 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17586 	 */
17587 	ipif->ipif_ill->ill_logical_down = 1;
17588 	return (ipif_down(ipif, q, mp));
17589 }
17590 
17591 /*
17592  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17593  * If the usesrc client ILL is already part of a usesrc group or not,
17594  * in either case a ire_stq with the matching usesrc client ILL will
17595  * locate the IRE's that need to be deleted. We want IREs to be created
17596  * with the new source address.
17597  */
17598 static void
17599 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17600 {
17601 	ill_t	*ucill = (ill_t *)ill_arg;
17602 
17603 	ASSERT(IAM_WRITER_ILL(ucill));
17604 
17605 	if (ire->ire_stq == NULL)
17606 		return;
17607 
17608 	if ((ire->ire_type == IRE_CACHE) &&
17609 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17610 		ire_delete(ire);
17611 }
17612 
17613 /*
17614  * ire_walk routine to delete every IRE dependent on the interface
17615  * address that is going down.	(Always called as writer.)
17616  * Works for both v4 and v6.
17617  * In addition for checking for ire_ipif matches it also checks for
17618  * IRE_CACHE entries which have the same source address as the
17619  * disappearing ipif since ipif_select_source might have picked
17620  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17621  * care of any IRE_INTERFACE with the disappearing source address.
17622  */
17623 static void
17624 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17625 {
17626 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17627 	ill_t *ire_ill;
17628 	ill_t *ipif_ill;
17629 
17630 	ASSERT(IAM_WRITER_IPIF(ipif));
17631 	if (ire->ire_ipif == NULL)
17632 		return;
17633 
17634 	/*
17635 	 * For IPv4, we derive source addresses for an IRE from ipif's
17636 	 * belonging to the same IPMP group as the IRE's outgoing
17637 	 * interface.  If an IRE's outgoing interface isn't in the
17638 	 * same IPMP group as a particular ipif, then that ipif
17639 	 * couldn't have been used as a source address for this IRE.
17640 	 *
17641 	 * For IPv6, source addresses are only restricted to the IPMP group
17642 	 * if the IRE is for a link-local address or a multicast address.
17643 	 * Otherwise, source addresses for an IRE can be chosen from
17644 	 * interfaces other than the the outgoing interface for that IRE.
17645 	 *
17646 	 * For source address selection details, see ipif_select_source()
17647 	 * and ipif_select_source_v6().
17648 	 */
17649 	if (ire->ire_ipversion == IPV4_VERSION ||
17650 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17651 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17652 		ire_ill = ire->ire_ipif->ipif_ill;
17653 		ipif_ill = ipif->ipif_ill;
17654 
17655 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17656 			return;
17657 		}
17658 	}
17659 
17660 
17661 	if (ire->ire_ipif != ipif) {
17662 		/*
17663 		 * Look for a matching source address.
17664 		 */
17665 		if (ire->ire_type != IRE_CACHE)
17666 			return;
17667 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17668 			return;
17669 
17670 		if (ire->ire_ipversion == IPV4_VERSION) {
17671 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17672 				return;
17673 		} else {
17674 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17675 			    &ipif->ipif_v6lcl_addr))
17676 				return;
17677 		}
17678 		ire_delete(ire);
17679 		return;
17680 	}
17681 	/*
17682 	 * ire_delete() will do an ire_flush_cache which will delete
17683 	 * all ire_ipif matches
17684 	 */
17685 	ire_delete(ire);
17686 }
17687 
17688 /*
17689  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17690  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17691  * 2) when an interface is brought up or down (on that ill).
17692  * This ensures that the IRE_CACHE entries don't retain stale source
17693  * address selection results.
17694  */
17695 void
17696 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17697 {
17698 	ill_t	*ill = (ill_t *)ill_arg;
17699 	ill_t	*ipif_ill;
17700 
17701 	ASSERT(IAM_WRITER_ILL(ill));
17702 	/*
17703 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17704 	 * Hence this should be IRE_CACHE.
17705 	 */
17706 	ASSERT(ire->ire_type == IRE_CACHE);
17707 
17708 	/*
17709 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17710 	 * We are only interested in IRE_CACHES that has borrowed
17711 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17712 	 * for which we need to look at ire_ipif->ipif_ill match
17713 	 * with ill.
17714 	 */
17715 	ASSERT(ire->ire_ipif != NULL);
17716 	ipif_ill = ire->ire_ipif->ipif_ill;
17717 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17718 	    ipif_ill->ill_group == ill->ill_group)) {
17719 		ire_delete(ire);
17720 	}
17721 }
17722 
17723 /*
17724  * Delete all the ire whose stq references ill_arg.
17725  */
17726 static void
17727 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17728 {
17729 	ill_t	*ill = (ill_t *)ill_arg;
17730 	ill_t	*ire_ill;
17731 
17732 	ASSERT(IAM_WRITER_ILL(ill));
17733 	/*
17734 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17735 	 * Hence this should be IRE_CACHE.
17736 	 */
17737 	ASSERT(ire->ire_type == IRE_CACHE);
17738 
17739 	/*
17740 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17741 	 * matches ill. We are only interested in IRE_CACHES that
17742 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17743 	 * filtering here.
17744 	 */
17745 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17746 
17747 	if (ire_ill == ill)
17748 		ire_delete(ire);
17749 }
17750 
17751 /*
17752  * This is called when an ill leaves the group. We want to delete
17753  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17754  * pointing at ill.
17755  */
17756 static void
17757 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17758 {
17759 	ill_t	*ill = (ill_t *)ill_arg;
17760 
17761 	ASSERT(IAM_WRITER_ILL(ill));
17762 	ASSERT(ill->ill_group == NULL);
17763 	/*
17764 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17765 	 * Hence this should be IRE_CACHE.
17766 	 */
17767 	ASSERT(ire->ire_type == IRE_CACHE);
17768 	/*
17769 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17770 	 * matches ill. We are interested in both.
17771 	 */
17772 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
17773 	    (ire->ire_ipif->ipif_ill == ill));
17774 
17775 	ire_delete(ire);
17776 }
17777 
17778 /*
17779  * Initiate deallocate of an IPIF. Always called as writer. Called by
17780  * ill_delete or ip_sioctl_removeif.
17781  */
17782 static void
17783 ipif_free(ipif_t *ipif)
17784 {
17785 	ASSERT(IAM_WRITER_IPIF(ipif));
17786 
17787 	/* Remove conn references */
17788 	reset_conn_ipif(ipif);
17789 
17790 	/*
17791 	 * Make sure we have valid net and subnet broadcast ire's for the
17792 	 * other ipif's which share them with this ipif.
17793 	 */
17794 	if (!ipif->ipif_isv6)
17795 		ipif_check_bcast_ires(ipif);
17796 
17797 	/*
17798 	 * Take down the interface. We can be called either from ill_delete
17799 	 * or from ip_sioctl_removeif.
17800 	 */
17801 	(void) ipif_down(ipif, NULL, NULL);
17802 
17803 	rw_enter(&ill_g_lock, RW_WRITER);
17804 	/* Remove pointers to this ill in the multicast routing tables */
17805 	reset_mrt_vif_ipif(ipif);
17806 	rw_exit(&ill_g_lock);
17807 }
17808 
17809 static void
17810 ipif_free_tail(ipif_t *ipif)
17811 {
17812 	mblk_t	*mp;
17813 	ipif_t	**ipifp;
17814 
17815 	/*
17816 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
17817 	 */
17818 	mutex_enter(&ipif->ipif_saved_ire_lock);
17819 	mp = ipif->ipif_saved_ire_mp;
17820 	ipif->ipif_saved_ire_mp = NULL;
17821 	mutex_exit(&ipif->ipif_saved_ire_lock);
17822 	freemsg(mp);
17823 
17824 	/*
17825 	 * Need to hold both ill_g_lock and ill_lock while
17826 	 * inserting or removing an ipif from the linked list
17827 	 * of ipifs hanging off the ill.
17828 	 */
17829 	rw_enter(&ill_g_lock, RW_WRITER);
17830 	/*
17831 	 * Remove all multicast memberships on the interface now.
17832 	 * This removes IPv4 multicast memberships joined within
17833 	 * the kernel as ipif_down does not do ipif_multicast_down
17834 	 * for IPv4. IPv6 is not handled here as the multicast memberships
17835 	 * are based on ill and not on ipif.
17836 	 */
17837 	ilm_free(ipif);
17838 
17839 	/*
17840 	 * Since we held the ill_g_lock while doing the ilm_free above,
17841 	 * we can assert the ilms were really deleted and not just marked
17842 	 * ILM_DELETED.
17843 	 */
17844 	ASSERT(ilm_walk_ipif(ipif) == 0);
17845 
17846 
17847 	IPIF_TRACE_CLEANUP(ipif);
17848 
17849 	/* Ask SCTP to take it out of it list */
17850 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
17851 
17852 	mutex_enter(&ipif->ipif_ill->ill_lock);
17853 	/* Get it out of the ILL interface list. */
17854 	ipifp = &ipif->ipif_ill->ill_ipif;
17855 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17856 		if (*ipifp == ipif) {
17857 			*ipifp = ipif->ipif_next;
17858 			break;
17859 		}
17860 	}
17861 
17862 	mutex_exit(&ipif->ipif_ill->ill_lock);
17863 	rw_exit(&ill_g_lock);
17864 
17865 	mutex_destroy(&ipif->ipif_saved_ire_lock);
17866 	/* Free the memory. */
17867 	mi_free((char *)ipif);
17868 }
17869 
17870 /*
17871  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
17872  * "ill_name" otherwise.
17873  */
17874 char *
17875 ipif_get_name(ipif_t *ipif, char *buf, int len)
17876 {
17877 	char	lbuf[32];
17878 	char	*name;
17879 	size_t	name_len;
17880 
17881 	buf[0] = '\0';
17882 	if (!ipif)
17883 		return (buf);
17884 	name = ipif->ipif_ill->ill_name;
17885 	name_len = ipif->ipif_ill->ill_name_length;
17886 	if (ipif->ipif_id != 0) {
17887 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
17888 		    ipif->ipif_id);
17889 		name = lbuf;
17890 		name_len = mi_strlen(name) + 1;
17891 	}
17892 	len -= 1;
17893 	buf[len] = '\0';
17894 	len = MIN(len, name_len);
17895 	bcopy(name, buf, len);
17896 	return (buf);
17897 }
17898 
17899 /*
17900  * Find an IPIF based on the name passed in.  Names can be of the
17901  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
17902  * The <phys> string can have forms like <dev><#> (e.g., le0),
17903  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
17904  * When there is no colon, the implied unit id is zero. <phys> must
17905  * correspond to the name of an ILL.  (May be called as writer.)
17906  */
17907 static ipif_t *
17908 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
17909     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
17910     mblk_t *mp, ipsq_func_t func, int *error)
17911 {
17912 	char	*cp;
17913 	char	*endp;
17914 	long	id;
17915 	ill_t	*ill;
17916 	ipif_t	*ipif;
17917 	uint_t	ire_type;
17918 	boolean_t did_alloc = B_FALSE;
17919 	ipsq_t	*ipsq;
17920 
17921 	if (error != NULL)
17922 		*error = 0;
17923 
17924 	/*
17925 	 * If the caller wants to us to create the ipif, make sure we have a
17926 	 * valid zoneid
17927 	 */
17928 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
17929 
17930 	if (namelen == 0) {
17931 		if (error != NULL)
17932 			*error = ENXIO;
17933 		return (NULL);
17934 	}
17935 
17936 	*exists = B_FALSE;
17937 	/* Look for a colon in the name. */
17938 	endp = &name[namelen];
17939 	for (cp = endp; --cp > name; ) {
17940 		if (*cp == IPIF_SEPARATOR_CHAR)
17941 			break;
17942 	}
17943 
17944 	if (*cp == IPIF_SEPARATOR_CHAR) {
17945 		/*
17946 		 * Reject any non-decimal aliases for logical
17947 		 * interfaces. Aliases with leading zeroes
17948 		 * are also rejected as they introduce ambiguity
17949 		 * in the naming of the interfaces.
17950 		 * In order to confirm with existing semantics,
17951 		 * and to not break any programs/script relying
17952 		 * on that behaviour, if<0>:0 is considered to be
17953 		 * a valid interface.
17954 		 *
17955 		 * If alias has two or more digits and the first
17956 		 * is zero, fail.
17957 		 */
17958 		if (&cp[2] < endp && cp[1] == '0')
17959 			return (NULL);
17960 	}
17961 
17962 	if (cp <= name) {
17963 		cp = endp;
17964 	} else {
17965 		*cp = '\0';
17966 	}
17967 
17968 	/*
17969 	 * Look up the ILL, based on the portion of the name
17970 	 * before the slash. ill_lookup_on_name returns a held ill.
17971 	 * Temporary to check whether ill exists already. If so
17972 	 * ill_lookup_on_name will clear it.
17973 	 */
17974 	ill = ill_lookup_on_name(name, do_alloc, isv6,
17975 	    q, mp, func, error, &did_alloc);
17976 	if (cp != endp)
17977 		*cp = IPIF_SEPARATOR_CHAR;
17978 	if (ill == NULL)
17979 		return (NULL);
17980 
17981 	/* Establish the unit number in the name. */
17982 	id = 0;
17983 	if (cp < endp && *endp == '\0') {
17984 		/* If there was a colon, the unit number follows. */
17985 		cp++;
17986 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
17987 			ill_refrele(ill);
17988 			if (error != NULL)
17989 				*error = ENXIO;
17990 			return (NULL);
17991 		}
17992 	}
17993 
17994 	GRAB_CONN_LOCK(q);
17995 	mutex_enter(&ill->ill_lock);
17996 	/* Now see if there is an IPIF with this unit number. */
17997 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
17998 		if (ipif->ipif_id == id) {
17999 			if (zoneid != ALL_ZONES &&
18000 			    zoneid != ipif->ipif_zoneid) {
18001 				mutex_exit(&ill->ill_lock);
18002 				RELEASE_CONN_LOCK(q);
18003 				ill_refrele(ill);
18004 				if (error != NULL)
18005 					*error = ENXIO;
18006 				return (NULL);
18007 			}
18008 			/*
18009 			 * The block comment at the start of ipif_down
18010 			 * explains the use of the macros used below
18011 			 */
18012 			if (IPIF_CAN_LOOKUP(ipif)) {
18013 				ipif_refhold_locked(ipif);
18014 				mutex_exit(&ill->ill_lock);
18015 				if (!did_alloc)
18016 					*exists = B_TRUE;
18017 				/*
18018 				 * Drop locks before calling ill_refrele
18019 				 * since it can potentially call into
18020 				 * ipif_ill_refrele_tail which can end up
18021 				 * in trying to acquire any lock.
18022 				 */
18023 				RELEASE_CONN_LOCK(q);
18024 				ill_refrele(ill);
18025 				return (ipif);
18026 			} else if (IPIF_CAN_WAIT(ipif, q)) {
18027 				ipsq = ill->ill_phyint->phyint_ipsq;
18028 				mutex_enter(&ipsq->ipsq_lock);
18029 				mutex_exit(&ill->ill_lock);
18030 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
18031 				mutex_exit(&ipsq->ipsq_lock);
18032 				RELEASE_CONN_LOCK(q);
18033 				ill_refrele(ill);
18034 				*error = EINPROGRESS;
18035 				return (NULL);
18036 			}
18037 		}
18038 	}
18039 	RELEASE_CONN_LOCK(q);
18040 
18041 	if (!do_alloc) {
18042 		mutex_exit(&ill->ill_lock);
18043 		ill_refrele(ill);
18044 		if (error != NULL)
18045 			*error = ENXIO;
18046 		return (NULL);
18047 	}
18048 
18049 	/*
18050 	 * If none found, atomically allocate and return a new one.
18051 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
18052 	 * to support "receive only" use of lo0:1 etc. as is still done
18053 	 * below as an initial guess.
18054 	 * However, this is now likely to be overriden later in ipif_up_done()
18055 	 * when we know for sure what address has been configured on the
18056 	 * interface, since we might have more than one loopback interface
18057 	 * with a loopback address, e.g. in the case of zones, and all the
18058 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
18059 	 */
18060 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
18061 		ire_type = IRE_LOOPBACK;
18062 	else
18063 		ire_type = IRE_LOCAL;
18064 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
18065 	if (ipif != NULL)
18066 		ipif_refhold_locked(ipif);
18067 	else if (error != NULL)
18068 		*error = ENOMEM;
18069 	mutex_exit(&ill->ill_lock);
18070 	ill_refrele(ill);
18071 	return (ipif);
18072 }
18073 
18074 /*
18075  * This routine is called whenever a new address comes up on an ipif.  If
18076  * we are configured to respond to address mask requests, then we are supposed
18077  * to broadcast an address mask reply at this time.  This routine is also
18078  * called if we are already up, but a netmask change is made.  This is legal
18079  * but might not make the system manager very popular.	(May be called
18080  * as writer.)
18081  */
18082 static void
18083 ipif_mask_reply(ipif_t *ipif)
18084 {
18085 	icmph_t	*icmph;
18086 	ipha_t	*ipha;
18087 	mblk_t	*mp;
18088 
18089 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18090 
18091 	if (!ip_respond_to_address_mask_broadcast)
18092 		return;
18093 
18094 	/* ICMP mask reply is IPv4 only */
18095 	ASSERT(!ipif->ipif_isv6);
18096 	/* ICMP mask reply is not for a loopback interface */
18097 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18098 
18099 	mp = allocb(REPLY_LEN, BPRI_HI);
18100 	if (mp == NULL)
18101 		return;
18102 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18103 
18104 	ipha = (ipha_t *)mp->b_rptr;
18105 	bzero(ipha, REPLY_LEN);
18106 	*ipha = icmp_ipha;
18107 	ipha->ipha_ttl = ip_broadcast_ttl;
18108 	ipha->ipha_src = ipif->ipif_src_addr;
18109 	ipha->ipha_dst = ipif->ipif_brd_addr;
18110 	ipha->ipha_length = htons(REPLY_LEN);
18111 	ipha->ipha_ident = 0;
18112 
18113 	icmph = (icmph_t *)&ipha[1];
18114 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18115 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18116 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18117 
18118 	put(ipif->ipif_wq, mp);
18119 
18120 #undef	REPLY_LEN
18121 }
18122 
18123 /*
18124  * When the mtu in the ipif changes, we call this routine through ire_walk
18125  * to update all the relevant IREs.
18126  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18127  */
18128 static void
18129 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18130 {
18131 	ipif_t *ipif = (ipif_t *)ipif_arg;
18132 
18133 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18134 		return;
18135 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18136 }
18137 
18138 /*
18139  * When the mtu in the ill changes, we call this routine through ire_walk
18140  * to update all the relevant IREs.
18141  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18142  */
18143 void
18144 ill_mtu_change(ire_t *ire, char *ill_arg)
18145 {
18146 	ill_t	*ill = (ill_t *)ill_arg;
18147 
18148 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18149 		return;
18150 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18151 }
18152 
18153 /*
18154  * Join the ipif specific multicast groups.
18155  * Must be called after a mapping has been set up in the resolver.  (Always
18156  * called as writer.)
18157  */
18158 void
18159 ipif_multicast_up(ipif_t *ipif)
18160 {
18161 	int err, index;
18162 	ill_t *ill;
18163 
18164 	ASSERT(IAM_WRITER_IPIF(ipif));
18165 
18166 	ill = ipif->ipif_ill;
18167 	index = ill->ill_phyint->phyint_ifindex;
18168 
18169 	ip1dbg(("ipif_multicast_up\n"));
18170 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18171 		return;
18172 
18173 	if (ipif->ipif_isv6) {
18174 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18175 			return;
18176 
18177 		/* Join the all hosts multicast address */
18178 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18179 		/*
18180 		 * Passing B_TRUE means we have to join the multicast
18181 		 * membership on this interface even though this is
18182 		 * FAILED. If we join on a different one in the group,
18183 		 * we will not be able to delete the membership later
18184 		 * as we currently don't track where we join when we
18185 		 * join within the kernel unlike applications where
18186 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18187 		 * for more on this.
18188 		 */
18189 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18190 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18191 		if (err != 0) {
18192 			ip0dbg(("ipif_multicast_up: "
18193 			    "all_hosts_mcast failed %d\n",
18194 			    err));
18195 			return;
18196 		}
18197 		/*
18198 		 * Enable multicast for the solicited node multicast address
18199 		 */
18200 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18201 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18202 
18203 			ipv6_multi.s6_addr32[3] |=
18204 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18205 
18206 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18207 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18208 			    NULL);
18209 			if (err != 0) {
18210 				ip0dbg(("ipif_multicast_up: solicited MC"
18211 				    " failed %d\n", err));
18212 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18213 				    ill, ill->ill_phyint->phyint_ifindex,
18214 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18215 				return;
18216 			}
18217 		}
18218 	} else {
18219 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18220 			return;
18221 
18222 		/* Join the all hosts multicast address */
18223 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18224 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18225 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18226 		if (err) {
18227 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18228 			return;
18229 		}
18230 	}
18231 	ipif->ipif_multicast_up = 1;
18232 }
18233 
18234 /*
18235  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18236  * any explicit memberships are blown away in ill_leave_multicast() when the
18237  * ill is brought down.
18238  */
18239 static void
18240 ipif_multicast_down(ipif_t *ipif)
18241 {
18242 	int err;
18243 
18244 	ASSERT(IAM_WRITER_IPIF(ipif));
18245 
18246 	ip1dbg(("ipif_multicast_down\n"));
18247 	if (!ipif->ipif_multicast_up)
18248 		return;
18249 
18250 	ASSERT(ipif->ipif_isv6);
18251 
18252 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18253 
18254 	/*
18255 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18256 	 * we should look for ilms on this ill rather than the ones that have
18257 	 * been failed over here.  They are here temporarily. As
18258 	 * ipif_multicast_up has joined on this ill, we should delete only
18259 	 * from this ill.
18260 	 */
18261 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18262 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18263 	    B_TRUE, B_TRUE);
18264 	if (err != 0) {
18265 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18266 		    err));
18267 	}
18268 	/*
18269 	 * Disable multicast for the solicited node multicast address
18270 	 */
18271 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18272 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18273 
18274 		ipv6_multi.s6_addr32[3] |=
18275 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18276 
18277 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18278 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18279 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18280 
18281 		if (err != 0) {
18282 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18283 			    err));
18284 		}
18285 	}
18286 
18287 	ipif->ipif_multicast_up = 0;
18288 }
18289 
18290 /*
18291  * Used when an interface comes up to recreate any extra routes on this
18292  * interface.
18293  */
18294 static ire_t **
18295 ipif_recover_ire(ipif_t *ipif)
18296 {
18297 	mblk_t	*mp;
18298 	ire_t	**ipif_saved_irep;
18299 	ire_t	**irep;
18300 
18301 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18302 	    ipif->ipif_id));
18303 
18304 	mutex_enter(&ipif->ipif_saved_ire_lock);
18305 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18306 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18307 	if (ipif_saved_irep == NULL) {
18308 		mutex_exit(&ipif->ipif_saved_ire_lock);
18309 		return (NULL);
18310 	}
18311 
18312 	irep = ipif_saved_irep;
18313 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18314 		ire_t		*ire;
18315 		queue_t		*rfq;
18316 		queue_t		*stq;
18317 		ifrt_t		*ifrt;
18318 		uchar_t		*src_addr;
18319 		uchar_t		*gateway_addr;
18320 		mblk_t		*resolver_mp;
18321 		ushort_t	type;
18322 
18323 		/*
18324 		 * When the ire was initially created and then added in
18325 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18326 		 * in the case of a traditional interface route, or as one of
18327 		 * the IRE_OFFSUBNET types (with the exception of
18328 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18329 		 * which we don't need to save or recover).  In the case where
18330 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18331 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18332 		 * to satisfy software like GateD and Sun Cluster which creates
18333 		 * routes using the the loopback interface's address as a
18334 		 * gateway.
18335 		 *
18336 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18337 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18338 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18339 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18340 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18341 		 * the route looks like a traditional interface route (where
18342 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18343 		 * the saved ifrt->ifrt_type.  This means that in the case where
18344 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18345 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18346 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18347 		 */
18348 		ifrt = (ifrt_t *)mp->b_rptr;
18349 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18350 			rfq = NULL;
18351 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18352 			    ? ipif->ipif_rq : ipif->ipif_wq;
18353 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18354 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18355 			    : (uint8_t *)&ipif->ipif_src_addr;
18356 			gateway_addr = NULL;
18357 			resolver_mp = ipif->ipif_resolver_mp;
18358 			type = ipif->ipif_net_type;
18359 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18360 			/* Recover multiroute broadcast IRE. */
18361 			rfq = ipif->ipif_rq;
18362 			stq = ipif->ipif_wq;
18363 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18364 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18365 			    : (uint8_t *)&ipif->ipif_src_addr;
18366 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18367 			resolver_mp = ipif->ipif_bcast_mp;
18368 			type = ifrt->ifrt_type;
18369 		} else {
18370 			rfq = NULL;
18371 			stq = NULL;
18372 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18373 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18374 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18375 			resolver_mp = NULL;
18376 			type = ifrt->ifrt_type;
18377 		}
18378 
18379 		/*
18380 		 * Create a copy of the IRE with the saved address and netmask.
18381 		 */
18382 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18383 		    "0x%x/0x%x\n",
18384 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18385 		    ntohl(ifrt->ifrt_addr),
18386 		    ntohl(ifrt->ifrt_mask)));
18387 		ire = ire_create(
18388 		    (uint8_t *)&ifrt->ifrt_addr,
18389 		    (uint8_t *)&ifrt->ifrt_mask,
18390 		    src_addr,
18391 		    gateway_addr,
18392 		    NULL,
18393 		    &ifrt->ifrt_max_frag,
18394 		    NULL,
18395 		    rfq,
18396 		    stq,
18397 		    type,
18398 		    resolver_mp,
18399 		    ipif,
18400 		    NULL,
18401 		    0,
18402 		    0,
18403 		    0,
18404 		    ifrt->ifrt_flags,
18405 		    &ifrt->ifrt_iulp_info);
18406 
18407 		if (ire == NULL) {
18408 			mutex_exit(&ipif->ipif_saved_ire_lock);
18409 			kmem_free(ipif_saved_irep,
18410 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18411 			return (NULL);
18412 		}
18413 
18414 		/*
18415 		 * Some software (for example, GateD and Sun Cluster) attempts
18416 		 * to create (what amount to) IRE_PREFIX routes with the
18417 		 * loopback address as the gateway.  This is primarily done to
18418 		 * set up prefixes with the RTF_REJECT flag set (for example,
18419 		 * when generating aggregate routes.)
18420 		 *
18421 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18422 		 * IRE_LOOPBACK, then we map the request into a
18423 		 * IRE_IF_NORESOLVER.
18424 		 */
18425 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18426 			ire->ire_type = IRE_IF_NORESOLVER;
18427 		/*
18428 		 * ire held by ire_add, will be refreled' towards the
18429 		 * the end of ipif_up_done
18430 		 */
18431 		(void) ire_add(&ire, NULL, NULL, NULL);
18432 		*irep = ire;
18433 		irep++;
18434 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18435 	}
18436 	mutex_exit(&ipif->ipif_saved_ire_lock);
18437 	return (ipif_saved_irep);
18438 }
18439 
18440 /*
18441  * Used to set the netmask and broadcast address to default values when the
18442  * interface is brought up.  (Always called as writer.)
18443  */
18444 static void
18445 ipif_set_default(ipif_t *ipif)
18446 {
18447 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18448 
18449 	if (!ipif->ipif_isv6) {
18450 		/*
18451 		 * Interface holds an IPv4 address. Default
18452 		 * mask is the natural netmask.
18453 		 */
18454 		if (!ipif->ipif_net_mask) {
18455 			ipaddr_t	v4mask;
18456 
18457 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18458 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18459 		}
18460 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18461 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18462 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18463 		} else {
18464 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18465 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18466 		}
18467 		/*
18468 		 * NOTE: SunOS 4.X does this even if the broadcast address
18469 		 * has been already set thus we do the same here.
18470 		 */
18471 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18472 			ipaddr_t	v4addr;
18473 
18474 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18475 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18476 		}
18477 	} else {
18478 		/*
18479 		 * Interface holds an IPv6-only address.  Default
18480 		 * mask is all-ones.
18481 		 */
18482 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18483 			ipif->ipif_v6net_mask = ipv6_all_ones;
18484 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18485 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18486 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18487 		} else {
18488 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18489 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18490 		}
18491 	}
18492 }
18493 
18494 /*
18495  * Return 0 if this address can be used as local address without causing
18496  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18497  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18498  * Special checks are needed to allow the same IPv6 link-local address
18499  * on different ills.
18500  * TODO: allowing the same site-local address on different ill's.
18501  */
18502 int
18503 ip_addr_availability_check(ipif_t *new_ipif)
18504 {
18505 	in6_addr_t our_v6addr;
18506 	ill_t *ill;
18507 	ipif_t *ipif;
18508 	ill_walk_context_t ctx;
18509 
18510 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18511 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18512 	ASSERT(RW_READ_HELD(&ill_g_lock));
18513 
18514 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18515 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18516 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18517 		return (0);
18518 
18519 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18520 
18521 	if (new_ipif->ipif_isv6)
18522 		ill = ILL_START_WALK_V6(&ctx);
18523 	else
18524 		ill = ILL_START_WALK_V4(&ctx);
18525 
18526 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18527 		for (ipif = ill->ill_ipif; ipif != NULL;
18528 		    ipif = ipif->ipif_next) {
18529 			if ((ipif == new_ipif) ||
18530 			    !(ipif->ipif_flags & IPIF_UP) ||
18531 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18532 				continue;
18533 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18534 			    &our_v6addr)) {
18535 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18536 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18537 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18538 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18539 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18540 				    new_ipif->ipif_ill != ill)
18541 					continue;
18542 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18543 				    new_ipif->ipif_ill != ill)
18544 					continue;
18545 				else if (new_ipif->ipif_zoneid !=
18546 				    ipif->ipif_zoneid &&
18547 				    (ill->ill_phyint->phyint_flags &
18548 				    PHYI_LOOPBACK))
18549 					continue;
18550 				else if (new_ipif->ipif_ill == ill)
18551 					return (EADDRINUSE);
18552 				else
18553 					return (EADDRNOTAVAIL);
18554 			}
18555 		}
18556 	}
18557 
18558 	return (0);
18559 }
18560 
18561 /*
18562  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18563  * IREs for the ipif.
18564  * When the routine returns EINPROGRESS then mp has been consumed and
18565  * the ioctl will be acked from ip_rput_dlpi.
18566  */
18567 static int
18568 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18569 {
18570 	ill_t	*ill = ipif->ipif_ill;
18571 	boolean_t isv6 = ipif->ipif_isv6;
18572 	int	err = 0;
18573 	boolean_t success;
18574 
18575 	ASSERT(IAM_WRITER_IPIF(ipif));
18576 
18577 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18578 
18579 	/* Shouldn't get here if it is already up. */
18580 	if (ipif->ipif_flags & IPIF_UP)
18581 		return (EALREADY);
18582 
18583 	/* Skip arp/ndp for any loopback interface. */
18584 	if (ill->ill_wq != NULL) {
18585 		conn_t *connp = Q_TO_CONN(q);
18586 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18587 
18588 		if (!ill->ill_dl_up) {
18589 			/*
18590 			 * ill_dl_up is not yet set. i.e. we are yet to
18591 			 * DL_BIND with the driver and this is the first
18592 			 * logical interface on the ill to become "up".
18593 			 * Tell the driver to get going (via DL_BIND_REQ).
18594 			 * Note that changing "significant" IFF_ flags
18595 			 * address/netmask etc cause a down/up dance, but
18596 			 * does not cause an unbind (DL_UNBIND) with the driver
18597 			 */
18598 			return (ill_dl_up(ill, ipif, mp, q));
18599 		}
18600 
18601 		/*
18602 		 * ipif_resolver_up may end up sending an
18603 		 * AR_INTERFACE_UP message to ARP, which would, in
18604 		 * turn send a DLPI message to the driver. ioctls are
18605 		 * serialized and so we cannot send more than one
18606 		 * interface up message at a time. If ipif_resolver_up
18607 		 * does send an interface up message to ARP, we get
18608 		 * EINPROGRESS and we will complete in ip_arp_done.
18609 		 */
18610 
18611 		ASSERT(connp != NULL);
18612 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18613 		mutex_enter(&connp->conn_lock);
18614 		mutex_enter(&ill->ill_lock);
18615 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18616 		mutex_exit(&ill->ill_lock);
18617 		mutex_exit(&connp->conn_lock);
18618 		if (!success)
18619 			return (EINTR);
18620 
18621 		/*
18622 		 * Crank up IPv6 neighbor discovery
18623 		 * Unlike ARP, this should complete when
18624 		 * ipif_ndp_up returns. However, for
18625 		 * ILLF_XRESOLV interfaces we also send a
18626 		 * AR_INTERFACE_UP to the external resolver.
18627 		 * That ioctl will complete in ip_rput.
18628 		 */
18629 		if (isv6) {
18630 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18631 			    B_FALSE);
18632 			if (err != 0) {
18633 				mp = ipsq_pending_mp_get(ipsq, &connp);
18634 				return (err);
18635 			}
18636 		}
18637 		/* Now, ARP */
18638 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18639 		    EINPROGRESS) {
18640 			/* We will complete it in ip_arp_done */
18641 			return (err);
18642 		}
18643 		mp = ipsq_pending_mp_get(ipsq, &connp);
18644 		ASSERT(mp != NULL);
18645 		if (err != 0)
18646 			return (err);
18647 	}
18648 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18649 }
18650 
18651 /*
18652  * Perform a bind for the physical device.
18653  * When the routine returns EINPROGRESS then mp has been consumed and
18654  * the ioctl will be acked from ip_rput_dlpi.
18655  * Allocate an unbind message and save it until ipif_down.
18656  */
18657 static int
18658 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18659 {
18660 	mblk_t	*areq_mp = NULL;
18661 	mblk_t	*bind_mp = NULL;
18662 	mblk_t	*unbind_mp = NULL;
18663 	conn_t	*connp;
18664 	boolean_t success;
18665 
18666 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18667 	ASSERT(IAM_WRITER_ILL(ill));
18668 
18669 	ASSERT(mp != NULL);
18670 
18671 	/* Create a resolver cookie for ARP */
18672 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18673 		areq_t		*areq;
18674 		uint16_t	sap_addr;
18675 
18676 		areq_mp = ill_arp_alloc(ill,
18677 			(uchar_t *)&ip_areq_template, 0);
18678 		if (areq_mp == NULL) {
18679 			return (ENOMEM);
18680 		}
18681 		freemsg(ill->ill_resolver_mp);
18682 		ill->ill_resolver_mp = areq_mp;
18683 		areq = (areq_t *)areq_mp->b_rptr;
18684 		sap_addr = ill->ill_sap;
18685 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18686 		/*
18687 		 * Wait till we call ill_pending_mp_add to determine
18688 		 * the success before we free the ill_resolver_mp and
18689 		 * attach areq_mp in it's place.
18690 		 */
18691 	}
18692 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18693 	    DL_BIND_REQ);
18694 	if (bind_mp == NULL)
18695 		goto bad;
18696 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18697 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18698 
18699 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18700 	if (unbind_mp == NULL)
18701 		goto bad;
18702 
18703 	/*
18704 	 * Record state needed to complete this operation when the
18705 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18706 	 */
18707 	if (WR(q)->q_next == NULL) {
18708 		connp = Q_TO_CONN(q);
18709 		mutex_enter(&connp->conn_lock);
18710 	} else {
18711 		connp = NULL;
18712 	}
18713 	mutex_enter(&ipif->ipif_ill->ill_lock);
18714 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18715 	mutex_exit(&ipif->ipif_ill->ill_lock);
18716 	if (connp != NULL)
18717 		mutex_exit(&connp->conn_lock);
18718 	if (!success)
18719 		goto bad;
18720 
18721 	/*
18722 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18723 	 * the interface goes down.
18724 	 */
18725 	ASSERT(ill->ill_unbind_mp == NULL);
18726 	ill->ill_unbind_mp = unbind_mp;
18727 
18728 	ill_dlpi_send(ill, bind_mp);
18729 	/* Send down link-layer capabilities probe if not already done. */
18730 	ill_capability_probe(ill);
18731 
18732 	/*
18733 	 * Sysid used to rely on the fact that netboots set domainname
18734 	 * and the like. Now that miniroot boots aren't strictly netboots
18735 	 * and miniroot network configuration is driven from userland
18736 	 * these things still need to be set. This situation can be detected
18737 	 * by comparing the interface being configured here to the one
18738 	 * dhcack was set to reference by the boot loader. Once sysid is
18739 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18740 	 */
18741 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18742 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18743 	    (strlen(srpc_domain) == 0)) {
18744 		if (dhcpinit() != 0)
18745 			cmn_err(CE_WARN, "no cached dhcp response");
18746 	}
18747 
18748 	/*
18749 	 * This operation will complete in ip_rput_dlpi with either
18750 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18751 	 */
18752 	return (EINPROGRESS);
18753 bad:
18754 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18755 	/*
18756 	 * We don't have to check for possible removal from illgrp
18757 	 * as we have not yet inserted in illgrp. For groups
18758 	 * without names, this ipif is still not UP and hence
18759 	 * this could not have possibly had any influence in forming
18760 	 * groups.
18761 	 */
18762 
18763 	if (bind_mp != NULL)
18764 		freemsg(bind_mp);
18765 	if (unbind_mp != NULL)
18766 		freemsg(unbind_mp);
18767 	return (ENOMEM);
18768 }
18769 
18770 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
18771 
18772 /*
18773  * DLPI and ARP is up.
18774  * Create all the IREs associated with an interface bring up multicast.
18775  * Set the interface flag and finish other initialization
18776  * that potentially had to be differed to after DL_BIND_ACK.
18777  */
18778 int
18779 ipif_up_done(ipif_t *ipif)
18780 {
18781 	ire_t	*ire_array[20];
18782 	ire_t	**irep = ire_array;
18783 	ire_t	**irep1;
18784 	ipaddr_t net_mask = 0;
18785 	ipaddr_t subnet_mask, route_mask;
18786 	ill_t	*ill = ipif->ipif_ill;
18787 	queue_t	*stq;
18788 	ipif_t	 *src_ipif;
18789 	ipif_t   *tmp_ipif;
18790 	boolean_t	flush_ire_cache = B_TRUE;
18791 	int	err = 0;
18792 	phyint_t *phyi;
18793 	ire_t	**ipif_saved_irep = NULL;
18794 	int ipif_saved_ire_cnt;
18795 	int	cnt;
18796 	boolean_t	src_ipif_held = B_FALSE;
18797 	boolean_t	ire_added = B_FALSE;
18798 	boolean_t	loopback = B_FALSE;
18799 
18800 	ip1dbg(("ipif_up_done(%s:%u)\n",
18801 		ipif->ipif_ill->ill_name, ipif->ipif_id));
18802 	/* Check if this is a loopback interface */
18803 	if (ipif->ipif_ill->ill_wq == NULL)
18804 		loopback = B_TRUE;
18805 
18806 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18807 	/*
18808 	 * If all other interfaces for this ill are down or DEPRECATED,
18809 	 * or otherwise unsuitable for source address selection, remove
18810 	 * any IRE_CACHE entries for this ill to make sure source
18811 	 * address selection gets to take this new ipif into account.
18812 	 * No need to hold ill_lock while traversing the ipif list since
18813 	 * we are writer
18814 	 */
18815 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
18816 		tmp_ipif = tmp_ipif->ipif_next) {
18817 		if (((tmp_ipif->ipif_flags &
18818 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
18819 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
18820 		    (tmp_ipif == ipif))
18821 			continue;
18822 		/* first useable pre-existing interface */
18823 		flush_ire_cache = B_FALSE;
18824 		break;
18825 	}
18826 	if (flush_ire_cache)
18827 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
18828 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
18829 
18830 	/*
18831 	 * Figure out which way the send-to queue should go.  Only
18832 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
18833 	 * should show up here.
18834 	 */
18835 	switch (ill->ill_net_type) {
18836 	case IRE_IF_RESOLVER:
18837 		stq = ill->ill_rq;
18838 		break;
18839 	case IRE_IF_NORESOLVER:
18840 	case IRE_LOOPBACK:
18841 		stq = ill->ill_wq;
18842 		break;
18843 	default:
18844 		return (EINVAL);
18845 	}
18846 
18847 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
18848 		/*
18849 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
18850 		 * ipif_lookup_on_name(), but in the case of zones we can have
18851 		 * several loopback addresses on lo0. So all the interfaces with
18852 		 * loopback addresses need to be marked IRE_LOOPBACK.
18853 		 */
18854 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
18855 		    htonl(INADDR_LOOPBACK))
18856 			ipif->ipif_ire_type = IRE_LOOPBACK;
18857 		else
18858 			ipif->ipif_ire_type = IRE_LOCAL;
18859 	}
18860 
18861 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
18862 		/*
18863 		 * Can't use our source address. Select a different
18864 		 * source address for the IRE_INTERFACE and IRE_LOCAL
18865 		 */
18866 		src_ipif = ipif_select_source(ipif->ipif_ill,
18867 		    ipif->ipif_subnet, ipif->ipif_zoneid);
18868 		if (src_ipif == NULL)
18869 			src_ipif = ipif;	/* Last resort */
18870 		else
18871 			src_ipif_held = B_TRUE;
18872 	} else {
18873 		src_ipif = ipif;
18874 	}
18875 
18876 	/* Create all the IREs associated with this interface */
18877 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18878 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18879 		/* Register the source address for __sin6_src_id */
18880 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
18881 		    ipif->ipif_zoneid);
18882 		if (err != 0) {
18883 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
18884 			return (err);
18885 		}
18886 		/* If the interface address is set, create the local IRE. */
18887 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
18888 			(void *)ipif,
18889 			ipif->ipif_ire_type,
18890 			ntohl(ipif->ipif_lcl_addr)));
18891 		*irep++ = ire_create(
18892 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
18893 		    (uchar_t *)&ip_g_all_ones,		/* mask */
18894 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
18895 		    NULL,				/* no gateway */
18896 		    NULL,
18897 		    &ip_loopback_mtuplus,		/* max frag size */
18898 		    NULL,
18899 		    ipif->ipif_rq,			/* recv-from queue */
18900 		    NULL,				/* no send-to queue */
18901 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
18902 		    NULL,
18903 		    ipif,
18904 		    NULL,
18905 		    0,
18906 		    0,
18907 		    0,
18908 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
18909 		    RTF_PRIVATE : 0,
18910 		    &ire_uinfo_null);
18911 	} else {
18912 		ip1dbg((
18913 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
18914 		    ipif->ipif_ire_type,
18915 		    ntohl(ipif->ipif_lcl_addr),
18916 		    (uint_t)ipif->ipif_flags));
18917 	}
18918 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18919 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18920 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
18921 	} else {
18922 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
18923 	}
18924 
18925 	subnet_mask = ipif->ipif_net_mask;
18926 
18927 	/*
18928 	 * If mask was not specified, use natural netmask of
18929 	 * interface address. Also, store this mask back into the
18930 	 * ipif struct.
18931 	 */
18932 	if (subnet_mask == 0) {
18933 		subnet_mask = net_mask;
18934 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
18935 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
18936 		    ipif->ipif_v6subnet);
18937 	}
18938 
18939 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
18940 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
18941 	    ipif->ipif_subnet != INADDR_ANY) {
18942 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18943 
18944 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18945 			route_mask = IP_HOST_MASK;
18946 		} else {
18947 			route_mask = subnet_mask;
18948 		}
18949 
18950 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
18951 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
18952 			(void *)ipif, (void *)ill,
18953 			ill->ill_net_type,
18954 			ntohl(ipif->ipif_subnet)));
18955 		*irep++ = ire_create(
18956 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
18957 		    (uchar_t *)&route_mask,		/* mask */
18958 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
18959 		    NULL,				/* no gateway */
18960 		    NULL,
18961 		    &ipif->ipif_mtu,			/* max frag */
18962 		    NULL,
18963 		    NULL,				/* no recv queue */
18964 		    stq,				/* send-to queue */
18965 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
18966 		    ill->ill_resolver_mp,		/* xmit header */
18967 		    ipif,
18968 		    NULL,
18969 		    0,
18970 		    0,
18971 		    0,
18972 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
18973 		    &ire_uinfo_null);
18974 	}
18975 
18976 	/*
18977 	 * If the interface address is set, create the broadcast IREs.
18978 	 *
18979 	 * ire_create_bcast checks if the proposed new IRE matches
18980 	 * any existing IRE's with the same physical interface (ILL).
18981 	 * This should get rid of duplicates.
18982 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
18983 	 * any broadcast ires.
18984 	 */
18985 	if ((ipif->ipif_subnet != INADDR_ANY) &&
18986 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
18987 		ipaddr_t addr;
18988 
18989 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
18990 		irep = ire_check_and_create_bcast(ipif, 0, irep,
18991 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18992 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
18993 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18994 
18995 		/*
18996 		 * For backward compatibility, we need to create net
18997 		 * broadcast ire's based on the old "IP address class
18998 		 * system."  The reason is that some old machines only
18999 		 * respond to these class derived net broadcast.
19000 		 *
19001 		 * But we should not create these net broadcast ire's if
19002 		 * the subnet_mask is shorter than the IP address class based
19003 		 * derived netmask.  Otherwise, we may create a net
19004 		 * broadcast address which is the same as an IP address
19005 		 * on the subnet.  Then TCP will refuse to talk to that
19006 		 * address.
19007 		 *
19008 		 * Nor do we need IRE_BROADCAST ire's for the interface
19009 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
19010 		 * interface is already created.  Creating these broadcast
19011 		 * ire's will only create confusion as the "addr" is going
19012 		 * to be same as that of the IP address of the interface.
19013 		 */
19014 		if (net_mask < subnet_mask) {
19015 			addr = net_mask & ipif->ipif_subnet;
19016 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19017 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19018 			irep = ire_check_and_create_bcast(ipif,
19019 			    ~net_mask | addr, irep,
19020 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19021 		}
19022 
19023 		if (subnet_mask != 0xFFFFFFFF) {
19024 			addr = ipif->ipif_subnet;
19025 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19026 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19027 			irep = ire_check_and_create_bcast(ipif,
19028 			    ~subnet_mask|addr, irep,
19029 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19030 		}
19031 	}
19032 
19033 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19034 
19035 	/* If an earlier ire_create failed, get out now */
19036 	for (irep1 = irep; irep1 > ire_array; ) {
19037 		irep1--;
19038 		if (*irep1 == NULL) {
19039 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
19040 			err = ENOMEM;
19041 			goto bad;
19042 		}
19043 	}
19044 
19045 	/*
19046 	 * Need to atomically check for ip_addr_availablity_check
19047 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
19048 	 * from group also.The ill_g_lock is grabbed as reader
19049 	 * just to make sure no new ills or new ipifs are being added
19050 	 * to the system while we are checking the uniqueness of addresses.
19051 	 */
19052 	rw_enter(&ill_g_lock, RW_READER);
19053 	mutex_enter(&ip_addr_avail_lock);
19054 	/* Mark it up, and increment counters. */
19055 	ill->ill_ipif_up_count++;
19056 	ipif->ipif_flags |= IPIF_UP;
19057 	err = ip_addr_availability_check(ipif);
19058 	mutex_exit(&ip_addr_avail_lock);
19059 	rw_exit(&ill_g_lock);
19060 
19061 	if (err != 0) {
19062 		/*
19063 		 * Our address may already be up on the same ill. In this case,
19064 		 * the ARP entry for our ipif replaced the one for the other
19065 		 * ipif. So we don't want to delete it (otherwise the other ipif
19066 		 * would be unable to send packets).
19067 		 * ip_addr_availability_check() identifies this case for us and
19068 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
19069 		 * which is the expected error code.
19070 		 */
19071 		if (err == EADDRINUSE) {
19072 			freemsg(ipif->ipif_arp_del_mp);
19073 			ipif->ipif_arp_del_mp = NULL;
19074 			err = EADDRNOTAVAIL;
19075 		}
19076 		ill->ill_ipif_up_count--;
19077 		ipif->ipif_flags &= ~IPIF_UP;
19078 		goto bad;
19079 	}
19080 
19081 	/*
19082 	 * Add in all newly created IREs.  ire_create_bcast() has
19083 	 * already checked for duplicates of the IRE_BROADCAST type.
19084 	 * We want to add before we call ifgrp_insert which wants
19085 	 * to know whether IRE_IF_RESOLVER exists or not.
19086 	 *
19087 	 * NOTE : We refrele the ire though we may branch to "bad"
19088 	 *	  later on where we do ire_delete. This is okay
19089 	 *	  because nobody can delete it as we are running
19090 	 *	  exclusively.
19091 	 */
19092 	for (irep1 = irep; irep1 > ire_array; ) {
19093 		irep1--;
19094 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19095 		/*
19096 		 * refheld by ire_add. refele towards the end of the func
19097 		 */
19098 		(void) ire_add(irep1, NULL, NULL, NULL);
19099 	}
19100 	ire_added = B_TRUE;
19101 	/*
19102 	 * Form groups if possible.
19103 	 *
19104 	 * If we are supposed to be in a ill_group with a name, insert it
19105 	 * now as we know that at least one ipif is UP. Otherwise form
19106 	 * nameless groups.
19107 	 *
19108 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19109 	 * this ipif into the appropriate interface group, or create a
19110 	 * new one. If this is already in a nameless group, we try to form
19111 	 * a bigger group looking at other ills potentially sharing this
19112 	 * ipif's prefix.
19113 	 */
19114 	phyi = ill->ill_phyint;
19115 	if (phyi->phyint_groupname_len != 0) {
19116 		ASSERT(phyi->phyint_groupname != NULL);
19117 		if (ill->ill_ipif_up_count == 1) {
19118 			ASSERT(ill->ill_group == NULL);
19119 			err = illgrp_insert(&illgrp_head_v4, ill,
19120 			    phyi->phyint_groupname, NULL, B_TRUE);
19121 			if (err != 0) {
19122 				ip1dbg(("ipif_up_done: illgrp allocation "
19123 				    "failed, error %d\n", err));
19124 				goto bad;
19125 			}
19126 		}
19127 		ASSERT(ill->ill_group != NULL);
19128 	}
19129 
19130 	/*
19131 	 * When this is part of group, we need to make sure that
19132 	 * any broadcast ires created because of this ipif coming
19133 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19134 	 * so that we don't receive duplicate broadcast packets.
19135 	 */
19136 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19137 		ipif_renominate_bcast(ipif);
19138 
19139 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19140 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19141 	ipif_saved_irep = ipif_recover_ire(ipif);
19142 
19143 	if (!loopback) {
19144 		/*
19145 		 * If the broadcast address has been set, make sure it makes
19146 		 * sense based on the interface address.
19147 		 * Only match on ill since we are sharing broadcast addresses.
19148 		 */
19149 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19150 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19151 			ire_t	*ire;
19152 
19153 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19154 			    IRE_BROADCAST, ipif, ALL_ZONES,
19155 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19156 
19157 			if (ire == NULL) {
19158 				/*
19159 				 * If there isn't a matching broadcast IRE,
19160 				 * revert to the default for this netmask.
19161 				 */
19162 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19163 				mutex_enter(&ipif->ipif_ill->ill_lock);
19164 				ipif_set_default(ipif);
19165 				mutex_exit(&ipif->ipif_ill->ill_lock);
19166 			} else {
19167 				ire_refrele(ire);
19168 			}
19169 		}
19170 
19171 	}
19172 
19173 
19174 	/* This is the first interface on this ill */
19175 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19176 		/*
19177 		 * Need to recover all multicast memberships in the driver.
19178 		 * This had to be deferred until we had attached.
19179 		 */
19180 		ill_recover_multicast(ill);
19181 	}
19182 	/* Join the allhosts multicast address */
19183 	ipif_multicast_up(ipif);
19184 
19185 	if (!loopback) {
19186 		/*
19187 		 * See whether anybody else would benefit from the
19188 		 * new ipif that we added. We call this always rather
19189 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19190 		 * ipif is for the benefit of illgrp_insert (done above)
19191 		 * which does not do source address selection as it does
19192 		 * not want to re-create interface routes that we are
19193 		 * having reference to it here.
19194 		 */
19195 		ill_update_source_selection(ill);
19196 	}
19197 
19198 	for (irep1 = irep; irep1 > ire_array; ) {
19199 		irep1--;
19200 		if (*irep1 != NULL) {
19201 			/* was held in ire_add */
19202 			ire_refrele(*irep1);
19203 		}
19204 	}
19205 
19206 	cnt = ipif_saved_ire_cnt;
19207 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19208 		if (*irep1 != NULL) {
19209 			/* was held in ire_add */
19210 			ire_refrele(*irep1);
19211 		}
19212 	}
19213 
19214 	/*
19215 	 * This had to be deferred until we had bound.
19216 	 * tell routing sockets that this interface is up
19217 	 */
19218 	ip_rts_ifmsg(ipif);
19219 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19220 
19221 	if (!loopback) {
19222 		/* Broadcast an address mask reply. */
19223 		ipif_mask_reply(ipif);
19224 	}
19225 	if (ipif_saved_irep != NULL) {
19226 		kmem_free(ipif_saved_irep,
19227 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19228 	}
19229 	if (src_ipif_held)
19230 		ipif_refrele(src_ipif);
19231 	/* Let SCTP update the status for this ipif */
19232 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19233 	return (0);
19234 
19235 bad:
19236 	ip1dbg(("ipif_up_done: FAILED \n"));
19237 	/*
19238 	 * We don't have to bother removing from ill groups because
19239 	 *
19240 	 * 1) For groups with names, we insert only when the first ipif
19241 	 *    comes up. In that case if it fails, it will not be in any
19242 	 *    group. So, we need not try to remove for that case.
19243 	 *
19244 	 * 2) For groups without names, either we tried to insert ipif_ill
19245 	 *    in a group as singleton or found some other group to become
19246 	 *    a bigger group. For the former, if it fails we don't have
19247 	 *    anything to do as ipif_ill is not in the group and for the
19248 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19249 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19250 	 */
19251 	while (irep > ire_array) {
19252 		irep--;
19253 		if (*irep != NULL) {
19254 			ire_delete(*irep);
19255 			if (ire_added)
19256 				ire_refrele(*irep);
19257 		}
19258 	}
19259 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19260 
19261 	if (ipif_saved_irep != NULL) {
19262 		kmem_free(ipif_saved_irep,
19263 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19264 	}
19265 	if (src_ipif_held)
19266 		ipif_refrele(src_ipif);
19267 
19268 	ipif_arp_down(ipif);
19269 	return (err);
19270 }
19271 
19272 /*
19273  * Turn off the ARP with the ILLF_NOARP flag.
19274  */
19275 static int
19276 ill_arp_off(ill_t *ill)
19277 {
19278 	mblk_t	*arp_off_mp = NULL;
19279 	mblk_t	*arp_on_mp = NULL;
19280 
19281 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19282 
19283 	ASSERT(IAM_WRITER_ILL(ill));
19284 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19285 
19286 	/*
19287 	 * If the on message is still around we've already done
19288 	 * an arp_off without doing an arp_on thus there is no
19289 	 * work needed.
19290 	 */
19291 	if (ill->ill_arp_on_mp != NULL)
19292 		return (0);
19293 
19294 	/*
19295 	 * Allocate an ARP on message (to be saved) and an ARP off message
19296 	 */
19297 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19298 	if (!arp_off_mp)
19299 		return (ENOMEM);
19300 
19301 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19302 	if (!arp_on_mp)
19303 		goto failed;
19304 
19305 	ASSERT(ill->ill_arp_on_mp == NULL);
19306 	ill->ill_arp_on_mp = arp_on_mp;
19307 
19308 	/* Send an AR_INTERFACE_OFF request */
19309 	putnext(ill->ill_rq, arp_off_mp);
19310 	return (0);
19311 failed:
19312 
19313 	if (arp_off_mp)
19314 		freemsg(arp_off_mp);
19315 	return (ENOMEM);
19316 }
19317 
19318 /*
19319  * Turn on ARP by turning off the ILLF_NOARP flag.
19320  */
19321 static int
19322 ill_arp_on(ill_t *ill)
19323 {
19324 	mblk_t	*mp;
19325 
19326 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19327 
19328 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19329 
19330 	ASSERT(IAM_WRITER_ILL(ill));
19331 	/*
19332 	 * Send an AR_INTERFACE_ON request if we have already done
19333 	 * an arp_off (which allocated the message).
19334 	 */
19335 	if (ill->ill_arp_on_mp != NULL) {
19336 		mp = ill->ill_arp_on_mp;
19337 		ill->ill_arp_on_mp = NULL;
19338 		putnext(ill->ill_rq, mp);
19339 	}
19340 	return (0);
19341 }
19342 
19343 /*
19344  * Called after either deleting ill from the group or when setting
19345  * FAILED or STANDBY on the interface.
19346  */
19347 static void
19348 illgrp_reset_schednext(ill_t *ill)
19349 {
19350 	ill_group_t *illgrp;
19351 	ill_t *save_ill;
19352 
19353 	ASSERT(IAM_WRITER_ILL(ill));
19354 	/*
19355 	 * When called from illgrp_delete, ill_group will be non-NULL.
19356 	 * But when called from ip_sioctl_flags, it could be NULL if
19357 	 * somebody is setting FAILED/INACTIVE on some interface which
19358 	 * is not part of a group.
19359 	 */
19360 	illgrp = ill->ill_group;
19361 	if (illgrp == NULL)
19362 		return;
19363 	if (illgrp->illgrp_ill_schednext != ill)
19364 		return;
19365 
19366 	illgrp->illgrp_ill_schednext = NULL;
19367 	save_ill = ill;
19368 	/*
19369 	 * Choose a good ill to be the next one for
19370 	 * outbound traffic. As the flags FAILED/STANDBY is
19371 	 * not yet marked when called from ip_sioctl_flags,
19372 	 * we check for ill separately.
19373 	 */
19374 	for (ill = illgrp->illgrp_ill; ill != NULL;
19375 	    ill = ill->ill_group_next) {
19376 		if ((ill != save_ill) &&
19377 		    !(ill->ill_phyint->phyint_flags &
19378 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19379 			illgrp->illgrp_ill_schednext = ill;
19380 			return;
19381 		}
19382 	}
19383 }
19384 
19385 /*
19386  * Given an ill, find the next ill in the group to be scheduled.
19387  * (This should be called by ip_newroute() before ire_create().)
19388  * The passed in ill may be pulled out of the group, after we have picked
19389  * up a different outgoing ill from the same group. However ire add will
19390  * atomically check this.
19391  */
19392 ill_t *
19393 illgrp_scheduler(ill_t *ill)
19394 {
19395 	ill_t *retill;
19396 	ill_group_t *illgrp;
19397 	int illcnt;
19398 	int i;
19399 	uint64_t flags;
19400 
19401 	/*
19402 	 * We don't use a lock to check for the ill_group. If this ill
19403 	 * is currently being inserted we may end up just returning this
19404 	 * ill itself. That is ok.
19405 	 */
19406 	if (ill->ill_group == NULL) {
19407 		ill_refhold(ill);
19408 		return (ill);
19409 	}
19410 
19411 	/*
19412 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19413 	 * a set of stable ills. No ill can be added or deleted or change
19414 	 * group while we hold the reader lock.
19415 	 */
19416 	rw_enter(&ill_g_lock, RW_READER);
19417 	if ((illgrp = ill->ill_group) == NULL) {
19418 		rw_exit(&ill_g_lock);
19419 		ill_refhold(ill);
19420 		return (ill);
19421 	}
19422 
19423 	illcnt = illgrp->illgrp_ill_count;
19424 	mutex_enter(&illgrp->illgrp_lock);
19425 	retill = illgrp->illgrp_ill_schednext;
19426 
19427 	if (retill == NULL)
19428 		retill = illgrp->illgrp_ill;
19429 
19430 	/*
19431 	 * We do a circular search beginning at illgrp_ill_schednext
19432 	 * or illgrp_ill. We don't check the flags against the ill lock
19433 	 * since it can change anytime. The ire creation will be atomic
19434 	 * and will fail if the ill is FAILED or OFFLINE.
19435 	 */
19436 	for (i = 0; i < illcnt; i++) {
19437 		flags = retill->ill_phyint->phyint_flags;
19438 
19439 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19440 		    ILL_CAN_LOOKUP(retill)) {
19441 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19442 			ill_refhold(retill);
19443 			break;
19444 		}
19445 		retill = retill->ill_group_next;
19446 		if (retill == NULL)
19447 			retill = illgrp->illgrp_ill;
19448 	}
19449 	mutex_exit(&illgrp->illgrp_lock);
19450 	rw_exit(&ill_g_lock);
19451 
19452 	return (i == illcnt ? NULL : retill);
19453 }
19454 
19455 /*
19456  * Checks for availbility of a usable source address (if there is one) when the
19457  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19458  * this selection is done regardless of the destination.
19459  */
19460 boolean_t
19461 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19462 {
19463 	uint_t	ifindex;
19464 	ipif_t	*ipif = NULL;
19465 	ill_t	*uill;
19466 	boolean_t isv6;
19467 
19468 	ASSERT(ill != NULL);
19469 
19470 	isv6 = ill->ill_isv6;
19471 	ifindex = ill->ill_usesrc_ifindex;
19472 	if (ifindex != 0) {
19473 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19474 		    NULL);
19475 		if (uill == NULL)
19476 			return (NULL);
19477 		mutex_enter(&uill->ill_lock);
19478 		for (ipif = uill->ill_ipif; ipif != NULL;
19479 		    ipif = ipif->ipif_next) {
19480 			if (!IPIF_CAN_LOOKUP(ipif))
19481 				continue;
19482 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19483 				continue;
19484 			if (!(ipif->ipif_flags & IPIF_UP))
19485 				continue;
19486 			if (ipif->ipif_zoneid != zoneid)
19487 				continue;
19488 			if ((isv6 &&
19489 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19490 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19491 				continue;
19492 			mutex_exit(&uill->ill_lock);
19493 			ill_refrele(uill);
19494 			return (B_TRUE);
19495 		}
19496 		mutex_exit(&uill->ill_lock);
19497 		ill_refrele(uill);
19498 	}
19499 	return (B_FALSE);
19500 }
19501 
19502 /*
19503  * Determine the best source address given a destination address and an ill.
19504  * Prefers non-deprecated over deprecated but will return a deprecated
19505  * address if there is no other choice. If there is a usable source address
19506  * on the interface pointed to by ill_usesrc_ifindex then that is given
19507  * first preference.
19508  *
19509  * Returns NULL if there is no suitable source address for the ill.
19510  * This only occurs when there is no valid source address for the ill.
19511  */
19512 ipif_t *
19513 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19514 {
19515 	ipif_t *ipif;
19516 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19517 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19518 	int index = 0;
19519 	boolean_t wrapped = B_FALSE;
19520 	boolean_t same_subnet_only = B_FALSE;
19521 	boolean_t ipif_same_found, ipif_other_found;
19522 	ill_t	*till, *usill = NULL;
19523 
19524 	if (ill->ill_usesrc_ifindex != 0) {
19525 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19526 		    NULL, NULL, NULL, NULL);
19527 		if (usill != NULL)
19528 			ill = usill;	/* Select source from usesrc ILL */
19529 		else
19530 			return (NULL);
19531 	}
19532 
19533 	/*
19534 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19535 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19536 	 * After selecting the right ipif, under ill_lock make sure ipif is
19537 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19538 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19539 	 * but not under a lock.
19540 	 */
19541 	rw_enter(&ill_g_lock, RW_READER);
19542 
19543 retry:
19544 	till = ill;
19545 	ipif_arr[0] = NULL;
19546 
19547 	if (till->ill_group != NULL)
19548 		till = till->ill_group->illgrp_ill;
19549 
19550 	/*
19551 	 * Choose one good source address from each ill across the group.
19552 	 * If possible choose a source address in the same subnet as
19553 	 * the destination address.
19554 	 *
19555 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19556 	 * This is okay because of the following.
19557 	 *
19558 	 *    If PHYI_FAILED is set and we still have non-deprecated
19559 	 *    addresses, it means the addresses have not yet been
19560 	 *    failed over to a different interface. We potentially
19561 	 *    select them to create IRE_CACHES, which will be later
19562 	 *    flushed when the addresses move over.
19563 	 *
19564 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19565 	 *    addresses, it means either the user has configured them
19566 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19567 	 *    been moved over. For the former, in.mpathd does a failover
19568 	 *    when the interface becomes INACTIVE and hence we should
19569 	 *    not find them. Once INACTIVE is set, we don't allow them
19570 	 *    to create logical interfaces anymore. For the latter, a
19571 	 *    flush will happen when INACTIVE is cleared which will
19572 	 *    flush the IRE_CACHES.
19573 	 *
19574 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19575 	 *    over soon. We potentially select them to create IRE_CACHEs,
19576 	 *    which will be later flushed when the addresses move over.
19577 	 *
19578 	 * NOTE : As ipif_select_source is called to borrow source address
19579 	 * for an ipif that is part of a group, source address selection
19580 	 * will be re-done whenever the group changes i.e either an
19581 	 * insertion/deletion in the group.
19582 	 *
19583 	 * Fill ipif_arr[] with source addresses, using these rules:
19584 	 *
19585 	 *	1. At most one source address from a given ill ends up
19586 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19587 	 *	   associated with a given ill ends up in ipif_arr[].
19588 	 *
19589 	 *	2. If there is at least one non-deprecated ipif in the
19590 	 *	   IPMP group with a source address on the same subnet as
19591 	 *	   our destination, then fill ipif_arr[] only with
19592 	 *	   source addresses on the same subnet as our destination.
19593 	 *	   Note that because of (1), only the first
19594 	 *	   non-deprecated ipif found with a source address
19595 	 *	   matching the destination ends up in ipif_arr[].
19596 	 *
19597 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19598 	 *	   addresses not in the same subnet as our destination.
19599 	 *	   Again, because of (1), only the first off-subnet source
19600 	 *	   address will be chosen.
19601 	 *
19602 	 *	4. If there are no non-deprecated ipifs, then just use
19603 	 *	   the source address associated with the last deprecated
19604 	 *	   one we find that happens to be on the same subnet,
19605 	 *	   otherwise the first one not in the same subnet.
19606 	 */
19607 	for (; till != NULL; till = till->ill_group_next) {
19608 		ipif_same_found = B_FALSE;
19609 		ipif_other_found = B_FALSE;
19610 		for (ipif = till->ill_ipif; ipif != NULL;
19611 		    ipif = ipif->ipif_next) {
19612 			if (!IPIF_CAN_LOOKUP(ipif))
19613 				continue;
19614 			/* Always skip NOLOCAL and ANYCAST interfaces */
19615 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19616 				continue;
19617 			if (!(ipif->ipif_flags & IPIF_UP))
19618 				continue;
19619 			if (ipif->ipif_zoneid != zoneid)
19620 				continue;
19621 			/*
19622 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19623 			 * but are not valid as source addresses.
19624 			 */
19625 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19626 				continue;
19627 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19628 				if (ipif_dep == NULL ||
19629 				    (ipif->ipif_net_mask & dst) ==
19630 				    ipif->ipif_subnet)
19631 					ipif_dep = ipif;
19632 				continue;
19633 			}
19634 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19635 				/* found a source address in the same subnet */
19636 				if (same_subnet_only == B_FALSE) {
19637 					same_subnet_only = B_TRUE;
19638 					index = 0;
19639 				}
19640 				ipif_same_found = B_TRUE;
19641 			} else {
19642 				if (same_subnet_only == B_TRUE ||
19643 				    ipif_other_found == B_TRUE)
19644 					continue;
19645 				ipif_other_found = B_TRUE;
19646 			}
19647 			ipif_arr[index++] = ipif;
19648 			if (index == MAX_IPIF_SELECT_SOURCE) {
19649 				wrapped = B_TRUE;
19650 				index = 0;
19651 			}
19652 			if (ipif_same_found == B_TRUE)
19653 				break;
19654 		}
19655 	}
19656 
19657 	if (ipif_arr[0] == NULL) {
19658 		ipif = ipif_dep;
19659 	} else {
19660 		if (wrapped)
19661 			index = MAX_IPIF_SELECT_SOURCE;
19662 		ipif = ipif_arr[ipif_rand() % index];
19663 		ASSERT(ipif != NULL);
19664 	}
19665 
19666 	if (ipif != NULL) {
19667 		mutex_enter(&ipif->ipif_ill->ill_lock);
19668 		if (!IPIF_CAN_LOOKUP(ipif)) {
19669 			mutex_exit(&ipif->ipif_ill->ill_lock);
19670 			goto retry;
19671 		}
19672 		ipif_refhold_locked(ipif);
19673 		mutex_exit(&ipif->ipif_ill->ill_lock);
19674 	}
19675 
19676 	rw_exit(&ill_g_lock);
19677 	if (usill != NULL)
19678 		ill_refrele(usill);
19679 
19680 #ifdef DEBUG
19681 	if (ipif == NULL) {
19682 		char buf1[INET6_ADDRSTRLEN];
19683 
19684 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
19685 		    ill->ill_name,
19686 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
19687 	} else {
19688 		char buf1[INET6_ADDRSTRLEN];
19689 		char buf2[INET6_ADDRSTRLEN];
19690 
19691 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
19692 		    ipif->ipif_ill->ill_name,
19693 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
19694 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
19695 		    buf2, sizeof (buf2))));
19696 	}
19697 #endif /* DEBUG */
19698 	return (ipif);
19699 }
19700 
19701 
19702 /*
19703  * If old_ipif is not NULL, see if ipif was derived from old
19704  * ipif and if so, recreate the interface route by re-doing
19705  * source address selection. This happens when ipif_down ->
19706  * ipif_update_other_ipifs calls us.
19707  *
19708  * If old_ipif is NULL, just redo the source address selection
19709  * if needed. This happens when illgrp_insert or ipif_up_done
19710  * calls us.
19711  */
19712 static void
19713 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
19714 {
19715 	ire_t *ire;
19716 	ire_t *ipif_ire;
19717 	queue_t *stq;
19718 	ipif_t *nipif;
19719 	ill_t *ill;
19720 	boolean_t need_rele = B_FALSE;
19721 
19722 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
19723 	ASSERT(IAM_WRITER_IPIF(ipif));
19724 
19725 	ill = ipif->ipif_ill;
19726 	if (!(ipif->ipif_flags &
19727 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
19728 		/*
19729 		 * Can't possibly have borrowed the source
19730 		 * from old_ipif.
19731 		 */
19732 		return;
19733 	}
19734 
19735 	/*
19736 	 * Is there any work to be done? No work if the address
19737 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
19738 	 * ipif_select_source() does not borrow addresses from
19739 	 * NOLOCAL and ANYCAST interfaces).
19740 	 */
19741 	if ((old_ipif != NULL) &&
19742 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
19743 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
19744 	    (old_ipif->ipif_flags &
19745 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
19746 		return;
19747 	}
19748 
19749 	/*
19750 	 * Perform the same checks as when creating the
19751 	 * IRE_INTERFACE in ipif_up_done.
19752 	 */
19753 	if (!(ipif->ipif_flags & IPIF_UP))
19754 		return;
19755 
19756 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
19757 	    (ipif->ipif_subnet == INADDR_ANY))
19758 		return;
19759 
19760 	ipif_ire = ipif_to_ire(ipif);
19761 	if (ipif_ire == NULL)
19762 		return;
19763 
19764 	/*
19765 	 * We know that ipif uses some other source for its
19766 	 * IRE_INTERFACE. Is it using the source of this
19767 	 * old_ipif?
19768 	 */
19769 	if (old_ipif != NULL &&
19770 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
19771 		ire_refrele(ipif_ire);
19772 		return;
19773 	}
19774 	if (ip_debug > 2) {
19775 		/* ip1dbg */
19776 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
19777 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
19778 	}
19779 
19780 	stq = ipif_ire->ire_stq;
19781 
19782 	/*
19783 	 * Can't use our source address. Select a different
19784 	 * source address for the IRE_INTERFACE.
19785 	 */
19786 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
19787 	if (nipif == NULL) {
19788 		/* Last resort - all ipif's have IPIF_NOLOCAL */
19789 		nipif = ipif;
19790 	} else {
19791 		need_rele = B_TRUE;
19792 	}
19793 
19794 	ire = ire_create(
19795 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
19796 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
19797 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
19798 	    NULL,				/* no gateway */
19799 	    NULL,
19800 	    &ipif->ipif_mtu,			/* max frag */
19801 	    NULL,				/* fast path header */
19802 	    NULL,				/* no recv from queue */
19803 	    stq,				/* send-to queue */
19804 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19805 	    ill->ill_resolver_mp,		/* xmit header */
19806 	    ipif,
19807 	    NULL,
19808 	    0,
19809 	    0,
19810 	    0,
19811 	    0,
19812 	    &ire_uinfo_null);
19813 
19814 	if (ire != NULL) {
19815 		ire_t *ret_ire;
19816 		int error;
19817 
19818 		/*
19819 		 * We don't need ipif_ire anymore. We need to delete
19820 		 * before we add so that ire_add does not detect
19821 		 * duplicates.
19822 		 */
19823 		ire_delete(ipif_ire);
19824 		ret_ire = ire;
19825 		error = ire_add(&ret_ire, NULL, NULL, NULL);
19826 		ASSERT(error == 0);
19827 		ASSERT(ire == ret_ire);
19828 		/* Held in ire_add */
19829 		ire_refrele(ret_ire);
19830 	}
19831 	/*
19832 	 * Either we are falling through from above or could not
19833 	 * allocate a replacement.
19834 	 */
19835 	ire_refrele(ipif_ire);
19836 	if (need_rele)
19837 		ipif_refrele(nipif);
19838 }
19839 
19840 /*
19841  * This old_ipif is going away.
19842  *
19843  * Determine if any other ipif's is using our address as
19844  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
19845  * IPIF_DEPRECATED).
19846  * Find the IRE_INTERFACE for such ipifs and recreate them
19847  * to use an different source address following the rules in
19848  * ipif_up_done.
19849  *
19850  * This function takes an illgrp as an argument so that illgrp_delete
19851  * can call this to update source address even after deleting the
19852  * old_ipif->ipif_ill from the ill group.
19853  */
19854 static void
19855 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
19856 {
19857 	ipif_t *ipif;
19858 	ill_t *ill;
19859 	char	buf[INET6_ADDRSTRLEN];
19860 
19861 	ASSERT(IAM_WRITER_IPIF(old_ipif));
19862 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
19863 
19864 	ill = old_ipif->ipif_ill;
19865 
19866 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
19867 	    ill->ill_name,
19868 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
19869 	    buf, sizeof (buf))));
19870 	/*
19871 	 * If this part of a group, look at all ills as ipif_select_source
19872 	 * borrows source address across all the ills in the group.
19873 	 */
19874 	if (illgrp != NULL)
19875 		ill = illgrp->illgrp_ill;
19876 
19877 	for (; ill != NULL; ill = ill->ill_group_next) {
19878 		for (ipif = ill->ill_ipif; ipif != NULL;
19879 		    ipif = ipif->ipif_next) {
19880 
19881 			if (ipif == old_ipif)
19882 				continue;
19883 
19884 			ipif_recreate_interface_routes(old_ipif, ipif);
19885 		}
19886 	}
19887 }
19888 
19889 /* ARGSUSED */
19890 int
19891 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19892 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19893 {
19894 	/*
19895 	 * ill_phyint_reinit merged the v4 and v6 into a single
19896 	 * ipsq. Could also have become part of a ipmp group in the
19897 	 * process, and we might not have been able to complete the
19898 	 * operation in ipif_set_values, if we could not become
19899 	 * exclusive.  If so restart it here.
19900 	 */
19901 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
19902 }
19903 
19904 
19905 /* ARGSUSED */
19906 int
19907 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19908     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19909 {
19910 	queue_t		*q1 = q;
19911 	char 		*cp;
19912 	char		interf_name[LIFNAMSIZ];
19913 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
19914 
19915 	if (!q->q_next) {
19916 		ip1dbg((
19917 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
19918 		return (EINVAL);
19919 	}
19920 
19921 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
19922 		return (EALREADY);
19923 
19924 	do {
19925 		q1 = q1->q_next;
19926 	} while (q1->q_next);
19927 	cp = q1->q_qinfo->qi_minfo->mi_idname;
19928 	(void) sprintf(interf_name, "%s%d", cp, ppa);
19929 
19930 	/*
19931 	 * Here we are not going to delay the ioack until after
19932 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
19933 	 * original ioctl message before sending the requests.
19934 	 */
19935 	return (ipif_set_values(q, mp, interf_name, &ppa));
19936 }
19937 
19938 /* ARGSUSED */
19939 int
19940 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19941     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19942 {
19943 	return (ENXIO);
19944 }
19945 
19946 /*
19947  * Net and subnet broadcast ire's are now specific to the particular
19948  * physical interface (ill) and not to any one locigal interface (ipif).
19949  * However, if a particular logical interface is being taken down, it's
19950  * associated ire's will be taken down as well.  Hence, when we go to
19951  * take down or change the local address, broadcast address or netmask
19952  * of a specific logical interface, we must check to make sure that we
19953  * have valid net and subnet broadcast ire's for the other logical
19954  * interfaces which may have been shared with the logical interface
19955  * being brought down or changed.
19956  *
19957  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
19958  * is tied to the first interface coming UP. If that ipif is going down,
19959  * we need to recreate them on the next valid ipif.
19960  *
19961  * Note: assume that the ipif passed in is still up so that it's IRE
19962  * entries are still valid.
19963  */
19964 static void
19965 ipif_check_bcast_ires(ipif_t *test_ipif)
19966 {
19967 	ipif_t	*ipif;
19968 	ire_t	*test_subnet_ire, *test_net_ire;
19969 	ire_t	*test_allzero_ire, *test_allone_ire;
19970 	ire_t	*ire_array[12];
19971 	ire_t	**irep = &ire_array[0];
19972 	ire_t	**irep1;
19973 
19974 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
19975 	ipaddr_t test_net_addr, test_subnet_addr;
19976 	ipaddr_t test_net_mask, test_subnet_mask;
19977 	boolean_t need_net_bcast_ire = B_FALSE;
19978 	boolean_t need_subnet_bcast_ire = B_FALSE;
19979 	boolean_t allzero_bcast_ire_created = B_FALSE;
19980 	boolean_t allone_bcast_ire_created = B_FALSE;
19981 	boolean_t net_bcast_ire_created = B_FALSE;
19982 	boolean_t subnet_bcast_ire_created = B_FALSE;
19983 
19984 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
19985 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
19986 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
19987 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
19988 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
19989 
19990 	ASSERT(!test_ipif->ipif_isv6);
19991 	ASSERT(IAM_WRITER_IPIF(test_ipif));
19992 
19993 	/*
19994 	 * No broadcast IREs for the LOOPBACK interface
19995 	 * or others such as point to point and IPIF_NOXMIT.
19996 	 */
19997 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
19998 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
19999 		return;
20000 
20001 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
20002 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20003 
20004 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
20005 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20006 
20007 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
20008 	test_subnet_mask = test_ipif->ipif_net_mask;
20009 
20010 	/*
20011 	 * If no net mask set, assume the default based on net class.
20012 	 */
20013 	if (test_subnet_mask == 0)
20014 		test_subnet_mask = test_net_mask;
20015 
20016 	/*
20017 	 * Check if there is a network broadcast ire associated with this ipif
20018 	 */
20019 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
20020 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
20021 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20022 
20023 	/*
20024 	 * Check if there is a subnet broadcast IRE associated with this ipif
20025 	 */
20026 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
20027 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
20028 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20029 
20030 	/*
20031 	 * No broadcast ire's associated with this ipif.
20032 	 */
20033 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
20034 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
20035 		return;
20036 	}
20037 
20038 	/*
20039 	 * We have established which bcast ires have to be replaced.
20040 	 * Next we try to locate ipifs that match there ires.
20041 	 * The rules are simple: If we find an ipif that matches on the subnet
20042 	 * address it will also match on the net address, the allzeros and
20043 	 * allones address. Any ipif that matches only on the net address will
20044 	 * also match the allzeros and allones addresses.
20045 	 * The other criterion is the ipif_flags. We look for non-deprecated
20046 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
20047 	 * ipifs with check_flags matching (deprecated, etc) are used only
20048 	 * if good ipifs are not available. While looping, we save existing
20049 	 * deprecated ipifs as backup_ipif.
20050 	 * We loop through all the ipifs for this ill looking for ipifs
20051 	 * whose broadcast addr match the ipif passed in, but do not have
20052 	 * their own broadcast ires. For creating 0.0.0.0 and
20053 	 * 255.255.255.255 we just need an ipif on this ill to create.
20054 	 */
20055 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
20056 	    ipif = ipif->ipif_next) {
20057 
20058 		ASSERT(!ipif->ipif_isv6);
20059 		/*
20060 		 * Already checked the ipif passed in.
20061 		 */
20062 		if (ipif == test_ipif) {
20063 			continue;
20064 		}
20065 
20066 		/*
20067 		 * We only need to recreate broadcast ires if another ipif in
20068 		 * the same zone uses them. The new ires must be created in the
20069 		 * same zone.
20070 		 */
20071 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20072 			continue;
20073 		}
20074 
20075 		/*
20076 		 * Only interested in logical interfaces with valid local
20077 		 * addresses or with the ability to broadcast.
20078 		 */
20079 		if ((ipif->ipif_subnet == 0) ||
20080 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20081 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20082 		    !(ipif->ipif_flags & IPIF_UP)) {
20083 			continue;
20084 		}
20085 		/*
20086 		 * Check if there is a net broadcast ire for this
20087 		 * net address.  If it turns out that the ipif we are
20088 		 * about to take down owns this ire, we must make a
20089 		 * new one because it is potentially going away.
20090 		 */
20091 		if (test_net_ire && (!net_bcast_ire_created)) {
20092 			net_mask = ip_net_mask(ipif->ipif_subnet);
20093 			net_addr = net_mask & ipif->ipif_subnet;
20094 			if (net_addr == test_net_addr) {
20095 				need_net_bcast_ire = B_TRUE;
20096 				/*
20097 				 * Use DEPRECATED ipif only if no good
20098 				 * ires are available. subnet_addr is
20099 				 * a better match than net_addr.
20100 				 */
20101 				if ((ipif->ipif_flags & check_flags) &&
20102 				    (backup_ipif_net == NULL)) {
20103 					backup_ipif_net = ipif;
20104 				}
20105 			}
20106 		}
20107 		/*
20108 		 * Check if there is a subnet broadcast ire for this
20109 		 * net address.  If it turns out that the ipif we are
20110 		 * about to take down owns this ire, we must make a
20111 		 * new one because it is potentially going away.
20112 		 */
20113 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20114 			subnet_mask = ipif->ipif_net_mask;
20115 			subnet_addr = ipif->ipif_subnet;
20116 			if (subnet_addr == test_subnet_addr) {
20117 				need_subnet_bcast_ire = B_TRUE;
20118 				if ((ipif->ipif_flags & check_flags) &&
20119 				    (backup_ipif_subnet == NULL)) {
20120 					backup_ipif_subnet = ipif;
20121 				}
20122 			}
20123 		}
20124 
20125 
20126 		/* Short circuit here if this ipif is deprecated */
20127 		if (ipif->ipif_flags & check_flags) {
20128 			if ((test_allzero_ire != NULL) &&
20129 			    (!allzero_bcast_ire_created) &&
20130 			    (backup_ipif_allzeros == NULL)) {
20131 				backup_ipif_allzeros = ipif;
20132 			}
20133 			if ((test_allone_ire != NULL) &&
20134 			    (!allone_bcast_ire_created) &&
20135 			    (backup_ipif_allones == NULL)) {
20136 				backup_ipif_allones = ipif;
20137 			}
20138 			continue;
20139 		}
20140 
20141 		/*
20142 		 * Found an ipif which has the same broadcast ire as the
20143 		 * ipif passed in and the ipif passed in "owns" the ire.
20144 		 * Create new broadcast ire's for this broadcast addr.
20145 		 */
20146 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20147 			irep = ire_create_bcast(ipif, net_addr, irep);
20148 			irep = ire_create_bcast(ipif,
20149 			    ~net_mask | net_addr, irep);
20150 			net_bcast_ire_created = B_TRUE;
20151 		}
20152 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20153 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20154 			irep = ire_create_bcast(ipif,
20155 			    ~subnet_mask | subnet_addr, irep);
20156 			subnet_bcast_ire_created = B_TRUE;
20157 		}
20158 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20159 			irep = ire_create_bcast(ipif, 0, irep);
20160 			allzero_bcast_ire_created = B_TRUE;
20161 		}
20162 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20163 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20164 			allone_bcast_ire_created = B_TRUE;
20165 		}
20166 		/*
20167 		 * Once we have created all the appropriate ires, we
20168 		 * just break out of this loop to add what we have created.
20169 		 * This has been indented similar to ire_match_args for
20170 		 * readability.
20171 		 */
20172 		if (((test_net_ire == NULL) ||
20173 			(net_bcast_ire_created)) &&
20174 		    ((test_subnet_ire == NULL) ||
20175 			(subnet_bcast_ire_created)) &&
20176 		    ((test_allzero_ire == NULL) ||
20177 			(allzero_bcast_ire_created)) &&
20178 		    ((test_allone_ire == NULL) ||
20179 			(allone_bcast_ire_created))) {
20180 			break;
20181 		}
20182 	}
20183 
20184 	/*
20185 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20186 	 * exist. 6 pairs of bcast ires are needed.
20187 	 * Note - the old ires are deleted in ipif_down.
20188 	 */
20189 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20190 		ipif = backup_ipif_net;
20191 		irep = ire_create_bcast(ipif, net_addr, irep);
20192 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20193 		net_bcast_ire_created = B_TRUE;
20194 	}
20195 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20196 	    backup_ipif_subnet) {
20197 		ipif = backup_ipif_subnet;
20198 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20199 		irep = ire_create_bcast(ipif,
20200 		    ~subnet_mask | subnet_addr, irep);
20201 		subnet_bcast_ire_created = B_TRUE;
20202 	}
20203 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20204 	    backup_ipif_allzeros) {
20205 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20206 		allzero_bcast_ire_created = B_TRUE;
20207 	}
20208 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20209 	    backup_ipif_allones) {
20210 		irep = ire_create_bcast(backup_ipif_allones,
20211 		    INADDR_BROADCAST, irep);
20212 		allone_bcast_ire_created = B_TRUE;
20213 	}
20214 
20215 	/*
20216 	 * If we can't create all of them, don't add any of them.
20217 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20218 	 * always have a non-loopback copy and loopback copy
20219 	 * for a given address.
20220 	 */
20221 	for (irep1 = irep; irep1 > ire_array; ) {
20222 		irep1--;
20223 		if (*irep1 == NULL) {
20224 			ip0dbg(("ipif_check_bcast_ires: can't create "
20225 			    "IRE_BROADCAST, memory allocation failure\n"));
20226 			while (irep > ire_array) {
20227 				irep--;
20228 				if (*irep != NULL)
20229 					ire_delete(*irep);
20230 			}
20231 			goto bad;
20232 		}
20233 	}
20234 	for (irep1 = irep; irep1 > ire_array; ) {
20235 		int error;
20236 
20237 		irep1--;
20238 		error = ire_add(irep1, NULL, NULL, NULL);
20239 		if (error == 0) {
20240 			ire_refrele(*irep1);		/* Held in ire_add */
20241 		}
20242 	}
20243 bad:
20244 	if (test_allzero_ire != NULL)
20245 		ire_refrele(test_allzero_ire);
20246 	if (test_allone_ire != NULL)
20247 		ire_refrele(test_allone_ire);
20248 	if (test_net_ire != NULL)
20249 		ire_refrele(test_net_ire);
20250 	if (test_subnet_ire != NULL)
20251 		ire_refrele(test_subnet_ire);
20252 }
20253 
20254 /*
20255  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20256  * from lifr_flags and the name from lifr_name.
20257  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20258  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20259  * Returns EINPROGRESS when mp has been consumed by queueing it on
20260  * ill_pending_mp and the ioctl will complete in ip_rput.
20261  */
20262 /* ARGSUSED */
20263 int
20264 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20265     ip_ioctl_cmd_t *ipip, void *if_req)
20266 {
20267 	int	err;
20268 	ill_t	*ill;
20269 	struct lifreq *lifr = (struct lifreq *)if_req;
20270 
20271 	ASSERT(ipif != NULL);
20272 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20273 	ASSERT(q->q_next != NULL);
20274 
20275 	ill = (ill_t *)q->q_ptr;
20276 	/*
20277 	 * If we are not writer on 'q' then this interface exists already
20278 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20279 	 * So return EALREADY
20280 	 */
20281 	if (ill != ipif->ipif_ill)
20282 		return (EALREADY);
20283 
20284 	if (ill->ill_name[0] != '\0')
20285 		return (EALREADY);
20286 
20287 	/*
20288 	 * Set all the flags. Allows all kinds of override. Provide some
20289 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20290 	 * unless there is either multicast/broadcast support in the driver
20291 	 * or it is a pt-pt link.
20292 	 */
20293 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20294 		/* Meaningless to IP thus don't allow them to be set. */
20295 		ip1dbg(("ip_setname: EINVAL 1\n"));
20296 		return (EINVAL);
20297 	}
20298 	/*
20299 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20300 	 * ill_bcast_addr_length info.
20301 	 */
20302 	if (!ill->ill_needs_attach &&
20303 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20304 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20305 	    ill->ill_bcast_addr_length == 0)) {
20306 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20307 		ip1dbg(("ip_setname: EINVAL 2\n"));
20308 		return (EINVAL);
20309 	}
20310 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20311 	    ((lifr->lifr_flags & IFF_IPV6) ||
20312 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20313 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20314 		ip1dbg(("ip_setname: EINVAL 3\n"));
20315 		return (EINVAL);
20316 	}
20317 	if (lifr->lifr_flags & IFF_UP) {
20318 		/* Can only be set with SIOCSLIFFLAGS */
20319 		ip1dbg(("ip_setname: EINVAL 4\n"));
20320 		return (EINVAL);
20321 	}
20322 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20323 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20324 		ip1dbg(("ip_setname: EINVAL 5\n"));
20325 		return (EINVAL);
20326 	}
20327 	/*
20328 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20329 	 */
20330 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20331 	    !(lifr->lifr_flags & IFF_IPV6) &&
20332 	    !(ipif->ipif_isv6)) {
20333 		ip1dbg(("ip_setname: EINVAL 6\n"));
20334 		return (EINVAL);
20335 	}
20336 
20337 	/*
20338 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20339 	 * we have all the flags here. So, we assign rather than we OR.
20340 	 * We can't OR the flags here because we don't want to set
20341 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20342 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20343 	 * on lifr_flags value here.
20344 	 */
20345 	/*
20346 	 * This ill has not been inserted into the global list.
20347 	 * So we are still single threaded and don't need any lock
20348 	 */
20349 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20350 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20351 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20352 
20353 	/* We started off as V4. */
20354 	if (ill->ill_flags & ILLF_IPV6) {
20355 		ill->ill_phyint->phyint_illv6 = ill;
20356 		ill->ill_phyint->phyint_illv4 = NULL;
20357 	}
20358 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20359 	return (err);
20360 }
20361 
20362 /* ARGSUSED */
20363 int
20364 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20365     ip_ioctl_cmd_t *ipip, void *if_req)
20366 {
20367 	/*
20368 	 * ill_phyint_reinit merged the v4 and v6 into a single
20369 	 * ipsq. Could also have become part of a ipmp group in the
20370 	 * process, and we might not have been able to complete the
20371 	 * slifname in ipif_set_values, if we could not become
20372 	 * exclusive.  If so restart it here
20373 	 */
20374 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20375 }
20376 
20377 /*
20378  * Return a pointer to the ipif which matches the index, IP version type and
20379  * zoneid.
20380  */
20381 ipif_t *
20382 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20383     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20384 {
20385 	ill_t	*ill;
20386 	ipsq_t  *ipsq;
20387 	phyint_t *phyi;
20388 	ipif_t	*ipif;
20389 
20390 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20391 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20392 
20393 	if (err != NULL)
20394 		*err = 0;
20395 
20396 	/*
20397 	 * Indexes are stored in the phyint - a common structure
20398 	 * to both IPv4 and IPv6.
20399 	 */
20400 
20401 	rw_enter(&ill_g_lock, RW_READER);
20402 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20403 	    (void *) &index, NULL);
20404 	if (phyi != NULL) {
20405 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20406 		if (ill == NULL) {
20407 			rw_exit(&ill_g_lock);
20408 			if (err != NULL)
20409 				*err = ENXIO;
20410 			return (NULL);
20411 		}
20412 		GRAB_CONN_LOCK(q);
20413 		mutex_enter(&ill->ill_lock);
20414 		if (ILL_CAN_LOOKUP(ill)) {
20415 			for (ipif = ill->ill_ipif; ipif != NULL;
20416 			    ipif = ipif->ipif_next) {
20417 				if (IPIF_CAN_LOOKUP(ipif) &&
20418 				    (zoneid == ALL_ZONES ||
20419 				    zoneid == ipif->ipif_zoneid)) {
20420 					ipif_refhold_locked(ipif);
20421 					mutex_exit(&ill->ill_lock);
20422 					RELEASE_CONN_LOCK(q);
20423 					rw_exit(&ill_g_lock);
20424 					return (ipif);
20425 				}
20426 			}
20427 		} else if (ILL_CAN_WAIT(ill, q)) {
20428 			ipsq = ill->ill_phyint->phyint_ipsq;
20429 			mutex_enter(&ipsq->ipsq_lock);
20430 			rw_exit(&ill_g_lock);
20431 			mutex_exit(&ill->ill_lock);
20432 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20433 			mutex_exit(&ipsq->ipsq_lock);
20434 			RELEASE_CONN_LOCK(q);
20435 			*err = EINPROGRESS;
20436 			return (NULL);
20437 		}
20438 		mutex_exit(&ill->ill_lock);
20439 		RELEASE_CONN_LOCK(q);
20440 	}
20441 	rw_exit(&ill_g_lock);
20442 	if (err != NULL)
20443 		*err = ENXIO;
20444 	return (NULL);
20445 }
20446 
20447 typedef struct conn_change_s {
20448 	uint_t cc_old_ifindex;
20449 	uint_t cc_new_ifindex;
20450 } conn_change_t;
20451 
20452 /*
20453  * ipcl_walk function for changing interface index.
20454  */
20455 static void
20456 conn_change_ifindex(conn_t *connp, caddr_t arg)
20457 {
20458 	conn_change_t *connc;
20459 	uint_t old_ifindex;
20460 	uint_t new_ifindex;
20461 	int i;
20462 	ilg_t *ilg;
20463 
20464 	connc = (conn_change_t *)arg;
20465 	old_ifindex = connc->cc_old_ifindex;
20466 	new_ifindex = connc->cc_new_ifindex;
20467 
20468 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20469 		connp->conn_orig_bound_ifindex = new_ifindex;
20470 
20471 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20472 		connp->conn_orig_multicast_ifindex = new_ifindex;
20473 
20474 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20475 		connp->conn_orig_xmit_ifindex = new_ifindex;
20476 
20477 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20478 		ilg = &connp->conn_ilg[i];
20479 		if (ilg->ilg_orig_ifindex == old_ifindex)
20480 			ilg->ilg_orig_ifindex = new_ifindex;
20481 	}
20482 }
20483 
20484 /*
20485  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20486  * to new_index if it matches the old_index.
20487  *
20488  * Failovers typically happen within a group of ills. But somebody
20489  * can remove an ill from the group after a failover happened. If
20490  * we are setting the ifindex after this, we potentially need to
20491  * look at all the ills rather than just the ones in the group.
20492  * We cut down the work by looking at matching ill_net_types
20493  * and ill_types as we could not possibly grouped them together.
20494  */
20495 static void
20496 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20497 {
20498 	ill_t *ill;
20499 	ipif_t *ipif;
20500 	uint_t old_ifindex;
20501 	uint_t new_ifindex;
20502 	ilm_t *ilm;
20503 	ill_walk_context_t ctx;
20504 
20505 	old_ifindex = connc->cc_old_ifindex;
20506 	new_ifindex = connc->cc_new_ifindex;
20507 
20508 	rw_enter(&ill_g_lock, RW_READER);
20509 	ill = ILL_START_WALK_ALL(&ctx);
20510 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20511 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20512 			(ill_orig->ill_type != ill->ill_type)) {
20513 			continue;
20514 		}
20515 		for (ipif = ill->ill_ipif; ipif != NULL;
20516 				ipif = ipif->ipif_next) {
20517 			if (ipif->ipif_orig_ifindex == old_ifindex)
20518 				ipif->ipif_orig_ifindex = new_ifindex;
20519 		}
20520 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20521 			if (ilm->ilm_orig_ifindex == old_ifindex)
20522 				ilm->ilm_orig_ifindex = new_ifindex;
20523 		}
20524 	}
20525 	rw_exit(&ill_g_lock);
20526 }
20527 
20528 /*
20529  * We first need to ensure that the new index is unique, and
20530  * then carry the change across both v4 and v6 ill representation
20531  * of the physical interface.
20532  */
20533 /* ARGSUSED */
20534 int
20535 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20536     ip_ioctl_cmd_t *ipip, void *ifreq)
20537 {
20538 	ill_t		*ill;
20539 	ill_t		*ill_other;
20540 	phyint_t	*phyi;
20541 	int		old_index;
20542 	conn_change_t	connc;
20543 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20544 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20545 	uint_t	index;
20546 	ill_t	*ill_v4;
20547 	ill_t	*ill_v6;
20548 
20549 	if (ipip->ipi_cmd_type == IF_CMD)
20550 		index = ifr->ifr_index;
20551 	else
20552 		index = lifr->lifr_index;
20553 
20554 	/*
20555 	 * Only allow on physical interface. Also, index zero is illegal.
20556 	 *
20557 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20558 	 *
20559 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20560 	 *    implies a possible failback might have to happen. As failback
20561 	 *    depends on the old index, we should fail setting the index.
20562 	 *
20563 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20564 	 *    any addresses or multicast memberships are failed over to
20565 	 *    a non-STANDBY interface. As failback depends on the old
20566 	 *    index, we should fail setting the index for this case also.
20567 	 *
20568 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20569 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20570 	 */
20571 	ill = ipif->ipif_ill;
20572 	phyi = ill->ill_phyint;
20573 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20574 	    ipif->ipif_id != 0 || index == 0) {
20575 		return (EINVAL);
20576 	}
20577 	old_index = phyi->phyint_ifindex;
20578 
20579 	/* If the index is not changing, no work to do */
20580 	if (old_index == index)
20581 		return (0);
20582 
20583 	/*
20584 	 * Use ill_lookup_on_ifindex to determine if the
20585 	 * new index is unused and if so allow the change.
20586 	 */
20587 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20588 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20589 	if (ill_v6 != NULL || ill_v4 != NULL) {
20590 		if (ill_v4 != NULL)
20591 			ill_refrele(ill_v4);
20592 		if (ill_v6 != NULL)
20593 			ill_refrele(ill_v6);
20594 		return (EBUSY);
20595 	}
20596 
20597 	/*
20598 	 * The new index is unused. Set it in the phyint.
20599 	 * Locate the other ill so that we can send a routing
20600 	 * sockets message.
20601 	 */
20602 	if (ill->ill_isv6) {
20603 		ill_other = phyi->phyint_illv4;
20604 	} else {
20605 		ill_other = phyi->phyint_illv6;
20606 	}
20607 
20608 	phyi->phyint_ifindex = index;
20609 
20610 	connc.cc_old_ifindex = old_index;
20611 	connc.cc_new_ifindex = index;
20612 	ip_change_ifindex(ill, &connc);
20613 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20614 
20615 	/* Send the routing sockets message */
20616 	ip_rts_ifmsg(ipif);
20617 	if (ill_other != NULL)
20618 		ip_rts_ifmsg(ill_other->ill_ipif);
20619 
20620 	return (0);
20621 }
20622 
20623 /* ARGSUSED */
20624 int
20625 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20626     ip_ioctl_cmd_t *ipip, void *ifreq)
20627 {
20628 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20629 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20630 
20631 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20632 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20633 	/* Get the interface index */
20634 	if (ipip->ipi_cmd_type == IF_CMD) {
20635 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20636 	} else {
20637 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20638 	}
20639 	return (0);
20640 }
20641 
20642 /* ARGSUSED */
20643 int
20644 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20645     ip_ioctl_cmd_t *ipip, void *ifreq)
20646 {
20647 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20648 
20649 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20650 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20651 	/* Get the interface zone */
20652 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20653 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20654 	return (0);
20655 }
20656 
20657 /*
20658  * Set the zoneid of an interface.
20659  */
20660 /* ARGSUSED */
20661 int
20662 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20663     ip_ioctl_cmd_t *ipip, void *ifreq)
20664 {
20665 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20666 	int err = 0;
20667 	boolean_t need_up = B_FALSE;
20668 	zone_t *zptr;
20669 	zone_status_t status;
20670 	zoneid_t zoneid;
20671 
20672 	/* cannot assign instance zero to a non-global zone */
20673 	if (ipif->ipif_id == 0)
20674 		return (ENOTSUP);
20675 
20676 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20677 	zoneid = lifr->lifr_zoneid;
20678 
20679 	/*
20680 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
20681 	 * the event of a race with the zone shutdown processing, since IP
20682 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
20683 	 * interface will be cleaned up even if the zone is shut down
20684 	 * immediately after the status check. If the interface can't be brought
20685 	 * down right away, and the zone is shut down before the restart
20686 	 * function is called, we resolve the possible races by rechecking the
20687 	 * zone status in the restart function.
20688 	 */
20689 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
20690 		return (EINVAL);
20691 	status = zone_status_get(zptr);
20692 	zone_rele(zptr);
20693 
20694 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
20695 		return (EINVAL);
20696 
20697 	if (ipif->ipif_flags & IPIF_UP) {
20698 		/*
20699 		 * If the interface is already marked up,
20700 		 * we call ipif_down which will take care
20701 		 * of ditching any IREs that have been set
20702 		 * up based on the old interface address.
20703 		 */
20704 		err = ipif_logical_down(ipif, q, mp);
20705 		if (err == EINPROGRESS)
20706 			return (err);
20707 		ipif_down_tail(ipif);
20708 		need_up = B_TRUE;
20709 	}
20710 
20711 	err = ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, need_up);
20712 	return (err);
20713 }
20714 
20715 static int
20716 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
20717     queue_t *q, mblk_t *mp, boolean_t need_up)
20718 {
20719 	int	err = 0;
20720 
20721 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
20722 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20723 
20724 	/* Set the new zone id. */
20725 	ipif->ipif_zoneid = zoneid;
20726 
20727 	/* Update sctp list */
20728 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
20729 
20730 	if (need_up) {
20731 		/*
20732 		 * Now bring the interface back up.  If this
20733 		 * is the only IPIF for the ILL, ipif_up
20734 		 * will have to re-bind to the device, so
20735 		 * we may get back EINPROGRESS, in which
20736 		 * case, this IOCTL will get completed in
20737 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
20738 		 */
20739 		err = ipif_up(ipif, q, mp);
20740 	}
20741 	return (err);
20742 }
20743 
20744 /* ARGSUSED */
20745 int
20746 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20747     ip_ioctl_cmd_t *ipip, void *if_req)
20748 {
20749 	struct lifreq *lifr = (struct lifreq *)if_req;
20750 	zoneid_t zoneid;
20751 	zone_t *zptr;
20752 	zone_status_t status;
20753 
20754 	ASSERT(ipif->ipif_id != 0);
20755 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20756 	zoneid = lifr->lifr_zoneid;
20757 
20758 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
20759 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20760 
20761 	/*
20762 	 * We recheck the zone status to resolve the following race condition:
20763 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
20764 	 * 2) hme0:1 is up and can't be brought down right away;
20765 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
20766 	 * 3) zone "myzone" is halted; the zone status switches to
20767 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
20768 	 * the interfaces to remove - hme0:1 is not returned because it's not
20769 	 * yet in "myzone", so it won't be removed;
20770 	 * 4) the restart function for SIOCSLIFZONE is called; without the
20771 	 * status check here, we would have hme0:1 in "myzone" after it's been
20772 	 * destroyed.
20773 	 * Note that if the status check fails, we need to bring the interface
20774 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
20775 	 * ipif_up_done[_v6]().
20776 	 */
20777 	status = ZONE_IS_UNINITIALIZED;
20778 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
20779 		status = zone_status_get(zptr);
20780 		zone_rele(zptr);
20781 	}
20782 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
20783 		if (ipif->ipif_isv6) {
20784 			(void) ipif_up_done_v6(ipif);
20785 		} else {
20786 			(void) ipif_up_done(ipif);
20787 		}
20788 		return (EINVAL);
20789 	}
20790 
20791 	ipif_down_tail(ipif);
20792 
20793 	return (ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, B_TRUE));
20794 }
20795 
20796 /* ARGSUSED */
20797 int
20798 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20799 	ip_ioctl_cmd_t *ipip, void *ifreq)
20800 {
20801 	struct lifreq	*lifr = ifreq;
20802 
20803 	ASSERT(q->q_next == NULL);
20804 	ASSERT(CONN_Q(q));
20805 
20806 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
20807 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20808 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
20809 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
20810 
20811 	return (0);
20812 }
20813 
20814 
20815 /* Find the previous ILL in this usesrc group */
20816 static ill_t *
20817 ill_prev_usesrc(ill_t *uill)
20818 {
20819 	ill_t *ill;
20820 
20821 	for (ill = uill->ill_usesrc_grp_next;
20822 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
20823 	    ill = ill->ill_usesrc_grp_next)
20824 		/* do nothing */;
20825 	return (ill);
20826 }
20827 
20828 /*
20829  * Release all members of the usesrc group. This routine is called
20830  * from ill_delete when the interface being unplumbed is the
20831  * group head.
20832  */
20833 static void
20834 ill_disband_usesrc_group(ill_t *uill)
20835 {
20836 	ill_t *next_ill, *tmp_ill;
20837 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
20838 	next_ill = uill->ill_usesrc_grp_next;
20839 
20840 	do {
20841 		ASSERT(next_ill != NULL);
20842 		tmp_ill = next_ill->ill_usesrc_grp_next;
20843 		ASSERT(tmp_ill != NULL);
20844 		next_ill->ill_usesrc_grp_next = NULL;
20845 		next_ill->ill_usesrc_ifindex = 0;
20846 		next_ill = tmp_ill;
20847 	} while (next_ill->ill_usesrc_ifindex != 0);
20848 	uill->ill_usesrc_grp_next = NULL;
20849 }
20850 
20851 /*
20852  * Remove the client usesrc ILL from the list and relink to a new list
20853  */
20854 int
20855 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
20856 {
20857 	ill_t *ill, *tmp_ill;
20858 
20859 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
20860 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
20861 
20862 	/*
20863 	 * Check if the usesrc client ILL passed in is not already
20864 	 * in use as a usesrc ILL i.e one whose source address is
20865 	 * in use OR a usesrc ILL is not already in use as a usesrc
20866 	 * client ILL
20867 	 */
20868 	if ((ucill->ill_usesrc_ifindex == 0) ||
20869 	    (uill->ill_usesrc_ifindex != 0)) {
20870 		return (-1);
20871 	}
20872 
20873 	ill = ill_prev_usesrc(ucill);
20874 	ASSERT(ill->ill_usesrc_grp_next != NULL);
20875 
20876 	/* Remove from the current list */
20877 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
20878 		/* Only two elements in the list */
20879 		ASSERT(ill->ill_usesrc_ifindex == 0);
20880 		ill->ill_usesrc_grp_next = NULL;
20881 	} else {
20882 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
20883 	}
20884 
20885 	if (ifindex == 0) {
20886 		ucill->ill_usesrc_ifindex = 0;
20887 		ucill->ill_usesrc_grp_next = NULL;
20888 		return (0);
20889 	}
20890 
20891 	ucill->ill_usesrc_ifindex = ifindex;
20892 	tmp_ill = uill->ill_usesrc_grp_next;
20893 	uill->ill_usesrc_grp_next = ucill;
20894 	ucill->ill_usesrc_grp_next =
20895 	    (tmp_ill != NULL) ? tmp_ill : uill;
20896 	return (0);
20897 }
20898 
20899 /*
20900  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
20901  * ip.c for locking details.
20902  */
20903 /* ARGSUSED */
20904 int
20905 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20906     ip_ioctl_cmd_t *ipip, void *ifreq)
20907 {
20908 	struct lifreq *lifr = (struct lifreq *)ifreq;
20909 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
20910 	    ill_flag_changed = B_FALSE;
20911 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
20912 	int err = 0, ret;
20913 	uint_t ifindex;
20914 	phyint_t *us_phyint, *us_cli_phyint;
20915 	ipsq_t *ipsq = NULL;
20916 
20917 	ASSERT(IAM_WRITER_IPIF(ipif));
20918 	ASSERT(q->q_next == NULL);
20919 	ASSERT(CONN_Q(q));
20920 
20921 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
20922 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
20923 
20924 	ASSERT(us_cli_phyint != NULL);
20925 
20926 	/*
20927 	 * If the client ILL is being used for IPMP, abort.
20928 	 * Note, this can be done before ipsq_try_enter since we are already
20929 	 * exclusive on this ILL
20930 	 */
20931 	if ((us_cli_phyint->phyint_groupname != NULL) ||
20932 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
20933 		return (EINVAL);
20934 	}
20935 
20936 	ifindex = lifr->lifr_index;
20937 	if (ifindex == 0) {
20938 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
20939 			/* non usesrc group interface, nothing to reset */
20940 			return (0);
20941 		}
20942 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
20943 		/* valid reset request */
20944 		reset_flg = B_TRUE;
20945 	}
20946 
20947 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
20948 	    ip_process_ioctl, &err);
20949 
20950 	if (usesrc_ill == NULL) {
20951 		return (err);
20952 	}
20953 
20954 	/*
20955 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
20956 	 * group nor can either of the interfaces be used for standy. So
20957 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
20958 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
20959 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
20960 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
20961 	 * the usesrc_cli_ill
20962 	 */
20963 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
20964 	    NEW_OP, B_TRUE);
20965 	if (ipsq == NULL) {
20966 		err = EINPROGRESS;
20967 		/* Operation enqueued on the ipsq of the usesrc ILL */
20968 		goto done;
20969 	}
20970 
20971 	/* Check if the usesrc_ill is used for IPMP */
20972 	us_phyint = usesrc_ill->ill_phyint;
20973 	if ((us_phyint->phyint_groupname != NULL) ||
20974 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
20975 		err = EINVAL;
20976 		goto done;
20977 	}
20978 
20979 	/*
20980 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
20981 	 * already a client then return EINVAL
20982 	 */
20983 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
20984 		err = EINVAL;
20985 		goto done;
20986 	}
20987 
20988 	/*
20989 	 * If the ill_usesrc_ifindex field is already set to what it needs to
20990 	 * be then this is a duplicate operation.
20991 	 */
20992 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
20993 		err = 0;
20994 		goto done;
20995 	}
20996 
20997 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
20998 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
20999 	    usesrc_ill->ill_isv6));
21000 
21001 	/*
21002 	 * The next step ensures that no new ires will be created referencing
21003 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
21004 	 * we go through an ire walk deleting all ire caches that reference
21005 	 * the client ill. New ires referencing the client ill that are added
21006 	 * to the ire table before the ILL_CHANGING flag is set, will be
21007 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
21008 	 * the client ill while the ILL_CHANGING flag is set will be failed
21009 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
21010 	 * checks (under the ill_g_usesrc_lock) that the ire being added
21011 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
21012 	 * belong to the same usesrc group.
21013 	 */
21014 	mutex_enter(&usesrc_cli_ill->ill_lock);
21015 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
21016 	mutex_exit(&usesrc_cli_ill->ill_lock);
21017 	ill_flag_changed = B_TRUE;
21018 
21019 	if (ipif->ipif_isv6)
21020 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21021 		    ALL_ZONES);
21022 	else
21023 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21024 		    ALL_ZONES);
21025 
21026 	/*
21027 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
21028 	 * and the ill_usesrc_ifindex fields
21029 	 */
21030 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
21031 
21032 	if (reset_flg) {
21033 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
21034 		if (ret != 0) {
21035 			err = EINVAL;
21036 		}
21037 		rw_exit(&ill_g_usesrc_lock);
21038 		goto done;
21039 	}
21040 
21041 	/*
21042 	 * Four possibilities to consider:
21043 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
21044 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
21045 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
21046 	 * 4. Both are part of their respective usesrc groups
21047 	 */
21048 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
21049 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21050 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
21051 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21052 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21053 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
21054 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
21055 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21056 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21057 		/* Insert at head of list */
21058 		usesrc_cli_ill->ill_usesrc_grp_next =
21059 		    usesrc_ill->ill_usesrc_grp_next;
21060 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21061 	} else {
21062 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
21063 		    ifindex);
21064 		if (ret != 0)
21065 			err = EINVAL;
21066 	}
21067 	rw_exit(&ill_g_usesrc_lock);
21068 
21069 done:
21070 	if (ill_flag_changed) {
21071 		mutex_enter(&usesrc_cli_ill->ill_lock);
21072 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21073 		mutex_exit(&usesrc_cli_ill->ill_lock);
21074 	}
21075 	if (ipsq != NULL)
21076 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21077 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21078 	ill_refrele(usesrc_ill);
21079 	return (err);
21080 }
21081 
21082 /*
21083  * comparison function used by avl.
21084  */
21085 static int
21086 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21087 {
21088 
21089 	uint_t index;
21090 
21091 	ASSERT(phyip != NULL && index_ptr != NULL);
21092 
21093 	index = *((uint_t *)index_ptr);
21094 	/*
21095 	 * let the phyint with the lowest index be on top.
21096 	 */
21097 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21098 		return (1);
21099 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21100 		return (-1);
21101 	return (0);
21102 }
21103 
21104 /*
21105  * comparison function used by avl.
21106  */
21107 static int
21108 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21109 {
21110 	ill_t *ill;
21111 	int res = 0;
21112 
21113 	ASSERT(phyip != NULL && name_ptr != NULL);
21114 
21115 	if (((phyint_t *)phyip)->phyint_illv4)
21116 		ill = ((phyint_t *)phyip)->phyint_illv4;
21117 	else
21118 		ill = ((phyint_t *)phyip)->phyint_illv6;
21119 	ASSERT(ill != NULL);
21120 
21121 	res = strcmp(ill->ill_name, (char *)name_ptr);
21122 	if (res > 0)
21123 		return (1);
21124 	else if (res < 0)
21125 		return (-1);
21126 	return (0);
21127 }
21128 /*
21129  * This function is called from ill_delete when the ill is being
21130  * unplumbed. We remove the reference from the phyint and we also
21131  * free the phyint when there are no more references to it.
21132  */
21133 static void
21134 ill_phyint_free(ill_t *ill)
21135 {
21136 	phyint_t *phyi;
21137 	phyint_t *next_phyint;
21138 	ipsq_t *cur_ipsq;
21139 
21140 	ASSERT(ill->ill_phyint != NULL);
21141 
21142 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21143 	phyi = ill->ill_phyint;
21144 	ill->ill_phyint = NULL;
21145 	/*
21146 	 * ill_init allocates a phyint always to store the copy
21147 	 * of flags relevant to phyint. At that point in time, we could
21148 	 * not assign the name and hence phyint_illv4/v6 could not be
21149 	 * initialized. Later in ipif_set_values, we assign the name to
21150 	 * the ill, at which point in time we assign phyint_illv4/v6.
21151 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21152 	 */
21153 	if (ill->ill_flags & ILLF_IPV6) {
21154 		phyi->phyint_illv6 = NULL;
21155 	} else {
21156 		phyi->phyint_illv4 = NULL;
21157 	}
21158 	/*
21159 	 * ipif_down removes it from the group when the last ipif goes
21160 	 * down.
21161 	 */
21162 	ASSERT(ill->ill_group == NULL);
21163 
21164 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21165 		return;
21166 
21167 	/*
21168 	 * Make sure this phyint was put in the list.
21169 	 */
21170 	if (phyi->phyint_ifindex > 0) {
21171 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21172 		    phyi);
21173 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21174 		    phyi);
21175 	}
21176 	/*
21177 	 * remove phyint from the ipsq list.
21178 	 */
21179 	cur_ipsq = phyi->phyint_ipsq;
21180 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21181 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21182 	} else {
21183 		next_phyint = cur_ipsq->ipsq_phyint_list;
21184 		while (next_phyint != NULL) {
21185 			if (next_phyint->phyint_ipsq_next == phyi) {
21186 				next_phyint->phyint_ipsq_next =
21187 					phyi->phyint_ipsq_next;
21188 				break;
21189 			}
21190 			next_phyint = next_phyint->phyint_ipsq_next;
21191 		}
21192 		ASSERT(next_phyint != NULL);
21193 	}
21194 	IPSQ_DEC_REF(cur_ipsq);
21195 
21196 	if (phyi->phyint_groupname_len != 0) {
21197 		ASSERT(phyi->phyint_groupname != NULL);
21198 		mi_free(phyi->phyint_groupname);
21199 	}
21200 	mi_free(phyi);
21201 }
21202 
21203 /*
21204  * Attach the ill to the phyint structure which can be shared by both
21205  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21206  * function is called from ipif_set_values and ill_lookup_on_name (for
21207  * loopback) where we know the name of the ill. We lookup the ill and if
21208  * there is one present already with the name use that phyint. Otherwise
21209  * reuse the one allocated by ill_init.
21210  */
21211 static void
21212 ill_phyint_reinit(ill_t *ill)
21213 {
21214 	boolean_t isv6 = ill->ill_isv6;
21215 	phyint_t *phyi_old;
21216 	phyint_t *phyi;
21217 	avl_index_t where = 0;
21218 	ill_t	*ill_other = NULL;
21219 	ipsq_t	*ipsq;
21220 
21221 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21222 
21223 	phyi_old = ill->ill_phyint;
21224 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21225 	    phyi_old->phyint_illv6 == NULL));
21226 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21227 	    phyi_old->phyint_illv4 == NULL));
21228 	ASSERT(phyi_old->phyint_ifindex == 0);
21229 
21230 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21231 	    ill->ill_name, &where);
21232 
21233 	/*
21234 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21235 	 *    the global list of ills. So no other thread could have located
21236 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21237 	 * 2. Now locate the other protocol instance of this ill.
21238 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21239 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21240 	 *    of neither ill can change.
21241 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21242 	 *    other ill.
21243 	 * 5. Release all locks.
21244 	 */
21245 
21246 	/*
21247 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21248 	 * we are initializing IPv4.
21249 	 */
21250 	if (phyi != NULL) {
21251 		ill_other = (isv6) ? phyi->phyint_illv4 :
21252 		    phyi->phyint_illv6;
21253 		ASSERT(ill_other->ill_phyint != NULL);
21254 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21255 		    (!isv6 && ill_other->ill_isv6));
21256 		GRAB_ILL_LOCKS(ill, ill_other);
21257 		/*
21258 		 * We are potentially throwing away phyint_flags which
21259 		 * could be different from the one that we obtain from
21260 		 * ill_other->ill_phyint. But it is okay as we are assuming
21261 		 * that the state maintained within IP is correct.
21262 		 */
21263 		mutex_enter(&phyi->phyint_lock);
21264 		if (isv6) {
21265 			ASSERT(phyi->phyint_illv6 == NULL);
21266 			phyi->phyint_illv6 = ill;
21267 		} else {
21268 			ASSERT(phyi->phyint_illv4 == NULL);
21269 			phyi->phyint_illv4 = ill;
21270 		}
21271 		/*
21272 		 * This is a new ill, currently undergoing SLIFNAME
21273 		 * So we could not have joined an IPMP group until now.
21274 		 */
21275 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21276 		    phyi_old->phyint_groupname == NULL);
21277 
21278 		/*
21279 		 * This phyi_old is going away. Decref ipsq_refs and
21280 		 * assert it is zero. The ipsq itself will be freed in
21281 		 * ipsq_exit
21282 		 */
21283 		ipsq = phyi_old->phyint_ipsq;
21284 		IPSQ_DEC_REF(ipsq);
21285 		ASSERT(ipsq->ipsq_refs == 0);
21286 		/* Get the singleton phyint out of the ipsq list */
21287 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21288 		ipsq->ipsq_phyint_list = NULL;
21289 		phyi_old->phyint_illv4 = NULL;
21290 		phyi_old->phyint_illv6 = NULL;
21291 		mi_free(phyi_old);
21292 	} else {
21293 		mutex_enter(&ill->ill_lock);
21294 		/*
21295 		 * We don't need to acquire any lock, since
21296 		 * the ill is not yet visible globally  and we
21297 		 * have not yet released the ill_g_lock.
21298 		 */
21299 		phyi = phyi_old;
21300 		mutex_enter(&phyi->phyint_lock);
21301 		/* XXX We need a recovery strategy here. */
21302 		if (!phyint_assign_ifindex(phyi))
21303 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21304 
21305 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21306 		    (void *)phyi, where);
21307 
21308 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21309 		    &phyi->phyint_ifindex, &where);
21310 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21311 		    (void *)phyi, where);
21312 	}
21313 
21314 	/*
21315 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21316 	 * pending mp is not affected because that is per ill basis.
21317 	 */
21318 	ill->ill_phyint = phyi;
21319 
21320 	/*
21321 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21322 	 * We do this here as when the first ipif was allocated,
21323 	 * ipif_allocate does not know the right interface index.
21324 	 */
21325 
21326 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21327 	/*
21328 	 * Now that the phyint's ifindex has been assigned, complete the
21329 	 * remaining
21330 	 */
21331 	if (ill->ill_isv6) {
21332 		ill->ill_ip6_mib->ipv6IfIndex =
21333 		    ill->ill_phyint->phyint_ifindex;
21334 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21335 		    ill->ill_phyint->phyint_ifindex;
21336 	}
21337 
21338 	RELEASE_ILL_LOCKS(ill, ill_other);
21339 	mutex_exit(&phyi->phyint_lock);
21340 }
21341 
21342 /*
21343  * Notify any downstream modules of the name of this interface.
21344  * An M_IOCTL is used even though we don't expect a successful reply.
21345  * Any reply message from the driver (presumably an M_IOCNAK) will
21346  * eventually get discarded somewhere upstream.  The message format is
21347  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21348  * to IP.
21349  */
21350 static void
21351 ip_ifname_notify(ill_t *ill, queue_t *q)
21352 {
21353 	mblk_t *mp1, *mp2;
21354 	struct iocblk *iocp;
21355 	struct lifreq *lifr;
21356 
21357 	mp1 = mkiocb(SIOCSLIFNAME);
21358 	if (mp1 == NULL)
21359 		return;
21360 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21361 	if (mp2 == NULL) {
21362 		freeb(mp1);
21363 		return;
21364 	}
21365 
21366 	mp1->b_cont = mp2;
21367 	iocp = (struct iocblk *)mp1->b_rptr;
21368 	iocp->ioc_count = sizeof (struct lifreq);
21369 
21370 	lifr = (struct lifreq *)mp2->b_rptr;
21371 	mp2->b_wptr += sizeof (struct lifreq);
21372 	bzero(lifr, sizeof (struct lifreq));
21373 
21374 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21375 	lifr->lifr_ppa = ill->ill_ppa;
21376 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21377 
21378 	putnext(q, mp1);
21379 }
21380 
21381 static boolean_t ip_trash_timer_started = B_FALSE;
21382 
21383 static int
21384 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21385 {
21386 	int err;
21387 
21388 	/* Set the obsolete NDD per-interface forwarding name. */
21389 	err = ill_set_ndd_name(ill);
21390 	if (err != 0) {
21391 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21392 		    err);
21393 	}
21394 
21395 	/* Tell downstream modules where they are. */
21396 	ip_ifname_notify(ill, q);
21397 
21398 	/*
21399 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21400 	 * Error cases are ENOMEM ...
21401 	 */
21402 	err = ill_dl_phys(ill, ipif, mp, q);
21403 
21404 	/*
21405 	 * If there is no IRE expiration timer running, get one started.
21406 	 * igmp and mld timers will be triggered by the first multicast
21407 	 */
21408 	if (!ip_trash_timer_started) {
21409 		/*
21410 		 * acquire the lock and check again.
21411 		 */
21412 		mutex_enter(&ip_trash_timer_lock);
21413 		if (!ip_trash_timer_started) {
21414 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21415 			    MSEC_TO_TICK(ip_timer_interval));
21416 			ip_trash_timer_started = B_TRUE;
21417 		}
21418 		mutex_exit(&ip_trash_timer_lock);
21419 	}
21420 
21421 	if (ill->ill_isv6) {
21422 		mutex_enter(&mld_slowtimeout_lock);
21423 		if (mld_slowtimeout_id == 0) {
21424 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21425 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21426 		}
21427 		mutex_exit(&mld_slowtimeout_lock);
21428 	} else {
21429 		mutex_enter(&igmp_slowtimeout_lock);
21430 		if (igmp_slowtimeout_id == 0) {
21431 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21432 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21433 		}
21434 		mutex_exit(&igmp_slowtimeout_lock);
21435 	}
21436 
21437 	return (err);
21438 }
21439 
21440 /*
21441  * Common routine for ppa and ifname setting. Should be called exclusive.
21442  *
21443  * Returns EINPROGRESS when mp has been consumed by queueing it on
21444  * ill_pending_mp and the ioctl will complete in ip_rput.
21445  *
21446  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21447  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21448  * For SLIFNAME, we pass these values back to the userland.
21449  */
21450 static int
21451 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21452 {
21453 	ill_t	*ill;
21454 	ipif_t	*ipif;
21455 	ipsq_t	*ipsq;
21456 	char	*ppa_ptr;
21457 	char	*old_ptr;
21458 	char	old_char;
21459 	int	error;
21460 
21461 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21462 	ASSERT(q->q_next != NULL);
21463 	ASSERT(interf_name != NULL);
21464 
21465 	ill = (ill_t *)q->q_ptr;
21466 
21467 	ASSERT(ill->ill_name[0] == '\0');
21468 	ASSERT(IAM_WRITER_ILL(ill));
21469 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21470 	ASSERT(ill->ill_ppa == UINT_MAX);
21471 
21472 	/* The ppa is sent down by ifconfig or is chosen */
21473 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21474 		return (EINVAL);
21475 	}
21476 
21477 	/*
21478 	 * make sure ppa passed in is same as ppa in the name.
21479 	 * This check is not made when ppa == UINT_MAX in that case ppa
21480 	 * in the name could be anything. System will choose a ppa and
21481 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21482 	 */
21483 	if (*new_ppa_ptr != UINT_MAX) {
21484 		/* stoi changes the pointer */
21485 		old_ptr = ppa_ptr;
21486 		/*
21487 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21488 		 * (they don't have an externally visible ppa).  We assign one
21489 		 * here so that we can manage the interface.  Note that in
21490 		 * the past this value was always 0 for DLPI 1 drivers.
21491 		 */
21492 		if (*new_ppa_ptr == 0)
21493 			*new_ppa_ptr = stoi(&old_ptr);
21494 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21495 			return (EINVAL);
21496 	}
21497 	/*
21498 	 * terminate string before ppa
21499 	 * save char at that location.
21500 	 */
21501 	old_char = ppa_ptr[0];
21502 	ppa_ptr[0] = '\0';
21503 
21504 	ill->ill_ppa = *new_ppa_ptr;
21505 	/*
21506 	 * Finish as much work now as possible before calling ill_glist_insert
21507 	 * which makes the ill globally visible and also merges it with the
21508 	 * other protocol instance of this phyint. The remaining work is
21509 	 * done after entering the ipsq which may happen sometime later.
21510 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21511 	 */
21512 	ipif = ill->ill_ipif;
21513 
21514 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21515 	ipif_assign_seqid(ipif);
21516 
21517 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21518 		ill->ill_flags |= ILLF_IPV4;
21519 
21520 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21521 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21522 
21523 	if (ill->ill_flags & ILLF_IPV6) {
21524 
21525 		ill->ill_isv6 = B_TRUE;
21526 		if (ill->ill_rq != NULL) {
21527 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21528 			ill->ill_wq->q_qinfo = &winit_ipv6;
21529 		}
21530 
21531 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21532 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21533 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21534 		ipif->ipif_v6subnet = ipv6_all_zeros;
21535 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21536 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21537 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21538 		/*
21539 		 * point-to-point or Non-mulicast capable
21540 		 * interfaces won't do NUD unless explicitly
21541 		 * configured to do so.
21542 		 */
21543 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21544 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21545 			ill->ill_flags |= ILLF_NONUD;
21546 		}
21547 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21548 		if (ill->ill_flags & ILLF_NOARP) {
21549 			/*
21550 			 * Note: xresolv interfaces will eventually need
21551 			 * NOARP set here as well, but that will require
21552 			 * those external resolvers to have some
21553 			 * knowledge of that flag and act appropriately.
21554 			 * Not to be changed at present.
21555 			 */
21556 			ill->ill_flags &= ~ILLF_NOARP;
21557 		}
21558 		/*
21559 		 * Set the ILLF_ROUTER flag according to the global
21560 		 * IPv6 forwarding policy.
21561 		 */
21562 		if (ipv6_forward != 0)
21563 			ill->ill_flags |= ILLF_ROUTER;
21564 	} else if (ill->ill_flags & ILLF_IPV4) {
21565 		ill->ill_isv6 = B_FALSE;
21566 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21567 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21568 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21569 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21570 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21571 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21572 		/*
21573 		 * Set the ILLF_ROUTER flag according to the global
21574 		 * IPv4 forwarding policy.
21575 		 */
21576 		if (ip_g_forward != 0)
21577 			ill->ill_flags |= ILLF_ROUTER;
21578 	}
21579 
21580 	ASSERT(ill->ill_phyint != NULL);
21581 
21582 	/*
21583 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21584 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21585 	 */
21586 	if (ill->ill_isv6) {
21587 		/* allocate v6 mib */
21588 		if (!ill_allocate_mibs(ill))
21589 			return (ENOMEM);
21590 	}
21591 
21592 	/*
21593 	 * Pick a default sap until we get the DL_INFO_ACK back from
21594 	 * the driver.
21595 	 */
21596 	if (ill->ill_sap == 0) {
21597 		if (ill->ill_isv6)
21598 			ill->ill_sap  = IP6_DL_SAP;
21599 		else
21600 			ill->ill_sap  = IP_DL_SAP;
21601 	}
21602 
21603 	ill->ill_ifname_pending = 1;
21604 	ill->ill_ifname_pending_err = 0;
21605 
21606 	ill_refhold(ill);
21607 	rw_enter(&ill_g_lock, RW_WRITER);
21608 	if ((error = ill_glist_insert(ill, interf_name,
21609 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21610 		ill->ill_ppa = UINT_MAX;
21611 		ill->ill_name[0] = '\0';
21612 		/*
21613 		 * undo null termination done above.
21614 		 */
21615 		ppa_ptr[0] = old_char;
21616 		rw_exit(&ill_g_lock);
21617 		ill_refrele(ill);
21618 		return (error);
21619 	}
21620 
21621 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21622 
21623 	/*
21624 	 * When we return the buffer pointed to by interf_name should contain
21625 	 * the same name as in ill_name.
21626 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21627 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21628 	 * so copy full name and update the ppa ptr.
21629 	 * When ppa passed in != UINT_MAX all values are correct just undo
21630 	 * null termination, this saves a bcopy.
21631 	 */
21632 	if (*new_ppa_ptr == UINT_MAX) {
21633 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21634 		*new_ppa_ptr = ill->ill_ppa;
21635 	} else {
21636 		/*
21637 		 * undo null termination done above.
21638 		 */
21639 		ppa_ptr[0] = old_char;
21640 	}
21641 
21642 	/* Let SCTP know about this ILL */
21643 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21644 
21645 	/* and also about the first ipif */
21646 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21647 
21648 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21649 	    B_TRUE);
21650 
21651 	rw_exit(&ill_g_lock);
21652 	ill_refrele(ill);
21653 	if (ipsq == NULL)
21654 		return (EINPROGRESS);
21655 
21656 	/*
21657 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21658 	 * due to the phyint merge in ill_phyint_reinit.
21659 	 */
21660 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21661 		ipsq->ipsq_current_ipif == ipif);
21662 	ipsq->ipsq_current_ipif = ipif;
21663 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21664 	error = ipif_set_values_tail(ill, ipif, mp, q);
21665 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21666 	if (error != 0 && error != EINPROGRESS) {
21667 		/*
21668 		 * restore previous values
21669 		 */
21670 		ill->ill_isv6 = B_FALSE;
21671 	}
21672 	return (error);
21673 }
21674 
21675 
21676 extern void (*ip_cleanup_func)(void);
21677 
21678 void
21679 ipif_init(void)
21680 {
21681 	hrtime_t hrt;
21682 	int i;
21683 
21684 	/*
21685 	 * Can't call drv_getparm here as it is too early in the boot.
21686 	 * As we use ipif_src_random just for picking a different
21687 	 * source address everytime, this need not be really random.
21688 	 */
21689 	hrt = gethrtime();
21690 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
21691 
21692 	for (i = 0; i < MAX_G_HEADS; i++) {
21693 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
21694 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
21695 	}
21696 
21697 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
21698 	    ill_phyint_compare_index,
21699 	    sizeof (phyint_t),
21700 	    offsetof(struct phyint, phyint_avl_by_index));
21701 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
21702 	    ill_phyint_compare_name,
21703 	    sizeof (phyint_t),
21704 	    offsetof(struct phyint, phyint_avl_by_name));
21705 
21706 	ip_cleanup_func = ip_thread_exit;
21707 }
21708 
21709 /*
21710  * This is called by ip_rt_add when src_addr value is other than zero.
21711  * src_addr signifies the source address of the incoming packet. For
21712  * reverse tunnel route we need to create a source addr based routing
21713  * table. This routine creates ip_mrtun_table if it's empty and then
21714  * it adds the route entry hashed by source address. It verifies that
21715  * the outgoing interface is always a non-resolver interface (tunnel).
21716  */
21717 int
21718 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
21719     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
21720 {
21721 	ire_t   *ire;
21722 	ire_t	*save_ire;
21723 	ipif_t  *ipif;
21724 	ill_t   *in_ill = NULL;
21725 	ill_t	*out_ill;
21726 	queue_t	*stq;
21727 	mblk_t	*dlureq_mp;
21728 	int	error;
21729 
21730 	if (ire_arg != NULL)
21731 		*ire_arg = NULL;
21732 	ASSERT(in_src_addr != INADDR_ANY);
21733 
21734 	ipif = ipif_arg;
21735 	if (ipif != NULL) {
21736 		out_ill = ipif->ipif_ill;
21737 	} else {
21738 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
21739 		return (EINVAL);
21740 	}
21741 
21742 	if (src_ipif == NULL) {
21743 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
21744 		return (EINVAL);
21745 	}
21746 	in_ill = src_ipif->ipif_ill;
21747 
21748 	/*
21749 	 * Check for duplicates. We don't need to
21750 	 * match out_ill, because the uniqueness of
21751 	 * a route is only dependent on src_addr and
21752 	 * in_ill.
21753 	 */
21754 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
21755 	if (ire != NULL) {
21756 		ire_refrele(ire);
21757 		return (EEXIST);
21758 	}
21759 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
21760 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
21761 		    ipif->ipif_net_type));
21762 		return (EINVAL);
21763 	}
21764 
21765 	stq = ipif->ipif_wq;
21766 	ASSERT(stq != NULL);
21767 
21768 	/*
21769 	 * The outgoing interface must be non-resolver
21770 	 * interface.
21771 	 */
21772 	dlureq_mp = ill_dlur_gen(NULL,
21773 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
21774 	    out_ill->ill_sap_length);
21775 
21776 	if (dlureq_mp == NULL) {
21777 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
21778 		return (ENOMEM);
21779 	}
21780 
21781 	/* Create the IRE. */
21782 
21783 	ire = ire_create(
21784 	    NULL,				/* Zero dst addr */
21785 	    NULL,				/* Zero mask */
21786 	    NULL,				/* Zero gateway addr */
21787 	    NULL,				/* Zero ipif_src addr */
21788 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
21789 	    &ipif->ipif_mtu,
21790 	    NULL,
21791 	    NULL,				/* rfq */
21792 	    stq,
21793 	    IRE_MIPRTUN,
21794 	    dlureq_mp,
21795 	    ipif,
21796 	    in_ill,
21797 	    0,
21798 	    0,
21799 	    0,
21800 	    flags,
21801 	    &ire_uinfo_null);
21802 
21803 	if (ire == NULL)
21804 		return (ENOMEM);
21805 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
21806 	    ire->ire_type));
21807 	save_ire = ire;
21808 	ASSERT(save_ire != NULL);
21809 	error = ire_add_mrtun(&ire, q, mp, func);
21810 	/*
21811 	 * If ire_add_mrtun() failed, the ire passed in was freed
21812 	 * so there is no need to do so here.
21813 	 */
21814 	if (error != 0) {
21815 		return (error);
21816 	}
21817 
21818 	/* Duplicate check */
21819 	if (ire != save_ire) {
21820 		/* route already exists by now */
21821 		ire_refrele(ire);
21822 		return (EEXIST);
21823 	}
21824 
21825 	if (ire_arg != NULL) {
21826 		/*
21827 		 * Store the ire that was just added. the caller
21828 		 * ip_rts_request responsible for doing ire_refrele()
21829 		 * on it.
21830 		 */
21831 		*ire_arg = ire;
21832 	} else {
21833 		ire_refrele(ire);	/* held in ire_add_mrtun */
21834 	}
21835 
21836 	return (0);
21837 }
21838 
21839 /*
21840  * It is called by ip_rt_delete() only when mipagent requests to delete
21841  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
21842  */
21843 
21844 int
21845 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
21846 {
21847 	ire_t   *ire = NULL;
21848 
21849 	if (in_src_addr == INADDR_ANY)
21850 		return (EINVAL);
21851 	if (src_ipif == NULL)
21852 		return (EINVAL);
21853 
21854 	/* search if this route exists in the ip_mrtun_table */
21855 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
21856 	if (ire == NULL) {
21857 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
21858 		return (ESRCH);
21859 	}
21860 	ire_delete(ire);
21861 	ire_refrele(ire);
21862 	return (0);
21863 }
21864 
21865 /*
21866  * Lookup the ipif corresponding to the onlink destination address. For
21867  * point-to-point interfaces, it matches with remote endpoint destination
21868  * address. For point-to-multipoint interfaces it only tries to match the
21869  * destination with the interface's subnet address. The longest, most specific
21870  * match is found to take care of such rare network configurations like -
21871  * le0: 129.146.1.1/16
21872  * le1: 129.146.2.2/24
21873  * It is used only by SO_DONTROUTE at the moment.
21874  */
21875 ipif_t *
21876 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
21877 {
21878 	ipif_t	*ipif, *best_ipif;
21879 	ill_t	*ill;
21880 	ill_walk_context_t ctx;
21881 
21882 	ASSERT(zoneid != ALL_ZONES);
21883 	best_ipif = NULL;
21884 
21885 	rw_enter(&ill_g_lock, RW_READER);
21886 	ill = ILL_START_WALK_V4(&ctx);
21887 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21888 		mutex_enter(&ill->ill_lock);
21889 		for (ipif = ill->ill_ipif; ipif != NULL;
21890 		    ipif = ipif->ipif_next) {
21891 			if (!IPIF_CAN_LOOKUP(ipif))
21892 				continue;
21893 			if (ipif->ipif_zoneid != zoneid)
21894 				continue;
21895 			/*
21896 			 * Point-to-point case. Look for exact match with
21897 			 * destination address.
21898 			 */
21899 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
21900 				if (ipif->ipif_pp_dst_addr == addr) {
21901 					ipif_refhold_locked(ipif);
21902 					mutex_exit(&ill->ill_lock);
21903 					rw_exit(&ill_g_lock);
21904 					if (best_ipif != NULL)
21905 						ipif_refrele(best_ipif);
21906 					return (ipif);
21907 				}
21908 			} else if (ipif->ipif_subnet == (addr &
21909 			    ipif->ipif_net_mask)) {
21910 				/*
21911 				 * Point-to-multipoint case. Looping through to
21912 				 * find the most specific match. If there are
21913 				 * multiple best match ipif's then prefer ipif's
21914 				 * that are UP. If there is only one best match
21915 				 * ipif and it is DOWN we must still return it.
21916 				 */
21917 				if ((best_ipif == NULL) ||
21918 				    (ipif->ipif_net_mask >
21919 				    best_ipif->ipif_net_mask) ||
21920 				    ((ipif->ipif_net_mask ==
21921 				    best_ipif->ipif_net_mask) &&
21922 				    ((ipif->ipif_flags & IPIF_UP) &&
21923 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
21924 					ipif_refhold_locked(ipif);
21925 					mutex_exit(&ill->ill_lock);
21926 					rw_exit(&ill_g_lock);
21927 					if (best_ipif != NULL)
21928 						ipif_refrele(best_ipif);
21929 					best_ipif = ipif;
21930 					rw_enter(&ill_g_lock, RW_READER);
21931 					mutex_enter(&ill->ill_lock);
21932 				}
21933 			}
21934 		}
21935 		mutex_exit(&ill->ill_lock);
21936 	}
21937 	rw_exit(&ill_g_lock);
21938 	return (best_ipif);
21939 }
21940 
21941 
21942 /*
21943  * Save enough information so that we can recreate the IRE if
21944  * the interface goes down and then up.
21945  */
21946 static void
21947 ipif_save_ire(ipif_t *ipif, ire_t *ire)
21948 {
21949 	mblk_t	*save_mp;
21950 
21951 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
21952 	if (save_mp != NULL) {
21953 		ifrt_t	*ifrt;
21954 
21955 		save_mp->b_wptr += sizeof (ifrt_t);
21956 		ifrt = (ifrt_t *)save_mp->b_rptr;
21957 		bzero(ifrt, sizeof (ifrt_t));
21958 		ifrt->ifrt_type = ire->ire_type;
21959 		ifrt->ifrt_addr = ire->ire_addr;
21960 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
21961 		ifrt->ifrt_src_addr = ire->ire_src_addr;
21962 		ifrt->ifrt_mask = ire->ire_mask;
21963 		ifrt->ifrt_flags = ire->ire_flags;
21964 		ifrt->ifrt_max_frag = ire->ire_max_frag;
21965 		mutex_enter(&ipif->ipif_saved_ire_lock);
21966 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
21967 		ipif->ipif_saved_ire_mp = save_mp;
21968 		ipif->ipif_saved_ire_cnt++;
21969 		mutex_exit(&ipif->ipif_saved_ire_lock);
21970 	}
21971 }
21972 
21973 
21974 static void
21975 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
21976 {
21977 	mblk_t	**mpp;
21978 	mblk_t	*mp;
21979 	ifrt_t	*ifrt;
21980 
21981 	/* Remove from ipif_saved_ire_mp list if it is there */
21982 	mutex_enter(&ipif->ipif_saved_ire_lock);
21983 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
21984 	    mpp = &(*mpp)->b_cont) {
21985 		/*
21986 		 * On a given ipif, the triple of address, gateway and
21987 		 * mask is unique for each saved IRE (in the case of
21988 		 * ordinary interface routes, the gateway address is
21989 		 * all-zeroes).
21990 		 */
21991 		mp = *mpp;
21992 		ifrt = (ifrt_t *)mp->b_rptr;
21993 		if (ifrt->ifrt_addr == ire->ire_addr &&
21994 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
21995 		    ifrt->ifrt_mask == ire->ire_mask) {
21996 			*mpp = mp->b_cont;
21997 			ipif->ipif_saved_ire_cnt--;
21998 			freeb(mp);
21999 			break;
22000 		}
22001 	}
22002 	mutex_exit(&ipif->ipif_saved_ire_lock);
22003 }
22004 
22005 
22006 /*
22007  * IP multirouting broadcast routes handling
22008  * Append CGTP broadcast IREs to regular ones created
22009  * at ifconfig time.
22010  */
22011 static void
22012 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
22013 {
22014 	ire_t *ire_prim;
22015 
22016 	ASSERT(ire != NULL);
22017 	ASSERT(ire_dst != NULL);
22018 
22019 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22020 	    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22021 	if (ire_prim != NULL) {
22022 		/*
22023 		 * We are in the special case of broadcasts for
22024 		 * CGTP. We add an IRE_BROADCAST that holds
22025 		 * the RTF_MULTIRT flag, the destination
22026 		 * address of ire_dst and the low level
22027 		 * info of ire_prim. In other words, CGTP
22028 		 * broadcast is added to the redundant ipif.
22029 		 */
22030 		ipif_t *ipif_prim;
22031 		ire_t  *bcast_ire;
22032 
22033 		ipif_prim = ire_prim->ire_ipif;
22034 
22035 		ip2dbg(("ip_cgtp_filter_bcast_add: "
22036 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22037 		    (void *)ire_dst, (void *)ire_prim,
22038 		    (void *)ipif_prim));
22039 
22040 		bcast_ire = ire_create(
22041 		    (uchar_t *)&ire->ire_addr,
22042 		    (uchar_t *)&ip_g_all_ones,
22043 		    (uchar_t *)&ire_dst->ire_src_addr,
22044 		    (uchar_t *)&ire->ire_gateway_addr,
22045 		    NULL,
22046 		    &ipif_prim->ipif_mtu,
22047 		    NULL,
22048 		    ipif_prim->ipif_rq,
22049 		    ipif_prim->ipif_wq,
22050 		    IRE_BROADCAST,
22051 		    ipif_prim->ipif_bcast_mp,
22052 		    ipif_prim,
22053 		    NULL,
22054 		    0,
22055 		    0,
22056 		    0,
22057 		    ire->ire_flags,
22058 		    &ire_uinfo_null);
22059 
22060 		if (bcast_ire != NULL) {
22061 
22062 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
22063 				ip2dbg(("ip_cgtp_filter_bcast_add: "
22064 				    "added bcast_ire %p\n",
22065 				    (void *)bcast_ire));
22066 
22067 				ipif_save_ire(bcast_ire->ire_ipif,
22068 				    bcast_ire);
22069 				ire_refrele(bcast_ire);
22070 			}
22071 		}
22072 		ire_refrele(ire_prim);
22073 	}
22074 }
22075 
22076 
22077 /*
22078  * IP multirouting broadcast routes handling
22079  * Remove the broadcast ire
22080  */
22081 static void
22082 ip_cgtp_bcast_delete(ire_t *ire)
22083 {
22084 	ire_t *ire_dst;
22085 
22086 	ASSERT(ire != NULL);
22087 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22088 	    NULL, NULL, MATCH_IRE_TYPE);
22089 	if (ire_dst != NULL) {
22090 		ire_t *ire_prim;
22091 
22092 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22093 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22094 		if (ire_prim != NULL) {
22095 			ipif_t *ipif_prim;
22096 			ire_t  *bcast_ire;
22097 
22098 			ipif_prim = ire_prim->ire_ipif;
22099 
22100 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22101 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22102 			    (void *)ire_dst, (void *)ire_prim,
22103 			    (void *)ipif_prim));
22104 
22105 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22106 			    ire->ire_gateway_addr,
22107 			    IRE_BROADCAST,
22108 			    ipif_prim,
22109 			    NULL,
22110 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22111 			    MATCH_IRE_MASK);
22112 
22113 			if (bcast_ire != NULL) {
22114 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22115 				    "looked up bcast_ire %p\n",
22116 				    (void *)bcast_ire));
22117 				ipif_remove_ire(bcast_ire->ire_ipif,
22118 					bcast_ire);
22119 				ire_delete(bcast_ire);
22120 			}
22121 			ire_refrele(ire_prim);
22122 		}
22123 		ire_refrele(ire_dst);
22124 	}
22125 }
22126 
22127 /*
22128  * IPsec hardware acceleration capabilities related functions.
22129  */
22130 
22131 /*
22132  * Free a per-ill IPsec capabilities structure.
22133  */
22134 static void
22135 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22136 {
22137 	if (capab->auth_hw_algs != NULL)
22138 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22139 	if (capab->encr_hw_algs != NULL)
22140 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22141 	if (capab->encr_algparm != NULL)
22142 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22143 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22144 }
22145 
22146 /*
22147  * Allocate a new per-ill IPsec capabilities structure. This structure
22148  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22149  * an array which specifies, for each algorithm, whether this algorithm
22150  * is supported by the ill or not.
22151  */
22152 static ill_ipsec_capab_t *
22153 ill_ipsec_capab_alloc(void)
22154 {
22155 	ill_ipsec_capab_t *capab;
22156 	uint_t nelems;
22157 
22158 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22159 	if (capab == NULL)
22160 		return (NULL);
22161 
22162 	/* we need one bit per algorithm */
22163 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22164 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22165 
22166 	/* allocate memory to store algorithm flags */
22167 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22168 	if (capab->encr_hw_algs == NULL)
22169 		goto nomem;
22170 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22171 	if (capab->auth_hw_algs == NULL)
22172 		goto nomem;
22173 	/*
22174 	 * Leave encr_algparm NULL for now since we won't need it half
22175 	 * the time
22176 	 */
22177 	return (capab);
22178 
22179 nomem:
22180 	ill_ipsec_capab_free(capab);
22181 	return (NULL);
22182 }
22183 
22184 /*
22185  * Resize capability array.  Since we're exclusive, this is OK.
22186  */
22187 static boolean_t
22188 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22189 {
22190 	ipsec_capab_algparm_t *nalp, *oalp;
22191 	uint32_t olen, nlen;
22192 
22193 	oalp = capab->encr_algparm;
22194 	olen = capab->encr_algparm_size;
22195 
22196 	if (oalp != NULL) {
22197 		if (algid < capab->encr_algparm_end)
22198 			return (B_TRUE);
22199 	}
22200 
22201 	nlen = (algid + 1) * sizeof (*nalp);
22202 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22203 	if (nalp == NULL)
22204 		return (B_FALSE);
22205 
22206 	if (oalp != NULL) {
22207 		bcopy(oalp, nalp, olen);
22208 		kmem_free(oalp, olen);
22209 	}
22210 	capab->encr_algparm = nalp;
22211 	capab->encr_algparm_size = nlen;
22212 	capab->encr_algparm_end = algid + 1;
22213 
22214 	return (B_TRUE);
22215 }
22216 
22217 /*
22218  * Compare the capabilities of the specified ill with the protocol
22219  * and algorithms specified by the SA passed as argument.
22220  * If they match, returns B_TRUE, B_FALSE if they do not match.
22221  *
22222  * The ill can be passed as a pointer to it, or by specifying its index
22223  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22224  *
22225  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22226  * packet is eligible for hardware acceleration, and by
22227  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22228  * to a particular ill.
22229  */
22230 boolean_t
22231 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22232     ipsa_t *sa)
22233 {
22234 	boolean_t sa_isv6;
22235 	uint_t algid;
22236 	struct ill_ipsec_capab_s *cpp;
22237 	boolean_t need_refrele = B_FALSE;
22238 
22239 	if (ill == NULL) {
22240 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22241 		    NULL, NULL, NULL);
22242 		if (ill == NULL) {
22243 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22244 			return (B_FALSE);
22245 		}
22246 		need_refrele = B_TRUE;
22247 	}
22248 
22249 	/*
22250 	 * Use the address length specified by the SA to determine
22251 	 * if it corresponds to a IPv6 address, and fail the matching
22252 	 * if the isv6 flag passed as argument does not match.
22253 	 * Note: this check is used for SADB capability checking before
22254 	 * sending SA information to an ill.
22255 	 */
22256 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22257 	if (sa_isv6 != ill_isv6)
22258 		/* protocol mismatch */
22259 		goto done;
22260 
22261 	/*
22262 	 * Check if the ill supports the protocol, algorithm(s) and
22263 	 * key size(s) specified by the SA, and get the pointers to
22264 	 * the algorithms supported by the ill.
22265 	 */
22266 	switch (sa->ipsa_type) {
22267 
22268 	case SADB_SATYPE_ESP:
22269 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22270 			/* ill does not support ESP acceleration */
22271 			goto done;
22272 		cpp = ill->ill_ipsec_capab_esp;
22273 		algid = sa->ipsa_auth_alg;
22274 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22275 			goto done;
22276 		algid = sa->ipsa_encr_alg;
22277 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22278 			goto done;
22279 		if (algid < cpp->encr_algparm_end) {
22280 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22281 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22282 				goto done;
22283 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22284 				goto done;
22285 		}
22286 		break;
22287 
22288 	case SADB_SATYPE_AH:
22289 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22290 			/* ill does not support AH acceleration */
22291 			goto done;
22292 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22293 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22294 			goto done;
22295 		break;
22296 	}
22297 
22298 	if (need_refrele)
22299 		ill_refrele(ill);
22300 	return (B_TRUE);
22301 done:
22302 	if (need_refrele)
22303 		ill_refrele(ill);
22304 	return (B_FALSE);
22305 }
22306 
22307 
22308 /*
22309  * Add a new ill to the list of IPsec capable ills.
22310  * Called from ill_capability_ipsec_ack() when an ACK was received
22311  * indicating that IPsec hardware processing was enabled for an ill.
22312  *
22313  * ill must point to the ill for which acceleration was enabled.
22314  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22315  */
22316 static void
22317 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22318 {
22319 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22320 	uint_t sa_type;
22321 	uint_t ipproto;
22322 
22323 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22324 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22325 
22326 	switch (dl_cap) {
22327 	case DL_CAPAB_IPSEC_AH:
22328 		sa_type = SADB_SATYPE_AH;
22329 		ills = &ipsec_capab_ills_ah;
22330 		ipproto = IPPROTO_AH;
22331 		break;
22332 	case DL_CAPAB_IPSEC_ESP:
22333 		sa_type = SADB_SATYPE_ESP;
22334 		ills = &ipsec_capab_ills_esp;
22335 		ipproto = IPPROTO_ESP;
22336 		break;
22337 	}
22338 
22339 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22340 
22341 	/*
22342 	 * Add ill index to list of hardware accelerators. If
22343 	 * already in list, do nothing.
22344 	 */
22345 	for (cur_ill = *ills; cur_ill != NULL &&
22346 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22347 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22348 		;
22349 
22350 	if (cur_ill == NULL) {
22351 		/* if this is a new entry for this ill */
22352 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22353 		if (new_ill == NULL) {
22354 			rw_exit(&ipsec_capab_ills_lock);
22355 			return;
22356 		}
22357 
22358 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22359 		new_ill->ill_isv6 = ill->ill_isv6;
22360 		new_ill->next = *ills;
22361 		*ills = new_ill;
22362 	} else if (!sadb_resync) {
22363 		/* not resync'ing SADB and an entry exists for this ill */
22364 		rw_exit(&ipsec_capab_ills_lock);
22365 		return;
22366 	}
22367 
22368 	rw_exit(&ipsec_capab_ills_lock);
22369 
22370 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22371 		/*
22372 		 * IPsec module for protocol loaded, initiate dump
22373 		 * of the SADB to this ill.
22374 		 */
22375 		sadb_ill_download(ill, sa_type);
22376 }
22377 
22378 /*
22379  * Remove an ill from the list of IPsec capable ills.
22380  */
22381 static void
22382 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22383 {
22384 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22385 
22386 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22387 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22388 
22389 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22390 	    &ipsec_capab_ills_esp;
22391 
22392 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22393 
22394 	prev_ill = NULL;
22395 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22396 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22397 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22398 		;
22399 	if (cur_ill == NULL) {
22400 		/* entry not found */
22401 		rw_exit(&ipsec_capab_ills_lock);
22402 		return;
22403 	}
22404 	if (prev_ill == NULL) {
22405 		/* entry at front of list */
22406 		*ills = NULL;
22407 	} else {
22408 		prev_ill->next = cur_ill->next;
22409 	}
22410 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22411 	rw_exit(&ipsec_capab_ills_lock);
22412 }
22413 
22414 
22415 /*
22416  * Handling of DL_CONTROL_REQ messages that must be sent down to
22417  * an ill while having exclusive access.
22418  */
22419 /* ARGSUSED */
22420 static void
22421 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22422 {
22423 	ill_t *ill = (ill_t *)q->q_ptr;
22424 
22425 	ill_dlpi_send(ill, mp);
22426 }
22427 
22428 
22429 /*
22430  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22431  * supporting the specified IPsec protocol acceleration.
22432  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22433  * We free the mblk and, if sa is non-null, release the held referece.
22434  */
22435 void
22436 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22437 {
22438 	ipsec_capab_ill_t *ici, *cur_ici;
22439 	ill_t *ill;
22440 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22441 
22442 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22443 	    ipsec_capab_ills_esp;
22444 
22445 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22446 
22447 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22448 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22449 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22450 
22451 		/*
22452 		 * Handle the case where the ill goes away while the SADB is
22453 		 * attempting to send messages.  If it's going away, it's
22454 		 * nuking its shadow SADB, so we don't care..
22455 		 */
22456 
22457 		if (ill == NULL)
22458 			continue;
22459 
22460 		if (sa != NULL) {
22461 			/*
22462 			 * Make sure capabilities match before
22463 			 * sending SA to ill.
22464 			 */
22465 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22466 			    cur_ici->ill_isv6, sa)) {
22467 				ill_refrele(ill);
22468 				continue;
22469 			}
22470 
22471 			mutex_enter(&sa->ipsa_lock);
22472 			sa->ipsa_flags |= IPSA_F_HW;
22473 			mutex_exit(&sa->ipsa_lock);
22474 		}
22475 
22476 		/*
22477 		 * Copy template message, and add it to the front
22478 		 * of the mblk ship list. We want to avoid holding
22479 		 * the ipsec_capab_ills_lock while sending the
22480 		 * message to the ills.
22481 		 *
22482 		 * The b_next and b_prev are temporarily used
22483 		 * to build a list of mblks to be sent down, and to
22484 		 * save the ill to which they must be sent.
22485 		 */
22486 		nmp = copymsg(mp);
22487 		if (nmp == NULL) {
22488 			ill_refrele(ill);
22489 			continue;
22490 		}
22491 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22492 		nmp->b_next = mp_ship_list;
22493 		mp_ship_list = nmp;
22494 		nmp->b_prev = (mblk_t *)ill;
22495 	}
22496 
22497 	rw_exit(&ipsec_capab_ills_lock);
22498 
22499 	nmp = mp_ship_list;
22500 	while (nmp != NULL) {
22501 		/* restore the mblk to a sane state */
22502 		next_mp = nmp->b_next;
22503 		nmp->b_next = NULL;
22504 		ill = (ill_t *)nmp->b_prev;
22505 		nmp->b_prev = NULL;
22506 
22507 		/*
22508 		 * Ship the mblk to the ill, must be exclusive. Keep the
22509 		 * reference to the ill as qwriter_ip() does a ill_referele().
22510 		 */
22511 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22512 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22513 
22514 		nmp = next_mp;
22515 	}
22516 
22517 	if (sa != NULL)
22518 		IPSA_REFRELE(sa);
22519 	freemsg(mp);
22520 }
22521 
22522 
22523 /*
22524  * Derive an interface id from the link layer address.
22525  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22526  */
22527 static boolean_t
22528 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22529 {
22530 	char		*addr;
22531 
22532 	if (phys_length != ETHERADDRL)
22533 		return (B_FALSE);
22534 
22535 	/* Form EUI-64 like address */
22536 	addr = (char *)&v6addr->s6_addr32[2];
22537 	bcopy((char *)phys_addr, addr, 3);
22538 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22539 	addr[3] = (char)0xff;
22540 	addr[4] = (char)0xfe;
22541 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22542 	return (B_TRUE);
22543 }
22544 
22545 /* ARGSUSED */
22546 static boolean_t
22547 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22548 {
22549 	return (B_FALSE);
22550 }
22551 
22552 /* ARGSUSED */
22553 static boolean_t
22554 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22555     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22556 {
22557 	/*
22558 	 * Multicast address mappings used over Ethernet/802.X.
22559 	 * This address is used as a base for mappings.
22560 	 */
22561 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22562 	    0x00, 0x00, 0x00};
22563 
22564 	/*
22565 	 * Extract low order 32 bits from IPv6 multicast address.
22566 	 * Or that into the link layer address, starting from the
22567 	 * second byte.
22568 	 */
22569 	*hw_start = 2;
22570 	v6_extract_mask->s6_addr32[0] = 0;
22571 	v6_extract_mask->s6_addr32[1] = 0;
22572 	v6_extract_mask->s6_addr32[2] = 0;
22573 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22574 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22575 	return (B_TRUE);
22576 }
22577 
22578 /*
22579  * Indicate by return value whether multicast is supported. If not,
22580  * this code should not touch/change any parameters.
22581  */
22582 /* ARGSUSED */
22583 static boolean_t
22584 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22585     uint32_t *hw_start, ipaddr_t *extract_mask)
22586 {
22587 	/*
22588 	 * Multicast address mappings used over Ethernet/802.X.
22589 	 * This address is used as a base for mappings.
22590 	 */
22591 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22592 	    0x00, 0x00, 0x00 };
22593 
22594 	if (phys_length != ETHERADDRL)
22595 		return (B_FALSE);
22596 
22597 	*extract_mask = htonl(0x007fffff);
22598 	*hw_start = 2;
22599 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22600 	return (B_TRUE);
22601 }
22602 
22603 /*
22604  * Derive IPoIB interface id from the link layer address.
22605  */
22606 static boolean_t
22607 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22608 {
22609 	char		*addr;
22610 
22611 	if (phys_length != 20)
22612 		return (B_FALSE);
22613 	addr = (char *)&v6addr->s6_addr32[2];
22614 	bcopy(phys_addr + 12, addr, 8);
22615 	/*
22616 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22617 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22618 	 * rules. In these cases, the IBA considers these GUIDs to be in
22619 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22620 	 * required; vendors are required not to assign global EUI-64's
22621 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22622 	 * of the interface identifier. Whether the GUID is in modified
22623 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22624 	 * bit set to 1.
22625 	 */
22626 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22627 	return (B_TRUE);
22628 }
22629 
22630 /*
22631  * Note on mapping from multicast IP addresses to IPoIB multicast link
22632  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22633  * The format of an IPoIB multicast address is:
22634  *
22635  *  4 byte QPN      Scope Sign.  Pkey
22636  * +--------------------------------------------+
22637  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22638  * +--------------------------------------------+
22639  *
22640  * The Scope and Pkey components are properties of the IBA port and
22641  * network interface. They can be ascertained from the broadcast address.
22642  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22643  */
22644 
22645 static boolean_t
22646 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22647     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22648 {
22649 	/*
22650 	 * Base IPoIB IPv6 multicast address used for mappings.
22651 	 * Does not contain the IBA scope/Pkey values.
22652 	 */
22653 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22654 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22655 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22656 
22657 	/*
22658 	 * Extract low order 80 bits from IPv6 multicast address.
22659 	 * Or that into the link layer address, starting from the
22660 	 * sixth byte.
22661 	 */
22662 	*hw_start = 6;
22663 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22664 
22665 	/*
22666 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22667 	 */
22668 	*(maddr + 5) = *(bphys_addr + 5);
22669 	*(maddr + 8) = *(bphys_addr + 8);
22670 	*(maddr + 9) = *(bphys_addr + 9);
22671 
22672 	v6_extract_mask->s6_addr32[0] = 0;
22673 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
22674 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
22675 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22676 	return (B_TRUE);
22677 }
22678 
22679 static boolean_t
22680 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22681     uint32_t *hw_start, ipaddr_t *extract_mask)
22682 {
22683 	/*
22684 	 * Base IPoIB IPv4 multicast address used for mappings.
22685 	 * Does not contain the IBA scope/Pkey values.
22686 	 */
22687 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22688 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
22689 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22690 
22691 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
22692 		return (B_FALSE);
22693 
22694 	/*
22695 	 * Extract low order 28 bits from IPv4 multicast address.
22696 	 * Or that into the link layer address, starting from the
22697 	 * sixteenth byte.
22698 	 */
22699 	*extract_mask = htonl(0x0fffffff);
22700 	*hw_start = 16;
22701 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
22702 
22703 	/*
22704 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22705 	 */
22706 	*(maddr + 5) = *(bphys_addr + 5);
22707 	*(maddr + 8) = *(bphys_addr + 8);
22708 	*(maddr + 9) = *(bphys_addr + 9);
22709 	return (B_TRUE);
22710 }
22711 
22712 /*
22713  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
22714  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
22715  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
22716  * the link-local address is preferred.
22717  */
22718 boolean_t
22719 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22720 {
22721 	ipif_t	*ipif;
22722 	ipif_t	*maybe_ipif = NULL;
22723 
22724 	mutex_enter(&ill->ill_lock);
22725 	if (ill->ill_state_flags & ILL_CONDEMNED) {
22726 		mutex_exit(&ill->ill_lock);
22727 		if (ipifp != NULL)
22728 			*ipifp = NULL;
22729 		return (B_FALSE);
22730 	}
22731 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
22732 		if (!IPIF_CAN_LOOKUP(ipif))
22733 			continue;
22734 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid)
22735 			continue;
22736 		if ((ipif->ipif_flags & flags) != flags)
22737 			continue;
22738 
22739 		if (ipifp == NULL) {
22740 			mutex_exit(&ill->ill_lock);
22741 			ASSERT(maybe_ipif == NULL);
22742 			return (B_TRUE);
22743 		}
22744 		if (!ill->ill_isv6 ||
22745 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
22746 			ipif_refhold_locked(ipif);
22747 			mutex_exit(&ill->ill_lock);
22748 			*ipifp = ipif;
22749 			return (B_TRUE);
22750 		}
22751 		if (maybe_ipif == NULL)
22752 			maybe_ipif = ipif;
22753 	}
22754 	if (ipifp != NULL) {
22755 		if (maybe_ipif != NULL)
22756 			ipif_refhold_locked(maybe_ipif);
22757 		*ipifp = maybe_ipif;
22758 	}
22759 	mutex_exit(&ill->ill_lock);
22760 	return (maybe_ipif != NULL);
22761 }
22762 
22763 /*
22764  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
22765  */
22766 boolean_t
22767 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22768 {
22769 	ill_t *illg;
22770 
22771 	/*
22772 	 * We look at the passed-in ill first without grabbing ill_g_lock.
22773 	 */
22774 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
22775 		return (B_TRUE);
22776 	}
22777 	rw_enter(&ill_g_lock, RW_READER);
22778 	if (ill->ill_group == NULL) {
22779 		/* ill not in a group */
22780 		rw_exit(&ill_g_lock);
22781 		return (B_FALSE);
22782 	}
22783 
22784 	/*
22785 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
22786 	 * group. We need to look for an ipif in the zone on all the ills in the
22787 	 * group.
22788 	 */
22789 	illg = ill->ill_group->illgrp_ill;
22790 	do {
22791 		/*
22792 		 * We don't call ipif_lookup_zoneid() on ill as we already know
22793 		 * that it's not there.
22794 		 */
22795 		if (illg != ill &&
22796 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
22797 			break;
22798 		}
22799 	} while ((illg = illg->ill_group_next) != NULL);
22800 	rw_exit(&ill_g_lock);
22801 	return (illg != NULL);
22802 }
22803 
22804 /*
22805  * Check if this ill is only being used to send ICMP probes for IPMP
22806  */
22807 boolean_t
22808 ill_is_probeonly(ill_t *ill)
22809 {
22810 	/*
22811 	 * Check if the interface is FAILED, or INACTIVE
22812 	 */
22813 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
22814 		return (B_TRUE);
22815 
22816 	return (B_FALSE);
22817 }
22818