xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 7ee93e3bbce920c0d0742deb6632b0939e30b783)
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/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 
87 #include <net/pfkeyv2.h>
88 #include <inet/ipsec_info.h>
89 #include <inet/sadb.h>
90 #include <inet/ipsec_impl.h>
91 #include <sys/iphada.h>
92 
93 
94 #include <netinet/igmp.h>
95 #include <inet/ip_listutils.h>
96 #include <netinet/ip_mroute.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac.h>
99 
100 #include <sys/systeminfo.h>
101 #include <sys/bootconf.h>
102 
103 /* The character which tells where the ill_name ends */
104 #define	IPIF_SEPARATOR_CHAR	':'
105 
106 /* IP ioctl function table entry */
107 typedef struct ipft_s {
108 	int	ipft_cmd;
109 	pfi_t	ipft_pfi;
110 	int	ipft_min_size;
111 	int	ipft_flags;
112 } ipft_t;
113 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
114 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
115 
116 typedef struct ip_sock_ar_s {
117 	union {
118 		area_t	ip_sock_area;
119 		ared_t	ip_sock_ared;
120 		areq_t	ip_sock_areq;
121 	} ip_sock_ar_u;
122 	queue_t	*ip_sock_ar_q;
123 } ip_sock_ar_t;
124 
125 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
126 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
127 		    char *value, caddr_t cp, cred_t *ioc_cr);
128 
129 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
130 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
131 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
132     mblk_t *mp, boolean_t need_up);
133 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
134     mblk_t *mp, boolean_t need_up);
135 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
136     queue_t *q, mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp);
141 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
144     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
145 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
146 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
147 static void	ipsq_flush(ill_t *ill);
148 static void	ipsq_clean_all(ill_t *ill);
149 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
150 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
151     queue_t *q, mblk_t *mp, boolean_t need_up);
152 static void	ipsq_delete(ipsq_t *);
153 
154 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
155 		    boolean_t initialize);
156 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
157 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
158 static void	ipif_delete_cache_ire(ire_t *, char *);
159 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
160 static void	ipif_down_tail(ipif_t *ipif);
161 static void	ipif_free(ipif_t *ipif);
162 static void	ipif_free_tail(ipif_t *ipif);
163 static void	ipif_mask_reply(ipif_t *);
164 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
165 static void	ipif_multicast_down(ipif_t *ipif);
166 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
167 static void	ipif_set_default(ipif_t *ipif);
168 static int	ipif_set_values(queue_t *q, mblk_t *mp,
169     char *interf_name, uint_t *ppa);
170 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
171     queue_t *q);
172 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
173     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
174     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
175 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
176 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
177 
178 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
179 static int	ill_arp_off(ill_t *ill);
180 static int	ill_arp_on(ill_t *ill);
181 static void	ill_delete_interface_type(ill_if_t *);
182 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
183 static void	ill_down(ill_t *ill);
184 static void	ill_downi(ire_t *ire, char *ill_arg);
185 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
186 static void	ill_down_tail(ill_t *ill);
187 static void	ill_free_mib(ill_t *ill);
188 static void	ill_glist_delete(ill_t *);
189 static boolean_t ill_has_usable_ipif(ill_t *);
190 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
191 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
192 static void	ill_phyint_free(ill_t *ill);
193 static void	ill_phyint_reinit(ill_t *ill);
194 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
195 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
196 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
197 static void	ill_stq_cache_delete(ire_t *, char *);
198 
199 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
200 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
201 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
202     in6_addr_t *);
203 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
204     ipaddr_t *);
205 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 
211 static void	ipif_save_ire(ipif_t *, ire_t *);
212 static void	ipif_remove_ire(ipif_t *, ire_t *);
213 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
214 static void 	ip_cgtp_bcast_delete(ire_t *);
215 
216 /*
217  * Per-ill IPsec capabilities management.
218  */
219 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
220 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
221 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
222 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
223 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
224 static void ill_capability_proto(ill_t *, int, mblk_t *);
225 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
226     boolean_t);
227 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
228 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
229 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
230 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
231 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
232 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
234 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
235     dl_capability_sub_t *);
236 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
237 
238 static void ill_capability_poll_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
240 static void ill_capability_poll_reset(ill_t *, mblk_t **);
241 
242 static void	illgrp_cache_delete(ire_t *, char *);
243 static void	illgrp_delete(ill_t *ill);
244 static void	illgrp_reset_schednext(ill_t *ill);
245 
246 static ill_t	*ill_prev_usesrc(ill_t *);
247 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
248 static void	ill_disband_usesrc_group(ill_t *);
249 
250 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
251 
252 /*
253  * if we go over the memory footprint limit more than once in this msec
254  * interval, we'll start pruning aggressively.
255  */
256 int ip_min_frag_prune_time = 0;
257 
258 /*
259  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
260  * and the IPsec DOI
261  */
262 #define	MAX_IPSEC_ALGS	256
263 
264 #define	BITSPERBYTE	8
265 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
266 
267 #define	IPSEC_ALG_ENABLE(algs, algid) \
268 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
269 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
270 
271 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
272 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
273 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
274 
275 typedef uint8_t ipsec_capab_elem_t;
276 
277 /*
278  * Per-algorithm parameters.  Note that at present, only encryption
279  * algorithms have variable keysize (IKE does not provide a way to negotiate
280  * auth algorithm keysize).
281  *
282  * All sizes here are in bits.
283  */
284 typedef struct
285 {
286 	uint16_t	minkeylen;
287 	uint16_t	maxkeylen;
288 } ipsec_capab_algparm_t;
289 
290 /*
291  * Per-ill capabilities.
292  */
293 struct ill_ipsec_capab_s {
294 	ipsec_capab_elem_t *encr_hw_algs;
295 	ipsec_capab_elem_t *auth_hw_algs;
296 	uint32_t algs_size;	/* size of _hw_algs in bytes */
297 	/* algorithm key lengths */
298 	ipsec_capab_algparm_t *encr_algparm;
299 	uint32_t encr_algparm_size;
300 	uint32_t encr_algparm_end;
301 };
302 
303 /*
304  * List of AH and ESP IPsec acceleration capable ills
305  */
306 typedef struct ipsec_capab_ill_s {
307 	uint_t ill_index;
308 	boolean_t ill_isv6;
309 	struct ipsec_capab_ill_s *next;
310 } ipsec_capab_ill_t;
311 
312 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
313 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
314 krwlock_t ipsec_capab_ills_lock;
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	6,				/* xmit_count */
394 	1000,				/* (re)xmit_interval in milliseconds */
395 	4				/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 
424 static arma_t	ip_arma_multi_template = {
425 	AR_MAPPING_ADD,
426 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
427 				/* Name offset */
428 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
429 	IP_ARP_PROTO_TYPE,
430 	sizeof (arma_t),			/* proto_addr_offset */
431 	IP_ADDR_LEN,				/* proto_addr_length */
432 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
433 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
434 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
435 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
436 	IP_MAX_HW_LEN,				/* hw_addr_length */
437 	0,					/* hw_mapping_start */
438 };
439 
440 static ipft_t	ip_ioctl_ftbl[] = {
441 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
442 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
443 		IPFT_F_NO_REPLY },
444 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
447 	{ 0 }
448 };
449 
450 /* Simple ICMP IP Header Template */
451 static ipha_t icmp_ipha = {
452 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
453 };
454 
455 /* Flag descriptors for ip_ipif_report */
456 static nv_t	ipif_nv_tbl[] = {
457 	{ IPIF_UP,		"UP" },
458 	{ IPIF_BROADCAST,	"BROADCAST" },
459 	{ ILLF_DEBUG,		"DEBUG" },
460 	{ PHYI_LOOPBACK,	"LOOPBACK" },
461 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
462 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
463 	{ PHYI_RUNNING,		"RUNNING" },
464 	{ ILLF_NOARP,		"NOARP" },
465 	{ PHYI_PROMISC,		"PROMISC" },
466 	{ PHYI_ALLMULTI,	"ALLMULTI" },
467 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
468 	{ ILLF_MULTICAST,	"MULTICAST" },
469 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
470 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
471 	{ IPIF_DHCPRUNNING,	"DHCP" },
472 	{ IPIF_PRIVATE,		"PRIVATE" },
473 	{ IPIF_NOXMIT,		"NOXMIT" },
474 	{ IPIF_NOLOCAL,		"NOLOCAL" },
475 	{ IPIF_DEPRECATED,	"DEPRECATED" },
476 	{ IPIF_PREFERRED,	"PREFERRED" },
477 	{ IPIF_TEMPORARY,	"TEMPORARY" },
478 	{ IPIF_ADDRCONF,	"ADDRCONF" },
479 	{ PHYI_VIRTUAL,		"VIRTUAL" },
480 	{ ILLF_ROUTER,		"ROUTER" },
481 	{ ILLF_NONUD,		"NONUD" },
482 	{ IPIF_ANYCAST,		"ANYCAST" },
483 	{ ILLF_NORTEXCH,	"NORTEXCH" },
484 	{ ILLF_IPV4,		"IPV4" },
485 	{ ILLF_IPV6,		"IPV6" },
486 	{ IPIF_MIPRUNNING,	"MIP" },
487 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
488 	{ PHYI_FAILED,		"FAILED" },
489 	{ PHYI_STANDBY,		"STANDBY" },
490 	{ PHYI_INACTIVE,	"INACTIVE" },
491 	{ PHYI_OFFLINE,		"OFFLINE" },
492 };
493 
494 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
495 
496 static ip_m_t	ip_m_tbl[] = {
497 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_ether_v6intfid },
499 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_ether_v6intfid },
507 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
508 	    ip_ib_v6intfid },
509 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
510 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
511 	    ip_nodef_v6intfid }
512 };
513 
514 static ill_t	ill_null;		/* Empty ILL for init. */
515 char	ipif_loopback_name[] = "lo0";
516 static char *ipv4_forward_suffix = ":ip_forwarding";
517 static char *ipv6_forward_suffix = ":ip6_forwarding";
518 static kstat_t *loopback_ksp = NULL;
519 static	sin6_t	sin6_null;	/* Zero address for quick clears */
520 static	sin_t	sin_null;	/* Zero address for quick clears */
521 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
522 /* When set search for unused index */
523 static boolean_t ill_index_wrap = B_FALSE;
524 /* When set search for unused ipif_seqid */
525 static ipif_t	ipif_zero;
526 uint_t	ipif_src_random;
527 
528 /*
529  * For details on the protection offered by these locks please refer
530  * to the notes under the Synchronization section at the start of ip.c
531  */
532 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
533 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
534 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
535 
536 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
537 
538 /*
539  * illgrp_head/ifgrp_head is protected by IP's perimeter.
540  */
541 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
542 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
543 
544 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
545 
546 /*
547  * ppa arena is created after these many
548  * interfaces have been plumbed.
549  */
550 uint_t	ill_no_arena = 12;
551 
552 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
553 static phyint_list_t phyint_g_list;	/* start of phyint list */
554 
555 /*
556  * Reflects value of FAILBACK variable in IPMP config file
557  * /etc/default/mpathd. Default value is B_TRUE.
558  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
559  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
560  */
561 static boolean_t ipmp_enable_failback = B_TRUE;
562 
563 static uint_t
564 ipif_rand(void)
565 {
566 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
567 	return ((ipif_src_random >> 16) & 0x7fff);
568 }
569 
570 /*
571  * Allocate per-interface mibs. Only used for ipv6.
572  * Returns true if ok. False otherwise.
573  *  ipsq  may not yet be allocated (loopback case ).
574  */
575 static boolean_t
576 ill_allocate_mibs(ill_t *ill)
577 {
578 	ASSERT(ill->ill_isv6);
579 
580 	/* Already allocated? */
581 	if (ill->ill_ip6_mib != NULL) {
582 		ASSERT(ill->ill_icmp6_mib != NULL);
583 		return (B_TRUE);
584 	}
585 
586 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
587 	    KM_NOSLEEP);
588 	if (ill->ill_ip6_mib == NULL) {
589 		return (B_FALSE);
590 	}
591 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
592 	    KM_NOSLEEP);
593 	if (ill->ill_icmp6_mib == NULL) {
594 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
595 		ill->ill_ip6_mib = NULL;
596 		return (B_FALSE);
597 	}
598 	/*
599 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
600 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
601 	 * -> ill_phyint_reinit
602 	 */
603 	return (B_TRUE);
604 }
605 
606 /*
607  * Common code for preparation of ARP commands.  Two points to remember:
608  * 	1) The ill_name is tacked on at the end of the allocated space so
609  *	   the templates name_offset field must contain the total space
610  *	   to allocate less the name length.
611  *
612  *	2) The templates name_length field should contain the *template*
613  *	   length.  We use it as a parameter to bcopy() and then write
614  *	   the real ill_name_length into the name_length field of the copy.
615  * (Always called as writer.)
616  */
617 mblk_t *
618 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
619 {
620 	arc_t	*arc = (arc_t *)template;
621 	char	*cp;
622 	int	len;
623 	mblk_t	*mp;
624 	uint_t	name_length = ill->ill_name_length;
625 	uint_t	template_len = arc->arc_name_length;
626 
627 	len = arc->arc_name_offset + name_length;
628 	mp = allocb(len, BPRI_HI);
629 	if (mp == NULL)
630 		return (NULL);
631 	cp = (char *)mp->b_rptr;
632 	mp->b_wptr = (uchar_t *)&cp[len];
633 	if (template_len)
634 		bcopy(template, cp, template_len);
635 	if (len > template_len)
636 		bzero(&cp[template_len], len - template_len);
637 	mp->b_datap->db_type = M_PROTO;
638 
639 	arc = (arc_t *)cp;
640 	arc->arc_name_length = name_length;
641 	cp = (char *)arc + arc->arc_name_offset;
642 	bcopy(ill->ill_name, cp, name_length);
643 
644 	if (addr) {
645 		area_t	*area = (area_t *)mp->b_rptr;
646 
647 		cp = (char *)area + area->area_proto_addr_offset;
648 		bcopy(addr, cp, area->area_proto_addr_length);
649 		if (area->area_cmd == AR_ENTRY_ADD) {
650 			cp = (char *)area;
651 			len = area->area_proto_addr_length;
652 			if (area->area_proto_mask_offset)
653 				cp += area->area_proto_mask_offset;
654 			else
655 				cp += area->area_proto_addr_offset + len;
656 			while (len-- > 0)
657 				*cp++ = (char)~0;
658 		}
659 	}
660 	return (mp);
661 }
662 
663 /*
664  * Completely vaporize a lower level tap and all associated interfaces.
665  * ill_delete is called only out of ip_close when the device control
666  * stream is being closed.
667  */
668 void
669 ill_delete(ill_t *ill)
670 {
671 	ipif_t	*ipif;
672 	ill_t	*prev_ill;
673 
674 	/*
675 	 * ill_delete may be forcibly entering the ipsq. The previous
676 	 * ioctl may not have completed and may need to be aborted.
677 	 * ipsq_flush takes care of it. If we don't need to enter the
678 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
679 	 * ill_delete_tail is sufficient.
680 	 */
681 	ipsq_flush(ill);
682 
683 	/*
684 	 * Nuke all interfaces.  ipif_free will take down the interface,
685 	 * remove it from the list, and free the data structure.
686 	 * Walk down the ipif list and remove the logical interfaces
687 	 * first before removing the main ipif. We can't unplumb
688 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
689 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
690 	 * POINTOPOINT.
691 	 *
692 	 * If ill_ipif was not properly initialized (i.e low on memory),
693 	 * then no interfaces to clean up. In this case just clean up the
694 	 * ill.
695 	 */
696 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
697 		ipif_free(ipif);
698 
699 	/*
700 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
701 	 * So nobody can be using this mp now. Free the mp allocated for
702 	 * honoring ILLF_NOARP
703 	 */
704 	freemsg(ill->ill_arp_on_mp);
705 	ill->ill_arp_on_mp = NULL;
706 
707 	/* Clean up msgs on pending upcalls for mrouted */
708 	reset_mrt_ill(ill);
709 
710 	/*
711 	 * ipif_free -> reset_conn_ipif will remove all multicast
712 	 * references for IPv4. For IPv6, we need to do it here as
713 	 * it points only at ills.
714 	 */
715 	reset_conn_ill(ill);
716 
717 	/*
718 	 * ill_down will arrange to blow off any IRE's dependent on this
719 	 * ILL, and shut down fragmentation reassembly.
720 	 */
721 	ill_down(ill);
722 
723 	/* Let SCTP know, so that it can remove this from its list. */
724 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
725 
726 	/*
727 	 * If an address on this ILL is being used as a source address then
728 	 * clear out the pointers in other ILLs that point to this ILL.
729 	 */
730 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
731 	if (ill->ill_usesrc_grp_next != NULL) {
732 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
733 			ill_disband_usesrc_group(ill);
734 		} else {	/* consumer of the usesrc ILL */
735 			prev_ill = ill_prev_usesrc(ill);
736 			prev_ill->ill_usesrc_grp_next =
737 			    ill->ill_usesrc_grp_next;
738 		}
739 	}
740 	rw_exit(&ill_g_usesrc_lock);
741 }
742 
743 /*
744  * ill_delete_tail is called from ip_modclose after all references
745  * to the closing ill are gone. The wait is done in ip_modclose
746  */
747 void
748 ill_delete_tail(ill_t *ill)
749 {
750 	mblk_t	**mpp;
751 	ipif_t	*ipif;
752 
753 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
754 		ipif_down_tail(ipif);
755 
756 	/*
757 	 * Send the detach if there's one to send (i.e., if we're above a
758 	 * style 2 DLPI driver).
759 	 */
760 	if (ill->ill_detach_mp != NULL) {
761 		ill_dlpi_send(ill, ill->ill_detach_mp);
762 		ill->ill_detach_mp = NULL;
763 	}
764 
765 	/*
766 	 * If polling capability is enabled (which signifies direct
767 	 * upcall into IP and driver has ill saved as a handle),
768 	 * we need to make sure that unbind has completed before we
769 	 * let the ill disappear and driver no longer has any reference
770 	 * to this ill.
771 	 */
772 	mutex_enter(&ill->ill_lock);
773 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
774 		while (!(ill->ill_state_flags & ILL_DL_UNBIND_DONE))
775 			cv_wait(&ill->ill_cv, &ill->ill_lock);
776 	}
777 	mutex_exit(&ill->ill_lock);
778 
779 	if (ill->ill_net_type != IRE_LOOPBACK)
780 		qprocsoff(ill->ill_rq);
781 
782 	/*
783 	 * We do an ipsq_flush once again now. New messages could have
784 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
785 	 * could also have landed up if an ioctl thread had looked up
786 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
787 	 * enqueued the ioctl when we did the ipsq_flush last time.
788 	 */
789 	ipsq_flush(ill);
790 
791 	/*
792 	 * Free capabilities.
793 	 */
794 	if (ill->ill_ipsec_capab_ah != NULL) {
795 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
796 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
797 		ill->ill_ipsec_capab_ah = NULL;
798 	}
799 
800 	if (ill->ill_ipsec_capab_esp != NULL) {
801 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
802 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
803 		ill->ill_ipsec_capab_esp = NULL;
804 	}
805 
806 	if (ill->ill_mdt_capab != NULL) {
807 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
808 		ill->ill_mdt_capab = NULL;
809 	}
810 
811 	if (ill->ill_hcksum_capab != NULL) {
812 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
813 		ill->ill_hcksum_capab = NULL;
814 	}
815 
816 	if (ill->ill_zerocopy_capab != NULL) {
817 		kmem_free(ill->ill_zerocopy_capab,
818 		    sizeof (ill_zerocopy_capab_t));
819 		ill->ill_zerocopy_capab = NULL;
820 	}
821 
822 	/*
823 	 * Clean up polling capabilities
824 	 */
825 	if (ill->ill_capabilities & ILL_CAPAB_POLL)
826 		ipsq_clean_all(ill);
827 
828 	if (ill->ill_poll_capab != NULL) {
829 		CONN_DEC_REF(ill->ill_poll_capab->ill_unbind_conn);
830 		ill->ill_poll_capab->ill_unbind_conn = NULL;
831 		kmem_free(ill->ill_poll_capab,
832 		    sizeof (ill_poll_capab_t) +
833 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
834 		ill->ill_poll_capab = NULL;
835 	}
836 
837 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
838 
839 	while (ill->ill_ipif != NULL)
840 		ipif_free_tail(ill->ill_ipif);
841 
842 	ill_down_tail(ill);
843 
844 	/*
845 	 * We have removed all references to ilm from conn and the ones joined
846 	 * within the kernel.
847 	 *
848 	 * We don't walk conns, mrts and ires because
849 	 *
850 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
851 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
852 	 *    ill references.
853 	 */
854 	ASSERT(ilm_walk_ill(ill) == 0);
855 	/*
856 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
857 	 * could free the phyint. No more reference to the phyint after this
858 	 * point.
859 	 */
860 	(void) ill_glist_delete(ill);
861 
862 	rw_enter(&ip_g_nd_lock, RW_WRITER);
863 	if (ill->ill_ndd_name != NULL)
864 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
865 	rw_exit(&ip_g_nd_lock);
866 
867 
868 	if (ill->ill_frag_ptr != NULL) {
869 		uint_t count;
870 
871 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
872 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
873 		}
874 		mi_free(ill->ill_frag_ptr);
875 		ill->ill_frag_ptr = NULL;
876 		ill->ill_frag_hash_tbl = NULL;
877 	}
878 	if (ill->ill_nd_lla_mp != NULL)
879 		freemsg(ill->ill_nd_lla_mp);
880 	/* Free all retained control messages. */
881 	mpp = &ill->ill_first_mp_to_free;
882 	do {
883 		while (mpp[0]) {
884 			mblk_t  *mp;
885 			mblk_t  *mp1;
886 
887 			mp = mpp[0];
888 			mpp[0] = mp->b_next;
889 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
890 				mp1->b_next = NULL;
891 				mp1->b_prev = NULL;
892 			}
893 			freemsg(mp);
894 		}
895 	} while (mpp++ != &ill->ill_last_mp_to_free);
896 
897 	ill_free_mib(ill);
898 	ILL_TRACE_CLEANUP(ill);
899 }
900 
901 static void
902 ill_free_mib(ill_t *ill)
903 {
904 	if (ill->ill_ip6_mib != NULL) {
905 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
906 		ill->ill_ip6_mib = NULL;
907 	}
908 	if (ill->ill_icmp6_mib != NULL) {
909 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
910 		ill->ill_icmp6_mib = NULL;
911 	}
912 }
913 
914 /*
915  * Concatenate together a physical address and a sap.
916  *
917  * Sap_lengths are interpreted as follows:
918  *   sap_length == 0	==>	no sap
919  *   sap_length > 0	==>	sap is at the head of the dlpi address
920  *   sap_length < 0	==>	sap is at the tail of the dlpi address
921  */
922 static void
923 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
924     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
925 {
926 	uint16_t sap_addr = (uint16_t)sap_src;
927 
928 	if (sap_length == 0) {
929 		if (phys_src == NULL)
930 			bzero(dst, phys_length);
931 		else
932 			bcopy(phys_src, dst, phys_length);
933 	} else if (sap_length < 0) {
934 		if (phys_src == NULL)
935 			bzero(dst, phys_length);
936 		else
937 			bcopy(phys_src, dst, phys_length);
938 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
939 	} else {
940 		bcopy(&sap_addr, dst, sizeof (sap_addr));
941 		if (phys_src == NULL)
942 			bzero((char *)dst + sap_length, phys_length);
943 		else
944 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
945 	}
946 }
947 
948 /*
949  * Generate a dl_unitdata_req mblk for the device and address given.
950  * addr_length is the length of the physical portion of the address.
951  * If addr is NULL include an all zero address of the specified length.
952  * TRUE? In any case, addr_length is taken to be the entire length of the
953  * dlpi address, including the absolute value of sap_length.
954  */
955 mblk_t *
956 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
957 		t_scalar_t sap_length)
958 {
959 	dl_unitdata_req_t *dlur;
960 	mblk_t	*mp;
961 	t_scalar_t	abs_sap_length;		/* absolute value */
962 
963 	abs_sap_length = ABS(sap_length);
964 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
965 		DL_UNITDATA_REQ);
966 	if (mp == NULL)
967 		return (NULL);
968 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
969 	/* HACK: accomodate incompatible DLPI drivers */
970 	if (addr_length == 8)
971 		addr_length = 6;
972 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
973 	dlur->dl_dest_addr_offset = sizeof (*dlur);
974 	dlur->dl_priority.dl_min = 0;
975 	dlur->dl_priority.dl_max = 0;
976 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
977 	    (uchar_t *)&dlur[1]);
978 	return (mp);
979 }
980 
981 /*
982  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
983  * Return an error if we already have 1 or more ioctls in progress.
984  * This is used only for non-exclusive ioctls. Currently this is used
985  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
986  * and thus need to use ipsq_pending_mp_add.
987  */
988 boolean_t
989 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
990 {
991 	ASSERT(MUTEX_HELD(&ill->ill_lock));
992 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
993 	/*
994 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
995 	 */
996 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
997 	    (add_mp->b_datap->db_type == M_IOCTL));
998 
999 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1000 	/*
1001 	 * Return error if the conn has started closing. The conn
1002 	 * could have finished cleaning up the pending mp list,
1003 	 * If so we should not add another mp to the list negating
1004 	 * the cleanup.
1005 	 */
1006 	if (connp->conn_state_flags & CONN_CLOSING)
1007 		return (B_FALSE);
1008 	/*
1009 	 * Add the pending mp to the head of the list, chained by b_next.
1010 	 * Note down the conn on which the ioctl request came, in b_prev.
1011 	 * This will be used to later get the conn, when we get a response
1012 	 * on the ill queue, from some other module (typically arp)
1013 	 */
1014 	add_mp->b_next = (void *)ill->ill_pending_mp;
1015 	add_mp->b_queue = CONNP_TO_WQ(connp);
1016 	ill->ill_pending_mp = add_mp;
1017 	if (connp != NULL)
1018 		connp->conn_oper_pending_ill = ill;
1019 	return (B_TRUE);
1020 }
1021 
1022 /*
1023  * Retrieve the ill_pending_mp and return it. We have to walk the list
1024  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1025  */
1026 mblk_t *
1027 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1028 {
1029 	mblk_t	*prev = NULL;
1030 	mblk_t	*curr = NULL;
1031 	uint_t	id;
1032 	conn_t	*connp;
1033 
1034 	/*
1035 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1036 	 * up the pending mp, but it does not know the ioc_id and
1037 	 * passes in a zero for it.
1038 	 */
1039 	mutex_enter(&ill->ill_lock);
1040 	if (ioc_id != 0)
1041 		*connpp = NULL;
1042 
1043 	/* Search the list for the appropriate ioctl based on ioc_id */
1044 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1045 	    prev = curr, curr = curr->b_next) {
1046 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1047 		connp = Q_TO_CONN(curr->b_queue);
1048 		/* Match based on the ioc_id or based on the conn */
1049 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1050 			break;
1051 	}
1052 
1053 	if (curr != NULL) {
1054 		/* Unlink the mblk from the pending mp list */
1055 		if (prev != NULL) {
1056 			prev->b_next = curr->b_next;
1057 		} else {
1058 			ASSERT(ill->ill_pending_mp == curr);
1059 			ill->ill_pending_mp = curr->b_next;
1060 		}
1061 
1062 		/*
1063 		 * conn refcnt must have been bumped up at the start of
1064 		 * the ioctl. So we can safely access the conn.
1065 		 */
1066 		ASSERT(CONN_Q(curr->b_queue));
1067 		*connpp = Q_TO_CONN(curr->b_queue);
1068 		curr->b_next = NULL;
1069 		curr->b_queue = NULL;
1070 	}
1071 
1072 	mutex_exit(&ill->ill_lock);
1073 
1074 	return (curr);
1075 }
1076 
1077 /*
1078  * Add the pending mp to the list. There can be only 1 pending mp
1079  * in the list. Any exclusive ioctl that needs to wait for a response
1080  * from another module or driver needs to use this function to set
1081  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1082  * the other module/driver. This is also used while waiting for the
1083  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1084  */
1085 boolean_t
1086 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1087     int waitfor)
1088 {
1089 	ipsq_t	*ipsq;
1090 
1091 	ASSERT(IAM_WRITER_IPIF(ipif));
1092 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1093 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1094 	/*
1095 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1096 	 * M_ERROR/M_HANGUP from driver
1097 	 */
1098 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1099 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1100 
1101 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1102 	if (connp != NULL) {
1103 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1104 		/*
1105 		 * Return error if the conn has started closing. The conn
1106 		 * could have finished cleaning up the pending mp list,
1107 		 * If so we should not add another mp to the list negating
1108 		 * the cleanup.
1109 		 */
1110 		if (connp->conn_state_flags & CONN_CLOSING)
1111 			return (B_FALSE);
1112 	}
1113 	mutex_enter(&ipsq->ipsq_lock);
1114 	ipsq->ipsq_pending_ipif = ipif;
1115 	/*
1116 	 * Note down the queue in b_queue. This will be returned by
1117 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1118 	 * the processing
1119 	 */
1120 	add_mp->b_next = NULL;
1121 	add_mp->b_queue = q;
1122 	ipsq->ipsq_pending_mp = add_mp;
1123 	ipsq->ipsq_waitfor = waitfor;
1124 	/*
1125 	 * ipsq_current_ipif is needed to restart the operation from
1126 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1127 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1128 	 * been set until now.
1129 	 */
1130 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1131 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1132 		ipsq->ipsq_current_ipif = ipif;
1133 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1134 	}
1135 	if (connp != NULL)
1136 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1137 	mutex_exit(&ipsq->ipsq_lock);
1138 	return (B_TRUE);
1139 }
1140 
1141 /*
1142  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1143  * queued in the list.
1144  */
1145 mblk_t *
1146 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1147 {
1148 	mblk_t	*curr = NULL;
1149 
1150 	mutex_enter(&ipsq->ipsq_lock);
1151 	*connpp = NULL;
1152 	if (ipsq->ipsq_pending_mp == NULL) {
1153 		mutex_exit(&ipsq->ipsq_lock);
1154 		return (NULL);
1155 	}
1156 
1157 	/* There can be only 1 such excl message */
1158 	curr = ipsq->ipsq_pending_mp;
1159 	ASSERT(curr != NULL && curr->b_next == NULL);
1160 	ipsq->ipsq_pending_ipif = NULL;
1161 	ipsq->ipsq_pending_mp = NULL;
1162 	ipsq->ipsq_waitfor = 0;
1163 	mutex_exit(&ipsq->ipsq_lock);
1164 
1165 	if (CONN_Q(curr->b_queue)) {
1166 		/*
1167 		 * This mp did a refhold on the conn, at the start of the ioctl.
1168 		 * So we can safely return a pointer to the conn to the caller.
1169 		 */
1170 		*connpp = Q_TO_CONN(curr->b_queue);
1171 	} else {
1172 		*connpp = NULL;
1173 	}
1174 	curr->b_next = NULL;
1175 	curr->b_prev = NULL;
1176 	return (curr);
1177 }
1178 
1179 /*
1180  * Cleanup the ioctl mp queued in ipsq_pending_mp
1181  * - Called in the ill_delete path
1182  * - Called in the M_ERROR or M_HANGUP path on the ill.
1183  * - Called in the conn close path.
1184  */
1185 boolean_t
1186 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1187 {
1188 	mblk_t	*mp;
1189 	ipsq_t	*ipsq;
1190 	queue_t	*q;
1191 	ipif_t	*ipif;
1192 
1193 	ASSERT(IAM_WRITER_ILL(ill));
1194 	ipsq = ill->ill_phyint->phyint_ipsq;
1195 	mutex_enter(&ipsq->ipsq_lock);
1196 	/*
1197 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1198 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1199 	 * even if it is meant for another ill, since we have to enqueue
1200 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1201 	 * If connp is non-null we are called from the conn close path.
1202 	 */
1203 	mp = ipsq->ipsq_pending_mp;
1204 	if (mp == NULL || (connp != NULL &&
1205 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1206 		mutex_exit(&ipsq->ipsq_lock);
1207 		return (B_FALSE);
1208 	}
1209 	/* Now remove from the ipsq_pending_mp */
1210 	ipsq->ipsq_pending_mp = NULL;
1211 	q = mp->b_queue;
1212 	mp->b_next = NULL;
1213 	mp->b_prev = NULL;
1214 	mp->b_queue = NULL;
1215 
1216 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1217 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1218 	if (ill->ill_move_in_progress) {
1219 		ILL_CLEAR_MOVE(ill);
1220 	} else if (ill->ill_up_ipifs) {
1221 		ill_group_cleanup(ill);
1222 	}
1223 
1224 	ipif = ipsq->ipsq_pending_ipif;
1225 	ipsq->ipsq_pending_ipif = NULL;
1226 	ipsq->ipsq_waitfor = 0;
1227 	ipsq->ipsq_current_ipif = NULL;
1228 	mutex_exit(&ipsq->ipsq_lock);
1229 
1230 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1231 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1232 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1233 	} else {
1234 		/*
1235 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1236 		 * be just inet_freemsg. we have to restart it
1237 		 * otherwise the thread will be stuck.
1238 		 */
1239 		inet_freemsg(mp);
1240 	}
1241 	return (B_TRUE);
1242 }
1243 
1244 /*
1245  * The ill is closing. Cleanup all the pending mps. Called exclusively
1246  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1247  * knows this ill, and hence nobody can add an mp to this list
1248  */
1249 static void
1250 ill_pending_mp_cleanup(ill_t *ill)
1251 {
1252 	mblk_t	*mp;
1253 	queue_t	*q;
1254 
1255 	ASSERT(IAM_WRITER_ILL(ill));
1256 
1257 	mutex_enter(&ill->ill_lock);
1258 	/*
1259 	 * Every mp on the pending mp list originating from an ioctl
1260 	 * added 1 to the conn refcnt, at the start of the ioctl.
1261 	 * So bump it down now.  See comments in ip_wput_nondata()
1262 	 */
1263 	while (ill->ill_pending_mp != NULL) {
1264 		mp = ill->ill_pending_mp;
1265 		ill->ill_pending_mp = mp->b_next;
1266 		mutex_exit(&ill->ill_lock);
1267 
1268 		q = mp->b_queue;
1269 		ASSERT(CONN_Q(q));
1270 		mp->b_next = NULL;
1271 		mp->b_prev = NULL;
1272 		mp->b_queue = NULL;
1273 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1274 		mutex_enter(&ill->ill_lock);
1275 	}
1276 	ill->ill_pending_ipif = NULL;
1277 
1278 	mutex_exit(&ill->ill_lock);
1279 }
1280 
1281 /*
1282  * Called in the conn close path and ill delete path
1283  */
1284 static void
1285 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1286 {
1287 	ipsq_t	*ipsq;
1288 	mblk_t	*prev;
1289 	mblk_t	*curr;
1290 	mblk_t	*next;
1291 	queue_t	*q;
1292 	mblk_t	*tmp_list = NULL;
1293 
1294 	ASSERT(IAM_WRITER_ILL(ill));
1295 	if (connp != NULL)
1296 		q = CONNP_TO_WQ(connp);
1297 	else
1298 		q = ill->ill_wq;
1299 
1300 	ipsq = ill->ill_phyint->phyint_ipsq;
1301 	/*
1302 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1303 	 * In the case of ioctl from a conn, there can be only 1 mp
1304 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1305 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1306 	 * ioctls meant for this ill form conn's are not flushed. They will
1307 	 * be processed during ipsq_exit and will not find the ill and will
1308 	 * return error.
1309 	 */
1310 	mutex_enter(&ipsq->ipsq_lock);
1311 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1312 	    curr = next) {
1313 		next = curr->b_next;
1314 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1315 			/* Unlink the mblk from the pending mp list */
1316 			if (prev != NULL) {
1317 				prev->b_next = curr->b_next;
1318 			} else {
1319 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1320 				ipsq->ipsq_xopq_mphead = curr->b_next;
1321 			}
1322 			if (ipsq->ipsq_xopq_mptail == curr)
1323 				ipsq->ipsq_xopq_mptail = prev;
1324 			/*
1325 			 * Create a temporary list and release the ipsq lock
1326 			 * New elements are added to the head of the tmp_list
1327 			 */
1328 			curr->b_next = tmp_list;
1329 			tmp_list = curr;
1330 		} else {
1331 			prev = curr;
1332 		}
1333 	}
1334 	mutex_exit(&ipsq->ipsq_lock);
1335 
1336 	while (tmp_list != NULL) {
1337 		curr = tmp_list;
1338 		tmp_list = curr->b_next;
1339 		curr->b_next = NULL;
1340 		curr->b_prev = NULL;
1341 		curr->b_queue = NULL;
1342 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1343 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1344 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1345 		} else {
1346 			/*
1347 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1348 			 * this can't be just inet_freemsg. we have to
1349 			 * restart it otherwise the thread will be stuck.
1350 			 */
1351 			inet_freemsg(curr);
1352 		}
1353 	}
1354 }
1355 
1356 /*
1357  * This conn has started closing. Cleanup any pending ioctl from this conn.
1358  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1359  */
1360 void
1361 conn_ioctl_cleanup(conn_t *connp)
1362 {
1363 	mblk_t *curr;
1364 	ipsq_t	*ipsq;
1365 	ill_t	*ill;
1366 	boolean_t refheld;
1367 
1368 	/*
1369 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1370 	 * ioctl has not yet started, the mp is pending in the list headed by
1371 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1372 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1373 	 * is currently executing now the mp is not queued anywhere but
1374 	 * conn_oper_pending_ill is null. The conn close will wait
1375 	 * till the conn_ref drops to zero.
1376 	 */
1377 	mutex_enter(&connp->conn_lock);
1378 	ill = connp->conn_oper_pending_ill;
1379 	if (ill == NULL) {
1380 		mutex_exit(&connp->conn_lock);
1381 		return;
1382 	}
1383 
1384 	curr = ill_pending_mp_get(ill, &connp, 0);
1385 	if (curr != NULL) {
1386 		mutex_exit(&connp->conn_lock);
1387 		CONN_DEC_REF(connp);
1388 		inet_freemsg(curr);
1389 		return;
1390 	}
1391 	/*
1392 	 * We may not be able to refhold the ill if the ill/ipif
1393 	 * is changing. But we need to make sure that the ill will
1394 	 * not vanish. So we just bump up the ill_waiter count.
1395 	 */
1396 	refheld = ill_waiter_inc(ill);
1397 	mutex_exit(&connp->conn_lock);
1398 	if (refheld) {
1399 		if (ipsq_enter(ill, B_TRUE)) {
1400 			ill_waiter_dcr(ill);
1401 			/*
1402 			 * Check whether this ioctl has started and is
1403 			 * pending now in ipsq_pending_mp. If it is not
1404 			 * found there then check whether this ioctl has
1405 			 * not even started and is in the ipsq_xopq list.
1406 			 */
1407 			if (!ipsq_pending_mp_cleanup(ill, connp))
1408 				ipsq_xopq_mp_cleanup(ill, connp);
1409 			ipsq = ill->ill_phyint->phyint_ipsq;
1410 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1411 			return;
1412 		}
1413 	}
1414 
1415 	/*
1416 	 * The ill is also closing and we could not bump up the
1417 	 * ill_waiter_count or we could not enter the ipsq. Leave
1418 	 * the cleanup to ill_delete
1419 	 */
1420 	mutex_enter(&connp->conn_lock);
1421 	while (connp->conn_oper_pending_ill != NULL)
1422 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1423 	mutex_exit(&connp->conn_lock);
1424 	if (refheld)
1425 		ill_waiter_dcr(ill);
1426 }
1427 
1428 /*
1429  * ipcl_walk function for cleaning up conn_*_ill fields.
1430  */
1431 static void
1432 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1433 {
1434 	ill_t	*ill = (ill_t *)arg;
1435 	ire_t	*ire;
1436 
1437 	mutex_enter(&connp->conn_lock);
1438 	if (connp->conn_multicast_ill == ill) {
1439 		/* Revert to late binding */
1440 		connp->conn_multicast_ill = NULL;
1441 		connp->conn_orig_multicast_ifindex = 0;
1442 	}
1443 	if (connp->conn_incoming_ill == ill)
1444 		connp->conn_incoming_ill = NULL;
1445 	if (connp->conn_outgoing_ill == ill)
1446 		connp->conn_outgoing_ill = NULL;
1447 	if (connp->conn_outgoing_pill == ill)
1448 		connp->conn_outgoing_pill = NULL;
1449 	if (connp->conn_nofailover_ill == ill)
1450 		connp->conn_nofailover_ill = NULL;
1451 	if (connp->conn_xmit_if_ill == ill)
1452 		connp->conn_xmit_if_ill = NULL;
1453 	if (connp->conn_ire_cache != NULL) {
1454 		ire = connp->conn_ire_cache;
1455 		/*
1456 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1457 		 * interface X and ipif coming from interface Y, if interface
1458 		 * X and Y are part of the same IPMPgroup. Thus whenever
1459 		 * interface X goes down, remove all references to it by
1460 		 * checking both on ire_ipif and ire_stq.
1461 		 */
1462 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1463 		    (ire->ire_type == IRE_CACHE &&
1464 		    ire->ire_stq == ill->ill_wq)) {
1465 			connp->conn_ire_cache = NULL;
1466 			mutex_exit(&connp->conn_lock);
1467 			ire_refrele_notr(ire);
1468 			return;
1469 		}
1470 	}
1471 	mutex_exit(&connp->conn_lock);
1472 
1473 }
1474 
1475 /* ARGSUSED */
1476 void
1477 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1478 {
1479 	ill_t	*ill = q->q_ptr;
1480 	ipif_t	*ipif;
1481 
1482 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1483 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1484 		ipif_down_tail(ipif);
1485 	ill_down_tail(ill);
1486 	freemsg(mp);
1487 	ipsq->ipsq_current_ipif = NULL;
1488 }
1489 
1490 /*
1491  * ill_down_start is called when we want to down this ill and bring it up again
1492  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1493  * all interfaces, but don't tear down any plumbing.
1494  */
1495 boolean_t
1496 ill_down_start(queue_t *q, mblk_t *mp)
1497 {
1498 	ill_t	*ill;
1499 	ipif_t	*ipif;
1500 
1501 	ill = q->q_ptr;
1502 
1503 	ASSERT(IAM_WRITER_ILL(ill));
1504 
1505 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1506 		(void) ipif_down(ipif, NULL, NULL);
1507 
1508 	ill_down(ill);
1509 
1510 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1511 	mutex_enter(&ill->ill_lock);
1512 	/*
1513 	 * Atomically test and add the pending mp if references are
1514 	 * still active.
1515 	 */
1516 	if (!ill_is_quiescent(ill)) {
1517 		/*
1518 		 * Get rid of any pending mps and cleanup. Call will
1519 		 * not fail since we are passing a null connp.
1520 		 */
1521 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1522 		    mp, ILL_DOWN);
1523 		mutex_exit(&ill->ill_lock);
1524 		return (B_FALSE);
1525 	}
1526 	mutex_exit(&ill->ill_lock);
1527 	return (B_TRUE);
1528 }
1529 
1530 static void
1531 ill_down(ill_t *ill)
1532 {
1533 	/* Blow off any IREs dependent on this ILL. */
1534 	ire_walk(ill_downi, (char *)ill);
1535 
1536 	mutex_enter(&ire_mrtun_lock);
1537 	if (ire_mrtun_count != 0) {
1538 		mutex_exit(&ire_mrtun_lock);
1539 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1540 		    (char *)ill, NULL);
1541 	} else {
1542 		mutex_exit(&ire_mrtun_lock);
1543 	}
1544 
1545 	/*
1546 	 * If any interface based forwarding table exists
1547 	 * Blow off the ires there dependent on this ill
1548 	 */
1549 	mutex_enter(&ire_srcif_table_lock);
1550 	if (ire_srcif_table_count > 0) {
1551 		mutex_exit(&ire_srcif_table_lock);
1552 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1553 	} else {
1554 		mutex_exit(&ire_srcif_table_lock);
1555 	}
1556 
1557 	/* Remove any conn_*_ill depending on this ill */
1558 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1559 
1560 	if (ill->ill_group != NULL) {
1561 		illgrp_delete(ill);
1562 	}
1563 
1564 }
1565 
1566 static void
1567 ill_down_tail(ill_t *ill)
1568 {
1569 	int	i;
1570 
1571 	/* Destroy ill_srcif_table if it exists */
1572 	/* Lock not reqd really because nobody should be able to access */
1573 	mutex_enter(&ill->ill_lock);
1574 	if (ill->ill_srcif_table != NULL) {
1575 		ill->ill_srcif_refcnt = 0;
1576 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1577 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1578 		}
1579 		kmem_free(ill->ill_srcif_table,
1580 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1581 		ill->ill_srcif_table = NULL;
1582 		ill->ill_srcif_refcnt = 0;
1583 		ill->ill_mrtun_refcnt = 0;
1584 	}
1585 	mutex_exit(&ill->ill_lock);
1586 }
1587 
1588 /*
1589  * ire_walk routine used to delete every IRE that depends on queues
1590  * associated with 'ill'.  (Always called as writer.)
1591  */
1592 static void
1593 ill_downi(ire_t *ire, char *ill_arg)
1594 {
1595 	ill_t	*ill = (ill_t *)ill_arg;
1596 
1597 	/*
1598 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1599 	 * interface X and ipif coming from interface Y, if interface
1600 	 * X and Y are part of the same IPMP group. Thus whenever interface
1601 	 * X goes down, remove all references to it by checking both
1602 	 * on ire_ipif and ire_stq.
1603 	 */
1604 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1605 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1606 		ire_delete(ire);
1607 	}
1608 }
1609 
1610 /*
1611  * A seperate routine for deleting revtun and srcif based routes
1612  * are needed because the ires only deleted when the interface
1613  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1614  * we want to keep mobile IP specific code separate.
1615  */
1616 static void
1617 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1618 {
1619 	ill_t   *ill = (ill_t *)ill_arg;
1620 
1621 	ASSERT(ire->ire_in_ill != NULL);
1622 
1623 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1624 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1625 		ire_delete(ire);
1626 	}
1627 }
1628 
1629 /*
1630  * Remove ire/nce from the fastpath list.
1631  */
1632 void
1633 ill_fastpath_nack(ill_t *ill)
1634 {
1635 	if (ill->ill_isv6) {
1636 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1637 	} else {
1638 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1639 	}
1640 }
1641 
1642 /* Consume an M_IOCACK of the fastpath probe. */
1643 void
1644 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1645 {
1646 	mblk_t	*mp1 = mp;
1647 
1648 	/*
1649 	 * If this was the first attempt turn on the fastpath probing.
1650 	 */
1651 	mutex_enter(&ill->ill_lock);
1652 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1653 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1654 	mutex_exit(&ill->ill_lock);
1655 
1656 	/* Free the M_IOCACK mblk, hold on to the data */
1657 	mp = mp->b_cont;
1658 	freeb(mp1);
1659 	if (mp == NULL)
1660 		return;
1661 	if (mp->b_cont != NULL) {
1662 		/*
1663 		 * Update all IRE's or NCE's that are waiting for
1664 		 * fastpath update.
1665 		 */
1666 		if (ill->ill_isv6) {
1667 			/*
1668 			 * update nce's in the fastpath list.
1669 			 */
1670 			nce_fastpath_list_dispatch(ill,
1671 			    ndp_fastpath_update, mp);
1672 		} else {
1673 
1674 			/*
1675 			 * update ire's in the fastpath list.
1676 			 */
1677 			ire_fastpath_list_dispatch(ill,
1678 			    ire_fastpath_update, mp);
1679 			/*
1680 			 * Check if we need to traverse reverse tunnel table.
1681 			 * Since there is only single ire_type (IRE_MIPRTUN)
1682 			 * in the table, we don't need to match on ire_type.
1683 			 * We have to check ire_mrtun_count and not the
1684 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1685 			 * on the incoming ill and here we are dealing with
1686 			 * outgoing ill.
1687 			 */
1688 			mutex_enter(&ire_mrtun_lock);
1689 			if (ire_mrtun_count != 0) {
1690 				mutex_exit(&ire_mrtun_lock);
1691 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1692 				    (void (*)(ire_t *, void *))
1693 					ire_fastpath_update, mp, ill);
1694 			} else {
1695 				mutex_exit(&ire_mrtun_lock);
1696 			}
1697 		}
1698 		mp1 = mp->b_cont;
1699 		freeb(mp);
1700 		mp = mp1;
1701 	} else {
1702 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1703 	}
1704 
1705 	freeb(mp);
1706 }
1707 
1708 /*
1709  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1710  * The data portion of the request is a dl_unitdata_req_t template for
1711  * what we would send downstream in the absence of a fastpath confirmation.
1712  */
1713 int
1714 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1715 {
1716 	struct iocblk	*ioc;
1717 	mblk_t	*mp;
1718 
1719 	if (dlur_mp == NULL)
1720 		return (EINVAL);
1721 
1722 	mutex_enter(&ill->ill_lock);
1723 	switch (ill->ill_dlpi_fastpath_state) {
1724 	case IDMS_FAILED:
1725 		/*
1726 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1727 		 * support it.
1728 		 */
1729 		mutex_exit(&ill->ill_lock);
1730 		return (ENOTSUP);
1731 	case IDMS_UNKNOWN:
1732 		/* This is the first probe */
1733 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1734 		break;
1735 	default:
1736 		break;
1737 	}
1738 	mutex_exit(&ill->ill_lock);
1739 
1740 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1741 		return (EAGAIN);
1742 
1743 	mp->b_cont = copyb(dlur_mp);
1744 	if (mp->b_cont == NULL) {
1745 		freeb(mp);
1746 		return (EAGAIN);
1747 	}
1748 
1749 	ioc = (struct iocblk *)mp->b_rptr;
1750 	ioc->ioc_count = msgdsize(mp->b_cont);
1751 
1752 	putnext(ill->ill_wq, mp);
1753 	return (0);
1754 }
1755 
1756 void
1757 ill_capability_probe(ill_t *ill)
1758 {
1759 	/*
1760 	 * Do so only if negotiation is enabled, capabilities are unknown,
1761 	 * and a capability negotiation is not already in progress.
1762 	 */
1763 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1764 	    ill->ill_capab_state != IDMS_RENEG)
1765 		return;
1766 
1767 	ill->ill_capab_state = IDMS_INPROGRESS;
1768 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1769 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1770 }
1771 
1772 void
1773 ill_capability_reset(ill_t *ill)
1774 {
1775 	mblk_t *sc_mp = NULL;
1776 	mblk_t *tmp;
1777 
1778 	/*
1779 	 * Note here that we reset the state to UNKNOWN, and later send
1780 	 * down the DL_CAPABILITY_REQ without first setting the state to
1781 	 * INPROGRESS.  We do this in order to distinguish the
1782 	 * DL_CAPABILITY_ACK response which may come back in response to
1783 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1784 	 * also handle the case where the driver doesn't send us back
1785 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1786 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1787 	 * features are turned off until the state reaches IDMS_OK.
1788 	 */
1789 	ill->ill_capab_state = IDMS_UNKNOWN;
1790 
1791 	/*
1792 	 * Disable sub-capabilities and request a list of sub-capability
1793 	 * messages which will be sent down to the driver.  Each handler
1794 	 * allocates the corresponding dl_capability_sub_t inside an
1795 	 * mblk, and links it to the existing sc_mp mblk, or return it
1796 	 * as sc_mp if it's the first sub-capability (the passed in
1797 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1798 	 * sc_mp will be pulled-up, before passing it downstream.
1799 	 */
1800 	ill_capability_mdt_reset(ill, &sc_mp);
1801 	ill_capability_hcksum_reset(ill, &sc_mp);
1802 	ill_capability_zerocopy_reset(ill, &sc_mp);
1803 	ill_capability_ipsec_reset(ill, &sc_mp);
1804 	ill_capability_poll_reset(ill, &sc_mp);
1805 
1806 	/* Nothing to send down in order to disable the capabilities? */
1807 	if (sc_mp == NULL)
1808 		return;
1809 
1810 	tmp = msgpullup(sc_mp, -1);
1811 	freemsg(sc_mp);
1812 	if ((sc_mp = tmp) == NULL) {
1813 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1814 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1815 		return;
1816 	}
1817 
1818 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1819 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1820 }
1821 
1822 /*
1823  * Request or set new-style hardware capabilities supported by DLS provider.
1824  */
1825 static void
1826 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1827 {
1828 	mblk_t *mp;
1829 	dl_capability_req_t *capb;
1830 	size_t size = 0;
1831 	uint8_t *ptr;
1832 
1833 	if (reqp != NULL)
1834 		size = MBLKL(reqp);
1835 
1836 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1837 	if (mp == NULL) {
1838 		freemsg(reqp);
1839 		return;
1840 	}
1841 	ptr = mp->b_rptr;
1842 
1843 	capb = (dl_capability_req_t *)ptr;
1844 	ptr += sizeof (dl_capability_req_t);
1845 
1846 	if (reqp != NULL) {
1847 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1848 		capb->dl_sub_length = size;
1849 		bcopy(reqp->b_rptr, ptr, size);
1850 		ptr += size;
1851 		mp->b_cont = reqp->b_cont;
1852 		freeb(reqp);
1853 	}
1854 	ASSERT(ptr == mp->b_wptr);
1855 
1856 	ill_dlpi_send(ill, mp);
1857 }
1858 
1859 static void
1860 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1861 {
1862 	dl_capab_id_t *id_ic;
1863 	uint_t sub_dl_cap = outers->dl_cap;
1864 	dl_capability_sub_t *inners;
1865 	uint8_t *capend;
1866 
1867 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1868 
1869 	/*
1870 	 * Note: range checks here are not absolutely sufficient to
1871 	 * make us robust against malformed messages sent by drivers;
1872 	 * this is in keeping with the rest of IP's dlpi handling.
1873 	 * (Remember, it's coming from something else in the kernel
1874 	 * address space)
1875 	 */
1876 
1877 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1878 	if (capend > mp->b_wptr) {
1879 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1880 		    "malformed sub-capability too long for mblk");
1881 		return;
1882 	}
1883 
1884 	id_ic = (dl_capab_id_t *)(outers + 1);
1885 
1886 	if (outers->dl_length < sizeof (*id_ic) ||
1887 	    (inners = &id_ic->id_subcap,
1888 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1889 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1890 		    "encapsulated capab type %d too long for mblk",
1891 		    inners->dl_cap);
1892 		return;
1893 	}
1894 
1895 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1896 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1897 		    "isn't as expected; pass-thru module(s) detected, "
1898 		    "discarding capability\n", inners->dl_cap));
1899 		return;
1900 	}
1901 
1902 	/* Process the encapsulated sub-capability */
1903 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1904 }
1905 
1906 /*
1907  * Process Multidata Transmit capability negotiation ack received from a
1908  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1909  * DL_CAPABILITY_ACK message.
1910  */
1911 static void
1912 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1913 {
1914 	mblk_t *nmp = NULL;
1915 	dl_capability_req_t *oc;
1916 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1917 	ill_mdt_capab_t **ill_mdt_capab;
1918 	uint_t sub_dl_cap = isub->dl_cap;
1919 	uint8_t *capend;
1920 
1921 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1922 
1923 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1924 
1925 	/*
1926 	 * Note: range checks here are not absolutely sufficient to
1927 	 * make us robust against malformed messages sent by drivers;
1928 	 * this is in keeping with the rest of IP's dlpi handling.
1929 	 * (Remember, it's coming from something else in the kernel
1930 	 * address space)
1931 	 */
1932 
1933 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1934 	if (capend > mp->b_wptr) {
1935 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1936 		    "malformed sub-capability too long for mblk");
1937 		return;
1938 	}
1939 
1940 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1941 
1942 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1943 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1944 		    "unsupported MDT sub-capability (version %d, expected %d)",
1945 		    mdt_ic->mdt_version, MDT_VERSION_2);
1946 		return;
1947 	}
1948 
1949 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1950 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1951 		    "capability isn't as expected; pass-thru module(s) "
1952 		    "detected, discarding capability\n"));
1953 		return;
1954 	}
1955 
1956 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1957 
1958 		if (*ill_mdt_capab == NULL) {
1959 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1960 			    KM_NOSLEEP);
1961 
1962 			if (*ill_mdt_capab == NULL) {
1963 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1964 				    "could not enable MDT version %d "
1965 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1966 				    ill->ill_name);
1967 				return;
1968 			}
1969 		}
1970 
1971 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1972 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1973 		    "header spaces, %d max pld bufs, %d span limit)\n",
1974 		    ill->ill_name, MDT_VERSION_2,
1975 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1976 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1977 
1978 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1979 		(*ill_mdt_capab)->ill_mdt_on = 1;
1980 		/*
1981 		 * Round the following values to the nearest 32-bit; ULP
1982 		 * may further adjust them to accomodate for additional
1983 		 * protocol headers.  We pass these values to ULP during
1984 		 * bind time.
1985 		 */
1986 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1987 		    roundup(mdt_ic->mdt_hdr_head, 4);
1988 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1989 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1990 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1991 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1992 
1993 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1994 	} else {
1995 		uint_t size;
1996 		uchar_t *rptr;
1997 
1998 		size = sizeof (dl_capability_req_t) +
1999 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2000 
2001 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2002 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2003 			    "could not enable MDT for %s (ENOMEM)\n",
2004 			    ill->ill_name);
2005 			return;
2006 		}
2007 
2008 		rptr = nmp->b_rptr;
2009 		/* initialize dl_capability_req_t */
2010 		oc = (dl_capability_req_t *)nmp->b_rptr;
2011 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2012 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2013 		    sizeof (dl_capab_mdt_t);
2014 		nmp->b_rptr += sizeof (dl_capability_req_t);
2015 
2016 		/* initialize dl_capability_sub_t */
2017 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2018 		nmp->b_rptr += sizeof (*isub);
2019 
2020 		/* initialize dl_capab_mdt_t */
2021 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2022 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2023 
2024 		nmp->b_rptr = rptr;
2025 
2026 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2027 		    "to enable MDT version %d\n", ill->ill_name,
2028 		    MDT_VERSION_2));
2029 
2030 		/* set ENABLE flag */
2031 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2032 
2033 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2034 		ill_dlpi_send(ill, nmp);
2035 	}
2036 }
2037 
2038 static void
2039 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2040 {
2041 	mblk_t *mp;
2042 	dl_capab_mdt_t *mdt_subcap;
2043 	dl_capability_sub_t *dl_subcap;
2044 	int size;
2045 
2046 	if (!ILL_MDT_CAPABLE(ill))
2047 		return;
2048 
2049 	ASSERT(ill->ill_mdt_capab != NULL);
2050 	/*
2051 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2052 	 * structure since it's possible that another thread is still
2053 	 * referring to it.  The structure only gets deallocated when
2054 	 * we destroy the ill.
2055 	 */
2056 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2057 
2058 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2059 
2060 	mp = allocb(size, BPRI_HI);
2061 	if (mp == NULL) {
2062 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2063 		    "request to disable MDT\n"));
2064 		return;
2065 	}
2066 
2067 	mp->b_wptr = mp->b_rptr + size;
2068 
2069 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2070 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2071 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2072 
2073 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2074 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2075 	mdt_subcap->mdt_flags = 0;
2076 	mdt_subcap->mdt_hdr_head = 0;
2077 	mdt_subcap->mdt_hdr_tail = 0;
2078 
2079 	if (*sc_mp != NULL)
2080 		linkb(*sc_mp, mp);
2081 	else
2082 		*sc_mp = mp;
2083 }
2084 
2085 /*
2086  * Send a DL_NOTIFY_REQ to the specified ill to enable
2087  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2088  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2089  * acceleration.
2090  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2091  */
2092 static boolean_t
2093 ill_enable_promisc_notify(ill_t *ill)
2094 {
2095 	mblk_t *mp;
2096 	dl_notify_req_t *req;
2097 
2098 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2099 
2100 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2101 	if (mp == NULL)
2102 		return (B_FALSE);
2103 
2104 	req = (dl_notify_req_t *)mp->b_rptr;
2105 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2106 	    DL_NOTE_PROMISC_OFF_PHYS;
2107 
2108 	ill_dlpi_send(ill, mp);
2109 
2110 	return (B_TRUE);
2111 }
2112 
2113 
2114 /*
2115  * Allocate an IPsec capability request which will be filled by our
2116  * caller to turn on support for one or more algorithms.
2117  */
2118 static mblk_t *
2119 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2120 {
2121 	mblk_t *nmp;
2122 	dl_capability_req_t	*ocap;
2123 	dl_capab_ipsec_t	*ocip;
2124 	dl_capab_ipsec_t	*icip;
2125 	uint8_t			*ptr;
2126 	icip = (dl_capab_ipsec_t *)(isub + 1);
2127 
2128 	/*
2129 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2130 	 * PROMISC_ON/OFF notification from the provider. We need to
2131 	 * do this before enabling the algorithms to avoid leakage of
2132 	 * cleartext packets.
2133 	 */
2134 
2135 	if (!ill_enable_promisc_notify(ill))
2136 		return (NULL);
2137 
2138 	/*
2139 	 * Allocate new mblk which will contain a new capability
2140 	 * request to enable the capabilities.
2141 	 */
2142 
2143 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2144 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2145 	if (nmp == NULL)
2146 		return (NULL);
2147 
2148 	ptr = nmp->b_rptr;
2149 
2150 	/* initialize dl_capability_req_t */
2151 	ocap = (dl_capability_req_t *)ptr;
2152 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2153 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2154 	ptr += sizeof (dl_capability_req_t);
2155 
2156 	/* initialize dl_capability_sub_t */
2157 	bcopy(isub, ptr, sizeof (*isub));
2158 	ptr += sizeof (*isub);
2159 
2160 	/* initialize dl_capab_ipsec_t */
2161 	ocip = (dl_capab_ipsec_t *)ptr;
2162 	bcopy(icip, ocip, sizeof (*icip));
2163 
2164 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2165 	return (nmp);
2166 }
2167 
2168 /*
2169  * Process an IPsec capability negotiation ack received from a DLS Provider.
2170  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2171  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2172  */
2173 static void
2174 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2175 {
2176 	dl_capab_ipsec_t	*icip;
2177 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2178 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2179 	uint_t cipher, nciphers;
2180 	mblk_t *nmp;
2181 	uint_t alg_len;
2182 	boolean_t need_sadb_dump;
2183 	uint_t sub_dl_cap = isub->dl_cap;
2184 	ill_ipsec_capab_t **ill_capab;
2185 	uint64_t ill_capab_flag;
2186 	uint8_t *capend, *ciphend;
2187 	boolean_t sadb_resync;
2188 
2189 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2190 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2191 
2192 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2193 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2194 		ill_capab_flag = ILL_CAPAB_AH;
2195 	} else {
2196 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2197 		ill_capab_flag = ILL_CAPAB_ESP;
2198 	}
2199 
2200 	/*
2201 	 * If the ill capability structure exists, then this incoming
2202 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2203 	 * If this is so, then we'd need to resynchronize the SADB
2204 	 * after re-enabling the offloaded ciphers.
2205 	 */
2206 	sadb_resync = (*ill_capab != NULL);
2207 
2208 	/*
2209 	 * Note: range checks here are not absolutely sufficient to
2210 	 * make us robust against malformed messages sent by drivers;
2211 	 * this is in keeping with the rest of IP's dlpi handling.
2212 	 * (Remember, it's coming from something else in the kernel
2213 	 * address space)
2214 	 */
2215 
2216 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2217 	if (capend > mp->b_wptr) {
2218 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2219 		    "malformed sub-capability too long for mblk");
2220 		return;
2221 	}
2222 
2223 	/*
2224 	 * There are two types of acks we process here:
2225 	 * 1. acks in reply to a (first form) generic capability req
2226 	 *    (no ENABLE flag set)
2227 	 * 2. acks in reply to a ENABLE capability req.
2228 	 *    (ENABLE flag set)
2229 	 *
2230 	 * We process the subcapability passed as argument as follows:
2231 	 * 1 do initializations
2232 	 *   1.1 initialize nmp = NULL
2233 	 *   1.2 set need_sadb_dump to B_FALSE
2234 	 * 2 for each cipher in subcapability:
2235 	 *   2.1 if ENABLE flag is set:
2236 	 *	2.1.1 update per-ill ipsec capabilities info
2237 	 *	2.1.2 set need_sadb_dump to B_TRUE
2238 	 *   2.2 if ENABLE flag is not set:
2239 	 *	2.2.1 if nmp is NULL:
2240 	 *		2.2.1.1 allocate and initialize nmp
2241 	 *		2.2.1.2 init current pos in nmp
2242 	 *	2.2.2 copy current cipher to current pos in nmp
2243 	 *	2.2.3 set ENABLE flag in nmp
2244 	 *	2.2.4 update current pos
2245 	 * 3 if nmp is not equal to NULL, send enable request
2246 	 *   3.1 send capability request
2247 	 * 4 if need_sadb_dump is B_TRUE
2248 	 *   4.1 enable promiscuous on/off notifications
2249 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2250 	 *	AH or ESP SA's to interface.
2251 	 */
2252 
2253 	nmp = NULL;
2254 	oalg = NULL;
2255 	need_sadb_dump = B_FALSE;
2256 	icip = (dl_capab_ipsec_t *)(isub + 1);
2257 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2258 
2259 	nciphers = icip->cip_nciphers;
2260 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2261 
2262 	if (ciphend > capend) {
2263 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2264 		    "too many ciphers for sub-capability len");
2265 		return;
2266 	}
2267 
2268 	for (cipher = 0; cipher < nciphers; cipher++) {
2269 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2270 
2271 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2272 			/*
2273 			 * TBD: when we provide a way to disable capabilities
2274 			 * from above, need to manage the request-pending state
2275 			 * and fail if we were not expecting this ACK.
2276 			 */
2277 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2278 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2279 
2280 			/*
2281 			 * Update IPsec capabilities for this ill
2282 			 */
2283 
2284 			if (*ill_capab == NULL) {
2285 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2286 				    ("ill_capability_ipsec_ack: "
2287 					"allocating ipsec_capab for ill\n"));
2288 				*ill_capab = ill_ipsec_capab_alloc();
2289 
2290 				if (*ill_capab == NULL) {
2291 					cmn_err(CE_WARN,
2292 					    "ill_capability_ipsec_ack: "
2293 					    "could not enable IPsec Hardware "
2294 					    "acceleration for %s (ENOMEM)\n",
2295 					    ill->ill_name);
2296 					return;
2297 				}
2298 			}
2299 
2300 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2301 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2302 
2303 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2304 				cmn_err(CE_WARN,
2305 				    "ill_capability_ipsec_ack: "
2306 				    "malformed IPsec algorithm id %d",
2307 				    ialg->alg_prim);
2308 				continue;
2309 			}
2310 
2311 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2312 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2313 				    ialg->alg_prim);
2314 			} else {
2315 				ipsec_capab_algparm_t *alp;
2316 
2317 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2318 				    ialg->alg_prim);
2319 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2320 				    ialg->alg_prim)) {
2321 					cmn_err(CE_WARN,
2322 					    "ill_capability_ipsec_ack: "
2323 					    "no space for IPsec alg id %d",
2324 					    ialg->alg_prim);
2325 					continue;
2326 				}
2327 				alp = &((*ill_capab)->encr_algparm[
2328 						ialg->alg_prim]);
2329 				alp->minkeylen = ialg->alg_minbits;
2330 				alp->maxkeylen = ialg->alg_maxbits;
2331 			}
2332 			ill->ill_capabilities |= ill_capab_flag;
2333 			/*
2334 			 * indicate that a capability was enabled, which
2335 			 * will be used below to kick off a SADB dump
2336 			 * to the ill.
2337 			 */
2338 			need_sadb_dump = B_TRUE;
2339 		} else {
2340 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2341 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2342 				ialg->alg_prim));
2343 
2344 			if (nmp == NULL) {
2345 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2346 				if (nmp == NULL) {
2347 					/*
2348 					 * Sending the PROMISC_ON/OFF
2349 					 * notification request failed.
2350 					 * We cannot enable the algorithms
2351 					 * since the Provider will not
2352 					 * notify IP of promiscous mode
2353 					 * changes, which could lead
2354 					 * to leakage of packets.
2355 					 */
2356 					cmn_err(CE_WARN,
2357 					    "ill_capability_ipsec_ack: "
2358 					    "could not enable IPsec Hardware "
2359 					    "acceleration for %s (ENOMEM)\n",
2360 					    ill->ill_name);
2361 					return;
2362 				}
2363 				/* ptr to current output alg specifier */
2364 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2365 			}
2366 
2367 			/*
2368 			 * Copy current alg specifier, set ENABLE
2369 			 * flag, and advance to next output alg.
2370 			 * For now we enable all IPsec capabilities.
2371 			 */
2372 			ASSERT(oalg != NULL);
2373 			bcopy(ialg, oalg, alg_len);
2374 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2375 			nmp->b_wptr += alg_len;
2376 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2377 		}
2378 
2379 		/* move to next input algorithm specifier */
2380 		ialg = (dl_capab_ipsec_alg_t *)
2381 		    ((char *)ialg + alg_len);
2382 	}
2383 
2384 	if (nmp != NULL)
2385 		/*
2386 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2387 		 * IPsec hardware acceleration.
2388 		 */
2389 		ill_dlpi_send(ill, nmp);
2390 
2391 	if (need_sadb_dump)
2392 		/*
2393 		 * An acknowledgement corresponding to a request to
2394 		 * enable acceleration was received, notify SADB.
2395 		 */
2396 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2397 }
2398 
2399 /*
2400  * Given an mblk with enough space in it, create sub-capability entries for
2401  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2402  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2403  * in preparation for the reset the DL_CAPABILITY_REQ message.
2404  */
2405 static void
2406 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2407     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2408 {
2409 	dl_capab_ipsec_t *oipsec;
2410 	dl_capab_ipsec_alg_t *oalg;
2411 	dl_capability_sub_t *dl_subcap;
2412 	int i, k;
2413 
2414 	ASSERT(nciphers > 0);
2415 	ASSERT(ill_cap != NULL);
2416 	ASSERT(mp != NULL);
2417 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2418 
2419 	/* dl_capability_sub_t for "stype" */
2420 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2421 	dl_subcap->dl_cap = stype;
2422 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2423 	mp->b_wptr += sizeof (dl_capability_sub_t);
2424 
2425 	/* dl_capab_ipsec_t for "stype" */
2426 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2427 	oipsec->cip_version = 1;
2428 	oipsec->cip_nciphers = nciphers;
2429 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2430 
2431 	/* create entries for "stype" AUTH ciphers */
2432 	for (i = 0; i < ill_cap->algs_size; i++) {
2433 		for (k = 0; k < BITSPERBYTE; k++) {
2434 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2435 				continue;
2436 
2437 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2438 			bzero((void *)oalg, sizeof (*oalg));
2439 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2440 			oalg->alg_prim = k + (BITSPERBYTE * i);
2441 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2442 		}
2443 	}
2444 	/* create entries for "stype" ENCR ciphers */
2445 	for (i = 0; i < ill_cap->algs_size; i++) {
2446 		for (k = 0; k < BITSPERBYTE; k++) {
2447 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2448 				continue;
2449 
2450 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2451 			bzero((void *)oalg, sizeof (*oalg));
2452 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2453 			oalg->alg_prim = k + (BITSPERBYTE * i);
2454 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2455 		}
2456 	}
2457 }
2458 
2459 /*
2460  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2461  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2462  * POPC instruction, but our macro is more flexible for an arbitrary length
2463  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2464  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2465  * stays that way, we can reduce the number of iterations required.
2466  */
2467 #define	COUNT_1S(val, sum) {					\
2468 	uint8_t x = val & 0xff;					\
2469 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2470 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2471 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2472 }
2473 
2474 /* ARGSUSED */
2475 static void
2476 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2477 {
2478 	mblk_t *mp;
2479 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2480 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2481 	uint64_t ill_capabilities = ill->ill_capabilities;
2482 	int ah_cnt = 0, esp_cnt = 0;
2483 	int ah_len = 0, esp_len = 0;
2484 	int i, size = 0;
2485 
2486 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2487 		return;
2488 
2489 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2490 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2491 
2492 	/* Find out the number of ciphers for AH */
2493 	if (cap_ah != NULL) {
2494 		for (i = 0; i < cap_ah->algs_size; i++) {
2495 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2496 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2497 		}
2498 		if (ah_cnt > 0) {
2499 			size += sizeof (dl_capability_sub_t) +
2500 			    sizeof (dl_capab_ipsec_t);
2501 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2502 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2503 			size += ah_len;
2504 		}
2505 	}
2506 
2507 	/* Find out the number of ciphers for ESP */
2508 	if (cap_esp != NULL) {
2509 		for (i = 0; i < cap_esp->algs_size; i++) {
2510 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2511 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2512 		}
2513 		if (esp_cnt > 0) {
2514 			size += sizeof (dl_capability_sub_t) +
2515 			    sizeof (dl_capab_ipsec_t);
2516 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2517 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2518 			size += esp_len;
2519 		}
2520 	}
2521 
2522 	if (size == 0) {
2523 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2524 		    "there's nothing to reset\n"));
2525 		return;
2526 	}
2527 
2528 	mp = allocb(size, BPRI_HI);
2529 	if (mp == NULL) {
2530 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2531 		    "request to disable IPSEC Hardware Acceleration\n"));
2532 		return;
2533 	}
2534 
2535 	/*
2536 	 * Clear the capability flags for IPSec HA but retain the ill
2537 	 * capability structures since it's possible that another thread
2538 	 * is still referring to them.  The structures only get deallocated
2539 	 * when we destroy the ill.
2540 	 *
2541 	 * Various places check the flags to see if the ill is capable of
2542 	 * hardware acceleration, and by clearing them we ensure that new
2543 	 * outbound IPSec packets are sent down encrypted.
2544 	 */
2545 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2546 
2547 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2548 	if (ah_cnt > 0) {
2549 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2550 		    cap_ah, mp);
2551 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2552 	}
2553 
2554 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2555 	if (esp_cnt > 0) {
2556 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2557 		    cap_esp, mp);
2558 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2559 	}
2560 
2561 	/*
2562 	 * At this point we've composed a bunch of sub-capabilities to be
2563 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2564 	 * by the caller.  Upon receiving this reset message, the driver
2565 	 * must stop inbound decryption (by destroying all inbound SAs)
2566 	 * and let the corresponding packets come in encrypted.
2567 	 */
2568 
2569 	if (*sc_mp != NULL)
2570 		linkb(*sc_mp, mp);
2571 	else
2572 		*sc_mp = mp;
2573 }
2574 
2575 static void
2576 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2577     boolean_t encapsulated)
2578 {
2579 	boolean_t legacy = B_FALSE;
2580 
2581 	/*
2582 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2583 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2584 	 * instructed the driver to disable its advertised capabilities,
2585 	 * so there's no point in accepting any response at this moment.
2586 	 */
2587 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2588 		return;
2589 
2590 	/*
2591 	 * Note that only the following two sub-capabilities may be
2592 	 * considered as "legacy", since their original definitions
2593 	 * do not incorporate the dl_mid_t module ID token, and hence
2594 	 * may require the use of the wrapper sub-capability.
2595 	 */
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		legacy = B_TRUE;
2600 		break;
2601 	}
2602 
2603 	/*
2604 	 * For legacy sub-capabilities which don't incorporate a queue_t
2605 	 * pointer in their structures, discard them if we detect that
2606 	 * there are intermediate modules in between IP and the driver.
2607 	 */
2608 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2609 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2610 		    "%d discarded; %d module(s) present below IP\n",
2611 		    subp->dl_cap, ill->ill_lmod_cnt));
2612 		return;
2613 	}
2614 
2615 	switch (subp->dl_cap) {
2616 	case DL_CAPAB_IPSEC_AH:
2617 	case DL_CAPAB_IPSEC_ESP:
2618 		ill_capability_ipsec_ack(ill, mp, subp);
2619 		break;
2620 	case DL_CAPAB_MDT:
2621 		ill_capability_mdt_ack(ill, mp, subp);
2622 		break;
2623 	case DL_CAPAB_HCKSUM:
2624 		ill_capability_hcksum_ack(ill, mp, subp);
2625 		break;
2626 	case DL_CAPAB_ZEROCOPY:
2627 		ill_capability_zerocopy_ack(ill, mp, subp);
2628 		break;
2629 	case DL_CAPAB_POLL:
2630 		ill_capability_poll_ack(ill, mp, subp);
2631 		break;
2632 	default:
2633 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2634 		    subp->dl_cap));
2635 	}
2636 }
2637 
2638 /*
2639  * As part of negotiating polling capability, the driver tells us
2640  * the default (or normal) blanking interval and packet threshold
2641  * (the receive timer fires if blanking interval is reached or
2642  * the packet threshold is reached).
2643  *
2644  * As part of manipulating the polling interval, we always use our
2645  * estimated interval (avg service time * number of packets queued
2646  * on the squeue) but we try to blank for a minimum of
2647  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2648  * packet threshold during this time. When we are not in polling mode
2649  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2650  * rr_min_blank_ratio but up the packet cnt by a ratio of
2651  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2652  * possible although for a shorter interval.
2653  */
2654 #define	RR_MAX_BLANK_RATIO	20
2655 #define	RR_MIN_BLANK_RATIO	10
2656 #define	RR_MAX_PKT_CNT_RATIO	3
2657 #define	RR_MIN_PKT_CNT_RATIO	3
2658 
2659 /*
2660  * These can be tuned via /etc/system.
2661  */
2662 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2663 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2664 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2665 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2666 
2667 static mac_resource_handle_t
2668 ill_ring_add(void *arg, mac_resource_t *mrp)
2669 {
2670 	ill_t			*ill = (ill_t *)arg;
2671 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2672 	ill_rx_ring_t		*rx_ring;
2673 	int			ip_rx_index;
2674 
2675 	if (mrp->mr_type != MAC_RX_FIFO) {
2676 		return (NULL);
2677 	}
2678 	ASSERT(ill != NULL);
2679 	ASSERT(ill->ill_poll_capab != NULL);
2680 	ASSERT(mrp != NULL);
2681 
2682 	mutex_enter(&ill->ill_lock);
2683 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2684 		rx_ring = &ill->ill_poll_capab->ill_ring_tbl[ip_rx_index];
2685 		ASSERT(rx_ring != NULL);
2686 
2687 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2688 			time_t normal_blank_time =
2689 			    mrfp->mrf_normal_blank_time;
2690 			uint_t normal_pkt_cnt =
2691 			    mrfp->mrf_normal_pkt_count;
2692 
2693 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2694 
2695 			rx_ring->rr_blank = mrfp->mrf_blank;
2696 			rx_ring->rr_handle = mrfp->mrf_arg;
2697 			rx_ring->rr_ill = ill;
2698 			rx_ring->rr_normal_blank_time = normal_blank_time;
2699 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2700 
2701 			rx_ring->rr_max_blank_time =
2702 			    normal_blank_time * rr_max_blank_ratio;
2703 			rx_ring->rr_min_blank_time =
2704 			    normal_blank_time * rr_min_blank_ratio;
2705 			rx_ring->rr_max_pkt_cnt =
2706 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2707 			rx_ring->rr_min_pkt_cnt =
2708 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2709 
2710 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2711 			mutex_exit(&ill->ill_lock);
2712 
2713 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2714 			    (int), ip_rx_index);
2715 			return ((mac_resource_handle_t)rx_ring);
2716 		}
2717 	}
2718 
2719 	/*
2720 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2721 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2722 	 * should be made configurable. Meanwhile it cause no panic because
2723 	 * driver will pass ip_input a NULL handle which will make
2724 	 * IP allocate the default squeue and Polling mode will not
2725 	 * be used for this ring.
2726 	 */
2727 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2728 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2729 
2730 	mutex_exit(&ill->ill_lock);
2731 	return (NULL);
2732 }
2733 
2734 static boolean_t
2735 ill_capability_poll_init(ill_t *ill)
2736 {
2737 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2738 	conn_t 			*connp;
2739 	size_t			sz;
2740 
2741 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2742 		if (ill_poll == NULL) {
2743 			cmn_err(CE_PANIC, "ill_capability_poll_init: "
2744 			    "polling enabled for ill=%s (%p) but data "
2745 			    "structs uninitialized\n", ill->ill_name,
2746 			    (void *)ill);
2747 		}
2748 		return (B_TRUE);
2749 	}
2750 
2751 	if (ill_poll != NULL) {
2752 		ill_rx_ring_t 	*rx_ring = ill_poll->ill_ring_tbl;
2753 		/* Polling is being re-enabled */
2754 
2755 		connp = ill_poll->ill_unbind_conn;
2756 		ASSERT(rx_ring != NULL);
2757 		bzero((void *)ill_poll, sizeof (ill_poll_capab_t));
2758 		bzero((void *)rx_ring,
2759 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2760 		ill_poll->ill_ring_tbl = rx_ring;
2761 		ill_poll->ill_unbind_conn = connp;
2762 		return (B_TRUE);
2763 	}
2764 
2765 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2766 		return (B_FALSE);
2767 
2768 	sz = sizeof (ill_poll_capab_t);
2769 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2770 
2771 	ill_poll = kmem_zalloc(sz, KM_NOSLEEP);
2772 	if (ill_poll == NULL) {
2773 		cmn_err(CE_WARN, "ill_capability_poll_init: could not "
2774 		    "allocate poll_capab for %s (%p)\n", ill->ill_name,
2775 		    (void *)ill);
2776 		CONN_DEC_REF(connp);
2777 		return (B_FALSE);
2778 	}
2779 
2780 	/* Allocate space to hold ring table */
2781 	ill_poll->ill_ring_tbl = (ill_rx_ring_t *)&ill_poll[1];
2782 	ill->ill_poll_capab = ill_poll;
2783 	ill_poll->ill_unbind_conn = connp;
2784 	return (B_TRUE);
2785 }
2786 
2787 /*
2788  * ill_capability_poll_disable: disable polling capability. Since
2789  * any of the rings might already be in use, need to call ipsq_clean_all()
2790  * which gets behind the squeue to disable direct calls if necessary.
2791  * Clean up the direct tx function pointers as well.
2792  */
2793 static void
2794 ill_capability_poll_disable(ill_t *ill)
2795 {
2796 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2797 
2798 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2799 		ipsq_clean_all(ill);
2800 		ill_poll->ill_tx = NULL;
2801 		ill_poll->ill_tx_handle = NULL;
2802 	}
2803 
2804 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
2805 }
2806 
2807 static void
2808 ill_capability_poll_capable(ill_t *ill, dl_capab_poll_t *ipoll,
2809     dl_capability_sub_t *isub)
2810 {
2811 	uint_t			size;
2812 	uchar_t			*rptr;
2813 	dl_capab_poll_t		poll, *opoll;
2814 	ill_poll_capab_t	*ill_poll;
2815 	mblk_t			*nmp = NULL;
2816 	dl_capability_req_t	*ocap;
2817 
2818 	if (!ill_capability_poll_init(ill))
2819 		return;
2820 	ill_poll = ill->ill_poll_capab;
2821 
2822 	/* Copy locally to get the members aligned */
2823 	bcopy((void *)ipoll, (void *)&poll, sizeof (dl_capab_poll_t));
2824 
2825 	/* Get the tx function and handle from dld */
2826 	ill_poll->ill_tx = (ip_dld_tx_t)poll.poll_tx;
2827 	ill_poll->ill_tx_handle = (void *)poll.poll_tx_handle;
2828 
2829 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2830 	    isub->dl_length;
2831 
2832 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2833 		cmn_err(CE_WARN, "ill_capability_poll_ack: could not allocate "
2834 		    "memory for CAPAB_REQ for %s (%p)\n", ill->ill_name,
2835 		    (void *)ill);
2836 		return;
2837 	}
2838 
2839 	/* initialize dl_capability_req_t */
2840 	rptr = nmp->b_rptr;
2841 	ocap = (dl_capability_req_t *)rptr;
2842 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2843 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2844 	rptr += sizeof (dl_capability_req_t);
2845 
2846 	/* initialize dl_capability_sub_t */
2847 	bcopy(isub, rptr, sizeof (*isub));
2848 	rptr += sizeof (*isub);
2849 
2850 	opoll = (dl_capab_poll_t *)rptr;
2851 	rptr += sizeof (dl_capab_poll_t);
2852 
2853 	/* initialize dl_capab_poll_t to be sent down */
2854 	poll.poll_rx_handle = (uintptr_t)ill;
2855 	poll.poll_rx = (uintptr_t)ip_input;
2856 	poll.poll_ring_add = (uintptr_t)ill_ring_add;
2857 	poll.poll_flags = POLL_ENABLE;
2858 	bcopy((void *)&poll, (void *)opoll, sizeof (dl_capab_poll_t));
2859 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2860 
2861 	ip1dbg(("ill_capability_poll_capable: asking interface %s "
2862 	    "to enable polling\n", ill->ill_name));
2863 
2864 	/* nmp points to a DL_CAPABILITY_REQ message to enable polling */
2865 	ill_dlpi_send(ill, nmp);
2866 }
2867 
2868 
2869 /*
2870  * Process a polling capability negotiation ack received
2871  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_POLL)
2872  * of a DL_CAPABILITY_ACK message.
2873  */
2874 static void
2875 ill_capability_poll_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2876 {
2877 	dl_capab_poll_t		*ipoll;
2878 	uint_t			sub_dl_cap = isub->dl_cap;
2879 	uint8_t			*capend;
2880 
2881 
2882 	ASSERT(sub_dl_cap == DL_CAPAB_POLL);
2883 
2884 	/*
2885 	 * Don't enable polling for ipv6 ill's
2886 	 */
2887 	if (ill->ill_isv6) {
2888 		return;
2889 	}
2890 
2891 	/*
2892 	 * Note: range checks here are not absolutely sufficient to
2893 	 * make us robust against malformed messages sent by drivers;
2894 	 * this is in keeping with the rest of IP's dlpi handling.
2895 	 * (Remember, it's coming from something else in the kernel
2896 	 * address space)
2897 	 */
2898 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2899 	if (capend > mp->b_wptr) {
2900 		cmn_err(CE_WARN, "ill_capability_poll_ack: "
2901 		    "malformed sub-capability too long for mblk");
2902 		return;
2903 	}
2904 
2905 	/*
2906 	 * There are two types of acks we process here:
2907 	 * 1. acks in reply to a (first form) generic capability req
2908 	 *    (poll_flag will be set to POLL_CAPABLE)
2909 	 * 2. acks in reply to a POLL_ENABLE capability req.
2910 	 *    (POLL_ENABLE flag set)
2911 	 */
2912 	ipoll = (dl_capab_poll_t *)(isub + 1);
2913 
2914 	if (!dlcapabcheckqid(&ipoll->poll_mid, ill->ill_lmod_rq)) {
2915 		ip1dbg(("ill_capability_poll_ack: mid token for polling "
2916 		    "capability isn't as expected; pass-thru "
2917 		    "module(s) detected, discarding capability\n"));
2918 		if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2919 			/*
2920 			 * This is a capability renegotitation case.
2921 			 * The interface better be unusable at this
2922 			 * point other wise bad things will happen
2923 			 * if we disable direct calls on a running
2924 			 * and up interface.
2925 			 */
2926 			ill_capability_poll_disable(ill);
2927 		}
2928 		return;
2929 	}
2930 
2931 	switch (ipoll->poll_flags) {
2932 	default:
2933 		/* Disable if unknown flag */
2934 	case POLL_DISABLE:
2935 		ill_capability_poll_disable(ill);
2936 		break;
2937 	case POLL_CAPABLE:
2938 		/*
2939 		 * If the capability was already enabled, its safe
2940 		 * to disable it first to get rid of stale information
2941 		 * and then start enabling it again.
2942 		 */
2943 		ill_capability_poll_disable(ill);
2944 		ill_capability_poll_capable(ill, ipoll, isub);
2945 		break;
2946 	case POLL_ENABLE:
2947 		if (!(ill->ill_capabilities & ILL_CAPAB_POLL)) {
2948 			ASSERT(ill->ill_poll_capab != NULL);
2949 			ill->ill_capabilities |= ILL_CAPAB_POLL;
2950 		}
2951 		ip1dbg(("ill_capability_poll_ack: interface %s "
2952 		    "has enabled polling\n", ill->ill_name));
2953 		break;
2954 	}
2955 }
2956 
2957 static void
2958 ill_capability_poll_reset(ill_t *ill, mblk_t **sc_mp)
2959 {
2960 	mblk_t *mp;
2961 	dl_capab_poll_t *ipoll;
2962 	dl_capability_sub_t *dl_subcap;
2963 	int size;
2964 
2965 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
2966 		return;
2967 
2968 	ASSERT(ill->ill_poll_capab != NULL);
2969 
2970 	/*
2971 	 * Disable polling capability
2972 	 */
2973 	ill_capability_poll_disable(ill);
2974 
2975 	size = sizeof (*dl_subcap) + sizeof (*ipoll);
2976 
2977 	mp = allocb(size, BPRI_HI);
2978 	if (mp == NULL) {
2979 		ip1dbg(("ill_capability_poll_reset: unable to allocate "
2980 		    "request to disable polling\n"));
2981 		return;
2982 	}
2983 
2984 	mp->b_wptr = mp->b_rptr + size;
2985 
2986 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2987 	dl_subcap->dl_cap = DL_CAPAB_POLL;
2988 	dl_subcap->dl_length = sizeof (*ipoll);
2989 
2990 	ipoll = (dl_capab_poll_t *)(dl_subcap + 1);
2991 	ipoll->poll_flags = POLL_DISABLE;
2992 
2993 	if (*sc_mp != NULL)
2994 		linkb(*sc_mp, mp);
2995 	else
2996 		*sc_mp = mp;
2997 }
2998 
2999 
3000 /*
3001  * Process a hardware checksum offload capability negotiation ack received
3002  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3003  * of a DL_CAPABILITY_ACK message.
3004  */
3005 static void
3006 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3007 {
3008 	dl_capability_req_t	*ocap;
3009 	dl_capab_hcksum_t	*ihck, *ohck;
3010 	ill_hcksum_capab_t	**ill_hcksum;
3011 	mblk_t			*nmp = NULL;
3012 	uint_t			sub_dl_cap = isub->dl_cap;
3013 	uint8_t			*capend;
3014 
3015 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3016 
3017 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3018 
3019 	/*
3020 	 * Note: range checks here are not absolutely sufficient to
3021 	 * make us robust against malformed messages sent by drivers;
3022 	 * this is in keeping with the rest of IP's dlpi handling.
3023 	 * (Remember, it's coming from something else in the kernel
3024 	 * address space)
3025 	 */
3026 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3027 	if (capend > mp->b_wptr) {
3028 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3029 		    "malformed sub-capability too long for mblk");
3030 		return;
3031 	}
3032 
3033 	/*
3034 	 * There are two types of acks we process here:
3035 	 * 1. acks in reply to a (first form) generic capability req
3036 	 *    (no ENABLE flag set)
3037 	 * 2. acks in reply to a ENABLE capability req.
3038 	 *    (ENABLE flag set)
3039 	 */
3040 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3041 
3042 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3043 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3044 		    "unsupported hardware checksum "
3045 		    "sub-capability (version %d, expected %d)",
3046 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3047 		return;
3048 	}
3049 
3050 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3051 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3052 		    "checksum capability isn't as expected; pass-thru "
3053 		    "module(s) detected, discarding capability\n"));
3054 		return;
3055 	}
3056 
3057 #define	CURR_HCKSUM_CAPAB				\
3058 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3059 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3060 
3061 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3062 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3063 		/* do ENABLE processing */
3064 		if (*ill_hcksum == NULL) {
3065 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3066 			    KM_NOSLEEP);
3067 
3068 			if (*ill_hcksum == NULL) {
3069 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3070 				    "could not enable hcksum version %d "
3071 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3072 				    ill->ill_name);
3073 				return;
3074 			}
3075 		}
3076 
3077 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3078 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3079 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3080 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3081 		    "has enabled hardware checksumming\n ",
3082 		    ill->ill_name));
3083 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3084 		/*
3085 		 * Enabling hardware checksum offload
3086 		 * Currently IP supports {TCP,UDP}/IPv4
3087 		 * partial and full cksum offload and
3088 		 * IPv4 header checksum offload.
3089 		 * Allocate new mblk which will
3090 		 * contain a new capability request
3091 		 * to enable hardware checksum offload.
3092 		 */
3093 		uint_t	size;
3094 		uchar_t	*rptr;
3095 
3096 		size = sizeof (dl_capability_req_t) +
3097 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3098 
3099 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3100 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3101 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3102 			    ill->ill_name);
3103 			return;
3104 		}
3105 
3106 		rptr = nmp->b_rptr;
3107 		/* initialize dl_capability_req_t */
3108 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3109 		ocap->dl_sub_offset =
3110 		    sizeof (dl_capability_req_t);
3111 		ocap->dl_sub_length =
3112 		    sizeof (dl_capability_sub_t) +
3113 		    isub->dl_length;
3114 		nmp->b_rptr += sizeof (dl_capability_req_t);
3115 
3116 		/* initialize dl_capability_sub_t */
3117 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3118 		nmp->b_rptr += sizeof (*isub);
3119 
3120 		/* initialize dl_capab_hcksum_t */
3121 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3122 		bcopy(ihck, ohck, sizeof (*ihck));
3123 
3124 		nmp->b_rptr = rptr;
3125 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3126 
3127 		/* Set ENABLE flag */
3128 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3129 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3130 
3131 		/*
3132 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3133 		 * hardware checksum acceleration.
3134 		 */
3135 		ill_dlpi_send(ill, nmp);
3136 	} else {
3137 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3138 		    "advertised %x hardware checksum capability flags\n",
3139 		    ill->ill_name, ihck->hcksum_txflags));
3140 	}
3141 }
3142 
3143 static void
3144 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3145 {
3146 	mblk_t *mp;
3147 	dl_capab_hcksum_t *hck_subcap;
3148 	dl_capability_sub_t *dl_subcap;
3149 	int size;
3150 
3151 	if (!ILL_HCKSUM_CAPABLE(ill))
3152 		return;
3153 
3154 	ASSERT(ill->ill_hcksum_capab != NULL);
3155 	/*
3156 	 * Clear the capability flag for hardware checksum offload but
3157 	 * retain the ill_hcksum_capab structure since it's possible that
3158 	 * another thread is still referring to it.  The structure only
3159 	 * gets deallocated when we destroy the ill.
3160 	 */
3161 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3162 
3163 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3164 
3165 	mp = allocb(size, BPRI_HI);
3166 	if (mp == NULL) {
3167 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3168 		    "request to disable hardware checksum offload\n"));
3169 		return;
3170 	}
3171 
3172 	mp->b_wptr = mp->b_rptr + size;
3173 
3174 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3175 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3176 	dl_subcap->dl_length = sizeof (*hck_subcap);
3177 
3178 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3179 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3180 	hck_subcap->hcksum_txflags = 0;
3181 
3182 	if (*sc_mp != NULL)
3183 		linkb(*sc_mp, mp);
3184 	else
3185 		*sc_mp = mp;
3186 }
3187 
3188 static void
3189 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3190 {
3191 	mblk_t *nmp = NULL;
3192 	dl_capability_req_t *oc;
3193 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3194 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3195 	uint_t sub_dl_cap = isub->dl_cap;
3196 	uint8_t *capend;
3197 
3198 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3199 
3200 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3201 
3202 	/*
3203 	 * Note: range checks here are not absolutely sufficient to
3204 	 * make us robust against malformed messages sent by drivers;
3205 	 * this is in keeping with the rest of IP's dlpi handling.
3206 	 * (Remember, it's coming from something else in the kernel
3207 	 * address space)
3208 	 */
3209 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3210 	if (capend > mp->b_wptr) {
3211 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3212 		    "malformed sub-capability too long for mblk");
3213 		return;
3214 	}
3215 
3216 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3217 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3218 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3219 		    "unsupported ZEROCOPY sub-capability (version %d, "
3220 		    "expected %d)", zc_ic->zerocopy_version,
3221 		    ZEROCOPY_VERSION_1);
3222 		return;
3223 	}
3224 
3225 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3226 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3227 		    "capability isn't as expected; pass-thru module(s) "
3228 		    "detected, discarding capability\n"));
3229 		return;
3230 	}
3231 
3232 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3233 		if (*ill_zerocopy_capab == NULL) {
3234 			*ill_zerocopy_capab =
3235 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3236 			    KM_NOSLEEP);
3237 
3238 			if (*ill_zerocopy_capab == NULL) {
3239 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3240 				    "could not enable Zero-copy version %d "
3241 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3242 				    ill->ill_name);
3243 				return;
3244 			}
3245 		}
3246 
3247 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3248 		    "supports Zero-copy version %d\n", ill->ill_name,
3249 		    ZEROCOPY_VERSION_1));
3250 
3251 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3252 		    zc_ic->zerocopy_version;
3253 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3254 		    zc_ic->zerocopy_flags;
3255 
3256 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3257 	} else {
3258 		uint_t size;
3259 		uchar_t *rptr;
3260 
3261 		size = sizeof (dl_capability_req_t) +
3262 		    sizeof (dl_capability_sub_t) +
3263 		    sizeof (dl_capab_zerocopy_t);
3264 
3265 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3266 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3267 			    "could not enable zerocopy for %s (ENOMEM)\n",
3268 			    ill->ill_name);
3269 			return;
3270 		}
3271 
3272 		rptr = nmp->b_rptr;
3273 		/* initialize dl_capability_req_t */
3274 		oc = (dl_capability_req_t *)rptr;
3275 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3276 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3277 		    sizeof (dl_capab_zerocopy_t);
3278 		rptr += sizeof (dl_capability_req_t);
3279 
3280 		/* initialize dl_capability_sub_t */
3281 		bcopy(isub, rptr, sizeof (*isub));
3282 		rptr += sizeof (*isub);
3283 
3284 		/* initialize dl_capab_zerocopy_t */
3285 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3286 		*zc_oc = *zc_ic;
3287 
3288 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3289 		    "to enable zero-copy version %d\n", ill->ill_name,
3290 		    ZEROCOPY_VERSION_1));
3291 
3292 		/* set VMSAFE_MEM flag */
3293 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3294 
3295 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3296 		ill_dlpi_send(ill, nmp);
3297 	}
3298 }
3299 
3300 static void
3301 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3302 {
3303 	mblk_t *mp;
3304 	dl_capab_zerocopy_t *zerocopy_subcap;
3305 	dl_capability_sub_t *dl_subcap;
3306 	int size;
3307 
3308 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3309 		return;
3310 
3311 	ASSERT(ill->ill_zerocopy_capab != NULL);
3312 	/*
3313 	 * Clear the capability flag for Zero-copy but retain the
3314 	 * ill_zerocopy_capab structure since it's possible that another
3315 	 * thread is still referring to it.  The structure only gets
3316 	 * deallocated when we destroy the ill.
3317 	 */
3318 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3319 
3320 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3321 
3322 	mp = allocb(size, BPRI_HI);
3323 	if (mp == NULL) {
3324 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3325 		    "request to disable Zero-copy\n"));
3326 		return;
3327 	}
3328 
3329 	mp->b_wptr = mp->b_rptr + size;
3330 
3331 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3332 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3333 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3334 
3335 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3336 	zerocopy_subcap->zerocopy_version =
3337 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3338 	zerocopy_subcap->zerocopy_flags = 0;
3339 
3340 	if (*sc_mp != NULL)
3341 		linkb(*sc_mp, mp);
3342 	else
3343 		*sc_mp = mp;
3344 }
3345 
3346 /*
3347  * Consume a new-style hardware capabilities negotiation ack.
3348  * Called from ip_rput_dlpi_writer().
3349  */
3350 void
3351 ill_capability_ack(ill_t *ill, mblk_t *mp)
3352 {
3353 	dl_capability_ack_t *capp;
3354 	dl_capability_sub_t *subp, *endp;
3355 
3356 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3357 		ill->ill_capab_state = IDMS_OK;
3358 
3359 	capp = (dl_capability_ack_t *)mp->b_rptr;
3360 
3361 	if (capp->dl_sub_length == 0)
3362 		/* no new-style capabilities */
3363 		return;
3364 
3365 	/* make sure the driver supplied correct dl_sub_length */
3366 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3367 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3368 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3369 		return;
3370 	}
3371 
3372 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3373 	/*
3374 	 * There are sub-capabilities. Process the ones we know about.
3375 	 * Loop until we don't have room for another sub-cap header..
3376 	 */
3377 	for (subp = SC(capp, capp->dl_sub_offset),
3378 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3379 	    subp <= endp;
3380 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3381 
3382 		switch (subp->dl_cap) {
3383 		case DL_CAPAB_ID_WRAPPER:
3384 			ill_capability_id_ack(ill, mp, subp);
3385 			break;
3386 		default:
3387 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3388 			break;
3389 		}
3390 	}
3391 #undef SC
3392 }
3393 
3394 /*
3395  * This routine is called to scan the fragmentation reassembly table for
3396  * the specified ILL for any packets that are starting to smell.
3397  * dead_interval is the maximum time in seconds that will be tolerated.  It
3398  * will either be the value specified in ip_g_frag_timeout, or zero if the
3399  * ILL is shutting down and it is time to blow everything off.
3400  *
3401  * It returns the number of seconds (as a time_t) that the next frag timer
3402  * should be scheduled for, 0 meaning that the timer doesn't need to be
3403  * re-started.  Note that the method of calculating next_timeout isn't
3404  * entirely accurate since time will flow between the time we grab
3405  * current_time and the time we schedule the next timeout.  This isn't a
3406  * big problem since this is the timer for sending an ICMP reassembly time
3407  * exceeded messages, and it doesn't have to be exactly accurate.
3408  *
3409  * This function is
3410  * sometimes called as writer, although this is not required.
3411  */
3412 time_t
3413 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3414 {
3415 	ipfb_t	*ipfb;
3416 	ipfb_t	*endp;
3417 	ipf_t	*ipf;
3418 	ipf_t	*ipfnext;
3419 	mblk_t	*mp;
3420 	time_t	current_time = gethrestime_sec();
3421 	time_t	next_timeout = 0;
3422 	uint32_t	hdr_length;
3423 	mblk_t	*send_icmp_head;
3424 	mblk_t	*send_icmp_head_v6;
3425 
3426 	ipfb = ill->ill_frag_hash_tbl;
3427 	if (ipfb == NULL)
3428 		return (B_FALSE);
3429 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3430 	/* Walk the frag hash table. */
3431 	for (; ipfb < endp; ipfb++) {
3432 		send_icmp_head = NULL;
3433 		send_icmp_head_v6 = NULL;
3434 		mutex_enter(&ipfb->ipfb_lock);
3435 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3436 			time_t frag_time = current_time - ipf->ipf_timestamp;
3437 			time_t frag_timeout;
3438 
3439 			if (frag_time < dead_interval) {
3440 				/*
3441 				 * There are some outstanding fragments
3442 				 * that will timeout later.  Make note of
3443 				 * the time so that we can reschedule the
3444 				 * next timeout appropriately.
3445 				 */
3446 				frag_timeout = dead_interval - frag_time;
3447 				if (next_timeout == 0 ||
3448 				    frag_timeout < next_timeout) {
3449 					next_timeout = frag_timeout;
3450 				}
3451 				break;
3452 			}
3453 			/* Time's up.  Get it out of here. */
3454 			hdr_length = ipf->ipf_nf_hdr_len;
3455 			ipfnext = ipf->ipf_hash_next;
3456 			if (ipfnext)
3457 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3458 			*ipf->ipf_ptphn = ipfnext;
3459 			mp = ipf->ipf_mp->b_cont;
3460 			for (; mp; mp = mp->b_cont) {
3461 				/* Extra points for neatness. */
3462 				IP_REASS_SET_START(mp, 0);
3463 				IP_REASS_SET_END(mp, 0);
3464 			}
3465 			mp = ipf->ipf_mp->b_cont;
3466 			ill->ill_frag_count -= ipf->ipf_count;
3467 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3468 			ipfb->ipfb_count -= ipf->ipf_count;
3469 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3470 			ipfb->ipfb_frag_pkts--;
3471 			/*
3472 			 * We do not send any icmp message from here because
3473 			 * we currently are holding the ipfb_lock for this
3474 			 * hash chain. If we try and send any icmp messages
3475 			 * from here we may end up via a put back into ip
3476 			 * trying to get the same lock, causing a recursive
3477 			 * mutex panic. Instead we build a list and send all
3478 			 * the icmp messages after we have dropped the lock.
3479 			 */
3480 			if (ill->ill_isv6) {
3481 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3482 				if (hdr_length != 0) {
3483 					mp->b_next = send_icmp_head_v6;
3484 					send_icmp_head_v6 = mp;
3485 				} else {
3486 					freemsg(mp);
3487 				}
3488 			} else {
3489 				BUMP_MIB(&ip_mib, ipReasmFails);
3490 				if (hdr_length != 0) {
3491 					mp->b_next = send_icmp_head;
3492 					send_icmp_head = mp;
3493 				} else {
3494 					freemsg(mp);
3495 				}
3496 			}
3497 			freeb(ipf->ipf_mp);
3498 		}
3499 		mutex_exit(&ipfb->ipfb_lock);
3500 		/*
3501 		 * Now need to send any icmp messages that we delayed from
3502 		 * above.
3503 		 */
3504 		while (send_icmp_head_v6 != NULL) {
3505 			mp = send_icmp_head_v6;
3506 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3507 			mp->b_next = NULL;
3508 			icmp_time_exceeded_v6(ill->ill_wq, mp,
3509 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, B_FALSE);
3510 		}
3511 		while (send_icmp_head != NULL) {
3512 			mp = send_icmp_head;
3513 			send_icmp_head = send_icmp_head->b_next;
3514 			mp->b_next = NULL;
3515 			icmp_time_exceeded(ill->ill_wq, mp,
3516 			    ICMP_REASSEMBLY_TIME_EXCEEDED);
3517 		}
3518 	}
3519 	/*
3520 	 * A non-dying ILL will use the return value to decide whether to
3521 	 * restart the frag timer, and for how long.
3522 	 */
3523 	return (next_timeout);
3524 }
3525 
3526 /*
3527  * This routine is called when the approximate count of mblk memory used
3528  * for the specified ILL has exceeded max_count.
3529  */
3530 void
3531 ill_frag_prune(ill_t *ill, uint_t max_count)
3532 {
3533 	ipfb_t	*ipfb;
3534 	ipf_t	*ipf;
3535 	size_t	count;
3536 
3537 	/*
3538 	 * If we are here within ip_min_frag_prune_time msecs remove
3539 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3540 	 * ill_frag_free_num_pkts.
3541 	 */
3542 	mutex_enter(&ill->ill_lock);
3543 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3544 	    (ip_min_frag_prune_time != 0 ?
3545 	    ip_min_frag_prune_time : msec_per_tick)) {
3546 
3547 		ill->ill_frag_free_num_pkts++;
3548 
3549 	} else {
3550 		ill->ill_frag_free_num_pkts = 0;
3551 	}
3552 	ill->ill_last_frag_clean_time = lbolt;
3553 	mutex_exit(&ill->ill_lock);
3554 
3555 	/*
3556 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3557 	 */
3558 	if (ill->ill_frag_free_num_pkts != 0) {
3559 		int ix;
3560 
3561 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3562 			ipfb = &ill->ill_frag_hash_tbl[ix];
3563 			mutex_enter(&ipfb->ipfb_lock);
3564 			if (ipfb->ipfb_ipf != NULL) {
3565 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3566 				    ill->ill_frag_free_num_pkts);
3567 			}
3568 			mutex_exit(&ipfb->ipfb_lock);
3569 		}
3570 	}
3571 	/*
3572 	 * While the reassembly list for this ILL is too big, prune a fragment
3573 	 * queue by age, oldest first.  Note that the per ILL count is
3574 	 * approximate, while the per frag hash bucket counts are accurate.
3575 	 */
3576 	while (ill->ill_frag_count > max_count) {
3577 		int	ix;
3578 		ipfb_t	*oipfb = NULL;
3579 		uint_t	oldest = UINT_MAX;
3580 
3581 		count = 0;
3582 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3583 			ipfb = &ill->ill_frag_hash_tbl[ix];
3584 			mutex_enter(&ipfb->ipfb_lock);
3585 			ipf = ipfb->ipfb_ipf;
3586 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3587 				oldest = ipf->ipf_gen;
3588 				oipfb = ipfb;
3589 			}
3590 			count += ipfb->ipfb_count;
3591 			mutex_exit(&ipfb->ipfb_lock);
3592 		}
3593 		/* Refresh the per ILL count */
3594 		ill->ill_frag_count = count;
3595 		if (oipfb == NULL) {
3596 			ill->ill_frag_count = 0;
3597 			break;
3598 		}
3599 		if (count <= max_count)
3600 			return;	/* Somebody beat us to it, nothing to do */
3601 		mutex_enter(&oipfb->ipfb_lock);
3602 		ipf = oipfb->ipfb_ipf;
3603 		if (ipf != NULL) {
3604 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3605 		}
3606 		mutex_exit(&oipfb->ipfb_lock);
3607 	}
3608 }
3609 
3610 /*
3611  * free 'free_cnt' fragmented packets starting at ipf.
3612  */
3613 void
3614 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3615 {
3616 	size_t	count;
3617 	mblk_t	*mp;
3618 	mblk_t	*tmp;
3619 	ipf_t **ipfp = ipf->ipf_ptphn;
3620 
3621 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3622 	ASSERT(ipfp != NULL);
3623 	ASSERT(ipf != NULL);
3624 
3625 	while (ipf != NULL && free_cnt-- > 0) {
3626 		count = ipf->ipf_count;
3627 		mp = ipf->ipf_mp;
3628 		ipf = ipf->ipf_hash_next;
3629 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3630 			IP_REASS_SET_START(tmp, 0);
3631 			IP_REASS_SET_END(tmp, 0);
3632 		}
3633 		ill->ill_frag_count -= count;
3634 		ASSERT(ipfb->ipfb_count >= count);
3635 		ipfb->ipfb_count -= count;
3636 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3637 		ipfb->ipfb_frag_pkts--;
3638 		freemsg(mp);
3639 		BUMP_MIB(&ip_mib, ipReasmFails);
3640 	}
3641 
3642 	if (ipf)
3643 		ipf->ipf_ptphn = ipfp;
3644 	ipfp[0] = ipf;
3645 }
3646 
3647 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3648 	"obsolete and may be removed in a future release of Solaris.  Use " \
3649 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3650 
3651 /*
3652  * For obsolete per-interface forwarding configuration;
3653  * called in response to ND_GET.
3654  */
3655 /* ARGSUSED */
3656 static int
3657 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3658 {
3659 	ill_t *ill = (ill_t *)cp;
3660 
3661 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3662 
3663 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3664 	return (0);
3665 }
3666 
3667 /*
3668  * For obsolete per-interface forwarding configuration;
3669  * called in response to ND_SET.
3670  */
3671 /* ARGSUSED */
3672 static int
3673 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3674     cred_t *ioc_cr)
3675 {
3676 	long value;
3677 	int retval;
3678 
3679 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3680 
3681 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3682 	    value < 0 || value > 1) {
3683 		return (EINVAL);
3684 	}
3685 
3686 	rw_enter(&ill_g_lock, RW_READER);
3687 	retval = ill_forward_set(q, mp, (value != 0), cp);
3688 	rw_exit(&ill_g_lock);
3689 	return (retval);
3690 }
3691 
3692 /*
3693  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3694  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3695  * up RTS_IFINFO routing socket messages for each interface whose flags we
3696  * change.
3697  */
3698 /* ARGSUSED */
3699 int
3700 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3701 {
3702 	ill_t *ill = (ill_t *)cp;
3703 	ill_group_t *illgrp;
3704 
3705 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3706 
3707 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3708 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3709 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3710 		return (EINVAL);
3711 
3712 	/*
3713 	 * If the ill is in an IPMP group, set the forwarding policy on all
3714 	 * members of the group to the same value.
3715 	 */
3716 	illgrp = ill->ill_group;
3717 	if (illgrp != NULL) {
3718 		ill_t *tmp_ill;
3719 
3720 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3721 		    tmp_ill = tmp_ill->ill_group_next) {
3722 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3723 			    (enable ? "Enabling" : "Disabling"),
3724 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3725 			    tmp_ill->ill_name));
3726 			mutex_enter(&tmp_ill->ill_lock);
3727 			if (enable)
3728 				tmp_ill->ill_flags |= ILLF_ROUTER;
3729 			else
3730 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3731 			mutex_exit(&tmp_ill->ill_lock);
3732 			if (tmp_ill->ill_isv6)
3733 				ill_set_nce_router_flags(tmp_ill, enable);
3734 			/* Notify routing socket listeners of this change. */
3735 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3736 		}
3737 	} else {
3738 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3739 		    (enable ? "Enabling" : "Disabling"),
3740 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3741 		mutex_enter(&ill->ill_lock);
3742 		if (enable)
3743 			ill->ill_flags |= ILLF_ROUTER;
3744 		else
3745 			ill->ill_flags &= ~ILLF_ROUTER;
3746 		mutex_exit(&ill->ill_lock);
3747 		if (ill->ill_isv6)
3748 			ill_set_nce_router_flags(ill, enable);
3749 		/* Notify routing socket listeners of this change. */
3750 		ip_rts_ifmsg(ill->ill_ipif);
3751 	}
3752 
3753 	return (0);
3754 }
3755 
3756 /*
3757  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3758  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3759  * set or clear.
3760  */
3761 static void
3762 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3763 {
3764 	ipif_t *ipif;
3765 	nce_t *nce;
3766 
3767 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3768 		nce = ndp_lookup(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3769 		if (nce != NULL) {
3770 			mutex_enter(&nce->nce_lock);
3771 			if (enable)
3772 				nce->nce_flags |= NCE_F_ISROUTER;
3773 			else
3774 				nce->nce_flags &= ~NCE_F_ISROUTER;
3775 			mutex_exit(&nce->nce_lock);
3776 			NCE_REFRELE(nce);
3777 		}
3778 	}
3779 }
3780 
3781 /*
3782  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3783  * for this ill.  Make sure the v6/v4 question has been answered about this
3784  * ill.  The creation of this ndd variable is only for backwards compatibility.
3785  * The preferred way to control per-interface IP forwarding is through the
3786  * ILLF_ROUTER interface flag.
3787  */
3788 static int
3789 ill_set_ndd_name(ill_t *ill)
3790 {
3791 	char *suffix;
3792 
3793 	ASSERT(IAM_WRITER_ILL(ill));
3794 
3795 	if (ill->ill_isv6)
3796 		suffix = ipv6_forward_suffix;
3797 	else
3798 		suffix = ipv4_forward_suffix;
3799 
3800 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3801 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3802 	/*
3803 	 * Copies over the '\0'.
3804 	 * Note that strlen(suffix) is always bounded.
3805 	 */
3806 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3807 	    strlen(suffix) + 1);
3808 
3809 	/*
3810 	 * Use of the nd table requires holding the reader lock.
3811 	 * Modifying the nd table thru nd_load/nd_unload requires
3812 	 * the writer lock.
3813 	 */
3814 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3815 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3816 	    nd_ill_forward_set, (caddr_t)ill)) {
3817 		/*
3818 		 * If the nd_load failed, it only meant that it could not
3819 		 * allocate a new bunch of room for further NDD expansion.
3820 		 * Because of that, the ill_ndd_name will be set to 0, and
3821 		 * this interface is at the mercy of the global ip_forwarding
3822 		 * variable.
3823 		 */
3824 		rw_exit(&ip_g_nd_lock);
3825 		ill->ill_ndd_name = NULL;
3826 		return (ENOMEM);
3827 	}
3828 	rw_exit(&ip_g_nd_lock);
3829 	return (0);
3830 }
3831 
3832 /*
3833  * Intializes the context structure and returns the first ill in the list
3834  * cuurently start_list and end_list can have values:
3835  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3836  * IP_V4_G_HEAD		Traverse IPV4 list only.
3837  * IP_V6_G_HEAD		Traverse IPV6 list only.
3838  */
3839 
3840 /*
3841  * We don't check for CONDEMNED ills here. Caller must do that if
3842  * necessary under the ill lock.
3843  */
3844 ill_t *
3845 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3846 {
3847 	ill_if_t *ifp;
3848 	ill_t *ill;
3849 	avl_tree_t *avl_tree;
3850 
3851 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3852 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3853 
3854 	/*
3855 	 * setup the lists to search
3856 	 */
3857 	if (end_list != MAX_G_HEADS) {
3858 		ctx->ctx_current_list = start_list;
3859 		ctx->ctx_last_list = end_list;
3860 	} else {
3861 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3862 		ctx->ctx_current_list = 0;
3863 	}
3864 
3865 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3866 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3867 		if (ifp != (ill_if_t *)
3868 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3869 			avl_tree = &ifp->illif_avl_by_ppa;
3870 			ill = avl_first(avl_tree);
3871 			/*
3872 			 * ill is guaranteed to be non NULL or ifp should have
3873 			 * not existed.
3874 			 */
3875 			ASSERT(ill != NULL);
3876 			return (ill);
3877 		}
3878 		ctx->ctx_current_list++;
3879 	}
3880 
3881 	return (NULL);
3882 }
3883 
3884 /*
3885  * returns the next ill in the list. ill_first() must have been called
3886  * before calling ill_next() or bad things will happen.
3887  */
3888 
3889 /*
3890  * We don't check for CONDEMNED ills here. Caller must do that if
3891  * necessary under the ill lock.
3892  */
3893 ill_t *
3894 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3895 {
3896 	ill_if_t *ifp;
3897 	ill_t *ill;
3898 
3899 
3900 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3901 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3902 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
3903 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3904 	    AVL_AFTER)) != NULL) {
3905 		return (ill);
3906 	}
3907 
3908 	/* goto next ill_ifp in the list. */
3909 	ifp = lastill->ill_ifptr->illif_next;
3910 
3911 	/* make sure not at end of circular list */
3912 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3913 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3914 			return (NULL);
3915 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3916 	}
3917 
3918 	return (avl_first(&ifp->illif_avl_by_ppa));
3919 }
3920 
3921 /*
3922  * Check interface name for correct format which is name+ppa.
3923  * name can contain characters and digits, the right most digits
3924  * make up the ppa number. use of octal is not allowed, name must contain
3925  * a ppa, return pointer to the start of ppa.
3926  * In case of error return NULL.
3927  */
3928 static char *
3929 ill_get_ppa_ptr(char *name)
3930 {
3931 	int namelen = mi_strlen(name);
3932 
3933 	int len = namelen;
3934 
3935 	name += len;
3936 	while (len > 0) {
3937 		name--;
3938 		if (*name < '0' || *name > '9')
3939 			break;
3940 		len--;
3941 	}
3942 
3943 	/* empty string, all digits, or no trailing digits */
3944 	if (len == 0 || len == (int)namelen)
3945 		return (NULL);
3946 
3947 	name++;
3948 	/* check for attempted use of octal */
3949 	if (*name == '0' && len != (int)namelen - 1)
3950 		return (NULL);
3951 	return (name);
3952 }
3953 
3954 /*
3955  * use avl tree to locate the ill.
3956  */
3957 static ill_t *
3958 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3959     ipsq_func_t func, int *error)
3960 {
3961 	char *ppa_ptr = NULL;
3962 	int len;
3963 	uint_t ppa;
3964 	ill_t *ill = NULL;
3965 	ill_if_t *ifp;
3966 	int list;
3967 	ipsq_t *ipsq;
3968 
3969 	if (error != NULL)
3970 		*error = 0;
3971 
3972 	/*
3973 	 * get ppa ptr
3974 	 */
3975 	if (isv6)
3976 		list = IP_V6_G_HEAD;
3977 	else
3978 		list = IP_V4_G_HEAD;
3979 
3980 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3981 		if (error != NULL)
3982 			*error = ENXIO;
3983 		return (NULL);
3984 	}
3985 
3986 	len = ppa_ptr - name + 1;
3987 
3988 	ppa = stoi(&ppa_ptr);
3989 
3990 	ifp = IP_VX_ILL_G_LIST(list);
3991 
3992 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
3993 		/*
3994 		 * match is done on len - 1 as the name is not null
3995 		 * terminated it contains ppa in addition to the interface
3996 		 * name.
3997 		 */
3998 		if ((ifp->illif_name_len == len) &&
3999 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4000 			break;
4001 		} else {
4002 			ifp = ifp->illif_next;
4003 		}
4004 	}
4005 
4006 
4007 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4008 		/*
4009 		 * Even the interface type does not exist.
4010 		 */
4011 		if (error != NULL)
4012 			*error = ENXIO;
4013 		return (NULL);
4014 	}
4015 
4016 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4017 	if (ill != NULL) {
4018 		/*
4019 		 * The block comment at the start of ipif_down
4020 		 * explains the use of the macros used below
4021 		 */
4022 		GRAB_CONN_LOCK(q);
4023 		mutex_enter(&ill->ill_lock);
4024 		if (ILL_CAN_LOOKUP(ill)) {
4025 			ill_refhold_locked(ill);
4026 			mutex_exit(&ill->ill_lock);
4027 			RELEASE_CONN_LOCK(q);
4028 			return (ill);
4029 		} else if (ILL_CAN_WAIT(ill, q)) {
4030 			ipsq = ill->ill_phyint->phyint_ipsq;
4031 			mutex_enter(&ipsq->ipsq_lock);
4032 			mutex_exit(&ill->ill_lock);
4033 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4034 			mutex_exit(&ipsq->ipsq_lock);
4035 			RELEASE_CONN_LOCK(q);
4036 			*error = EINPROGRESS;
4037 			return (NULL);
4038 		}
4039 		mutex_exit(&ill->ill_lock);
4040 		RELEASE_CONN_LOCK(q);
4041 	}
4042 	if (error != NULL)
4043 		*error = ENXIO;
4044 	return (NULL);
4045 }
4046 
4047 /*
4048  * comparison function for use with avl.
4049  */
4050 static int
4051 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4052 {
4053 	uint_t ppa;
4054 	uint_t ill_ppa;
4055 
4056 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4057 
4058 	ppa = *((uint_t *)ppa_ptr);
4059 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4060 	/*
4061 	 * We want the ill with the lowest ppa to be on the
4062 	 * top.
4063 	 */
4064 	if (ill_ppa < ppa)
4065 		return (1);
4066 	if (ill_ppa > ppa)
4067 		return (-1);
4068 	return (0);
4069 }
4070 
4071 /*
4072  * remove an interface type from the global list.
4073  */
4074 static void
4075 ill_delete_interface_type(ill_if_t *interface)
4076 {
4077 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4078 
4079 	ASSERT(interface != NULL);
4080 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4081 
4082 	avl_destroy(&interface->illif_avl_by_ppa);
4083 	if (interface->illif_ppa_arena != NULL)
4084 		vmem_destroy(interface->illif_ppa_arena);
4085 
4086 	remque(interface);
4087 
4088 	mi_free(interface);
4089 }
4090 
4091 /*
4092  * remove ill from the global list.
4093  */
4094 static void
4095 ill_glist_delete(ill_t *ill)
4096 {
4097 	if (ill == NULL)
4098 		return;
4099 
4100 	rw_enter(&ill_g_lock, RW_WRITER);
4101 	/*
4102 	 * If the ill was never inserted into the AVL tree
4103 	 * we skip the if branch.
4104 	 */
4105 	if (ill->ill_ifptr != NULL) {
4106 		/*
4107 		 * remove from AVL tree and free ppa number
4108 		 */
4109 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4110 
4111 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4112 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4113 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4114 		}
4115 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4116 			ill_delete_interface_type(ill->ill_ifptr);
4117 		}
4118 
4119 		/*
4120 		 * Indicate ill is no longer in the list.
4121 		 */
4122 		ill->ill_ifptr = NULL;
4123 		ill->ill_name_length = 0;
4124 		ill->ill_name[0] = '\0';
4125 		ill->ill_ppa = UINT_MAX;
4126 	}
4127 	ill_phyint_free(ill);
4128 	rw_exit(&ill_g_lock);
4129 }
4130 
4131 /*
4132  * allocate a ppa, if the number of plumbed interfaces of this type are
4133  * less than ill_no_arena do a linear search to find a unused ppa.
4134  * When the number goes beyond ill_no_arena switch to using an arena.
4135  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4136  * is the return value for an error condition, so allocation starts at one
4137  * and is decremented by one.
4138  */
4139 static int
4140 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4141 {
4142 	ill_t *tmp_ill;
4143 	uint_t start, end;
4144 	int ppa;
4145 
4146 	if (ifp->illif_ppa_arena == NULL &&
4147 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4148 		/*
4149 		 * Create an arena.
4150 		 */
4151 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4152 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4153 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4154 			/* allocate what has already been assigned */
4155 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4156 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4157 		    tmp_ill, AVL_AFTER)) {
4158 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4159 			    1,		/* size */
4160 			    1,		/* align/quantum */
4161 			    0,		/* phase */
4162 			    0,		/* nocross */
4163 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4164 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4165 			    VM_NOSLEEP|VM_FIRSTFIT);
4166 			if (ppa == 0) {
4167 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4168 				    " failed while switching"));
4169 				vmem_destroy(ifp->illif_ppa_arena);
4170 				ifp->illif_ppa_arena = NULL;
4171 				break;
4172 			}
4173 		}
4174 	}
4175 
4176 	if (ifp->illif_ppa_arena != NULL) {
4177 		if (ill->ill_ppa == UINT_MAX) {
4178 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4179 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4180 			if (ppa == 0)
4181 				return (EAGAIN);
4182 			ill->ill_ppa = --ppa;
4183 		} else {
4184 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4185 			    1, 		/* size */
4186 			    1, 		/* align/quantum */
4187 			    0, 		/* phase */
4188 			    0, 		/* nocross */
4189 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4190 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4191 			    VM_NOSLEEP|VM_FIRSTFIT);
4192 			/*
4193 			 * Most likely the allocation failed because
4194 			 * the requested ppa was in use.
4195 			 */
4196 			if (ppa == 0)
4197 				return (EEXIST);
4198 		}
4199 		return (0);
4200 	}
4201 
4202 	/*
4203 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4204 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4205 	 */
4206 	if (ill->ill_ppa == UINT_MAX) {
4207 		end = UINT_MAX - 1;
4208 		start = 0;
4209 	} else {
4210 		end = start = ill->ill_ppa;
4211 	}
4212 
4213 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4214 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4215 		if (start++ >= end) {
4216 			if (ill->ill_ppa == UINT_MAX)
4217 				return (EAGAIN);
4218 			else
4219 				return (EEXIST);
4220 		}
4221 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4222 	}
4223 	ill->ill_ppa = start;
4224 	return (0);
4225 }
4226 
4227 /*
4228  * Insert ill into the list of configured ill's. Once this function completes,
4229  * the ill is globally visible and is available through lookups. More precisely
4230  * this happens after the caller drops the ill_g_lock.
4231  */
4232 static int
4233 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4234 {
4235 	ill_if_t *ill_interface;
4236 	avl_index_t where = 0;
4237 	int error;
4238 	int name_length;
4239 	int index;
4240 	boolean_t check_length = B_FALSE;
4241 
4242 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4243 
4244 	name_length = mi_strlen(name) + 1;
4245 
4246 	if (isv6)
4247 		index = IP_V6_G_HEAD;
4248 	else
4249 		index = IP_V4_G_HEAD;
4250 
4251 	ill_interface = IP_VX_ILL_G_LIST(index);
4252 	/*
4253 	 * Search for interface type based on name
4254 	 */
4255 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4256 		if ((ill_interface->illif_name_len == name_length) &&
4257 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4258 			break;
4259 		}
4260 		ill_interface = ill_interface->illif_next;
4261 	}
4262 
4263 	/*
4264 	 * Interface type not found, create one.
4265 	 */
4266 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4267 
4268 		ill_g_head_t ghead;
4269 
4270 		/*
4271 		 * allocate ill_if_t structure
4272 		 */
4273 
4274 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4275 		if (ill_interface == NULL) {
4276 			return (ENOMEM);
4277 		}
4278 
4279 
4280 
4281 		(void) strcpy(ill_interface->illif_name, name);
4282 		ill_interface->illif_name_len = name_length;
4283 
4284 		avl_create(&ill_interface->illif_avl_by_ppa,
4285 		    ill_compare_ppa, sizeof (ill_t),
4286 		    offsetof(struct ill_s, ill_avl_byppa));
4287 
4288 		/*
4289 		 * link the structure in the back to maintain order
4290 		 * of configuration for ifconfig output.
4291 		 */
4292 		ghead = ill_g_heads[index];
4293 		insque(ill_interface, ghead.ill_g_list_tail);
4294 
4295 	}
4296 
4297 	if (ill->ill_ppa == UINT_MAX)
4298 		check_length = B_TRUE;
4299 
4300 	error = ill_alloc_ppa(ill_interface, ill);
4301 	if (error != 0) {
4302 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4303 			ill_delete_interface_type(ill->ill_ifptr);
4304 		return (error);
4305 	}
4306 
4307 	/*
4308 	 * When the ppa is choosen by the system, check that there is
4309 	 * enough space to insert ppa. if a specific ppa was passed in this
4310 	 * check is not required as the interface name passed in will have
4311 	 * the right ppa in it.
4312 	 */
4313 	if (check_length) {
4314 		/*
4315 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4316 		 */
4317 		char buf[sizeof (uint_t) * 3];
4318 
4319 		/*
4320 		 * convert ppa to string to calculate the amount of space
4321 		 * required for it in the name.
4322 		 */
4323 		numtos(ill->ill_ppa, buf);
4324 
4325 		/* Do we have enough space to insert ppa ? */
4326 
4327 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4328 			/* Free ppa and interface type struct */
4329 			if (ill_interface->illif_ppa_arena != NULL) {
4330 				vmem_free(ill_interface->illif_ppa_arena,
4331 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4332 			}
4333 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4334 			    0) {
4335 				ill_delete_interface_type(ill->ill_ifptr);
4336 			}
4337 
4338 			return (EINVAL);
4339 		}
4340 	}
4341 
4342 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4343 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4344 
4345 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4346 	    &where);
4347 	ill->ill_ifptr = ill_interface;
4348 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4349 
4350 	ill_phyint_reinit(ill);
4351 	return (0);
4352 }
4353 
4354 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4355 static boolean_t
4356 ipsq_init(ill_t *ill)
4357 {
4358 	ipsq_t  *ipsq;
4359 
4360 	/* Init the ipsq and impicitly enter as writer */
4361 	ill->ill_phyint->phyint_ipsq =
4362 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4363 	if (ill->ill_phyint->phyint_ipsq == NULL)
4364 		return (B_FALSE);
4365 	ipsq = ill->ill_phyint->phyint_ipsq;
4366 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4367 	ill->ill_phyint->phyint_ipsq_next = NULL;
4368 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4369 	ipsq->ipsq_refs = 1;
4370 	ipsq->ipsq_writer = curthread;
4371 	ipsq->ipsq_reentry_cnt = 1;
4372 #ifdef ILL_DEBUG
4373 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4374 #endif
4375 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4376 	return (B_TRUE);
4377 }
4378 
4379 /*
4380  * ill_init is called by ip_open when a device control stream is opened.
4381  * It does a few initializations, and shoots a DL_INFO_REQ message down
4382  * to the driver.  The response is later picked up in ip_rput_dlpi and
4383  * used to set up default mechanisms for talking to the driver.  (Always
4384  * called as writer.)
4385  *
4386  * If this function returns error, ip_open will call ip_close which in
4387  * turn will call ill_delete to clean up any memory allocated here that
4388  * is not yet freed.
4389  */
4390 int
4391 ill_init(queue_t *q, ill_t *ill)
4392 {
4393 	int	count;
4394 	dl_info_req_t	*dlir;
4395 	mblk_t	*info_mp;
4396 	uchar_t *frag_ptr;
4397 
4398 	/*
4399 	 * The ill is initialized to zero by mi_alloc*(). In addition
4400 	 * some fields already contain valid values, initialized in
4401 	 * ip_open(), before we reach here.
4402 	 */
4403 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4404 
4405 	ill->ill_rq = q;
4406 	ill->ill_wq = WR(q);
4407 
4408 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4409 	    BPRI_HI);
4410 	if (info_mp == NULL)
4411 		return (ENOMEM);
4412 
4413 	/*
4414 	 * Allocate sufficient space to contain our fragment hash table and
4415 	 * the device name.
4416 	 */
4417 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4418 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4419 	if (frag_ptr == NULL) {
4420 		freemsg(info_mp);
4421 		return (ENOMEM);
4422 	}
4423 	ill->ill_frag_ptr = frag_ptr;
4424 	ill->ill_frag_free_num_pkts = 0;
4425 	ill->ill_last_frag_clean_time = 0;
4426 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4427 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4428 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4429 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4430 		    NULL, MUTEX_DEFAULT, NULL);
4431 	}
4432 
4433 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4434 	if (ill->ill_phyint == NULL) {
4435 		freemsg(info_mp);
4436 		mi_free(frag_ptr);
4437 		return (ENOMEM);
4438 	}
4439 
4440 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4441 	/*
4442 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4443 	 * at this point because of the following reason. If we can't
4444 	 * enter the ipsq at some point and cv_wait, the writer that
4445 	 * wakes us up tries to locate us using the list of all phyints
4446 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4447 	 * If we don't set it now, we risk a missed wakeup.
4448 	 */
4449 	ill->ill_phyint->phyint_illv4 = ill;
4450 	ill->ill_ppa = UINT_MAX;
4451 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4452 
4453 	if (!ipsq_init(ill)) {
4454 		freemsg(info_mp);
4455 		mi_free(frag_ptr);
4456 		mi_free(ill->ill_phyint);
4457 		return (ENOMEM);
4458 	}
4459 
4460 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4461 
4462 
4463 	/* Frag queue limit stuff */
4464 	ill->ill_frag_count = 0;
4465 	ill->ill_ipf_gen = 0;
4466 
4467 	ill->ill_global_timer = INFINITY;
4468 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4469 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4470 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4471 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4472 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4473 
4474 	/*
4475 	 * Initialize IPv6 configuration variables.  The IP module is always
4476 	 * opened as an IPv4 module.  Instead tracking down the cases where
4477 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4478 	 * here for convenience, this has no effect until the ill is set to do
4479 	 * IPv6.
4480 	 */
4481 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4482 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4483 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4484 	ill->ill_max_buf = ND_MAX_Q;
4485 	ill->ill_refcnt = 0;
4486 
4487 	/* Send down the Info Request to the driver. */
4488 	info_mp->b_datap->db_type = M_PCPROTO;
4489 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4490 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4491 	dlir->dl_primitive = DL_INFO_REQ;
4492 
4493 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4494 
4495 	qprocson(q);
4496 	ill_dlpi_send(ill, info_mp);
4497 
4498 	return (0);
4499 }
4500 
4501 /*
4502  * ill_dls_info
4503  * creates datalink socket info from the device.
4504  */
4505 int
4506 ill_dls_info(struct sockaddr_dl *sdl, ipif_t *ipif)
4507 {
4508 	size_t	length;
4509 	ill_t	*ill = ipif->ipif_ill;
4510 
4511 	sdl->sdl_family = AF_LINK;
4512 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4513 	sdl->sdl_type = ipif->ipif_type;
4514 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4515 	length = mi_strlen(sdl->sdl_data);
4516 	ASSERT(length < 256);
4517 	sdl->sdl_nlen = (uchar_t)length;
4518 	sdl->sdl_alen = ill->ill_phys_addr_length;
4519 	mutex_enter(&ill->ill_lock);
4520 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4521 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4522 		    ill->ill_phys_addr_length);
4523 	}
4524 	mutex_exit(&ill->ill_lock);
4525 	sdl->sdl_slen = 0;
4526 	return (sizeof (struct sockaddr_dl));
4527 }
4528 
4529 /*
4530  * ill_xarp_info
4531  * creates xarp info from the device.
4532  */
4533 static int
4534 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4535 {
4536 	sdl->sdl_family = AF_LINK;
4537 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4538 	sdl->sdl_type = ill->ill_type;
4539 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4540 	    sizeof (sdl->sdl_data));
4541 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4542 	sdl->sdl_alen = ill->ill_phys_addr_length;
4543 	sdl->sdl_slen = 0;
4544 	return (sdl->sdl_nlen);
4545 }
4546 
4547 static int
4548 loopback_kstat_update(kstat_t *ksp, int rw)
4549 {
4550 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4551 
4552 	if (rw == KSTAT_WRITE)
4553 		return (EACCES);
4554 	kn[0].value.ui32 = loopback_packets;
4555 	kn[1].value.ui32 = loopback_packets;
4556 	return (0);
4557 }
4558 
4559 
4560 /*
4561  * Has ifindex been plumbed already.
4562  */
4563 static boolean_t
4564 phyint_exists(uint_t index)
4565 {
4566 	phyint_t *phyi;
4567 
4568 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4569 	/*
4570 	 * Indexes are stored in the phyint - a common structure
4571 	 * to both IPv4 and IPv6.
4572 	 */
4573 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4574 	    (void *) &index, NULL);
4575 	return (phyi != NULL);
4576 }
4577 
4578 /*
4579  * Assign a unique interface index for the phyint.
4580  */
4581 static boolean_t
4582 phyint_assign_ifindex(phyint_t *phyi)
4583 {
4584 	uint_t starting_index;
4585 
4586 	ASSERT(phyi->phyint_ifindex == 0);
4587 	if (!ill_index_wrap) {
4588 		phyi->phyint_ifindex = ill_index++;
4589 		if (ill_index == 0) {
4590 			/* Reached the uint_t limit Next time wrap  */
4591 			ill_index_wrap = B_TRUE;
4592 		}
4593 		return (B_TRUE);
4594 	}
4595 
4596 	/*
4597 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4598 	 * at this point and don't want to call any function that attempts
4599 	 * to get the lock again.
4600 	 */
4601 	starting_index = ill_index++;
4602 	for (; ill_index != starting_index; ill_index++) {
4603 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4604 			/* found unused index - use it */
4605 			phyi->phyint_ifindex = ill_index;
4606 			return (B_TRUE);
4607 		}
4608 	}
4609 
4610 	/*
4611 	 * all interface indicies are inuse.
4612 	 */
4613 	return (B_FALSE);
4614 }
4615 
4616 /*
4617  * Return a pointer to the ill which matches the supplied name.  Note that
4618  * the ill name length includes the null termination character.  (May be
4619  * called as writer.)
4620  * If do_alloc and the interface is "lo0" it will be automatically created.
4621  * Cannot bump up reference on condemned ills. So dup detect can't be done
4622  * using this func.
4623  */
4624 ill_t *
4625 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4626     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4627 {
4628 	ill_t	*ill;
4629 	ipif_t	*ipif;
4630 	kstat_named_t	*kn;
4631 	boolean_t isloopback;
4632 	ipsq_t *old_ipsq;
4633 
4634 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4635 
4636 	rw_enter(&ill_g_lock, RW_READER);
4637 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4638 	rw_exit(&ill_g_lock);
4639 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4640 		return (ill);
4641 
4642 	/*
4643 	 * Couldn't find it.  Does this happen to be a lookup for the
4644 	 * loopback device and are we allowed to allocate it?
4645 	 */
4646 	if (!isloopback || !do_alloc)
4647 		return (NULL);
4648 
4649 	rw_enter(&ill_g_lock, RW_WRITER);
4650 
4651 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4652 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4653 		rw_exit(&ill_g_lock);
4654 		return (ill);
4655 	}
4656 
4657 	/* Create the loopback device on demand */
4658 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4659 	    sizeof (ipif_loopback_name), BPRI_MED));
4660 	if (ill == NULL)
4661 		goto done;
4662 
4663 	*ill = ill_null;
4664 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4665 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4666 	if (ill->ill_phyint == NULL)
4667 		goto done;
4668 
4669 	if (isv6)
4670 		ill->ill_phyint->phyint_illv6 = ill;
4671 	else
4672 		ill->ill_phyint->phyint_illv4 = ill;
4673 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4674 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4675 	/* Add room for tcp+ip headers */
4676 	if (isv6) {
4677 		ill->ill_isv6 = B_TRUE;
4678 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4679 		if (!ill_allocate_mibs(ill))
4680 			goto done;
4681 	} else {
4682 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4683 	}
4684 	ill->ill_max_mtu = ill->ill_max_frag;
4685 	/*
4686 	 * ipif_loopback_name can't be pointed at directly because its used
4687 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4688 	 * from the glist, ill_glist_delete() sets the first character of
4689 	 * ill_name to '\0'.
4690 	 */
4691 	ill->ill_name = (char *)ill + sizeof (*ill);
4692 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4693 	ill->ill_name_length = sizeof (ipif_loopback_name);
4694 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4695 
4696 	ill->ill_global_timer = INFINITY;
4697 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4698 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4699 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4700 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4701 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4702 
4703 	/* No resolver here. */
4704 	ill->ill_net_type = IRE_LOOPBACK;
4705 
4706 	/* Initialize the ipsq */
4707 	if (!ipsq_init(ill))
4708 		goto done;
4709 
4710 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4711 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4712 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4713 #ifdef ILL_DEBUG
4714 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4715 #endif
4716 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4717 	if (ipif == NULL)
4718 		goto done;
4719 
4720 	ill->ill_flags = ILLF_MULTICAST;
4721 
4722 	/* Set up default loopback address and mask. */
4723 	if (!isv6) {
4724 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4725 
4726 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4727 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4728 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4729 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4730 		    ipif->ipif_v6subnet);
4731 		ill->ill_flags |= ILLF_IPV4;
4732 	} else {
4733 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4734 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4735 		ipif->ipif_v6net_mask = ipv6_all_ones;
4736 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4737 		    ipif->ipif_v6subnet);
4738 		ill->ill_flags |= ILLF_IPV6;
4739 	}
4740 
4741 	/*
4742 	 * Chain us in at the end of the ill list. hold the ill
4743 	 * before we make it globally visible. 1 for the lookup.
4744 	 */
4745 	ill->ill_refcnt = 0;
4746 	ill_refhold(ill);
4747 
4748 	ill->ill_frag_count = 0;
4749 	ill->ill_frag_free_num_pkts = 0;
4750 	ill->ill_last_frag_clean_time = 0;
4751 
4752 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4753 
4754 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4755 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4756 
4757 	/* Let SCTP know so that it can add this to its list */
4758 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4759 
4760 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4761 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4762 
4763 	/*
4764 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4765 	 */
4766 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4767 		/* Loopback ills aren't in any IPMP group */
4768 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4769 		ipsq_delete(old_ipsq);
4770 	}
4771 
4772 	/*
4773 	 * Delay this till the ipif is allocated as ipif_allocate
4774 	 * de-references ill_phyint for getting the ifindex. We
4775 	 * can't do this before ipif_allocate because ill_phyint_reinit
4776 	 * -> phyint_assign_ifindex expects ipif to be present.
4777 	 */
4778 	mutex_enter(&ill->ill_phyint->phyint_lock);
4779 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4780 	mutex_exit(&ill->ill_phyint->phyint_lock);
4781 
4782 	if (loopback_ksp == NULL) {
4783 		/* Export loopback interface statistics */
4784 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4785 		    KSTAT_TYPE_NAMED, 2, 0);
4786 		if (loopback_ksp != NULL) {
4787 			loopback_ksp->ks_update = loopback_kstat_update;
4788 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4789 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4790 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4791 			kstat_install(loopback_ksp);
4792 		}
4793 	}
4794 
4795 	if (error != NULL)
4796 		*error = 0;
4797 	*did_alloc = B_TRUE;
4798 	rw_exit(&ill_g_lock);
4799 	return (ill);
4800 done:
4801 	if (ill != NULL) {
4802 		if (ill->ill_phyint != NULL) {
4803 			ipsq_t	*ipsq;
4804 
4805 			ipsq = ill->ill_phyint->phyint_ipsq;
4806 			if (ipsq != NULL)
4807 				kmem_free(ipsq, sizeof (ipsq_t));
4808 			mi_free(ill->ill_phyint);
4809 		}
4810 		ill_free_mib(ill);
4811 		mi_free(ill);
4812 	}
4813 	rw_exit(&ill_g_lock);
4814 	if (error != NULL)
4815 		*error = ENOMEM;
4816 	return (NULL);
4817 }
4818 
4819 /*
4820  * Return a pointer to the ill which matches the index and IP version type.
4821  */
4822 ill_t *
4823 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4824     ipsq_func_t func, int *err)
4825 {
4826 	ill_t	*ill;
4827 	ipsq_t  *ipsq;
4828 	phyint_t *phyi;
4829 
4830 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4831 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4832 
4833 	if (err != NULL)
4834 		*err = 0;
4835 
4836 	/*
4837 	 * Indexes are stored in the phyint - a common structure
4838 	 * to both IPv4 and IPv6.
4839 	 */
4840 	rw_enter(&ill_g_lock, RW_READER);
4841 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4842 	    (void *) &index, NULL);
4843 	if (phyi != NULL) {
4844 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4845 		if (ill != NULL) {
4846 			/*
4847 			 * The block comment at the start of ipif_down
4848 			 * explains the use of the macros used below
4849 			 */
4850 			GRAB_CONN_LOCK(q);
4851 			mutex_enter(&ill->ill_lock);
4852 			if (ILL_CAN_LOOKUP(ill)) {
4853 				ill_refhold_locked(ill);
4854 				mutex_exit(&ill->ill_lock);
4855 				RELEASE_CONN_LOCK(q);
4856 				rw_exit(&ill_g_lock);
4857 				return (ill);
4858 			} else if (ILL_CAN_WAIT(ill, q)) {
4859 				ipsq = ill->ill_phyint->phyint_ipsq;
4860 				mutex_enter(&ipsq->ipsq_lock);
4861 				rw_exit(&ill_g_lock);
4862 				mutex_exit(&ill->ill_lock);
4863 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4864 				mutex_exit(&ipsq->ipsq_lock);
4865 				RELEASE_CONN_LOCK(q);
4866 				*err = EINPROGRESS;
4867 				return (NULL);
4868 			}
4869 			RELEASE_CONN_LOCK(q);
4870 			mutex_exit(&ill->ill_lock);
4871 		}
4872 	}
4873 	rw_exit(&ill_g_lock);
4874 	if (err != NULL)
4875 		*err = ENXIO;
4876 	return (NULL);
4877 }
4878 
4879 /*
4880  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4881  * that gives a running thread a reference to the ill. This reference must be
4882  * released by the thread when it is done accessing the ill and related
4883  * objects. ill_refcnt can not be used to account for static references
4884  * such as other structures pointing to an ill. Callers must generally
4885  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4886  * or be sure that the ill is not being deleted or changing state before
4887  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4888  * ill won't change any of its critical state such as address, netmask etc.
4889  */
4890 void
4891 ill_refhold(ill_t *ill)
4892 {
4893 	mutex_enter(&ill->ill_lock);
4894 	ill->ill_refcnt++;
4895 	ILL_TRACE_REF(ill);
4896 	mutex_exit(&ill->ill_lock);
4897 }
4898 
4899 void
4900 ill_refhold_locked(ill_t *ill)
4901 {
4902 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4903 	ill->ill_refcnt++;
4904 	ILL_TRACE_REF(ill);
4905 }
4906 
4907 int
4908 ill_check_and_refhold(ill_t *ill)
4909 {
4910 	mutex_enter(&ill->ill_lock);
4911 	if (ILL_CAN_LOOKUP(ill)) {
4912 		ill_refhold_locked(ill);
4913 		mutex_exit(&ill->ill_lock);
4914 		return (0);
4915 	}
4916 	mutex_exit(&ill->ill_lock);
4917 	return (ILL_LOOKUP_FAILED);
4918 }
4919 
4920 /*
4921  * Must not be called while holding any locks. Otherwise if this is
4922  * the last reference to be released, there is a chance of recursive mutex
4923  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4924  * to restart an ioctl.
4925  */
4926 void
4927 ill_refrele(ill_t *ill)
4928 {
4929 	mutex_enter(&ill->ill_lock);
4930 	ASSERT(ill->ill_refcnt != 0);
4931 	ill->ill_refcnt--;
4932 	ILL_UNTRACE_REF(ill);
4933 	if (ill->ill_refcnt != 0) {
4934 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4935 		mutex_exit(&ill->ill_lock);
4936 		return;
4937 	}
4938 
4939 	/* Drops the ill_lock */
4940 	ipif_ill_refrele_tail(ill);
4941 }
4942 
4943 /*
4944  * Obtain a weak reference count on the ill. This reference ensures the
4945  * ill won't be freed, but the ill may change any of its critical state
4946  * such as netmask, address etc. Returns an error if the ill has started
4947  * closing.
4948  */
4949 boolean_t
4950 ill_waiter_inc(ill_t *ill)
4951 {
4952 	mutex_enter(&ill->ill_lock);
4953 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4954 		mutex_exit(&ill->ill_lock);
4955 		return (B_FALSE);
4956 	}
4957 	ill->ill_waiters++;
4958 	mutex_exit(&ill->ill_lock);
4959 	return (B_TRUE);
4960 }
4961 
4962 void
4963 ill_waiter_dcr(ill_t *ill)
4964 {
4965 	mutex_enter(&ill->ill_lock);
4966 	ill->ill_waiters--;
4967 	if (ill->ill_waiters == 0)
4968 		cv_broadcast(&ill->ill_cv);
4969 	mutex_exit(&ill->ill_lock);
4970 }
4971 
4972 /*
4973  * Named Dispatch routine to produce a formatted report on all ILLs.
4974  * This report is accessed by using the ndd utility to "get" ND variable
4975  * "ip_ill_status".
4976  */
4977 /* ARGSUSED */
4978 int
4979 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
4980 {
4981 	ill_t		*ill;
4982 	ill_walk_context_t ctx;
4983 
4984 	(void) mi_mpprintf(mp,
4985 	    "ILL      " MI_COL_HDRPAD_STR
4986 	/*   01234567[89ABCDEF] */
4987 	    "rq       " MI_COL_HDRPAD_STR
4988 	/*   01234567[89ABCDEF] */
4989 	    "wq       " MI_COL_HDRPAD_STR
4990 	/*   01234567[89ABCDEF] */
4991 	    "upcnt mxfrg err name");
4992 	/*   12345 12345 123 xxxxxxxx  */
4993 
4994 	rw_enter(&ill_g_lock, RW_READER);
4995 	ill = ILL_START_WALK_ALL(&ctx);
4996 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4997 		(void) mi_mpprintf(mp,
4998 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
4999 		    "%05u %05u %03d %s",
5000 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5001 		    ill->ill_ipif_up_count,
5002 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5003 	}
5004 	rw_exit(&ill_g_lock);
5005 
5006 	return (0);
5007 }
5008 
5009 /*
5010  * Named Dispatch routine to produce a formatted report on all IPIFs.
5011  * This report is accessed by using the ndd utility to "get" ND variable
5012  * "ip_ipif_status".
5013  */
5014 /* ARGSUSED */
5015 int
5016 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5017 {
5018 	char	buf1[INET6_ADDRSTRLEN];
5019 	char	buf2[INET6_ADDRSTRLEN];
5020 	char	buf3[INET6_ADDRSTRLEN];
5021 	char	buf4[INET6_ADDRSTRLEN];
5022 	char	buf5[INET6_ADDRSTRLEN];
5023 	char	buf6[INET6_ADDRSTRLEN];
5024 	char	buf[LIFNAMSIZ];
5025 	ill_t	*ill;
5026 	ipif_t	*ipif;
5027 	nv_t	*nvp;
5028 	uint64_t flags;
5029 	zoneid_t zoneid;
5030 	ill_walk_context_t ctx;
5031 
5032 	(void) mi_mpprintf(mp,
5033 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5034 	    "\tlocal address\n"
5035 	    "\tsrc address\n"
5036 	    "\tsubnet\n"
5037 	    "\tmask\n"
5038 	    "\tbroadcast\n"
5039 	    "\tp-p-dst");
5040 
5041 	ASSERT(q->q_next == NULL);
5042 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5043 
5044 	rw_enter(&ill_g_lock, RW_READER);
5045 	ill = ILL_START_WALK_ALL(&ctx);
5046 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5047 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
5048 			if (zoneid != GLOBAL_ZONEID &&
5049 			    zoneid != ipif->ipif_zoneid)
5050 				continue;
5051 			(void) mi_mpprintf(mp,
5052 			    MI_COL_PTRFMT_STR
5053 			    "%04u %05u %u/%u/%u %s %d",
5054 			    (void *)ipif,
5055 			    ipif->ipif_metric, ipif->ipif_mtu,
5056 			    ipif->ipif_ib_pkt_count,
5057 			    ipif->ipif_ob_pkt_count,
5058 			    ipif->ipif_fo_pkt_count,
5059 			    ipif_get_name(ipif, buf, sizeof (buf)),
5060 			    ipif->ipif_zoneid);
5061 
5062 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5063 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5064 
5065 		/* Tack on text strings for any flags. */
5066 		nvp = ipif_nv_tbl;
5067 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5068 			if (nvp->nv_value & flags)
5069 				(void) mi_mpprintf_nr(mp, " %s",
5070 				    nvp->nv_name);
5071 		}
5072 		(void) mi_mpprintf(mp,
5073 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5074 		    inet_ntop(AF_INET6,
5075 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5076 		    inet_ntop(AF_INET6,
5077 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5078 		    inet_ntop(AF_INET6,
5079 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5080 		    inet_ntop(AF_INET6,
5081 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5082 		    inet_ntop(AF_INET6,
5083 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5084 		    inet_ntop(AF_INET6,
5085 			&ipif->ipif_v6pp_dst_addr,
5086 			buf6, sizeof (buf6)));
5087 		}
5088 	}
5089 	rw_exit(&ill_g_lock);
5090 	return (0);
5091 }
5092 
5093 /*
5094  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5095  * driver.  We construct best guess defaults for lower level information that
5096  * we need.  If an interface is brought up without injection of any overriding
5097  * information from outside, we have to be ready to go with these defaults.
5098  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5099  * we primarely want the dl_provider_style.
5100  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5101  * at which point we assume the other part of the information is valid.
5102  */
5103 void
5104 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5105 {
5106 	uchar_t		*brdcst_addr;
5107 	uint_t		brdcst_addr_length, phys_addr_length;
5108 	t_scalar_t	sap_length;
5109 	dl_info_ack_t	*dlia;
5110 	ip_m_t		*ipm;
5111 	dl_qos_cl_sel1_t *sel1;
5112 
5113 	ASSERT(IAM_WRITER_ILL(ill));
5114 
5115 	/*
5116 	 * Till the ill is fully up ILL_CHANGING will be set and
5117 	 * the ill is not globally visible. So no need for a lock.
5118 	 */
5119 	dlia = (dl_info_ack_t *)mp->b_rptr;
5120 	ill->ill_mactype = dlia->dl_mac_type;
5121 
5122 	ipm = ip_m_lookup(dlia->dl_mac_type);
5123 	if (ipm == NULL) {
5124 		ipm = ip_m_lookup(DL_OTHER);
5125 		ASSERT(ipm != NULL);
5126 	}
5127 	ill->ill_media = ipm;
5128 
5129 	/*
5130 	 * When the new DLPI stuff is ready we'll pull lengths
5131 	 * from dlia.
5132 	 */
5133 	if (dlia->dl_version == DL_VERSION_2) {
5134 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5135 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5136 		    brdcst_addr_length);
5137 		if (brdcst_addr == NULL) {
5138 			brdcst_addr_length = 0;
5139 		}
5140 		sap_length = dlia->dl_sap_length;
5141 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5142 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5143 		    brdcst_addr_length, sap_length, phys_addr_length));
5144 	} else {
5145 		brdcst_addr_length = 6;
5146 		brdcst_addr = ip_six_byte_all_ones;
5147 		sap_length = -2;
5148 		phys_addr_length = brdcst_addr_length;
5149 	}
5150 
5151 	ill->ill_bcast_addr_length = brdcst_addr_length;
5152 	ill->ill_phys_addr_length = phys_addr_length;
5153 	ill->ill_sap_length = sap_length;
5154 	ill->ill_max_frag = dlia->dl_max_sdu;
5155 	ill->ill_max_mtu = ill->ill_max_frag;
5156 
5157 	ill->ill_type = ipm->ip_m_type;
5158 
5159 	if (!ill->ill_dlpi_style_set) {
5160 		if (dlia->dl_provider_style == DL_STYLE2)
5161 			ill->ill_needs_attach = 1;
5162 
5163 		/*
5164 		 * Allocate the first ipif on this ill. We don't delay it
5165 		 * further as ioctl handling assumes atleast one ipif to
5166 		 * be present.
5167 		 *
5168 		 * At this point we don't know whether the ill is v4 or v6.
5169 		 * We will know this whan the SIOCSLIFNAME happens and
5170 		 * the correct value for ill_isv6 will be assigned in
5171 		 * ipif_set_values(). We need to hold the ill lock and
5172 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5173 		 * the wakeup.
5174 		 */
5175 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5176 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5177 		mutex_enter(&ill->ill_lock);
5178 		ASSERT(ill->ill_dlpi_style_set == 0);
5179 		ill->ill_dlpi_style_set = 1;
5180 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5181 		cv_broadcast(&ill->ill_cv);
5182 		mutex_exit(&ill->ill_lock);
5183 		freemsg(mp);
5184 		return;
5185 	}
5186 	ASSERT(ill->ill_ipif != NULL);
5187 	/*
5188 	 * We know whether it is IPv4 or IPv6 now, as this is the
5189 	 * second DL_INFO_ACK we are recieving in response to the
5190 	 * DL_INFO_REQ sent in ipif_set_values.
5191 	 */
5192 	if (ill->ill_isv6)
5193 		ill->ill_sap = IP6_DL_SAP;
5194 	else
5195 		ill->ill_sap = IP_DL_SAP;
5196 	/*
5197 	 * Set ipif_mtu which is used to set the IRE's
5198 	 * ire_max_frag value. The driver could have sent
5199 	 * a different mtu from what it sent last time. No
5200 	 * need to call ipif_mtu_change because IREs have
5201 	 * not yet been created.
5202 	 */
5203 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5204 	/*
5205 	 * Clear all the flags that were set based on ill_bcast_addr_length
5206 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5207 	 * changed now and we need to re-evaluate.
5208 	 */
5209 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5210 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5211 
5212 	/*
5213 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5214 	 * changed now.
5215 	 */
5216 	if (ill->ill_bcast_addr_length == 0) {
5217 		if (ill->ill_resolver_mp != NULL)
5218 			freemsg(ill->ill_resolver_mp);
5219 		if (ill->ill_bcast_mp != NULL)
5220 			freemsg(ill->ill_bcast_mp);
5221 		if (ill->ill_flags & ILLF_XRESOLV)
5222 			ill->ill_net_type = IRE_IF_RESOLVER;
5223 		else
5224 			ill->ill_net_type = IRE_IF_NORESOLVER;
5225 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5226 		    ill->ill_phys_addr_length,
5227 		    ill->ill_sap,
5228 		    ill->ill_sap_length);
5229 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5230 
5231 		if (ill->ill_isv6)
5232 			/*
5233 			 * Note: xresolv interfaces will eventually need NOARP
5234 			 * set here as well, but that will require those
5235 			 * external resolvers to have some knowledge of
5236 			 * that flag and act appropriately. Not to be changed
5237 			 * at present.
5238 			 */
5239 			ill->ill_flags |= ILLF_NONUD;
5240 		else
5241 			ill->ill_flags |= ILLF_NOARP;
5242 
5243 		if (ill->ill_phys_addr_length == 0) {
5244 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5245 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5246 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5247 			} else {
5248 				/* pt-pt supports multicast. */
5249 				ill->ill_flags |= ILLF_MULTICAST;
5250 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5251 			}
5252 		}
5253 	} else {
5254 		ill->ill_net_type = IRE_IF_RESOLVER;
5255 		if (ill->ill_bcast_mp != NULL)
5256 			freemsg(ill->ill_bcast_mp);
5257 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5258 		    ill->ill_bcast_addr_length, ill->ill_sap,
5259 		    ill->ill_sap_length);
5260 		/*
5261 		 * Later detect lack of DLPI driver multicast
5262 		 * capability by catching DL_ENABMULTI errors in
5263 		 * ip_rput_dlpi.
5264 		 */
5265 		ill->ill_flags |= ILLF_MULTICAST;
5266 		if (!ill->ill_isv6)
5267 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5268 	}
5269 	/* By default an interface does not support any CoS marking */
5270 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5271 
5272 	/*
5273 	 * If we get QoS information in DL_INFO_ACK, the device supports
5274 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5275 	 */
5276 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5277 	    dlia->dl_qos_length);
5278 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5279 		ill->ill_flags |= ILLF_COS_ENABLED;
5280 	}
5281 
5282 	/* Clear any previous error indication. */
5283 	ill->ill_error = 0;
5284 	freemsg(mp);
5285 }
5286 
5287 /*
5288  * Perform various checks to verify that an address would make sense as a
5289  * local, remote, or subnet interface address.
5290  */
5291 static boolean_t
5292 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5293 {
5294 	ipaddr_t	net_mask;
5295 
5296 	/*
5297 	 * Don't allow all zeroes, all ones or experimental address, but allow
5298 	 * all ones netmask.
5299 	 */
5300 	if ((net_mask = ip_net_mask(addr)) == 0)
5301 		return (B_FALSE);
5302 	/* A given netmask overrides the "guess" netmask */
5303 	if (subnet_mask != 0)
5304 		net_mask = subnet_mask;
5305 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5306 	    (addr == (addr | ~net_mask)))) {
5307 		return (B_FALSE);
5308 	}
5309 	if (CLASSD(addr))
5310 		return (B_FALSE);
5311 
5312 	return (B_TRUE);
5313 }
5314 
5315 /*
5316  * ipif_lookup_group
5317  * Returns held ipif
5318  */
5319 ipif_t *
5320 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5321 {
5322 	ire_t	*ire;
5323 	ipif_t	*ipif;
5324 
5325 	ire = ire_lookup_multi(group, zoneid);
5326 	if (ire == NULL)
5327 		return (NULL);
5328 	ipif = ire->ire_ipif;
5329 	ipif_refhold(ipif);
5330 	ire_refrele(ire);
5331 	return (ipif);
5332 }
5333 
5334 /*
5335  * Look for an ipif with the specified interface address and destination.
5336  * The destination address is used only for matching point-to-point interfaces.
5337  */
5338 ipif_t *
5339 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5340     ipsq_func_t func, int *error)
5341 {
5342 	ipif_t	*ipif;
5343 	ill_t	*ill;
5344 	ill_walk_context_t ctx;
5345 	ipsq_t	*ipsq;
5346 
5347 	if (error != NULL)
5348 		*error = 0;
5349 
5350 	/*
5351 	 * First match all the point-to-point interfaces
5352 	 * before looking at non-point-to-point interfaces.
5353 	 * This is done to avoid returning non-point-to-point
5354 	 * ipif instead of unnumbered point-to-point ipif.
5355 	 */
5356 	rw_enter(&ill_g_lock, RW_READER);
5357 	ill = ILL_START_WALK_V4(&ctx);
5358 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5359 		GRAB_CONN_LOCK(q);
5360 		mutex_enter(&ill->ill_lock);
5361 		for (ipif = ill->ill_ipif; ipif != NULL;
5362 		    ipif = ipif->ipif_next) {
5363 			/* Allow the ipif to be down */
5364 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5365 			    (ipif->ipif_lcl_addr == if_addr) &&
5366 			    (ipif->ipif_pp_dst_addr == dst)) {
5367 				/*
5368 				 * The block comment at the start of ipif_down
5369 				 * explains the use of the macros used below
5370 				 */
5371 				if (IPIF_CAN_LOOKUP(ipif)) {
5372 					ipif_refhold_locked(ipif);
5373 					mutex_exit(&ill->ill_lock);
5374 					RELEASE_CONN_LOCK(q);
5375 					rw_exit(&ill_g_lock);
5376 					return (ipif);
5377 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5378 					ipsq = ill->ill_phyint->phyint_ipsq;
5379 					mutex_enter(&ipsq->ipsq_lock);
5380 					mutex_exit(&ill->ill_lock);
5381 					rw_exit(&ill_g_lock);
5382 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5383 						ill);
5384 					mutex_exit(&ipsq->ipsq_lock);
5385 					RELEASE_CONN_LOCK(q);
5386 					*error = EINPROGRESS;
5387 					return (NULL);
5388 				}
5389 			}
5390 		}
5391 		mutex_exit(&ill->ill_lock);
5392 		RELEASE_CONN_LOCK(q);
5393 	}
5394 	rw_exit(&ill_g_lock);
5395 
5396 	/* lookup the ipif based on interface address */
5397 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5398 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5399 	return (ipif);
5400 }
5401 
5402 /*
5403  * Look for an ipif with the specified address. For point-point links
5404  * we look for matches on either the destination address and the local
5405  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5406  * is set.
5407  * Matches on a specific ill if match_ill is set.
5408  */
5409 ipif_t *
5410 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5411     mblk_t *mp, ipsq_func_t func, int *error)
5412 {
5413 	ipif_t  *ipif;
5414 	ill_t   *ill;
5415 	boolean_t ptp = B_FALSE;
5416 	ipsq_t	*ipsq;
5417 	ill_walk_context_t	ctx;
5418 
5419 	if (error != NULL)
5420 		*error = 0;
5421 
5422 	rw_enter(&ill_g_lock, RW_READER);
5423 	/*
5424 	 * Repeat twice, first based on local addresses and
5425 	 * next time for pointopoint.
5426 	 */
5427 repeat:
5428 	ill = ILL_START_WALK_V4(&ctx);
5429 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5430 		if (match_ill != NULL && ill != match_ill) {
5431 			continue;
5432 		}
5433 		GRAB_CONN_LOCK(q);
5434 		mutex_enter(&ill->ill_lock);
5435 		for (ipif = ill->ill_ipif; ipif != NULL;
5436 		    ipif = ipif->ipif_next) {
5437 			if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5438 				continue;
5439 			/* Allow the ipif to be down */
5440 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5441 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5442 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5443 			    (ipif->ipif_pp_dst_addr == addr))) {
5444 				/*
5445 				 * The block comment at the start of ipif_down
5446 				 * explains the use of the macros used below
5447 				 */
5448 				if (IPIF_CAN_LOOKUP(ipif)) {
5449 					ipif_refhold_locked(ipif);
5450 					mutex_exit(&ill->ill_lock);
5451 					RELEASE_CONN_LOCK(q);
5452 					rw_exit(&ill_g_lock);
5453 					return (ipif);
5454 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5455 					ipsq = ill->ill_phyint->phyint_ipsq;
5456 					mutex_enter(&ipsq->ipsq_lock);
5457 					mutex_exit(&ill->ill_lock);
5458 					rw_exit(&ill_g_lock);
5459 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5460 						ill);
5461 					mutex_exit(&ipsq->ipsq_lock);
5462 					RELEASE_CONN_LOCK(q);
5463 					*error = EINPROGRESS;
5464 					return (NULL);
5465 				}
5466 			}
5467 		}
5468 		mutex_exit(&ill->ill_lock);
5469 		RELEASE_CONN_LOCK(q);
5470 	}
5471 
5472 	/* Now try the ptp case */
5473 	if (ptp) {
5474 		rw_exit(&ill_g_lock);
5475 		if (error != NULL)
5476 			*error = ENXIO;
5477 		return (NULL);
5478 	}
5479 	ptp = B_TRUE;
5480 	goto repeat;
5481 }
5482 
5483 /*
5484  * Look for an ipif that matches the specified remote address i.e. the
5485  * ipif that would receive the specified packet.
5486  * First look for directly connected interfaces and then do a recursive
5487  * IRE lookup and pick the first ipif corresponding to the source address in the
5488  * ire.
5489  * Returns: held ipif
5490  */
5491 ipif_t *
5492 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5493 {
5494 	ipif_t	*ipif;
5495 	ire_t	*ire;
5496 
5497 	ASSERT(!ill->ill_isv6);
5498 
5499 	/*
5500 	 * Someone could be changing this ipif currently or change it
5501 	 * after we return this. Thus  a few packets could use the old
5502 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5503 	 * will atomically be updated or cleaned up with the new value
5504 	 * Thus we don't need a lock to check the flags or other attrs below.
5505 	 */
5506 	mutex_enter(&ill->ill_lock);
5507 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5508 		if (!IPIF_CAN_LOOKUP(ipif))
5509 			continue;
5510 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5511 			continue;
5512 		/* Allow the ipif to be down */
5513 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5514 			if ((ipif->ipif_pp_dst_addr == addr) ||
5515 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5516 			    ipif->ipif_lcl_addr == addr)) {
5517 				ipif_refhold_locked(ipif);
5518 				mutex_exit(&ill->ill_lock);
5519 				return (ipif);
5520 			}
5521 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5522 			ipif_refhold_locked(ipif);
5523 			mutex_exit(&ill->ill_lock);
5524 			return (ipif);
5525 		}
5526 	}
5527 	mutex_exit(&ill->ill_lock);
5528 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5529 	    MATCH_IRE_RECURSIVE);
5530 	if (ire != NULL) {
5531 		/*
5532 		 * The callers of this function wants to know the
5533 		 * interface on which they have to send the replies
5534 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5535 		 * derived from different ills, we really don't care
5536 		 * what we return here.
5537 		 */
5538 		ipif = ire->ire_ipif;
5539 		if (ipif != NULL) {
5540 			ipif_refhold(ipif);
5541 			ire_refrele(ire);
5542 			return (ipif);
5543 		}
5544 		ire_refrele(ire);
5545 	}
5546 	/* Pick the first interface */
5547 	ipif = ipif_get_next_ipif(NULL, ill);
5548 	return (ipif);
5549 }
5550 
5551 /*
5552  * This func does not prevent refcnt from increasing. But if
5553  * the caller has taken steps to that effect, then this func
5554  * can be used to determine whether the ill has become quiescent
5555  */
5556 boolean_t
5557 ill_is_quiescent(ill_t *ill)
5558 {
5559 	ipif_t	*ipif;
5560 
5561 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5562 
5563 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5564 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5565 			return (B_FALSE);
5566 	}
5567 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5568 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5569 	    ill->ill_mrtun_refcnt != 0)
5570 		return (B_FALSE);
5571 	return (B_TRUE);
5572 }
5573 
5574 /*
5575  * This func does not prevent refcnt from increasing. But if
5576  * the caller has taken steps to that effect, then this func
5577  * can be used to determine whether the ipif has become quiescent
5578  */
5579 static boolean_t
5580 ipif_is_quiescent(ipif_t *ipif)
5581 {
5582 	ill_t *ill;
5583 
5584 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5585 
5586 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5587 		return (B_FALSE);
5588 
5589 	ill = ipif->ipif_ill;
5590 	if (ill->ill_ipif_up_count != 0 || ill->ill_logical_down)
5591 		return (B_TRUE);
5592 
5593 	/* This is the last ipif going down or being deleted on this ill */
5594 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0)
5595 		return (B_FALSE);
5596 
5597 	return (B_TRUE);
5598 }
5599 
5600 /*
5601  * This func does not prevent refcnt from increasing. But if
5602  * the caller has taken steps to that effect, then this func
5603  * can be used to determine whether the ipifs marked with IPIF_MOVING
5604  * have become quiescent and can be moved in a failover/failback.
5605  */
5606 static ipif_t *
5607 ill_quiescent_to_move(ill_t *ill)
5608 {
5609 	ipif_t  *ipif;
5610 
5611 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5612 
5613 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5614 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5615 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5616 				return (ipif);
5617 			}
5618 		}
5619 	}
5620 	return (NULL);
5621 }
5622 
5623 /*
5624  * The ipif/ill/ire has been refreled. Do the tail processing.
5625  * Determine if the ipif or ill in question has become quiescent and if so
5626  * wakeup close and/or restart any queued pending ioctl that is waiting
5627  * for the ipif_down (or ill_down)
5628  */
5629 void
5630 ipif_ill_refrele_tail(ill_t *ill)
5631 {
5632 	mblk_t	*mp;
5633 	conn_t	*connp;
5634 	ipsq_t	*ipsq;
5635 	ipif_t	*ipif;
5636 
5637 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5638 
5639 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5640 	    ill_is_quiescent(ill)) {
5641 		/* ill_close may be waiting */
5642 		cv_broadcast(&ill->ill_cv);
5643 	}
5644 
5645 	/* ipsq can't change because ill_lock  is held */
5646 	ipsq = ill->ill_phyint->phyint_ipsq;
5647 	if (ipsq->ipsq_waitfor == 0) {
5648 		/* Not waiting for anything, just return. */
5649 		mutex_exit(&ill->ill_lock);
5650 		return;
5651 	}
5652 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5653 		ipsq->ipsq_pending_ipif != NULL);
5654 	/*
5655 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5656 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5657 	 * be zero for restarting an ioctl that ends up downing the ill.
5658 	 */
5659 	ipif = ipsq->ipsq_pending_ipif;
5660 	if (ipif->ipif_ill != ill) {
5661 		/* The ioctl is pending on some other ill. */
5662 		mutex_exit(&ill->ill_lock);
5663 		return;
5664 	}
5665 
5666 	switch (ipsq->ipsq_waitfor) {
5667 	case IPIF_DOWN:
5668 	case IPIF_FREE:
5669 		if (!ipif_is_quiescent(ipif)) {
5670 			mutex_exit(&ill->ill_lock);
5671 			return;
5672 		}
5673 		break;
5674 
5675 	case ILL_DOWN:
5676 	case ILL_FREE:
5677 		/*
5678 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5679 		 * waits synchronously in ip_close, and no message is queued in
5680 		 * ipsq_pending_mp at all in this case
5681 		 */
5682 		if (!ill_is_quiescent(ill)) {
5683 			mutex_exit(&ill->ill_lock);
5684 			return;
5685 		}
5686 
5687 		break;
5688 
5689 	case ILL_MOVE_OK:
5690 		if (ill_quiescent_to_move(ill) != NULL) {
5691 			mutex_exit(&ill->ill_lock);
5692 			return;
5693 		}
5694 
5695 		break;
5696 	default:
5697 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5698 		    (void *)ipsq, ipsq->ipsq_waitfor);
5699 	}
5700 
5701 	/*
5702 	 * Incr refcnt for the qwriter_ip call below which
5703 	 * does a refrele
5704 	 */
5705 	ill_refhold_locked(ill);
5706 	mutex_exit(&ill->ill_lock);
5707 
5708 	mp = ipsq_pending_mp_get(ipsq, &connp);
5709 	ASSERT(mp != NULL);
5710 
5711 	switch (mp->b_datap->db_type) {
5712 	case M_ERROR:
5713 	case M_HANGUP:
5714 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5715 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5716 		return;
5717 
5718 	case M_IOCTL:
5719 	case M_IOCDATA:
5720 		(void) qwriter_ip(NULL, ill,
5721 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5722 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5723 		return;
5724 
5725 	default:
5726 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5727 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5728 	}
5729 }
5730 
5731 #ifdef ILL_DEBUG
5732 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5733 void
5734 th_trace_rrecord(th_trace_t *th_trace)
5735 {
5736 	tr_buf_t *tr_buf;
5737 	uint_t lastref;
5738 
5739 	lastref = th_trace->th_trace_lastref;
5740 	lastref++;
5741 	if (lastref == TR_BUF_MAX)
5742 		lastref = 0;
5743 	th_trace->th_trace_lastref = lastref;
5744 	tr_buf = &th_trace->th_trbuf[lastref];
5745 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5746 }
5747 
5748 th_trace_t *
5749 th_trace_ipif_lookup(ipif_t *ipif)
5750 {
5751 	int bucket_id;
5752 	th_trace_t *th_trace;
5753 
5754 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5755 
5756 	bucket_id = IP_TR_HASH(curthread);
5757 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5758 
5759 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5760 	    th_trace = th_trace->th_next) {
5761 		if (th_trace->th_id == curthread)
5762 			return (th_trace);
5763 	}
5764 	return (NULL);
5765 }
5766 
5767 void
5768 ipif_trace_ref(ipif_t *ipif)
5769 {
5770 	int bucket_id;
5771 	th_trace_t *th_trace;
5772 
5773 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5774 
5775 	if (ipif->ipif_trace_disable)
5776 		return;
5777 
5778 	/*
5779 	 * Attempt to locate the trace buffer for the curthread.
5780 	 * If it does not exist, then allocate a new trace buffer
5781 	 * and link it in list of trace bufs for this ipif, at the head
5782 	 */
5783 	th_trace = th_trace_ipif_lookup(ipif);
5784 	if (th_trace == NULL) {
5785 		bucket_id = IP_TR_HASH(curthread);
5786 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5787 		    KM_NOSLEEP);
5788 		if (th_trace == NULL) {
5789 			ipif->ipif_trace_disable = B_TRUE;
5790 			ipif_trace_cleanup(ipif);
5791 			return;
5792 		}
5793 		th_trace->th_id = curthread;
5794 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5795 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5796 		if (th_trace->th_next != NULL)
5797 			th_trace->th_next->th_prev = &th_trace->th_next;
5798 		ipif->ipif_trace[bucket_id] = th_trace;
5799 	}
5800 	ASSERT(th_trace->th_refcnt >= 0 &&
5801 		th_trace->th_refcnt < TR_BUF_MAX -1);
5802 	th_trace->th_refcnt++;
5803 	th_trace_rrecord(th_trace);
5804 }
5805 
5806 void
5807 ipif_untrace_ref(ipif_t *ipif)
5808 {
5809 	th_trace_t *th_trace;
5810 
5811 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5812 
5813 	if (ipif->ipif_trace_disable)
5814 		return;
5815 	th_trace = th_trace_ipif_lookup(ipif);
5816 	ASSERT(th_trace != NULL);
5817 	ASSERT(th_trace->th_refcnt > 0);
5818 
5819 	th_trace->th_refcnt--;
5820 	th_trace_rrecord(th_trace);
5821 }
5822 
5823 th_trace_t *
5824 th_trace_ill_lookup(ill_t *ill)
5825 {
5826 	th_trace_t *th_trace;
5827 	int bucket_id;
5828 
5829 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5830 
5831 	bucket_id = IP_TR_HASH(curthread);
5832 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5833 
5834 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
5835 	    th_trace = th_trace->th_next) {
5836 		if (th_trace->th_id == curthread)
5837 			return (th_trace);
5838 	}
5839 	return (NULL);
5840 }
5841 
5842 void
5843 ill_trace_ref(ill_t *ill)
5844 {
5845 	int bucket_id;
5846 	th_trace_t *th_trace;
5847 
5848 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5849 	if (ill->ill_trace_disable)
5850 		return;
5851 	/*
5852 	 * Attempt to locate the trace buffer for the curthread.
5853 	 * If it does not exist, then allocate a new trace buffer
5854 	 * and link it in list of trace bufs for this ill, at the head
5855 	 */
5856 	th_trace = th_trace_ill_lookup(ill);
5857 	if (th_trace == NULL) {
5858 		bucket_id = IP_TR_HASH(curthread);
5859 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5860 		    KM_NOSLEEP);
5861 		if (th_trace == NULL) {
5862 			ill->ill_trace_disable = B_TRUE;
5863 			ill_trace_cleanup(ill);
5864 			return;
5865 		}
5866 		th_trace->th_id = curthread;
5867 		th_trace->th_next = ill->ill_trace[bucket_id];
5868 		th_trace->th_prev = &ill->ill_trace[bucket_id];
5869 		if (th_trace->th_next != NULL)
5870 			th_trace->th_next->th_prev = &th_trace->th_next;
5871 		ill->ill_trace[bucket_id] = th_trace;
5872 	}
5873 	ASSERT(th_trace->th_refcnt >= 0 &&
5874 		th_trace->th_refcnt < TR_BUF_MAX - 1);
5875 
5876 	th_trace->th_refcnt++;
5877 	th_trace_rrecord(th_trace);
5878 }
5879 
5880 void
5881 ill_untrace_ref(ill_t *ill)
5882 {
5883 	th_trace_t *th_trace;
5884 
5885 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5886 
5887 	if (ill->ill_trace_disable)
5888 		return;
5889 	th_trace = th_trace_ill_lookup(ill);
5890 	ASSERT(th_trace != NULL);
5891 	ASSERT(th_trace->th_refcnt > 0);
5892 
5893 	th_trace->th_refcnt--;
5894 	th_trace_rrecord(th_trace);
5895 }
5896 
5897 /*
5898  * Verify that this thread has no refs to the ipif and free
5899  * the trace buffers
5900  */
5901 /* ARGSUSED */
5902 void
5903 ipif_thread_exit(ipif_t *ipif, void *dummy)
5904 {
5905 	th_trace_t *th_trace;
5906 
5907 	mutex_enter(&ipif->ipif_ill->ill_lock);
5908 
5909 	th_trace = th_trace_ipif_lookup(ipif);
5910 	if (th_trace == NULL) {
5911 		mutex_exit(&ipif->ipif_ill->ill_lock);
5912 		return;
5913 	}
5914 	ASSERT(th_trace->th_refcnt == 0);
5915 	/* unlink th_trace and free it */
5916 	*th_trace->th_prev = th_trace->th_next;
5917 	if (th_trace->th_next != NULL)
5918 		th_trace->th_next->th_prev = th_trace->th_prev;
5919 	th_trace->th_next = NULL;
5920 	th_trace->th_prev = NULL;
5921 	kmem_free(th_trace, sizeof (th_trace_t));
5922 
5923 	mutex_exit(&ipif->ipif_ill->ill_lock);
5924 }
5925 
5926 /*
5927  * Verify that this thread has no refs to the ill and free
5928  * the trace buffers
5929  */
5930 /* ARGSUSED */
5931 void
5932 ill_thread_exit(ill_t *ill, void *dummy)
5933 {
5934 	th_trace_t *th_trace;
5935 
5936 	mutex_enter(&ill->ill_lock);
5937 
5938 	th_trace = th_trace_ill_lookup(ill);
5939 	if (th_trace == NULL) {
5940 		mutex_exit(&ill->ill_lock);
5941 		return;
5942 	}
5943 	ASSERT(th_trace->th_refcnt == 0);
5944 	/* unlink th_trace and free it */
5945 	*th_trace->th_prev = th_trace->th_next;
5946 	if (th_trace->th_next != NULL)
5947 		th_trace->th_next->th_prev = th_trace->th_prev;
5948 	th_trace->th_next = NULL;
5949 	th_trace->th_prev = NULL;
5950 	kmem_free(th_trace, sizeof (th_trace_t));
5951 
5952 	mutex_exit(&ill->ill_lock);
5953 }
5954 #endif
5955 
5956 #ifdef ILL_DEBUG
5957 void
5958 ip_thread_exit(void)
5959 {
5960 	ill_t	*ill;
5961 	ipif_t	*ipif;
5962 	ill_walk_context_t	ctx;
5963 
5964 	rw_enter(&ill_g_lock, RW_READER);
5965 	ill = ILL_START_WALK_ALL(&ctx);
5966 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5967 		for (ipif = ill->ill_ipif; ipif != NULL;
5968 		    ipif = ipif->ipif_next) {
5969 			ipif_thread_exit(ipif, NULL);
5970 		}
5971 		ill_thread_exit(ill, NULL);
5972 	}
5973 	rw_exit(&ill_g_lock);
5974 
5975 	ire_walk(ire_thread_exit, NULL);
5976 	ndp_walk_impl(NULL, nce_thread_exit, NULL, B_FALSE);
5977 }
5978 
5979 /*
5980  * Called when ipif is unplumbed or when memory alloc fails
5981  */
5982 void
5983 ipif_trace_cleanup(ipif_t *ipif)
5984 {
5985 	int	i;
5986 	th_trace_t	*th_trace;
5987 	th_trace_t	*th_trace_next;
5988 
5989 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
5990 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
5991 		    th_trace = th_trace_next) {
5992 			th_trace_next = th_trace->th_next;
5993 			kmem_free(th_trace, sizeof (th_trace_t));
5994 		}
5995 		ipif->ipif_trace[i] = NULL;
5996 	}
5997 }
5998 
5999 /*
6000  * Called when ill is unplumbed or when memory alloc fails
6001  */
6002 void
6003 ill_trace_cleanup(ill_t *ill)
6004 {
6005 	int	i;
6006 	th_trace_t	*th_trace;
6007 	th_trace_t	*th_trace_next;
6008 
6009 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6010 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6011 		    th_trace = th_trace_next) {
6012 			th_trace_next = th_trace->th_next;
6013 			kmem_free(th_trace, sizeof (th_trace_t));
6014 		}
6015 		ill->ill_trace[i] = NULL;
6016 	}
6017 }
6018 
6019 #else
6020 void ip_thread_exit(void) {}
6021 #endif
6022 
6023 void
6024 ipif_refhold_locked(ipif_t *ipif)
6025 {
6026 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6027 	ipif->ipif_refcnt++;
6028 	IPIF_TRACE_REF(ipif);
6029 }
6030 
6031 void
6032 ipif_refhold(ipif_t *ipif)
6033 {
6034 	ill_t	*ill;
6035 
6036 	ill = ipif->ipif_ill;
6037 	mutex_enter(&ill->ill_lock);
6038 	ipif->ipif_refcnt++;
6039 	IPIF_TRACE_REF(ipif);
6040 	mutex_exit(&ill->ill_lock);
6041 }
6042 
6043 /*
6044  * Must not be called while holding any locks. Otherwise if this is
6045  * the last reference to be released there is a chance of recursive mutex
6046  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6047  * to restart an ioctl.
6048  */
6049 void
6050 ipif_refrele(ipif_t *ipif)
6051 {
6052 	ill_t	*ill;
6053 
6054 	ill = ipif->ipif_ill;
6055 
6056 	mutex_enter(&ill->ill_lock);
6057 	ASSERT(ipif->ipif_refcnt != 0);
6058 	ipif->ipif_refcnt--;
6059 	IPIF_UNTRACE_REF(ipif);
6060 	if (ipif->ipif_refcnt != 0) {
6061 		mutex_exit(&ill->ill_lock);
6062 		return;
6063 	}
6064 
6065 	/* Drops the ill_lock */
6066 	ipif_ill_refrele_tail(ill);
6067 }
6068 
6069 ipif_t *
6070 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6071 {
6072 	ipif_t	*ipif;
6073 
6074 	mutex_enter(&ill->ill_lock);
6075 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6076 	    ipif != NULL; ipif = ipif->ipif_next) {
6077 		if (!IPIF_CAN_LOOKUP(ipif))
6078 			continue;
6079 		ipif_refhold_locked(ipif);
6080 		mutex_exit(&ill->ill_lock);
6081 		return (ipif);
6082 	}
6083 	mutex_exit(&ill->ill_lock);
6084 	return (NULL);
6085 }
6086 
6087 /*
6088  * TODO: make this table extendible at run time
6089  * Return a pointer to the mac type info for 'mac_type'
6090  */
6091 static ip_m_t *
6092 ip_m_lookup(t_uscalar_t mac_type)
6093 {
6094 	ip_m_t	*ipm;
6095 
6096 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6097 		if (ipm->ip_m_mac_type == mac_type)
6098 			return (ipm);
6099 	return (NULL);
6100 }
6101 
6102 /*
6103  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6104  * ipif_arg is passed in to associate it with the correct interface.
6105  * We may need to restart this operation if the ipif cannot be looked up
6106  * due to an exclusive operation that is currently in progress. The restart
6107  * entry point is specified by 'func'
6108  */
6109 int
6110 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6111     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6112     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6113     ipsq_func_t func)
6114 {
6115 	ire_t	*ire;
6116 	ire_t	*gw_ire = NULL;
6117 	ipif_t	*ipif = NULL;
6118 	boolean_t ipif_refheld = B_FALSE;
6119 	uint_t	type;
6120 	int	match_flags = MATCH_IRE_TYPE;
6121 	int	error;
6122 
6123 	ip1dbg(("ip_rt_add:"));
6124 
6125 	if (ire_arg != NULL)
6126 		*ire_arg = NULL;
6127 
6128 	/*
6129 	 * If this is the case of RTF_HOST being set, then we set the netmask
6130 	 * to all ones (regardless if one was supplied).
6131 	 */
6132 	if (flags & RTF_HOST)
6133 		mask = IP_HOST_MASK;
6134 
6135 	/*
6136 	 * Prevent routes with a zero gateway from being created (since
6137 	 * interfaces can currently be plumbed and brought up no assigned
6138 	 * address).
6139 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6140 	 */
6141 	if (gw_addr == 0 && src_ipif == NULL)
6142 		return (ENETUNREACH);
6143 	/*
6144 	 * Get the ipif, if any, corresponding to the gw_addr
6145 	 */
6146 	if (gw_addr != 0) {
6147 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6148 		    &error);
6149 		if (ipif != NULL) {
6150 			if (IS_VNI(ipif->ipif_ill)) {
6151 				ipif_refrele(ipif);
6152 				return (EINVAL);
6153 			}
6154 			ipif_refheld = B_TRUE;
6155 		} else if (error == EINPROGRESS) {
6156 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6157 			return (EINPROGRESS);
6158 		} else {
6159 			error = 0;
6160 		}
6161 	}
6162 
6163 	if (ipif != NULL) {
6164 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6165 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6166 	} else {
6167 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6168 	}
6169 
6170 	/*
6171 	 * GateD will attempt to create routes with a loopback interface
6172 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6173 	 * these routes to be added, but create them as interface routes
6174 	 * since the gateway is an interface address.
6175 	 */
6176 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK))
6177 		flags &= ~RTF_GATEWAY;
6178 
6179 	/*
6180 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6181 	 * and the gateway address provided is one of the system's interface
6182 	 * addresses.  By using the routing socket interface and supplying an
6183 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6184 	 * specifying an interface route to be created is available which uses
6185 	 * the interface index that specifies the outgoing interface rather than
6186 	 * the address of an outgoing interface (which may not be able to
6187 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6188 	 * flag, routes can be specified which not only specify the next-hop to
6189 	 * be used when routing to a certain prefix, but also which outgoing
6190 	 * interface should be used.
6191 	 *
6192 	 * Previously, interfaces would have unique addresses assigned to them
6193 	 * and so the address assigned to a particular interface could be used
6194 	 * to identify a particular interface.  One exception to this was the
6195 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6196 	 *
6197 	 * With the advent of IPv6 and its link-local addresses, this
6198 	 * restriction was relaxed and interfaces could share addresses between
6199 	 * themselves.  In fact, typically all of the link-local interfaces on
6200 	 * an IPv6 node or router will have the same link-local address.  In
6201 	 * order to differentiate between these interfaces, the use of an
6202 	 * interface index is necessary and this index can be carried inside a
6203 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6204 	 * of using the interface index, however, is that all of the ipif's that
6205 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6206 	 * cannot be used to differentiate between ipif's (or logical
6207 	 * interfaces) that belong to the same ill (physical interface).
6208 	 *
6209 	 * For example, in the following case involving IPv4 interfaces and
6210 	 * logical interfaces
6211 	 *
6212 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6213 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6214 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6215 	 *
6216 	 * the ipif's corresponding to each of these interface routes can be
6217 	 * uniquely identified by the "gateway" (actually interface address).
6218 	 *
6219 	 * In this case involving multiple IPv6 default routes to a particular
6220 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6221 	 * default route is of interest:
6222 	 *
6223 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6224 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6225 	 */
6226 
6227 	/* RTF_GATEWAY not set */
6228 	if (!(flags & RTF_GATEWAY)) {
6229 		queue_t	*stq;
6230 		queue_t	*rfq = NULL;
6231 		ill_t	*in_ill = NULL;
6232 
6233 		/*
6234 		 * As the interface index specified with the RTA_IFP sockaddr is
6235 		 * the same for all ipif's off of an ill, the matching logic
6236 		 * below uses MATCH_IRE_ILL if such an index was specified.
6237 		 * This means that routes sharing the same prefix when added
6238 		 * using a RTA_IFP sockaddr must have distinct interface
6239 		 * indices (namely, they must be on distinct ill's).
6240 		 *
6241 		 * On the other hand, since the gateway address will usually be
6242 		 * different for each ipif on the system, the matching logic
6243 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6244 		 * route.  This means that interface routes for the same prefix
6245 		 * can be created if they belong to distinct ipif's and if a
6246 		 * RTA_IFP sockaddr is not present.
6247 		 */
6248 		if (ipif_arg != NULL) {
6249 			if (ipif_refheld)  {
6250 				ipif_refrele(ipif);
6251 				ipif_refheld = B_FALSE;
6252 			}
6253 			ipif = ipif_arg;
6254 			match_flags |= MATCH_IRE_ILL;
6255 		} else {
6256 			/*
6257 			 * Check the ipif corresponding to the gw_addr
6258 			 */
6259 			if (ipif == NULL)
6260 				return (ENETUNREACH);
6261 			match_flags |= MATCH_IRE_IPIF;
6262 		}
6263 		ASSERT(ipif != NULL);
6264 		/*
6265 		 * If src_ipif is not NULL, we have to create
6266 		 * an ire with non-null ire_in_ill value
6267 		 */
6268 		if (src_ipif != NULL) {
6269 			in_ill = src_ipif->ipif_ill;
6270 		}
6271 
6272 		/*
6273 		 * We check for an existing entry at this point.
6274 		 *
6275 		 * Since a netmask isn't passed in via the ioctl interface
6276 		 * (SIOCADDRT), we don't check for a matching netmask in that
6277 		 * case.
6278 		 */
6279 		if (!ioctl_msg)
6280 			match_flags |= MATCH_IRE_MASK;
6281 		if (src_ipif != NULL) {
6282 			/* Look up in the special table */
6283 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6284 			    ipif, src_ipif->ipif_ill, match_flags);
6285 		} else {
6286 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6287 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6288 			    match_flags);
6289 		}
6290 		if (ire != NULL) {
6291 			ire_refrele(ire);
6292 			if (ipif_refheld)
6293 				ipif_refrele(ipif);
6294 			return (EEXIST);
6295 		}
6296 
6297 		if (src_ipif != NULL) {
6298 			/*
6299 			 * Create the special ire for the IRE table
6300 			 * which hangs out of ire_in_ill. This ire
6301 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6302 			 * Thus rfq is non-NULL.
6303 			 */
6304 			rfq = ipif->ipif_rq;
6305 		}
6306 		/* Create the usual interface ires */
6307 
6308 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6309 		    ? ipif->ipif_rq : ipif->ipif_wq;
6310 
6311 		/*
6312 		 * Create a copy of the IRE_LOOPBACK,
6313 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6314 		 * the modified address and netmask.
6315 		 */
6316 		ire = ire_create(
6317 		    (uchar_t *)&dst_addr,
6318 		    (uint8_t *)&mask,
6319 		    (uint8_t *)&ipif->ipif_src_addr,
6320 		    NULL,
6321 		    NULL,
6322 		    &ipif->ipif_mtu,
6323 		    NULL,
6324 		    rfq,
6325 		    stq,
6326 		    ipif->ipif_net_type,
6327 		    ipif->ipif_resolver_mp,
6328 		    ipif,
6329 		    in_ill,
6330 		    0,
6331 		    0,
6332 		    0,
6333 		    flags,
6334 		    &ire_uinfo_null);
6335 		if (ire == NULL) {
6336 			if (ipif_refheld)
6337 				ipif_refrele(ipif);
6338 			return (ENOMEM);
6339 		}
6340 
6341 		/*
6342 		 * Some software (for example, GateD and Sun Cluster) attempts
6343 		 * to create (what amount to) IRE_PREFIX routes with the
6344 		 * loopback address as the gateway.  This is primarily done to
6345 		 * set up prefixes with the RTF_REJECT flag set (for example,
6346 		 * when generating aggregate routes.)
6347 		 *
6348 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6349 		 * IRE_LOOPBACK, then we map the request into a
6350 		 * IRE_IF_NORESOLVER.
6351 		 *
6352 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6353 		 * routine, but rather using ire_create() directly.
6354 		 */
6355 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6356 			ire->ire_type = IRE_IF_NORESOLVER;
6357 		error = ire_add(&ire, q, mp, func);
6358 		if (error == 0)
6359 			goto save_ire;
6360 
6361 		/*
6362 		 * In the result of failure, ire_add() will have already
6363 		 * deleted the ire in question, so there is no need to
6364 		 * do that here.
6365 		 */
6366 		if (ipif_refheld)
6367 			ipif_refrele(ipif);
6368 		return (error);
6369 	}
6370 	if (ipif_refheld) {
6371 		ipif_refrele(ipif);
6372 		ipif_refheld = B_FALSE;
6373 	}
6374 
6375 	if (src_ipif != NULL) {
6376 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6377 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6378 		return (EINVAL);
6379 	}
6380 	/*
6381 	 * Get an interface IRE for the specified gateway.
6382 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6383 	 * gateway, it is currently unreachable and we fail the request
6384 	 * accordingly.
6385 	 */
6386 	ipif = ipif_arg;
6387 	if (ipif_arg != NULL)
6388 		match_flags |= MATCH_IRE_ILL;
6389 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6390 	    ALL_ZONES, 0, match_flags);
6391 	if (gw_ire == NULL)
6392 		return (ENETUNREACH);
6393 
6394 	/*
6395 	 * We create one of three types of IREs as a result of this request
6396 	 * based on the netmask.  A netmask of all ones (which is automatically
6397 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6398 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6399 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6400 	 * destination prefix.
6401 	 */
6402 	if (mask == IP_HOST_MASK)
6403 		type = IRE_HOST;
6404 	else if (mask == 0)
6405 		type = IRE_DEFAULT;
6406 	else
6407 		type = IRE_PREFIX;
6408 
6409 	/* check for a duplicate entry */
6410 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6411 	    NULL, ALL_ZONES, 0, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6412 	if (ire != NULL) {
6413 		ire_refrele(gw_ire);
6414 		ire_refrele(ire);
6415 		return (EEXIST);
6416 	}
6417 
6418 	/* Create the IRE. */
6419 	ire = ire_create(
6420 	    (uchar_t *)&dst_addr,		/* dest address */
6421 	    (uchar_t *)&mask,			/* mask */
6422 	    /* src address assigned by the caller? */
6423 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6424 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6425 	    (uchar_t *)&gw_addr,		/* gateway address */
6426 	    NULL,				/* no in-srcaddress */
6427 	    &gw_ire->ire_max_frag,
6428 	    NULL,				/* no Fast Path header */
6429 	    NULL,				/* no recv-from queue */
6430 	    NULL,				/* no send-to queue */
6431 	    (ushort_t)type,			/* IRE type */
6432 	    NULL,
6433 	    ipif_arg,
6434 	    NULL,
6435 	    0,
6436 	    0,
6437 	    0,
6438 	    flags,
6439 	    &gw_ire->ire_uinfo);		/* Inherit ULP info from gw */
6440 	if (ire == NULL) {
6441 		ire_refrele(gw_ire);
6442 		return (ENOMEM);
6443 	}
6444 
6445 	/*
6446 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6447 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6448 	 */
6449 
6450 	/* Add the new IRE. */
6451 	error = ire_add(&ire, q, mp, func);
6452 	if (error != 0) {
6453 		/*
6454 		 * In the result of failure, ire_add() will have already
6455 		 * deleted the ire in question, so there is no need to
6456 		 * do that here.
6457 		 */
6458 		ire_refrele(gw_ire);
6459 		return (error);
6460 	}
6461 
6462 	if (flags & RTF_MULTIRT) {
6463 		/*
6464 		 * Invoke the CGTP (multirouting) filtering module
6465 		 * to add the dst address in the filtering database.
6466 		 * Replicated inbound packets coming from that address
6467 		 * will be filtered to discard the duplicates.
6468 		 * It is not necessary to call the CGTP filter hook
6469 		 * when the dst address is a broadcast or multicast,
6470 		 * because an IP source address cannot be a broadcast
6471 		 * or a multicast.
6472 		 */
6473 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6474 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
6475 		if (ire_dst != NULL) {
6476 			ip_cgtp_bcast_add(ire, ire_dst);
6477 			ire_refrele(ire_dst);
6478 			goto save_ire;
6479 		}
6480 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6481 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6482 			    ire->ire_addr,
6483 			    ire->ire_gateway_addr,
6484 			    ire->ire_src_addr,
6485 			    gw_ire->ire_src_addr);
6486 			if (res != 0) {
6487 				ire_refrele(gw_ire);
6488 				ire_delete(ire);
6489 				return (res);
6490 			}
6491 		}
6492 	}
6493 
6494 save_ire:
6495 	if (gw_ire != NULL) {
6496 		ire_refrele(gw_ire);
6497 	}
6498 	/*
6499 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6500 	 * flag. This route is only added and deleted by mipagent.
6501 	 * So, for simplicity of design, we refrain from saving
6502 	 * ires that are created with srcif value. This may change
6503 	 * in future if we find more usage of srcifp feature.
6504 	 */
6505 	if (ipif != NULL && src_ipif == NULL) {
6506 		/*
6507 		 * Save enough information so that we can recreate the IRE if
6508 		 * the interface goes down and then up.  The metrics associated
6509 		 * with the route will be saved as well when rts_setmetrics() is
6510 		 * called after the IRE has been created.  In the case where
6511 		 * memory cannot be allocated, none of this information will be
6512 		 * saved.
6513 		 */
6514 		ipif_save_ire(ipif, ire);
6515 	}
6516 	if (ioctl_msg)
6517 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6518 	if (ire_arg != NULL) {
6519 		/*
6520 		 * Store the ire that was successfully added into where ire_arg
6521 		 * points to so that callers don't have to look it up
6522 		 * themselves (but they are responsible for ire_refrele()ing
6523 		 * the ire when they are finished with it).
6524 		 */
6525 		*ire_arg = ire;
6526 	} else {
6527 		ire_refrele(ire);		/* Held in ire_add */
6528 	}
6529 	if (ipif_refheld)
6530 		ipif_refrele(ipif);
6531 	return (0);
6532 }
6533 
6534 /*
6535  * ip_rt_delete is called to delete an IPv4 route.
6536  * ipif_arg is passed in to associate it with the correct interface.
6537  * src_ipif is passed to associate the incoming interface of the packet.
6538  * We may need to restart this operation if the ipif cannot be looked up
6539  * due to an exclusive operation that is currently in progress. The restart
6540  * entry point is specified by 'func'
6541  */
6542 /* ARGSUSED4 */
6543 int
6544 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6545     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6546     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6547 {
6548 	ire_t	*ire = NULL;
6549 	ipif_t	*ipif;
6550 	boolean_t ipif_refheld = B_FALSE;
6551 	uint_t	type;
6552 	uint_t	match_flags = MATCH_IRE_TYPE;
6553 	int	err = 0;
6554 
6555 	ip1dbg(("ip_rt_delete:"));
6556 	/*
6557 	 * If this is the case of RTF_HOST being set, then we set the netmask
6558 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6559 	 */
6560 	if (flags & RTF_HOST) {
6561 		mask = IP_HOST_MASK;
6562 		match_flags |= MATCH_IRE_MASK;
6563 	} else if (rtm_addrs & RTA_NETMASK) {
6564 		match_flags |= MATCH_IRE_MASK;
6565 	}
6566 
6567 	/*
6568 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6569 	 * we check if the gateway address is one of our interfaces first,
6570 	 * and fall back on RTF_GATEWAY routes.
6571 	 *
6572 	 * This makes it possible to delete an original
6573 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6574 	 *
6575 	 * As the interface index specified with the RTA_IFP sockaddr is the
6576 	 * same for all ipif's off of an ill, the matching logic below uses
6577 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6578 	 * sharing the same prefix and interface index as the the route
6579 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6580 	 * is specified in the request.
6581 	 *
6582 	 * On the other hand, since the gateway address will usually be
6583 	 * different for each ipif on the system, the matching logic
6584 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6585 	 * route.  This means that interface routes for the same prefix can be
6586 	 * uniquely identified if they belong to distinct ipif's and if a
6587 	 * RTA_IFP sockaddr is not present.
6588 	 *
6589 	 * For more detail on specifying routes by gateway address and by
6590 	 * interface index, see the comments in ip_rt_add().
6591 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6592 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6593 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6594 	 * succeed.
6595 	 */
6596 	if (src_ipif != NULL) {
6597 		if (ipif_arg == NULL && gw_addr != 0) {
6598 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6599 			    q, mp, func, &err);
6600 			if (ipif_arg != NULL)
6601 				ipif_refheld = B_TRUE;
6602 		}
6603 		if (ipif_arg == NULL) {
6604 			err = (err == EINPROGRESS) ? err : ESRCH;
6605 			return (err);
6606 		}
6607 		ipif = ipif_arg;
6608 	} else {
6609 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6610 			    q, mp, func, &err);
6611 		if (ipif != NULL)
6612 			ipif_refheld = B_TRUE;
6613 		else if (err == EINPROGRESS)
6614 			return (err);
6615 		else
6616 			err = 0;
6617 	}
6618 	if (ipif != NULL) {
6619 		if (ipif_arg != NULL) {
6620 			if (ipif_refheld) {
6621 				ipif_refrele(ipif);
6622 				ipif_refheld = B_FALSE;
6623 			}
6624 			ipif = ipif_arg;
6625 			match_flags |= MATCH_IRE_ILL;
6626 		} else {
6627 			match_flags |= MATCH_IRE_IPIF;
6628 		}
6629 		if (src_ipif != NULL) {
6630 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6631 			    ipif, src_ipif->ipif_ill, match_flags);
6632 		} else {
6633 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6634 				ire = ire_ctable_lookup(dst_addr, 0,
6635 				    IRE_LOOPBACK, ipif, ALL_ZONES, match_flags);
6636 			}
6637 			if (ire == NULL) {
6638 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6639 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6640 				    match_flags);
6641 			}
6642 		}
6643 	}
6644 
6645 	if (ire == NULL) {
6646 		/*
6647 		 * At this point, the gateway address is not one of our own
6648 		 * addresses or a matching interface route was not found.  We
6649 		 * set the IRE type to lookup based on whether
6650 		 * this is a host route, a default route or just a prefix.
6651 		 *
6652 		 * If an ipif_arg was passed in, then the lookup is based on an
6653 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6654 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6655 		 * set as the route being looked up is not a traditional
6656 		 * interface route.
6657 		 * Since we do not add gateway route with srcipif, we don't
6658 		 * expect to find it either.
6659 		 */
6660 		if (src_ipif != NULL) {
6661 			if (ipif_refheld)
6662 				ipif_refrele(ipif);
6663 			return (ESRCH);
6664 		} else {
6665 			match_flags &= ~MATCH_IRE_IPIF;
6666 			match_flags |= MATCH_IRE_GW;
6667 			if (ipif_arg != NULL)
6668 				match_flags |= MATCH_IRE_ILL;
6669 			if (mask == IP_HOST_MASK)
6670 				type = IRE_HOST;
6671 			else if (mask == 0)
6672 				type = IRE_DEFAULT;
6673 			else
6674 				type = IRE_PREFIX;
6675 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6676 			    ipif_arg, NULL, ALL_ZONES, 0, match_flags);
6677 			if (ire == NULL && type == IRE_HOST) {
6678 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6679 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6680 				    ALL_ZONES, 0, match_flags);
6681 			}
6682 		}
6683 	}
6684 
6685 	if (ipif_refheld)
6686 		ipif_refrele(ipif);
6687 
6688 	/* ipif is not refheld anymore */
6689 	if (ire == NULL)
6690 		return (ESRCH);
6691 
6692 	if (ire->ire_flags & RTF_MULTIRT) {
6693 		/*
6694 		 * Invoke the CGTP (multirouting) filtering module
6695 		 * to remove the dst address from the filtering database.
6696 		 * Packets coming from that address will no longer be
6697 		 * filtered to remove duplicates.
6698 		 */
6699 		if (ip_cgtp_filter_ops != NULL) {
6700 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6701 			    ire->ire_gateway_addr);
6702 		}
6703 		ip_cgtp_bcast_delete(ire);
6704 	}
6705 
6706 	ipif = ire->ire_ipif;
6707 	/*
6708 	 * Removing from ipif_saved_ire_mp is not necessary
6709 	 * when src_ipif being non-NULL. ip_rt_add does not
6710 	 * save the ires which src_ipif being non-NULL.
6711 	 */
6712 	if (ipif != NULL && src_ipif == NULL) {
6713 		ipif_remove_ire(ipif, ire);
6714 	}
6715 	if (ioctl_msg)
6716 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6717 	ire_delete(ire);
6718 	ire_refrele(ire);
6719 	return (err);
6720 }
6721 
6722 /*
6723  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6724  */
6725 /* ARGSUSED */
6726 int
6727 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6728     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6729 {
6730 	ipaddr_t dst_addr;
6731 	ipaddr_t gw_addr;
6732 	ipaddr_t mask;
6733 	int error = 0;
6734 	mblk_t *mp1;
6735 	struct rtentry *rt;
6736 	ipif_t *ipif = NULL;
6737 
6738 	ip1dbg(("ip_siocaddrt:"));
6739 	/* Existence of mp1 verified in ip_wput_nondata */
6740 	mp1 = mp->b_cont->b_cont;
6741 	rt = (struct rtentry *)mp1->b_rptr;
6742 
6743 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6744 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6745 
6746 	/*
6747 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6748 	 * to a particular host address.  In this case, we set the netmask to
6749 	 * all ones for the particular destination address.  Otherwise,
6750 	 * determine the netmask to be used based on dst_addr and the interfaces
6751 	 * in use.
6752 	 */
6753 	if (rt->rt_flags & RTF_HOST) {
6754 		mask = IP_HOST_MASK;
6755 	} else {
6756 		/*
6757 		 * Note that ip_subnet_mask returns a zero mask in the case of
6758 		 * default (an all-zeroes address).
6759 		 */
6760 		mask = ip_subnet_mask(dst_addr, &ipif);
6761 	}
6762 
6763 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags,
6764 	    NULL, NULL, NULL, B_TRUE, q, mp, ip_process_ioctl);
6765 	if (ipif != NULL)
6766 		ipif_refrele(ipif);
6767 	return (error);
6768 }
6769 
6770 /*
6771  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6772  */
6773 /* ARGSUSED */
6774 int
6775 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6776     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6777 {
6778 	ipaddr_t dst_addr;
6779 	ipaddr_t gw_addr;
6780 	ipaddr_t mask;
6781 	int error;
6782 	mblk_t *mp1;
6783 	struct rtentry *rt;
6784 	ipif_t *ipif = NULL;
6785 
6786 	ip1dbg(("ip_siocdelrt:"));
6787 	/* Existence of mp1 verified in ip_wput_nondata */
6788 	mp1 = mp->b_cont->b_cont;
6789 	rt = (struct rtentry *)mp1->b_rptr;
6790 
6791 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6792 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6793 
6794 	/*
6795 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6796 	 * to a particular host address.  In this case, we set the netmask to
6797 	 * all ones for the particular destination address.  Otherwise,
6798 	 * determine the netmask to be used based on dst_addr and the interfaces
6799 	 * in use.
6800 	 */
6801 	if (rt->rt_flags & RTF_HOST) {
6802 		mask = IP_HOST_MASK;
6803 	} else {
6804 		/*
6805 		 * Note that ip_subnet_mask returns a zero mask in the case of
6806 		 * default (an all-zeroes address).
6807 		 */
6808 		mask = ip_subnet_mask(dst_addr, &ipif);
6809 	}
6810 
6811 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6812 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6813 	    B_TRUE, q, mp, ip_process_ioctl);
6814 	if (ipif != NULL)
6815 		ipif_refrele(ipif);
6816 	return (error);
6817 }
6818 
6819 /*
6820  * Enqueue the mp onto the ipsq, chained by b_next.
6821  * b_prev stores the function to be executed later, and b_queue the queue
6822  * where this mp originated.
6823  */
6824 void
6825 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6826     ill_t *pending_ill)
6827 {
6828 	conn_t	*connp = NULL;
6829 
6830 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6831 	ASSERT(func != NULL);
6832 
6833 	mp->b_queue = q;
6834 	mp->b_prev = (void *)func;
6835 	mp->b_next = NULL;
6836 
6837 	switch (type) {
6838 	case CUR_OP:
6839 		if (ipsq->ipsq_mptail != NULL) {
6840 			ASSERT(ipsq->ipsq_mphead != NULL);
6841 			ipsq->ipsq_mptail->b_next = mp;
6842 		} else {
6843 			ASSERT(ipsq->ipsq_mphead == NULL);
6844 			ipsq->ipsq_mphead = mp;
6845 		}
6846 		ipsq->ipsq_mptail = mp;
6847 		break;
6848 
6849 	case NEW_OP:
6850 		if (ipsq->ipsq_xopq_mptail != NULL) {
6851 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6852 			ipsq->ipsq_xopq_mptail->b_next = mp;
6853 		} else {
6854 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6855 			ipsq->ipsq_xopq_mphead = mp;
6856 		}
6857 		ipsq->ipsq_xopq_mptail = mp;
6858 		break;
6859 	default:
6860 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6861 	}
6862 
6863 	if (CONN_Q(q) && pending_ill != NULL) {
6864 		connp = Q_TO_CONN(q);
6865 
6866 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6867 		connp->conn_oper_pending_ill = pending_ill;
6868 	}
6869 }
6870 
6871 /*
6872  * Return the mp at the head of the ipsq. After emptying the ipsq
6873  * look at the next ioctl, if this ioctl is complete. Otherwise
6874  * return, we will resume when we complete the current ioctl.
6875  * The current ioctl will wait till it gets a response from the
6876  * driver below.
6877  */
6878 static mblk_t *
6879 ipsq_dq(ipsq_t *ipsq)
6880 {
6881 	mblk_t	*mp;
6882 
6883 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6884 
6885 	mp = ipsq->ipsq_mphead;
6886 	if (mp != NULL) {
6887 		ipsq->ipsq_mphead = mp->b_next;
6888 		if (ipsq->ipsq_mphead == NULL)
6889 			ipsq->ipsq_mptail = NULL;
6890 		mp->b_next = NULL;
6891 		return (mp);
6892 	}
6893 	if (ipsq->ipsq_current_ipif != NULL)
6894 		return (NULL);
6895 	mp = ipsq->ipsq_xopq_mphead;
6896 	if (mp != NULL) {
6897 		ipsq->ipsq_xopq_mphead = mp->b_next;
6898 		if (ipsq->ipsq_xopq_mphead == NULL)
6899 			ipsq->ipsq_xopq_mptail = NULL;
6900 		mp->b_next = NULL;
6901 		return (mp);
6902 	}
6903 	return (NULL);
6904 }
6905 
6906 /*
6907  * Enter the ipsq corresponding to ill, by waiting synchronously till
6908  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6909  * will have to drain completely before ipsq_enter returns success.
6910  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
6911  * and the ipsq_exit logic will start the next enqueued ioctl after
6912  * completion of the current ioctl. If 'force' is used, we don't wait
6913  * for the enqueued ioctls. This is needed when a conn_close wants to
6914  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6915  * of an ill can also use this option. But we dont' use it currently.
6916  */
6917 #define	ENTER_SQ_WAIT_TICKS 100
6918 boolean_t
6919 ipsq_enter(ill_t *ill, boolean_t force)
6920 {
6921 	ipsq_t	*ipsq;
6922 	boolean_t waited_enough = B_FALSE;
6923 
6924 	/*
6925 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
6926 	 * Since the <ill-ipsq> assocs could change while we wait for the
6927 	 * writer, it is easier to wait on a fixed global rather than try to
6928 	 * cv_wait on a changing ipsq.
6929 	 */
6930 	mutex_enter(&ill->ill_lock);
6931 	for (;;) {
6932 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6933 			mutex_exit(&ill->ill_lock);
6934 			return (B_FALSE);
6935 		}
6936 
6937 		ipsq = ill->ill_phyint->phyint_ipsq;
6938 		mutex_enter(&ipsq->ipsq_lock);
6939 		if (ipsq->ipsq_writer == NULL &&
6940 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
6941 			break;
6942 		} else if (ipsq->ipsq_writer != NULL) {
6943 			mutex_exit(&ipsq->ipsq_lock);
6944 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6945 		} else {
6946 			mutex_exit(&ipsq->ipsq_lock);
6947 			if (force) {
6948 				(void) cv_timedwait(&ill->ill_cv,
6949 				    &ill->ill_lock,
6950 				    lbolt + ENTER_SQ_WAIT_TICKS);
6951 				waited_enough = B_TRUE;
6952 				continue;
6953 			} else {
6954 				cv_wait(&ill->ill_cv, &ill->ill_lock);
6955 			}
6956 		}
6957 	}
6958 
6959 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
6960 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
6961 	ipsq->ipsq_writer = curthread;
6962 	ipsq->ipsq_reentry_cnt++;
6963 #ifdef ILL_DEBUG
6964 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
6965 #endif
6966 	mutex_exit(&ipsq->ipsq_lock);
6967 	mutex_exit(&ill->ill_lock);
6968 	return (B_TRUE);
6969 }
6970 
6971 /*
6972  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6973  * certain critical operations like plumbing (i.e. most set ioctls),
6974  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
6975  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
6976  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
6977  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
6978  * threads executing in the ipsq. Responses from the driver pertain to the
6979  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
6980  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
6981  *
6982  * If a thread does not want to reenter the ipsq when it is already writer,
6983  * it must make sure that the specified reentry point to be called later
6984  * when the ipsq is empty, nor any code path starting from the specified reentry
6985  * point must never ever try to enter the ipsq again. Otherwise it can lead
6986  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6987  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6988  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
6989  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
6990  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6991  * ioctl if the current ioctl has completed. If the current ioctl is still
6992  * in progress it simply returns. The current ioctl could be waiting for
6993  * a response from another module (arp_ or the driver or could be waiting for
6994  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
6995  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
6996  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6997  * ipsq_current_ipif is clear which happens only on ioctl completion.
6998  */
6999 
7000 /*
7001  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7002  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7003  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7004  * completion.
7005  */
7006 ipsq_t *
7007 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7008     ipsq_func_t func, int type, boolean_t reentry_ok)
7009 {
7010 	ipsq_t	*ipsq;
7011 
7012 	/* Only 1 of ipif or ill can be specified */
7013 	ASSERT((ipif != NULL) ^ (ill != NULL));
7014 	if (ipif != NULL)
7015 		ill = ipif->ipif_ill;
7016 
7017 	/*
7018 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7019 	 * ipsq of an ill can't change when ill_lock is held.
7020 	 */
7021 	GRAB_CONN_LOCK(q);
7022 	mutex_enter(&ill->ill_lock);
7023 	ipsq = ill->ill_phyint->phyint_ipsq;
7024 	mutex_enter(&ipsq->ipsq_lock);
7025 
7026 	/*
7027 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7028 	 *    (Note: If the caller does not specify reentry_ok then neither
7029 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7030 	 *    again. Otherwise it can lead to an infinite loop
7031 	 * 2. Enter the ipsq if there is no current writer and this attempted
7032 	 *    entry is part of the current ioctl or operation
7033 	 * 3. Enter the ipsq if there is no current writer and this is a new
7034 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7035 	 *    empty and there is no ioctl (or operation) currently in progress
7036 	 */
7037 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7038 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7039 	    ipsq->ipsq_current_ipif == NULL))) ||
7040 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7041 		/* Success. */
7042 		ipsq->ipsq_reentry_cnt++;
7043 		ipsq->ipsq_writer = curthread;
7044 		mutex_exit(&ipsq->ipsq_lock);
7045 		mutex_exit(&ill->ill_lock);
7046 		RELEASE_CONN_LOCK(q);
7047 #ifdef ILL_DEBUG
7048 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7049 #endif
7050 		return (ipsq);
7051 	}
7052 
7053 	ipsq_enq(ipsq, q, mp, func, type, ill);
7054 
7055 	mutex_exit(&ipsq->ipsq_lock);
7056 	mutex_exit(&ill->ill_lock);
7057 	RELEASE_CONN_LOCK(q);
7058 	return (NULL);
7059 }
7060 
7061 /*
7062  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7063  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7064  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7065  * completion.
7066  *
7067  * This function does a refrele on the ipif/ill.
7068  */
7069 void
7070 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7071     ipsq_func_t func, int type, boolean_t reentry_ok)
7072 {
7073 	ipsq_t	*ipsq;
7074 
7075 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7076 	/*
7077 	 * Caller must have done a refhold on the ipif. ipif_refrele
7078 	 * happens on the passed ipif. We can do this since we are
7079 	 * already exclusive, or we won't access ipif henceforth, Both
7080 	 * this func and caller will just return if we ipsq_try_enter
7081 	 * fails above. This is needed because func needs to
7082 	 * see the correct refcount. Eg. removeif can work only then.
7083 	 */
7084 	if (ipif != NULL)
7085 		ipif_refrele(ipif);
7086 	else
7087 		ill_refrele(ill);
7088 	if (ipsq != NULL) {
7089 		(*func)(ipsq, q, mp, NULL);
7090 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7091 	}
7092 }
7093 
7094 /*
7095  * If there are more than ILL_GRP_CNT ills in a group,
7096  * we use kmem alloc'd buffers, else use the stack
7097  */
7098 #define	ILL_GRP_CNT	14
7099 /*
7100  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7101  * Called by a thread that is currently exclusive on this ipsq.
7102  */
7103 void
7104 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7105 {
7106 	queue_t	*q;
7107 	mblk_t	*mp;
7108 	ipsq_func_t	func;
7109 	int	next;
7110 	ill_t	**ill_list = NULL;
7111 	size_t	ill_list_size = 0;
7112 	int	cnt = 0;
7113 	boolean_t need_ipsq_free = B_FALSE;
7114 
7115 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7116 	mutex_enter(&ipsq->ipsq_lock);
7117 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7118 	if (ipsq->ipsq_reentry_cnt != 1) {
7119 		ipsq->ipsq_reentry_cnt--;
7120 		mutex_exit(&ipsq->ipsq_lock);
7121 		return;
7122 	}
7123 
7124 	mp = ipsq_dq(ipsq);
7125 	while (mp != NULL) {
7126 again:
7127 		mutex_exit(&ipsq->ipsq_lock);
7128 		func = (ipsq_func_t)mp->b_prev;
7129 		q = (queue_t *)mp->b_queue;
7130 		mp->b_prev = NULL;
7131 		mp->b_queue = NULL;
7132 
7133 		/*
7134 		 * If 'q' is an conn queue, it is valid, since we did a
7135 		 * a refhold on the connp, at the start of the ioctl.
7136 		 * If 'q' is an ill queue, it is valid, since close of an
7137 		 * ill will clean up the 'ipsq'.
7138 		 */
7139 		(*func)(ipsq, q, mp, NULL);
7140 
7141 		mutex_enter(&ipsq->ipsq_lock);
7142 		mp = ipsq_dq(ipsq);
7143 	}
7144 
7145 	mutex_exit(&ipsq->ipsq_lock);
7146 
7147 	/*
7148 	 * Need to grab the locks in the right order. Need to
7149 	 * atomically check (under ipsq_lock) that there are no
7150 	 * messages before relinquishing the ipsq. Also need to
7151 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7152 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7153 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7154 	 * to grab ill_g_lock as writer.
7155 	 */
7156 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7157 
7158 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7159 	if (ipsq->ipsq_refs != 0) {
7160 		/* At most 2 ills v4/v6 per phyint */
7161 		cnt = ipsq->ipsq_refs << 1;
7162 		ill_list_size = cnt * sizeof (ill_t *);
7163 		/*
7164 		 * If memory allocation fails, we will do the split
7165 		 * the next time ipsq_exit is called for whatever reason.
7166 		 * As long as the ipsq_split flag is set the need to
7167 		 * split is remembered.
7168 		 */
7169 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7170 		if (ill_list != NULL)
7171 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7172 	}
7173 	mutex_enter(&ipsq->ipsq_lock);
7174 	mp = ipsq_dq(ipsq);
7175 	if (mp != NULL) {
7176 		/* oops, some message has landed up, we can't get out */
7177 		if (ill_list != NULL)
7178 			ill_unlock_ills(ill_list, cnt);
7179 		rw_exit(&ill_g_lock);
7180 		if (ill_list != NULL)
7181 			kmem_free(ill_list, ill_list_size);
7182 		ill_list = NULL;
7183 		ill_list_size = 0;
7184 		cnt = 0;
7185 		goto again;
7186 	}
7187 
7188 	/*
7189 	 * Split only if no ioctl is pending and if memory alloc succeeded
7190 	 * above.
7191 	 */
7192 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7193 		ill_list != NULL) {
7194 		/*
7195 		 * No new ill can join this ipsq since we are holding the
7196 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7197 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7198 		 * If so we will retry on the next ipsq_exit.
7199 		 */
7200 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7201 	}
7202 
7203 	/*
7204 	 * We are holding the ipsq lock, hence no new messages can
7205 	 * land up on the ipsq, and there are no messages currently.
7206 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7207 	 * atomically while holding ill locks.
7208 	 */
7209 	ipsq->ipsq_writer = NULL;
7210 	ipsq->ipsq_reentry_cnt--;
7211 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7212 #ifdef ILL_DEBUG
7213 	ipsq->ipsq_depth = 0;
7214 #endif
7215 	mutex_exit(&ipsq->ipsq_lock);
7216 	/*
7217 	 * For IPMP this should wake up all ills in this ipsq.
7218 	 * We need to hold the ill_lock while waking up waiters to
7219 	 * avoid missed wakeups. But there is no need to acquire all
7220 	 * the ill locks and then wakeup. If we have not acquired all
7221 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7222 	 * wakes up ills one at a time after getting the right ill_lock
7223 	 */
7224 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7225 	if (ill_list != NULL)
7226 		ill_unlock_ills(ill_list, cnt);
7227 	if (ipsq->ipsq_refs == 0)
7228 		need_ipsq_free = B_TRUE;
7229 	rw_exit(&ill_g_lock);
7230 	if (ill_list != 0)
7231 		kmem_free(ill_list, ill_list_size);
7232 
7233 	if (need_ipsq_free) {
7234 		/*
7235 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7236 		 * looked up. ipsq can be looked up only thru ill or phyint
7237 		 * and there are no ills/phyint on this ipsq.
7238 		 */
7239 		ipsq_delete(ipsq);
7240 	}
7241 	/*
7242 	 * Now start any igmp or mld timers that could not be started
7243 	 * while inside the ipsq. The timers can't be started while inside
7244 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7245 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7246 	 * there could be a deadlock since the timeout handlers
7247 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7248 	 * wait in ipsq_enter() trying to get the ipsq.
7249 	 *
7250 	 * However there is one exception to the above. If this thread is
7251 	 * itself the igmp/mld timeout handler thread, then we don't want
7252 	 * to start any new timer until the current handler is done. The
7253 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7254 	 * all others pass B_TRUE.
7255 	 */
7256 	if (start_igmp_timer) {
7257 		mutex_enter(&igmp_timer_lock);
7258 		next = igmp_deferred_next;
7259 		igmp_deferred_next = INFINITY;
7260 		mutex_exit(&igmp_timer_lock);
7261 
7262 		if (next != INFINITY)
7263 			igmp_start_timers(next);
7264 	}
7265 
7266 	if (start_mld_timer) {
7267 		mutex_enter(&mld_timer_lock);
7268 		next = mld_deferred_next;
7269 		mld_deferred_next = INFINITY;
7270 		mutex_exit(&mld_timer_lock);
7271 
7272 		if (next != INFINITY)
7273 			mld_start_timers(next);
7274 	}
7275 }
7276 
7277 /*
7278  * The ill is closing. Flush all messages on the ipsq that originated
7279  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7280  * for this ill since ipsq_enter could not have entered until then.
7281  * New messages can't be queued since the CONDEMNED flag is set.
7282  */
7283 static void
7284 ipsq_flush(ill_t *ill)
7285 {
7286 	queue_t	*q;
7287 	mblk_t	*prev;
7288 	mblk_t	*mp;
7289 	mblk_t	*mp_next;
7290 	ipsq_t	*ipsq;
7291 
7292 	ASSERT(IAM_WRITER_ILL(ill));
7293 	ipsq = ill->ill_phyint->phyint_ipsq;
7294 	/*
7295 	 * Flush any messages sent up by the driver.
7296 	 */
7297 	mutex_enter(&ipsq->ipsq_lock);
7298 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7299 		mp_next = mp->b_next;
7300 		q = mp->b_queue;
7301 		if (q == ill->ill_rq || q == ill->ill_wq) {
7302 			/* Remove the mp from the ipsq */
7303 			if (prev == NULL)
7304 				ipsq->ipsq_mphead = mp->b_next;
7305 			else
7306 				prev->b_next = mp->b_next;
7307 			if (ipsq->ipsq_mptail == mp) {
7308 				ASSERT(mp_next == NULL);
7309 				ipsq->ipsq_mptail = prev;
7310 			}
7311 			inet_freemsg(mp);
7312 		} else {
7313 			prev = mp;
7314 		}
7315 	}
7316 	mutex_exit(&ipsq->ipsq_lock);
7317 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7318 	ipsq_xopq_mp_cleanup(ill, NULL);
7319 	ill_pending_mp_cleanup(ill);
7320 }
7321 
7322 /*
7323  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7324  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7325  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7326  * time (possible with one port going down for aggr and someone tearing down the
7327  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7328  * to indicate when the cleanup has started (1 ref) and when the cleanup
7329  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7330  * putting 2 ref on ill_inuse_ref.
7331  */
7332 static void
7333 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7334 {
7335 	conn_t *connp;
7336 	squeue_t *sqp;
7337 	mblk_t *mp;
7338 
7339 	ASSERT(rx_ring != NULL);
7340 
7341 	/* Just clean one squeue */
7342 	mutex_enter(&ill->ill_lock);
7343 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7344 		/* Some operations pending on the ring. Wait */
7345 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7346 
7347 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7348 		/*
7349 		 * Someone already trying to clean
7350 		 * this squeue or its already been cleaned.
7351 		 */
7352 		mutex_exit(&ill->ill_lock);
7353 		return;
7354 	}
7355 	sqp = rx_ring->rr_sqp;
7356 
7357 	if (sqp == NULL) {
7358 		/*
7359 		 * The rx_ring never had a squeue assigned to it.
7360 		 * We are under ill_lock so we can clean it up
7361 		 * here itself since no one can get to it.
7362 		 */
7363 		rx_ring->rr_blank = NULL;
7364 		rx_ring->rr_handle = NULL;
7365 		rx_ring->rr_sqp = NULL;
7366 		rx_ring->rr_ring_state = ILL_RING_FREE;
7367 		mutex_exit(&ill->ill_lock);
7368 		return;
7369 	}
7370 
7371 	/* Set the state that its being cleaned */
7372 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7373 	ASSERT(sqp != NULL);
7374 	mutex_exit(&ill->ill_lock);
7375 
7376 	/*
7377 	 * Use the preallocated ill_unbind_conn for this purpose
7378 	 */
7379 	connp = ill->ill_poll_capab->ill_unbind_conn;
7380 	mp = &connp->conn_tcp->tcp_closemp;
7381 	CONN_INC_REF(connp);
7382 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7383 
7384 	mutex_enter(&ill->ill_lock);
7385 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7386 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7387 
7388 	mutex_exit(&ill->ill_lock);
7389 }
7390 
7391 static void
7392 ipsq_clean_all(ill_t *ill)
7393 {
7394 	int idx;
7395 
7396 	/*
7397 	 * No need to clean if poll_capab isn't set for this ill
7398 	 */
7399 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
7400 		return;
7401 
7402 	ill->ill_capabilities &= ~ILL_CAPAB_POLL;
7403 
7404 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7405 		ill_rx_ring_t *ipr = &ill->ill_poll_capab->ill_ring_tbl[idx];
7406 		ipsq_clean_ring(ill, ipr);
7407 	}
7408 }
7409 
7410 /* ARGSUSED */
7411 int
7412 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7413     ip_ioctl_cmd_t *ipip, void *ifreq)
7414 {
7415 	ill_t	*ill;
7416 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7417 	boolean_t isv6;
7418 	conn_t	*connp;
7419 
7420 	connp = Q_TO_CONN(q);
7421 	isv6 = connp->conn_af_isv6;
7422 	/*
7423 	 * Set original index.
7424 	 * Failover and failback move logical interfaces
7425 	 * from one physical interface to another.  The
7426 	 * original index indicates the parent of a logical
7427 	 * interface, in other words, the physical interface
7428 	 * the logical interface will be moved back to on
7429 	 * failback.
7430 	 */
7431 
7432 	/*
7433 	 * Don't allow the original index to be changed
7434 	 * for non-failover addresses, autoconfigured
7435 	 * addresses, or IPv6 link local addresses.
7436 	 */
7437 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7438 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7439 		return (EINVAL);
7440 	}
7441 	/*
7442 	 * The new original index must be in use by some
7443 	 * physical interface.
7444 	 */
7445 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7446 	    NULL, NULL);
7447 	if (ill == NULL)
7448 		return (ENXIO);
7449 	ill_refrele(ill);
7450 
7451 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7452 	/*
7453 	 * When this ipif gets failed back, don't
7454 	 * preserve the original id, as it is no
7455 	 * longer applicable.
7456 	 */
7457 	ipif->ipif_orig_ipifid = 0;
7458 	/*
7459 	 * For IPv4, change the original index of any
7460 	 * multicast addresses associated with the
7461 	 * ipif to the new value.
7462 	 */
7463 	if (!isv6) {
7464 		ilm_t *ilm;
7465 
7466 		mutex_enter(&ipif->ipif_ill->ill_lock);
7467 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7468 		    ilm = ilm->ilm_next) {
7469 			if (ilm->ilm_ipif == ipif) {
7470 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7471 			}
7472 		}
7473 		mutex_exit(&ipif->ipif_ill->ill_lock);
7474 	}
7475 	return (0);
7476 }
7477 
7478 /* ARGSUSED */
7479 int
7480 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7481     ip_ioctl_cmd_t *ipip, void *ifreq)
7482 {
7483 	struct lifreq *lifr = (struct lifreq *)ifreq;
7484 
7485 	/*
7486 	 * Get the original interface index i.e the one
7487 	 * before FAILOVER if it ever happened.
7488 	 */
7489 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7490 	return (0);
7491 }
7492 
7493 /*
7494  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7495  * refhold and return the associated ipif
7496  */
7497 int
7498 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7499 {
7500 	boolean_t exists;
7501 	struct iftun_req *ta;
7502 	ipif_t	*ipif;
7503 	ill_t	*ill;
7504 	boolean_t isv6;
7505 	mblk_t	*mp1;
7506 	int	error;
7507 	conn_t	*connp;
7508 
7509 	/* Existence verified in ip_wput_nondata */
7510 	mp1 = mp->b_cont->b_cont;
7511 	ta = (struct iftun_req *)mp1->b_rptr;
7512 	/*
7513 	 * Null terminate the string to protect against buffer
7514 	 * overrun. String was generated by user code and may not
7515 	 * be trusted.
7516 	 */
7517 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7518 
7519 	connp = Q_TO_CONN(q);
7520 	isv6 = connp->conn_af_isv6;
7521 
7522 	/* Disallows implicit create */
7523 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7524 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7525 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7526 	if (ipif == NULL)
7527 		return (error);
7528 
7529 	if (ipif->ipif_id != 0) {
7530 		/*
7531 		 * We really don't want to set/get tunnel parameters
7532 		 * on virtual tunnel interfaces.  Only allow the
7533 		 * base tunnel to do these.
7534 		 */
7535 		ipif_refrele(ipif);
7536 		return (EINVAL);
7537 	}
7538 
7539 	/*
7540 	 * Send down to tunnel mod for ioctl processing.
7541 	 * Will finish ioctl in ip_rput_other().
7542 	 */
7543 	ill = ipif->ipif_ill;
7544 	if (ill->ill_net_type == IRE_LOOPBACK) {
7545 		ipif_refrele(ipif);
7546 		return (EOPNOTSUPP);
7547 	}
7548 
7549 	if (ill->ill_wq == NULL) {
7550 		ipif_refrele(ipif);
7551 		return (ENXIO);
7552 	}
7553 	/*
7554 	 * Mark the ioctl as coming from an IPv6 interface for
7555 	 * tun's convenience.
7556 	 */
7557 	if (ill->ill_isv6)
7558 		ta->ifta_flags |= 0x80000000;
7559 	*ipifp = ipif;
7560 	return (0);
7561 }
7562 
7563 /*
7564  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7565  * and return the associated ipif.
7566  * Return value:
7567  *	Non zero: An error has occurred. ci may not be filled out.
7568  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7569  *	a held ipif in ci.ci_ipif.
7570  */
7571 int
7572 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7573     cmd_info_t *ci, ipsq_func_t func)
7574 {
7575 	sin_t		*sin;
7576 	sin6_t		*sin6;
7577 	char		*name;
7578 	struct ifreq    *ifr;
7579 	struct lifreq    *lifr;
7580 	ipif_t		*ipif = NULL;
7581 	ill_t		*ill;
7582 	conn_t		*connp;
7583 	boolean_t	isv6;
7584 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7585 	boolean_t	exists;
7586 	int		err;
7587 	mblk_t		*mp1;
7588 	zoneid_t	zoneid;
7589 
7590 	if (q->q_next != NULL) {
7591 		ill = (ill_t *)q->q_ptr;
7592 		isv6 = ill->ill_isv6;
7593 		connp = NULL;
7594 		zoneid = ALL_ZONES;
7595 	} else {
7596 		ill = NULL;
7597 		connp = Q_TO_CONN(q);
7598 		isv6 = connp->conn_af_isv6;
7599 		zoneid = connp->conn_zoneid;
7600 		if (zoneid == GLOBAL_ZONEID) {
7601 			/* global zone can access ipifs in all zones */
7602 			zoneid = ALL_ZONES;
7603 		}
7604 	}
7605 
7606 	/* Has been checked in ip_wput_nondata */
7607 	mp1 = mp->b_cont->b_cont;
7608 
7609 
7610 	if (cmd_type == IF_CMD) {
7611 		/* This a old style SIOC[GS]IF* command */
7612 		ifr = (struct ifreq *)mp1->b_rptr;
7613 		/*
7614 		 * Null terminate the string to protect against buffer
7615 		 * overrun. String was generated by user code and may not
7616 		 * be trusted.
7617 		 */
7618 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7619 		sin = (sin_t *)&ifr->ifr_addr;
7620 		name = ifr->ifr_name;
7621 		ci->ci_sin = sin;
7622 		ci->ci_sin6 = NULL;
7623 		ci->ci_lifr = (struct lifreq *)ifr;
7624 	} else {
7625 		/* This a new style SIOC[GS]LIF* command */
7626 		ASSERT(cmd_type == LIF_CMD);
7627 		lifr = (struct lifreq *)mp1->b_rptr;
7628 		/*
7629 		 * Null terminate the string to protect against buffer
7630 		 * overrun. String was generated by user code and may not
7631 		 * be trusted.
7632 		 */
7633 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7634 		name = lifr->lifr_name;
7635 		sin = (sin_t *)&lifr->lifr_addr;
7636 		sin6 = (sin6_t *)&lifr->lifr_addr;
7637 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7638 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7639 			    LIFNAMSIZ);
7640 		}
7641 		ci->ci_sin = sin;
7642 		ci->ci_sin6 = sin6;
7643 		ci->ci_lifr = lifr;
7644 	}
7645 
7646 
7647 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7648 		/*
7649 		 * The ioctl will be failed if the ioctl comes down
7650 		 * an conn stream
7651 		 */
7652 		if (ill == NULL) {
7653 			/*
7654 			 * Not an ill queue, return EINVAL same as the
7655 			 * old error code.
7656 			 */
7657 			return (ENXIO);
7658 		}
7659 		ipif = ill->ill_ipif;
7660 		ipif_refhold(ipif);
7661 	} else {
7662 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7663 		    &exists, isv6, zoneid,
7664 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7665 		if (ipif == NULL) {
7666 			if (err == EINPROGRESS)
7667 				return (err);
7668 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7669 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7670 				/*
7671 				 * Need to try both v4 and v6 since this
7672 				 * ioctl can come down either v4 or v6
7673 				 * socket. The lifreq.lifr_family passed
7674 				 * down by this ioctl is AF_UNSPEC.
7675 				 */
7676 				ipif = ipif_lookup_on_name(name,
7677 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7678 				    zoneid, (connp == NULL) ? q :
7679 				    CONNP_TO_WQ(connp), mp, func, &err);
7680 				if (err == EINPROGRESS)
7681 					return (err);
7682 			}
7683 			err = 0;	/* Ensure we don't use it below */
7684 		}
7685 	}
7686 
7687 	/*
7688 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7689 	 */
7690 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7691 		ipif_refrele(ipif);
7692 		return (ENXIO);
7693 	}
7694 
7695 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7696 	    name[0] == '\0') {
7697 		/*
7698 		 * Handle a or a SIOC?IF* with a null name
7699 		 * during plumb (on the ill queue before the I_PLINK).
7700 		 */
7701 		ipif = ill->ill_ipif;
7702 		ipif_refhold(ipif);
7703 	}
7704 
7705 	if (ipif == NULL)
7706 		return (ENXIO);
7707 
7708 	/*
7709 	 * Allow only GET operations if this ipif has been created
7710 	 * temporarily due to a MOVE operation.
7711 	 */
7712 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7713 		ipif_refrele(ipif);
7714 		return (EINVAL);
7715 	}
7716 
7717 	ci->ci_ipif = ipif;
7718 	return (0);
7719 }
7720 
7721 /*
7722  * Return the total number of ipifs.
7723  */
7724 static uint_t
7725 ip_get_numifs(zoneid_t zoneid)
7726 {
7727 	uint_t numifs = 0;
7728 	ill_t	*ill;
7729 	ill_walk_context_t	ctx;
7730 	ipif_t	*ipif;
7731 
7732 	rw_enter(&ill_g_lock, RW_READER);
7733 	ill = ILL_START_WALK_V4(&ctx);
7734 
7735 	while (ill != NULL) {
7736 		for (ipif = ill->ill_ipif; ipif != NULL;
7737 		    ipif = ipif->ipif_next) {
7738 			if (ipif->ipif_zoneid == zoneid)
7739 				numifs++;
7740 		}
7741 		ill = ill_next(&ctx, ill);
7742 	}
7743 	rw_exit(&ill_g_lock);
7744 	return (numifs);
7745 }
7746 
7747 /*
7748  * Return the total number of ipifs.
7749  */
7750 static uint_t
7751 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7752 {
7753 	uint_t numifs = 0;
7754 	ill_t	*ill;
7755 	ipif_t	*ipif;
7756 	ill_walk_context_t	ctx;
7757 
7758 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7759 
7760 	rw_enter(&ill_g_lock, RW_READER);
7761 	if (family == AF_INET)
7762 		ill = ILL_START_WALK_V4(&ctx);
7763 	else if (family == AF_INET6)
7764 		ill = ILL_START_WALK_V6(&ctx);
7765 	else
7766 		ill = ILL_START_WALK_ALL(&ctx);
7767 
7768 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7769 		for (ipif = ill->ill_ipif; ipif != NULL;
7770 		    ipif = ipif->ipif_next) {
7771 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7772 			    !(lifn_flags & LIFC_NOXMIT))
7773 				continue;
7774 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7775 			    !(lifn_flags & LIFC_TEMPORARY))
7776 				continue;
7777 			if (((ipif->ipif_flags &
7778 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7779 			    IPIF_DEPRECATED)) ||
7780 			    (ill->ill_phyint->phyint_flags &
7781 			    PHYI_LOOPBACK) ||
7782 			    !(ipif->ipif_flags & IPIF_UP)) &&
7783 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7784 				continue;
7785 
7786 			if (zoneid != ipif->ipif_zoneid &&
7787 			    (zoneid != GLOBAL_ZONEID ||
7788 			    !(lifn_flags & LIFC_ALLZONES)))
7789 				continue;
7790 
7791 			numifs++;
7792 		}
7793 	}
7794 	rw_exit(&ill_g_lock);
7795 	return (numifs);
7796 }
7797 
7798 uint_t
7799 ip_get_lifsrcofnum(ill_t *ill)
7800 {
7801 	uint_t numifs = 0;
7802 	ill_t	*ill_head = ill;
7803 
7804 	/*
7805 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7806 	 * other thread may be trying to relink the ILLs in this usesrc group
7807 	 * and adjusting the ill_usesrc_grp_next pointers
7808 	 */
7809 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7810 	if ((ill->ill_usesrc_ifindex == 0) &&
7811 	    (ill->ill_usesrc_grp_next != NULL)) {
7812 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7813 		    ill = ill->ill_usesrc_grp_next)
7814 			numifs++;
7815 	}
7816 	rw_exit(&ill_g_usesrc_lock);
7817 
7818 	return (numifs);
7819 }
7820 
7821 /* Null values are passed in for ipif, sin, and ifreq */
7822 /* ARGSUSED */
7823 int
7824 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7825     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7826 {
7827 	int *nump;
7828 
7829 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7830 
7831 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7832 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7833 
7834 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
7835 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7836 	return (0);
7837 }
7838 
7839 /* Null values are passed in for ipif, sin, and ifreq */
7840 /* ARGSUSED */
7841 int
7842 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7843     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7844 {
7845 	struct lifnum *lifn;
7846 	mblk_t	*mp1;
7847 
7848 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7849 
7850 	/* Existence checked in ip_wput_nondata */
7851 	mp1 = mp->b_cont->b_cont;
7852 
7853 	lifn = (struct lifnum *)mp1->b_rptr;
7854 	switch (lifn->lifn_family) {
7855 	case AF_UNSPEC:
7856 	case AF_INET:
7857 	case AF_INET6:
7858 		break;
7859 	default:
7860 		return (EAFNOSUPPORT);
7861 	}
7862 
7863 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7864 	    Q_TO_CONN(q)->conn_zoneid);
7865 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7866 	return (0);
7867 }
7868 
7869 /* ARGSUSED */
7870 int
7871 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7872     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7873 {
7874 	STRUCT_HANDLE(ifconf, ifc);
7875 	mblk_t *mp1;
7876 	struct iocblk *iocp;
7877 	struct ifreq *ifr;
7878 	ill_walk_context_t	ctx;
7879 	ill_t	*ill;
7880 	ipif_t	*ipif;
7881 	struct sockaddr_in *sin;
7882 	int32_t	ifclen;
7883 	zoneid_t zoneid;
7884 
7885 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7886 
7887 	ip1dbg(("ip_sioctl_get_ifconf"));
7888 	/* Existence verified in ip_wput_nondata */
7889 	mp1 = mp->b_cont->b_cont;
7890 	iocp = (struct iocblk *)mp->b_rptr;
7891 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7892 
7893 	/*
7894 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7895 	 * the user buffer address and length into which the list of struct
7896 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7897 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7898 	 * the SIOCGIFCONF operation was redefined to simply provide
7899 	 * a large output buffer into which we are supposed to jam the ifreq
7900 	 * array.  The same ioctl command code was used, despite the fact that
7901 	 * both the applications and the kernel code had to change, thus making
7902 	 * it impossible to support both interfaces.
7903 	 *
7904 	 * For reasons not good enough to try to explain, the following
7905 	 * algorithm is used for deciding what to do with one of these:
7906 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7907 	 * form with the output buffer coming down as the continuation message.
7908 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7909 	 * and we have to copy in the ifconf structure to find out how big the
7910 	 * output buffer is and where to copy out to.  Sure no problem...
7911 	 *
7912 	 */
7913 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7914 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7915 		int numifs = 0;
7916 		size_t ifc_bufsize;
7917 
7918 		/*
7919 		 * Must be (better be!) continuation of a TRANSPARENT
7920 		 * IOCTL.  We just copied in the ifconf structure.
7921 		 */
7922 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7923 		    (struct ifconf *)mp1->b_rptr);
7924 
7925 		/*
7926 		 * Allocate a buffer to hold requested information.
7927 		 *
7928 		 * If ifc_len is larger than what is needed, we only
7929 		 * allocate what we will use.
7930 		 *
7931 		 * If ifc_len is smaller than what is needed, return
7932 		 * EINVAL.
7933 		 *
7934 		 * XXX: the ill_t structure can hava 2 counters, for
7935 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7936 		 * number of interfaces for a device, so we don't need
7937 		 * to count them here...
7938 		 */
7939 		numifs = ip_get_numifs(zoneid);
7940 
7941 		ifclen = STRUCT_FGET(ifc, ifc_len);
7942 		ifc_bufsize = numifs * sizeof (struct ifreq);
7943 		if (ifc_bufsize > ifclen) {
7944 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7945 				/* old behaviour */
7946 				return (EINVAL);
7947 			} else {
7948 				ifc_bufsize = ifclen;
7949 			}
7950 		}
7951 
7952 		mp1 = mi_copyout_alloc(q, mp,
7953 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7954 		if (mp1 == NULL)
7955 			return (ENOMEM);
7956 
7957 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7958 	}
7959 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7960 	/*
7961 	 * the SIOCGIFCONF ioctl only knows about
7962 	 * IPv4 addresses, so don't try to tell
7963 	 * it about interfaces with IPv6-only
7964 	 * addresses. (Last parm 'isv6' is B_FALSE)
7965 	 */
7966 
7967 	ifr = (struct ifreq *)mp1->b_rptr;
7968 
7969 	rw_enter(&ill_g_lock, RW_READER);
7970 	ill = ILL_START_WALK_V4(&ctx);
7971 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7972 		for (ipif = ill->ill_ipif; ipif;
7973 		    ipif = ipif->ipif_next) {
7974 			if (zoneid != ipif->ipif_zoneid)
7975 				continue;
7976 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7977 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7978 					/* old behaviour */
7979 					rw_exit(&ill_g_lock);
7980 					return (EINVAL);
7981 				} else {
7982 					goto if_copydone;
7983 				}
7984 			}
7985 			(void) ipif_get_name(ipif,
7986 			    ifr->ifr_name,
7987 			    sizeof (ifr->ifr_name));
7988 			sin = (sin_t *)&ifr->ifr_addr;
7989 			*sin = sin_null;
7990 			sin->sin_family = AF_INET;
7991 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7992 			ifr++;
7993 		}
7994 	}
7995 if_copydone:
7996 	rw_exit(&ill_g_lock);
7997 	mp1->b_wptr = (uchar_t *)ifr;
7998 
7999 	if (STRUCT_BUF(ifc) != NULL) {
8000 		STRUCT_FSET(ifc, ifc_len,
8001 			(int)((uchar_t *)ifr - mp1->b_rptr));
8002 	}
8003 	return (0);
8004 }
8005 
8006 /*
8007  * Get the interfaces using the address hosted on the interface passed in,
8008  * as a source adddress
8009  */
8010 /* ARGSUSED */
8011 int
8012 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8013     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8014 {
8015 	mblk_t *mp1;
8016 	ill_t	*ill, *ill_head;
8017 	ipif_t	*ipif, *orig_ipif;
8018 	int	numlifs = 0;
8019 	size_t	lifs_bufsize, lifsmaxlen;
8020 	struct	lifreq *lifr;
8021 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8022 	uint_t	ifindex;
8023 	zoneid_t zoneid;
8024 	int err = 0;
8025 	boolean_t isv6 = B_FALSE;
8026 	struct	sockaddr_in	*sin;
8027 	struct	sockaddr_in6	*sin6;
8028 
8029 	STRUCT_HANDLE(lifsrcof, lifs);
8030 
8031 	ASSERT(q->q_next == NULL);
8032 
8033 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8034 
8035 	/* Existence verified in ip_wput_nondata */
8036 	mp1 = mp->b_cont->b_cont;
8037 
8038 	/*
8039 	 * Must be (better be!) continuation of a TRANSPARENT
8040 	 * IOCTL.  We just copied in the lifsrcof structure.
8041 	 */
8042 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8043 	    (struct lifsrcof *)mp1->b_rptr);
8044 
8045 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8046 		return (EINVAL);
8047 
8048 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8049 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8050 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8051 	    ip_process_ioctl, &err);
8052 	if (ipif == NULL) {
8053 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8054 		    ifindex));
8055 		return (err);
8056 	}
8057 
8058 
8059 	/* Allocate a buffer to hold requested information */
8060 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8061 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8062 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8063 	/* The actual size needed is always returned in lifs_len */
8064 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8065 
8066 	/* If the amount we need is more than what is passed in, abort */
8067 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8068 		ipif_refrele(ipif);
8069 		return (0);
8070 	}
8071 
8072 	mp1 = mi_copyout_alloc(q, mp,
8073 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8074 	if (mp1 == NULL) {
8075 		ipif_refrele(ipif);
8076 		return (ENOMEM);
8077 	}
8078 
8079 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8080 	bzero(mp1->b_rptr, lifs_bufsize);
8081 
8082 	lifr = (struct lifreq *)mp1->b_rptr;
8083 
8084 	ill = ill_head = ipif->ipif_ill;
8085 	orig_ipif = ipif;
8086 
8087 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8088 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8089 	rw_enter(&ill_g_lock, RW_READER);
8090 
8091 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8092 	for (; (ill != NULL) && (ill != ill_head);
8093 	    ill = ill->ill_usesrc_grp_next) {
8094 
8095 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8096 			break;
8097 
8098 		ipif = ill->ill_ipif;
8099 		(void) ipif_get_name(ipif,
8100 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8101 		if (ipif->ipif_isv6) {
8102 			sin6 = (sin6_t *)&lifr->lifr_addr;
8103 			*sin6 = sin6_null;
8104 			sin6->sin6_family = AF_INET6;
8105 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8106 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8107 			    &ipif->ipif_v6net_mask);
8108 		} else {
8109 			sin = (sin_t *)&lifr->lifr_addr;
8110 			*sin = sin_null;
8111 			sin->sin_family = AF_INET;
8112 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8113 			lifr->lifr_addrlen = ip_mask_to_plen(
8114 			    ipif->ipif_net_mask);
8115 		}
8116 		lifr++;
8117 	}
8118 	rw_exit(&ill_g_usesrc_lock);
8119 	rw_exit(&ill_g_lock);
8120 	ipif_refrele(orig_ipif);
8121 	mp1->b_wptr = (uchar_t *)lifr;
8122 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8123 
8124 	return (0);
8125 }
8126 
8127 /* ARGSUSED */
8128 int
8129 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8130     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8131 {
8132 	mblk_t *mp1;
8133 	int	list;
8134 	ill_t	*ill;
8135 	ipif_t	*ipif;
8136 	int	flags;
8137 	int	numlifs = 0;
8138 	size_t	lifc_bufsize;
8139 	struct	lifreq *lifr;
8140 	sa_family_t	family;
8141 	struct	sockaddr_in	*sin;
8142 	struct	sockaddr_in6	*sin6;
8143 	ill_walk_context_t	ctx;
8144 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8145 	int32_t	lifclen;
8146 	zoneid_t zoneid;
8147 	STRUCT_HANDLE(lifconf, lifc);
8148 
8149 	ip1dbg(("ip_sioctl_get_lifconf"));
8150 
8151 	ASSERT(q->q_next == NULL);
8152 
8153 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8154 
8155 	/* Existence verified in ip_wput_nondata */
8156 	mp1 = mp->b_cont->b_cont;
8157 
8158 	/*
8159 	 * An extended version of SIOCGIFCONF that takes an
8160 	 * additional address family and flags field.
8161 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8162 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8163 	 * interfaces are omitted.
8164 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8165 	 * unless LIFC_TEMPORARY is specified.
8166 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8167 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8168 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8169 	 * has priority over LIFC_NOXMIT.
8170 	 */
8171 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8172 
8173 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8174 		return (EINVAL);
8175 
8176 	/*
8177 	 * Must be (better be!) continuation of a TRANSPARENT
8178 	 * IOCTL.  We just copied in the lifconf structure.
8179 	 */
8180 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8181 
8182 	family = STRUCT_FGET(lifc, lifc_family);
8183 	flags = STRUCT_FGET(lifc, lifc_flags);
8184 
8185 	switch (family) {
8186 	case AF_UNSPEC:
8187 		/*
8188 		 * walk all ILL's.
8189 		 */
8190 		list = MAX_G_HEADS;
8191 		break;
8192 	case AF_INET:
8193 		/*
8194 		 * walk only IPV4 ILL's.
8195 		 */
8196 		list = IP_V4_G_HEAD;
8197 		break;
8198 	case AF_INET6:
8199 		/*
8200 		 * walk only IPV6 ILL's.
8201 		 */
8202 		list = IP_V6_G_HEAD;
8203 		break;
8204 	default:
8205 		return (EAFNOSUPPORT);
8206 	}
8207 
8208 	/*
8209 	 * Allocate a buffer to hold requested information.
8210 	 *
8211 	 * If lifc_len is larger than what is needed, we only
8212 	 * allocate what we will use.
8213 	 *
8214 	 * If lifc_len is smaller than what is needed, return
8215 	 * EINVAL.
8216 	 */
8217 	numlifs = ip_get_numlifs(family, flags, zoneid);
8218 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8219 	lifclen = STRUCT_FGET(lifc, lifc_len);
8220 	if (lifc_bufsize > lifclen) {
8221 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8222 			return (EINVAL);
8223 		else
8224 			lifc_bufsize = lifclen;
8225 	}
8226 
8227 	mp1 = mi_copyout_alloc(q, mp,
8228 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8229 	if (mp1 == NULL)
8230 		return (ENOMEM);
8231 
8232 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8233 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8234 
8235 	lifr = (struct lifreq *)mp1->b_rptr;
8236 
8237 	rw_enter(&ill_g_lock, RW_READER);
8238 	ill = ill_first(list, list, &ctx);
8239 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8240 		for (ipif = ill->ill_ipif; ipif != NULL;
8241 		    ipif = ipif->ipif_next) {
8242 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8243 			    !(flags & LIFC_NOXMIT))
8244 				continue;
8245 
8246 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8247 			    !(flags & LIFC_TEMPORARY))
8248 				continue;
8249 
8250 			if (((ipif->ipif_flags &
8251 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8252 			    IPIF_DEPRECATED)) ||
8253 			    (ill->ill_phyint->phyint_flags &
8254 			    PHYI_LOOPBACK) ||
8255 			    !(ipif->ipif_flags & IPIF_UP)) &&
8256 			    (flags & LIFC_EXTERNAL_SOURCE))
8257 				continue;
8258 
8259 			if (zoneid != ipif->ipif_zoneid &&
8260 			    (zoneid != GLOBAL_ZONEID ||
8261 			    !(flags & LIFC_ALLZONES)))
8262 				continue;
8263 
8264 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8265 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8266 					rw_exit(&ill_g_lock);
8267 					return (EINVAL);
8268 				} else {
8269 					goto lif_copydone;
8270 				}
8271 			}
8272 
8273 			(void) ipif_get_name(ipif,
8274 				lifr->lifr_name,
8275 				sizeof (lifr->lifr_name));
8276 			if (ipif->ipif_isv6) {
8277 				sin6 = (sin6_t *)&lifr->lifr_addr;
8278 				*sin6 = sin6_null;
8279 				sin6->sin6_family = AF_INET6;
8280 				sin6->sin6_addr =
8281 				ipif->ipif_v6lcl_addr;
8282 				lifr->lifr_addrlen =
8283 				ip_mask_to_plen_v6(
8284 				    &ipif->ipif_v6net_mask);
8285 			} else {
8286 				sin = (sin_t *)&lifr->lifr_addr;
8287 				*sin = sin_null;
8288 				sin->sin_family = AF_INET;
8289 				sin->sin_addr.s_addr =
8290 				    ipif->ipif_lcl_addr;
8291 				lifr->lifr_addrlen =
8292 				    ip_mask_to_plen(
8293 				    ipif->ipif_net_mask);
8294 			}
8295 			lifr++;
8296 		}
8297 	}
8298 lif_copydone:
8299 	rw_exit(&ill_g_lock);
8300 
8301 	mp1->b_wptr = (uchar_t *)lifr;
8302 	if (STRUCT_BUF(lifc) != NULL) {
8303 		STRUCT_FSET(lifc, lifc_len,
8304 			(int)((uchar_t *)lifr - mp1->b_rptr));
8305 	}
8306 	return (0);
8307 }
8308 
8309 /* ARGSUSED */
8310 int
8311 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8312     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8313 {
8314 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8315 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8316 	return (0);
8317 }
8318 
8319 static void
8320 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8321 {
8322 	ip6_asp_t *table;
8323 	size_t table_size;
8324 	mblk_t *data_mp;
8325 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8326 
8327 	/* These two ioctls are I_STR only */
8328 	if (iocp->ioc_count == TRANSPARENT) {
8329 		miocnak(q, mp, 0, EINVAL);
8330 		return;
8331 	}
8332 
8333 	data_mp = mp->b_cont;
8334 	if (data_mp == NULL) {
8335 		/* The user passed us a NULL argument */
8336 		table = NULL;
8337 		table_size = iocp->ioc_count;
8338 	} else {
8339 		/*
8340 		 * The user provided a table.  The stream head
8341 		 * may have copied in the user data in chunks,
8342 		 * so make sure everything is pulled up
8343 		 * properly.
8344 		 */
8345 		if (MBLKL(data_mp) < iocp->ioc_count) {
8346 			mblk_t *new_data_mp;
8347 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8348 			    NULL) {
8349 				miocnak(q, mp, 0, ENOMEM);
8350 				return;
8351 			}
8352 			freemsg(data_mp);
8353 			data_mp = new_data_mp;
8354 			mp->b_cont = data_mp;
8355 		}
8356 		table = (ip6_asp_t *)data_mp->b_rptr;
8357 		table_size = iocp->ioc_count;
8358 	}
8359 
8360 	switch (iocp->ioc_cmd) {
8361 	case SIOCGIP6ADDRPOLICY:
8362 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8363 		if (iocp->ioc_rval == -1)
8364 			iocp->ioc_error = EINVAL;
8365 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8366 		else if (table != NULL &&
8367 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8368 			ip6_asp_t *src = table;
8369 			ip6_asp32_t *dst = (void *)table;
8370 			int count = table_size / sizeof (ip6_asp_t);
8371 			int i;
8372 
8373 			/*
8374 			 * We need to do an in-place shrink of the array
8375 			 * to match the alignment attributes of the
8376 			 * 32-bit ABI looking at it.
8377 			 */
8378 			/* LINTED: logical expression always true: op "||" */
8379 			ASSERT(sizeof (*src) > sizeof (*dst));
8380 			for (i = 1; i < count; i++)
8381 				bcopy(src + i, dst + i, sizeof (*dst));
8382 		}
8383 #endif
8384 		break;
8385 
8386 	case SIOCSIP6ADDRPOLICY:
8387 		ASSERT(mp->b_prev == NULL);
8388 		mp->b_prev = (void *)q;
8389 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8390 		/*
8391 		 * We pass in the datamodel here so that the ip6_asp_replace()
8392 		 * routine can handle converting from 32-bit to native formats
8393 		 * where necessary.
8394 		 *
8395 		 * A better way to handle this might be to convert the inbound
8396 		 * data structure here, and hang it off a new 'mp'; thus the
8397 		 * ip6_asp_replace() logic would always be dealing with native
8398 		 * format data structures..
8399 		 *
8400 		 * (An even simpler way to handle these ioctls is to just
8401 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8402 		 * and just recompile everything that depends on it.)
8403 		 */
8404 #endif
8405 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8406 		    iocp->ioc_flag & IOC_MODELS);
8407 		return;
8408 	}
8409 
8410 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8411 	qreply(q, mp);
8412 }
8413 
8414 static void
8415 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8416 {
8417 	mblk_t 		*data_mp;
8418 	struct dstinforeq	*dir;
8419 	uint8_t		*end, *cur;
8420 	in6_addr_t	*daddr, *saddr;
8421 	ipaddr_t	v4daddr;
8422 	ire_t		*ire;
8423 	char		*slabel, *dlabel;
8424 	boolean_t	isipv4;
8425 	int		match_ire;
8426 	ill_t		*dst_ill;
8427 	ipif_t		*src_ipif, *ire_ipif;
8428 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8429 	zoneid_t	zoneid;
8430 
8431 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8432 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8433 
8434 	/*
8435 	 * This ioctl is I_STR only, and must have a
8436 	 * data mblk following the M_IOCTL mblk.
8437 	 */
8438 	data_mp = mp->b_cont;
8439 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8440 		miocnak(q, mp, 0, EINVAL);
8441 		return;
8442 	}
8443 
8444 	if (MBLKL(data_mp) < iocp->ioc_count) {
8445 		mblk_t *new_data_mp;
8446 
8447 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8448 			miocnak(q, mp, 0, ENOMEM);
8449 			return;
8450 		}
8451 		freemsg(data_mp);
8452 		data_mp = new_data_mp;
8453 		mp->b_cont = data_mp;
8454 	}
8455 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8456 
8457 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8458 	    end - cur >= sizeof (struct dstinforeq);
8459 	    cur += sizeof (struct dstinforeq)) {
8460 		dir = (struct dstinforeq *)cur;
8461 		daddr = &dir->dir_daddr;
8462 		saddr = &dir->dir_saddr;
8463 
8464 		/*
8465 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8466 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8467 		 * and ipif_select_source[_v6]() do not.
8468 		 */
8469 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8470 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8471 
8472 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8473 		if (isipv4) {
8474 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8475 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8476 			    0, NULL, NULL, zoneid, 0, match_ire);
8477 		} else {
8478 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8479 			    0, NULL, NULL, zoneid, 0, match_ire);
8480 		}
8481 		if (ire == NULL) {
8482 			dir->dir_dreachable = 0;
8483 
8484 			/* move on to next dst addr */
8485 			continue;
8486 		}
8487 		dir->dir_dreachable = 1;
8488 
8489 		ire_ipif = ire->ire_ipif;
8490 		if (ire_ipif == NULL)
8491 			goto next_dst;
8492 
8493 		/*
8494 		 * We expect to get back an interface ire or a
8495 		 * gateway ire cache entry.  For both types, the
8496 		 * output interface is ire_ipif->ipif_ill.
8497 		 */
8498 		dst_ill = ire_ipif->ipif_ill;
8499 		dir->dir_dmactype = dst_ill->ill_mactype;
8500 
8501 		if (isipv4) {
8502 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8503 		} else {
8504 			src_ipif = ipif_select_source_v6(dst_ill,
8505 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8506 			    zoneid);
8507 		}
8508 		if (src_ipif == NULL)
8509 			goto next_dst;
8510 
8511 		*saddr = src_ipif->ipif_v6lcl_addr;
8512 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8513 		slabel = ip6_asp_lookup(saddr, NULL);
8514 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8515 		dir->dir_sdeprecated =
8516 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8517 		ipif_refrele(src_ipif);
8518 next_dst:
8519 		ire_refrele(ire);
8520 	}
8521 	miocack(q, mp, iocp->ioc_count, 0);
8522 }
8523 
8524 
8525 /*
8526  * Check if this is an address assigned to this machine.
8527  * Skips interfaces that are down by using ire checks.
8528  * Translates mapped addresses to v4 addresses and then
8529  * treats them as such, returning true if the v4 address
8530  * associated with this mapped address is configured.
8531  * Note: Applications will have to be careful what they do
8532  * with the response; use of mapped addresses limits
8533  * what can be done with the socket, especially with
8534  * respect to socket options and ioctls - neither IPv4
8535  * options nor IPv6 sticky options/ancillary data options
8536  * may be used.
8537  */
8538 /* ARGSUSED */
8539 int
8540 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8541     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8542 {
8543 	struct sioc_addrreq *sia;
8544 	sin_t *sin;
8545 	ire_t *ire;
8546 	mblk_t *mp1;
8547 	zoneid_t zoneid;
8548 
8549 	ip1dbg(("ip_sioctl_tmyaddr"));
8550 
8551 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8552 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8553 
8554 	/* Existence verified in ip_wput_nondata */
8555 	mp1 = mp->b_cont->b_cont;
8556 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8557 	sin = (sin_t *)&sia->sa_addr;
8558 	switch (sin->sin_family) {
8559 	case AF_INET6: {
8560 		sin6_t *sin6 = (sin6_t *)sin;
8561 
8562 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8563 			ipaddr_t v4_addr;
8564 
8565 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8566 			    v4_addr);
8567 			ire = ire_ctable_lookup(v4_addr, 0,
8568 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8569 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8570 		} else {
8571 			in6_addr_t v6addr;
8572 
8573 			v6addr = sin6->sin6_addr;
8574 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8575 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8576 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8577 		}
8578 		break;
8579 	}
8580 	case AF_INET: {
8581 		ipaddr_t v4addr;
8582 
8583 		v4addr = sin->sin_addr.s_addr;
8584 		ire = ire_ctable_lookup(v4addr, 0,
8585 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8586 		    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8587 		break;
8588 	}
8589 	default:
8590 		return (EAFNOSUPPORT);
8591 	}
8592 	if (ire != NULL) {
8593 		sia->sa_res = 1;
8594 		ire_refrele(ire);
8595 	} else {
8596 		sia->sa_res = 0;
8597 	}
8598 	return (0);
8599 }
8600 
8601 /*
8602  * Check if this is an address assigned on-link i.e. neighbor,
8603  * and makes sure it's reachable from the current zone.
8604  * Returns true for my addresses as well.
8605  * Translates mapped addresses to v4 addresses and then
8606  * treats them as such, returning true if the v4 address
8607  * associated with this mapped address is configured.
8608  * Note: Applications will have to be careful what they do
8609  * with the response; use of mapped addresses limits
8610  * what can be done with the socket, especially with
8611  * respect to socket options and ioctls - neither IPv4
8612  * options nor IPv6 sticky options/ancillary data options
8613  * may be used.
8614  */
8615 /* ARGSUSED */
8616 int
8617 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8618     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8619 {
8620 	struct sioc_addrreq *sia;
8621 	sin_t *sin;
8622 	mblk_t	*mp1;
8623 	ire_t *ire = NULL;
8624 	zoneid_t zoneid;
8625 
8626 	ip1dbg(("ip_sioctl_tonlink"));
8627 
8628 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8629 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8630 
8631 	/* Existence verified in ip_wput_nondata */
8632 	mp1 = mp->b_cont->b_cont;
8633 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8634 	sin = (sin_t *)&sia->sa_addr;
8635 
8636 	/*
8637 	 * Match addresses with a zero gateway field to avoid
8638 	 * routes going through a router.
8639 	 * Exclude broadcast and multicast addresses.
8640 	 */
8641 	switch (sin->sin_family) {
8642 	case AF_INET6: {
8643 		sin6_t *sin6 = (sin6_t *)sin;
8644 
8645 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8646 			ipaddr_t v4_addr;
8647 
8648 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8649 			    v4_addr);
8650 			if (!CLASSD(v4_addr)) {
8651 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8652 				    NULL, NULL, zoneid, MATCH_IRE_GW);
8653 			}
8654 		} else {
8655 			in6_addr_t v6addr;
8656 			in6_addr_t v6gw;
8657 
8658 			v6addr = sin6->sin6_addr;
8659 			v6gw = ipv6_all_zeros;
8660 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8661 				ire = ire_route_lookup_v6(&v6addr, 0,
8662 				    &v6gw, 0, NULL, NULL, zoneid,
8663 				    MATCH_IRE_GW);
8664 			}
8665 		}
8666 		break;
8667 	}
8668 	case AF_INET: {
8669 		ipaddr_t v4addr;
8670 
8671 		v4addr = sin->sin_addr.s_addr;
8672 		if (!CLASSD(v4addr)) {
8673 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8674 			    NULL, NULL, zoneid, MATCH_IRE_GW);
8675 		}
8676 		break;
8677 	}
8678 	default:
8679 		return (EAFNOSUPPORT);
8680 	}
8681 	sia->sa_res = 0;
8682 	if (ire != NULL) {
8683 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8684 		    IRE_LOCAL|IRE_LOOPBACK)) {
8685 			sia->sa_res = 1;
8686 		}
8687 		ire_refrele(ire);
8688 	}
8689 	return (0);
8690 }
8691 
8692 /*
8693  * TBD: implement when kernel maintaines a list of site prefixes.
8694  */
8695 /* ARGSUSED */
8696 int
8697 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8698     ip_ioctl_cmd_t *ipip, void *ifreq)
8699 {
8700 	return (ENXIO);
8701 }
8702 
8703 /* ARGSUSED */
8704 int
8705 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8706     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8707 {
8708 	ill_t  		*ill;
8709 	mblk_t		*mp1;
8710 	conn_t		*connp;
8711 	boolean_t	success;
8712 
8713 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8714 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8715 	/* ioctl comes down on an conn */
8716 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8717 	connp = Q_TO_CONN(q);
8718 
8719 	mp->b_datap->db_type = M_IOCTL;
8720 
8721 	/*
8722 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8723 	 * The original mp contains contaminated b_next values due to 'mi',
8724 	 * which is needed to do the mi_copy_done. Unfortunately if we
8725 	 * send down the original mblk itself and if we are popped due to an
8726 	 * an unplumb before the response comes back from tunnel,
8727 	 * the streamhead (which does a freemsg) will see this contaminated
8728 	 * message and the assertion in freemsg about non-null b_next/b_prev
8729 	 * will panic a DEBUG kernel.
8730 	 */
8731 	mp1 = copymsg(mp);
8732 	if (mp1 == NULL)
8733 		return (ENOMEM);
8734 
8735 	ill = ipif->ipif_ill;
8736 	mutex_enter(&connp->conn_lock);
8737 	mutex_enter(&ill->ill_lock);
8738 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8739 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8740 		    mp, 0);
8741 	} else {
8742 		success = ill_pending_mp_add(ill, connp, mp);
8743 	}
8744 	mutex_exit(&ill->ill_lock);
8745 	mutex_exit(&connp->conn_lock);
8746 
8747 	if (success) {
8748 		ip1dbg(("sending down tunparam request "));
8749 		putnext(ill->ill_wq, mp1);
8750 		return (EINPROGRESS);
8751 	} else {
8752 		/* The conn has started closing */
8753 		freemsg(mp1);
8754 		return (EINTR);
8755 	}
8756 }
8757 
8758 static int
8759 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8760     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8761 {
8762 	mblk_t *mp1;
8763 	mblk_t *mp2;
8764 	mblk_t *pending_mp;
8765 	ipaddr_t ipaddr;
8766 	area_t *area;
8767 	struct iocblk *iocp;
8768 	conn_t *connp;
8769 	struct arpreq *ar;
8770 	struct xarpreq *xar;
8771 	boolean_t success;
8772 	int flags, alength;
8773 	char *lladdr;
8774 
8775 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8776 	connp = Q_TO_CONN(q);
8777 
8778 	iocp = (struct iocblk *)mp->b_rptr;
8779 	/*
8780 	 * ill has already been set depending on whether
8781 	 * bsd style or interface style ioctl.
8782 	 */
8783 	ASSERT(ill != NULL);
8784 
8785 	/*
8786 	 * Is this one of the new SIOC*XARP ioctls?
8787 	 */
8788 	if (x_arp_ioctl) {
8789 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8790 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8791 		ar = NULL;
8792 
8793 		flags = xar->xarp_flags;
8794 		lladdr = LLADDR(&xar->xarp_ha);
8795 		/*
8796 		 * Validate against user's link layer address length
8797 		 * input and name and addr length limits.
8798 		 */
8799 		alength = ill->ill_phys_addr_length;
8800 		if (iocp->ioc_cmd == SIOCSXARP) {
8801 			if (alength != xar->xarp_ha.sdl_alen ||
8802 			    (alength + xar->xarp_ha.sdl_nlen >
8803 			    sizeof (xar->xarp_ha.sdl_data)))
8804 				return (EINVAL);
8805 		}
8806 	} else {
8807 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8808 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8809 		xar = NULL;
8810 
8811 		flags = ar->arp_flags;
8812 		lladdr = ar->arp_ha.sa_data;
8813 		/*
8814 		 * Theoretically, the sa_family could tell us what link
8815 		 * layer type this operation is trying to deal with. By
8816 		 * common usage AF_UNSPEC means ethernet. We'll assume
8817 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8818 		 * for now. Our new SIOC*XARP ioctls can be used more
8819 		 * generally.
8820 		 *
8821 		 * If the underlying media happens to have a non 6 byte
8822 		 * address, arp module will fail set/get, but the del
8823 		 * operation will succeed.
8824 		 */
8825 		alength = 6;
8826 		if ((iocp->ioc_cmd != SIOCDARP) &&
8827 		    (alength != ill->ill_phys_addr_length)) {
8828 			return (EINVAL);
8829 		}
8830 	}
8831 
8832 	/*
8833 	 * We are going to pass up to ARP a packet chain that looks
8834 	 * like:
8835 	 *
8836 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
8837 	 *
8838 	 * Get a copy of the original IOCTL mblk to head the chain,
8839 	 * to be sent up (in mp1). Also get another copy to store
8840 	 * in the ill_pending_mp list, for matching the response
8841 	 * when it comes back from ARP.
8842 	 */
8843 	mp1 = copyb(mp);
8844 	pending_mp = copymsg(mp);
8845 	if (mp1 == NULL || pending_mp == NULL) {
8846 		if (mp1 != NULL)
8847 			freeb(mp1);
8848 		if (pending_mp != NULL)
8849 			inet_freemsg(pending_mp);
8850 		return (ENOMEM);
8851 	}
8852 
8853 	ipaddr = sin->sin_addr.s_addr;
8854 
8855 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
8856 	    (caddr_t)&ipaddr);
8857 	if (mp2 == NULL) {
8858 		freeb(mp1);
8859 		inet_freemsg(pending_mp);
8860 		return (ENOMEM);
8861 	}
8862 	/* Put together the chain. */
8863 	mp1->b_cont = mp2;
8864 	mp1->b_datap->db_type = M_IOCTL;
8865 	mp2->b_cont = mp;
8866 	mp2->b_datap->db_type = M_DATA;
8867 
8868 	iocp = (struct iocblk *)mp1->b_rptr;
8869 
8870 	/*
8871 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
8872 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
8873 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
8874 	 * ioc_count field; set ioc_count to be correct.
8875 	 */
8876 	iocp->ioc_count = MBLKL(mp1->b_cont);
8877 
8878 	/*
8879 	 * Set the proper command in the ARP message.
8880 	 * Convert the SIOC{G|S|D}ARP calls into our
8881 	 * AR_ENTRY_xxx calls.
8882 	 */
8883 	area = (area_t *)mp2->b_rptr;
8884 	switch (iocp->ioc_cmd) {
8885 	case SIOCDARP:
8886 	case SIOCDXARP:
8887 		/*
8888 		 * We defer deleting the corresponding IRE until
8889 		 * we return from arp.
8890 		 */
8891 		area->area_cmd = AR_ENTRY_DELETE;
8892 		area->area_proto_mask_offset = 0;
8893 		break;
8894 	case SIOCGARP:
8895 	case SIOCGXARP:
8896 		area->area_cmd = AR_ENTRY_SQUERY;
8897 		area->area_proto_mask_offset = 0;
8898 		break;
8899 	case SIOCSARP:
8900 	case SIOCSXARP: {
8901 		/*
8902 		 * Delete the corresponding ire to make sure IP will
8903 		 * pick up any change from arp.
8904 		 */
8905 		if (!if_arp_ioctl) {
8906 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
8907 			break;
8908 		} else {
8909 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8910 			if (ipif != NULL) {
8911 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
8912 				ipif_refrele(ipif);
8913 			}
8914 			break;
8915 		}
8916 	}
8917 	}
8918 	iocp->ioc_cmd = area->area_cmd;
8919 
8920 	/*
8921 	 * Before sending 'mp' to ARP, we have to clear the b_next
8922 	 * and b_prev. Otherwise if STREAMS encounters such a message
8923 	 * in freemsg(), (because ARP can close any time) it can cause
8924 	 * a panic. But mi code needs the b_next and b_prev values of
8925 	 * mp->b_cont, to complete the ioctl. So we store it here
8926 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
8927 	 * when the response comes down from ARP.
8928 	 */
8929 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
8930 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
8931 	mp->b_cont->b_next = NULL;
8932 	mp->b_cont->b_prev = NULL;
8933 
8934 	mutex_enter(&connp->conn_lock);
8935 	mutex_enter(&ill->ill_lock);
8936 	/* conn has not yet started closing, hence this can't fail */
8937 	success = ill_pending_mp_add(ill, connp, pending_mp);
8938 	ASSERT(success);
8939 	mutex_exit(&ill->ill_lock);
8940 	mutex_exit(&connp->conn_lock);
8941 
8942 	/*
8943 	 * Fill in the rest of the ARP operation fields.
8944 	 */
8945 	area->area_hw_addr_length = alength;
8946 	bcopy(lladdr,
8947 	    (char *)area + area->area_hw_addr_offset,
8948 	    area->area_hw_addr_length);
8949 	/* Translate the flags. */
8950 	if (flags & ATF_PERM)
8951 		area->area_flags |= ACE_F_PERMANENT;
8952 	if (flags & ATF_PUBL)
8953 		area->area_flags |= ACE_F_PUBLISH;
8954 
8955 	/*
8956 	 * Up to ARP it goes.  The response will come
8957 	 * back in ip_wput as an M_IOCACK message, and
8958 	 * will be handed to ip_sioctl_iocack for
8959 	 * completion.
8960 	 */
8961 	putnext(ill->ill_rq, mp1);
8962 	return (EINPROGRESS);
8963 }
8964 
8965 /* ARGSUSED */
8966 int
8967 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8968     ip_ioctl_cmd_t *ipip, void *ifreq)
8969 {
8970 	struct xarpreq *xar;
8971 	boolean_t isv6;
8972 	mblk_t	*mp1;
8973 	int	err;
8974 	conn_t	*connp;
8975 	int ifnamelen;
8976 	ire_t	*ire = NULL;
8977 	ill_t	*ill = NULL;
8978 	struct sockaddr_in *sin;
8979 	boolean_t if_arp_ioctl = B_FALSE;
8980 
8981 	/* ioctl comes down on an conn */
8982 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8983 	connp = Q_TO_CONN(q);
8984 	isv6 = connp->conn_af_isv6;
8985 
8986 	/* Existance verified in ip_wput_nondata */
8987 	mp1 = mp->b_cont->b_cont;
8988 
8989 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
8990 	xar = (struct xarpreq *)mp1->b_rptr;
8991 	sin = (sin_t *)&xar->xarp_pa;
8992 
8993 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
8994 	    (xar->xarp_pa.ss_family != AF_INET))
8995 		return (ENXIO);
8996 
8997 	ifnamelen = xar->xarp_ha.sdl_nlen;
8998 	if (ifnamelen != 0) {
8999 		char	*cptr, cval;
9000 
9001 		if (ifnamelen >= LIFNAMSIZ)
9002 			return (EINVAL);
9003 
9004 		/*
9005 		 * Instead of bcopying a bunch of bytes,
9006 		 * null-terminate the string in-situ.
9007 		 */
9008 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9009 		cval = *cptr;
9010 		*cptr = '\0';
9011 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9012 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9013 		    &err, NULL);
9014 		*cptr = cval;
9015 		if (ill == NULL)
9016 			return (err);
9017 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9018 			ill_refrele(ill);
9019 			return (ENXIO);
9020 		}
9021 
9022 		if_arp_ioctl = B_TRUE;
9023 	} else {
9024 		/*
9025 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9026 		 * as an extended BSD ioctl. The kernel uses the IP address
9027 		 * to figure out the network interface.
9028 		 */
9029 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9030 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9031 		    ((ill = ire_to_ill(ire)) == NULL)) {
9032 			if (ire != NULL)
9033 				ire_refrele(ire);
9034 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9035 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9036 			    MATCH_IRE_TYPE);
9037 			if ((ire == NULL) ||
9038 			    ((ill = ire_to_ill(ire)) == NULL)) {
9039 				if (ire != NULL)
9040 					ire_refrele(ire);
9041 				return (ENXIO);
9042 			}
9043 		}
9044 		ASSERT(ire != NULL && ill != NULL);
9045 	}
9046 
9047 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9048 	if (if_arp_ioctl)
9049 		ill_refrele(ill);
9050 	if (ire != NULL)
9051 		ire_refrele(ire);
9052 
9053 	return (err);
9054 }
9055 
9056 /*
9057  * ARP IOCTLs.
9058  * How does IP get in the business of fronting ARP configuration/queries?
9059  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9060  * are by tradition passed in through a datagram socket.  That lands in IP.
9061  * As it happens, this is just as well since the interface is quite crude in
9062  * that it passes in no information about protocol or hardware types, or
9063  * interface association.  After making the protocol assumption, IP is in
9064  * the position to look up the name of the ILL, which ARP will need, and
9065  * format a request that can be handled by ARP.	 The request is passed up
9066  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9067  * back a response.  ARP supports its own set of more general IOCTLs, in
9068  * case anyone is interested.
9069  */
9070 /* ARGSUSED */
9071 int
9072 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9073     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9074 {
9075 	struct arpreq *ar;
9076 	struct sockaddr_in *sin;
9077 	ire_t	*ire;
9078 	boolean_t isv6;
9079 	mblk_t	*mp1;
9080 	int	err;
9081 	conn_t	*connp;
9082 	ill_t	*ill;
9083 
9084 	/* ioctl comes down on an conn */
9085 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9086 	connp = Q_TO_CONN(q);
9087 	isv6 = connp->conn_af_isv6;
9088 	if (isv6)
9089 		return (ENXIO);
9090 
9091 	/* Existance verified in ip_wput_nondata */
9092 	mp1 = mp->b_cont->b_cont;
9093 
9094 	ar = (struct arpreq *)mp1->b_rptr;
9095 	sin = (sin_t *)&ar->arp_pa;
9096 
9097 	/*
9098 	 * We need to let ARP know on which interface the IP
9099 	 * address has an ARP mapping. In the IPMP case, a
9100 	 * simple forwarding table lookup will return the
9101 	 * IRE_IF_RESOLVER for the first interface in the group,
9102 	 * which might not be the interface on which the
9103 	 * requested IP address was resolved due to the ill
9104 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9105 	 * So we do a cache table lookup first: if the IRE cache
9106 	 * entry for the IP address is still there, it will
9107 	 * contain the ill pointer for the right interface, so
9108 	 * we use that. If the cache entry has been flushed, we
9109 	 * fall back to the forwarding table lookup. This should
9110 	 * be rare enough since IRE cache entries have a longer
9111 	 * life expectancy than ARP cache entries.
9112 	 */
9113 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9114 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9115 	    ((ill = ire_to_ill(ire)) == NULL)) {
9116 		if (ire != NULL)
9117 			ire_refrele(ire);
9118 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9119 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9120 		    MATCH_IRE_TYPE);
9121 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9122 			if (ire != NULL)
9123 				ire_refrele(ire);
9124 			return (ENXIO);
9125 		}
9126 	}
9127 	ASSERT(ire != NULL && ill != NULL);
9128 
9129 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9130 	ire_refrele(ire);
9131 	return (err);
9132 }
9133 
9134 /*
9135  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9136  * atomically set/clear the muxids. Also complete the ioctl by acking or
9137  * naking it.  Note that the code is structured such that the link type,
9138  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9139  * its clones use the persistent link, while pppd(1M) and perhaps many
9140  * other daemons may use non-persistent link.  When combined with some
9141  * ill_t states, linking and unlinking lower streams may be used as
9142  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9143  */
9144 /* ARGSUSED */
9145 void
9146 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9147 {
9148 	mblk_t *mp1;
9149 	mblk_t *mp2;
9150 	struct linkblk *li;
9151 	queue_t	*ipwq;
9152 	char	*name;
9153 	struct qinit *qinfo;
9154 	struct ipmx_s *ipmxp;
9155 	ill_t	*ill = NULL;
9156 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9157 	int	err = 0;
9158 	boolean_t	entered_ipsq = B_FALSE;
9159 	boolean_t islink;
9160 	queue_t *dwq = NULL;
9161 
9162 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9163 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9164 
9165 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9166 	    B_TRUE : B_FALSE;
9167 
9168 	mp1 = mp->b_cont;	/* This is the linkblk info */
9169 	li = (struct linkblk *)mp1->b_rptr;
9170 
9171 	/*
9172 	 * ARP has added this special mblk, and the utility is asking us
9173 	 * to perform consistency checks, and also atomically set the
9174 	 * muxid. Ifconfig is an example.  It achieves this by using
9175 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9176 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9177 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9178 	 * and other comments in this routine for more details.
9179 	 */
9180 	mp2 = mp1->b_cont;	/* This is added by ARP */
9181 
9182 	/*
9183 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9184 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9185 	 * get the special mblk above.  For backward compatibility, we just
9186 	 * return success.  The utility will use SIOCSLIFMUXID to store
9187 	 * the muxids.  This is not atomic, and can leave the streams
9188 	 * unplumbable if the utility is interrrupted, before it does the
9189 	 * SIOCSLIFMUXID.
9190 	 */
9191 	if (mp2 == NULL) {
9192 		/*
9193 		 * At this point we don't know whether or not this is the
9194 		 * IP module stream or the ARP device stream.  We need to
9195 		 * walk the lower stream in order to find this out, since
9196 		 * the capability negotiation is done only on the IP module
9197 		 * stream.  IP module instance is identified by the module
9198 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9199 		 * STREAMS ensures that the lower stream (l_qbot) will not
9200 		 * vanish until this ioctl completes. So we can safely walk
9201 		 * the stream or refer to the q_ptr.
9202 		 */
9203 		ipwq = li->l_qbot;
9204 		while (ipwq != NULL) {
9205 			qinfo = ipwq->q_qinfo;
9206 			name = qinfo->qi_minfo->mi_idname;
9207 			if (name != NULL && name[0] != NULL &&
9208 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9209 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9210 			    (ipwq->q_next != NULL)) {
9211 				break;
9212 			}
9213 			ipwq = ipwq->q_next;
9214 		}
9215 		/*
9216 		 * This looks like an IP module stream, so trigger
9217 		 * the capability reset or re-negotiation if necessary.
9218 		 */
9219 		if (ipwq != NULL) {
9220 			ill = ipwq->q_ptr;
9221 			ASSERT(ill != NULL);
9222 
9223 			if (ipsq == NULL) {
9224 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9225 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9226 				if (ipsq == NULL)
9227 					return;
9228 				entered_ipsq = B_TRUE;
9229 			}
9230 			ASSERT(IAM_WRITER_ILL(ill));
9231 			/*
9232 			 * Store the upper read queue of the module
9233 			 * immediately below IP, and count the total
9234 			 * number of lower modules.  Do this only
9235 			 * for I_PLINK or I_LINK event.
9236 			 */
9237 			ill->ill_lmod_rq = NULL;
9238 			ill->ill_lmod_cnt = 0;
9239 			if (islink && (dwq = ipwq->q_next) != NULL) {
9240 				ill->ill_lmod_rq = RD(dwq);
9241 
9242 				while (dwq != NULL) {
9243 					ill->ill_lmod_cnt++;
9244 					dwq = dwq->q_next;
9245 				}
9246 			}
9247 			/*
9248 			 * There's no point in resetting or re-negotiating if
9249 			 * we are not bound to the driver, so only do this if
9250 			 * the DLPI state is idle (up); we assume such state
9251 			 * since ill_ipif_up_count gets incremented in
9252 			 * ipif_up_done(), which is after we are bound to the
9253 			 * driver.  Note that in the case of logical
9254 			 * interfaces, IP won't rebind to the driver unless
9255 			 * the ill_ipif_up_count is 0, meaning that all other
9256 			 * IP interfaces (including the main ipif) are in the
9257 			 * down state.  Because of this, we use such counter
9258 			 * as an indicator, instead of relying on the IPIF_UP
9259 			 * flag, which is per ipif instance.
9260 			 */
9261 			if (ill->ill_ipif_up_count > 0) {
9262 				if (islink)
9263 					ill_capability_probe(ill);
9264 				else
9265 					ill_capability_reset(ill);
9266 			}
9267 		}
9268 		goto done;
9269 	}
9270 
9271 	/*
9272 	 * This is an I_{P}LINK sent down by ifconfig on
9273 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9274 	 * giving more info. STREAMS ensures that the lower
9275 	 * stream (l_qbot) will not vanish until this ioctl
9276 	 * completes. So we can safely walk the stream or refer
9277 	 * to the q_ptr.
9278 	 */
9279 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9280 	if (ipmxp->ipmx_arpdev_stream) {
9281 		/*
9282 		 * The operation is occuring on the arp-device
9283 		 * stream.
9284 		 */
9285 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9286 		    q, mp, ip_sioctl_plink, &err, NULL);
9287 		if (ill == NULL) {
9288 			if (err == EINPROGRESS) {
9289 				return;
9290 			} else {
9291 				err = EINVAL;
9292 				goto done;
9293 			}
9294 		}
9295 
9296 		if (ipsq == NULL) {
9297 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9298 			    NEW_OP, B_TRUE);
9299 			if (ipsq == NULL) {
9300 				ill_refrele(ill);
9301 				return;
9302 			}
9303 			entered_ipsq = B_TRUE;
9304 		}
9305 		ASSERT(IAM_WRITER_ILL(ill));
9306 		ill_refrele(ill);
9307 		/*
9308 		 * To ensure consistency between IP and ARP,
9309 		 * the following LIFO scheme is used in
9310 		 * plink/punlink. (IP first, ARP last).
9311 		 * This is because the muxid's are stored
9312 		 * in the IP stream on the ill.
9313 		 *
9314 		 * I_{P}LINK: ifconfig plinks the IP stream before
9315 		 * plinking the ARP stream. On an arp-dev
9316 		 * stream, IP checks that it is not yet
9317 		 * plinked, and it also checks that the
9318 		 * corresponding IP stream is already plinked.
9319 		 *
9320 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9321 		 * before punlinking the IP stream. IP does
9322 		 * not allow punlink of the IP stream unless
9323 		 * the arp stream has been punlinked.
9324 		 *
9325 		 */
9326 		if ((islink &&
9327 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9328 		    (!islink &&
9329 		    ill->ill_arp_muxid != li->l_index)) {
9330 			err = EINVAL;
9331 			goto done;
9332 		}
9333 		if (islink) {
9334 			ill->ill_arp_muxid = li->l_index;
9335 		} else {
9336 			ill->ill_arp_muxid = 0;
9337 		}
9338 	} else {
9339 		/*
9340 		 * This must be the IP module stream with or
9341 		 * without arp. Walk the stream and locate the
9342 		 * IP module. An IP module instance is
9343 		 * identified by the module name IP, non-null
9344 		 * q_next, and it's wput not being ip_lwput.
9345 		 */
9346 		ipwq = li->l_qbot;
9347 		while (ipwq != NULL) {
9348 			qinfo = ipwq->q_qinfo;
9349 			name = qinfo->qi_minfo->mi_idname;
9350 			if (name != NULL && name[0] != NULL &&
9351 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9352 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9353 			    (ipwq->q_next != NULL)) {
9354 				break;
9355 			}
9356 			ipwq = ipwq->q_next;
9357 		}
9358 		if (ipwq != NULL) {
9359 			ill = ipwq->q_ptr;
9360 			ASSERT(ill != NULL);
9361 
9362 			if (ipsq == NULL) {
9363 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9364 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9365 				if (ipsq == NULL)
9366 					return;
9367 				entered_ipsq = B_TRUE;
9368 			}
9369 			ASSERT(IAM_WRITER_ILL(ill));
9370 			/*
9371 			 * Return error if the ip_mux_id is
9372 			 * non-zero and command is I_{P}LINK.
9373 			 * If command is I_{P}UNLINK, return
9374 			 * error if the arp-devstr is not
9375 			 * yet punlinked.
9376 			 */
9377 			if ((islink && ill->ill_ip_muxid != 0) ||
9378 			    (!islink && ill->ill_arp_muxid != 0)) {
9379 				err = EINVAL;
9380 				goto done;
9381 			}
9382 			ill->ill_lmod_rq = NULL;
9383 			ill->ill_lmod_cnt = 0;
9384 			if (islink) {
9385 				/*
9386 				 * Store the upper read queue of the module
9387 				 * immediately below IP, and count the total
9388 				 * number of lower modules.
9389 				 */
9390 				if ((dwq = ipwq->q_next) != NULL) {
9391 					ill->ill_lmod_rq = RD(dwq);
9392 
9393 					while (dwq != NULL) {
9394 						ill->ill_lmod_cnt++;
9395 						dwq = dwq->q_next;
9396 					}
9397 				}
9398 				ill->ill_ip_muxid = li->l_index;
9399 			} else {
9400 				ill->ill_ip_muxid = 0;
9401 			}
9402 
9403 			/*
9404 			 * See comments above about resetting/re-
9405 			 * negotiating driver sub-capabilities.
9406 			 */
9407 			if (ill->ill_ipif_up_count > 0) {
9408 				if (islink)
9409 					ill_capability_probe(ill);
9410 				else
9411 					ill_capability_reset(ill);
9412 			}
9413 		}
9414 	}
9415 done:
9416 	iocp->ioc_count = 0;
9417 	iocp->ioc_error = err;
9418 	if (err == 0)
9419 		mp->b_datap->db_type = M_IOCACK;
9420 	else
9421 		mp->b_datap->db_type = M_IOCNAK;
9422 	qreply(q, mp);
9423 
9424 	/* Conn was refheld in ip_sioctl_copyin_setup */
9425 	if (CONN_Q(q))
9426 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9427 	if (entered_ipsq)
9428 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9429 }
9430 
9431 /*
9432  * Search the ioctl command in the ioctl tables and return a pointer
9433  * to the ioctl command information. The ioctl command tables are
9434  * static and fully populated at compile time.
9435  */
9436 ip_ioctl_cmd_t *
9437 ip_sioctl_lookup(int ioc_cmd)
9438 {
9439 	int index;
9440 	ip_ioctl_cmd_t *ipip;
9441 	ip_ioctl_cmd_t *ipip_end;
9442 
9443 	if (ioc_cmd == IPI_DONTCARE)
9444 		return (NULL);
9445 
9446 	/*
9447 	 * Do a 2 step search. First search the indexed table
9448 	 * based on the least significant byte of the ioctl cmd.
9449 	 * If we don't find a match, then search the misc table
9450 	 * serially.
9451 	 */
9452 	index = ioc_cmd & 0xFF;
9453 	if (index < ip_ndx_ioctl_count) {
9454 		ipip = &ip_ndx_ioctl_table[index];
9455 		if (ipip->ipi_cmd == ioc_cmd) {
9456 			/* Found a match in the ndx table */
9457 			return (ipip);
9458 		}
9459 	}
9460 
9461 	/* Search the misc table */
9462 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9463 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9464 		if (ipip->ipi_cmd == ioc_cmd)
9465 			/* Found a match in the misc table */
9466 			return (ipip);
9467 	}
9468 
9469 	return (NULL);
9470 }
9471 
9472 /*
9473  * Wrapper function for resuming deferred ioctl processing
9474  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9475  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9476  */
9477 /* ARGSUSED */
9478 void
9479 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9480     void *dummy_arg)
9481 {
9482 	ip_sioctl_copyin_setup(q, mp);
9483 }
9484 
9485 /*
9486  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9487  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9488  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9489  * We establish here the size of the block to be copied in.  mi_copyin
9490  * arranges for this to happen, an processing continues in ip_wput with
9491  * an M_IOCDATA message.
9492  */
9493 void
9494 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9495 {
9496 	int	copyin_size;
9497 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9498 	ip_ioctl_cmd_t *ipip;
9499 	cred_t *cr;
9500 
9501 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9502 	if (ipip == NULL) {
9503 		/*
9504 		 * The ioctl is not one we understand or own.
9505 		 * Pass it along to be processed down stream,
9506 		 * if this is a module instance of IP, else nak
9507 		 * the ioctl.
9508 		 */
9509 		if (q->q_next == NULL) {
9510 			goto nak;
9511 		} else {
9512 			putnext(q, mp);
9513 			return;
9514 		}
9515 	}
9516 
9517 	/*
9518 	 * If this is deferred, then we will do all the checks when we
9519 	 * come back.
9520 	 */
9521 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9522 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9523 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9524 		return;
9525 	}
9526 
9527 	/*
9528 	 * Only allow a very small subset of IP ioctls on this stream if
9529 	 * IP is a module and not a driver. Allowing ioctls to be processed
9530 	 * in this case may cause assert failures or data corruption.
9531 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9532 	 * ioctls allowed on an IP module stream, after which this stream
9533 	 * normally becomes a multiplexor (at which time the stream head
9534 	 * will fail all ioctls).
9535 	 */
9536 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9537 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9538 			/*
9539 			 * Pass common Streams ioctls which the IP
9540 			 * module does not own or consume along to
9541 			 * be processed down stream.
9542 			 */
9543 			putnext(q, mp);
9544 			return;
9545 		} else {
9546 			goto nak;
9547 		}
9548 	}
9549 
9550 	/* Make sure we have ioctl data to process. */
9551 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9552 		goto nak;
9553 
9554 	/*
9555 	 * Prefer dblk credential over ioctl credential; some synthesized
9556 	 * ioctls have kcred set because there's no way to crhold()
9557 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9558 	 * the framework; the caller of ioctl needs to hold the reference
9559 	 * for the duration of the call).
9560 	 */
9561 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9562 
9563 	/* Make sure normal users don't send down privileged ioctls */
9564 	if ((ipip->ipi_flags & IPI_PRIV) &&
9565 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9566 		/* We checked the privilege earlier but log it here */
9567 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9568 		return;
9569 	}
9570 
9571 	/*
9572 	 * The ioctl command tables can only encode fixed length
9573 	 * ioctl data. If the length is variable, the table will
9574 	 * encode the length as zero. Such special cases are handled
9575 	 * below in the switch.
9576 	 */
9577 	if (ipip->ipi_copyin_size != 0) {
9578 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9579 		return;
9580 	}
9581 
9582 	switch (iocp->ioc_cmd) {
9583 	case O_SIOCGIFCONF:
9584 	case SIOCGIFCONF:
9585 		/*
9586 		 * This IOCTL is hilarious.  See comments in
9587 		 * ip_sioctl_get_ifconf for the story.
9588 		 */
9589 		if (iocp->ioc_count == TRANSPARENT)
9590 			copyin_size = SIZEOF_STRUCT(ifconf,
9591 			    iocp->ioc_flag);
9592 		else
9593 			copyin_size = iocp->ioc_count;
9594 		mi_copyin(q, mp, NULL, copyin_size);
9595 		return;
9596 
9597 	case O_SIOCGLIFCONF:
9598 	case SIOCGLIFCONF:
9599 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9600 		mi_copyin(q, mp, NULL, copyin_size);
9601 		return;
9602 
9603 	case SIOCGLIFSRCOF:
9604 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9605 		mi_copyin(q, mp, NULL, copyin_size);
9606 		return;
9607 	case SIOCGIP6ADDRPOLICY:
9608 		ip_sioctl_ip6addrpolicy(q, mp);
9609 		ip6_asp_table_refrele();
9610 		return;
9611 
9612 	case SIOCSIP6ADDRPOLICY:
9613 		ip_sioctl_ip6addrpolicy(q, mp);
9614 		return;
9615 
9616 	case SIOCGDSTINFO:
9617 		ip_sioctl_dstinfo(q, mp);
9618 		ip6_asp_table_refrele();
9619 		return;
9620 
9621 	case I_PLINK:
9622 	case I_PUNLINK:
9623 	case I_LINK:
9624 	case I_UNLINK:
9625 		/*
9626 		 * We treat non-persistent link similarly as the persistent
9627 		 * link case, in terms of plumbing/unplumbing, as well as
9628 		 * dynamic re-plumbing events indicator.  See comments
9629 		 * in ip_sioctl_plink() for more.
9630 		 *
9631 		 * Request can be enqueued in the 'ipsq' while waiting
9632 		 * to become exclusive. So bump up the conn ref.
9633 		 */
9634 		if (CONN_Q(q))
9635 			CONN_INC_REF(Q_TO_CONN(q));
9636 		ip_sioctl_plink(NULL, q, mp, NULL);
9637 		return;
9638 
9639 	case ND_GET:
9640 	case ND_SET:
9641 		/*
9642 		 * Use of the nd table requires holding the reader lock.
9643 		 * Modifying the nd table thru nd_load/nd_unload requires
9644 		 * the writer lock.
9645 		 */
9646 		rw_enter(&ip_g_nd_lock, RW_READER);
9647 		if (nd_getset(q, ip_g_nd, mp)) {
9648 			rw_exit(&ip_g_nd_lock);
9649 
9650 			if (iocp->ioc_error)
9651 				iocp->ioc_count = 0;
9652 			mp->b_datap->db_type = M_IOCACK;
9653 			qreply(q, mp);
9654 			return;
9655 		}
9656 		rw_exit(&ip_g_nd_lock);
9657 		/*
9658 		 * We don't understand this subioctl of ND_GET / ND_SET.
9659 		 * Maybe intended for some driver / module below us
9660 		 */
9661 		if (q->q_next) {
9662 			putnext(q, mp);
9663 		} else {
9664 			iocp->ioc_error = ENOENT;
9665 			mp->b_datap->db_type = M_IOCNAK;
9666 			iocp->ioc_count = 0;
9667 			qreply(q, mp);
9668 		}
9669 		return;
9670 
9671 	case IP_IOCTL:
9672 		ip_wput_ioctl(q, mp);
9673 		return;
9674 	default:
9675 		cmn_err(CE_PANIC, "should not happen ");
9676 	}
9677 nak:
9678 	if (mp->b_cont != NULL) {
9679 		freemsg(mp->b_cont);
9680 		mp->b_cont = NULL;
9681 	}
9682 	iocp->ioc_error = EINVAL;
9683 	mp->b_datap->db_type = M_IOCNAK;
9684 	iocp->ioc_count = 0;
9685 	qreply(q, mp);
9686 }
9687 
9688 /* ip_wput hands off ARP IOCTL responses to us */
9689 void
9690 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9691 {
9692 	struct arpreq *ar;
9693 	struct xarpreq *xar;
9694 	area_t	*area;
9695 	mblk_t	*area_mp;
9696 	struct iocblk *iocp;
9697 	mblk_t	*orig_ioc_mp, *tmp;
9698 	struct iocblk	*orig_iocp;
9699 	ill_t *ill;
9700 	conn_t *connp = NULL;
9701 	uint_t ioc_id;
9702 	mblk_t *pending_mp;
9703 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9704 	int *flagsp;
9705 	char *storage = NULL;
9706 	sin_t *sin;
9707 	ipaddr_t addr;
9708 	int err;
9709 
9710 	ill = q->q_ptr;
9711 	ASSERT(ill != NULL);
9712 
9713 	/*
9714 	 * We should get back from ARP a packet chain that looks like:
9715 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9716 	 */
9717 	if (!(area_mp = mp->b_cont) ||
9718 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9719 	    !(orig_ioc_mp = area_mp->b_cont) ||
9720 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9721 		freemsg(mp);
9722 		return;
9723 	}
9724 
9725 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9726 
9727 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9728 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9729 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9730 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9731 		x_arp_ioctl = B_TRUE;
9732 		xar = (struct xarpreq *)tmp->b_rptr;
9733 		sin = (sin_t *)&xar->xarp_pa;
9734 		flagsp = &xar->xarp_flags;
9735 		storage = xar->xarp_ha.sdl_data;
9736 		if (xar->xarp_ha.sdl_nlen != 0)
9737 			ifx_arp_ioctl = B_TRUE;
9738 	} else {
9739 		ar = (struct arpreq *)tmp->b_rptr;
9740 		sin = (sin_t *)&ar->arp_pa;
9741 		flagsp = &ar->arp_flags;
9742 		storage = ar->arp_ha.sa_data;
9743 	}
9744 
9745 	iocp = (struct iocblk *)mp->b_rptr;
9746 
9747 	/*
9748 	 * Pick out the originating queue based on the ioc_id.
9749 	 */
9750 	ioc_id = iocp->ioc_id;
9751 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9752 	if (pending_mp == NULL) {
9753 		ASSERT(connp == NULL);
9754 		inet_freemsg(mp);
9755 		return;
9756 	}
9757 	ASSERT(connp != NULL);
9758 	q = CONNP_TO_WQ(connp);
9759 
9760 	/* Uncouple the internally generated IOCTL from the original one */
9761 	area = (area_t *)area_mp->b_rptr;
9762 	area_mp->b_cont = NULL;
9763 
9764 	/*
9765 	 * Restore the b_next and b_prev used by mi code. This is needed
9766 	 * to complete the ioctl using mi* functions. We stored them in
9767 	 * the pending mp prior to sending the request to ARP.
9768 	 */
9769 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9770 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9771 	inet_freemsg(pending_mp);
9772 
9773 	/*
9774 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9775 	 * Catch the case where there is an IRE_CACHE by no entry in the
9776 	 * arp table.
9777 	 */
9778 	addr = sin->sin_addr.s_addr;
9779 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9780 		ire_t			*ire;
9781 		dl_unitdata_req_t	*dlup;
9782 		mblk_t			*llmp;
9783 		int			addr_len;
9784 		ill_t			*ipsqill = NULL;
9785 
9786 		if (ifx_arp_ioctl) {
9787 			/*
9788 			 * There's no need to lookup the ill, since
9789 			 * we've already done that when we started
9790 			 * processing the ioctl and sent the message
9791 			 * to ARP on that ill.  So use the ill that
9792 			 * is stored in q->q_ptr.
9793 			 */
9794 			ipsqill = ill;
9795 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9796 			    ipsqill->ill_ipif, ALL_ZONES,
9797 			    MATCH_IRE_TYPE | MATCH_IRE_ILL);
9798 		} else {
9799 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9800 			    NULL, ALL_ZONES, MATCH_IRE_TYPE);
9801 			if (ire != NULL)
9802 				ipsqill = ire_to_ill(ire);
9803 		}
9804 
9805 		if ((x_arp_ioctl) && (ipsqill != NULL))
9806 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9807 
9808 		if (ire != NULL) {
9809 			*flagsp = ATF_INUSE;
9810 			llmp = ire->ire_dlureq_mp;
9811 			if (llmp != NULL && ipsqill != NULL) {
9812 				uchar_t *macaddr;
9813 
9814 				addr_len = ipsqill->ill_phys_addr_length;
9815 				if (x_arp_ioctl && ((addr_len +
9816 				    ipsqill->ill_name_length) >
9817 				    sizeof (xar->xarp_ha.sdl_data))) {
9818 					ire_refrele(ire);
9819 					freemsg(mp);
9820 					ip_ioctl_finish(q, orig_ioc_mp,
9821 					    EINVAL, NO_COPYOUT, NULL, NULL);
9822 					return;
9823 				}
9824 				*flagsp |= ATF_COM;
9825 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
9826 				if (ipsqill->ill_sap_length < 0)
9827 					macaddr = llmp->b_rptr +
9828 					    dlup->dl_dest_addr_offset;
9829 				else
9830 					macaddr = llmp->b_rptr +
9831 					    dlup->dl_dest_addr_offset +
9832 					    ipsqill->ill_sap_length;
9833 				/*
9834 				 * For SIOCGARP, MAC address length
9835 				 * validation has already been done
9836 				 * before the ioctl was issued to ARP to
9837 				 * allow it to progress only on 6 byte
9838 				 * addressable (ethernet like) media. Thus
9839 				 * the mac address copying can not overwrite
9840 				 * the sa_data area below.
9841 				 */
9842 				bcopy(macaddr, storage, addr_len);
9843 			}
9844 			/* Ditch the internal IOCTL. */
9845 			freemsg(mp);
9846 			ire_refrele(ire);
9847 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9848 			return;
9849 		}
9850 	}
9851 
9852 	/*
9853 	 * Delete the coresponding IRE_CACHE if any.
9854 	 * Reset the error if there was one (in case there was no entry
9855 	 * in arp.)
9856 	 */
9857 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
9858 		ipif_t *ipintf = NULL;
9859 
9860 		if (ifx_arp_ioctl) {
9861 			/*
9862 			 * There's no need to lookup the ill, since
9863 			 * we've already done that when we started
9864 			 * processing the ioctl and sent the message
9865 			 * to ARP on that ill.  So use the ill that
9866 			 * is stored in q->q_ptr.
9867 			 */
9868 			ipintf = ill->ill_ipif;
9869 		}
9870 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
9871 			/*
9872 			 * The address in "addr" may be an entry for a
9873 			 * router. If that's true, then any off-net
9874 			 * IRE_CACHE entries that go through the router
9875 			 * with address "addr" must be clobbered. Use
9876 			 * ire_walk to achieve this goal.
9877 			 */
9878 			if (ifx_arp_ioctl)
9879 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
9880 				    ire_delete_cache_gw, (char *)&addr, ill);
9881 			else
9882 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
9883 				    ALL_ZONES);
9884 			iocp->ioc_error = 0;
9885 		}
9886 	}
9887 
9888 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
9889 		err = iocp->ioc_error;
9890 		freemsg(mp);
9891 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
9892 		return;
9893 	}
9894 
9895 	/*
9896 	 * Completion of an SIOCG{X}ARP.  Translate the information from
9897 	 * the area_t into the struct {x}arpreq.
9898 	 */
9899 	if (x_arp_ioctl) {
9900 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9901 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9902 		    sizeof (xar->xarp_ha.sdl_data)) {
9903 			freemsg(mp);
9904 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
9905 			    NO_COPYOUT, NULL, NULL);
9906 			return;
9907 		}
9908 	}
9909 	*flagsp = ATF_INUSE;
9910 	if (area->area_flags & ACE_F_PERMANENT)
9911 		*flagsp |= ATF_PERM;
9912 	if (area->area_flags & ACE_F_PUBLISH)
9913 		*flagsp |= ATF_PUBL;
9914 	if (area->area_hw_addr_length != 0) {
9915 		*flagsp |= ATF_COM;
9916 		/*
9917 		 * For SIOCGARP, MAC address length validation has
9918 		 * already been done before the ioctl was issued to ARP
9919 		 * to allow it to progress only on 6 byte addressable
9920 		 * (ethernet like) media. Thus the mac address copying
9921 		 * can not overwrite the sa_data area below.
9922 		 */
9923 		bcopy((char *)area + area->area_hw_addr_offset,
9924 		    storage, area->area_hw_addr_length);
9925 	}
9926 
9927 	/* Ditch the internal IOCTL. */
9928 	freemsg(mp);
9929 	/* Complete the original. */
9930 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9931 }
9932 
9933 /*
9934  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9935  * interface) create the next available logical interface for this
9936  * physical interface.
9937  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9938  * ipif with the specified name.
9939  *
9940  * If the address family is not AF_UNSPEC then set the address as well.
9941  *
9942  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9943  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9944  *
9945  * Executed as a writer on the ill or ill group.
9946  * So no lock is needed to traverse the ipif chain, or examine the
9947  * phyint flags.
9948  */
9949 /* ARGSUSED */
9950 int
9951 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9952     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9953 {
9954 	mblk_t	*mp1;
9955 	struct lifreq *lifr;
9956 	boolean_t	isv6;
9957 	boolean_t	exists;
9958 	char 	*name;
9959 	char	*endp;
9960 	char	*cp;
9961 	int	namelen;
9962 	ipif_t	*ipif;
9963 	long	id;
9964 	ipsq_t	*ipsq;
9965 	ill_t	*ill;
9966 	sin_t	*sin;
9967 	int	err = 0;
9968 	boolean_t found_sep = B_FALSE;
9969 	conn_t	*connp;
9970 	zoneid_t zoneid;
9971 	int	orig_ifindex = 0;
9972 
9973 	ip1dbg(("ip_sioctl_addif\n"));
9974 	/* Existence of mp1 has been checked in ip_wput_nondata */
9975 	mp1 = mp->b_cont->b_cont;
9976 	/*
9977 	 * Null terminate the string to protect against buffer
9978 	 * overrun. String was generated by user code and may not
9979 	 * be trusted.
9980 	 */
9981 	lifr = (struct lifreq *)mp1->b_rptr;
9982 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9983 	name = lifr->lifr_name;
9984 	ASSERT(CONN_Q(q));
9985 	connp = Q_TO_CONN(q);
9986 	isv6 = connp->conn_af_isv6;
9987 	zoneid = connp->conn_zoneid;
9988 	namelen = mi_strlen(name);
9989 	if (namelen == 0)
9990 		return (EINVAL);
9991 
9992 	exists = B_FALSE;
9993 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9994 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9995 		/*
9996 		 * Allow creating lo0 using SIOCLIFADDIF.
9997 		 * can't be any other writer thread. So can pass null below
9998 		 * for the last 4 args to ipif_lookup_name.
9999 		 */
10000 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10001 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10002 		/* Prevent any further action */
10003 		if (ipif == NULL) {
10004 			return (ENOBUFS);
10005 		} else if (!exists) {
10006 			/* We created the ipif now and as writer */
10007 			ipif_refrele(ipif);
10008 			return (0);
10009 		} else {
10010 			ill = ipif->ipif_ill;
10011 			ill_refhold(ill);
10012 			ipif_refrele(ipif);
10013 		}
10014 	} else {
10015 		/* Look for a colon in the name. */
10016 		endp = &name[namelen];
10017 		for (cp = endp; --cp > name; ) {
10018 			if (*cp == IPIF_SEPARATOR_CHAR) {
10019 				found_sep = B_TRUE;
10020 				/*
10021 				 * Reject any non-decimal aliases for plumbing
10022 				 * of logical interfaces. Aliases with leading
10023 				 * zeroes are also rejected as they introduce
10024 				 * ambiguity in the naming of the interfaces.
10025 				 * Comparing with "0" takes care of all such
10026 				 * cases.
10027 				 */
10028 				if ((strncmp("0", cp+1, 1)) == 0)
10029 					return (EINVAL);
10030 
10031 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10032 				    id <= 0 || *endp != '\0') {
10033 					return (EINVAL);
10034 				}
10035 				*cp = '\0';
10036 				break;
10037 			}
10038 		}
10039 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10040 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10041 		if (found_sep)
10042 			*cp = IPIF_SEPARATOR_CHAR;
10043 		if (ill == NULL)
10044 			return (err);
10045 	}
10046 
10047 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10048 	    B_TRUE);
10049 
10050 	/*
10051 	 * Release the refhold due to the lookup, now that we are excl
10052 	 * or we are just returning
10053 	 */
10054 	ill_refrele(ill);
10055 
10056 	if (ipsq == NULL)
10057 		return (EINPROGRESS);
10058 
10059 	/*
10060 	 * If the interface is failed, inactive or offlined, look for a working
10061 	 * interface in the ill group and create the ipif there. If we can't
10062 	 * find a good interface, create the ipif anyway so that in.mpathd can
10063 	 * move it to the first repaired interface.
10064 	 */
10065 	if ((ill->ill_phyint->phyint_flags &
10066 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10067 	    ill->ill_phyint->phyint_groupname_len != 0) {
10068 		phyint_t *phyi;
10069 		char *groupname = ill->ill_phyint->phyint_groupname;
10070 
10071 		/*
10072 		 * We're looking for a working interface, but it doesn't matter
10073 		 * if it's up or down; so instead of following the group lists,
10074 		 * we look at each physical interface and compare the groupname.
10075 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10076 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10077 		 * Otherwise we create the ipif on the failed interface.
10078 		 */
10079 		rw_enter(&ill_g_lock, RW_READER);
10080 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10081 		for (; phyi != NULL;
10082 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10083 		    phyi, AVL_AFTER)) {
10084 			if (phyi->phyint_groupname_len == 0)
10085 				continue;
10086 			ASSERT(phyi->phyint_groupname != NULL);
10087 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10088 			    !(phyi->phyint_flags &
10089 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10090 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10091 			    (phyi->phyint_illv4 != NULL))) {
10092 				break;
10093 			}
10094 		}
10095 		rw_exit(&ill_g_lock);
10096 
10097 		if (phyi != NULL) {
10098 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10099 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10100 			    phyi->phyint_illv4);
10101 		}
10102 	}
10103 
10104 	/*
10105 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10106 	 * before or after us.
10107 	 */
10108 	ASSERT(IAM_WRITER_ILL(ill));
10109 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10110 
10111 	if (found_sep && orig_ifindex == 0) {
10112 		/* Now see if there is an IPIF with this unit number. */
10113 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10114 			if (ipif->ipif_id == id) {
10115 				err = EEXIST;
10116 				goto done;
10117 			}
10118 		}
10119 	}
10120 
10121 	/*
10122 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10123 	 * of lo0. We never come here when we plumb lo0:0. It
10124 	 * happens in ipif_lookup_on_name.
10125 	 * The specified unit number is ignored when we create the ipif on a
10126 	 * different interface. However, we save it in ipif_orig_ipifid below so
10127 	 * that the ipif fails back to the right position.
10128 	 */
10129 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10130 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10131 		err = ENOBUFS;
10132 		goto done;
10133 	}
10134 
10135 	/* Return created name with ioctl */
10136 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10137 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10138 	ip1dbg(("created %s\n", lifr->lifr_name));
10139 
10140 	/* Set address */
10141 	sin = (sin_t *)&lifr->lifr_addr;
10142 	if (sin->sin_family != AF_UNSPEC) {
10143 		err = ip_sioctl_addr(ipif, sin, q, mp,
10144 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10145 	}
10146 
10147 	/* Set ifindex and unit number for failback */
10148 	if (err == 0 && orig_ifindex != 0) {
10149 		ipif->ipif_orig_ifindex = orig_ifindex;
10150 		if (found_sep) {
10151 			ipif->ipif_orig_ipifid = id;
10152 		}
10153 	}
10154 
10155 done:
10156 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10157 	return (err);
10158 }
10159 
10160 /*
10161  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10162  * interface) delete it based on the IP address (on this physical interface).
10163  * Otherwise delete it based on the ipif_id.
10164  * Also, special handling to allow a removeif of lo0.
10165  */
10166 /* ARGSUSED */
10167 int
10168 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10169     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10170 {
10171 	conn_t		*connp;
10172 	ill_t		*ill = ipif->ipif_ill;
10173 	boolean_t	 success;
10174 
10175 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10176 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10177 	ASSERT(IAM_WRITER_IPIF(ipif));
10178 
10179 	connp = Q_TO_CONN(q);
10180 	/*
10181 	 * Special case for unplumbing lo0 (the loopback physical interface).
10182 	 * If unplumbing lo0, the incoming address structure has been
10183 	 * initialized to all zeros. When unplumbing lo0, all its logical
10184 	 * interfaces must be removed too.
10185 	 *
10186 	 * Note that this interface may be called to remove a specific
10187 	 * loopback logical interface (eg, lo0:1). But in that case
10188 	 * ipif->ipif_id != 0 so that the code path for that case is the
10189 	 * same as any other interface (meaning it skips the code directly
10190 	 * below).
10191 	 */
10192 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10193 		if (sin->sin_family == AF_UNSPEC &&
10194 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10195 			/*
10196 			 * Mark it condemned. No new ref. will be made to ill.
10197 			 */
10198 			mutex_enter(&ill->ill_lock);
10199 			ill->ill_state_flags |= ILL_CONDEMNED;
10200 			for (ipif = ill->ill_ipif; ipif != NULL;
10201 			    ipif = ipif->ipif_next) {
10202 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10203 			}
10204 			mutex_exit(&ill->ill_lock);
10205 
10206 			ipif = ill->ill_ipif;
10207 			/* unplumb the loopback interface */
10208 			ill_delete(ill);
10209 			mutex_enter(&connp->conn_lock);
10210 			mutex_enter(&ill->ill_lock);
10211 			ASSERT(ill->ill_group == NULL);
10212 
10213 			/* Are any references to this ill active */
10214 			if (ill_is_quiescent(ill)) {
10215 				mutex_exit(&ill->ill_lock);
10216 				mutex_exit(&connp->conn_lock);
10217 				ill_delete_tail(ill);
10218 				mi_free(ill);
10219 				return (0);
10220 			}
10221 			success = ipsq_pending_mp_add(connp, ipif,
10222 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10223 			mutex_exit(&connp->conn_lock);
10224 			mutex_exit(&ill->ill_lock);
10225 			if (success)
10226 				return (EINPROGRESS);
10227 			else
10228 				return (EINTR);
10229 		}
10230 	}
10231 
10232 	/*
10233 	 * We are exclusive on the ipsq, so an ill move will be serialized
10234 	 * before or after us.
10235 	 */
10236 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10237 
10238 	if (ipif->ipif_id == 0) {
10239 		/* Find based on address */
10240 		if (ipif->ipif_isv6) {
10241 			sin6_t *sin6;
10242 
10243 			if (sin->sin_family != AF_INET6)
10244 				return (EAFNOSUPPORT);
10245 
10246 			sin6 = (sin6_t *)sin;
10247 			/* We are a writer, so we should be able to lookup */
10248 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10249 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10250 			if (ipif == NULL) {
10251 				/*
10252 				 * Maybe the address in on another interface in
10253 				 * the same IPMP group? We check this below.
10254 				 */
10255 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10256 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10257 			}
10258 		} else {
10259 			ipaddr_t addr;
10260 
10261 			if (sin->sin_family != AF_INET)
10262 				return (EAFNOSUPPORT);
10263 
10264 			addr = sin->sin_addr.s_addr;
10265 			/* We are a writer, so we should be able to lookup */
10266 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10267 			    NULL, NULL, NULL);
10268 			if (ipif == NULL) {
10269 				/*
10270 				 * Maybe the address in on another interface in
10271 				 * the same IPMP group? We check this below.
10272 				 */
10273 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10274 				    NULL, NULL, NULL, NULL);
10275 			}
10276 		}
10277 		if (ipif == NULL) {
10278 			return (EADDRNOTAVAIL);
10279 		}
10280 		/*
10281 		 * When the address to be removed is hosted on a different
10282 		 * interface, we check if the interface is in the same IPMP
10283 		 * group as the specified one; if so we proceed with the
10284 		 * removal.
10285 		 * ill->ill_group is NULL when the ill is down, so we have to
10286 		 * compare the group names instead.
10287 		 */
10288 		if (ipif->ipif_ill != ill &&
10289 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10290 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10291 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10292 		    ill->ill_phyint->phyint_groupname) != 0)) {
10293 			ipif_refrele(ipif);
10294 			return (EADDRNOTAVAIL);
10295 		}
10296 
10297 		/* This is a writer */
10298 		ipif_refrele(ipif);
10299 	}
10300 
10301 	/*
10302 	 * Can not delete instance zero since it is tied to the ill.
10303 	 */
10304 	if (ipif->ipif_id == 0)
10305 		return (EBUSY);
10306 
10307 	mutex_enter(&ill->ill_lock);
10308 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10309 	mutex_exit(&ill->ill_lock);
10310 
10311 	ipif_free(ipif);
10312 
10313 	mutex_enter(&connp->conn_lock);
10314 	mutex_enter(&ill->ill_lock);
10315 
10316 	/* Are any references to this ipif active */
10317 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10318 		mutex_exit(&ill->ill_lock);
10319 		mutex_exit(&connp->conn_lock);
10320 		ipif_down_tail(ipif);
10321 		ipif_free_tail(ipif);
10322 		return (0);
10323 	    }
10324 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10325 	    IPIF_FREE);
10326 	mutex_exit(&ill->ill_lock);
10327 	mutex_exit(&connp->conn_lock);
10328 	if (success)
10329 		return (EINPROGRESS);
10330 	else
10331 		return (EINTR);
10332 }
10333 
10334 /*
10335  * Restart the removeif ioctl. The refcnt has gone down to 0.
10336  * The ipif is already condemned. So can't find it thru lookups.
10337  */
10338 /* ARGSUSED */
10339 int
10340 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10341     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10342 {
10343 	ill_t *ill;
10344 
10345 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10346 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10347 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10348 		ill = ipif->ipif_ill;
10349 		ASSERT(IAM_WRITER_ILL(ill));
10350 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10351 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10352 		ill_delete_tail(ill);
10353 		mi_free(ill);
10354 		return (0);
10355 	}
10356 
10357 	ill = ipif->ipif_ill;
10358 	ASSERT(IAM_WRITER_IPIF(ipif));
10359 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10360 
10361 	ipif_down_tail(ipif);
10362 	ipif_free_tail(ipif);
10363 
10364 	ILL_UNMARK_CHANGING(ill);
10365 	return (0);
10366 }
10367 
10368 /*
10369  * Set the local interface address.
10370  * Allow an address of all zero when the interface is down.
10371  */
10372 /* ARGSUSED */
10373 int
10374 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10375     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10376 {
10377 	int err = 0;
10378 	in6_addr_t v6addr;
10379 	boolean_t need_up = B_FALSE;
10380 
10381 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10382 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10383 
10384 	ASSERT(IAM_WRITER_IPIF(ipif));
10385 
10386 	if (ipif->ipif_isv6) {
10387 		sin6_t *sin6;
10388 		ill_t *ill;
10389 		phyint_t *phyi;
10390 
10391 		if (sin->sin_family != AF_INET6)
10392 			return (EAFNOSUPPORT);
10393 
10394 		sin6 = (sin6_t *)sin;
10395 		v6addr = sin6->sin6_addr;
10396 		ill = ipif->ipif_ill;
10397 		phyi = ill->ill_phyint;
10398 
10399 		/*
10400 		 * Enforce that true multicast interfaces have a link-local
10401 		 * address for logical unit 0.
10402 		 */
10403 		if (ipif->ipif_id == 0 &&
10404 		    (ill->ill_flags & ILLF_MULTICAST) &&
10405 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10406 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10407 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10408 			return (EADDRNOTAVAIL);
10409 		}
10410 
10411 		/*
10412 		 * up interfaces shouldn't have the unspecified address
10413 		 * unless they also have the IPIF_NOLOCAL flags set and
10414 		 * have a subnet assigned.
10415 		 */
10416 		if ((ipif->ipif_flags & IPIF_UP) &&
10417 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10418 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10419 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10420 			return (EADDRNOTAVAIL);
10421 		}
10422 
10423 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10424 			return (EADDRNOTAVAIL);
10425 	} else {
10426 		ipaddr_t addr;
10427 
10428 		if (sin->sin_family != AF_INET)
10429 			return (EAFNOSUPPORT);
10430 
10431 		addr = sin->sin_addr.s_addr;
10432 
10433 		/* Allow 0 as the local address. */
10434 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10435 			return (EADDRNOTAVAIL);
10436 
10437 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10438 	}
10439 
10440 
10441 	/*
10442 	 * Even if there is no change we redo things just to rerun
10443 	 * ipif_set_default.
10444 	 */
10445 	if (ipif->ipif_flags & IPIF_UP) {
10446 		/*
10447 		 * Setting a new local address, make sure
10448 		 * we have net and subnet bcast ire's for
10449 		 * the old address if we need them.
10450 		 */
10451 		if (!ipif->ipif_isv6)
10452 			ipif_check_bcast_ires(ipif);
10453 		/*
10454 		 * If the interface is already marked up,
10455 		 * we call ipif_down which will take care
10456 		 * of ditching any IREs that have been set
10457 		 * up based on the old interface address.
10458 		 */
10459 		err = ipif_logical_down(ipif, q, mp);
10460 		if (err == EINPROGRESS)
10461 			return (err);
10462 		ipif_down_tail(ipif);
10463 		need_up = 1;
10464 	}
10465 
10466 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10467 	return (err);
10468 }
10469 
10470 int
10471 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10472     boolean_t need_up)
10473 {
10474 	in6_addr_t v6addr;
10475 	ipaddr_t addr;
10476 	sin6_t	*sin6;
10477 	int	err = 0;
10478 
10479 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10480 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10481 	ASSERT(IAM_WRITER_IPIF(ipif));
10482 	if (ipif->ipif_isv6) {
10483 		sin6 = (sin6_t *)sin;
10484 		v6addr = sin6->sin6_addr;
10485 	} else {
10486 		addr = sin->sin_addr.s_addr;
10487 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10488 	}
10489 	mutex_enter(&ipif->ipif_ill->ill_lock);
10490 	ipif->ipif_v6lcl_addr = v6addr;
10491 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10492 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10493 	} else {
10494 		ipif->ipif_v6src_addr = v6addr;
10495 	}
10496 
10497 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10498 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10499 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10500 
10501 		/*
10502 		 * The local address of this interface is a 6to4 address,
10503 		 * check if this interface is in fact a 6to4 tunnel or just
10504 		 * an interface configured with a 6to4 address.  We are only
10505 		 * interested in the former.
10506 		 */
10507 		if (wqp != NULL) {
10508 			while ((wqp->q_next != NULL) &&
10509 			    (wqp->q_next->q_qinfo != NULL) &&
10510 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10511 
10512 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10513 				    == TUN6TO4_MODID) {
10514 					/* set for use in IP */
10515 					ipif->ipif_ill->ill_is_6to4tun = 1;
10516 					break;
10517 				}
10518 				wqp = wqp->q_next;
10519 			}
10520 		}
10521 	}
10522 
10523 	ipif_set_default(ipif);
10524 	mutex_exit(&ipif->ipif_ill->ill_lock);
10525 
10526 	if (need_up) {
10527 		/*
10528 		 * Now bring the interface back up.  If this
10529 		 * is the only IPIF for the ILL, ipif_up
10530 		 * will have to re-bind to the device, so
10531 		 * we may get back EINPROGRESS, in which
10532 		 * case, this IOCTL will get completed in
10533 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10534 		 */
10535 		err = ipif_up(ipif, q, mp);
10536 	} else {
10537 		/*
10538 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10539 		 * if need_up is true.
10540 		 */
10541 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10542 	}
10543 
10544 	return (err);
10545 }
10546 
10547 
10548 /*
10549  * Restart entry point to restart the address set operation after the
10550  * refcounts have dropped to zero.
10551  */
10552 /* ARGSUSED */
10553 int
10554 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10555     ip_ioctl_cmd_t *ipip, void *ifreq)
10556 {
10557 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10558 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10559 	ASSERT(IAM_WRITER_IPIF(ipif));
10560 	ipif_down_tail(ipif);
10561 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10562 }
10563 
10564 /* ARGSUSED */
10565 int
10566 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10567     ip_ioctl_cmd_t *ipip, void *if_req)
10568 {
10569 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10570 	struct lifreq *lifr = (struct lifreq *)if_req;
10571 
10572 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10573 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10574 	/*
10575 	 * The net mask and address can't change since we have a
10576 	 * reference to the ipif. So no lock is necessary.
10577 	 */
10578 	if (ipif->ipif_isv6) {
10579 		*sin6 = sin6_null;
10580 		sin6->sin6_family = AF_INET6;
10581 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10582 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10583 		lifr->lifr_addrlen =
10584 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10585 	} else {
10586 		*sin = sin_null;
10587 		sin->sin_family = AF_INET;
10588 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10589 		if (ipip->ipi_cmd_type == LIF_CMD) {
10590 			lifr->lifr_addrlen =
10591 			    ip_mask_to_plen(ipif->ipif_net_mask);
10592 		}
10593 	}
10594 	return (0);
10595 }
10596 
10597 /*
10598  * Set the destination address for a pt-pt interface.
10599  */
10600 /* ARGSUSED */
10601 int
10602 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10603     ip_ioctl_cmd_t *ipip, void *if_req)
10604 {
10605 	int err = 0;
10606 	in6_addr_t v6addr;
10607 	boolean_t need_up = B_FALSE;
10608 
10609 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10610 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10611 	ASSERT(IAM_WRITER_IPIF(ipif));
10612 
10613 	if (ipif->ipif_isv6) {
10614 		sin6_t *sin6;
10615 
10616 		if (sin->sin_family != AF_INET6)
10617 			return (EAFNOSUPPORT);
10618 
10619 		sin6 = (sin6_t *)sin;
10620 		v6addr = sin6->sin6_addr;
10621 
10622 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10623 			return (EADDRNOTAVAIL);
10624 	} else {
10625 		ipaddr_t addr;
10626 
10627 		if (sin->sin_family != AF_INET)
10628 			return (EAFNOSUPPORT);
10629 
10630 		addr = sin->sin_addr.s_addr;
10631 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10632 			return (EADDRNOTAVAIL);
10633 
10634 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10635 	}
10636 
10637 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10638 		return (0);	/* No change */
10639 
10640 	if (ipif->ipif_flags & IPIF_UP) {
10641 		/*
10642 		 * If the interface is already marked up,
10643 		 * we call ipif_down which will take care
10644 		 * of ditching any IREs that have been set
10645 		 * up based on the old pp dst address.
10646 		 */
10647 		err = ipif_logical_down(ipif, q, mp);
10648 		if (err == EINPROGRESS)
10649 			return (err);
10650 		ipif_down_tail(ipif);
10651 		need_up = B_TRUE;
10652 	}
10653 	/*
10654 	 * could return EINPROGRESS. If so ioctl will complete in
10655 	 * ip_rput_dlpi_writer
10656 	 */
10657 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10658 	return (err);
10659 }
10660 
10661 static int
10662 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10663     boolean_t need_up)
10664 {
10665 	in6_addr_t v6addr;
10666 	ill_t	*ill = ipif->ipif_ill;
10667 	int	err = 0;
10668 
10669 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10670 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10671 	if (ipif->ipif_isv6) {
10672 		sin6_t *sin6;
10673 
10674 		sin6 = (sin6_t *)sin;
10675 		v6addr = sin6->sin6_addr;
10676 	} else {
10677 		ipaddr_t addr;
10678 
10679 		addr = sin->sin_addr.s_addr;
10680 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10681 	}
10682 	mutex_enter(&ill->ill_lock);
10683 	/* Set point to point destination address. */
10684 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10685 		/*
10686 		 * Allow this as a means of creating logical
10687 		 * pt-pt interfaces on top of e.g. an Ethernet.
10688 		 * XXX Undocumented HACK for testing.
10689 		 * pt-pt interfaces are created with NUD disabled.
10690 		 */
10691 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10692 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10693 		if (ipif->ipif_isv6)
10694 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10695 	}
10696 
10697 	/* Set the new address. */
10698 	ipif->ipif_v6pp_dst_addr = v6addr;
10699 	/* Make sure subnet tracks pp_dst */
10700 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10701 	mutex_exit(&ill->ill_lock);
10702 
10703 	if (need_up) {
10704 		/*
10705 		 * Now bring the interface back up.  If this
10706 		 * is the only IPIF for the ILL, ipif_up
10707 		 * will have to re-bind to the device, so
10708 		 * we may get back EINPROGRESS, in which
10709 		 * case, this IOCTL will get completed in
10710 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10711 		 */
10712 		err = ipif_up(ipif, q, mp);
10713 	}
10714 	return (err);
10715 }
10716 
10717 /*
10718  * Restart entry point to restart the dstaddress set operation after the
10719  * refcounts have dropped to zero.
10720  */
10721 /* ARGSUSED */
10722 int
10723 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10724     ip_ioctl_cmd_t *ipip, void *ifreq)
10725 {
10726 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10727 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10728 	ipif_down_tail(ipif);
10729 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10730 }
10731 
10732 /* ARGSUSED */
10733 int
10734 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10735     ip_ioctl_cmd_t *ipip, void *if_req)
10736 {
10737 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10738 
10739 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10740 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10741 	/*
10742 	 * Get point to point destination address. The addresses can't
10743 	 * change since we hold a reference to the ipif.
10744 	 */
10745 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10746 		return (EADDRNOTAVAIL);
10747 
10748 	if (ipif->ipif_isv6) {
10749 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10750 		*sin6 = sin6_null;
10751 		sin6->sin6_family = AF_INET6;
10752 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10753 	} else {
10754 		*sin = sin_null;
10755 		sin->sin_family = AF_INET;
10756 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10757 	}
10758 	return (0);
10759 }
10760 
10761 /*
10762  * part of ipmp, make this func return the active/inactive state and
10763  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10764  */
10765 /*
10766  * This function either sets or clears the IFF_INACTIVE flag.
10767  *
10768  * As long as there are some addresses or multicast memberships on the
10769  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10770  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10771  * will be used for outbound packets.
10772  *
10773  * Caller needs to verify the validity of setting IFF_INACTIVE.
10774  */
10775 static void
10776 phyint_inactive(phyint_t *phyi)
10777 {
10778 	ill_t *ill_v4;
10779 	ill_t *ill_v6;
10780 	ipif_t *ipif;
10781 	ilm_t *ilm;
10782 
10783 	ill_v4 = phyi->phyint_illv4;
10784 	ill_v6 = phyi->phyint_illv6;
10785 
10786 	/*
10787 	 * No need for a lock while traversing the list since iam
10788 	 * a writer
10789 	 */
10790 	if (ill_v4 != NULL) {
10791 		ASSERT(IAM_WRITER_ILL(ill_v4));
10792 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10793 		    ipif = ipif->ipif_next) {
10794 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10795 				mutex_enter(&phyi->phyint_lock);
10796 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10797 				mutex_exit(&phyi->phyint_lock);
10798 				return;
10799 			}
10800 		}
10801 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10802 		    ilm = ilm->ilm_next) {
10803 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10804 				mutex_enter(&phyi->phyint_lock);
10805 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10806 				mutex_exit(&phyi->phyint_lock);
10807 				return;
10808 			}
10809 		}
10810 	}
10811 	if (ill_v6 != NULL) {
10812 		ill_v6 = phyi->phyint_illv6;
10813 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
10814 		    ipif = ipif->ipif_next) {
10815 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10816 				mutex_enter(&phyi->phyint_lock);
10817 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10818 				mutex_exit(&phyi->phyint_lock);
10819 				return;
10820 			}
10821 		}
10822 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
10823 		    ilm = ilm->ilm_next) {
10824 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10825 				mutex_enter(&phyi->phyint_lock);
10826 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10827 				mutex_exit(&phyi->phyint_lock);
10828 				return;
10829 			}
10830 		}
10831 	}
10832 	mutex_enter(&phyi->phyint_lock);
10833 	phyi->phyint_flags |= PHYI_INACTIVE;
10834 	mutex_exit(&phyi->phyint_lock);
10835 }
10836 
10837 /*
10838  * This function is called only when the phyint flags change. Currently
10839  * called from ip_sioctl_flags. We re-do the broadcast nomination so
10840  * that we can select a good ill.
10841  */
10842 static void
10843 ip_redo_nomination(phyint_t *phyi)
10844 {
10845 	ill_t *ill_v4;
10846 
10847 	ill_v4 = phyi->phyint_illv4;
10848 
10849 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
10850 		ASSERT(IAM_WRITER_ILL(ill_v4));
10851 		if (ill_v4->ill_group->illgrp_ill_count > 1)
10852 			ill_nominate_bcast_rcv(ill_v4->ill_group);
10853 	}
10854 }
10855 
10856 /*
10857  * Heuristic to check if ill is INACTIVE.
10858  * Checks if ill has an ipif with an usable ip address.
10859  *
10860  * Return values:
10861  *	B_TRUE	- ill is INACTIVE; has no usable ipif
10862  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
10863  */
10864 static boolean_t
10865 ill_is_inactive(ill_t *ill)
10866 {
10867 	ipif_t *ipif;
10868 
10869 	/* Check whether it is in an IPMP group */
10870 	if (ill->ill_phyint->phyint_groupname == NULL)
10871 		return (B_FALSE);
10872 
10873 	if (ill->ill_ipif_up_count == 0)
10874 		return (B_TRUE);
10875 
10876 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
10877 		uint64_t flags = ipif->ipif_flags;
10878 
10879 		/*
10880 		 * This ipif is usable if it is IPIF_UP and not a
10881 		 * dedicated test address.  A dedicated test address
10882 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
10883 		 * (note in particular that V6 test addresses are
10884 		 * link-local data addresses and thus are marked
10885 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
10886 		 */
10887 		if ((flags & IPIF_UP) &&
10888 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
10889 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
10890 			return (B_FALSE);
10891 	}
10892 	return (B_TRUE);
10893 }
10894 
10895 /*
10896  * Set interface flags.
10897  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
10898  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
10899  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
10900  *
10901  * NOTE : We really don't enforce that ipif_id zero should be used
10902  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10903  *	  is because applications generally does SICGLIFFLAGS and
10904  *	  ORs in the new flags (that affects the logical) and does a
10905  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10906  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10907  *	  flags that will be turned on is correct with respect to
10908  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10909  */
10910 /* ARGSUSED */
10911 int
10912 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10913     ip_ioctl_cmd_t *ipip, void *if_req)
10914 {
10915 	uint64_t turn_on;
10916 	uint64_t turn_off;
10917 	int	err;
10918 	boolean_t need_up = B_FALSE;
10919 	phyint_t *phyi;
10920 	ill_t *ill;
10921 	uint64_t intf_flags;
10922 	boolean_t phyint_flags_modified = B_FALSE;
10923 	uint64_t flags;
10924 	struct ifreq *ifr;
10925 	struct lifreq *lifr;
10926 	boolean_t set_linklocal = B_FALSE;
10927 	boolean_t zero_source = B_FALSE;
10928 
10929 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10930 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10931 
10932 	ASSERT(IAM_WRITER_IPIF(ipif));
10933 
10934 	ill = ipif->ipif_ill;
10935 	phyi = ill->ill_phyint;
10936 
10937 	if (ipip->ipi_cmd_type == IF_CMD) {
10938 		ifr = (struct ifreq *)if_req;
10939 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10940 	} else {
10941 		lifr = (struct lifreq *)if_req;
10942 		flags = lifr->lifr_flags;
10943 	}
10944 
10945 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10946 
10947 	/*
10948 	 * Has the flags been set correctly till now ?
10949 	 */
10950 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10951 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10952 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10953 	/*
10954 	 * Compare the new flags to the old, and partition
10955 	 * into those coming on and those going off.
10956 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10957 	 */
10958 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10959 		flags |= intf_flags & ~0xFFFF;
10960 
10961 	/*
10962 	 * First check which bits will change and then which will
10963 	 * go on and off
10964 	 */
10965 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
10966 	if (!turn_on)
10967 		return (0);	/* No change */
10968 
10969 	turn_off = intf_flags & turn_on;
10970 	turn_on ^= turn_off;
10971 	err = 0;
10972 
10973 	/*
10974 	 * Don't allow any bits belonging to the logical interface
10975 	 * to be set or cleared on the replacement ipif that was
10976 	 * created temporarily during a MOVE.
10977 	 */
10978 	if (ipif->ipif_replace_zero &&
10979 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
10980 		return (EINVAL);
10981 	}
10982 
10983 	/*
10984 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
10985 	 * IPv6 interfaces.
10986 	 */
10987 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
10988 		return (EINVAL);
10989 
10990 	/*
10991 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10992 	 * interfaces.  It makes no sense in that context.
10993 	 */
10994 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10995 		return (EINVAL);
10996 
10997 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
10998 		zero_source = B_TRUE;
10999 
11000 	/*
11001 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11002 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11003 	 * If the link local address isn't set, and can be set, it will get
11004 	 * set later on in this function.
11005 	 */
11006 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11007 	    (flags & IFF_UP) && !zero_source &&
11008 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11009 		if (ipif_cant_setlinklocal(ipif))
11010 			return (EINVAL);
11011 		set_linklocal = B_TRUE;
11012 	}
11013 
11014 	/*
11015 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11016 	 * same time. No need to grab ill_g_usesrc_lock here, see
11017 	 * synchronization notes in ip.c
11018 	 */
11019 	if (turn_on & PHYI_STANDBY &&
11020 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11021 		return (EINVAL);
11022 	}
11023 
11024 	/*
11025 	 * If we modify physical interface flags, we'll potentially need to
11026 	 * send up two routing socket messages for the changes (one for the
11027 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11028 	 */
11029 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11030 		phyint_flags_modified = B_TRUE;
11031 
11032 	/*
11033 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11034 	 * we need to flush the IRE_CACHES belonging to this ill.
11035 	 * We handle this case here without doing the DOWN/UP dance
11036 	 * like it is done for other flags. If some other flags are
11037 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11038 	 * below will handle it by bringing it down and then
11039 	 * bringing it UP.
11040 	 */
11041 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11042 		ill_t *ill_v4, *ill_v6;
11043 
11044 		ill_v4 = phyi->phyint_illv4;
11045 		ill_v6 = phyi->phyint_illv6;
11046 
11047 		/*
11048 		 * First set the INACTIVE flag if needed. Then delete the ires.
11049 		 * ire_add will atomically prevent creating new IRE_CACHEs
11050 		 * unless hidden flag is set.
11051 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11052 		 */
11053 		if ((turn_on & PHYI_FAILED) &&
11054 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11055 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11056 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11057 		}
11058 		if ((turn_off & PHYI_FAILED) &&
11059 		    ((intf_flags & PHYI_STANDBY) ||
11060 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11061 			phyint_inactive(phyi);
11062 		}
11063 
11064 		if (turn_on & PHYI_STANDBY) {
11065 			/*
11066 			 * We implicitly set INACTIVE only when STANDBY is set.
11067 			 * INACTIVE is also set on non-STANDBY phyint when user
11068 			 * disables FAILBACK using configuration file.
11069 			 * Do not allow STANDBY to be set on such INACTIVE
11070 			 * phyint
11071 			 */
11072 			if (phyi->phyint_flags & PHYI_INACTIVE)
11073 				return (EINVAL);
11074 			if (!(phyi->phyint_flags & PHYI_FAILED))
11075 				phyint_inactive(phyi);
11076 		}
11077 		if (turn_off & PHYI_STANDBY) {
11078 			if (ipmp_enable_failback) {
11079 				/*
11080 				 * Reset PHYI_INACTIVE.
11081 				 */
11082 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11083 			} else if (ill_is_inactive(ill) &&
11084 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11085 				/*
11086 				 * Need to set INACTIVE, when user sets
11087 				 * STANDBY on a non-STANDBY phyint and
11088 				 * later resets STANDBY
11089 				 */
11090 				phyint_inactive(phyi);
11091 			}
11092 		}
11093 		/*
11094 		 * We should always send up a message so that the
11095 		 * daemons come to know of it. Note that the zeroth
11096 		 * interface can be down and the check below for IPIF_UP
11097 		 * will not make sense as we are actually setting
11098 		 * a phyint flag here. We assume that the ipif used
11099 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11100 		 * send up any message for non-zero ipifs).
11101 		 */
11102 		phyint_flags_modified = B_TRUE;
11103 
11104 		if (ill_v4 != NULL) {
11105 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11106 			    IRE_CACHE, ill_stq_cache_delete,
11107 			    (char *)ill_v4, ill_v4);
11108 			illgrp_reset_schednext(ill_v4);
11109 		}
11110 		if (ill_v6 != NULL) {
11111 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11112 			    IRE_CACHE, ill_stq_cache_delete,
11113 			    (char *)ill_v6, ill_v6);
11114 			illgrp_reset_schednext(ill_v6);
11115 		}
11116 	}
11117 
11118 	/*
11119 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11120 	 * status of the interface and, if the interface is part of an IPMP
11121 	 * group, all other interfaces that are part of the same IPMP
11122 	 * group.
11123 	 */
11124 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11125 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11126 		    (caddr_t)ill);
11127 	}
11128 
11129 	/*
11130 	 * If the interface is not UP and we are not going to
11131 	 * bring it UP, record the flags and return. When the
11132 	 * interface comes UP later, the right actions will be
11133 	 * taken.
11134 	 */
11135 	if (!(ipif->ipif_flags & IPIF_UP) &&
11136 	    !(turn_on & IPIF_UP)) {
11137 		/* Record new flags in their respective places. */
11138 		mutex_enter(&ill->ill_lock);
11139 		mutex_enter(&ill->ill_phyint->phyint_lock);
11140 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11141 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11142 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11143 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11144 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11145 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11146 		mutex_exit(&ill->ill_lock);
11147 		mutex_exit(&ill->ill_phyint->phyint_lock);
11148 
11149 		/*
11150 		 * We do the broadcast and nomination here rather
11151 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11152 		 * the case of FAILBACK from INACTIVE standby to the
11153 		 * interface that has been repaired, PHYI_FAILED has not
11154 		 * been cleared yet. If there are only two interfaces in
11155 		 * that group, all we have is a FAILED and INACTIVE
11156 		 * interface. If we do the nomination soon after a failback,
11157 		 * the broadcast nomination code would select the
11158 		 * INACTIVE interface for receiving broadcasts as FAILED is
11159 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11160 		 * receive broadcast packets, we need to redo nomination
11161 		 * when the FAILED is cleared here. Thus, in general we
11162 		 * always do the nomination here for FAILED, STANDBY
11163 		 * and OFFLINE.
11164 		 */
11165 		if (((turn_on | turn_off) &
11166 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11167 			ip_redo_nomination(phyi);
11168 		}
11169 		if (phyint_flags_modified) {
11170 			if (phyi->phyint_illv4 != NULL) {
11171 				ip_rts_ifmsg(phyi->phyint_illv4->
11172 				    ill_ipif);
11173 			}
11174 			if (phyi->phyint_illv6 != NULL) {
11175 				ip_rts_ifmsg(phyi->phyint_illv6->
11176 				    ill_ipif);
11177 			}
11178 		}
11179 		return (0);
11180 	} else if (set_linklocal || zero_source) {
11181 		mutex_enter(&ill->ill_lock);
11182 		if (set_linklocal)
11183 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11184 		if (zero_source)
11185 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11186 		mutex_exit(&ill->ill_lock);
11187 	}
11188 
11189 	/*
11190 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11191 	 * or point-to-point interfaces with an unspecified destination. We do
11192 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11193 	 * have a subnet assigned, which is how in.ndpd currently manages its
11194 	 * onlink prefix list when no addresses are configured with those
11195 	 * prefixes.
11196 	 */
11197 	if (ipif->ipif_isv6 &&
11198 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11199 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11200 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11201 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11202 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11203 		return (EINVAL);
11204 	}
11205 
11206 	/*
11207 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11208 	 * from being brought up.
11209 	 */
11210 	if (!ipif->ipif_isv6 &&
11211 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11212 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11213 		return (EINVAL);
11214 	}
11215 
11216 	/*
11217 	 * The only flag changes that we currently take specific action on
11218 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11219 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11220 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11221 	 * the flags and bringing it back up again.
11222 	 */
11223 	if ((turn_on|turn_off) &
11224 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11225 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11226 		/*
11227 		 * Taking this ipif down, make sure we have
11228 		 * valid net and subnet bcast ire's for other
11229 		 * logical interfaces, if we need them.
11230 		 */
11231 		if (!ipif->ipif_isv6)
11232 			ipif_check_bcast_ires(ipif);
11233 
11234 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11235 		    !(turn_off & IPIF_UP)) {
11236 			need_up = B_TRUE;
11237 			if (ipif->ipif_flags & IPIF_UP)
11238 				ill->ill_logical_down = 1;
11239 			turn_on &= ~IPIF_UP;
11240 		}
11241 		err = ipif_down(ipif, q, mp);
11242 		ip1dbg(("ipif_down returns %d err ", err));
11243 		if (err == EINPROGRESS)
11244 			return (err);
11245 		ipif_down_tail(ipif);
11246 	}
11247 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11248 }
11249 
11250 static int
11251 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11252     boolean_t need_up)
11253 {
11254 	ill_t	*ill;
11255 	phyint_t *phyi;
11256 	uint64_t turn_on;
11257 	uint64_t turn_off;
11258 	uint64_t intf_flags;
11259 	boolean_t phyint_flags_modified = B_FALSE;
11260 	int	err = 0;
11261 	boolean_t set_linklocal = B_FALSE;
11262 	boolean_t zero_source = B_FALSE;
11263 
11264 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11265 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11266 
11267 	ASSERT(IAM_WRITER_IPIF(ipif));
11268 
11269 	ill = ipif->ipif_ill;
11270 	phyi = ill->ill_phyint;
11271 
11272 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11273 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11274 
11275 	turn_off = intf_flags & turn_on;
11276 	turn_on ^= turn_off;
11277 
11278 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11279 		phyint_flags_modified = B_TRUE;
11280 
11281 	/*
11282 	 * Now we change the flags. Track current value of
11283 	 * other flags in their respective places.
11284 	 */
11285 	mutex_enter(&ill->ill_lock);
11286 	mutex_enter(&phyi->phyint_lock);
11287 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11288 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11289 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11290 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11291 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11292 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11293 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11294 		set_linklocal = B_TRUE;
11295 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11296 	}
11297 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11298 		zero_source = B_TRUE;
11299 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11300 	}
11301 	mutex_exit(&ill->ill_lock);
11302 	mutex_exit(&phyi->phyint_lock);
11303 
11304 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11305 		ip_redo_nomination(phyi);
11306 
11307 	if (set_linklocal)
11308 		(void) ipif_setlinklocal(ipif);
11309 
11310 	if (zero_source)
11311 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11312 	else
11313 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11314 
11315 	if (need_up) {
11316 		/*
11317 		 * XXX ipif_up really does not know whether a phyint flags
11318 		 * was modified or not. So, it sends up information on
11319 		 * only one routing sockets message. As we don't bring up
11320 		 * the interface and also set STANDBY/FAILED simultaneously
11321 		 * it should be okay.
11322 		 */
11323 		err = ipif_up(ipif, q, mp);
11324 	} else {
11325 		/*
11326 		 * Make sure routing socket sees all changes to the flags.
11327 		 * ipif_up_done* handles this when we use ipif_up.
11328 		 */
11329 		if (phyint_flags_modified) {
11330 			if (phyi->phyint_illv4 != NULL) {
11331 				ip_rts_ifmsg(phyi->phyint_illv4->
11332 				    ill_ipif);
11333 			}
11334 			if (phyi->phyint_illv6 != NULL) {
11335 				ip_rts_ifmsg(phyi->phyint_illv6->
11336 				    ill_ipif);
11337 			}
11338 		} else {
11339 			ip_rts_ifmsg(ipif);
11340 		}
11341 	}
11342 	return (err);
11343 }
11344 
11345 /*
11346  * Restart entry point to restart the flags restart operation after the
11347  * refcounts have dropped to zero.
11348  */
11349 /* ARGSUSED */
11350 int
11351 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11352     ip_ioctl_cmd_t *ipip, void *if_req)
11353 {
11354 	int	err;
11355 	struct ifreq *ifr = (struct ifreq *)if_req;
11356 	struct lifreq *lifr = (struct lifreq *)if_req;
11357 
11358 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11359 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11360 
11361 	ipif_down_tail(ipif);
11362 	if (ipip->ipi_cmd_type == IF_CMD) {
11363 		/*
11364 		 * Since ip_sioctl_flags expects an int and ifr_flags
11365 		 * is a short we need to cast ifr_flags into an int
11366 		 * to avoid having sign extension cause bits to get
11367 		 * set that should not be.
11368 		 */
11369 		err = ip_sioctl_flags_tail(ipif,
11370 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11371 		    q, mp, B_TRUE);
11372 	} else {
11373 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11374 		    q, mp, B_TRUE);
11375 	}
11376 	return (err);
11377 }
11378 
11379 /* ARGSUSED */
11380 int
11381 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11382     ip_ioctl_cmd_t *ipip, void *if_req)
11383 {
11384 	/*
11385 	 * Has the flags been set correctly till now ?
11386 	 */
11387 	ill_t *ill = ipif->ipif_ill;
11388 	phyint_t *phyi = ill->ill_phyint;
11389 
11390 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11391 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11392 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11393 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11394 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11395 
11396 	/*
11397 	 * Need a lock since some flags can be set even when there are
11398 	 * references to the ipif.
11399 	 */
11400 	mutex_enter(&ill->ill_lock);
11401 	if (ipip->ipi_cmd_type == IF_CMD) {
11402 		struct ifreq *ifr = (struct ifreq *)if_req;
11403 
11404 		/* Get interface flags (low 16 only). */
11405 		ifr->ifr_flags = ((ipif->ipif_flags |
11406 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11407 	} else {
11408 		struct lifreq *lifr = (struct lifreq *)if_req;
11409 
11410 		/* Get interface flags. */
11411 		lifr->lifr_flags = ipif->ipif_flags |
11412 		    ill->ill_flags | phyi->phyint_flags;
11413 	}
11414 	mutex_exit(&ill->ill_lock);
11415 	return (0);
11416 }
11417 
11418 /* ARGSUSED */
11419 int
11420 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11421     ip_ioctl_cmd_t *ipip, void *if_req)
11422 {
11423 	int mtu;
11424 	int ip_min_mtu;
11425 	struct ifreq	*ifr;
11426 	struct lifreq *lifr;
11427 	ire_t	*ire;
11428 
11429 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11430 	    ipif->ipif_id, (void *)ipif));
11431 	if (ipip->ipi_cmd_type == IF_CMD) {
11432 		ifr = (struct ifreq *)if_req;
11433 		mtu = ifr->ifr_metric;
11434 	} else {
11435 		lifr = (struct lifreq *)if_req;
11436 		mtu = lifr->lifr_mtu;
11437 	}
11438 
11439 	if (ipif->ipif_isv6)
11440 		ip_min_mtu = IPV6_MIN_MTU;
11441 	else
11442 		ip_min_mtu = IP_MIN_MTU;
11443 
11444 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11445 		return (EINVAL);
11446 
11447 	/*
11448 	 * Change the MTU size in all relevant ire's.
11449 	 * Mtu change Vs. new ire creation - protocol below.
11450 	 * First change ipif_mtu and the ire_max_frag of the
11451 	 * interface ire. Then do an ire walk and change the
11452 	 * ire_max_frag of all affected ires. During ire_add
11453 	 * under the bucket lock, set the ire_max_frag of the
11454 	 * new ire being created from the ipif/ire from which
11455 	 * it is being derived. If an mtu change happens after
11456 	 * the ire is added, the new ire will be cleaned up.
11457 	 * Conversely if the mtu change happens before the ire
11458 	 * is added, ire_add will see the new value of the mtu.
11459 	 */
11460 	ipif->ipif_mtu = mtu;
11461 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11462 
11463 	if (ipif->ipif_isv6)
11464 		ire = ipif_to_ire_v6(ipif);
11465 	else
11466 		ire = ipif_to_ire(ipif);
11467 	if (ire != NULL) {
11468 		ire->ire_max_frag = ipif->ipif_mtu;
11469 		ire_refrele(ire);
11470 	}
11471 	if (ipif->ipif_flags & IPIF_UP) {
11472 		if (ipif->ipif_isv6)
11473 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11474 		else
11475 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11476 	}
11477 	/* Update the MTU in SCTP's list */
11478 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11479 	return (0);
11480 }
11481 
11482 /* Get interface MTU. */
11483 /* ARGSUSED */
11484 int
11485 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11486 	ip_ioctl_cmd_t *ipip, void *if_req)
11487 {
11488 	struct ifreq	*ifr;
11489 	struct lifreq	*lifr;
11490 
11491 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11492 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11493 	if (ipip->ipi_cmd_type == IF_CMD) {
11494 		ifr = (struct ifreq *)if_req;
11495 		ifr->ifr_metric = ipif->ipif_mtu;
11496 	} else {
11497 		lifr = (struct lifreq *)if_req;
11498 		lifr->lifr_mtu = ipif->ipif_mtu;
11499 	}
11500 	return (0);
11501 }
11502 
11503 /* Set interface broadcast address. */
11504 /* ARGSUSED2 */
11505 int
11506 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11507 	ip_ioctl_cmd_t *ipip, void *if_req)
11508 {
11509 	ipaddr_t addr;
11510 	ire_t	*ire;
11511 
11512 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11513 	    ipif->ipif_id));
11514 
11515 	ASSERT(IAM_WRITER_IPIF(ipif));
11516 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11517 		return (EADDRNOTAVAIL);
11518 
11519 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11520 
11521 	if (sin->sin_family != AF_INET)
11522 		return (EAFNOSUPPORT);
11523 
11524 	addr = sin->sin_addr.s_addr;
11525 	if (ipif->ipif_flags & IPIF_UP) {
11526 		/*
11527 		 * If we are already up, make sure the new
11528 		 * broadcast address makes sense.  If it does,
11529 		 * there should be an IRE for it already.
11530 		 * Don't match on ipif, only on the ill
11531 		 * since we are sharing these now. Don't use
11532 		 * MATCH_IRE_ILL_GROUP as we are looking for
11533 		 * the broadcast ire on this ill and each ill
11534 		 * in the group has its own broadcast ire.
11535 		 */
11536 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11537 		    ipif, ALL_ZONES, (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11538 		if (ire == NULL) {
11539 			return (EINVAL);
11540 		} else {
11541 			ire_refrele(ire);
11542 		}
11543 	}
11544 	/*
11545 	 * Changing the broadcast addr for this ipif.
11546 	 * Make sure we have valid net and subnet bcast
11547 	 * ire's for other logical interfaces, if needed.
11548 	 */
11549 	if (addr != ipif->ipif_brd_addr)
11550 		ipif_check_bcast_ires(ipif);
11551 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11552 	return (0);
11553 }
11554 
11555 /* Get interface broadcast address. */
11556 /* ARGSUSED */
11557 int
11558 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11559     ip_ioctl_cmd_t *ipip, void *if_req)
11560 {
11561 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11562 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11563 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11564 		return (EADDRNOTAVAIL);
11565 
11566 	/* IPIF_BROADCAST not possible with IPv6 */
11567 	ASSERT(!ipif->ipif_isv6);
11568 	*sin = sin_null;
11569 	sin->sin_family = AF_INET;
11570 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11571 	return (0);
11572 }
11573 
11574 /*
11575  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11576  */
11577 /* ARGSUSED */
11578 int
11579 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11580     ip_ioctl_cmd_t *ipip, void *if_req)
11581 {
11582 	int err = 0;
11583 	in6_addr_t v6mask;
11584 
11585 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11586 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11587 
11588 	ASSERT(IAM_WRITER_IPIF(ipif));
11589 
11590 	if (ipif->ipif_isv6) {
11591 		sin6_t *sin6;
11592 
11593 		if (sin->sin_family != AF_INET6)
11594 			return (EAFNOSUPPORT);
11595 
11596 		sin6 = (sin6_t *)sin;
11597 		v6mask = sin6->sin6_addr;
11598 	} else {
11599 		ipaddr_t mask;
11600 
11601 		if (sin->sin_family != AF_INET)
11602 			return (EAFNOSUPPORT);
11603 
11604 		mask = sin->sin_addr.s_addr;
11605 		V4MASK_TO_V6(mask, v6mask);
11606 	}
11607 
11608 	/*
11609 	 * No big deal if the interface isn't already up, or the mask
11610 	 * isn't really changing, or this is pt-pt.
11611 	 */
11612 	if (!(ipif->ipif_flags & IPIF_UP) ||
11613 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11614 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11615 		ipif->ipif_v6net_mask = v6mask;
11616 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11617 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11618 			    ipif->ipif_v6net_mask,
11619 			    ipif->ipif_v6subnet);
11620 		}
11621 		return (0);
11622 	}
11623 	/*
11624 	 * Make sure we have valid net and subnet broadcast ire's
11625 	 * for the old netmask, if needed by other logical interfaces.
11626 	 */
11627 	if (!ipif->ipif_isv6)
11628 		ipif_check_bcast_ires(ipif);
11629 
11630 	err = ipif_logical_down(ipif, q, mp);
11631 	if (err == EINPROGRESS)
11632 		return (err);
11633 	ipif_down_tail(ipif);
11634 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11635 	return (err);
11636 }
11637 
11638 static int
11639 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11640 {
11641 	in6_addr_t v6mask;
11642 	int err = 0;
11643 
11644 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11645 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11646 
11647 	if (ipif->ipif_isv6) {
11648 		sin6_t *sin6;
11649 
11650 		sin6 = (sin6_t *)sin;
11651 		v6mask = sin6->sin6_addr;
11652 	} else {
11653 		ipaddr_t mask;
11654 
11655 		mask = sin->sin_addr.s_addr;
11656 		V4MASK_TO_V6(mask, v6mask);
11657 	}
11658 
11659 	ipif->ipif_v6net_mask = v6mask;
11660 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11661 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11662 		    ipif->ipif_v6subnet);
11663 	}
11664 	err = ipif_up(ipif, q, mp);
11665 
11666 	if (err == 0 || err == EINPROGRESS) {
11667 		/*
11668 		 * The interface must be DL_BOUND if this packet has to
11669 		 * go out on the wire. Since we only go through a logical
11670 		 * down and are bound with the driver during an internal
11671 		 * down/up that is satisfied.
11672 		 */
11673 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11674 			/* Potentially broadcast an address mask reply. */
11675 			ipif_mask_reply(ipif);
11676 		}
11677 	}
11678 	return (err);
11679 }
11680 
11681 /* ARGSUSED */
11682 int
11683 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11684     ip_ioctl_cmd_t *ipip, void *if_req)
11685 {
11686 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11687 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11688 	ipif_down_tail(ipif);
11689 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11690 }
11691 
11692 /* Get interface net mask. */
11693 /* ARGSUSED */
11694 int
11695 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11696     ip_ioctl_cmd_t *ipip, void *if_req)
11697 {
11698 	struct lifreq *lifr = (struct lifreq *)if_req;
11699 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11700 
11701 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11702 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11703 
11704 	/*
11705 	 * net mask can't change since we have a reference to the ipif.
11706 	 */
11707 	if (ipif->ipif_isv6) {
11708 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11709 		*sin6 = sin6_null;
11710 		sin6->sin6_family = AF_INET6;
11711 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11712 		lifr->lifr_addrlen =
11713 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11714 	} else {
11715 		*sin = sin_null;
11716 		sin->sin_family = AF_INET;
11717 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11718 		if (ipip->ipi_cmd_type == LIF_CMD) {
11719 			lifr->lifr_addrlen =
11720 			    ip_mask_to_plen(ipif->ipif_net_mask);
11721 		}
11722 	}
11723 	return (0);
11724 }
11725 
11726 /* ARGSUSED */
11727 int
11728 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11729     ip_ioctl_cmd_t *ipip, void *if_req)
11730 {
11731 
11732 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11733 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11734 	/*
11735 	 * Set interface metric.  We don't use this for
11736 	 * anything but we keep track of it in case it is
11737 	 * important to routing applications or such.
11738 	 */
11739 	if (ipip->ipi_cmd_type == IF_CMD) {
11740 		struct ifreq    *ifr;
11741 
11742 		ifr = (struct ifreq *)if_req;
11743 		ipif->ipif_metric = ifr->ifr_metric;
11744 	} else {
11745 		struct lifreq   *lifr;
11746 
11747 		lifr = (struct lifreq *)if_req;
11748 		ipif->ipif_metric = lifr->lifr_metric;
11749 	}
11750 	return (0);
11751 }
11752 
11753 
11754 /* ARGSUSED */
11755 int
11756 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11757     ip_ioctl_cmd_t *ipip, void *if_req)
11758 {
11759 
11760 	/* Get interface metric. */
11761 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11762 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11763 	if (ipip->ipi_cmd_type == IF_CMD) {
11764 		struct ifreq    *ifr;
11765 
11766 		ifr = (struct ifreq *)if_req;
11767 		ifr->ifr_metric = ipif->ipif_metric;
11768 	} else {
11769 		struct lifreq   *lifr;
11770 
11771 		lifr = (struct lifreq *)if_req;
11772 		lifr->lifr_metric = ipif->ipif_metric;
11773 	}
11774 
11775 	return (0);
11776 }
11777 
11778 /* ARGSUSED */
11779 int
11780 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11781     ip_ioctl_cmd_t *ipip, void *if_req)
11782 {
11783 
11784 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11785 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11786 	/*
11787 	 * Set the muxid returned from I_PLINK.
11788 	 */
11789 	if (ipip->ipi_cmd_type == IF_CMD) {
11790 		struct ifreq *ifr = (struct ifreq *)if_req;
11791 
11792 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11793 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11794 	} else {
11795 		struct lifreq *lifr = (struct lifreq *)if_req;
11796 
11797 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11798 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11799 	}
11800 	return (0);
11801 }
11802 
11803 /* ARGSUSED */
11804 int
11805 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11806     ip_ioctl_cmd_t *ipip, void *if_req)
11807 {
11808 
11809 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11810 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11811 	/*
11812 	 * Get the muxid saved in ill for I_PUNLINK.
11813 	 */
11814 	if (ipip->ipi_cmd_type == IF_CMD) {
11815 		struct ifreq *ifr = (struct ifreq *)if_req;
11816 
11817 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11818 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11819 	} else {
11820 		struct lifreq *lifr = (struct lifreq *)if_req;
11821 
11822 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11823 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11824 	}
11825 	return (0);
11826 }
11827 
11828 /*
11829  * Set the subnet prefix. Does not modify the broadcast address.
11830  */
11831 /* ARGSUSED */
11832 int
11833 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11834     ip_ioctl_cmd_t *ipip, void *if_req)
11835 {
11836 	int err = 0;
11837 	in6_addr_t v6addr;
11838 	in6_addr_t v6mask;
11839 	boolean_t need_up = B_FALSE;
11840 	int addrlen;
11841 
11842 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11843 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11844 
11845 	ASSERT(IAM_WRITER_IPIF(ipif));
11846 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11847 
11848 	if (ipif->ipif_isv6) {
11849 		sin6_t *sin6;
11850 
11851 		if (sin->sin_family != AF_INET6)
11852 			return (EAFNOSUPPORT);
11853 
11854 		sin6 = (sin6_t *)sin;
11855 		v6addr = sin6->sin6_addr;
11856 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11857 			return (EADDRNOTAVAIL);
11858 	} else {
11859 		ipaddr_t addr;
11860 
11861 		if (sin->sin_family != AF_INET)
11862 			return (EAFNOSUPPORT);
11863 
11864 		addr = sin->sin_addr.s_addr;
11865 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11866 			return (EADDRNOTAVAIL);
11867 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11868 		/* Add 96 bits */
11869 		addrlen += IPV6_ABITS - IP_ABITS;
11870 	}
11871 
11872 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11873 		return (EINVAL);
11874 
11875 	/* Check if bits in the address is set past the mask */
11876 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11877 		return (EINVAL);
11878 
11879 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11880 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11881 		return (0);	/* No change */
11882 
11883 	if (ipif->ipif_flags & IPIF_UP) {
11884 		/*
11885 		 * If the interface is already marked up,
11886 		 * we call ipif_down which will take care
11887 		 * of ditching any IREs that have been set
11888 		 * up based on the old interface address.
11889 		 */
11890 		err = ipif_logical_down(ipif, q, mp);
11891 		if (err == EINPROGRESS)
11892 			return (err);
11893 		ipif_down_tail(ipif);
11894 		need_up = B_TRUE;
11895 	}
11896 
11897 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11898 	return (err);
11899 }
11900 
11901 static int
11902 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11903     queue_t *q, mblk_t *mp, boolean_t need_up)
11904 {
11905 	ill_t	*ill = ipif->ipif_ill;
11906 	int	err = 0;
11907 
11908 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11909 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11910 
11911 	/* Set the new address. */
11912 	mutex_enter(&ill->ill_lock);
11913 	ipif->ipif_v6net_mask = v6mask;
11914 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11915 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11916 		    ipif->ipif_v6subnet);
11917 	}
11918 	mutex_exit(&ill->ill_lock);
11919 
11920 	if (need_up) {
11921 		/*
11922 		 * Now bring the interface back up.  If this
11923 		 * is the only IPIF for the ILL, ipif_up
11924 		 * will have to re-bind to the device, so
11925 		 * we may get back EINPROGRESS, in which
11926 		 * case, this IOCTL will get completed in
11927 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11928 		 */
11929 		err = ipif_up(ipif, q, mp);
11930 		if (err == EINPROGRESS)
11931 			return (err);
11932 	}
11933 	return (err);
11934 }
11935 
11936 /* ARGSUSED */
11937 int
11938 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11939     ip_ioctl_cmd_t *ipip, void *if_req)
11940 {
11941 	int	addrlen;
11942 	in6_addr_t v6addr;
11943 	in6_addr_t v6mask;
11944 	struct lifreq *lifr = (struct lifreq *)if_req;
11945 
11946 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11947 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11948 	ipif_down_tail(ipif);
11949 
11950 	addrlen = lifr->lifr_addrlen;
11951 	if (ipif->ipif_isv6) {
11952 		sin6_t *sin6;
11953 
11954 		sin6 = (sin6_t *)sin;
11955 		v6addr = sin6->sin6_addr;
11956 	} else {
11957 		ipaddr_t addr;
11958 
11959 		addr = sin->sin_addr.s_addr;
11960 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11961 		addrlen += IPV6_ABITS - IP_ABITS;
11962 	}
11963 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11964 
11965 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11966 }
11967 
11968 /* ARGSUSED */
11969 int
11970 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11971     ip_ioctl_cmd_t *ipip, void *if_req)
11972 {
11973 	struct lifreq *lifr = (struct lifreq *)if_req;
11974 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11975 
11976 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11977 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11978 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11979 
11980 	if (ipif->ipif_isv6) {
11981 		*sin6 = sin6_null;
11982 		sin6->sin6_family = AF_INET6;
11983 		sin6->sin6_addr = ipif->ipif_v6subnet;
11984 		lifr->lifr_addrlen =
11985 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11986 	} else {
11987 		*sin = sin_null;
11988 		sin->sin_family = AF_INET;
11989 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11990 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11991 	}
11992 	return (0);
11993 }
11994 
11995 /*
11996  * Set the IPv6 address token.
11997  */
11998 /* ARGSUSED */
11999 int
12000 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12001     ip_ioctl_cmd_t *ipi, void *if_req)
12002 {
12003 	ill_t *ill = ipif->ipif_ill;
12004 	int err;
12005 	in6_addr_t v6addr;
12006 	in6_addr_t v6mask;
12007 	boolean_t need_up = B_FALSE;
12008 	int i;
12009 	sin6_t *sin6 = (sin6_t *)sin;
12010 	struct lifreq *lifr = (struct lifreq *)if_req;
12011 	int addrlen;
12012 
12013 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12014 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12015 	ASSERT(IAM_WRITER_IPIF(ipif));
12016 
12017 	addrlen = lifr->lifr_addrlen;
12018 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12019 	if (ipif->ipif_id != 0)
12020 		return (EINVAL);
12021 
12022 	if (!ipif->ipif_isv6)
12023 		return (EINVAL);
12024 
12025 	if (addrlen > IPV6_ABITS)
12026 		return (EINVAL);
12027 
12028 	v6addr = sin6->sin6_addr;
12029 
12030 	/*
12031 	 * The length of the token is the length from the end.  To get
12032 	 * the proper mask for this, compute the mask of the bits not
12033 	 * in the token; ie. the prefix, and then xor to get the mask.
12034 	 */
12035 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12036 		return (EINVAL);
12037 	for (i = 0; i < 4; i++) {
12038 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12039 	}
12040 
12041 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12042 	    ill->ill_token_length == addrlen)
12043 		return (0);	/* No change */
12044 
12045 	if (ipif->ipif_flags & IPIF_UP) {
12046 		err = ipif_logical_down(ipif, q, mp);
12047 		if (err == EINPROGRESS)
12048 			return (err);
12049 		ipif_down_tail(ipif);
12050 		need_up = B_TRUE;
12051 	}
12052 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12053 	return (err);
12054 }
12055 
12056 static int
12057 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12058     mblk_t *mp, boolean_t need_up)
12059 {
12060 	in6_addr_t v6addr;
12061 	in6_addr_t v6mask;
12062 	ill_t	*ill = ipif->ipif_ill;
12063 	int	i;
12064 	int	err = 0;
12065 
12066 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12067 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12068 	v6addr = sin6->sin6_addr;
12069 	/*
12070 	 * The length of the token is the length from the end.  To get
12071 	 * the proper mask for this, compute the mask of the bits not
12072 	 * in the token; ie. the prefix, and then xor to get the mask.
12073 	 */
12074 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12075 	for (i = 0; i < 4; i++)
12076 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12077 
12078 	mutex_enter(&ill->ill_lock);
12079 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12080 	ill->ill_token_length = addrlen;
12081 	mutex_exit(&ill->ill_lock);
12082 
12083 	if (need_up) {
12084 		/*
12085 		 * Now bring the interface back up.  If this
12086 		 * is the only IPIF for the ILL, ipif_up
12087 		 * will have to re-bind to the device, so
12088 		 * we may get back EINPROGRESS, in which
12089 		 * case, this IOCTL will get completed in
12090 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12091 		 */
12092 		err = ipif_up(ipif, q, mp);
12093 		if (err == EINPROGRESS)
12094 			return (err);
12095 	}
12096 	return (err);
12097 }
12098 
12099 /* ARGSUSED */
12100 int
12101 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12102     ip_ioctl_cmd_t *ipi, void *if_req)
12103 {
12104 	ill_t *ill;
12105 	sin6_t *sin6 = (sin6_t *)sin;
12106 	struct lifreq *lifr = (struct lifreq *)if_req;
12107 
12108 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12109 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12110 	if (ipif->ipif_id != 0)
12111 		return (EINVAL);
12112 
12113 	ill = ipif->ipif_ill;
12114 	if (!ill->ill_isv6)
12115 		return (ENXIO);
12116 
12117 	*sin6 = sin6_null;
12118 	sin6->sin6_family = AF_INET6;
12119 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12120 	sin6->sin6_addr = ill->ill_token;
12121 	lifr->lifr_addrlen = ill->ill_token_length;
12122 	return (0);
12123 }
12124 
12125 /*
12126  * Set (hardware) link specific information that might override
12127  * what was acquired through the DL_INFO_ACK.
12128  * The logic is as follows.
12129  *
12130  * become exclusive
12131  * set CHANGING flag
12132  * change mtu on affected IREs
12133  * clear CHANGING flag
12134  *
12135  * An ire add that occurs before the CHANGING flag is set will have its mtu
12136  * changed by the ip_sioctl_lnkinfo.
12137  *
12138  * During the time the CHANGING flag is set, no new ires will be added to the
12139  * bucket, and ire add will fail (due the CHANGING flag).
12140  *
12141  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12142  * before it is added to the bucket.
12143  *
12144  * Obviously only 1 thread can set the CHANGING flag and we need to become
12145  * exclusive to set the flag.
12146  */
12147 /* ARGSUSED */
12148 int
12149 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12150     ip_ioctl_cmd_t *ipi, void *if_req)
12151 {
12152 	ill_t		*ill = ipif->ipif_ill;
12153 	ipif_t		*nipif;
12154 	int		ip_min_mtu;
12155 	boolean_t	mtu_walk = B_FALSE;
12156 	struct lifreq	*lifr = (struct lifreq *)if_req;
12157 	lif_ifinfo_req_t *lir;
12158 	ire_t		*ire;
12159 
12160 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12161 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12162 	lir = &lifr->lifr_ifinfo;
12163 	ASSERT(IAM_WRITER_IPIF(ipif));
12164 
12165 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12166 	if (ipif->ipif_id != 0)
12167 		return (EINVAL);
12168 
12169 	/* Set interface MTU. */
12170 	if (ipif->ipif_isv6)
12171 		ip_min_mtu = IPV6_MIN_MTU;
12172 	else
12173 		ip_min_mtu = IP_MIN_MTU;
12174 
12175 	/*
12176 	 * Verify values before we set anything. Allow zero to
12177 	 * mean unspecified.
12178 	 */
12179 	if (lir->lir_maxmtu != 0 &&
12180 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12181 	    lir->lir_maxmtu < ip_min_mtu))
12182 		return (EINVAL);
12183 	if (lir->lir_reachtime != 0 &&
12184 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12185 		return (EINVAL);
12186 	if (lir->lir_reachretrans != 0 &&
12187 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12188 		return (EINVAL);
12189 
12190 	mutex_enter(&ill->ill_lock);
12191 	ill->ill_state_flags |= ILL_CHANGING;
12192 	for (nipif = ill->ill_ipif; nipif != NULL;
12193 	    nipif = nipif->ipif_next) {
12194 		nipif->ipif_state_flags |= IPIF_CHANGING;
12195 	}
12196 
12197 	mutex_exit(&ill->ill_lock);
12198 
12199 	if (lir->lir_maxmtu != 0) {
12200 		ill->ill_max_mtu = lir->lir_maxmtu;
12201 		ill->ill_mtu_userspecified = 1;
12202 		mtu_walk = B_TRUE;
12203 	}
12204 
12205 	if (lir->lir_reachtime != 0)
12206 		ill->ill_reachable_time = lir->lir_reachtime;
12207 
12208 	if (lir->lir_reachretrans != 0)
12209 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12210 
12211 	ill->ill_max_hops = lir->lir_maxhops;
12212 
12213 	ill->ill_max_buf = ND_MAX_Q;
12214 
12215 	if (mtu_walk) {
12216 		/*
12217 		 * Set the MTU on all ipifs associated with this ill except
12218 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12219 		 */
12220 		for (nipif = ill->ill_ipif; nipif != NULL;
12221 		    nipif = nipif->ipif_next) {
12222 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12223 				continue;
12224 
12225 			nipif->ipif_mtu = ill->ill_max_mtu;
12226 
12227 			if (!(nipif->ipif_flags & IPIF_UP))
12228 				continue;
12229 
12230 			if (nipif->ipif_isv6)
12231 				ire = ipif_to_ire_v6(nipif);
12232 			else
12233 				ire = ipif_to_ire(nipif);
12234 			if (ire != NULL) {
12235 				ire->ire_max_frag = ipif->ipif_mtu;
12236 				ire_refrele(ire);
12237 			}
12238 			if (ill->ill_isv6) {
12239 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12240 				    ipif_mtu_change, (char *)nipif,
12241 				    ill);
12242 			} else {
12243 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12244 				    ipif_mtu_change, (char *)nipif,
12245 				    ill);
12246 			}
12247 		}
12248 	}
12249 
12250 	mutex_enter(&ill->ill_lock);
12251 	for (nipif = ill->ill_ipif; nipif != NULL;
12252 	    nipif = nipif->ipif_next) {
12253 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12254 	}
12255 	ILL_UNMARK_CHANGING(ill);
12256 	mutex_exit(&ill->ill_lock);
12257 
12258 	return (0);
12259 }
12260 
12261 /* ARGSUSED */
12262 int
12263 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12264     ip_ioctl_cmd_t *ipi, void *if_req)
12265 {
12266 	struct lif_ifinfo_req *lir;
12267 	ill_t *ill = ipif->ipif_ill;
12268 
12269 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12270 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12271 	if (ipif->ipif_id != 0)
12272 		return (EINVAL);
12273 
12274 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12275 	lir->lir_maxhops = ill->ill_max_hops;
12276 	lir->lir_reachtime = ill->ill_reachable_time;
12277 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12278 	lir->lir_maxmtu = ill->ill_max_mtu;
12279 
12280 	return (0);
12281 }
12282 
12283 /*
12284  * Return best guess as to the subnet mask for the specified address.
12285  * Based on the subnet masks for all the configured interfaces.
12286  *
12287  * We end up returning a zero mask in the case of default, multicast or
12288  * experimental.
12289  */
12290 static ipaddr_t
12291 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12292 {
12293 	ipaddr_t net_mask;
12294 	ill_t	*ill;
12295 	ipif_t	*ipif;
12296 	ill_walk_context_t ctx;
12297 	ipif_t	*fallback_ipif = NULL;
12298 
12299 	net_mask = ip_net_mask(addr);
12300 	if (net_mask == 0) {
12301 		*ipifp = NULL;
12302 		return (0);
12303 	}
12304 
12305 	/* Let's check to see if this is maybe a local subnet route. */
12306 	/* this function only applies to IPv4 interfaces */
12307 	rw_enter(&ill_g_lock, RW_READER);
12308 	ill = ILL_START_WALK_V4(&ctx);
12309 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12310 		mutex_enter(&ill->ill_lock);
12311 		for (ipif = ill->ill_ipif; ipif != NULL;
12312 		    ipif = ipif->ipif_next) {
12313 			if (!IPIF_CAN_LOOKUP(ipif))
12314 				continue;
12315 			if (!(ipif->ipif_flags & IPIF_UP))
12316 				continue;
12317 			if ((ipif->ipif_subnet & net_mask) ==
12318 			    (addr & net_mask)) {
12319 				/*
12320 				 * Don't trust pt-pt interfaces if there are
12321 				 * other interfaces.
12322 				 */
12323 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12324 					if (fallback_ipif == NULL) {
12325 						ipif_refhold_locked(ipif);
12326 						fallback_ipif = ipif;
12327 					}
12328 					continue;
12329 				}
12330 
12331 				/*
12332 				 * Fine. Just assume the same net mask as the
12333 				 * directly attached subnet interface is using.
12334 				 */
12335 				ipif_refhold_locked(ipif);
12336 				mutex_exit(&ill->ill_lock);
12337 				rw_exit(&ill_g_lock);
12338 				if (fallback_ipif != NULL)
12339 					ipif_refrele(fallback_ipif);
12340 				*ipifp = ipif;
12341 				return (ipif->ipif_net_mask);
12342 			}
12343 		}
12344 		mutex_exit(&ill->ill_lock);
12345 	}
12346 	rw_exit(&ill_g_lock);
12347 
12348 	*ipifp = fallback_ipif;
12349 	return ((fallback_ipif != NULL) ?
12350 	    fallback_ipif->ipif_net_mask : net_mask);
12351 }
12352 
12353 /*
12354  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12355  */
12356 static void
12357 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12358 {
12359 	IOCP	iocp;
12360 	ipft_t	*ipft;
12361 	ipllc_t	*ipllc;
12362 	mblk_t	*mp1;
12363 	cred_t	*cr;
12364 	int	error = 0;
12365 	conn_t	*connp;
12366 
12367 	ip1dbg(("ip_wput_ioctl"));
12368 	iocp = (IOCP)mp->b_rptr;
12369 	mp1 = mp->b_cont;
12370 	if (mp1 == NULL) {
12371 		iocp->ioc_error = EINVAL;
12372 		mp->b_datap->db_type = M_IOCNAK;
12373 		iocp->ioc_count = 0;
12374 		qreply(q, mp);
12375 		return;
12376 	}
12377 
12378 	/*
12379 	 * These IOCTLs provide various control capabilities to
12380 	 * upstream agents such as ULPs and processes.	There
12381 	 * are currently two such IOCTLs implemented.  They
12382 	 * are used by TCP to provide update information for
12383 	 * existing IREs and to forcibly delete an IRE for a
12384 	 * host that is not responding, thereby forcing an
12385 	 * attempt at a new route.
12386 	 */
12387 	iocp->ioc_error = EINVAL;
12388 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12389 		goto done;
12390 
12391 	ipllc = (ipllc_t *)mp1->b_rptr;
12392 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12393 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12394 			break;
12395 	}
12396 	/*
12397 	 * prefer credential from mblk over ioctl;
12398 	 * see ip_sioctl_copyin_setup
12399 	 */
12400 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12401 
12402 	/*
12403 	 * Refhold the conn in case the request gets queued up in some lookup
12404 	 */
12405 	ASSERT(CONN_Q(q));
12406 	connp = Q_TO_CONN(q);
12407 	CONN_INC_REF(connp);
12408 	if (ipft->ipft_pfi &&
12409 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12410 		pullupmsg(mp1, ipft->ipft_min_size))) {
12411 		error = (*ipft->ipft_pfi)(q,
12412 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12413 	}
12414 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12415 		/*
12416 		 * CONN_OPER_PENDING_DONE happens in the function called
12417 		 * through ipft_pfi above.
12418 		 */
12419 		return;
12420 	}
12421 
12422 	CONN_OPER_PENDING_DONE(connp);
12423 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12424 		freemsg(mp);
12425 		return;
12426 	}
12427 	iocp->ioc_error = error;
12428 
12429 done:
12430 	mp->b_datap->db_type = M_IOCACK;
12431 	if (iocp->ioc_error)
12432 		iocp->ioc_count = 0;
12433 	qreply(q, mp);
12434 }
12435 
12436 /*
12437  * Lookup an ipif using the sequence id (ipif_seqid)
12438  */
12439 ipif_t *
12440 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12441 {
12442 	ipif_t *ipif;
12443 
12444 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12445 
12446 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12447 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12448 			return (ipif);
12449 	}
12450 	return (NULL);
12451 }
12452 
12453 uint64_t ipif_g_seqid;
12454 
12455 /*
12456  * Assign a unique id for the ipif. This is used later when we send
12457  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12458  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12459  * IRE is added, we verify that ipif has not disappeared.
12460  */
12461 
12462 static void
12463 ipif_assign_seqid(ipif_t *ipif)
12464 {
12465 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12466 }
12467 
12468 /*
12469  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12470  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12471  * be inserted into the first space available in the list. The value of
12472  * ipif_id will then be set to the appropriate value for its position.
12473  */
12474 static int
12475 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12476 {
12477 	ill_t *ill;
12478 	ipif_t *tipif;
12479 	ipif_t **tipifp;
12480 	int id;
12481 
12482 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12483 	    IAM_WRITER_IPIF(ipif));
12484 
12485 	ill = ipif->ipif_ill;
12486 	ASSERT(ill != NULL);
12487 
12488 	/*
12489 	 * In the case of lo0:0 we already hold the ill_g_lock.
12490 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12491 	 * ipif_insert. Another such caller is ipif_move.
12492 	 */
12493 	if (acquire_g_lock)
12494 		rw_enter(&ill_g_lock, RW_WRITER);
12495 	if (acquire_ill_lock)
12496 		mutex_enter(&ill->ill_lock);
12497 	id = ipif->ipif_id;
12498 	tipifp = &(ill->ill_ipif);
12499 	if (id == -1) {	/* need to find a real id */
12500 		id = 0;
12501 		while ((tipif = *tipifp) != NULL) {
12502 			ASSERT(tipif->ipif_id >= id);
12503 			if (tipif->ipif_id != id)
12504 				break; /* non-consecutive id */
12505 			id++;
12506 			tipifp = &(tipif->ipif_next);
12507 		}
12508 		/* limit number of logical interfaces */
12509 		if (id >= ip_addrs_per_if) {
12510 			if (acquire_ill_lock)
12511 				mutex_exit(&ill->ill_lock);
12512 			if (acquire_g_lock)
12513 				rw_exit(&ill_g_lock);
12514 			return (-1);
12515 		}
12516 		ipif->ipif_id = id; /* assign new id */
12517 	} else if (id < ip_addrs_per_if) {
12518 		/* we have a real id; insert ipif in the right place */
12519 		while ((tipif = *tipifp) != NULL) {
12520 			ASSERT(tipif->ipif_id != id);
12521 			if (tipif->ipif_id > id)
12522 				break; /* found correct location */
12523 			tipifp = &(tipif->ipif_next);
12524 		}
12525 	} else {
12526 		if (acquire_ill_lock)
12527 			mutex_exit(&ill->ill_lock);
12528 		if (acquire_g_lock)
12529 			rw_exit(&ill_g_lock);
12530 		return (-1);
12531 	}
12532 
12533 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12534 
12535 	ipif->ipif_next = tipif;
12536 	*tipifp = ipif;
12537 	if (acquire_ill_lock)
12538 		mutex_exit(&ill->ill_lock);
12539 	if (acquire_g_lock)
12540 		rw_exit(&ill_g_lock);
12541 	return (0);
12542 }
12543 
12544 /*
12545  * Allocate and initialize a new interface control structure.  (Always
12546  * called as writer.)
12547  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12548  * is not part of the global linked list of ills. ipif_seqid is unique
12549  * in the system and to preserve the uniqueness, it is assigned only
12550  * when ill becomes part of the global list. At that point ill will
12551  * have a name. If it doesn't get assigned here, it will get assigned
12552  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12553  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12554  * the interface flags or any other information from the DL_INFO_ACK for
12555  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12556  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12557  * second DL_INFO_ACK comes in from the driver.
12558  */
12559 static ipif_t *
12560 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12561 {
12562 	ipif_t	*ipif;
12563 	phyint_t *phyi;
12564 
12565 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12566 	    ill->ill_name, id, (void *)ill));
12567 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12568 
12569 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12570 		return (NULL);
12571 	*ipif = ipif_zero;	/* start clean */
12572 
12573 	ipif->ipif_ill = ill;
12574 	ipif->ipif_id = id;	/* could be -1 */
12575 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12576 
12577 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12578 
12579 	ipif->ipif_refcnt = 0;
12580 	ipif->ipif_saved_ire_cnt = 0;
12581 
12582 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12583 		mi_free(ipif);
12584 		return (NULL);
12585 	}
12586 	/* -1 id should have been replaced by real id */
12587 	id = ipif->ipif_id;
12588 	ASSERT(id >= 0);
12589 
12590 	if (ill->ill_name[0] != '\0') {
12591 		ipif_assign_seqid(ipif);
12592 		if (ill->ill_phyint->phyint_ifindex != 0)
12593 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12594 	}
12595 	/*
12596 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12597 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12598 	 * ioctl sets ipif_orig_ipifid to zero.
12599 	 */
12600 	ipif->ipif_orig_ipifid = id;
12601 
12602 	/*
12603 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12604 	 * The ipif is still not up and can't be looked up until the
12605 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12606 	 */
12607 	mutex_enter(&ill->ill_lock);
12608 	mutex_enter(&ill->ill_phyint->phyint_lock);
12609 	/*
12610 	 * Set the running flag when logical interface zero is created.
12611 	 * For subsequent logical interfaces, a DLPI link down
12612 	 * notification message may have cleared the running flag to
12613 	 * indicate the link is down, so we shouldn't just blindly set it.
12614 	 */
12615 	if (id == 0)
12616 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12617 	ipif->ipif_ire_type = ire_type;
12618 	phyi = ill->ill_phyint;
12619 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12620 
12621 	if (ipif->ipif_isv6) {
12622 		ill->ill_flags |= ILLF_IPV6;
12623 	} else {
12624 		ipaddr_t inaddr_any = INADDR_ANY;
12625 
12626 		ill->ill_flags |= ILLF_IPV4;
12627 
12628 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12629 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12630 		    &ipif->ipif_v6lcl_addr);
12631 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12632 		    &ipif->ipif_v6src_addr);
12633 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12634 		    &ipif->ipif_v6subnet);
12635 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12636 		    &ipif->ipif_v6net_mask);
12637 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12638 		    &ipif->ipif_v6brd_addr);
12639 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12640 		    &ipif->ipif_v6pp_dst_addr);
12641 	}
12642 
12643 	/*
12644 	 * Don't set the interface flags etc. now, will do it in
12645 	 * ip_ll_subnet_defaults.
12646 	 */
12647 	if (!initialize) {
12648 		mutex_exit(&ill->ill_lock);
12649 		mutex_exit(&ill->ill_phyint->phyint_lock);
12650 		return (ipif);
12651 	}
12652 	ipif->ipif_mtu = ill->ill_max_mtu;
12653 
12654 	if (ill->ill_bcast_addr_length != 0) {
12655 		/*
12656 		 * Later detect lack of DLPI driver multicast
12657 		 * capability by catching DL_ENABMULTI errors in
12658 		 * ip_rput_dlpi.
12659 		 */
12660 		ill->ill_flags |= ILLF_MULTICAST;
12661 		if (!ipif->ipif_isv6)
12662 			ipif->ipif_flags |= IPIF_BROADCAST;
12663 	} else {
12664 		if (ill->ill_net_type != IRE_LOOPBACK) {
12665 			if (ipif->ipif_isv6)
12666 				/*
12667 				 * Note: xresolv interfaces will eventually need
12668 				 * NOARP set here as well, but that will require
12669 				 * those external resolvers to have some
12670 				 * knowledge of that flag and act appropriately.
12671 				 * Not to be changed at present.
12672 				 */
12673 				ill->ill_flags |= ILLF_NONUD;
12674 			else
12675 				ill->ill_flags |= ILLF_NOARP;
12676 		}
12677 		if (ill->ill_phys_addr_length == 0) {
12678 			if (ill->ill_media &&
12679 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12680 				ipif->ipif_flags |= IPIF_NOXMIT;
12681 				phyi->phyint_flags |= PHYI_VIRTUAL;
12682 			} else {
12683 				/* pt-pt supports multicast. */
12684 				ill->ill_flags |= ILLF_MULTICAST;
12685 				if (ill->ill_net_type == IRE_LOOPBACK) {
12686 					phyi->phyint_flags |=
12687 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12688 				} else {
12689 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12690 				}
12691 			}
12692 		}
12693 	}
12694 	mutex_exit(&ill->ill_lock);
12695 	mutex_exit(&ill->ill_phyint->phyint_lock);
12696 	return (ipif);
12697 }
12698 
12699 /*
12700  * If appropriate, send a message up to the resolver delete the entry
12701  * for the address of this interface which is going out of business.
12702  * (Always called as writer).
12703  *
12704  * NOTE : We need to check for NULL mps as some of the fields are
12705  *	  initialized only for some interface types. See ipif_resolver_up()
12706  *	  for details.
12707  */
12708 void
12709 ipif_arp_down(ipif_t *ipif)
12710 {
12711 	mblk_t	*mp;
12712 
12713 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12714 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12715 	ASSERT(IAM_WRITER_IPIF(ipif));
12716 
12717 	/* Delete the mapping for the local address */
12718 	mp = ipif->ipif_arp_del_mp;
12719 	if (mp != NULL) {
12720 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12721 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12722 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12723 		putnext(ipif->ipif_ill->ill_rq, mp);
12724 		ipif->ipif_arp_del_mp = NULL;
12725 	}
12726 
12727 	/*
12728 	 * If this is the last ipif that is going down, we need
12729 	 * to clean up ARP completely.
12730 	 */
12731 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12732 
12733 		/* Send up AR_INTERFACE_DOWN message */
12734 		mp = ipif->ipif_ill->ill_arp_down_mp;
12735 		if (mp != NULL) {
12736 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12737 			    dlpi_prim_str(*(int *)mp->b_rptr),
12738 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12739 			    ipif->ipif_id));
12740 			putnext(ipif->ipif_ill->ill_rq, mp);
12741 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12742 		}
12743 
12744 		/* Tell ARP to delete the multicast mappings */
12745 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12746 		if (mp != NULL) {
12747 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12748 			    dlpi_prim_str(*(int *)mp->b_rptr),
12749 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12750 			    ipif->ipif_id));
12751 			putnext(ipif->ipif_ill->ill_rq, mp);
12752 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12753 		}
12754 	}
12755 }
12756 
12757 /*
12758  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12759  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12760  * that it wants the add_mp allocated in this function to be returned
12761  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12762  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12763  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12764  * as it does a ipif_arp_down after calling this function - which will
12765  * remove what we add here.
12766  *
12767  * Returns -1 on failures and 0 on success.
12768  */
12769 int
12770 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12771 {
12772 	mblk_t	*del_mp = NULL;
12773 	mblk_t *add_mp = NULL;
12774 	mblk_t *mp;
12775 	ill_t	*ill = ipif->ipif_ill;
12776 	phyint_t *phyi = ill->ill_phyint;
12777 	ipaddr_t addr, mask, extract_mask = 0;
12778 	arma_t	*arma;
12779 	uint8_t *maddr, *bphys_addr;
12780 	uint32_t hw_start;
12781 	dl_unitdata_req_t *dlur;
12782 
12783 	ASSERT(IAM_WRITER_IPIF(ipif));
12784 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12785 		return (0);
12786 
12787 	/*
12788 	 * Delete the existing mapping from ARP. Normally ipif_down
12789 	 * -> ipif_arp_down should send this up to ARP. The only
12790 	 * reason we would find this when we are switching from
12791 	 * Multicast to Broadcast where we did not do a down.
12792 	 */
12793 	mp = ill->ill_arp_del_mapping_mp;
12794 	if (mp != NULL) {
12795 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12796 		    dlpi_prim_str(*(int *)mp->b_rptr),
12797 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12798 		putnext(ill->ill_rq, mp);
12799 		ill->ill_arp_del_mapping_mp = NULL;
12800 	}
12801 
12802 	if (arp_add_mapping_mp != NULL)
12803 		*arp_add_mapping_mp = NULL;
12804 
12805 	/*
12806 	 * Check that the address is not to long for the constant
12807 	 * length reserved in the template arma_t.
12808 	 */
12809 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12810 		return (-1);
12811 
12812 	/* Add mapping mblk */
12813 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
12814 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
12815 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
12816 	    (caddr_t)&addr);
12817 	if (add_mp == NULL)
12818 		return (-1);
12819 	arma = (arma_t *)add_mp->b_rptr;
12820 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
12821 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
12822 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
12823 
12824 	/*
12825 	 * Determine the broadcast address.
12826 	 */
12827 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
12828 	if (ill->ill_sap_length < 0)
12829 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
12830 	else
12831 		bphys_addr = (uchar_t *)dlur +
12832 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
12833 	/*
12834 	 * Check PHYI_MULTI_BCAST and length of physical
12835 	 * address to determine if we use the mapping or the
12836 	 * broadcast address.
12837 	 */
12838 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
12839 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
12840 		    bphys_addr, maddr, &hw_start, &extract_mask))
12841 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
12842 
12843 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
12844 	    (ill->ill_flags & ILLF_MULTICAST)) {
12845 		/* Make sure this will not match the "exact" entry. */
12846 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
12847 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
12848 		    (caddr_t)&addr);
12849 		if (del_mp == NULL) {
12850 			freemsg(add_mp);
12851 			return (-1);
12852 		}
12853 		bcopy(&extract_mask, (char *)arma +
12854 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
12855 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
12856 			/* Use link-layer broadcast address for MULTI_BCAST */
12857 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
12858 			ip2dbg(("ipif_arp_setup_multicast: adding"
12859 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
12860 		} else {
12861 			arma->arma_hw_mapping_start = hw_start;
12862 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
12863 			    " ARP setup for %s\n", ill->ill_name));
12864 		}
12865 	} else {
12866 		freemsg(add_mp);
12867 		ASSERT(del_mp == NULL);
12868 		/* It is neither MULTICAST nor MULTI_BCAST */
12869 		return (0);
12870 	}
12871 	ASSERT(add_mp != NULL && del_mp != NULL);
12872 	ill->ill_arp_del_mapping_mp = del_mp;
12873 	if (arp_add_mapping_mp != NULL) {
12874 		/* The caller just wants the mblks allocated */
12875 		*arp_add_mapping_mp = add_mp;
12876 	} else {
12877 		/* The caller wants us to send it to arp */
12878 		putnext(ill->ill_rq, add_mp);
12879 	}
12880 	return (0);
12881 }
12882 
12883 /*
12884  * Get the resolver set up for a new interface address.
12885  * (Always called as writer.)
12886  * Called both for IPv4 and IPv6 interfaces,
12887  * though it only sets up the resolver for v6
12888  * if it's an xresolv interface (one using an external resolver).
12889  * Honors ILLF_NOARP.
12890  * The boolean value arp_just_publish, if B_TRUE, indicates that
12891  * it only needs to send an AR_ENTRY_ADD message up to ARP for
12892  * IPv4 interfaces. Currently, B_TRUE is only set when this
12893  * function is called by ip_rput_dlpi_writer() to handle
12894  * asynchronous hardware address change notification.
12895  * Returns error on failure.
12896  */
12897 int
12898 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
12899 {
12900 	caddr_t	addr;
12901 	mblk_t	*arp_up_mp = NULL;
12902 	mblk_t	*arp_down_mp = NULL;
12903 	mblk_t	*arp_add_mp = NULL;
12904 	mblk_t	*arp_del_mp = NULL;
12905 	mblk_t	*arp_add_mapping_mp = NULL;
12906 	mblk_t	*arp_del_mapping_mp = NULL;
12907 	ill_t	*ill = ipif->ipif_ill;
12908 	uchar_t	*area_p = NULL;
12909 	uchar_t	*ared_p = NULL;
12910 	int	err = ENOMEM;
12911 
12912 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12913 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
12914 	    (uint_t)ipif->ipif_flags));
12915 	ASSERT(IAM_WRITER_IPIF(ipif));
12916 
12917 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
12918 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
12919 		return (0);
12920 	}
12921 
12922 	if (ill->ill_isv6) {
12923 		/*
12924 		 * External resolver for IPv6
12925 		 */
12926 		ASSERT(!arp_just_publish);
12927 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12928 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
12929 			area_p = (uchar_t *)&ip6_area_template;
12930 			ared_p = (uchar_t *)&ip6_ared_template;
12931 		}
12932 	} else {
12933 		/*
12934 		 * IPv4 arp case. If the ARP stream has already started
12935 		 * closing, fail this request for ARP bringup. Else
12936 		 * record the fact that an ARP bringup is pending.
12937 		 */
12938 		mutex_enter(&ill->ill_lock);
12939 		if (ill->ill_arp_closing) {
12940 			mutex_exit(&ill->ill_lock);
12941 			err = EINVAL;
12942 			goto failed;
12943 		} else {
12944 			if (ill->ill_ipif_up_count == 0)
12945 				ill->ill_arp_bringup_pending = 1;
12946 			mutex_exit(&ill->ill_lock);
12947 		}
12948 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
12949 			addr = (caddr_t)&ipif->ipif_lcl_addr;
12950 			area_p = (uchar_t *)&ip_area_template;
12951 			ared_p = (uchar_t *)&ip_ared_template;
12952 		}
12953 	}
12954 
12955 	/*
12956 	 * Add an entry for the local address in ARP only if it
12957 	 * is not UNNUMBERED and the address is not INADDR_ANY.
12958 	 */
12959 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
12960 		/* Now ask ARP to publish our address. */
12961 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
12962 		if (arp_add_mp == NULL)
12963 			goto failed;
12964 		if (arp_just_publish) {
12965 			/*
12966 			 * Copy the new hardware address and length into
12967 			 * arp_add_mp to be sent to ARP.
12968 			 */
12969 			area_t *area = (area_t *)arp_add_mp->b_rptr;
12970 			area->area_hw_addr_length =
12971 			    ill->ill_phys_addr_length;
12972 			bcopy((char *)ill->ill_phys_addr,
12973 			    ((char *)area + area->area_hw_addr_offset),
12974 			    area->area_hw_addr_length);
12975 		}
12976 
12977 		((area_t *)arp_add_mp->b_rptr)->area_flags =
12978 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
12979 
12980 		if (arp_just_publish)
12981 			goto arp_setup_multicast;
12982 
12983 		/*
12984 		 * Allocate an ARP deletion message so we know we can tell ARP
12985 		 * when the interface goes down.
12986 		 */
12987 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
12988 		if (arp_del_mp == NULL)
12989 			goto failed;
12990 
12991 	} else {
12992 		if (arp_just_publish)
12993 			goto done;
12994 	}
12995 	/*
12996 	 * Need to bring up ARP or setup multicast mapping only
12997 	 * when the first interface is coming UP.
12998 	 */
12999 	if (ill->ill_ipif_up_count != 0)
13000 		goto done;
13001 
13002 	/*
13003 	 * Allocate an ARP down message (to be saved) and an ARP up
13004 	 * message.
13005 	 */
13006 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13007 	if (arp_down_mp == NULL)
13008 		goto failed;
13009 
13010 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13011 	if (arp_up_mp == NULL)
13012 		goto failed;
13013 
13014 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13015 		goto done;
13016 
13017 arp_setup_multicast:
13018 	/*
13019 	 * Setup the multicast mappings. This function initializes
13020 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13021 	 * IPv6.
13022 	 */
13023 	if (!ill->ill_isv6) {
13024 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13025 		if (err != 0)
13026 			goto failed;
13027 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13028 		ASSERT(arp_add_mapping_mp != NULL);
13029 	}
13030 
13031 done:;
13032 	if (arp_del_mp != NULL) {
13033 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13034 		ipif->ipif_arp_del_mp = arp_del_mp;
13035 	}
13036 	if (arp_down_mp != NULL) {
13037 		ASSERT(ill->ill_arp_down_mp == NULL);
13038 		ill->ill_arp_down_mp = arp_down_mp;
13039 	}
13040 	if (arp_del_mapping_mp != NULL) {
13041 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13042 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13043 	}
13044 	if (arp_up_mp != NULL) {
13045 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13046 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13047 		putnext(ill->ill_rq, arp_up_mp);
13048 	}
13049 	if (arp_add_mp != NULL) {
13050 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13051 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13052 		putnext(ill->ill_rq, arp_add_mp);
13053 	}
13054 	if (arp_add_mapping_mp != NULL) {
13055 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13056 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13057 		putnext(ill->ill_rq, arp_add_mapping_mp);
13058 	}
13059 	if (arp_just_publish)
13060 		return (0);
13061 
13062 	if (ill->ill_flags & ILLF_NOARP)
13063 		err = ill_arp_off(ill);
13064 	else
13065 		err = ill_arp_on(ill);
13066 	if (err) {
13067 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13068 		freemsg(ipif->ipif_arp_del_mp);
13069 		if (arp_down_mp != NULL)
13070 			freemsg(ill->ill_arp_down_mp);
13071 		if (ill->ill_arp_del_mapping_mp != NULL)
13072 			freemsg(ill->ill_arp_del_mapping_mp);
13073 		ipif->ipif_arp_del_mp = NULL;
13074 		ill->ill_arp_down_mp = NULL;
13075 		ill->ill_arp_del_mapping_mp = NULL;
13076 		return (err);
13077 	}
13078 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
13079 
13080 failed:;
13081 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13082 	freemsg(arp_add_mp);
13083 	freemsg(arp_del_mp);
13084 	freemsg(arp_add_mapping_mp);
13085 	freemsg(arp_up_mp);
13086 	freemsg(arp_down_mp);
13087 	ill->ill_arp_bringup_pending = 0;
13088 	return (err);
13089 }
13090 
13091 /*
13092  * Wakeup all threads waiting to enter the ipsq, and sleeping
13093  * on any of the ills in this ipsq. The ill_lock of the ill
13094  * must be held so that waiters don't miss wakeups
13095  */
13096 static void
13097 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13098 {
13099 	phyint_t *phyint;
13100 
13101 	phyint = ipsq->ipsq_phyint_list;
13102 	while (phyint != NULL) {
13103 		if (phyint->phyint_illv4) {
13104 			if (!caller_holds_lock)
13105 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13106 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13107 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13108 			if (!caller_holds_lock)
13109 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13110 		}
13111 		if (phyint->phyint_illv6) {
13112 			if (!caller_holds_lock)
13113 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13114 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13115 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13116 			if (!caller_holds_lock)
13117 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13118 		}
13119 		phyint = phyint->phyint_ipsq_next;
13120 	}
13121 }
13122 
13123 static ipsq_t *
13124 ipsq_create(char *groupname)
13125 {
13126 	ipsq_t	*ipsq;
13127 
13128 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13129 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13130 	if (ipsq == NULL) {
13131 		return (NULL);
13132 	}
13133 
13134 	if (groupname != NULL)
13135 		(void) strcpy(ipsq->ipsq_name, groupname);
13136 	else
13137 		ipsq->ipsq_name[0] = '\0';
13138 
13139 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13140 	ipsq->ipsq_flags |= IPSQ_GROUP;
13141 	ipsq->ipsq_next = ipsq_g_head;
13142 	ipsq_g_head = ipsq;
13143 	return (ipsq);
13144 }
13145 
13146 /*
13147  * Return an ipsq correspoding to the groupname. If 'create' is true
13148  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13149  * uniquely with an IPMP group. However during IPMP groupname operations,
13150  * multiple IPMP groups may be associated with a single ipsq. But no
13151  * IPMP group can be associated with more than 1 ipsq at any time.
13152  * For example
13153  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13154  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13155  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13156  *
13157  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13158  * status shown below during the execution of the above command.
13159  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13160  *
13161  * After the completion of the above groupname command we return to the stable
13162  * state shown below.
13163  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13164  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13165  *
13166  * Because of the above, we don't search based on the ipsq_name since that
13167  * would miss the correct ipsq during certain windows as shown above.
13168  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13169  * natural state.
13170  */
13171 static ipsq_t *
13172 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13173 {
13174 	ipsq_t	*ipsq;
13175 	int	group_len;
13176 	phyint_t *phyint;
13177 
13178 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13179 
13180 	group_len = strlen(groupname);
13181 	ASSERT(group_len != 0);
13182 	group_len++;
13183 
13184 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13185 		/*
13186 		 * When an ipsq is being split, and ill_split_ipsq
13187 		 * calls this function, we exclude it from being considered.
13188 		 */
13189 		if (ipsq == exclude_ipsq)
13190 			continue;
13191 
13192 		/*
13193 		 * Compare against the ipsq_name. The groupname change happens
13194 		 * in 2 phases. The 1st phase merges the from group into
13195 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13196 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13197 		 * ipsq_name. At this point the phyint_groupname has not been
13198 		 * updated.
13199 		 */
13200 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13201 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13202 			/*
13203 			 * Verify that an ipmp groupname is exactly
13204 			 * part of 1 ipsq and is not found in any other
13205 			 * ipsq.
13206 			 */
13207 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13208 			    NULL);
13209 			return (ipsq);
13210 		}
13211 
13212 		/*
13213 		 * Comparison against ipsq_name alone is not sufficient.
13214 		 * In the case when groups are currently being
13215 		 * merged, the ipsq could hold other IPMP groups temporarily.
13216 		 * so we walk the phyint list and compare against the
13217 		 * phyint_groupname as well.
13218 		 */
13219 		phyint = ipsq->ipsq_phyint_list;
13220 		while (phyint != NULL) {
13221 			if ((group_len == phyint->phyint_groupname_len) &&
13222 			    (bcmp(phyint->phyint_groupname, groupname,
13223 			    group_len) == 0)) {
13224 				/*
13225 				 * Verify that an ipmp groupname is exactly
13226 				 * part of 1 ipsq and is not found in any other
13227 				 * ipsq.
13228 				 */
13229 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13230 					== NULL);
13231 				return (ipsq);
13232 			}
13233 			phyint = phyint->phyint_ipsq_next;
13234 		}
13235 	}
13236 	if (create)
13237 		ipsq = ipsq_create(groupname);
13238 	return (ipsq);
13239 }
13240 
13241 static void
13242 ipsq_delete(ipsq_t *ipsq)
13243 {
13244 	ipsq_t *nipsq;
13245 	ipsq_t *pipsq = NULL;
13246 
13247 	/*
13248 	 * We don't hold the ipsq lock, but we are sure no new
13249 	 * messages can land up, since the ipsq_refs is zero.
13250 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13251 	 * is associated with this ipsq. (Lookups are based on ill_name
13252 	 * or phyint_group_name)
13253 	 */
13254 	ASSERT(ipsq->ipsq_refs == 0);
13255 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13256 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13257 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13258 		/*
13259 		 * This is not the ipsq of an IPMP group.
13260 		 */
13261 		kmem_free(ipsq, sizeof (ipsq_t));
13262 		return;
13263 	}
13264 
13265 	rw_enter(&ill_g_lock, RW_WRITER);
13266 
13267 	/*
13268 	 * Locate the ipsq  before we can remove it from
13269 	 * the singly linked list of ipsq's.
13270 	 */
13271 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13272 		if (nipsq == ipsq) {
13273 			break;
13274 		}
13275 		pipsq = nipsq;
13276 	}
13277 
13278 	ASSERT(nipsq == ipsq);
13279 
13280 	/* unlink ipsq from the list */
13281 	if (pipsq != NULL)
13282 		pipsq->ipsq_next = ipsq->ipsq_next;
13283 	else
13284 		ipsq_g_head = ipsq->ipsq_next;
13285 	kmem_free(ipsq, sizeof (ipsq_t));
13286 	rw_exit(&ill_g_lock);
13287 }
13288 
13289 static void
13290 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13291     queue_t *q)
13292 
13293 {
13294 
13295 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13296 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13297 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13298 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13299 	ASSERT(current_mp != NULL);
13300 
13301 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13302 		NEW_OP, NULL);
13303 
13304 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13305 	    new_ipsq->ipsq_xopq_mphead != NULL);
13306 
13307 	/*
13308 	 * move from old ipsq to the new ipsq.
13309 	 */
13310 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13311 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13312 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13313 
13314 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13315 }
13316 
13317 void
13318 ill_group_cleanup(ill_t *ill)
13319 {
13320 	ill_t *ill_v4;
13321 	ill_t *ill_v6;
13322 	ipif_t *ipif;
13323 
13324 	ill_v4 = ill->ill_phyint->phyint_illv4;
13325 	ill_v6 = ill->ill_phyint->phyint_illv6;
13326 
13327 	if (ill_v4 != NULL) {
13328 		mutex_enter(&ill_v4->ill_lock);
13329 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13330 		    ipif = ipif->ipif_next) {
13331 			IPIF_UNMARK_MOVING(ipif);
13332 		}
13333 		ill_v4->ill_up_ipifs = B_FALSE;
13334 		mutex_exit(&ill_v4->ill_lock);
13335 	}
13336 
13337 	if (ill_v6 != NULL) {
13338 		mutex_enter(&ill_v6->ill_lock);
13339 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13340 		    ipif = ipif->ipif_next) {
13341 			IPIF_UNMARK_MOVING(ipif);
13342 		}
13343 		ill_v6->ill_up_ipifs = B_FALSE;
13344 		mutex_exit(&ill_v6->ill_lock);
13345 	}
13346 }
13347 /*
13348  * This function is called when an ill has had a change in its group status
13349  * to bring up all the ipifs that were up before the change.
13350  */
13351 int
13352 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13353 {
13354 	ipif_t *ipif;
13355 	ill_t *ill_v4;
13356 	ill_t *ill_v6;
13357 	ill_t *from_ill;
13358 	int err = 0;
13359 
13360 
13361 	ASSERT(IAM_WRITER_ILL(ill));
13362 
13363 	/*
13364 	 * Except for ipif_state_flags and ill_state_flags the other
13365 	 * fields of the ipif/ill that are modified below are protected
13366 	 * implicitly since we are a writer. We would have tried to down
13367 	 * even an ipif that was already down, in ill_down_ipifs. So we
13368 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13369 	 */
13370 	ill_v4 = ill->ill_phyint->phyint_illv4;
13371 	ill_v6 = ill->ill_phyint->phyint_illv6;
13372 	if (ill_v4 != NULL) {
13373 		ill_v4->ill_up_ipifs = B_TRUE;
13374 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13375 		    ipif = ipif->ipif_next) {
13376 			mutex_enter(&ill_v4->ill_lock);
13377 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13378 			IPIF_UNMARK_MOVING(ipif);
13379 			mutex_exit(&ill_v4->ill_lock);
13380 			if (ipif->ipif_was_up) {
13381 				if (!(ipif->ipif_flags & IPIF_UP))
13382 					err = ipif_up(ipif, q, mp);
13383 				ipif->ipif_was_up = B_FALSE;
13384 				if (err != 0) {
13385 					/*
13386 					 * Can there be any other error ?
13387 					 */
13388 					ASSERT(err == EINPROGRESS);
13389 					return (err);
13390 				}
13391 			}
13392 		}
13393 		mutex_enter(&ill_v4->ill_lock);
13394 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13395 		mutex_exit(&ill_v4->ill_lock);
13396 		ill_v4->ill_up_ipifs = B_FALSE;
13397 		if (ill_v4->ill_move_in_progress) {
13398 			ASSERT(ill_v4->ill_move_peer != NULL);
13399 			ill_v4->ill_move_in_progress = B_FALSE;
13400 			from_ill = ill_v4->ill_move_peer;
13401 			from_ill->ill_move_in_progress = B_FALSE;
13402 			from_ill->ill_move_peer = NULL;
13403 			mutex_enter(&from_ill->ill_lock);
13404 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13405 			mutex_exit(&from_ill->ill_lock);
13406 			if (ill_v6 == NULL) {
13407 				if (from_ill->ill_phyint->phyint_flags &
13408 				    PHYI_STANDBY) {
13409 					phyint_inactive(from_ill->ill_phyint);
13410 				}
13411 				if (ill_v4->ill_phyint->phyint_flags &
13412 				    PHYI_STANDBY) {
13413 					phyint_inactive(ill_v4->ill_phyint);
13414 				}
13415 			}
13416 			ill_v4->ill_move_peer = NULL;
13417 		}
13418 	}
13419 
13420 	if (ill_v6 != NULL) {
13421 		ill_v6->ill_up_ipifs = B_TRUE;
13422 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13423 		    ipif = ipif->ipif_next) {
13424 			mutex_enter(&ill_v6->ill_lock);
13425 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13426 			IPIF_UNMARK_MOVING(ipif);
13427 			mutex_exit(&ill_v6->ill_lock);
13428 			if (ipif->ipif_was_up) {
13429 				if (!(ipif->ipif_flags & IPIF_UP))
13430 					err = ipif_up(ipif, q, mp);
13431 				ipif->ipif_was_up = B_FALSE;
13432 				if (err != 0) {
13433 					/*
13434 					 * Can there be any other error ?
13435 					 */
13436 					ASSERT(err == EINPROGRESS);
13437 					return (err);
13438 				}
13439 			}
13440 		}
13441 		mutex_enter(&ill_v6->ill_lock);
13442 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13443 		mutex_exit(&ill_v6->ill_lock);
13444 		ill_v6->ill_up_ipifs = B_FALSE;
13445 		if (ill_v6->ill_move_in_progress) {
13446 			ASSERT(ill_v6->ill_move_peer != NULL);
13447 			ill_v6->ill_move_in_progress = B_FALSE;
13448 			from_ill = ill_v6->ill_move_peer;
13449 			from_ill->ill_move_in_progress = B_FALSE;
13450 			from_ill->ill_move_peer = NULL;
13451 			mutex_enter(&from_ill->ill_lock);
13452 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13453 			mutex_exit(&from_ill->ill_lock);
13454 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13455 				phyint_inactive(from_ill->ill_phyint);
13456 			}
13457 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13458 				phyint_inactive(ill_v6->ill_phyint);
13459 			}
13460 			ill_v6->ill_move_peer = NULL;
13461 		}
13462 	}
13463 	return (0);
13464 }
13465 
13466 /*
13467  * bring down all the approriate ipifs.
13468  */
13469 /* ARGSUSED */
13470 static void
13471 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13472 {
13473 	ipif_t *ipif;
13474 
13475 	ASSERT(IAM_WRITER_ILL(ill));
13476 
13477 	/*
13478 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13479 	 * are modified below are protected implicitly since we are a writer
13480 	 */
13481 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13482 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13483 			continue;
13484 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13485 			/*
13486 			 * We go through the ipif_down logic even if the ipif
13487 			 * is already down, since routes can be added based
13488 			 * on down ipifs. Going through ipif_down once again
13489 			 * will delete any IREs created based on these routes.
13490 			 */
13491 			if (ipif->ipif_flags & IPIF_UP)
13492 				ipif->ipif_was_up = B_TRUE;
13493 			/*
13494 			 * If called with chk_nofailover true ipif is moving.
13495 			 */
13496 			mutex_enter(&ill->ill_lock);
13497 			if (chk_nofailover) {
13498 				ipif->ipif_state_flags |=
13499 					IPIF_MOVING | IPIF_CHANGING;
13500 			} else {
13501 				ipif->ipif_state_flags |= IPIF_CHANGING;
13502 			}
13503 			mutex_exit(&ill->ill_lock);
13504 			/*
13505 			 * Need to re-create net/subnet bcast ires if
13506 			 * they are dependent on ipif.
13507 			 */
13508 			if (!ipif->ipif_isv6)
13509 				ipif_check_bcast_ires(ipif);
13510 			(void) ipif_logical_down(ipif, NULL, NULL);
13511 			ipif_down_tail(ipif);
13512 			/*
13513 			 * We don't do ipif_multicast_down for IPv4 in
13514 			 * ipif_down. We need to set this so that
13515 			 * ipif_multicast_up will join the
13516 			 * ALLHOSTS_GROUP on to_ill.
13517 			 */
13518 			ipif->ipif_multicast_up = B_FALSE;
13519 		}
13520 	}
13521 }
13522 
13523 #define	IPSQ_INC_REF(ipsq)	{			\
13524 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13525 	(ipsq)->ipsq_refs++;				\
13526 }
13527 
13528 #define	IPSQ_DEC_REF(ipsq)	{			\
13529 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13530 	(ipsq)->ipsq_refs--;				\
13531 	if ((ipsq)->ipsq_refs == 0)				\
13532 		(ipsq)->ipsq_name[0] = '\0'; 		\
13533 }
13534 
13535 /*
13536  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13537  * new_ipsq.
13538  */
13539 static void
13540 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13541 {
13542 	phyint_t *phyint;
13543 	phyint_t *next_phyint;
13544 
13545 	/*
13546 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13547 	 * writer and the ill_lock of the ill in question. Also the dest
13548 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13549 	 */
13550 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13551 
13552 	phyint = cur_ipsq->ipsq_phyint_list;
13553 	cur_ipsq->ipsq_phyint_list = NULL;
13554 	while (phyint != NULL) {
13555 		next_phyint = phyint->phyint_ipsq_next;
13556 		IPSQ_DEC_REF(cur_ipsq);
13557 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13558 		new_ipsq->ipsq_phyint_list = phyint;
13559 		IPSQ_INC_REF(new_ipsq);
13560 		phyint->phyint_ipsq = new_ipsq;
13561 		phyint = next_phyint;
13562 	}
13563 }
13564 
13565 #define	SPLIT_SUCCESS		0
13566 #define	SPLIT_NOT_NEEDED	1
13567 #define	SPLIT_FAILED		2
13568 
13569 int
13570 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13571 {
13572 	ipsq_t *newipsq = NULL;
13573 
13574 	/*
13575 	 * Assertions denote pre-requisites for changing the ipsq of
13576 	 * a phyint
13577 	 */
13578 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13579 	/*
13580 	 * <ill-phyint> assocs can't change while ill_g_lock
13581 	 * is held as writer. See ill_phyint_reinit()
13582 	 */
13583 	ASSERT(phyint->phyint_illv4 == NULL ||
13584 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13585 	ASSERT(phyint->phyint_illv6 == NULL ||
13586 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13587 
13588 	if ((phyint->phyint_groupname_len !=
13589 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13590 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13591 	    phyint->phyint_groupname_len) != 0)) {
13592 		/*
13593 		 * Once we fail in creating a new ipsq due to memory shortage,
13594 		 * don't attempt to create new ipsq again, based on another
13595 		 * phyint, since we want all phyints belonging to an IPMP group
13596 		 * to be in the same ipsq even in the event of mem alloc fails.
13597 		 */
13598 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13599 		    cur_ipsq);
13600 		if (newipsq == NULL) {
13601 			/* Memory allocation failure */
13602 			return (SPLIT_FAILED);
13603 		} else {
13604 			/* ipsq_refs protected by ill_g_lock (writer) */
13605 			IPSQ_DEC_REF(cur_ipsq);
13606 			phyint->phyint_ipsq = newipsq;
13607 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13608 			newipsq->ipsq_phyint_list = phyint;
13609 			IPSQ_INC_REF(newipsq);
13610 			return (SPLIT_SUCCESS);
13611 		}
13612 	}
13613 	return (SPLIT_NOT_NEEDED);
13614 }
13615 
13616 /*
13617  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13618  * to do this split
13619  */
13620 static int
13621 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13622 {
13623 	ipsq_t *newipsq;
13624 
13625 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13626 	/*
13627 	 * <ill-phyint> assocs can't change while ill_g_lock
13628 	 * is held as writer. See ill_phyint_reinit()
13629 	 */
13630 
13631 	ASSERT(phyint->phyint_illv4 == NULL ||
13632 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13633 	ASSERT(phyint->phyint_illv6 == NULL ||
13634 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13635 
13636 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13637 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13638 		/*
13639 		 * ipsq_init failed due to no memory
13640 		 * caller will use the same ipsq
13641 		 */
13642 		return (SPLIT_FAILED);
13643 	}
13644 
13645 	/* ipsq_ref is protected by ill_g_lock (writer) */
13646 	IPSQ_DEC_REF(cur_ipsq);
13647 
13648 	/*
13649 	 * This is a new ipsq that is unknown to the world.
13650 	 * So we don't need to hold ipsq_lock,
13651 	 */
13652 	newipsq = phyint->phyint_ipsq;
13653 	newipsq->ipsq_writer = NULL;
13654 	newipsq->ipsq_reentry_cnt--;
13655 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13656 #ifdef ILL_DEBUG
13657 	newipsq->ipsq_depth = 0;
13658 #endif
13659 
13660 	return (SPLIT_SUCCESS);
13661 }
13662 
13663 /*
13664  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13665  * ipsq's representing their individual groups or themselves. Return
13666  * whether split needs to be retried again later.
13667  */
13668 static boolean_t
13669 ill_split_ipsq(ipsq_t *cur_ipsq)
13670 {
13671 	phyint_t *phyint;
13672 	phyint_t *next_phyint;
13673 	int	error;
13674 	boolean_t need_retry = B_FALSE;
13675 
13676 	phyint = cur_ipsq->ipsq_phyint_list;
13677 	cur_ipsq->ipsq_phyint_list = NULL;
13678 	while (phyint != NULL) {
13679 		next_phyint = phyint->phyint_ipsq_next;
13680 		/*
13681 		 * 'created' will tell us whether the callee actually
13682 		 * created an ipsq. Lack of memory may force the callee
13683 		 * to return without creating an ipsq.
13684 		 */
13685 		if (phyint->phyint_groupname == NULL) {
13686 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13687 		} else {
13688 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13689 					need_retry);
13690 		}
13691 
13692 		switch (error) {
13693 		case SPLIT_FAILED:
13694 			need_retry = B_TRUE;
13695 			/* FALLTHRU */
13696 		case SPLIT_NOT_NEEDED:
13697 			/*
13698 			 * Keep it on the list.
13699 			 */
13700 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13701 			cur_ipsq->ipsq_phyint_list = phyint;
13702 			break;
13703 		case SPLIT_SUCCESS:
13704 			break;
13705 		default:
13706 			ASSERT(0);
13707 		}
13708 
13709 		phyint = next_phyint;
13710 	}
13711 	return (need_retry);
13712 }
13713 
13714 /*
13715  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13716  * and return the ills in the list. This list will be
13717  * needed to unlock all the ills later on by the caller.
13718  * The <ill-ipsq> associations could change between the
13719  * lock and unlock. Hence the unlock can't traverse the
13720  * ipsq to get the list of ills.
13721  */
13722 static int
13723 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13724 {
13725 	int	cnt = 0;
13726 	phyint_t	*phyint;
13727 
13728 	/*
13729 	 * The caller holds ill_g_lock to ensure that the ill memberships
13730 	 * of the ipsq don't change
13731 	 */
13732 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13733 
13734 	phyint = ipsq->ipsq_phyint_list;
13735 	while (phyint != NULL) {
13736 		if (phyint->phyint_illv4 != NULL) {
13737 			ASSERT(cnt < list_max);
13738 			list[cnt++] = phyint->phyint_illv4;
13739 		}
13740 		if (phyint->phyint_illv6 != NULL) {
13741 			ASSERT(cnt < list_max);
13742 			list[cnt++] = phyint->phyint_illv6;
13743 		}
13744 		phyint = phyint->phyint_ipsq_next;
13745 	}
13746 	ill_lock_ills(list, cnt);
13747 	return (cnt);
13748 }
13749 
13750 void
13751 ill_lock_ills(ill_t **list, int cnt)
13752 {
13753 	int	i;
13754 
13755 	if (cnt > 1) {
13756 		boolean_t try_again;
13757 		do {
13758 			try_again = B_FALSE;
13759 			for (i = 0; i < cnt - 1; i++) {
13760 				if (list[i] < list[i + 1]) {
13761 					ill_t	*tmp;
13762 
13763 					/* swap the elements */
13764 					tmp = list[i];
13765 					list[i] = list[i + 1];
13766 					list[i + 1] = tmp;
13767 					try_again = B_TRUE;
13768 				}
13769 			}
13770 		} while (try_again);
13771 	}
13772 
13773 	for (i = 0; i < cnt; i++) {
13774 		if (i == 0) {
13775 			if (list[i] != NULL)
13776 				mutex_enter(&list[i]->ill_lock);
13777 			else
13778 				return;
13779 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13780 			mutex_enter(&list[i]->ill_lock);
13781 		}
13782 	}
13783 }
13784 
13785 void
13786 ill_unlock_ills(ill_t **list, int cnt)
13787 {
13788 	int	i;
13789 
13790 	for (i = 0; i < cnt; i++) {
13791 		if ((i == 0) && (list[i] != NULL)) {
13792 			mutex_exit(&list[i]->ill_lock);
13793 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13794 			mutex_exit(&list[i]->ill_lock);
13795 		}
13796 	}
13797 }
13798 
13799 /*
13800  * Merge all the ills from 1 ipsq group into another ipsq group.
13801  * The source ipsq group is specified by the ipsq associated with
13802  * 'from_ill'. The destination ipsq group is specified by the ipsq
13803  * associated with 'to_ill' or 'groupname' respectively.
13804  * Note that ipsq itself does not have a reference count mechanism
13805  * and functions don't look up an ipsq and pass it around. Instead
13806  * functions pass around an ill or groupname, and the ipsq is looked
13807  * up from the ill or groupname and the required operation performed
13808  * atomically with the lookup on the ipsq.
13809  */
13810 static int
13811 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13812     queue_t *q)
13813 {
13814 	ipsq_t *old_ipsq;
13815 	ipsq_t *new_ipsq;
13816 	ill_t	**ill_list;
13817 	int	cnt;
13818 	size_t	ill_list_size;
13819 	boolean_t became_writer_on_new_sq = B_FALSE;
13820 
13821 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
13822 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
13823 
13824 	/*
13825 	 * Need to hold ill_g_lock as writer and also the ill_lock to
13826 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
13827 	 * ipsq_lock to prevent new messages from landing on an ipsq.
13828 	 */
13829 	rw_enter(&ill_g_lock, RW_WRITER);
13830 
13831 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
13832 	if (groupname != NULL)
13833 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
13834 	else {
13835 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
13836 	}
13837 
13838 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
13839 
13840 	/*
13841 	 * both groups are on the same ipsq.
13842 	 */
13843 	if (old_ipsq == new_ipsq) {
13844 		rw_exit(&ill_g_lock);
13845 		return (0);
13846 	}
13847 
13848 	cnt = old_ipsq->ipsq_refs << 1;
13849 	ill_list_size = cnt * sizeof (ill_t *);
13850 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
13851 	if (ill_list == NULL) {
13852 		rw_exit(&ill_g_lock);
13853 		return (ENOMEM);
13854 	}
13855 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
13856 
13857 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
13858 	mutex_enter(&new_ipsq->ipsq_lock);
13859 	if ((new_ipsq->ipsq_writer == NULL &&
13860 		new_ipsq->ipsq_current_ipif == NULL) ||
13861 	    (new_ipsq->ipsq_writer == curthread)) {
13862 		new_ipsq->ipsq_writer = curthread;
13863 		new_ipsq->ipsq_reentry_cnt++;
13864 		became_writer_on_new_sq = B_TRUE;
13865 	}
13866 
13867 	/*
13868 	 * We are holding ill_g_lock as writer and all the ill locks of
13869 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
13870 	 * message can land up on the old ipsq even though we don't hold the
13871 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
13872 	 */
13873 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
13874 
13875 	/*
13876 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
13877 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
13878 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
13879 	 */
13880 	ill_merge_ipsq(old_ipsq, new_ipsq);
13881 
13882 	/*
13883 	 * Mark the new ipsq as needing a split since it is currently
13884 	 * being shared by more than 1 IPMP group. The split will
13885 	 * occur at the end of ipsq_exit
13886 	 */
13887 	new_ipsq->ipsq_split = B_TRUE;
13888 
13889 	/* Now release all the locks */
13890 	mutex_exit(&new_ipsq->ipsq_lock);
13891 	ill_unlock_ills(ill_list, cnt);
13892 	rw_exit(&ill_g_lock);
13893 
13894 	kmem_free(ill_list, ill_list_size);
13895 
13896 	/*
13897 	 * If we succeeded in becoming writer on the new ipsq, then
13898 	 * drain the new ipsq and start processing  all enqueued messages
13899 	 * including the current ioctl we are processing which is either
13900 	 * a set groupname or failover/failback.
13901 	 */
13902 	if (became_writer_on_new_sq)
13903 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
13904 
13905 	/*
13906 	 * syncq has been changed and all the messages have been moved.
13907 	 */
13908 	mutex_enter(&old_ipsq->ipsq_lock);
13909 	old_ipsq->ipsq_current_ipif = NULL;
13910 	mutex_exit(&old_ipsq->ipsq_lock);
13911 	return (EINPROGRESS);
13912 }
13913 
13914 /*
13915  * Delete and add the loopback copy and non-loopback copy of
13916  * the BROADCAST ire corresponding to ill and addr. Used to
13917  * group broadcast ires together when ill becomes part of
13918  * a group.
13919  *
13920  * This function is also called when ill is leaving the group
13921  * so that the ires belonging to the group gets re-grouped.
13922  */
13923 static void
13924 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
13925 {
13926 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
13927 	ire_t **ire_ptpn = &ire_head;
13928 
13929 	/*
13930 	 * The loopback and non-loopback IREs are inserted in the order in which
13931 	 * they're found, on the basis that they are correctly ordered (loopback
13932 	 * first).
13933 	 */
13934 	for (;;) {
13935 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
13936 		    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
13937 		if (ire == NULL)
13938 			break;
13939 
13940 		/*
13941 		 * we are passing in KM_SLEEP because it is not easy to
13942 		 * go back to a sane state in case of memory failure.
13943 		 */
13944 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
13945 		ASSERT(nire != NULL);
13946 		bzero(nire, sizeof (ire_t));
13947 		/*
13948 		 * Don't use ire_max_frag directly since we don't
13949 		 * hold on to 'ire' until we add the new ire 'nire' and
13950 		 * we don't want the new ire to have a dangling reference
13951 		 * to 'ire'. The ire_max_frag of a broadcast ire must
13952 		 * be in sync with the ipif_mtu of the associate ipif.
13953 		 * For eg. this happens as a result of SIOCSLIFNAME,
13954 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
13955 		 * the driver. A change in ire_max_frag triggered as
13956 		 * as a result of path mtu discovery, or due to an
13957 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
13958 		 * route change -mtu command does not apply to broadcast ires.
13959 		 *
13960 		 * XXX We need a recovery strategy here if ire_init fails
13961 		 */
13962 		if (ire_init(nire,
13963 		    (uchar_t *)&ire->ire_addr,
13964 		    (uchar_t *)&ire->ire_mask,
13965 		    (uchar_t *)&ire->ire_src_addr,
13966 		    (uchar_t *)&ire->ire_gateway_addr,
13967 		    (uchar_t *)&ire->ire_in_src_addr,
13968 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
13969 			&ire->ire_ipif->ipif_mtu,
13970 		    ire->ire_fp_mp,
13971 		    ire->ire_rfq,
13972 		    ire->ire_stq,
13973 		    ire->ire_type,
13974 		    ire->ire_dlureq_mp,
13975 		    ire->ire_ipif,
13976 		    ire->ire_in_ill,
13977 		    ire->ire_cmask,
13978 		    ire->ire_phandle,
13979 		    ire->ire_ihandle,
13980 		    ire->ire_flags,
13981 		    &ire->ire_uinfo) == NULL) {
13982 			cmn_err(CE_PANIC, "ire_init() failed");
13983 		}
13984 		ire_delete(ire);
13985 		ire_refrele(ire);
13986 
13987 		/*
13988 		 * The newly created IREs are inserted at the tail of the list
13989 		 * starting with ire_head. As we've just allocated them no one
13990 		 * knows about them so it's safe.
13991 		 */
13992 		*ire_ptpn = nire;
13993 		ire_ptpn = &nire->ire_next;
13994 	}
13995 
13996 	for (nire = ire_head; nire != NULL; nire = nire_next) {
13997 		int error;
13998 		ire_t *oire;
13999 		/* unlink the IRE from our list before calling ire_add() */
14000 		nire_next = nire->ire_next;
14001 		nire->ire_next = NULL;
14002 
14003 		/* ire_add adds the ire at the right place in the list */
14004 		oire = nire;
14005 		error = ire_add(&nire, NULL, NULL, NULL);
14006 		ASSERT(error == 0);
14007 		ASSERT(oire == nire);
14008 		ire_refrele(nire);	/* Held in ire_add */
14009 	}
14010 }
14011 
14012 /*
14013  * This function is usually called when an ill is inserted in
14014  * a group and all the ipifs are already UP. As all the ipifs
14015  * are already UP, the broadcast ires have already been created
14016  * and been inserted. But, ire_add_v4 would not have grouped properly.
14017  * We need to re-group for the benefit of ip_wput_ire which
14018  * expects BROADCAST ires to be grouped properly to avoid sending
14019  * more than one copy of the broadcast packet per group.
14020  *
14021  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14022  *	  because when ipif_up_done ends up calling this, ires have
14023  *        already been added before illgrp_insert i.e before ill_group
14024  *	  has been initialized.
14025  */
14026 static void
14027 ill_group_bcast_for_xmit(ill_t *ill)
14028 {
14029 	ill_group_t *illgrp;
14030 	ipif_t *ipif;
14031 	ipaddr_t addr;
14032 	ipaddr_t net_mask;
14033 	ipaddr_t subnet_netmask;
14034 
14035 	illgrp = ill->ill_group;
14036 
14037 	/*
14038 	 * This function is called even when an ill is deleted from
14039 	 * the group. Hence, illgrp could be null.
14040 	 */
14041 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14042 		return;
14043 
14044 	/*
14045 	 * Delete all the BROADCAST ires matching this ill and add
14046 	 * them back. This time, ire_add_v4 should take care of
14047 	 * grouping them with others because ill is part of the
14048 	 * group.
14049 	 */
14050 	ill_bcast_delete_and_add(ill, 0);
14051 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14052 
14053 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14054 
14055 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14056 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14057 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14058 		} else {
14059 			net_mask = htonl(IN_CLASSA_NET);
14060 		}
14061 		addr = net_mask & ipif->ipif_subnet;
14062 		ill_bcast_delete_and_add(ill, addr);
14063 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14064 
14065 		subnet_netmask = ipif->ipif_net_mask;
14066 		addr = ipif->ipif_subnet;
14067 		ill_bcast_delete_and_add(ill, addr);
14068 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14069 	}
14070 }
14071 
14072 /*
14073  * This function is called from illgrp_delete when ill is being deleted
14074  * from the group.
14075  *
14076  * As ill is not there in the group anymore, any address belonging
14077  * to this ill should be cleared of IRE_MARK_NORECV.
14078  */
14079 static void
14080 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14081 {
14082 	ire_t *ire;
14083 	irb_t *irb;
14084 
14085 	ASSERT(ill->ill_group == NULL);
14086 
14087 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14088 	    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14089 
14090 	if (ire != NULL) {
14091 		/*
14092 		 * IPMP and plumbing operations are serialized on the ipsq, so
14093 		 * no one will insert or delete a broadcast ire under our feet.
14094 		 */
14095 		irb = ire->ire_bucket;
14096 		rw_enter(&irb->irb_lock, RW_READER);
14097 		ire_refrele(ire);
14098 
14099 		for (; ire != NULL; ire = ire->ire_next) {
14100 			if (ire->ire_addr != addr)
14101 				break;
14102 			if (ire_to_ill(ire) != ill)
14103 				continue;
14104 
14105 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14106 			ire->ire_marks &= ~IRE_MARK_NORECV;
14107 		}
14108 		rw_exit(&irb->irb_lock);
14109 	}
14110 }
14111 
14112 /*
14113  * This function must be called only after the broadcast ires
14114  * have been grouped together. For a given address addr, nominate
14115  * only one of the ires whose interface is not FAILED or OFFLINE.
14116  *
14117  * This is also called when an ipif goes down, so that we can nominate
14118  * a different ire with the same address for receiving.
14119  */
14120 static void
14121 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14122 {
14123 	irb_t *irb;
14124 	ire_t *ire;
14125 	ire_t *ire1;
14126 	ire_t *save_ire;
14127 	ire_t **irep = NULL;
14128 	boolean_t first = B_TRUE;
14129 	ire_t *clear_ire = NULL;
14130 	ire_t *start_ire = NULL;
14131 	ire_t	*new_lb_ire;
14132 	ire_t	*new_nlb_ire;
14133 	boolean_t new_lb_ire_used = B_FALSE;
14134 	boolean_t new_nlb_ire_used = B_FALSE;
14135 	uint64_t match_flags;
14136 	uint64_t phyi_flags;
14137 	boolean_t fallback = B_FALSE;
14138 
14139 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14140 	    MATCH_IRE_TYPE);
14141 	/*
14142 	 * We may not be able to find some ires if a previous
14143 	 * ire_create failed. This happens when an ipif goes
14144 	 * down and we are unable to create BROADCAST ires due
14145 	 * to memory failure. Thus, we have to check for NULL
14146 	 * below. This should handle the case for LOOPBACK,
14147 	 * POINTOPOINT and interfaces with some POINTOPOINT
14148 	 * logicals for which there are no BROADCAST ires.
14149 	 */
14150 	if (ire == NULL)
14151 		return;
14152 	/*
14153 	 * Currently IRE_BROADCASTS are deleted when an ipif
14154 	 * goes down which runs exclusively. Thus, setting
14155 	 * IRE_MARK_RCVD should not race with ire_delete marking
14156 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14157 	 * be consistent with other parts of the code that walks
14158 	 * a given bucket.
14159 	 */
14160 	save_ire = ire;
14161 	irb = ire->ire_bucket;
14162 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14163 	if (new_lb_ire == NULL) {
14164 		ire_refrele(ire);
14165 		return;
14166 	}
14167 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14168 	if (new_nlb_ire == NULL) {
14169 		ire_refrele(ire);
14170 		kmem_cache_free(ire_cache, new_lb_ire);
14171 		return;
14172 	}
14173 	IRB_REFHOLD(irb);
14174 	rw_enter(&irb->irb_lock, RW_WRITER);
14175 	/*
14176 	 * Get to the first ire matching the address and the
14177 	 * group. If the address does not match we are done
14178 	 * as we could not find the IRE. If the address matches
14179 	 * we should get to the first one matching the group.
14180 	 */
14181 	while (ire != NULL) {
14182 		if (ire->ire_addr != addr ||
14183 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14184 			break;
14185 		}
14186 		ire = ire->ire_next;
14187 	}
14188 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14189 	start_ire = ire;
14190 redo:
14191 	while (ire != NULL && ire->ire_addr == addr &&
14192 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14193 		/*
14194 		 * The first ire for any address within a group
14195 		 * should always be the one with IRE_MARK_NORECV cleared
14196 		 * so that ip_wput_ire can avoid searching for one.
14197 		 * Note down the insertion point which will be used
14198 		 * later.
14199 		 */
14200 		if (first && (irep == NULL))
14201 			irep = ire->ire_ptpn;
14202 		/*
14203 		 * PHYI_FAILED is set when the interface fails.
14204 		 * This interface might have become good, but the
14205 		 * daemon has not yet detected. We should still
14206 		 * not receive on this. PHYI_OFFLINE should never
14207 		 * be picked as this has been offlined and soon
14208 		 * be removed.
14209 		 */
14210 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14211 		if (phyi_flags & PHYI_OFFLINE) {
14212 			ire->ire_marks |= IRE_MARK_NORECV;
14213 			ire = ire->ire_next;
14214 			continue;
14215 		}
14216 		if (phyi_flags & match_flags) {
14217 			ire->ire_marks |= IRE_MARK_NORECV;
14218 			ire = ire->ire_next;
14219 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14220 			    PHYI_INACTIVE) {
14221 				fallback = B_TRUE;
14222 			}
14223 			continue;
14224 		}
14225 		if (first) {
14226 			/*
14227 			 * We will move this to the front of the list later
14228 			 * on.
14229 			 */
14230 			clear_ire = ire;
14231 			ire->ire_marks &= ~IRE_MARK_NORECV;
14232 		} else {
14233 			ire->ire_marks |= IRE_MARK_NORECV;
14234 		}
14235 		first = B_FALSE;
14236 		ire = ire->ire_next;
14237 	}
14238 	/*
14239 	 * If we never nominated anybody, try nominating at least
14240 	 * an INACTIVE, if we found one. Do it only once though.
14241 	 */
14242 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14243 	    fallback) {
14244 		match_flags = PHYI_FAILED;
14245 		ire = start_ire;
14246 		irep = NULL;
14247 		goto redo;
14248 	}
14249 	ire_refrele(save_ire);
14250 
14251 	/*
14252 	 * irep non-NULL indicates that we entered the while loop
14253 	 * above. If clear_ire is at the insertion point, we don't
14254 	 * have to do anything. clear_ire will be NULL if all the
14255 	 * interfaces are failed.
14256 	 *
14257 	 * We cannot unlink and reinsert the ire at the right place
14258 	 * in the list since there can be other walkers of this bucket.
14259 	 * Instead we delete and recreate the ire
14260 	 */
14261 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14262 		ire_t *clear_ire_stq = NULL;
14263 		bzero(new_lb_ire, sizeof (ire_t));
14264 		/* XXX We need a recovery strategy here. */
14265 		if (ire_init(new_lb_ire,
14266 		    (uchar_t *)&clear_ire->ire_addr,
14267 		    (uchar_t *)&clear_ire->ire_mask,
14268 		    (uchar_t *)&clear_ire->ire_src_addr,
14269 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14270 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14271 		    &clear_ire->ire_max_frag,
14272 		    clear_ire->ire_fp_mp,
14273 		    clear_ire->ire_rfq,
14274 		    clear_ire->ire_stq,
14275 		    clear_ire->ire_type,
14276 		    clear_ire->ire_dlureq_mp,
14277 		    clear_ire->ire_ipif,
14278 		    clear_ire->ire_in_ill,
14279 		    clear_ire->ire_cmask,
14280 		    clear_ire->ire_phandle,
14281 		    clear_ire->ire_ihandle,
14282 		    clear_ire->ire_flags,
14283 		    &clear_ire->ire_uinfo) == NULL)
14284 			cmn_err(CE_PANIC, "ire_init() failed");
14285 		if (clear_ire->ire_stq == NULL) {
14286 			ire_t *ire_next = clear_ire->ire_next;
14287 			if (ire_next != NULL &&
14288 			    ire_next->ire_stq != NULL &&
14289 			    ire_next->ire_addr == clear_ire->ire_addr &&
14290 			    ire_next->ire_ipif->ipif_ill ==
14291 			    clear_ire->ire_ipif->ipif_ill) {
14292 				clear_ire_stq = ire_next;
14293 
14294 				bzero(new_nlb_ire, sizeof (ire_t));
14295 				/* XXX We need a recovery strategy here. */
14296 				if (ire_init(new_nlb_ire,
14297 				    (uchar_t *)&clear_ire_stq->ire_addr,
14298 				    (uchar_t *)&clear_ire_stq->ire_mask,
14299 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14300 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14301 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14302 				    &clear_ire_stq->ire_max_frag,
14303 				    clear_ire_stq->ire_fp_mp,
14304 				    clear_ire_stq->ire_rfq,
14305 				    clear_ire_stq->ire_stq,
14306 				    clear_ire_stq->ire_type,
14307 				    clear_ire_stq->ire_dlureq_mp,
14308 				    clear_ire_stq->ire_ipif,
14309 				    clear_ire_stq->ire_in_ill,
14310 				    clear_ire_stq->ire_cmask,
14311 				    clear_ire_stq->ire_phandle,
14312 				    clear_ire_stq->ire_ihandle,
14313 				    clear_ire_stq->ire_flags,
14314 				    &clear_ire_stq->ire_uinfo) == NULL)
14315 					cmn_err(CE_PANIC, "ire_init() failed");
14316 			}
14317 		}
14318 
14319 		/*
14320 		 * Delete the ire. We can't call ire_delete() since
14321 		 * we are holding the bucket lock. We can't release the
14322 		 * bucket lock since we can't allow irep to change. So just
14323 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14324 		 * ire from the list and do the refrele.
14325 		 */
14326 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14327 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14328 
14329 		if (clear_ire_stq != NULL) {
14330 			ire_fastpath_list_delete(
14331 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14332 			    clear_ire_stq);
14333 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14334 		}
14335 
14336 		/*
14337 		 * Also take care of otherfields like ib/ob pkt count
14338 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14339 		 */
14340 
14341 		/* Add the new ire's. Insert at *irep */
14342 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14343 		ire1 = *irep;
14344 		if (ire1 != NULL)
14345 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14346 		new_lb_ire->ire_next = ire1;
14347 		/* Link the new one in. */
14348 		new_lb_ire->ire_ptpn = irep;
14349 		membar_producer();
14350 		*irep = new_lb_ire;
14351 		new_lb_ire_used = B_TRUE;
14352 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14353 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14354 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14355 
14356 		if (clear_ire_stq != NULL) {
14357 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14358 			irep = &new_lb_ire->ire_next;
14359 			/* Add the new ire. Insert at *irep */
14360 			ire1 = *irep;
14361 			if (ire1 != NULL)
14362 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14363 			new_nlb_ire->ire_next = ire1;
14364 			/* Link the new one in. */
14365 			new_nlb_ire->ire_ptpn = irep;
14366 			membar_producer();
14367 			*irep = new_nlb_ire;
14368 			new_nlb_ire_used = B_TRUE;
14369 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14370 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14371 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14372 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14373 		}
14374 	}
14375 	rw_exit(&irb->irb_lock);
14376 	if (!new_lb_ire_used)
14377 		kmem_cache_free(ire_cache, new_lb_ire);
14378 	if (!new_nlb_ire_used)
14379 		kmem_cache_free(ire_cache, new_nlb_ire);
14380 	IRB_REFRELE(irb);
14381 }
14382 
14383 /*
14384  * Whenever an ipif goes down we have to renominate a different
14385  * broadcast ire to receive. Whenever an ipif comes up, we need
14386  * to make sure that we have only one nominated to receive.
14387  */
14388 static void
14389 ipif_renominate_bcast(ipif_t *ipif)
14390 {
14391 	ill_t *ill = ipif->ipif_ill;
14392 	ipaddr_t subnet_addr;
14393 	ipaddr_t net_addr;
14394 	ipaddr_t net_mask = 0;
14395 	ipaddr_t subnet_netmask;
14396 	ipaddr_t addr;
14397 	ill_group_t *illgrp;
14398 
14399 	illgrp = ill->ill_group;
14400 	/*
14401 	 * If this is the last ipif going down, it might take
14402 	 * the ill out of the group. In that case ipif_down ->
14403 	 * illgrp_delete takes care of doing the nomination.
14404 	 * ipif_down does not call for this case.
14405 	 */
14406 	ASSERT(illgrp != NULL);
14407 
14408 	/* There could not have been any ires associated with this */
14409 	if (ipif->ipif_subnet == 0)
14410 		return;
14411 
14412 	ill_mark_bcast(illgrp, 0);
14413 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14414 
14415 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14416 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14417 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14418 	} else {
14419 		net_mask = htonl(IN_CLASSA_NET);
14420 	}
14421 	addr = net_mask & ipif->ipif_subnet;
14422 	ill_mark_bcast(illgrp, addr);
14423 
14424 	net_addr = ~net_mask | addr;
14425 	ill_mark_bcast(illgrp, net_addr);
14426 
14427 	subnet_netmask = ipif->ipif_net_mask;
14428 	addr = ipif->ipif_subnet;
14429 	ill_mark_bcast(illgrp, addr);
14430 
14431 	subnet_addr = ~subnet_netmask | addr;
14432 	ill_mark_bcast(illgrp, subnet_addr);
14433 }
14434 
14435 /*
14436  * Whenever we form or delete ill groups, we need to nominate one set of
14437  * BROADCAST ires for receiving in the group.
14438  *
14439  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14440  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14441  *    for ill_ipif_up_count to be non-zero. This is the only case where
14442  *    ill_ipif_up_count is zero and we would still find the ires.
14443  *
14444  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14445  *    ipif is UP and we just have to do the nomination.
14446  *
14447  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14448  *    from the group. So, we have to do the nomination.
14449  *
14450  * Because of (3), there could be just one ill in the group. But we have
14451  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14452  * Thus, this function does not optimize when there is only one ill as
14453  * it is not correct for (3).
14454  */
14455 static void
14456 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14457 {
14458 	ill_t *ill;
14459 	ipif_t *ipif;
14460 	ipaddr_t subnet_addr;
14461 	ipaddr_t prev_subnet_addr = 0;
14462 	ipaddr_t net_addr;
14463 	ipaddr_t prev_net_addr = 0;
14464 	ipaddr_t net_mask = 0;
14465 	ipaddr_t subnet_netmask;
14466 	ipaddr_t addr;
14467 
14468 	/*
14469 	 * When the last memeber is leaving, there is nothing to
14470 	 * nominate.
14471 	 */
14472 	if (illgrp->illgrp_ill_count == 0) {
14473 		ASSERT(illgrp->illgrp_ill == NULL);
14474 		return;
14475 	}
14476 
14477 	ill = illgrp->illgrp_ill;
14478 	ASSERT(!ill->ill_isv6);
14479 	/*
14480 	 * We assume that ires with same address and belonging to the
14481 	 * same group, has been grouped together. Nominating a *single*
14482 	 * ill in the group for sending and receiving broadcast is done
14483 	 * by making sure that the first BROADCAST ire (which will be
14484 	 * the one returned by ire_ctable_lookup for ip_rput and the
14485 	 * one that will be used in ip_wput_ire) will be the one that
14486 	 * will not have IRE_MARK_NORECV set.
14487 	 *
14488 	 * 1) ip_rput checks and discards packets received on ires marked
14489 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14490 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14491 	 *    first ire in the group for every broadcast address in the group.
14492 	 *    ip_rput will accept packets only on the first ire i.e only
14493 	 *    one copy of the ill.
14494 	 *
14495 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14496 	 *    packet for the whole group. It needs to send out on the ill
14497 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14498 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14499 	 *    the copy echoed back on other port where the ire is not marked
14500 	 *    with IRE_MARK_NORECV.
14501 	 *
14502 	 * Note that we just need to have the first IRE either loopback or
14503 	 * non-loopback (either of them may not exist if ire_create failed
14504 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14505 	 * always hit the first one and hence will always accept one copy.
14506 	 *
14507 	 * We have a broadcast ire per ill for all the unique prefixes
14508 	 * hosted on that ill. As we don't have a way of knowing the
14509 	 * unique prefixes on a given ill and hence in the whole group,
14510 	 * we just call ill_mark_bcast on all the prefixes that exist
14511 	 * in the group. For the common case of one prefix, the code
14512 	 * below optimizes by remebering the last address used for
14513 	 * markng. In the case of multiple prefixes, this will still
14514 	 * optimize depending the order of prefixes.
14515 	 *
14516 	 * The only unique address across the whole group is 0.0.0.0 and
14517 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14518 	 * the first ire in the bucket for receiving and disables the
14519 	 * others.
14520 	 */
14521 	ill_mark_bcast(illgrp, 0);
14522 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14523 	for (; ill != NULL; ill = ill->ill_group_next) {
14524 
14525 		for (ipif = ill->ill_ipif; ipif != NULL;
14526 		    ipif = ipif->ipif_next) {
14527 
14528 			if (!(ipif->ipif_flags & IPIF_UP) ||
14529 			    ipif->ipif_subnet == 0) {
14530 				continue;
14531 			}
14532 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14533 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14534 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14535 			} else {
14536 				net_mask = htonl(IN_CLASSA_NET);
14537 			}
14538 			addr = net_mask & ipif->ipif_subnet;
14539 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14540 				ill_mark_bcast(illgrp, addr);
14541 				net_addr = ~net_mask | addr;
14542 				ill_mark_bcast(illgrp, net_addr);
14543 			}
14544 			prev_net_addr = addr;
14545 
14546 			subnet_netmask = ipif->ipif_net_mask;
14547 			addr = ipif->ipif_subnet;
14548 			if (prev_subnet_addr == 0 ||
14549 			    prev_subnet_addr != addr) {
14550 				ill_mark_bcast(illgrp, addr);
14551 				subnet_addr = ~subnet_netmask | addr;
14552 				ill_mark_bcast(illgrp, subnet_addr);
14553 			}
14554 			prev_subnet_addr = addr;
14555 		}
14556 	}
14557 }
14558 
14559 /*
14560  * This function is called while forming ill groups.
14561  *
14562  * Currently, we handle only allmulti groups. We want to join
14563  * allmulti on only one of the ills in the groups. In future,
14564  * when we have link aggregation, we may have to join normal
14565  * multicast groups on multiple ills as switch does inbound load
14566  * balancing. Following are the functions that calls this
14567  * function :
14568  *
14569  * 1) ill_recover_multicast : Interface is coming back UP.
14570  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14571  *    will call ill_recover_multicast to recover all the multicast
14572  *    groups. We need to make sure that only one member is joined
14573  *    in the ill group.
14574  *
14575  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14576  *    Somebody is joining allmulti. We need to make sure that only one
14577  *    member is joined in the group.
14578  *
14579  * 3) illgrp_insert : If allmulti has already joined, we need to make
14580  *    sure that only one member is joined in the group.
14581  *
14582  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14583  *    allmulti who we have nominated. We need to pick someother ill.
14584  *
14585  * 5) illgrp_delete : The ill we nominated is leaving the group,
14586  *    we need to pick a new ill to join the group.
14587  *
14588  * For (1), (2), (5) - we just have to check whether there is
14589  * a good ill joined in the group. If we could not find any ills
14590  * joined the group, we should join.
14591  *
14592  * For (4), the one that was nominated to receive, left the group.
14593  * There could be nobody joined in the group when this function is
14594  * called.
14595  *
14596  * For (3) - we need to explicitly check whether there are multiple
14597  * ills joined in the group.
14598  *
14599  * For simplicity, we don't differentiate any of the above cases. We
14600  * just leave the group if it is joined on any of them and join on
14601  * the first good ill.
14602  */
14603 int
14604 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14605 {
14606 	ilm_t *ilm;
14607 	ill_t *ill;
14608 	ill_t *fallback_inactive_ill = NULL;
14609 	ill_t *fallback_failed_ill = NULL;
14610 	int ret = 0;
14611 
14612 	/*
14613 	 * Leave the allmulti on all the ills and start fresh.
14614 	 */
14615 	for (ill = illgrp->illgrp_ill; ill != NULL;
14616 	    ill = ill->ill_group_next) {
14617 		if (ill->ill_join_allmulti)
14618 			(void) ip_leave_allmulti(ill->ill_ipif);
14619 	}
14620 
14621 	/*
14622 	 * Choose a good ill. Fallback to inactive or failed if
14623 	 * none available. We need to fallback to FAILED in the
14624 	 * case where we have 2 interfaces in a group - where
14625 	 * one of them is failed and another is a good one and
14626 	 * the good one (not marked inactive) is leaving the group.
14627 	 */
14628 	ret = 0;
14629 	for (ill = illgrp->illgrp_ill; ill != NULL;
14630 	    ill = ill->ill_group_next) {
14631 		/* Never pick an offline interface */
14632 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14633 			continue;
14634 
14635 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14636 			fallback_failed_ill = ill;
14637 			continue;
14638 		}
14639 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14640 			fallback_inactive_ill = ill;
14641 			continue;
14642 		}
14643 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14644 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14645 				ret = ip_join_allmulti(ill->ill_ipif);
14646 				/*
14647 				 * ip_join_allmulti can fail because of memory
14648 				 * failures. So, make sure we join at least
14649 				 * on one ill.
14650 				 */
14651 				if (ill->ill_join_allmulti)
14652 					return (0);
14653 			}
14654 		}
14655 	}
14656 	if (ret != 0) {
14657 		/*
14658 		 * If we tried nominating above and failed to do so,
14659 		 * return error. We might have tried multiple times.
14660 		 * But, return the latest error.
14661 		 */
14662 		return (ret);
14663 	}
14664 	if ((ill = fallback_inactive_ill) != NULL) {
14665 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14666 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14667 				ret = ip_join_allmulti(ill->ill_ipif);
14668 				return (ret);
14669 			}
14670 		}
14671 	} else if ((ill = fallback_failed_ill) != NULL) {
14672 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14673 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14674 				ret = ip_join_allmulti(ill->ill_ipif);
14675 				return (ret);
14676 			}
14677 		}
14678 	}
14679 	return (0);
14680 }
14681 
14682 /*
14683  * This function is called from illgrp_delete after it is
14684  * deleted from the group to reschedule responsibilities
14685  * to a different ill.
14686  */
14687 static void
14688 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14689 {
14690 	ilm_t	*ilm;
14691 	ipif_t	*ipif;
14692 	ipaddr_t subnet_addr;
14693 	ipaddr_t net_addr;
14694 	ipaddr_t net_mask = 0;
14695 	ipaddr_t subnet_netmask;
14696 	ipaddr_t addr;
14697 
14698 	ASSERT(ill->ill_group == NULL);
14699 	/*
14700 	 * Broadcast Responsibility:
14701 	 *
14702 	 * 1. If this ill has been nominated for receiving broadcast
14703 	 * packets, we need to find a new one. Before we find a new
14704 	 * one, we need to re-group the ires that are part of this new
14705 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14706 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14707 	 * thing for us.
14708 	 *
14709 	 * 2. If this ill was not nominated for receiving broadcast
14710 	 * packets, we need to clear the IRE_MARK_NORECV flag
14711 	 * so that we continue to send up broadcast packets.
14712 	 */
14713 	if (!ill->ill_isv6) {
14714 		/*
14715 		 * Case 1 above : No optimization here. Just redo the
14716 		 * nomination.
14717 		 */
14718 		ill_group_bcast_for_xmit(ill);
14719 		ill_nominate_bcast_rcv(illgrp);
14720 
14721 		/*
14722 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14723 		 */
14724 		ill_clear_bcast_mark(ill, 0);
14725 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14726 
14727 		for (ipif = ill->ill_ipif; ipif != NULL;
14728 		    ipif = ipif->ipif_next) {
14729 
14730 			if (!(ipif->ipif_flags & IPIF_UP) ||
14731 			    ipif->ipif_subnet == 0) {
14732 				continue;
14733 			}
14734 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14735 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14736 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14737 			} else {
14738 				net_mask = htonl(IN_CLASSA_NET);
14739 			}
14740 			addr = net_mask & ipif->ipif_subnet;
14741 			ill_clear_bcast_mark(ill, addr);
14742 
14743 			net_addr = ~net_mask | addr;
14744 			ill_clear_bcast_mark(ill, net_addr);
14745 
14746 			subnet_netmask = ipif->ipif_net_mask;
14747 			addr = ipif->ipif_subnet;
14748 			ill_clear_bcast_mark(ill, addr);
14749 
14750 			subnet_addr = ~subnet_netmask | addr;
14751 			ill_clear_bcast_mark(ill, subnet_addr);
14752 		}
14753 	}
14754 
14755 	/*
14756 	 * Multicast Responsibility.
14757 	 *
14758 	 * If we have joined allmulti on this one, find a new member
14759 	 * in the group to join allmulti. As this ill is already part
14760 	 * of allmulti, we don't have to join on this one.
14761 	 *
14762 	 * If we have not joined allmulti on this one, there is no
14763 	 * responsibility to handoff. But we need to take new
14764 	 * responsibility i.e, join allmulti on this one if we need
14765 	 * to.
14766 	 */
14767 	if (ill->ill_join_allmulti) {
14768 		(void) ill_nominate_mcast_rcv(illgrp);
14769 	} else {
14770 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14771 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14772 				(void) ip_join_allmulti(ill->ill_ipif);
14773 				break;
14774 			}
14775 		}
14776 	}
14777 
14778 	/*
14779 	 * We intentionally do the flushing of IRE_CACHES only matching
14780 	 * on the ill and not on groups. Note that we are already deleted
14781 	 * from the group.
14782 	 *
14783 	 * This will make sure that all IRE_CACHES whose stq is pointing
14784 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14785 	 * deleted and IRE_CACHES that are not pointing at this ill will
14786 	 * be left alone.
14787 	 */
14788 	if (ill->ill_isv6) {
14789 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14790 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14791 	} else {
14792 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14793 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14794 	}
14795 
14796 	/*
14797 	 * Some conn may have cached one of the IREs deleted above. By removing
14798 	 * the ire reference, we clean up the extra reference to the ill held in
14799 	 * ire->ire_stq.
14800 	 */
14801 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14802 
14803 	/*
14804 	 * Re-do source address selection for all the members in the
14805 	 * group, if they borrowed source address from one of the ipifs
14806 	 * in this ill.
14807 	 */
14808 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14809 		if (ill->ill_isv6) {
14810 			ipif_update_other_ipifs_v6(ipif, illgrp);
14811 		} else {
14812 			ipif_update_other_ipifs(ipif, illgrp);
14813 		}
14814 	}
14815 }
14816 
14817 /*
14818  * Delete the ill from the group. The caller makes sure that it is
14819  * in a group and it okay to delete from the group. So, we always
14820  * delete here.
14821  */
14822 static void
14823 illgrp_delete(ill_t *ill)
14824 {
14825 	ill_group_t *illgrp;
14826 	ill_group_t *tmpg;
14827 	ill_t *tmp_ill;
14828 
14829 	/*
14830 	 * Reset illgrp_ill_schednext if it was pointing at us.
14831 	 * We need to do this before we set ill_group to NULL.
14832 	 */
14833 	rw_enter(&ill_g_lock, RW_WRITER);
14834 	mutex_enter(&ill->ill_lock);
14835 
14836 	illgrp_reset_schednext(ill);
14837 
14838 	illgrp = ill->ill_group;
14839 
14840 	/* Delete the ill from illgrp. */
14841 	if (illgrp->illgrp_ill == ill) {
14842 		illgrp->illgrp_ill = ill->ill_group_next;
14843 	} else {
14844 		tmp_ill = illgrp->illgrp_ill;
14845 		while (tmp_ill->ill_group_next != ill) {
14846 			tmp_ill = tmp_ill->ill_group_next;
14847 			ASSERT(tmp_ill != NULL);
14848 		}
14849 		tmp_ill->ill_group_next = ill->ill_group_next;
14850 	}
14851 	ill->ill_group = NULL;
14852 	ill->ill_group_next = NULL;
14853 
14854 	illgrp->illgrp_ill_count--;
14855 	mutex_exit(&ill->ill_lock);
14856 	rw_exit(&ill_g_lock);
14857 
14858 	/*
14859 	 * As this ill is leaving the group, we need to hand off
14860 	 * the responsibilities to the other ills in the group, if
14861 	 * this ill had some responsibilities.
14862 	 */
14863 
14864 	ill_handoff_responsibility(ill, illgrp);
14865 
14866 	rw_enter(&ill_g_lock, RW_WRITER);
14867 
14868 	if (illgrp->illgrp_ill_count == 0) {
14869 
14870 		ASSERT(illgrp->illgrp_ill == NULL);
14871 		if (ill->ill_isv6) {
14872 			if (illgrp == illgrp_head_v6) {
14873 				illgrp_head_v6 = illgrp->illgrp_next;
14874 			} else {
14875 				tmpg = illgrp_head_v6;
14876 				while (tmpg->illgrp_next != illgrp) {
14877 					tmpg = tmpg->illgrp_next;
14878 					ASSERT(tmpg != NULL);
14879 				}
14880 				tmpg->illgrp_next = illgrp->illgrp_next;
14881 			}
14882 		} else {
14883 			if (illgrp == illgrp_head_v4) {
14884 				illgrp_head_v4 = illgrp->illgrp_next;
14885 			} else {
14886 				tmpg = illgrp_head_v4;
14887 				while (tmpg->illgrp_next != illgrp) {
14888 					tmpg = tmpg->illgrp_next;
14889 					ASSERT(tmpg != NULL);
14890 				}
14891 				tmpg->illgrp_next = illgrp->illgrp_next;
14892 			}
14893 		}
14894 		mutex_destroy(&illgrp->illgrp_lock);
14895 		mi_free(illgrp);
14896 	}
14897 	rw_exit(&ill_g_lock);
14898 
14899 	/*
14900 	 * Even though the ill is out of the group its not necessary
14901 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
14902 	 * We will split the ipsq when phyint_groupname is set to NULL.
14903 	 */
14904 
14905 	/*
14906 	 * Send a routing sockets message if we are deleting from
14907 	 * groups with names.
14908 	 */
14909 	if (ill->ill_phyint->phyint_groupname_len != 0)
14910 		ip_rts_ifmsg(ill->ill_ipif);
14911 }
14912 
14913 /*
14914  * Re-do source address selection. This is normally called when
14915  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
14916  * ipif comes up.
14917  */
14918 void
14919 ill_update_source_selection(ill_t *ill)
14920 {
14921 	ipif_t *ipif;
14922 
14923 	ASSERT(IAM_WRITER_ILL(ill));
14924 
14925 	if (ill->ill_group != NULL)
14926 		ill = ill->ill_group->illgrp_ill;
14927 
14928 	for (; ill != NULL; ill = ill->ill_group_next) {
14929 		for (ipif = ill->ill_ipif; ipif != NULL;
14930 		    ipif = ipif->ipif_next) {
14931 			if (ill->ill_isv6)
14932 				ipif_recreate_interface_routes_v6(NULL, ipif);
14933 			else
14934 				ipif_recreate_interface_routes(NULL, ipif);
14935 		}
14936 	}
14937 }
14938 
14939 /*
14940  * Insert ill in a group headed by illgrp_head. The caller can either
14941  * pass a groupname in which case we search for a group with the
14942  * same name to insert in or pass a group to insert in. This function
14943  * would only search groups with names.
14944  *
14945  * NOTE : The caller should make sure that there is at least one ipif
14946  *	  UP on this ill so that illgrp_scheduler can pick this ill
14947  *	  for outbound packets. If ill_ipif_up_count is zero, we have
14948  *	  already sent a DL_UNBIND to the driver and we don't want to
14949  *	  send anymore packets. We don't assert for ipif_up_count
14950  *	  to be greater than zero, because ipif_up_done wants to call
14951  *	  this function before bumping up the ipif_up_count. See
14952  *	  ipif_up_done() for details.
14953  */
14954 int
14955 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
14956     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
14957 {
14958 	ill_group_t *illgrp;
14959 	ill_t *prev_ill;
14960 	phyint_t *phyi;
14961 
14962 	ASSERT(ill->ill_group == NULL);
14963 
14964 	rw_enter(&ill_g_lock, RW_WRITER);
14965 	mutex_enter(&ill->ill_lock);
14966 
14967 	if (groupname != NULL) {
14968 		/*
14969 		 * Look for a group with a matching groupname to insert.
14970 		 */
14971 		for (illgrp = *illgrp_head; illgrp != NULL;
14972 		    illgrp = illgrp->illgrp_next) {
14973 
14974 			ill_t *tmp_ill;
14975 
14976 			tmp_ill = illgrp->illgrp_ill;
14977 			ASSERT(tmp_ill != NULL && tmp_ill->ill_phyint != NULL);
14978 			phyi = tmp_ill->ill_phyint;
14979 			/*
14980 			 * Look at groups which has names only.
14981 			 */
14982 			if (phyi->phyint_groupname_len == 0)
14983 				continue;
14984 			/*
14985 			 * Names are stored in the phyint common to both
14986 			 * IPv4 and IPv6.
14987 			 */
14988 			if (mi_strcmp(phyi->phyint_groupname,
14989 			    groupname) == 0) {
14990 				break;
14991 			}
14992 		}
14993 	} else {
14994 		/*
14995 		 * If the caller passes in a NULL "grp_to_insert", we
14996 		 * allocate one below and insert this singleton.
14997 		 */
14998 		illgrp = grp_to_insert;
14999 	}
15000 
15001 	ill->ill_group_next = NULL;
15002 
15003 	if (illgrp == NULL) {
15004 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15005 		if (illgrp == NULL) {
15006 			return (ENOMEM);
15007 		}
15008 		illgrp->illgrp_next = *illgrp_head;
15009 		*illgrp_head = illgrp;
15010 		illgrp->illgrp_ill = ill;
15011 		illgrp->illgrp_ill_count = 1;
15012 		ill->ill_group = illgrp;
15013 		/*
15014 		 * Used in illgrp_scheduler to protect multiple threads
15015 		 * from traversing the list.
15016 		 */
15017 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15018 	} else {
15019 		ASSERT(ill->ill_net_type ==
15020 		    illgrp->illgrp_ill->ill_net_type);
15021 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15022 
15023 		/* Insert ill at tail of this group */
15024 		prev_ill = illgrp->illgrp_ill;
15025 		while (prev_ill->ill_group_next != NULL)
15026 			prev_ill = prev_ill->ill_group_next;
15027 		prev_ill->ill_group_next = ill;
15028 		ill->ill_group = illgrp;
15029 		illgrp->illgrp_ill_count++;
15030 		/*
15031 		 * Inherit group properties. Currently only forwarding
15032 		 * is the property we try to keep the same with all the
15033 		 * ills. When there are more, we will abstract this into
15034 		 * a function.
15035 		 */
15036 		ill->ill_flags &= ~ILLF_ROUTER;
15037 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15038 	}
15039 	mutex_exit(&ill->ill_lock);
15040 	rw_exit(&ill_g_lock);
15041 
15042 	/*
15043 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15044 	 *    may be zero as it has not yet been bumped. But the ires
15045 	 *    have already been added. So, we do the nomination here
15046 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15047 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15048 	 *    ill_ipif_up_count here while nominating broadcast ires for
15049 	 *    receive.
15050 	 *
15051 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15052 	 *    to group them properly as ire_add() has already happened
15053 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15054 	 *    case, we need to do it here anyway.
15055 	 */
15056 	if (!ill->ill_isv6) {
15057 		ill_group_bcast_for_xmit(ill);
15058 		ill_nominate_bcast_rcv(illgrp);
15059 	}
15060 
15061 	if (!ipif_is_coming_up) {
15062 		/*
15063 		 * When ipif_up_done() calls this function, the multicast
15064 		 * groups have not been joined yet. So, there is no point in
15065 		 * nomination. ip_join_allmulti will handle groups when
15066 		 * ill_recover_multicast is called from ipif_up_done() later.
15067 		 */
15068 		(void) ill_nominate_mcast_rcv(illgrp);
15069 		/*
15070 		 * ipif_up_done calls ill_update_source_selection
15071 		 * anyway. Moreover, we don't want to re-create
15072 		 * interface routes while ipif_up_done() still has reference
15073 		 * to them. Refer to ipif_up_done() for more details.
15074 		 */
15075 		ill_update_source_selection(ill);
15076 	}
15077 
15078 	/*
15079 	 * Send a routing sockets message if we are inserting into
15080 	 * groups with names.
15081 	 */
15082 	if (groupname != NULL)
15083 		ip_rts_ifmsg(ill->ill_ipif);
15084 	return (0);
15085 }
15086 
15087 /*
15088  * Return the first phyint matching the groupname. There could
15089  * be more than one when there are ill groups.
15090  *
15091  * Needs work: called only from ip_sioctl_groupname
15092  */
15093 static phyint_t *
15094 phyint_lookup_group(char *groupname)
15095 {
15096 	phyint_t *phyi;
15097 
15098 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15099 	/*
15100 	 * Group names are stored in the phyint - a common structure
15101 	 * to both IPv4 and IPv6.
15102 	 */
15103 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15104 	for (; phyi != NULL;
15105 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15106 	    phyi, AVL_AFTER)) {
15107 		if (phyi->phyint_groupname_len == 0)
15108 			continue;
15109 		ASSERT(phyi->phyint_groupname != NULL);
15110 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15111 			return (phyi);
15112 	}
15113 	return (NULL);
15114 }
15115 
15116 
15117 
15118 /*
15119  * MT notes on creation and deletion of IPMP groups
15120  *
15121  * Creation and deletion of IPMP groups introduce the need to merge or
15122  * split the associated serialization objects i.e the ipsq's. Normally all
15123  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15124  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15125  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15126  * is a need to change the <ill-ipsq> association and we have to operate on both
15127  * the source and destination IPMP groups. For eg. attempting to set the
15128  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15129  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15130  * source or destination IPMP group are mapped to a single ipsq for executing
15131  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15132  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15133  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15134  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15135  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15136  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15137  *
15138  * In the above example the ioctl handling code locates the current ipsq of hme0
15139  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15140  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15141  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15142  * the destination ipsq. If the destination ipsq is not busy, it also enters
15143  * the destination ipsq exclusively. Now the actual groupname setting operation
15144  * can proceed. If the destination ipsq is busy, the operation is enqueued
15145  * on the destination (merged) ipsq and will be handled in the unwind from
15146  * ipsq_exit.
15147  *
15148  * To prevent other threads accessing the ill while the group name change is
15149  * in progres, we bring down the ipifs which also removes the ill from the
15150  * group. The group is changed in phyint and when the first ipif on the ill
15151  * is brought up, the ill is inserted into the right IPMP group by
15152  * illgrp_insert.
15153  */
15154 /* ARGSUSED */
15155 int
15156 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15157     ip_ioctl_cmd_t *ipip, void *ifreq)
15158 {
15159 	int i;
15160 	char *tmp;
15161 	int namelen;
15162 	ill_t *ill = ipif->ipif_ill;
15163 	ill_t *ill_v4, *ill_v6;
15164 	int err = 0;
15165 	phyint_t *phyi;
15166 	phyint_t *phyi_tmp;
15167 	struct lifreq *lifr;
15168 	mblk_t	*mp1;
15169 	char *groupname;
15170 	ipsq_t *ipsq;
15171 
15172 	ASSERT(IAM_WRITER_IPIF(ipif));
15173 
15174 	/* Existance verified in ip_wput_nondata */
15175 	mp1 = mp->b_cont->b_cont;
15176 	lifr = (struct lifreq *)mp1->b_rptr;
15177 	groupname = lifr->lifr_groupname;
15178 
15179 	if (ipif->ipif_id != 0)
15180 		return (EINVAL);
15181 
15182 	phyi = ill->ill_phyint;
15183 	ASSERT(phyi != NULL);
15184 
15185 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15186 		return (EINVAL);
15187 
15188 	tmp = groupname;
15189 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15190 		;
15191 
15192 	if (i == LIFNAMSIZ) {
15193 		/* no null termination */
15194 		return (EINVAL);
15195 	}
15196 
15197 	/*
15198 	 * Calculate the namelen exclusive of the null
15199 	 * termination character.
15200 	 */
15201 	namelen = tmp - groupname;
15202 
15203 	ill_v4 = phyi->phyint_illv4;
15204 	ill_v6 = phyi->phyint_illv6;
15205 
15206 	/*
15207 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15208 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15209 	 * synchronization notes in ip.c
15210 	 */
15211 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15212 		return (EINVAL);
15213 	}
15214 
15215 	/*
15216 	 * mark the ill as changing.
15217 	 * this should queue all new requests on the syncq.
15218 	 */
15219 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15220 
15221 	if (ill_v4 != NULL)
15222 		ill_v4->ill_state_flags |= ILL_CHANGING;
15223 	if (ill_v6 != NULL)
15224 		ill_v6->ill_state_flags |= ILL_CHANGING;
15225 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15226 
15227 	if (namelen == 0) {
15228 		/*
15229 		 * Null string means remove this interface from the
15230 		 * existing group.
15231 		 */
15232 		if (phyi->phyint_groupname_len == 0) {
15233 			/*
15234 			 * Never was in a group.
15235 			 */
15236 			err = 0;
15237 			goto done;
15238 		}
15239 
15240 		/*
15241 		 * IPv4 or IPv6 may be temporarily out of the group when all
15242 		 * the ipifs are down. Thus, we need to check for ill_group to
15243 		 * be non-NULL.
15244 		 */
15245 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15246 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15247 			mutex_enter(&ill_v4->ill_lock);
15248 			if (!ill_is_quiescent(ill_v4)) {
15249 				/*
15250 				 * ipsq_pending_mp_add will not fail since
15251 				 * connp is NULL
15252 				 */
15253 				(void) ipsq_pending_mp_add(NULL,
15254 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15255 				mutex_exit(&ill_v4->ill_lock);
15256 				err = EINPROGRESS;
15257 				goto done;
15258 			}
15259 			mutex_exit(&ill_v4->ill_lock);
15260 		}
15261 
15262 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15263 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15264 			mutex_enter(&ill_v6->ill_lock);
15265 			if (!ill_is_quiescent(ill_v6)) {
15266 				(void) ipsq_pending_mp_add(NULL,
15267 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15268 				mutex_exit(&ill_v6->ill_lock);
15269 				err = EINPROGRESS;
15270 				goto done;
15271 			}
15272 			mutex_exit(&ill_v6->ill_lock);
15273 		}
15274 
15275 		rw_enter(&ill_g_lock, RW_WRITER);
15276 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15277 		mutex_enter(&phyi->phyint_lock);
15278 		ASSERT(phyi->phyint_groupname != NULL);
15279 		mi_free(phyi->phyint_groupname);
15280 		phyi->phyint_groupname = NULL;
15281 		phyi->phyint_groupname_len = 0;
15282 		mutex_exit(&phyi->phyint_lock);
15283 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15284 		rw_exit(&ill_g_lock);
15285 		err = ill_up_ipifs(ill, q, mp);
15286 
15287 		/*
15288 		 * set the split flag so that the ipsq can be split
15289 		 */
15290 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15291 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15292 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15293 
15294 	} else {
15295 		if (phyi->phyint_groupname_len != 0) {
15296 			ASSERT(phyi->phyint_groupname != NULL);
15297 			/* Are we inserting in the same group ? */
15298 			if (mi_strcmp(groupname,
15299 			    phyi->phyint_groupname) == 0) {
15300 				err = 0;
15301 				goto done;
15302 			}
15303 		}
15304 
15305 		rw_enter(&ill_g_lock, RW_READER);
15306 		/*
15307 		 * Merge ipsq for the group's.
15308 		 * This check is here as multiple groups/ills might be
15309 		 * sharing the same ipsq.
15310 		 * If we have to merege than the operation is restarted
15311 		 * on the new ipsq.
15312 		 */
15313 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15314 		if (phyi->phyint_ipsq != ipsq) {
15315 			rw_exit(&ill_g_lock);
15316 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15317 			goto done;
15318 		}
15319 		/*
15320 		 * Running exclusive on new ipsq.
15321 		 */
15322 
15323 		ASSERT(ipsq != NULL);
15324 		ASSERT(ipsq->ipsq_writer == curthread);
15325 
15326 		/*
15327 		 * Check whether the ill_type and ill_net_type matches before
15328 		 * we allocate any memory so that the cleanup is easier.
15329 		 *
15330 		 * We can't group dissimilar ones as we can't load spread
15331 		 * packets across the group because of potential link-level
15332 		 * header differences.
15333 		 */
15334 		phyi_tmp = phyint_lookup_group(groupname);
15335 		if (phyi_tmp != NULL) {
15336 			if ((ill_v4 != NULL &&
15337 			    phyi_tmp->phyint_illv4 != NULL) &&
15338 			    ((ill_v4->ill_net_type !=
15339 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15340 			    (ill_v4->ill_type !=
15341 			    phyi_tmp->phyint_illv4->ill_type))) {
15342 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15343 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15344 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15345 				rw_exit(&ill_g_lock);
15346 				return (EINVAL);
15347 			}
15348 			if ((ill_v6 != NULL &&
15349 			    phyi_tmp->phyint_illv6 != NULL) &&
15350 			    ((ill_v6->ill_net_type !=
15351 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15352 			    (ill_v6->ill_type !=
15353 			    phyi_tmp->phyint_illv6->ill_type))) {
15354 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15355 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15356 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15357 				rw_exit(&ill_g_lock);
15358 				return (EINVAL);
15359 			}
15360 		}
15361 
15362 		rw_exit(&ill_g_lock);
15363 
15364 		/*
15365 		 * bring down all v4 ipifs.
15366 		 */
15367 		if (ill_v4 != NULL) {
15368 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15369 		}
15370 
15371 		/*
15372 		 * bring down all v6 ipifs.
15373 		 */
15374 		if (ill_v6 != NULL) {
15375 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15376 		}
15377 
15378 		/*
15379 		 * make sure all ipifs are down and there are no active
15380 		 * references. Call to ipsq_pending_mp_add will not fail
15381 		 * since connp is NULL.
15382 		 */
15383 		if (ill_v4 != NULL) {
15384 			mutex_enter(&ill_v4->ill_lock);
15385 			if (!ill_is_quiescent(ill_v4)) {
15386 				(void) ipsq_pending_mp_add(NULL,
15387 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15388 				mutex_exit(&ill_v4->ill_lock);
15389 				err = EINPROGRESS;
15390 				goto done;
15391 			}
15392 			mutex_exit(&ill_v4->ill_lock);
15393 		}
15394 
15395 		if (ill_v6 != NULL) {
15396 			mutex_enter(&ill_v6->ill_lock);
15397 			if (!ill_is_quiescent(ill_v6)) {
15398 				(void) ipsq_pending_mp_add(NULL,
15399 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15400 				mutex_exit(&ill_v6->ill_lock);
15401 				err = EINPROGRESS;
15402 				goto done;
15403 			}
15404 			mutex_exit(&ill_v6->ill_lock);
15405 		}
15406 
15407 		/*
15408 		 * allocate including space for null terminator
15409 		 * before we insert.
15410 		 */
15411 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15412 		if (tmp == NULL)
15413 			return (ENOMEM);
15414 
15415 		rw_enter(&ill_g_lock, RW_WRITER);
15416 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15417 		mutex_enter(&phyi->phyint_lock);
15418 		if (phyi->phyint_groupname_len != 0) {
15419 			ASSERT(phyi->phyint_groupname != NULL);
15420 			mi_free(phyi->phyint_groupname);
15421 		}
15422 
15423 		/*
15424 		 * setup the new group name.
15425 		 */
15426 		phyi->phyint_groupname = tmp;
15427 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15428 		phyi->phyint_groupname_len = namelen + 1;
15429 		mutex_exit(&phyi->phyint_lock);
15430 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15431 		rw_exit(&ill_g_lock);
15432 
15433 		err = ill_up_ipifs(ill, q, mp);
15434 	}
15435 
15436 done:
15437 	/*
15438 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15439 	 */
15440 	if (err != EINPROGRESS) {
15441 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15442 		if (ill_v4 != NULL)
15443 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15444 		if (ill_v6 != NULL)
15445 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15446 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15447 	}
15448 	return (err);
15449 }
15450 
15451 /* ARGSUSED */
15452 int
15453 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15454     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15455 {
15456 	ill_t *ill;
15457 	phyint_t *phyi;
15458 	struct lifreq *lifr;
15459 	mblk_t	*mp1;
15460 
15461 	/* Existence verified in ip_wput_nondata */
15462 	mp1 = mp->b_cont->b_cont;
15463 	lifr = (struct lifreq *)mp1->b_rptr;
15464 	ill = ipif->ipif_ill;
15465 	phyi = ill->ill_phyint;
15466 
15467 	lifr->lifr_groupname[0] = '\0';
15468 	/*
15469 	 * ill_group may be null if all the interfaces
15470 	 * are down. But still, the phyint should always
15471 	 * hold the name.
15472 	 */
15473 	if (phyi->phyint_groupname_len != 0) {
15474 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15475 		    phyi->phyint_groupname_len);
15476 	}
15477 
15478 	return (0);
15479 }
15480 
15481 
15482 typedef struct conn_move_s {
15483 	ill_t	*cm_from_ill;
15484 	ill_t	*cm_to_ill;
15485 	int	cm_ifindex;
15486 } conn_move_t;
15487 
15488 /*
15489  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15490  */
15491 static void
15492 conn_move(conn_t *connp, caddr_t arg)
15493 {
15494 	conn_move_t *connm;
15495 	int ifindex;
15496 	int i;
15497 	ill_t *from_ill;
15498 	ill_t *to_ill;
15499 	ilg_t *ilg;
15500 	ilm_t *ret_ilm;
15501 
15502 	connm = (conn_move_t *)arg;
15503 	ifindex = connm->cm_ifindex;
15504 	from_ill = connm->cm_from_ill;
15505 	to_ill = connm->cm_to_ill;
15506 
15507 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15508 
15509 	/* All multicast fields protected by conn_lock */
15510 	mutex_enter(&connp->conn_lock);
15511 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15512 	if ((connp->conn_outgoing_ill == from_ill) &&
15513 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15514 		connp->conn_outgoing_ill = to_ill;
15515 		connp->conn_incoming_ill = to_ill;
15516 	}
15517 
15518 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15519 
15520 	if ((connp->conn_multicast_ill == from_ill) &&
15521 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15522 		connp->conn_multicast_ill = connm->cm_to_ill;
15523 	}
15524 
15525 	/* Change IP_XMIT_IF associations */
15526 	if ((connp->conn_xmit_if_ill == from_ill) &&
15527 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15528 		connp->conn_xmit_if_ill = to_ill;
15529 	}
15530 	/*
15531 	 * Change the ilg_ill to point to the new one. This assumes
15532 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15533 	 * has been told to receive packets on this interface.
15534 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15535 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15536 	 * some ilms may not have moved. We check to see whether
15537 	 * the ilms have moved to to_ill. We can't check on from_ill
15538 	 * as in the process of moving, we could have split an ilm
15539 	 * in to two - which has the same orig_ifindex and v6group.
15540 	 *
15541 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15542 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15543 	 */
15544 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15545 		ilg = &connp->conn_ilg[i];
15546 		if ((ilg->ilg_ill == from_ill) &&
15547 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15548 			/* ifindex != 0 indicates failback */
15549 			if (ifindex != 0) {
15550 				connp->conn_ilg[i].ilg_ill = to_ill;
15551 				continue;
15552 			}
15553 
15554 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15555 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15556 			    connp->conn_zoneid);
15557 
15558 			if (ret_ilm != NULL)
15559 				connp->conn_ilg[i].ilg_ill = to_ill;
15560 		}
15561 	}
15562 	mutex_exit(&connp->conn_lock);
15563 }
15564 
15565 static void
15566 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15567 {
15568 	conn_move_t connm;
15569 
15570 	connm.cm_from_ill = from_ill;
15571 	connm.cm_to_ill = to_ill;
15572 	connm.cm_ifindex = ifindex;
15573 
15574 	ipcl_walk(conn_move, (caddr_t)&connm);
15575 }
15576 
15577 /*
15578  * ilm has been moved from from_ill to to_ill.
15579  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15580  * appropriately.
15581  *
15582  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15583  *	  the code there de-references ipif_ill to get the ill to
15584  *	  send multicast requests. It does not work as ipif is on its
15585  *	  move and already moved when this function is called.
15586  *	  Thus, we need to use from_ill and to_ill send down multicast
15587  *	  requests.
15588  */
15589 static void
15590 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15591 {
15592 	ipif_t *ipif;
15593 	ilm_t *ilm;
15594 
15595 	/*
15596 	 * See whether we need to send down DL_ENABMULTI_REQ on
15597 	 * to_ill as ilm has just been added.
15598 	 */
15599 	ASSERT(IAM_WRITER_ILL(to_ill));
15600 	ASSERT(IAM_WRITER_ILL(from_ill));
15601 
15602 	ILM_WALKER_HOLD(to_ill);
15603 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15604 
15605 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
15606 			continue;
15607 		/*
15608 		 * no locks held, ill/ipif cannot dissappear as long
15609 		 * as we are writer.
15610 		 */
15611 		ipif = to_ill->ill_ipif;
15612 		/*
15613 		 * No need to hold any lock as we are the writer and this
15614 		 * can only be changed by a writer.
15615 		 */
15616 		ilm->ilm_is_new = B_FALSE;
15617 
15618 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15619 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15620 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15621 			    "resolver\n"));
15622 			continue;		/* Must be IRE_IF_NORESOLVER */
15623 		}
15624 
15625 
15626 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15627 			ip1dbg(("ilm_send_multicast_reqs: "
15628 			    "to_ill MULTI_BCAST\n"));
15629 			goto from;
15630 		}
15631 
15632 		if (to_ill->ill_isv6)
15633 			mld_joingroup(ilm);
15634 		else
15635 			igmp_joingroup(ilm);
15636 
15637 		if (to_ill->ill_ipif_up_count == 0) {
15638 			/*
15639 			 * Nobody there. All multicast addresses will be
15640 			 * re-joined when we get the DL_BIND_ACK bringing the
15641 			 * interface up.
15642 			 */
15643 			ilm->ilm_notify_driver = B_FALSE;
15644 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15645 			goto from;
15646 		}
15647 
15648 		/*
15649 		 * For allmulti address, we want to join on only one interface.
15650 		 * Checking for ilm_numentries_v6 is not correct as you may
15651 		 * find an ilm with zero address on to_ill, but we may not
15652 		 * have nominated to_ill for receiving. Thus, if we have
15653 		 * nominated from_ill (ill_join_allmulti is set), nominate
15654 		 * only if to_ill is not already nominated (to_ill normally
15655 		 * should not have been nominated if "from_ill" has already
15656 		 * been nominated. As we don't prevent failovers from happening
15657 		 * across groups, we don't assert).
15658 		 */
15659 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15660 			/*
15661 			 * There is no need to hold ill locks as we are
15662 			 * writer on both ills and when ill_join_allmulti
15663 			 * is changed the thread is always a writer.
15664 			 */
15665 			if (from_ill->ill_join_allmulti &&
15666 			    !to_ill->ill_join_allmulti) {
15667 				(void) ip_join_allmulti(to_ill->ill_ipif);
15668 			}
15669 		} else if (ilm->ilm_notify_driver) {
15670 
15671 			/*
15672 			 * This is a newly moved ilm so we need to tell the
15673 			 * driver about the new group. There can be more than
15674 			 * one ilm's for the same group in the list each with a
15675 			 * different orig_ifindex. We have to inform the driver
15676 			 * once. In ilm_move_v[4,6] we only set the flag
15677 			 * ilm_notify_driver for the first ilm.
15678 			 */
15679 
15680 			(void) ip_ll_send_enabmulti_req(to_ill,
15681 			    &ilm->ilm_v6addr);
15682 		}
15683 
15684 		ilm->ilm_notify_driver = B_FALSE;
15685 
15686 		/*
15687 		 * See whether we need to send down DL_DISABMULTI_REQ on
15688 		 * from_ill as ilm has just been removed.
15689 		 */
15690 from:
15691 		ipif = from_ill->ill_ipif;
15692 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15693 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15694 			ip1dbg(("ilm_send_multicast_reqs: "
15695 			    "from_ill not resolver\n"));
15696 			continue;		/* Must be IRE_IF_NORESOLVER */
15697 		}
15698 
15699 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15700 			ip1dbg(("ilm_send_multicast_reqs: "
15701 			    "from_ill MULTI_BCAST\n"));
15702 			continue;
15703 		}
15704 
15705 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15706 			if (from_ill->ill_join_allmulti)
15707 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15708 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15709 			(void) ip_ll_send_disabmulti_req(from_ill,
15710 		    &ilm->ilm_v6addr);
15711 		}
15712 	}
15713 	ILM_WALKER_RELE(to_ill);
15714 }
15715 
15716 /*
15717  * This function is called when all multicast memberships needs
15718  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15719  * called only once unlike the IPv4 counterpart where it is called after
15720  * every logical interface is moved. The reason is due to multicast
15721  * memberships are joined using an interface address in IPv4 while in
15722  * IPv6, interface index is used.
15723  */
15724 static void
15725 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15726 {
15727 	ilm_t	*ilm;
15728 	ilm_t	*ilm_next;
15729 	ilm_t	*new_ilm;
15730 	ilm_t	**ilmp;
15731 	int	count;
15732 	char buf[INET6_ADDRSTRLEN];
15733 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15734 
15735 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15736 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15737 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15738 
15739 	if (ifindex == 0) {
15740 		/*
15741 		 * Form the solicited node mcast address which is used later.
15742 		 */
15743 		ipif_t *ipif;
15744 
15745 		ipif = from_ill->ill_ipif;
15746 		ASSERT(ipif->ipif_id == 0);
15747 
15748 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15749 	}
15750 
15751 	ilmp = &from_ill->ill_ilm;
15752 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
15753 
15754 		if (ilm->ilm_flags & ILM_DELETED)
15755 			continue;
15756 
15757 		ilm_next = ilm->ilm_next;
15758 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15759 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15760 		ASSERT(ilm->ilm_orig_ifindex != 0);
15761 		if (ilm->ilm_orig_ifindex == ifindex) {
15762 			/*
15763 			 * We are failing back multicast memberships.
15764 			 * If the same ilm exists in to_ill, it means somebody
15765 			 * has joined the same group there e.g. ff02::1
15766 			 * is joined within the kernel when the interfaces
15767 			 * came UP.
15768 			 */
15769 			ASSERT(ilm->ilm_ipif == NULL);
15770 			if (new_ilm != NULL) {
15771 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15772 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15773 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15774 					new_ilm->ilm_is_new = B_TRUE;
15775 				}
15776 			} else {
15777 				/*
15778 				 * check if we can just move the ilm
15779 				 */
15780 				if (from_ill->ill_ilm_walker_cnt != 0) {
15781 					/*
15782 					 * We have walkers we cannot move
15783 					 * the ilm, so allocate a new ilm,
15784 					 * this (old) ilm will be marked
15785 					 * ILM_DELETED at the end of the loop
15786 					 * and will be freed when the
15787 					 * last walker exits.
15788 					 */
15789 					new_ilm = (ilm_t *)mi_zalloc
15790 					    (sizeof (ilm_t));
15791 					if (new_ilm == NULL) {
15792 						ip0dbg(("ilm_move_v6: "
15793 						    "FAILBACK of IPv6"
15794 						    " multicast address %s : "
15795 						    "from %s to"
15796 						    " %s failed : ENOMEM \n",
15797 						    inet_ntop(AF_INET6,
15798 						    &ilm->ilm_v6addr, buf,
15799 						    sizeof (buf)),
15800 						    from_ill->ill_name,
15801 						    to_ill->ill_name));
15802 
15803 							ilmp = &ilm->ilm_next;
15804 							continue;
15805 					}
15806 					*new_ilm = *ilm;
15807 					/*
15808 					 * we don't want new_ilm linked to
15809 					 * ilm's filter list.
15810 					 */
15811 					new_ilm->ilm_filter = NULL;
15812 				} else {
15813 					/*
15814 					 * No walkers we can move the ilm.
15815 					 * lets take it out of the list.
15816 					 */
15817 					*ilmp = ilm->ilm_next;
15818 					ilm->ilm_next = NULL;
15819 					new_ilm = ilm;
15820 				}
15821 
15822 				/*
15823 				 * if this is the first ilm for the group
15824 				 * set ilm_notify_driver so that we notify the
15825 				 * driver in ilm_send_multicast_reqs.
15826 				 */
15827 				if (ilm_lookup_ill_v6(to_ill,
15828 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15829 					new_ilm->ilm_notify_driver = B_TRUE;
15830 
15831 				new_ilm->ilm_ill = to_ill;
15832 				/* Add to the to_ill's list */
15833 				new_ilm->ilm_next = to_ill->ill_ilm;
15834 				to_ill->ill_ilm = new_ilm;
15835 				/*
15836 				 * set the flag so that mld_joingroup is
15837 				 * called in ilm_send_multicast_reqs().
15838 				 */
15839 				new_ilm->ilm_is_new = B_TRUE;
15840 			}
15841 			goto bottom;
15842 		} else if (ifindex != 0) {
15843 			/*
15844 			 * If this is FAILBACK (ifindex != 0) and the ifindex
15845 			 * has not matched above, look at the next ilm.
15846 			 */
15847 			ilmp = &ilm->ilm_next;
15848 			continue;
15849 		}
15850 		/*
15851 		 * If we are here, it means ifindex is 0. Failover
15852 		 * everything.
15853 		 *
15854 		 * We need to handle solicited node mcast address
15855 		 * and all_nodes mcast address differently as they
15856 		 * are joined witin the kenrel (ipif_multicast_up)
15857 		 * and potentially from the userland. We are called
15858 		 * after the ipifs of from_ill has been moved.
15859 		 * If we still find ilms on ill with solicited node
15860 		 * mcast address or all_nodes mcast address, it must
15861 		 * belong to the UP interface that has not moved e.g.
15862 		 * ipif_id 0 with the link local prefix does not move.
15863 		 * We join this on the new ill accounting for all the
15864 		 * userland memberships so that applications don't
15865 		 * see any failure.
15866 		 *
15867 		 * We need to make sure that we account only for the
15868 		 * solicited node and all node multicast addresses
15869 		 * that was brought UP on these. In the case of
15870 		 * a failover from A to B, we might have ilms belonging
15871 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
15872 		 * for the membership from the userland. If we are failing
15873 		 * over from B to C now, we will find the ones belonging
15874 		 * to A on B. These don't account for the ill_ipif_up_count.
15875 		 * They just move from B to C. The check below on
15876 		 * ilm_orig_ifindex ensures that.
15877 		 */
15878 		if ((ilm->ilm_orig_ifindex ==
15879 		    from_ill->ill_phyint->phyint_ifindex) &&
15880 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
15881 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
15882 		    &ilm->ilm_v6addr))) {
15883 			ASSERT(ilm->ilm_refcnt > 0);
15884 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
15885 			/*
15886 			 * For indentation reasons, we are not using a
15887 			 * "else" here.
15888 			 */
15889 			if (count == 0) {
15890 				ilmp = &ilm->ilm_next;
15891 				continue;
15892 			}
15893 			ilm->ilm_refcnt -= count;
15894 			if (new_ilm != NULL) {
15895 				/*
15896 				 * Can find one with the same
15897 				 * ilm_orig_ifindex, if we are failing
15898 				 * over to a STANDBY. This happens
15899 				 * when somebody wants to join a group
15900 				 * on a STANDBY interface and we
15901 				 * internally join on a different one.
15902 				 * If we had joined on from_ill then, a
15903 				 * failover now will find a new ilm
15904 				 * with this index.
15905 				 */
15906 				ip1dbg(("ilm_move_v6: FAILOVER, found"
15907 				    " new ilm on %s, group address %s\n",
15908 				    to_ill->ill_name,
15909 				    inet_ntop(AF_INET6,
15910 				    &ilm->ilm_v6addr, buf,
15911 				    sizeof (buf))));
15912 				new_ilm->ilm_refcnt += count;
15913 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15914 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15915 					new_ilm->ilm_is_new = B_TRUE;
15916 				}
15917 			} else {
15918 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
15919 				if (new_ilm == NULL) {
15920 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
15921 					    " multicast address %s : from %s to"
15922 					    " %s failed : ENOMEM \n",
15923 					    inet_ntop(AF_INET6,
15924 					    &ilm->ilm_v6addr, buf,
15925 					    sizeof (buf)), from_ill->ill_name,
15926 					    to_ill->ill_name));
15927 					ilmp = &ilm->ilm_next;
15928 					continue;
15929 				}
15930 				*new_ilm = *ilm;
15931 				new_ilm->ilm_filter = NULL;
15932 				new_ilm->ilm_refcnt = count;
15933 				new_ilm->ilm_timer = INFINITY;
15934 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
15935 				new_ilm->ilm_is_new = B_TRUE;
15936 				/*
15937 				 * If the to_ill has not joined this
15938 				 * group we need to tell the driver in
15939 				 * ill_send_multicast_reqs.
15940 				 */
15941 				if (ilm_lookup_ill_v6(to_ill,
15942 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15943 					new_ilm->ilm_notify_driver = B_TRUE;
15944 
15945 				new_ilm->ilm_ill = to_ill;
15946 				/* Add to the to_ill's list */
15947 				new_ilm->ilm_next = to_ill->ill_ilm;
15948 				to_ill->ill_ilm = new_ilm;
15949 				ASSERT(new_ilm->ilm_ipif == NULL);
15950 			}
15951 			if (ilm->ilm_refcnt == 0) {
15952 				goto bottom;
15953 			} else {
15954 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
15955 				CLEAR_SLIST(new_ilm->ilm_filter);
15956 				ilmp = &ilm->ilm_next;
15957 			}
15958 			continue;
15959 		} else {
15960 			/*
15961 			 * ifindex = 0 means, move everything pointing at
15962 			 * from_ill. We are doing this becuase ill has
15963 			 * either FAILED or became INACTIVE.
15964 			 *
15965 			 * As we would like to move things later back to
15966 			 * from_ill, we want to retain the identity of this
15967 			 * ilm. Thus, we don't blindly increment the reference
15968 			 * count on the ilms matching the address alone. We
15969 			 * need to match on the ilm_orig_index also. new_ilm
15970 			 * was obtained by matching ilm_orig_index also.
15971 			 */
15972 			if (new_ilm != NULL) {
15973 				/*
15974 				 * This is possible only if a previous restore
15975 				 * was incomplete i.e restore to
15976 				 * ilm_orig_ifindex left some ilms because
15977 				 * of some failures. Thus when we are failing
15978 				 * again, we might find our old friends there.
15979 				 */
15980 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
15981 				    " on %s, group address %s\n",
15982 				    to_ill->ill_name,
15983 				    inet_ntop(AF_INET6,
15984 				    &ilm->ilm_v6addr, buf,
15985 				    sizeof (buf))));
15986 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15987 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15988 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15989 					new_ilm->ilm_is_new = B_TRUE;
15990 				}
15991 			} else {
15992 				if (from_ill->ill_ilm_walker_cnt != 0) {
15993 					new_ilm = (ilm_t *)
15994 					    mi_zalloc(sizeof (ilm_t));
15995 					if (new_ilm == NULL) {
15996 						ip0dbg(("ilm_move_v6: "
15997 						    "FAILOVER of IPv6"
15998 						    " multicast address %s : "
15999 						    "from %s to"
16000 						    " %s failed : ENOMEM \n",
16001 						    inet_ntop(AF_INET6,
16002 						    &ilm->ilm_v6addr, buf,
16003 						    sizeof (buf)),
16004 						    from_ill->ill_name,
16005 						    to_ill->ill_name));
16006 
16007 							ilmp = &ilm->ilm_next;
16008 							continue;
16009 					}
16010 					*new_ilm = *ilm;
16011 					new_ilm->ilm_filter = NULL;
16012 				} else {
16013 					*ilmp = ilm->ilm_next;
16014 					new_ilm = ilm;
16015 				}
16016 				/*
16017 				 * If the to_ill has not joined this
16018 				 * group we need to tell the driver in
16019 				 * ill_send_multicast_reqs.
16020 				 */
16021 				if (ilm_lookup_ill_v6(to_ill,
16022 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16023 					new_ilm->ilm_notify_driver = B_TRUE;
16024 
16025 				/* Add to the to_ill's list */
16026 				new_ilm->ilm_next = to_ill->ill_ilm;
16027 				to_ill->ill_ilm = new_ilm;
16028 				ASSERT(ilm->ilm_ipif == NULL);
16029 				new_ilm->ilm_ill = to_ill;
16030 				new_ilm->ilm_is_new = B_TRUE;
16031 			}
16032 
16033 		}
16034 
16035 bottom:
16036 		/*
16037 		 * Revert multicast filter state to (EXCLUDE, NULL).
16038 		 * new_ilm->ilm_is_new should already be set if needed.
16039 		 */
16040 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16041 		CLEAR_SLIST(new_ilm->ilm_filter);
16042 		/*
16043 		 * We allocated/got a new ilm, free the old one.
16044 		 */
16045 		if (new_ilm != ilm) {
16046 			if (from_ill->ill_ilm_walker_cnt == 0) {
16047 				*ilmp = ilm->ilm_next;
16048 				ilm->ilm_next = NULL;
16049 				FREE_SLIST(ilm->ilm_filter);
16050 				FREE_SLIST(ilm->ilm_pendsrcs);
16051 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16052 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16053 				mi_free((char *)ilm);
16054 			} else {
16055 				ilm->ilm_flags |= ILM_DELETED;
16056 				from_ill->ill_ilm_cleanup_reqd = 1;
16057 				ilmp = &ilm->ilm_next;
16058 			}
16059 		}
16060 	}
16061 }
16062 
16063 /*
16064  * Move all the multicast memberships to to_ill. Called when
16065  * an ipif moves from "from_ill" to "to_ill". This function is slightly
16066  * different from IPv6 counterpart as multicast memberships are associated
16067  * with ills in IPv6. This function is called after every ipif is moved
16068  * unlike IPv6, where it is moved only once.
16069  */
16070 static void
16071 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
16072 {
16073 	ilm_t	*ilm;
16074 	ilm_t	*ilm_next;
16075 	ilm_t	*new_ilm;
16076 	ilm_t	**ilmp;
16077 
16078 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16079 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16080 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16081 
16082 	ilmp = &from_ill->ill_ilm;
16083 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16084 
16085 		if (ilm->ilm_flags & ILM_DELETED)
16086 			continue;
16087 
16088 		ilm_next = ilm->ilm_next;
16089 		ASSERT(ilm->ilm_ipif != NULL);
16090 
16091 		if (ilm->ilm_ipif != ipif) {
16092 			ilmp = &ilm->ilm_next;
16093 			continue;
16094 		}
16095 
16096 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16097 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16098 			/*
16099 			 * We joined this in ipif_multicast_up
16100 			 * and we never did an ipif_multicast_down
16101 			 * for IPv4. If nobody else from the userland
16102 			 * has reference, we free the ilm, and later
16103 			 * when this ipif comes up on the new ill,
16104 			 * we will join this again.
16105 			 */
16106 			if (--ilm->ilm_refcnt == 0)
16107 				goto delete_ilm;
16108 
16109 			new_ilm = ilm_lookup_ipif(ipif,
16110 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16111 			if (new_ilm != NULL) {
16112 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16113 				/*
16114 				 * We still need to deal with the from_ill.
16115 				 */
16116 				new_ilm->ilm_is_new = B_TRUE;
16117 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16118 				CLEAR_SLIST(new_ilm->ilm_filter);
16119 				goto delete_ilm;
16120 			}
16121 			/*
16122 			 * If we could not find one e.g. ipif is
16123 			 * still down on to_ill, we add this ilm
16124 			 * on ill_new to preserve the reference
16125 			 * count.
16126 			 */
16127 		}
16128 		/*
16129 		 * When ipifs move, ilms always move with it
16130 		 * to the NEW ill. Thus we should never be
16131 		 * able to find ilm till we really move it here.
16132 		 */
16133 		ASSERT(ilm_lookup_ipif(ipif,
16134 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16135 
16136 		if (from_ill->ill_ilm_walker_cnt != 0) {
16137 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16138 			if (new_ilm == NULL) {
16139 				char buf[INET6_ADDRSTRLEN];
16140 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16141 				    " multicast address %s : "
16142 				    "from %s to"
16143 				    " %s failed : ENOMEM \n",
16144 				    inet_ntop(AF_INET,
16145 				    &ilm->ilm_v6addr, buf,
16146 				    sizeof (buf)),
16147 				    from_ill->ill_name,
16148 				    to_ill->ill_name));
16149 
16150 				ilmp = &ilm->ilm_next;
16151 				continue;
16152 			}
16153 			*new_ilm = *ilm;
16154 			/* We don't want new_ilm linked to ilm's filter list */
16155 			new_ilm->ilm_filter = NULL;
16156 		} else {
16157 			/* Remove from the list */
16158 			*ilmp = ilm->ilm_next;
16159 			new_ilm = ilm;
16160 		}
16161 
16162 		/*
16163 		 * If we have never joined this group on the to_ill
16164 		 * make sure we tell the driver.
16165 		 */
16166 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16167 		    ALL_ZONES) == NULL)
16168 			new_ilm->ilm_notify_driver = B_TRUE;
16169 
16170 		/* Add to the to_ill's list */
16171 		new_ilm->ilm_next = to_ill->ill_ilm;
16172 		to_ill->ill_ilm = new_ilm;
16173 		new_ilm->ilm_is_new = B_TRUE;
16174 
16175 		/*
16176 		 * Revert multicast filter state to (EXCLUDE, NULL)
16177 		 */
16178 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16179 		CLEAR_SLIST(new_ilm->ilm_filter);
16180 
16181 		/*
16182 		 * Delete only if we have allocated a new ilm.
16183 		 */
16184 		if (new_ilm != ilm) {
16185 delete_ilm:
16186 			if (from_ill->ill_ilm_walker_cnt == 0) {
16187 				/* Remove from the list */
16188 				*ilmp = ilm->ilm_next;
16189 				ilm->ilm_next = NULL;
16190 				FREE_SLIST(ilm->ilm_filter);
16191 				FREE_SLIST(ilm->ilm_pendsrcs);
16192 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16193 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16194 				mi_free((char *)ilm);
16195 			} else {
16196 				ilm->ilm_flags |= ILM_DELETED;
16197 				from_ill->ill_ilm_cleanup_reqd = 1;
16198 				ilmp = &ilm->ilm_next;
16199 			}
16200 		}
16201 	}
16202 }
16203 
16204 static uint_t
16205 ipif_get_id(ill_t *ill, uint_t id)
16206 {
16207 	uint_t	unit;
16208 	ipif_t	*tipif;
16209 	boolean_t found = B_FALSE;
16210 
16211 	/*
16212 	 * During failback, we want to go back to the same id
16213 	 * instead of the smallest id so that the original
16214 	 * configuration is maintained. id is non-zero in that
16215 	 * case.
16216 	 */
16217 	if (id != 0) {
16218 		/*
16219 		 * While failing back, if we still have an ipif with
16220 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16221 		 * as soon as we return from this function. It was
16222 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16223 		 * we can choose the smallest id. Thus we return zero
16224 		 * in that case ignoring the hint.
16225 		 */
16226 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16227 			return (0);
16228 		for (tipif = ill->ill_ipif; tipif != NULL;
16229 		    tipif = tipif->ipif_next) {
16230 			if (tipif->ipif_id == id) {
16231 				found = B_TRUE;
16232 				break;
16233 			}
16234 		}
16235 		/*
16236 		 * If somebody already plumbed another logical
16237 		 * with the same id, we won't be able to find it.
16238 		 */
16239 		if (!found)
16240 			return (id);
16241 	}
16242 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16243 		found = B_FALSE;
16244 		for (tipif = ill->ill_ipif; tipif != NULL;
16245 		    tipif = tipif->ipif_next) {
16246 			if (tipif->ipif_id == unit) {
16247 				found = B_TRUE;
16248 				break;
16249 			}
16250 		}
16251 		if (!found)
16252 			break;
16253 	}
16254 	return (unit);
16255 }
16256 
16257 /* ARGSUSED */
16258 static int
16259 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16260     ipif_t **rep_ipif_ptr)
16261 {
16262 	ill_t	*from_ill;
16263 	ipif_t	*rep_ipif;
16264 	ipif_t	**ipifp;
16265 	uint_t	unit;
16266 	int err = 0;
16267 	ipif_t	*to_ipif;
16268 	struct iocblk	*iocp;
16269 	boolean_t failback_cmd;
16270 	boolean_t remove_ipif;
16271 	int	rc;
16272 
16273 	ASSERT(IAM_WRITER_ILL(to_ill));
16274 	ASSERT(IAM_WRITER_IPIF(ipif));
16275 
16276 	iocp = (struct iocblk *)mp->b_rptr;
16277 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16278 	remove_ipif = B_FALSE;
16279 
16280 	from_ill = ipif->ipif_ill;
16281 
16282 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16283 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16284 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16285 
16286 	/*
16287 	 * Don't move LINK LOCAL addresses as they are tied to
16288 	 * physical interface.
16289 	 */
16290 	if (from_ill->ill_isv6 &&
16291 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16292 		ipif->ipif_was_up = B_FALSE;
16293 		IPIF_UNMARK_MOVING(ipif);
16294 		return (0);
16295 	}
16296 
16297 	/*
16298 	 * We set the ipif_id to maximum so that the search for
16299 	 * ipif_id will pick the lowest number i.e 0 in the
16300 	 * following 2 cases :
16301 	 *
16302 	 * 1) We have a replacement ipif at the head of to_ill.
16303 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16304 	 *    on to_ill and hence the MOVE might fail. We want to
16305 	 *    remove it only if we could move the ipif. Thus, by
16306 	 *    setting it to the MAX value, we make the search in
16307 	 *    ipif_get_id return the zeroth id.
16308 	 *
16309 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16310 	 *    we might just have a zero address plumbed on the ipif
16311 	 *    with zero id in the case of IPv4. We remove that while
16312 	 *    doing the failback. We want to remove it only if we
16313 	 *    could move the ipif. Thus, by setting it to the MAX
16314 	 *    value, we make the search in ipif_get_id return the
16315 	 *    zeroth id.
16316 	 *
16317 	 * Both (1) and (2) are done only when when we are moving
16318 	 * an ipif (either due to failover/failback) which originally
16319 	 * belonged to this interface i.e the ipif_orig_ifindex is
16320 	 * the same as to_ill's ifindex. This is needed so that
16321 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16322 	 * from B -> A (B is being removed from the group) and
16323 	 * FAILBACK from A -> B restores the original configuration.
16324 	 * Without the check for orig_ifindex, the second FAILOVER
16325 	 * could make the ipif belonging to B replace the A's zeroth
16326 	 * ipif and the subsequent failback re-creating the replacement
16327 	 * ipif again.
16328 	 *
16329 	 * NOTE : We created the replacement ipif when we did a
16330 	 * FAILOVER (See below). We could check for FAILBACK and
16331 	 * then look for replacement ipif to be removed. But we don't
16332 	 * want to do that because we wan't to allow the possibility
16333 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16334 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16335 	 * from B -> A.
16336 	 */
16337 	to_ipif = to_ill->ill_ipif;
16338 	if ((to_ill->ill_phyint->phyint_ifindex ==
16339 	    ipif->ipif_orig_ifindex) &&
16340 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16341 		ASSERT(to_ipif->ipif_id == 0);
16342 		remove_ipif = B_TRUE;
16343 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16344 	}
16345 	/*
16346 	 * Find the lowest logical unit number on the to_ill.
16347 	 * If we are failing back, try to get the original id
16348 	 * rather than the lowest one so that the original
16349 	 * configuration is maintained.
16350 	 *
16351 	 * XXX need a better scheme for this.
16352 	 */
16353 	if (failback_cmd) {
16354 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16355 	} else {
16356 		unit = ipif_get_id(to_ill, 0);
16357 	}
16358 
16359 	/* Reset back to zero in case we fail below */
16360 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16361 		to_ipif->ipif_id = 0;
16362 
16363 	if (unit == ip_addrs_per_if) {
16364 		ipif->ipif_was_up = B_FALSE;
16365 		IPIF_UNMARK_MOVING(ipif);
16366 		return (EINVAL);
16367 	}
16368 
16369 	/*
16370 	 * ipif is ready to move from "from_ill" to "to_ill".
16371 	 *
16372 	 * 1) If we are moving ipif with id zero, create a
16373 	 *    replacement ipif for this ipif on from_ill. If this fails
16374 	 *    fail the MOVE operation.
16375 	 *
16376 	 * 2) Remove the replacement ipif on to_ill if any.
16377 	 *    We could remove the replacement ipif when we are moving
16378 	 *    the ipif with id zero. But what if somebody already
16379 	 *    unplumbed it ? Thus we always remove it if it is present.
16380 	 *    We want to do it only if we are sure we are going to
16381 	 *    move the ipif to to_ill which is why there are no
16382 	 *    returns due to error till ipif is linked to to_ill.
16383 	 *    Note that the first ipif that we failback will always
16384 	 *    be zero if it is present.
16385 	 */
16386 	if (ipif->ipif_id == 0) {
16387 		ipaddr_t inaddr_any = INADDR_ANY;
16388 
16389 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16390 		if (rep_ipif == NULL) {
16391 			ipif->ipif_was_up = B_FALSE;
16392 			IPIF_UNMARK_MOVING(ipif);
16393 			return (ENOMEM);
16394 		}
16395 		*rep_ipif = ipif_zero;
16396 		/*
16397 		 * Before we put the ipif on the list, store the addresses
16398 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16399 		 * assumes so. This logic is not any different from what
16400 		 * ipif_allocate does.
16401 		 */
16402 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16403 		    &rep_ipif->ipif_v6lcl_addr);
16404 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16405 		    &rep_ipif->ipif_v6src_addr);
16406 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16407 		    &rep_ipif->ipif_v6subnet);
16408 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16409 		    &rep_ipif->ipif_v6net_mask);
16410 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16411 		    &rep_ipif->ipif_v6brd_addr);
16412 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16413 		    &rep_ipif->ipif_v6pp_dst_addr);
16414 		/*
16415 		 * We mark IPIF_NOFAILOVER so that this can never
16416 		 * move.
16417 		 */
16418 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16419 		rep_ipif->ipif_flags &= ~IPIF_UP;
16420 		rep_ipif->ipif_replace_zero = B_TRUE;
16421 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16422 		    MUTEX_DEFAULT, NULL);
16423 		rep_ipif->ipif_id = 0;
16424 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16425 		rep_ipif->ipif_ill = from_ill;
16426 		rep_ipif->ipif_orig_ifindex =
16427 		    from_ill->ill_phyint->phyint_ifindex;
16428 		/* Insert at head */
16429 		rep_ipif->ipif_next = from_ill->ill_ipif;
16430 		from_ill->ill_ipif = rep_ipif;
16431 		/*
16432 		 * We don't really care to let apps know about
16433 		 * this interface.
16434 		 */
16435 	}
16436 
16437 	if (remove_ipif) {
16438 		/*
16439 		 * We set to a max value above for this case to get
16440 		 * id zero. ASSERT that we did get one.
16441 		 */
16442 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16443 		rep_ipif = to_ipif;
16444 		to_ill->ill_ipif = rep_ipif->ipif_next;
16445 		rep_ipif->ipif_next = NULL;
16446 		/*
16447 		 * If some apps scanned and find this interface,
16448 		 * it is time to let them know, so that they can
16449 		 * delete it.
16450 		 */
16451 
16452 		*rep_ipif_ptr = rep_ipif;
16453 	}
16454 
16455 	/* Get it out of the ILL interface list. */
16456 	ipifp = &ipif->ipif_ill->ill_ipif;
16457 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16458 		if (*ipifp == ipif) {
16459 			*ipifp = ipif->ipif_next;
16460 			break;
16461 		}
16462 	}
16463 
16464 	/* Assign the new ill */
16465 	ipif->ipif_ill = to_ill;
16466 	ipif->ipif_id = unit;
16467 	/* id has already been checked */
16468 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16469 	ASSERT(rc == 0);
16470 	/* Let SCTP update its list */
16471 	sctp_move_ipif(ipif, from_ill, to_ill);
16472 	/*
16473 	 * Handle the failover and failback of ipif_t between
16474 	 * ill_t that have differing maximum mtu values.
16475 	 */
16476 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16477 		if (ipif->ipif_saved_mtu == 0) {
16478 			/*
16479 			 * As this ipif_t is moving to an ill_t
16480 			 * that has a lower ill_max_mtu, its
16481 			 * ipif_mtu needs to be saved so it can
16482 			 * be restored during failback or during
16483 			 * failover to an ill_t which has a
16484 			 * higher ill_max_mtu.
16485 			 */
16486 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16487 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16488 		} else {
16489 			/*
16490 			 * The ipif_t is, once again, moving to
16491 			 * an ill_t that has a lower maximum mtu
16492 			 * value.
16493 			 */
16494 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16495 		}
16496 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16497 	    ipif->ipif_saved_mtu != 0) {
16498 		/*
16499 		 * The mtu of this ipif_t had to be reduced
16500 		 * during an earlier failover; this is an
16501 		 * opportunity for it to be increased (either as
16502 		 * part of another failover or a failback).
16503 		 */
16504 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16505 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16506 			ipif->ipif_saved_mtu = 0;
16507 		} else {
16508 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16509 		}
16510 	}
16511 
16512 	/*
16513 	 * We preserve all the other fields of the ipif including
16514 	 * ipif_saved_ire_mp. The routes that are saved here will
16515 	 * be recreated on the new interface and back on the old
16516 	 * interface when we move back.
16517 	 */
16518 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16519 
16520 	return (err);
16521 }
16522 
16523 static int
16524 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16525     int ifindex, ipif_t **rep_ipif_ptr)
16526 {
16527 	ipif_t *mipif;
16528 	ipif_t *ipif_next;
16529 	int err;
16530 
16531 	/*
16532 	 * We don't really try to MOVE back things if some of the
16533 	 * operations fail. The daemon will take care of moving again
16534 	 * later on.
16535 	 */
16536 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16537 		ipif_next = mipif->ipif_next;
16538 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16539 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16540 
16541 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16542 
16543 			/*
16544 			 * When the MOVE fails, it is the job of the
16545 			 * application to take care of this properly
16546 			 * i.e try again if it is ENOMEM.
16547 			 */
16548 			if (mipif->ipif_ill != from_ill) {
16549 				/*
16550 				 * ipif has moved.
16551 				 *
16552 				 * Move the multicast memberships associated
16553 				 * with this ipif to the new ill. For IPv6, we
16554 				 * do it once after all the ipifs are moved
16555 				 * (in ill_move) as they are not associated
16556 				 * with ipifs.
16557 				 *
16558 				 * We need to move the ilms as the ipif has
16559 				 * already been moved to a new ill even
16560 				 * in the case of errors. Neither
16561 				 * ilm_free(ipif) will find the ilm
16562 				 * when somebody unplumbs this ipif nor
16563 				 * ilm_delete(ilm) will be able to find the
16564 				 * ilm, if we don't move now.
16565 				 */
16566 				if (!from_ill->ill_isv6)
16567 					ilm_move_v4(from_ill, to_ill, mipif);
16568 			}
16569 
16570 			if (err != 0)
16571 				return (err);
16572 		}
16573 	}
16574 	return (0);
16575 }
16576 
16577 static int
16578 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16579 {
16580 	int ifindex;
16581 	int err;
16582 	struct iocblk	*iocp;
16583 	ipif_t	*ipif;
16584 	ipif_t *rep_ipif_ptr = NULL;
16585 	ipif_t	*from_ipif = NULL;
16586 	boolean_t check_rep_if = B_FALSE;
16587 
16588 	iocp = (struct iocblk *)mp->b_rptr;
16589 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16590 		/*
16591 		 * Move everything pointing at from_ill to to_ill.
16592 		 * We acheive this by passing in 0 as ifindex.
16593 		 */
16594 		ifindex = 0;
16595 	} else {
16596 		/*
16597 		 * Move everything pointing at from_ill whose original
16598 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16599 		 * We acheive this by passing in ifindex rather than 0.
16600 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16601 		 */
16602 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16603 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16604 	}
16605 
16606 	/*
16607 	 * Determine if there is at least one ipif that would move from
16608 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16609 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16610 	 * the move, in which case we need to quiesce the replacement ipif also.
16611 	 */
16612 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16613 	    from_ipif = from_ipif->ipif_next) {
16614 		if (((ifindex == 0) ||
16615 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16616 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16617 			check_rep_if = B_TRUE;
16618 			break;
16619 		}
16620 	}
16621 
16622 
16623 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16624 
16625 	GRAB_ILL_LOCKS(from_ill, to_ill);
16626 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16627 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16628 		    mp, ILL_MOVE_OK);
16629 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16630 		return (EINPROGRESS);
16631 	}
16632 
16633 	/* Check if the replacement ipif is quiescent to delete */
16634 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16635 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16636 		to_ill->ill_ipif->ipif_state_flags |=
16637 		    IPIF_MOVING | IPIF_CHANGING;
16638 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16639 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16640 			    mp, ILL_MOVE_OK);
16641 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16642 			return (EINPROGRESS);
16643 		}
16644 	}
16645 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16646 
16647 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16648 	rw_enter(&ill_g_lock, RW_WRITER);
16649 	GRAB_ILL_LOCKS(from_ill, to_ill);
16650 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16651 
16652 	/* ilm_move is done inside ipif_move for IPv4 */
16653 	if (err == 0 && from_ill->ill_isv6)
16654 		ilm_move_v6(from_ill, to_ill, ifindex);
16655 
16656 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16657 	rw_exit(&ill_g_lock);
16658 
16659 	/*
16660 	 * send rts messages and multicast messages.
16661 	 */
16662 	if (rep_ipif_ptr != NULL) {
16663 		ip_rts_ifmsg(rep_ipif_ptr);
16664 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16665 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16666 		mi_free(rep_ipif_ptr);
16667 	}
16668 
16669 	ilm_send_multicast_reqs(from_ill, to_ill);
16670 
16671 	conn_move_ill(from_ill, to_ill, ifindex);
16672 
16673 	return (err);
16674 }
16675 
16676 /*
16677  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16678  * Also checks for the validity of the arguments.
16679  * Note: We are already exclusive inside the from group.
16680  * It is upto the caller to release refcnt on the to_ill's.
16681  */
16682 static int
16683 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16684     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16685 {
16686 	int dst_index;
16687 	ipif_t *ipif_v4, *ipif_v6;
16688 	struct lifreq *lifr;
16689 	mblk_t *mp1;
16690 	boolean_t exists;
16691 	sin_t	*sin;
16692 	int	err = 0;
16693 
16694 	if ((mp1 = mp->b_cont) == NULL)
16695 		return (EPROTO);
16696 
16697 	if ((mp1 = mp1->b_cont) == NULL)
16698 		return (EPROTO);
16699 
16700 	lifr = (struct lifreq *)mp1->b_rptr;
16701 	sin = (sin_t *)&lifr->lifr_addr;
16702 
16703 	/*
16704 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16705 	 * specific operations.
16706 	 */
16707 	if (sin->sin_family != AF_UNSPEC)
16708 		return (EINVAL);
16709 
16710 	/*
16711 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16712 	 * NULLs for the last 4 args and we know the lookup won't fail
16713 	 * with EINPROGRESS.
16714 	 */
16715 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16716 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16717 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16718 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16719 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16720 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16721 
16722 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16723 		return (ENXIO);
16724 
16725 	if (ipif_v4 != NULL) {
16726 		ASSERT(ipif_v4->ipif_refcnt != 0);
16727 		if (ipif_v4->ipif_id != 0) {
16728 			err = EINVAL;
16729 			goto done;
16730 		}
16731 
16732 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16733 		*ill_from_v4 = ipif_v4->ipif_ill;
16734 	}
16735 
16736 	if (ipif_v6 != NULL) {
16737 		ASSERT(ipif_v6->ipif_refcnt != 0);
16738 		if (ipif_v6->ipif_id != 0) {
16739 			err = EINVAL;
16740 			goto done;
16741 		}
16742 
16743 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16744 		*ill_from_v6 = ipif_v6->ipif_ill;
16745 	}
16746 
16747 	err = 0;
16748 	dst_index = lifr->lifr_movetoindex;
16749 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16750 	    q, mp, ip_process_ioctl, &err);
16751 	if (err != 0) {
16752 		/*
16753 		 * There could be only v6.
16754 		 */
16755 		if (err != ENXIO)
16756 			goto done;
16757 		err = 0;
16758 	}
16759 
16760 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16761 	    q, mp, ip_process_ioctl, &err);
16762 	if (err != 0) {
16763 		if (err != ENXIO)
16764 			goto done;
16765 		if (*ill_to_v4 == NULL) {
16766 			err = ENXIO;
16767 			goto done;
16768 		}
16769 		err = 0;
16770 	}
16771 
16772 	/*
16773 	 * If we have something to MOVE i.e "from" not NULL,
16774 	 * "to" should be non-NULL.
16775 	 */
16776 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16777 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16778 		err = EINVAL;
16779 	}
16780 
16781 done:
16782 	if (ipif_v4 != NULL)
16783 		ipif_refrele(ipif_v4);
16784 	if (ipif_v6 != NULL)
16785 		ipif_refrele(ipif_v6);
16786 	return (err);
16787 }
16788 
16789 /*
16790  * FAILOVER and FAILBACK are modelled as MOVE operations.
16791  *
16792  * We don't check whether the MOVE is within the same group or
16793  * not, because this ioctl can be used as a generic mechanism
16794  * to failover from interface A to B, though things will function
16795  * only if they are really part of the same group. Moreover,
16796  * all ipifs may be down and hence temporarily out of the group.
16797  *
16798  * ipif's that need to be moved are first brought down; V4 ipifs are brought
16799  * down first and then V6.  For each we wait for the ipif's to become quiescent.
16800  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
16801  * have been deleted and there are no active references. Once quiescent the
16802  * ipif's are moved and brought up on the new ill.
16803  *
16804  * Normally the source ill and destination ill belong to the same IPMP group
16805  * and hence the same ipsq_t. In the event they don't belong to the same
16806  * same group the two ipsq's are first merged into one ipsq - that of the
16807  * to_ill. The multicast memberships on the source and destination ill cannot
16808  * change during the move operation since multicast joins/leaves also have to
16809  * execute on the same ipsq and are hence serialized.
16810  */
16811 /* ARGSUSED */
16812 int
16813 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16814     ip_ioctl_cmd_t *ipip, void *ifreq)
16815 {
16816 	ill_t *ill_to_v4 = NULL;
16817 	ill_t *ill_to_v6 = NULL;
16818 	ill_t *ill_from_v4 = NULL;
16819 	ill_t *ill_from_v6 = NULL;
16820 	int err = 0;
16821 
16822 	/*
16823 	 * setup from and to ill's, we can get EINPROGRESS only for
16824 	 * to_ill's.
16825 	 */
16826 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
16827 	    &ill_to_v4, &ill_to_v6);
16828 
16829 	if (err != 0) {
16830 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
16831 		goto done;
16832 	}
16833 
16834 	/*
16835 	 * nothing to do.
16836 	 */
16837 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
16838 		goto done;
16839 	}
16840 
16841 	/*
16842 	 * nothing to do.
16843 	 */
16844 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
16845 		goto done;
16846 	}
16847 
16848 	/*
16849 	 * Mark the ill as changing.
16850 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
16851 	 * in ill_up_ipifs in case of error they are cleared below.
16852 	 */
16853 
16854 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16855 	if (ill_from_v4 != NULL)
16856 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
16857 	if (ill_from_v6 != NULL)
16858 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
16859 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16860 
16861 	/*
16862 	 * Make sure that both src and dst are
16863 	 * in the same syncq group. If not make it happen.
16864 	 * We are not holding any locks because we are the writer
16865 	 * on the from_ipsq and we will hold locks in ill_merge_groups
16866 	 * to protect to_ipsq against changing.
16867 	 */
16868 	if (ill_from_v4 != NULL) {
16869 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
16870 		    ill_to_v4->ill_phyint->phyint_ipsq) {
16871 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
16872 			    NULL, mp, q);
16873 			goto err_ret;
16874 
16875 		}
16876 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
16877 	} else {
16878 
16879 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
16880 		    ill_to_v6->ill_phyint->phyint_ipsq) {
16881 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
16882 			    NULL, mp, q);
16883 			goto err_ret;
16884 
16885 		}
16886 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
16887 	}
16888 
16889 	/*
16890 	 * Now that the ipsq's have been merged and we are the writer
16891 	 * lets mark to_ill as changing as well.
16892 	 */
16893 
16894 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16895 	if (ill_to_v4 != NULL)
16896 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
16897 	if (ill_to_v6 != NULL)
16898 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
16899 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16900 
16901 	/*
16902 	 * Its ok for us to proceed with the move even if
16903 	 * ill_pending_mp is non null on one of the from ill's as the reply
16904 	 * should not be looking at the ipif, it should only care about the
16905 	 * ill itself.
16906 	 */
16907 
16908 	/*
16909 	 * lets move ipv4 first.
16910 	 */
16911 	if (ill_from_v4 != NULL) {
16912 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
16913 		ill_from_v4->ill_move_in_progress = B_TRUE;
16914 		ill_to_v4->ill_move_in_progress = B_TRUE;
16915 		ill_to_v4->ill_move_peer = ill_from_v4;
16916 		ill_from_v4->ill_move_peer = ill_to_v4;
16917 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
16918 	}
16919 
16920 	/*
16921 	 * Now lets move ipv6.
16922 	 */
16923 	if (err == 0 && ill_from_v6 != NULL) {
16924 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
16925 		ill_from_v6->ill_move_in_progress = B_TRUE;
16926 		ill_to_v6->ill_move_in_progress = B_TRUE;
16927 		ill_to_v6->ill_move_peer = ill_from_v6;
16928 		ill_from_v6->ill_move_peer = ill_to_v6;
16929 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
16930 	}
16931 
16932 err_ret:
16933 	/*
16934 	 * EINPROGRESS means we are waiting for the ipif's that need to be
16935 	 * moved to become quiescent.
16936 	 */
16937 	if (err == EINPROGRESS) {
16938 		goto done;
16939 	}
16940 
16941 	/*
16942 	 * if err is set ill_up_ipifs will not be called
16943 	 * lets clear the flags.
16944 	 */
16945 
16946 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16947 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16948 	/*
16949 	 * Some of the clearing may be redundant. But it is simple
16950 	 * not making any extra checks.
16951 	 */
16952 	if (ill_from_v6 != NULL) {
16953 		ill_from_v6->ill_move_in_progress = B_FALSE;
16954 		ill_from_v6->ill_move_peer = NULL;
16955 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
16956 	}
16957 	if (ill_from_v4 != NULL) {
16958 		ill_from_v4->ill_move_in_progress = B_FALSE;
16959 		ill_from_v4->ill_move_peer = NULL;
16960 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
16961 	}
16962 	if (ill_to_v6 != NULL) {
16963 		ill_to_v6->ill_move_in_progress = B_FALSE;
16964 		ill_to_v6->ill_move_peer = NULL;
16965 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
16966 	}
16967 	if (ill_to_v4 != NULL) {
16968 		ill_to_v4->ill_move_in_progress = B_FALSE;
16969 		ill_to_v4->ill_move_peer = NULL;
16970 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
16971 	}
16972 
16973 	/*
16974 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
16975 	 * Do this always to maintain proper state i.e even in case of errors.
16976 	 * As phyint_inactive looks at both v4 and v6 interfaces,
16977 	 * we need not call on both v4 and v6 interfaces.
16978 	 */
16979 	if (ill_from_v4 != NULL) {
16980 		if ((ill_from_v4->ill_phyint->phyint_flags &
16981 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
16982 			phyint_inactive(ill_from_v4->ill_phyint);
16983 		}
16984 	} else if (ill_from_v6 != NULL) {
16985 		if ((ill_from_v6->ill_phyint->phyint_flags &
16986 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
16987 			phyint_inactive(ill_from_v6->ill_phyint);
16988 		}
16989 	}
16990 
16991 	if (ill_to_v4 != NULL) {
16992 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
16993 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
16994 		}
16995 	} else if (ill_to_v6 != NULL) {
16996 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
16997 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
16998 		}
16999 	}
17000 
17001 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17002 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17003 
17004 no_err:
17005 	/*
17006 	 * lets bring the interfaces up on the to_ill.
17007 	 */
17008 	if (err == 0) {
17009 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
17010 		    q, mp);
17011 	}
17012 done:
17013 
17014 	if (ill_to_v4 != NULL) {
17015 		ill_refrele(ill_to_v4);
17016 	}
17017 	if (ill_to_v6 != NULL) {
17018 		ill_refrele(ill_to_v6);
17019 	}
17020 
17021 	return (err);
17022 }
17023 
17024 static void
17025 ill_dl_down(ill_t *ill)
17026 {
17027 	/*
17028 	 * The ill is down; unbind but stay attached since we're still
17029 	 * associated with a PPA.
17030 	 */
17031 	mblk_t	*mp = ill->ill_unbind_mp;
17032 
17033 	ill->ill_unbind_mp = NULL;
17034 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
17035 	if (mp != NULL) {
17036 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
17037 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
17038 		    ill->ill_name));
17039 		ill_dlpi_send(ill, mp);
17040 	}
17041 
17042 	/*
17043 	 * Toss all of our multicast memberships.  We could keep them, but
17044 	 * then we'd have to do bookkeeping of any joins and leaves performed
17045 	 * by the application while the the interface is down (we can't just
17046 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
17047 	 * on a downed interface).
17048 	 */
17049 	ill_leave_multicast(ill);
17050 
17051 	mutex_enter(&ill->ill_lock);
17052 	ill->ill_dl_up = 0;
17053 	mutex_exit(&ill->ill_lock);
17054 }
17055 
17056 void
17057 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
17058 {
17059 	union DL_primitives *dlp;
17060 	t_uscalar_t prim;
17061 
17062 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17063 
17064 	dlp = (union DL_primitives *)mp->b_rptr;
17065 	prim = dlp->dl_primitive;
17066 
17067 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
17068 		dlpi_prim_str(prim), prim, ill->ill_name));
17069 
17070 	switch (prim) {
17071 	case DL_PHYS_ADDR_REQ:
17072 	{
17073 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
17074 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
17075 		break;
17076 	}
17077 	case DL_BIND_REQ:
17078 		mutex_enter(&ill->ill_lock);
17079 		ill->ill_state_flags &= ~ILL_DL_UNBIND_DONE;
17080 		mutex_exit(&ill->ill_lock);
17081 		break;
17082 	}
17083 
17084 	ill->ill_dlpi_pending = prim;
17085 
17086 	/*
17087 	 * Some drivers send M_FLUSH up to IP as part of unbind
17088 	 * request.  When this M_FLUSH is sent back to the driver,
17089 	 * this can go after we send the detach request if the
17090 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17091 	 * to the M_FLUSH in ip_rput and locally generate another
17092 	 * M_FLUSH for the correctness.  This will get freed in
17093 	 * ip_wput_nondata.
17094 	 */
17095 	if (prim == DL_UNBIND_REQ)
17096 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17097 
17098 	putnext(ill->ill_wq, mp);
17099 }
17100 
17101 /*
17102  * Send a DLPI control message to the driver but make sure there
17103  * is only one outstanding message. Uses ill_dlpi_pending to tell
17104  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17105  * when an ACK or a NAK is received to process the next queued message.
17106  *
17107  * We don't protect ill_dlpi_pending with any lock. This is okay as
17108  * every place where its accessed, ip is exclusive while accessing
17109  * ill_dlpi_pending except when this function is called from ill_init()
17110  */
17111 void
17112 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17113 {
17114 	mblk_t **mpp;
17115 
17116 	ASSERT(IAM_WRITER_ILL(ill));
17117 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17118 
17119 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17120 		/* Must queue message. Tail insertion */
17121 		mpp = &ill->ill_dlpi_deferred;
17122 		while (*mpp != NULL)
17123 			mpp = &((*mpp)->b_next);
17124 
17125 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17126 		    ill->ill_name));
17127 
17128 		*mpp = mp;
17129 		return;
17130 	}
17131 
17132 	ill_dlpi_dispatch(ill, mp);
17133 }
17134 
17135 /*
17136  * Called when an DLPI control message has been acked or nacked to
17137  * send down the next queued message (if any).
17138  */
17139 void
17140 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17141 {
17142 	mblk_t *mp;
17143 
17144 	ASSERT(IAM_WRITER_ILL(ill));
17145 
17146 	ASSERT(prim != DL_PRIM_INVAL);
17147 	if (ill->ill_dlpi_pending != prim) {
17148 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17149 			(void) mi_strlog(ill->ill_rq, 1,
17150 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17151 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17152 			    dlpi_prim_str(prim), ill->ill_name);
17153 		} else {
17154 			(void) mi_strlog(ill->ill_rq, 1,
17155 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17156 			    "ill_dlpi_done: unexpected ack for %s from %s "
17157 			    "(expecting ack for %s)\n",
17158 			    dlpi_prim_str(prim), ill->ill_name,
17159 			    dlpi_prim_str(ill->ill_dlpi_pending));
17160 		}
17161 		return;
17162 	}
17163 
17164 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17165 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17166 
17167 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17168 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17169 		return;
17170 	}
17171 
17172 	ill->ill_dlpi_deferred = mp->b_next;
17173 	mp->b_next = NULL;
17174 
17175 	ill_dlpi_dispatch(ill, mp);
17176 }
17177 
17178 void
17179 conn_delete_ire(conn_t *connp, caddr_t arg)
17180 {
17181 	ipif_t	*ipif = (ipif_t *)arg;
17182 	ire_t	*ire;
17183 
17184 	/*
17185 	 * Look at the cached ires on conns which has pointers to ipifs.
17186 	 * We just call ire_refrele which clears up the reference
17187 	 * to ire. Called when a conn closes. Also called from ipif_free
17188 	 * to cleanup indirect references to the stale ipif via the cached ire.
17189 	 */
17190 	mutex_enter(&connp->conn_lock);
17191 	ire = connp->conn_ire_cache;
17192 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17193 		connp->conn_ire_cache = NULL;
17194 		mutex_exit(&connp->conn_lock);
17195 		IRE_REFRELE_NOTR(ire);
17196 		return;
17197 	}
17198 	mutex_exit(&connp->conn_lock);
17199 
17200 }
17201 
17202 /*
17203  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17204  * of IREs. Those IREs may have been previously cached in the conn structure.
17205  * This ipcl_walk() walker function releases all references to such IREs based
17206  * on the condemned flag.
17207  */
17208 /* ARGSUSED */
17209 void
17210 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17211 {
17212 	ire_t	*ire;
17213 
17214 	mutex_enter(&connp->conn_lock);
17215 	ire = connp->conn_ire_cache;
17216 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17217 		connp->conn_ire_cache = NULL;
17218 		mutex_exit(&connp->conn_lock);
17219 		IRE_REFRELE_NOTR(ire);
17220 		return;
17221 	}
17222 	mutex_exit(&connp->conn_lock);
17223 }
17224 
17225 /*
17226  * Take down a specific interface, but don't lose any information about it.
17227  * Also delete interface from its interface group (ifgrp).
17228  * (Always called as writer.)
17229  * This function goes through the down sequence even if the interface is
17230  * already down. There are 2 reasons.
17231  * a. Currently we permit interface routes that depend on down interfaces
17232  *    to be added. This behaviour itself is questionable. However it appears
17233  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17234  *    time. We go thru the cleanup in order to remove these routes.
17235  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17236  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17237  *    down, but we need to cleanup i.e. do ill_dl_down and
17238  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17239  *
17240  * IP-MT notes:
17241  *
17242  * Model of reference to interfaces.
17243  *
17244  * The following members in ipif_t track references to the ipif.
17245  *	int     ipif_refcnt;    Active reference count
17246  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17247  * The following members in ill_t track references to the ill.
17248  *	int             ill_refcnt;     active refcnt
17249  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17250  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17251  *
17252  * Reference to an ipif or ill can be obtained in any of the following ways.
17253  *
17254  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17255  * Pointers to ipif / ill from other data structures viz ire and conn.
17256  * Implicit reference to the ipif / ill by holding a reference to the ire.
17257  *
17258  * The ipif/ill lookup functions return a reference held ipif / ill.
17259  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17260  * This is a purely dynamic reference count associated with threads holding
17261  * references to the ipif / ill. Pointers from other structures do not
17262  * count towards this reference count.
17263  *
17264  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17265  * ipif/ill. This is incremented whenever a new ire is created referencing the
17266  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17267  * actually added to the ire hash table. The count is decremented in
17268  * ire_inactive where the ire is destroyed.
17269  *
17270  * nce's reference ill's thru nce_ill and the count of nce's associated with
17271  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17272  * ndp_add() where the nce is actually added to the table. Similarly it is
17273  * decremented in ndp_inactive where the nce is destroyed.
17274  *
17275  * Flow of ioctls involving interface down/up
17276  *
17277  * The following is the sequence of an attempt to set some critical flags on an
17278  * up interface.
17279  * ip_sioctl_flags
17280  * ipif_down
17281  * wait for ipif to be quiescent
17282  * ipif_down_tail
17283  * ip_sioctl_flags_tail
17284  *
17285  * All set ioctls that involve down/up sequence would have a skeleton similar
17286  * to the above. All the *tail functions are called after the refcounts have
17287  * dropped to the appropriate values.
17288  *
17289  * The mechanism to quiesce an ipif is as follows.
17290  *
17291  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17292  * on the ipif. Callers either pass a flag requesting wait or the lookup
17293  *  functions will return NULL.
17294  *
17295  * Delete all ires referencing this ipif
17296  *
17297  * Any thread attempting to do an ipif_refhold on an ipif that has been
17298  * obtained thru a cached pointer will first make sure that
17299  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17300  * increment the refcount.
17301  *
17302  * The above guarantees that the ipif refcount will eventually come down to
17303  * zero and the ipif will quiesce, once all threads that currently hold a
17304  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17305  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17306  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17307  * drop to zero.
17308  *
17309  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17310  *
17311  * Threads trying to lookup an ipif or ill can pass a flag requesting
17312  * wait and restart if the ipif / ill cannot be looked up currently.
17313  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17314  * failure if the ipif is currently undergoing an exclusive operation, and
17315  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17316  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17317  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17318  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17319  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17320  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17321  * until we release the ipsq_lock, even though the the ill/ipif state flags
17322  * can change after we drop the ill_lock.
17323  *
17324  * An attempt to send out a packet using an ipif that is currently
17325  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17326  * operation and restart it later when the exclusive condition on the ipif ends.
17327  * This is an example of not passing the wait flag to the lookup functions. For
17328  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17329  * out a multicast packet on that ipif will fail while the ipif is
17330  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17331  * currently IPIF_CHANGING will also fail.
17332  */
17333 int
17334 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17335 {
17336 	ill_t		*ill = ipif->ipif_ill;
17337 	phyint_t	*phyi;
17338 	conn_t		*connp;
17339 	boolean_t	success;
17340 	boolean_t	ipif_was_up = B_FALSE;
17341 
17342 	ASSERT(IAM_WRITER_IPIF(ipif));
17343 
17344 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17345 
17346 	if (ipif->ipif_flags & IPIF_UP) {
17347 		mutex_enter(&ill->ill_lock);
17348 		ipif->ipif_flags &= ~IPIF_UP;
17349 		ASSERT(ill->ill_ipif_up_count > 0);
17350 		--ill->ill_ipif_up_count;
17351 		mutex_exit(&ill->ill_lock);
17352 		ipif_was_up = B_TRUE;
17353 		/* Update status in SCTP's list */
17354 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17355 	}
17356 
17357 	/*
17358 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17359 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17360 	 * know not to rejoin when the interface is brought back up).
17361 	 */
17362 	if (ipif->ipif_isv6)
17363 		ipif_multicast_down(ipif);
17364 	/*
17365 	 * Remove from the mapping for __sin6_src_id. We insert only
17366 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17367 	 * stored as mapped addresses, we need to check for mapped
17368 	 * INADDR_ANY also.
17369 	 */
17370 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17371 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17372 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17373 		int err;
17374 
17375 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17376 		    ipif->ipif_zoneid);
17377 		if (err != 0) {
17378 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17379 		}
17380 	}
17381 
17382 	/*
17383 	 * Before we delete the ill from the group (if any), we need
17384 	 * to make sure that we delete all the routes dependent on
17385 	 * this and also any ipifs dependent on this ipif for
17386 	 * source address. We need to do before we delete from
17387 	 * the group because
17388 	 *
17389 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17390 	 *
17391 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17392 	 *    for re-doing source address selection. Note that
17393 	 *    ipif_select_source[_v6] called from
17394 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17395 	 *    because we have already marked down here i.e cleared
17396 	 *    IPIF_UP.
17397 	 */
17398 	if (ipif->ipif_isv6)
17399 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17400 	else
17401 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17402 
17403 	/*
17404 	 * Need to add these also to be saved and restored when the
17405 	 * ipif is brought down and up
17406 	 */
17407 	mutex_enter(&ire_mrtun_lock);
17408 	if (ire_mrtun_count != 0) {
17409 		mutex_exit(&ire_mrtun_lock);
17410 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17411 		    (char *)ipif, NULL);
17412 	} else {
17413 		mutex_exit(&ire_mrtun_lock);
17414 	}
17415 
17416 	mutex_enter(&ire_srcif_table_lock);
17417 	if (ire_srcif_table_count > 0) {
17418 		mutex_exit(&ire_srcif_table_lock);
17419 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17420 	} else {
17421 		mutex_exit(&ire_srcif_table_lock);
17422 	}
17423 
17424 	/*
17425 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17426 	 * ires have been deleted above. Otherwise a thread could end up
17427 	 * caching an ire in a conn after we have finished the cleanup of the
17428 	 * conn. The caching is done after making sure that the ire is not yet
17429 	 * condemned. Also documented in the block comment above ip_output
17430 	 */
17431 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17432 	/* Also, delete the ires cached in SCTP */
17433 	sctp_ire_cache_flush(ipif);
17434 
17435 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17436 	nattymod_clean_ipif(ipif);
17437 
17438 	/*
17439 	 * Update any other ipifs which have used "our" local address as
17440 	 * a source address. This entails removing and recreating IRE_INTERFACE
17441 	 * entries for such ipifs.
17442 	 */
17443 	if (ipif->ipif_isv6)
17444 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17445 	else
17446 		ipif_update_other_ipifs(ipif, ill->ill_group);
17447 
17448 	if (ipif_was_up) {
17449 		/*
17450 		 * Check whether it is last ipif to leave this group.
17451 		 * If this is the last ipif to leave, we should remove
17452 		 * this ill from the group as ipif_select_source will not
17453 		 * be able to find any useful ipifs if this ill is selected
17454 		 * for load balancing.
17455 		 *
17456 		 * For nameless groups, we should call ifgrp_delete if this
17457 		 * belongs to some group. As this ipif is going down, we may
17458 		 * need to reconstruct groups.
17459 		 */
17460 		phyi = ill->ill_phyint;
17461 		/*
17462 		 * If the phyint_groupname_len is 0, it may or may not
17463 		 * be in the nameless group. If the phyint_groupname_len is
17464 		 * not 0, then this ill should be part of some group.
17465 		 * As we always insert this ill in the group if
17466 		 * phyint_groupname_len is not zero when the first ipif
17467 		 * comes up (in ipif_up_done), it should be in a group
17468 		 * when the namelen is not 0.
17469 		 *
17470 		 * NOTE : When we delete the ill from the group,it will
17471 		 * blow away all the IRE_CACHES pointing either at this ipif or
17472 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17473 		 * should be pointing at this ill.
17474 		 */
17475 		ASSERT(phyi->phyint_groupname_len == 0 ||
17476 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17477 
17478 		if (phyi->phyint_groupname_len != 0) {
17479 			if (ill->ill_ipif_up_count == 0)
17480 				illgrp_delete(ill);
17481 		}
17482 
17483 		/*
17484 		 * If we have deleted some of the broadcast ires associated
17485 		 * with this ipif, we need to re-nominate somebody else if
17486 		 * the ires that we deleted were the nominated ones.
17487 		 */
17488 		if (ill->ill_group != NULL && !ill->ill_isv6)
17489 			ipif_renominate_bcast(ipif);
17490 	}
17491 
17492 	if (ipif->ipif_isv6)
17493 		ipif_ndp_down(ipif);
17494 
17495 	/*
17496 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17497 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17498 	 * and ill_delete -> ipif_free -> ipif_down
17499 	 */
17500 	if (mp == NULL) {
17501 		ASSERT(q == NULL);
17502 		return (0);
17503 	}
17504 
17505 	if (CONN_Q(q)) {
17506 		connp = Q_TO_CONN(q);
17507 		mutex_enter(&connp->conn_lock);
17508 	} else {
17509 		connp = NULL;
17510 	}
17511 	mutex_enter(&ill->ill_lock);
17512 	/*
17513 	 * Are there any ire's pointing to this ipif that are still active ?
17514 	 * If this is the last ipif going down, are there any ire's pointing
17515 	 * to this ill that are still active ?
17516 	 */
17517 	if (ipif_is_quiescent(ipif)) {
17518 		mutex_exit(&ill->ill_lock);
17519 		if (connp != NULL)
17520 			mutex_exit(&connp->conn_lock);
17521 		return (0);
17522 	}
17523 
17524 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17525 	    ill->ill_name, (void *)ill));
17526 	/*
17527 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17528 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17529 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17530 	 */
17531 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17532 	if (!success) {
17533 		/* The conn is closing. So just return */
17534 		ASSERT(connp != NULL);
17535 		mutex_exit(&ill->ill_lock);
17536 		mutex_exit(&connp->conn_lock);
17537 		return (EINTR);
17538 	}
17539 
17540 	mutex_exit(&ill->ill_lock);
17541 	if (connp != NULL)
17542 		mutex_exit(&connp->conn_lock);
17543 	return (EINPROGRESS);
17544 }
17545 
17546 static void
17547 ipif_down_tail(ipif_t *ipif)
17548 {
17549 	ill_t	*ill = ipif->ipif_ill;
17550 
17551 	/*
17552 	 * Skip any loopback interface (null wq).
17553 	 * If this is the last logical interface on the ill
17554 	 * have ill_dl_down tell the driver we are gone (unbind)
17555 	 * Note that lun 0 can ipif_down even though
17556 	 * there are other logical units that are up.
17557 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17558 	 */
17559 	if (ipif->ipif_ill->ill_wq != NULL) {
17560 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17561 		    ill->ill_dl_up) {
17562 			ill_dl_down(ill);
17563 		}
17564 	}
17565 	ill->ill_logical_down = 0;
17566 
17567 	/*
17568 	 * Have to be after removing the routes in ipif_down_delete_ire.
17569 	 */
17570 	if (ipif->ipif_isv6) {
17571 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17572 			ipif_arp_down(ipif);
17573 	} else {
17574 		ipif_arp_down(ipif);
17575 	}
17576 
17577 	ip_rts_ifmsg(ipif);
17578 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17579 }
17580 
17581 /*
17582  * Bring interface logically down without bringing the physical interface
17583  * down e.g. when the netmask is changed. This avoids long lasting link
17584  * negotiations between an ethernet interface and a certain switches.
17585  */
17586 static int
17587 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17588 {
17589 	/*
17590 	 * The ill_logical_down flag is a transient flag. It is set here
17591 	 * and is cleared once the down has completed in ipif_down_tail.
17592 	 * This flag does not indicate whether the ill stream is in the
17593 	 * DL_BOUND state with the driver. Instead this flag is used by
17594 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17595 	 * the driver. The state of the ill stream i.e. whether it is
17596 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17597 	 */
17598 	ipif->ipif_ill->ill_logical_down = 1;
17599 	return (ipif_down(ipif, q, mp));
17600 }
17601 
17602 /*
17603  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17604  * If the usesrc client ILL is already part of a usesrc group or not,
17605  * in either case a ire_stq with the matching usesrc client ILL will
17606  * locate the IRE's that need to be deleted. We want IREs to be created
17607  * with the new source address.
17608  */
17609 static void
17610 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17611 {
17612 	ill_t	*ucill = (ill_t *)ill_arg;
17613 
17614 	ASSERT(IAM_WRITER_ILL(ucill));
17615 
17616 	if (ire->ire_stq == NULL)
17617 		return;
17618 
17619 	if ((ire->ire_type == IRE_CACHE) &&
17620 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17621 		ire_delete(ire);
17622 }
17623 
17624 /*
17625  * ire_walk routine to delete every IRE dependent on the interface
17626  * address that is going down.	(Always called as writer.)
17627  * Works for both v4 and v6.
17628  * In addition for checking for ire_ipif matches it also checks for
17629  * IRE_CACHE entries which have the same source address as the
17630  * disappearing ipif since ipif_select_source might have picked
17631  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17632  * care of any IRE_INTERFACE with the disappearing source address.
17633  */
17634 static void
17635 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17636 {
17637 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17638 	ill_t *ire_ill;
17639 	ill_t *ipif_ill;
17640 
17641 	ASSERT(IAM_WRITER_IPIF(ipif));
17642 	if (ire->ire_ipif == NULL)
17643 		return;
17644 
17645 	/*
17646 	 * For IPv4, we derive source addresses for an IRE from ipif's
17647 	 * belonging to the same IPMP group as the IRE's outgoing
17648 	 * interface.  If an IRE's outgoing interface isn't in the
17649 	 * same IPMP group as a particular ipif, then that ipif
17650 	 * couldn't have been used as a source address for this IRE.
17651 	 *
17652 	 * For IPv6, source addresses are only restricted to the IPMP group
17653 	 * if the IRE is for a link-local address or a multicast address.
17654 	 * Otherwise, source addresses for an IRE can be chosen from
17655 	 * interfaces other than the the outgoing interface for that IRE.
17656 	 *
17657 	 * For source address selection details, see ipif_select_source()
17658 	 * and ipif_select_source_v6().
17659 	 */
17660 	if (ire->ire_ipversion == IPV4_VERSION ||
17661 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17662 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17663 		ire_ill = ire->ire_ipif->ipif_ill;
17664 		ipif_ill = ipif->ipif_ill;
17665 
17666 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17667 			return;
17668 		}
17669 	}
17670 
17671 
17672 	if (ire->ire_ipif != ipif) {
17673 		/*
17674 		 * Look for a matching source address.
17675 		 */
17676 		if (ire->ire_type != IRE_CACHE)
17677 			return;
17678 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17679 			return;
17680 
17681 		if (ire->ire_ipversion == IPV4_VERSION) {
17682 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17683 				return;
17684 		} else {
17685 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17686 			    &ipif->ipif_v6lcl_addr))
17687 				return;
17688 		}
17689 		ire_delete(ire);
17690 		return;
17691 	}
17692 	/*
17693 	 * ire_delete() will do an ire_flush_cache which will delete
17694 	 * all ire_ipif matches
17695 	 */
17696 	ire_delete(ire);
17697 }
17698 
17699 /*
17700  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17701  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17702  * 2) when an interface is brought up or down (on that ill).
17703  * This ensures that the IRE_CACHE entries don't retain stale source
17704  * address selection results.
17705  */
17706 void
17707 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17708 {
17709 	ill_t	*ill = (ill_t *)ill_arg;
17710 	ill_t	*ipif_ill;
17711 
17712 	ASSERT(IAM_WRITER_ILL(ill));
17713 	/*
17714 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17715 	 * Hence this should be IRE_CACHE.
17716 	 */
17717 	ASSERT(ire->ire_type == IRE_CACHE);
17718 
17719 	/*
17720 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17721 	 * We are only interested in IRE_CACHES that has borrowed
17722 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17723 	 * for which we need to look at ire_ipif->ipif_ill match
17724 	 * with ill.
17725 	 */
17726 	ASSERT(ire->ire_ipif != NULL);
17727 	ipif_ill = ire->ire_ipif->ipif_ill;
17728 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17729 	    ipif_ill->ill_group == ill->ill_group)) {
17730 		ire_delete(ire);
17731 	}
17732 }
17733 
17734 /*
17735  * Delete all the ire whose stq references ill_arg.
17736  */
17737 static void
17738 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17739 {
17740 	ill_t	*ill = (ill_t *)ill_arg;
17741 	ill_t	*ire_ill;
17742 
17743 	ASSERT(IAM_WRITER_ILL(ill));
17744 	/*
17745 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17746 	 * Hence this should be IRE_CACHE.
17747 	 */
17748 	ASSERT(ire->ire_type == IRE_CACHE);
17749 
17750 	/*
17751 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17752 	 * matches ill. We are only interested in IRE_CACHES that
17753 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17754 	 * filtering here.
17755 	 */
17756 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17757 
17758 	if (ire_ill == ill)
17759 		ire_delete(ire);
17760 }
17761 
17762 /*
17763  * This is called when an ill leaves the group. We want to delete
17764  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17765  * pointing at ill.
17766  */
17767 static void
17768 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17769 {
17770 	ill_t	*ill = (ill_t *)ill_arg;
17771 
17772 	ASSERT(IAM_WRITER_ILL(ill));
17773 	ASSERT(ill->ill_group == NULL);
17774 	/*
17775 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17776 	 * Hence this should be IRE_CACHE.
17777 	 */
17778 	ASSERT(ire->ire_type == IRE_CACHE);
17779 	/*
17780 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17781 	 * matches ill. We are interested in both.
17782 	 */
17783 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
17784 	    (ire->ire_ipif->ipif_ill == ill));
17785 
17786 	ire_delete(ire);
17787 }
17788 
17789 /*
17790  * Initiate deallocate of an IPIF. Always called as writer. Called by
17791  * ill_delete or ip_sioctl_removeif.
17792  */
17793 static void
17794 ipif_free(ipif_t *ipif)
17795 {
17796 	ASSERT(IAM_WRITER_IPIF(ipif));
17797 
17798 	/* Remove conn references */
17799 	reset_conn_ipif(ipif);
17800 
17801 	/*
17802 	 * Make sure we have valid net and subnet broadcast ire's for the
17803 	 * other ipif's which share them with this ipif.
17804 	 */
17805 	if (!ipif->ipif_isv6)
17806 		ipif_check_bcast_ires(ipif);
17807 
17808 	/*
17809 	 * Take down the interface. We can be called either from ill_delete
17810 	 * or from ip_sioctl_removeif.
17811 	 */
17812 	(void) ipif_down(ipif, NULL, NULL);
17813 
17814 	rw_enter(&ill_g_lock, RW_WRITER);
17815 	/* Remove pointers to this ill in the multicast routing tables */
17816 	reset_mrt_vif_ipif(ipif);
17817 	rw_exit(&ill_g_lock);
17818 }
17819 
17820 static void
17821 ipif_free_tail(ipif_t *ipif)
17822 {
17823 	mblk_t	*mp;
17824 	ipif_t	**ipifp;
17825 
17826 	/*
17827 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
17828 	 */
17829 	mutex_enter(&ipif->ipif_saved_ire_lock);
17830 	mp = ipif->ipif_saved_ire_mp;
17831 	ipif->ipif_saved_ire_mp = NULL;
17832 	mutex_exit(&ipif->ipif_saved_ire_lock);
17833 	freemsg(mp);
17834 
17835 	/*
17836 	 * Need to hold both ill_g_lock and ill_lock while
17837 	 * inserting or removing an ipif from the linked list
17838 	 * of ipifs hanging off the ill.
17839 	 */
17840 	rw_enter(&ill_g_lock, RW_WRITER);
17841 	/*
17842 	 * Remove all multicast memberships on the interface now.
17843 	 * This removes IPv4 multicast memberships joined within
17844 	 * the kernel as ipif_down does not do ipif_multicast_down
17845 	 * for IPv4. IPv6 is not handled here as the multicast memberships
17846 	 * are based on ill and not on ipif.
17847 	 */
17848 	ilm_free(ipif);
17849 
17850 	/*
17851 	 * Since we held the ill_g_lock while doing the ilm_free above,
17852 	 * we can assert the ilms were really deleted and not just marked
17853 	 * ILM_DELETED.
17854 	 */
17855 	ASSERT(ilm_walk_ipif(ipif) == 0);
17856 
17857 
17858 	IPIF_TRACE_CLEANUP(ipif);
17859 
17860 	/* Ask SCTP to take it out of it list */
17861 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
17862 
17863 	mutex_enter(&ipif->ipif_ill->ill_lock);
17864 	/* Get it out of the ILL interface list. */
17865 	ipifp = &ipif->ipif_ill->ill_ipif;
17866 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17867 		if (*ipifp == ipif) {
17868 			*ipifp = ipif->ipif_next;
17869 			break;
17870 		}
17871 	}
17872 
17873 	mutex_exit(&ipif->ipif_ill->ill_lock);
17874 	rw_exit(&ill_g_lock);
17875 
17876 	mutex_destroy(&ipif->ipif_saved_ire_lock);
17877 	/* Free the memory. */
17878 	mi_free((char *)ipif);
17879 }
17880 
17881 /*
17882  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
17883  * "ill_name" otherwise.
17884  */
17885 char *
17886 ipif_get_name(ipif_t *ipif, char *buf, int len)
17887 {
17888 	char	lbuf[32];
17889 	char	*name;
17890 	size_t	name_len;
17891 
17892 	buf[0] = '\0';
17893 	if (!ipif)
17894 		return (buf);
17895 	name = ipif->ipif_ill->ill_name;
17896 	name_len = ipif->ipif_ill->ill_name_length;
17897 	if (ipif->ipif_id != 0) {
17898 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
17899 		    ipif->ipif_id);
17900 		name = lbuf;
17901 		name_len = mi_strlen(name) + 1;
17902 	}
17903 	len -= 1;
17904 	buf[len] = '\0';
17905 	len = MIN(len, name_len);
17906 	bcopy(name, buf, len);
17907 	return (buf);
17908 }
17909 
17910 /*
17911  * Find an IPIF based on the name passed in.  Names can be of the
17912  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
17913  * The <phys> string can have forms like <dev><#> (e.g., le0),
17914  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
17915  * When there is no colon, the implied unit id is zero. <phys> must
17916  * correspond to the name of an ILL.  (May be called as writer.)
17917  */
17918 static ipif_t *
17919 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
17920     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
17921     mblk_t *mp, ipsq_func_t func, int *error)
17922 {
17923 	char	*cp;
17924 	char	*endp;
17925 	long	id;
17926 	ill_t	*ill;
17927 	ipif_t	*ipif;
17928 	uint_t	ire_type;
17929 	boolean_t did_alloc = B_FALSE;
17930 	ipsq_t	*ipsq;
17931 
17932 	if (error != NULL)
17933 		*error = 0;
17934 
17935 	/*
17936 	 * If the caller wants to us to create the ipif, make sure we have a
17937 	 * valid zoneid
17938 	 */
17939 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
17940 
17941 	if (namelen == 0) {
17942 		if (error != NULL)
17943 			*error = ENXIO;
17944 		return (NULL);
17945 	}
17946 
17947 	*exists = B_FALSE;
17948 	/* Look for a colon in the name. */
17949 	endp = &name[namelen];
17950 	for (cp = endp; --cp > name; ) {
17951 		if (*cp == IPIF_SEPARATOR_CHAR)
17952 			break;
17953 	}
17954 
17955 	if (*cp == IPIF_SEPARATOR_CHAR) {
17956 		/*
17957 		 * Reject any non-decimal aliases for logical
17958 		 * interfaces. Aliases with leading zeroes
17959 		 * are also rejected as they introduce ambiguity
17960 		 * in the naming of the interfaces.
17961 		 * In order to confirm with existing semantics,
17962 		 * and to not break any programs/script relying
17963 		 * on that behaviour, if<0>:0 is considered to be
17964 		 * a valid interface.
17965 		 *
17966 		 * If alias has two or more digits and the first
17967 		 * is zero, fail.
17968 		 */
17969 		if (&cp[2] < endp && cp[1] == '0')
17970 			return (NULL);
17971 	}
17972 
17973 	if (cp <= name) {
17974 		cp = endp;
17975 	} else {
17976 		*cp = '\0';
17977 	}
17978 
17979 	/*
17980 	 * Look up the ILL, based on the portion of the name
17981 	 * before the slash. ill_lookup_on_name returns a held ill.
17982 	 * Temporary to check whether ill exists already. If so
17983 	 * ill_lookup_on_name will clear it.
17984 	 */
17985 	ill = ill_lookup_on_name(name, do_alloc, isv6,
17986 	    q, mp, func, error, &did_alloc);
17987 	if (cp != endp)
17988 		*cp = IPIF_SEPARATOR_CHAR;
17989 	if (ill == NULL)
17990 		return (NULL);
17991 
17992 	/* Establish the unit number in the name. */
17993 	id = 0;
17994 	if (cp < endp && *endp == '\0') {
17995 		/* If there was a colon, the unit number follows. */
17996 		cp++;
17997 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
17998 			ill_refrele(ill);
17999 			if (error != NULL)
18000 				*error = ENXIO;
18001 			return (NULL);
18002 		}
18003 	}
18004 
18005 	GRAB_CONN_LOCK(q);
18006 	mutex_enter(&ill->ill_lock);
18007 	/* Now see if there is an IPIF with this unit number. */
18008 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
18009 		if (ipif->ipif_id == id) {
18010 			if (zoneid != ALL_ZONES &&
18011 			    zoneid != ipif->ipif_zoneid) {
18012 				mutex_exit(&ill->ill_lock);
18013 				RELEASE_CONN_LOCK(q);
18014 				ill_refrele(ill);
18015 				if (error != NULL)
18016 					*error = ENXIO;
18017 				return (NULL);
18018 			}
18019 			/*
18020 			 * The block comment at the start of ipif_down
18021 			 * explains the use of the macros used below
18022 			 */
18023 			if (IPIF_CAN_LOOKUP(ipif)) {
18024 				ipif_refhold_locked(ipif);
18025 				mutex_exit(&ill->ill_lock);
18026 				if (!did_alloc)
18027 					*exists = B_TRUE;
18028 				/*
18029 				 * Drop locks before calling ill_refrele
18030 				 * since it can potentially call into
18031 				 * ipif_ill_refrele_tail which can end up
18032 				 * in trying to acquire any lock.
18033 				 */
18034 				RELEASE_CONN_LOCK(q);
18035 				ill_refrele(ill);
18036 				return (ipif);
18037 			} else if (IPIF_CAN_WAIT(ipif, q)) {
18038 				ipsq = ill->ill_phyint->phyint_ipsq;
18039 				mutex_enter(&ipsq->ipsq_lock);
18040 				mutex_exit(&ill->ill_lock);
18041 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
18042 				mutex_exit(&ipsq->ipsq_lock);
18043 				RELEASE_CONN_LOCK(q);
18044 				ill_refrele(ill);
18045 				*error = EINPROGRESS;
18046 				return (NULL);
18047 			}
18048 		}
18049 	}
18050 	RELEASE_CONN_LOCK(q);
18051 
18052 	if (!do_alloc) {
18053 		mutex_exit(&ill->ill_lock);
18054 		ill_refrele(ill);
18055 		if (error != NULL)
18056 			*error = ENXIO;
18057 		return (NULL);
18058 	}
18059 
18060 	/*
18061 	 * If none found, atomically allocate and return a new one.
18062 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
18063 	 * to support "receive only" use of lo0:1 etc. as is still done
18064 	 * below as an initial guess.
18065 	 * However, this is now likely to be overriden later in ipif_up_done()
18066 	 * when we know for sure what address has been configured on the
18067 	 * interface, since we might have more than one loopback interface
18068 	 * with a loopback address, e.g. in the case of zones, and all the
18069 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
18070 	 */
18071 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
18072 		ire_type = IRE_LOOPBACK;
18073 	else
18074 		ire_type = IRE_LOCAL;
18075 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
18076 	if (ipif != NULL)
18077 		ipif_refhold_locked(ipif);
18078 	else if (error != NULL)
18079 		*error = ENOMEM;
18080 	mutex_exit(&ill->ill_lock);
18081 	ill_refrele(ill);
18082 	return (ipif);
18083 }
18084 
18085 /*
18086  * This routine is called whenever a new address comes up on an ipif.  If
18087  * we are configured to respond to address mask requests, then we are supposed
18088  * to broadcast an address mask reply at this time.  This routine is also
18089  * called if we are already up, but a netmask change is made.  This is legal
18090  * but might not make the system manager very popular.	(May be called
18091  * as writer.)
18092  */
18093 static void
18094 ipif_mask_reply(ipif_t *ipif)
18095 {
18096 	icmph_t	*icmph;
18097 	ipha_t	*ipha;
18098 	mblk_t	*mp;
18099 
18100 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18101 
18102 	if (!ip_respond_to_address_mask_broadcast)
18103 		return;
18104 
18105 	/* ICMP mask reply is IPv4 only */
18106 	ASSERT(!ipif->ipif_isv6);
18107 	/* ICMP mask reply is not for a loopback interface */
18108 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18109 
18110 	mp = allocb(REPLY_LEN, BPRI_HI);
18111 	if (mp == NULL)
18112 		return;
18113 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18114 
18115 	ipha = (ipha_t *)mp->b_rptr;
18116 	bzero(ipha, REPLY_LEN);
18117 	*ipha = icmp_ipha;
18118 	ipha->ipha_ttl = ip_broadcast_ttl;
18119 	ipha->ipha_src = ipif->ipif_src_addr;
18120 	ipha->ipha_dst = ipif->ipif_brd_addr;
18121 	ipha->ipha_length = htons(REPLY_LEN);
18122 	ipha->ipha_ident = 0;
18123 
18124 	icmph = (icmph_t *)&ipha[1];
18125 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18126 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18127 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18128 	if (icmph->icmph_checksum == 0)
18129 		icmph->icmph_checksum = 0xffff;
18130 
18131 	put(ipif->ipif_wq, mp);
18132 
18133 #undef	REPLY_LEN
18134 }
18135 
18136 /*
18137  * When the mtu in the ipif changes, we call this routine through ire_walk
18138  * to update all the relevant IREs.
18139  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18140  */
18141 static void
18142 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18143 {
18144 	ipif_t *ipif = (ipif_t *)ipif_arg;
18145 
18146 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18147 		return;
18148 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18149 }
18150 
18151 /*
18152  * When the mtu in the ill changes, we call this routine through ire_walk
18153  * to update all the relevant IREs.
18154  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18155  */
18156 void
18157 ill_mtu_change(ire_t *ire, char *ill_arg)
18158 {
18159 	ill_t	*ill = (ill_t *)ill_arg;
18160 
18161 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18162 		return;
18163 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18164 }
18165 
18166 /*
18167  * Join the ipif specific multicast groups.
18168  * Must be called after a mapping has been set up in the resolver.  (Always
18169  * called as writer.)
18170  */
18171 void
18172 ipif_multicast_up(ipif_t *ipif)
18173 {
18174 	int err, index;
18175 	ill_t *ill;
18176 
18177 	ASSERT(IAM_WRITER_IPIF(ipif));
18178 
18179 	ill = ipif->ipif_ill;
18180 	index = ill->ill_phyint->phyint_ifindex;
18181 
18182 	ip1dbg(("ipif_multicast_up\n"));
18183 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18184 		return;
18185 
18186 	if (ipif->ipif_isv6) {
18187 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18188 			return;
18189 
18190 		/* Join the all hosts multicast address */
18191 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18192 		/*
18193 		 * Passing B_TRUE means we have to join the multicast
18194 		 * membership on this interface even though this is
18195 		 * FAILED. If we join on a different one in the group,
18196 		 * we will not be able to delete the membership later
18197 		 * as we currently don't track where we join when we
18198 		 * join within the kernel unlike applications where
18199 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18200 		 * for more on this.
18201 		 */
18202 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18203 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18204 		if (err != 0) {
18205 			ip0dbg(("ipif_multicast_up: "
18206 			    "all_hosts_mcast failed %d\n",
18207 			    err));
18208 			return;
18209 		}
18210 		/*
18211 		 * Enable multicast for the solicited node multicast address
18212 		 */
18213 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18214 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18215 
18216 			ipv6_multi.s6_addr32[3] |=
18217 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18218 
18219 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18220 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18221 			    NULL);
18222 			if (err != 0) {
18223 				ip0dbg(("ipif_multicast_up: solicited MC"
18224 				    " failed %d\n", err));
18225 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18226 				    ill, ill->ill_phyint->phyint_ifindex,
18227 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18228 				return;
18229 			}
18230 		}
18231 	} else {
18232 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18233 			return;
18234 
18235 		/* Join the all hosts multicast address */
18236 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18237 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18238 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18239 		if (err) {
18240 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18241 			return;
18242 		}
18243 	}
18244 	ipif->ipif_multicast_up = 1;
18245 }
18246 
18247 /*
18248  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18249  * any explicit memberships are blown away in ill_leave_multicast() when the
18250  * ill is brought down.
18251  */
18252 static void
18253 ipif_multicast_down(ipif_t *ipif)
18254 {
18255 	int err;
18256 
18257 	ASSERT(IAM_WRITER_IPIF(ipif));
18258 
18259 	ip1dbg(("ipif_multicast_down\n"));
18260 	if (!ipif->ipif_multicast_up)
18261 		return;
18262 
18263 	ASSERT(ipif->ipif_isv6);
18264 
18265 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18266 
18267 	/*
18268 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18269 	 * we should look for ilms on this ill rather than the ones that have
18270 	 * been failed over here.  They are here temporarily. As
18271 	 * ipif_multicast_up has joined on this ill, we should delete only
18272 	 * from this ill.
18273 	 */
18274 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18275 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18276 	    B_TRUE, B_TRUE);
18277 	if (err != 0) {
18278 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18279 		    err));
18280 	}
18281 	/*
18282 	 * Disable multicast for the solicited node multicast address
18283 	 */
18284 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18285 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18286 
18287 		ipv6_multi.s6_addr32[3] |=
18288 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18289 
18290 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18291 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18292 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18293 
18294 		if (err != 0) {
18295 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18296 			    err));
18297 		}
18298 	}
18299 
18300 	ipif->ipif_multicast_up = 0;
18301 }
18302 
18303 /*
18304  * Used when an interface comes up to recreate any extra routes on this
18305  * interface.
18306  */
18307 static ire_t **
18308 ipif_recover_ire(ipif_t *ipif)
18309 {
18310 	mblk_t	*mp;
18311 	ire_t	**ipif_saved_irep;
18312 	ire_t	**irep;
18313 
18314 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18315 	    ipif->ipif_id));
18316 
18317 	mutex_enter(&ipif->ipif_saved_ire_lock);
18318 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18319 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18320 	if (ipif_saved_irep == NULL) {
18321 		mutex_exit(&ipif->ipif_saved_ire_lock);
18322 		return (NULL);
18323 	}
18324 
18325 	irep = ipif_saved_irep;
18326 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18327 		ire_t		*ire;
18328 		queue_t		*rfq;
18329 		queue_t		*stq;
18330 		ifrt_t		*ifrt;
18331 		uchar_t		*src_addr;
18332 		uchar_t		*gateway_addr;
18333 		mblk_t		*resolver_mp;
18334 		ushort_t	type;
18335 
18336 		/*
18337 		 * When the ire was initially created and then added in
18338 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18339 		 * in the case of a traditional interface route, or as one of
18340 		 * the IRE_OFFSUBNET types (with the exception of
18341 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18342 		 * which we don't need to save or recover).  In the case where
18343 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18344 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18345 		 * to satisfy software like GateD and Sun Cluster which creates
18346 		 * routes using the the loopback interface's address as a
18347 		 * gateway.
18348 		 *
18349 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18350 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18351 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18352 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18353 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18354 		 * the route looks like a traditional interface route (where
18355 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18356 		 * the saved ifrt->ifrt_type.  This means that in the case where
18357 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18358 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18359 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18360 		 */
18361 		ifrt = (ifrt_t *)mp->b_rptr;
18362 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18363 			rfq = NULL;
18364 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18365 			    ? ipif->ipif_rq : ipif->ipif_wq;
18366 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18367 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18368 			    : (uint8_t *)&ipif->ipif_src_addr;
18369 			gateway_addr = NULL;
18370 			resolver_mp = ipif->ipif_resolver_mp;
18371 			type = ipif->ipif_net_type;
18372 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18373 			/* Recover multiroute broadcast IRE. */
18374 			rfq = ipif->ipif_rq;
18375 			stq = ipif->ipif_wq;
18376 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18377 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18378 			    : (uint8_t *)&ipif->ipif_src_addr;
18379 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18380 			resolver_mp = ipif->ipif_bcast_mp;
18381 			type = ifrt->ifrt_type;
18382 		} else {
18383 			rfq = NULL;
18384 			stq = NULL;
18385 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18386 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18387 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18388 			resolver_mp = NULL;
18389 			type = ifrt->ifrt_type;
18390 		}
18391 
18392 		/*
18393 		 * Create a copy of the IRE with the saved address and netmask.
18394 		 */
18395 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18396 		    "0x%x/0x%x\n",
18397 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18398 		    ntohl(ifrt->ifrt_addr),
18399 		    ntohl(ifrt->ifrt_mask)));
18400 		ire = ire_create(
18401 		    (uint8_t *)&ifrt->ifrt_addr,
18402 		    (uint8_t *)&ifrt->ifrt_mask,
18403 		    src_addr,
18404 		    gateway_addr,
18405 		    NULL,
18406 		    &ifrt->ifrt_max_frag,
18407 		    NULL,
18408 		    rfq,
18409 		    stq,
18410 		    type,
18411 		    resolver_mp,
18412 		    ipif,
18413 		    NULL,
18414 		    0,
18415 		    0,
18416 		    0,
18417 		    ifrt->ifrt_flags,
18418 		    &ifrt->ifrt_iulp_info);
18419 
18420 		if (ire == NULL) {
18421 			mutex_exit(&ipif->ipif_saved_ire_lock);
18422 			kmem_free(ipif_saved_irep,
18423 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18424 			return (NULL);
18425 		}
18426 
18427 		/*
18428 		 * Some software (for example, GateD and Sun Cluster) attempts
18429 		 * to create (what amount to) IRE_PREFIX routes with the
18430 		 * loopback address as the gateway.  This is primarily done to
18431 		 * set up prefixes with the RTF_REJECT flag set (for example,
18432 		 * when generating aggregate routes.)
18433 		 *
18434 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18435 		 * IRE_LOOPBACK, then we map the request into a
18436 		 * IRE_IF_NORESOLVER.
18437 		 */
18438 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18439 			ire->ire_type = IRE_IF_NORESOLVER;
18440 		/*
18441 		 * ire held by ire_add, will be refreled' towards the
18442 		 * the end of ipif_up_done
18443 		 */
18444 		(void) ire_add(&ire, NULL, NULL, NULL);
18445 		*irep = ire;
18446 		irep++;
18447 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18448 	}
18449 	mutex_exit(&ipif->ipif_saved_ire_lock);
18450 	return (ipif_saved_irep);
18451 }
18452 
18453 /*
18454  * Used to set the netmask and broadcast address to default values when the
18455  * interface is brought up.  (Always called as writer.)
18456  */
18457 static void
18458 ipif_set_default(ipif_t *ipif)
18459 {
18460 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18461 
18462 	if (!ipif->ipif_isv6) {
18463 		/*
18464 		 * Interface holds an IPv4 address. Default
18465 		 * mask is the natural netmask.
18466 		 */
18467 		if (!ipif->ipif_net_mask) {
18468 			ipaddr_t	v4mask;
18469 
18470 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18471 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18472 		}
18473 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18474 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18475 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18476 		} else {
18477 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18478 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18479 		}
18480 		/*
18481 		 * NOTE: SunOS 4.X does this even if the broadcast address
18482 		 * has been already set thus we do the same here.
18483 		 */
18484 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18485 			ipaddr_t	v4addr;
18486 
18487 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18488 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18489 		}
18490 	} else {
18491 		/*
18492 		 * Interface holds an IPv6-only address.  Default
18493 		 * mask is all-ones.
18494 		 */
18495 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18496 			ipif->ipif_v6net_mask = ipv6_all_ones;
18497 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18498 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18499 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18500 		} else {
18501 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18502 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18503 		}
18504 	}
18505 }
18506 
18507 /*
18508  * Return 0 if this address can be used as local address without causing
18509  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18510  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18511  * Special checks are needed to allow the same IPv6 link-local address
18512  * on different ills.
18513  * TODO: allowing the same site-local address on different ill's.
18514  */
18515 int
18516 ip_addr_availability_check(ipif_t *new_ipif)
18517 {
18518 	in6_addr_t our_v6addr;
18519 	ill_t *ill;
18520 	ipif_t *ipif;
18521 	ill_walk_context_t ctx;
18522 
18523 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18524 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18525 	ASSERT(RW_READ_HELD(&ill_g_lock));
18526 
18527 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18528 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18529 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18530 		return (0);
18531 
18532 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18533 
18534 	if (new_ipif->ipif_isv6)
18535 		ill = ILL_START_WALK_V6(&ctx);
18536 	else
18537 		ill = ILL_START_WALK_V4(&ctx);
18538 
18539 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18540 		for (ipif = ill->ill_ipif; ipif != NULL;
18541 		    ipif = ipif->ipif_next) {
18542 			if ((ipif == new_ipif) ||
18543 			    !(ipif->ipif_flags & IPIF_UP) ||
18544 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18545 				continue;
18546 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18547 			    &our_v6addr)) {
18548 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18549 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18550 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18551 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18552 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18553 				    new_ipif->ipif_ill != ill)
18554 					continue;
18555 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18556 				    new_ipif->ipif_ill != ill)
18557 					continue;
18558 				else if (new_ipif->ipif_zoneid !=
18559 				    ipif->ipif_zoneid &&
18560 				    (ill->ill_phyint->phyint_flags &
18561 				    PHYI_LOOPBACK))
18562 					continue;
18563 				else if (new_ipif->ipif_ill == ill)
18564 					return (EADDRINUSE);
18565 				else
18566 					return (EADDRNOTAVAIL);
18567 			}
18568 		}
18569 	}
18570 
18571 	return (0);
18572 }
18573 
18574 /*
18575  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18576  * IREs for the ipif.
18577  * When the routine returns EINPROGRESS then mp has been consumed and
18578  * the ioctl will be acked from ip_rput_dlpi.
18579  */
18580 static int
18581 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18582 {
18583 	ill_t	*ill = ipif->ipif_ill;
18584 	boolean_t isv6 = ipif->ipif_isv6;
18585 	int	err = 0;
18586 	boolean_t success;
18587 
18588 	ASSERT(IAM_WRITER_IPIF(ipif));
18589 
18590 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18591 
18592 	/* Shouldn't get here if it is already up. */
18593 	if (ipif->ipif_flags & IPIF_UP)
18594 		return (EALREADY);
18595 
18596 	/* Skip arp/ndp for any loopback interface. */
18597 	if (ill->ill_wq != NULL) {
18598 		conn_t *connp = Q_TO_CONN(q);
18599 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18600 
18601 		if (!ill->ill_dl_up) {
18602 			/*
18603 			 * ill_dl_up is not yet set. i.e. we are yet to
18604 			 * DL_BIND with the driver and this is the first
18605 			 * logical interface on the ill to become "up".
18606 			 * Tell the driver to get going (via DL_BIND_REQ).
18607 			 * Note that changing "significant" IFF_ flags
18608 			 * address/netmask etc cause a down/up dance, but
18609 			 * does not cause an unbind (DL_UNBIND) with the driver
18610 			 */
18611 			return (ill_dl_up(ill, ipif, mp, q));
18612 		}
18613 
18614 		/*
18615 		 * ipif_resolver_up may end up sending an
18616 		 * AR_INTERFACE_UP message to ARP, which would, in
18617 		 * turn send a DLPI message to the driver. ioctls are
18618 		 * serialized and so we cannot send more than one
18619 		 * interface up message at a time. If ipif_resolver_up
18620 		 * does send an interface up message to ARP, we get
18621 		 * EINPROGRESS and we will complete in ip_arp_done.
18622 		 */
18623 
18624 		ASSERT(connp != NULL);
18625 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18626 		mutex_enter(&connp->conn_lock);
18627 		mutex_enter(&ill->ill_lock);
18628 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18629 		mutex_exit(&ill->ill_lock);
18630 		mutex_exit(&connp->conn_lock);
18631 		if (!success)
18632 			return (EINTR);
18633 
18634 		/*
18635 		 * Crank up IPv6 neighbor discovery
18636 		 * Unlike ARP, this should complete when
18637 		 * ipif_ndp_up returns. However, for
18638 		 * ILLF_XRESOLV interfaces we also send a
18639 		 * AR_INTERFACE_UP to the external resolver.
18640 		 * That ioctl will complete in ip_rput.
18641 		 */
18642 		if (isv6) {
18643 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18644 			    B_FALSE);
18645 			if (err != 0) {
18646 				mp = ipsq_pending_mp_get(ipsq, &connp);
18647 				return (err);
18648 			}
18649 		}
18650 		/* Now, ARP */
18651 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18652 		    EINPROGRESS) {
18653 			/* We will complete it in ip_arp_done */
18654 			return (err);
18655 		}
18656 		mp = ipsq_pending_mp_get(ipsq, &connp);
18657 		ASSERT(mp != NULL);
18658 		if (err != 0)
18659 			return (err);
18660 	}
18661 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18662 }
18663 
18664 /*
18665  * Perform a bind for the physical device.
18666  * When the routine returns EINPROGRESS then mp has been consumed and
18667  * the ioctl will be acked from ip_rput_dlpi.
18668  * Allocate an unbind message and save it until ipif_down.
18669  */
18670 static int
18671 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18672 {
18673 	mblk_t	*areq_mp = NULL;
18674 	mblk_t	*bind_mp = NULL;
18675 	mblk_t	*unbind_mp = NULL;
18676 	conn_t	*connp;
18677 	boolean_t success;
18678 
18679 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18680 	ASSERT(IAM_WRITER_ILL(ill));
18681 
18682 	ASSERT(mp != NULL);
18683 
18684 	/* Create a resolver cookie for ARP */
18685 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18686 		areq_t		*areq;
18687 		uint16_t	sap_addr;
18688 
18689 		areq_mp = ill_arp_alloc(ill,
18690 			(uchar_t *)&ip_areq_template, 0);
18691 		if (areq_mp == NULL) {
18692 			return (ENOMEM);
18693 		}
18694 		freemsg(ill->ill_resolver_mp);
18695 		ill->ill_resolver_mp = areq_mp;
18696 		areq = (areq_t *)areq_mp->b_rptr;
18697 		sap_addr = ill->ill_sap;
18698 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18699 		/*
18700 		 * Wait till we call ill_pending_mp_add to determine
18701 		 * the success before we free the ill_resolver_mp and
18702 		 * attach areq_mp in it's place.
18703 		 */
18704 	}
18705 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18706 	    DL_BIND_REQ);
18707 	if (bind_mp == NULL)
18708 		goto bad;
18709 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18710 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18711 
18712 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18713 	if (unbind_mp == NULL)
18714 		goto bad;
18715 
18716 	/*
18717 	 * Record state needed to complete this operation when the
18718 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18719 	 */
18720 	if (WR(q)->q_next == NULL) {
18721 		connp = Q_TO_CONN(q);
18722 		mutex_enter(&connp->conn_lock);
18723 	} else {
18724 		connp = NULL;
18725 	}
18726 	mutex_enter(&ipif->ipif_ill->ill_lock);
18727 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18728 	mutex_exit(&ipif->ipif_ill->ill_lock);
18729 	if (connp != NULL)
18730 		mutex_exit(&connp->conn_lock);
18731 	if (!success)
18732 		goto bad;
18733 
18734 	/*
18735 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18736 	 * the interface goes down.
18737 	 */
18738 	ASSERT(ill->ill_unbind_mp == NULL);
18739 	ill->ill_unbind_mp = unbind_mp;
18740 
18741 	ill_dlpi_send(ill, bind_mp);
18742 	/* Send down link-layer capabilities probe if not already done. */
18743 	ill_capability_probe(ill);
18744 
18745 	/*
18746 	 * Sysid used to rely on the fact that netboots set domainname
18747 	 * and the like. Now that miniroot boots aren't strictly netboots
18748 	 * and miniroot network configuration is driven from userland
18749 	 * these things still need to be set. This situation can be detected
18750 	 * by comparing the interface being configured here to the one
18751 	 * dhcack was set to reference by the boot loader. Once sysid is
18752 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18753 	 */
18754 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18755 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18756 	    (strlen(srpc_domain) == 0)) {
18757 		if (dhcpinit() != 0)
18758 			cmn_err(CE_WARN, "no cached dhcp response");
18759 	}
18760 
18761 	/*
18762 	 * This operation will complete in ip_rput_dlpi with either
18763 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18764 	 */
18765 	return (EINPROGRESS);
18766 bad:
18767 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18768 	/*
18769 	 * We don't have to check for possible removal from illgrp
18770 	 * as we have not yet inserted in illgrp. For groups
18771 	 * without names, this ipif is still not UP and hence
18772 	 * this could not have possibly had any influence in forming
18773 	 * groups.
18774 	 */
18775 
18776 	if (bind_mp != NULL)
18777 		freemsg(bind_mp);
18778 	if (unbind_mp != NULL)
18779 		freemsg(unbind_mp);
18780 	return (ENOMEM);
18781 }
18782 
18783 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
18784 
18785 /*
18786  * DLPI and ARP is up.
18787  * Create all the IREs associated with an interface bring up multicast.
18788  * Set the interface flag and finish other initialization
18789  * that potentially had to be differed to after DL_BIND_ACK.
18790  */
18791 int
18792 ipif_up_done(ipif_t *ipif)
18793 {
18794 	ire_t	*ire_array[20];
18795 	ire_t	**irep = ire_array;
18796 	ire_t	**irep1;
18797 	ipaddr_t net_mask = 0;
18798 	ipaddr_t subnet_mask, route_mask;
18799 	ill_t	*ill = ipif->ipif_ill;
18800 	queue_t	*stq;
18801 	ipif_t	 *src_ipif;
18802 	ipif_t   *tmp_ipif;
18803 	boolean_t	flush_ire_cache = B_TRUE;
18804 	int	err = 0;
18805 	phyint_t *phyi;
18806 	ire_t	**ipif_saved_irep = NULL;
18807 	int ipif_saved_ire_cnt;
18808 	int	cnt;
18809 	boolean_t	src_ipif_held = B_FALSE;
18810 	boolean_t	ire_added = B_FALSE;
18811 	boolean_t	loopback = B_FALSE;
18812 
18813 	ip1dbg(("ipif_up_done(%s:%u)\n",
18814 		ipif->ipif_ill->ill_name, ipif->ipif_id));
18815 	/* Check if this is a loopback interface */
18816 	if (ipif->ipif_ill->ill_wq == NULL)
18817 		loopback = B_TRUE;
18818 
18819 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18820 	/*
18821 	 * If all other interfaces for this ill are down or DEPRECATED,
18822 	 * or otherwise unsuitable for source address selection, remove
18823 	 * any IRE_CACHE entries for this ill to make sure source
18824 	 * address selection gets to take this new ipif into account.
18825 	 * No need to hold ill_lock while traversing the ipif list since
18826 	 * we are writer
18827 	 */
18828 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
18829 		tmp_ipif = tmp_ipif->ipif_next) {
18830 		if (((tmp_ipif->ipif_flags &
18831 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
18832 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
18833 		    (tmp_ipif == ipif))
18834 			continue;
18835 		/* first useable pre-existing interface */
18836 		flush_ire_cache = B_FALSE;
18837 		break;
18838 	}
18839 	if (flush_ire_cache)
18840 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
18841 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
18842 
18843 	/*
18844 	 * Figure out which way the send-to queue should go.  Only
18845 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
18846 	 * should show up here.
18847 	 */
18848 	switch (ill->ill_net_type) {
18849 	case IRE_IF_RESOLVER:
18850 		stq = ill->ill_rq;
18851 		break;
18852 	case IRE_IF_NORESOLVER:
18853 	case IRE_LOOPBACK:
18854 		stq = ill->ill_wq;
18855 		break;
18856 	default:
18857 		return (EINVAL);
18858 	}
18859 
18860 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
18861 		/*
18862 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
18863 		 * ipif_lookup_on_name(), but in the case of zones we can have
18864 		 * several loopback addresses on lo0. So all the interfaces with
18865 		 * loopback addresses need to be marked IRE_LOOPBACK.
18866 		 */
18867 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
18868 		    htonl(INADDR_LOOPBACK))
18869 			ipif->ipif_ire_type = IRE_LOOPBACK;
18870 		else
18871 			ipif->ipif_ire_type = IRE_LOCAL;
18872 	}
18873 
18874 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
18875 		/*
18876 		 * Can't use our source address. Select a different
18877 		 * source address for the IRE_INTERFACE and IRE_LOCAL
18878 		 */
18879 		src_ipif = ipif_select_source(ipif->ipif_ill,
18880 		    ipif->ipif_subnet, ipif->ipif_zoneid);
18881 		if (src_ipif == NULL)
18882 			src_ipif = ipif;	/* Last resort */
18883 		else
18884 			src_ipif_held = B_TRUE;
18885 	} else {
18886 		src_ipif = ipif;
18887 	}
18888 
18889 	/* Create all the IREs associated with this interface */
18890 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18891 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18892 		/* Register the source address for __sin6_src_id */
18893 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
18894 		    ipif->ipif_zoneid);
18895 		if (err != 0) {
18896 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
18897 			return (err);
18898 		}
18899 		/* If the interface address is set, create the local IRE. */
18900 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
18901 			(void *)ipif,
18902 			ipif->ipif_ire_type,
18903 			ntohl(ipif->ipif_lcl_addr)));
18904 		*irep++ = ire_create(
18905 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
18906 		    (uchar_t *)&ip_g_all_ones,		/* mask */
18907 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
18908 		    NULL,				/* no gateway */
18909 		    NULL,
18910 		    &ip_loopback_mtuplus,		/* max frag size */
18911 		    NULL,
18912 		    ipif->ipif_rq,			/* recv-from queue */
18913 		    NULL,				/* no send-to queue */
18914 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
18915 		    NULL,
18916 		    ipif,
18917 		    NULL,
18918 		    0,
18919 		    0,
18920 		    0,
18921 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
18922 		    RTF_PRIVATE : 0,
18923 		    &ire_uinfo_null);
18924 	} else {
18925 		ip1dbg((
18926 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
18927 		    ipif->ipif_ire_type,
18928 		    ntohl(ipif->ipif_lcl_addr),
18929 		    (uint_t)ipif->ipif_flags));
18930 	}
18931 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18932 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18933 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
18934 	} else {
18935 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
18936 	}
18937 
18938 	subnet_mask = ipif->ipif_net_mask;
18939 
18940 	/*
18941 	 * If mask was not specified, use natural netmask of
18942 	 * interface address. Also, store this mask back into the
18943 	 * ipif struct.
18944 	 */
18945 	if (subnet_mask == 0) {
18946 		subnet_mask = net_mask;
18947 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
18948 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
18949 		    ipif->ipif_v6subnet);
18950 	}
18951 
18952 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
18953 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
18954 	    ipif->ipif_subnet != INADDR_ANY) {
18955 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18956 
18957 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18958 			route_mask = IP_HOST_MASK;
18959 		} else {
18960 			route_mask = subnet_mask;
18961 		}
18962 
18963 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
18964 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
18965 			(void *)ipif, (void *)ill,
18966 			ill->ill_net_type,
18967 			ntohl(ipif->ipif_subnet)));
18968 		*irep++ = ire_create(
18969 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
18970 		    (uchar_t *)&route_mask,		/* mask */
18971 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
18972 		    NULL,				/* no gateway */
18973 		    NULL,
18974 		    &ipif->ipif_mtu,			/* max frag */
18975 		    NULL,
18976 		    NULL,				/* no recv queue */
18977 		    stq,				/* send-to queue */
18978 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
18979 		    ill->ill_resolver_mp,		/* xmit header */
18980 		    ipif,
18981 		    NULL,
18982 		    0,
18983 		    0,
18984 		    0,
18985 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
18986 		    &ire_uinfo_null);
18987 	}
18988 
18989 	/*
18990 	 * If the interface address is set, create the broadcast IREs.
18991 	 *
18992 	 * ire_create_bcast checks if the proposed new IRE matches
18993 	 * any existing IRE's with the same physical interface (ILL).
18994 	 * This should get rid of duplicates.
18995 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
18996 	 * any broadcast ires.
18997 	 */
18998 	if ((ipif->ipif_subnet != INADDR_ANY) &&
18999 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
19000 		ipaddr_t addr;
19001 
19002 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
19003 		irep = ire_check_and_create_bcast(ipif, 0, irep,
19004 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19005 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
19006 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19007 
19008 		/*
19009 		 * For backward compatibility, we need to create net
19010 		 * broadcast ire's based on the old "IP address class
19011 		 * system."  The reason is that some old machines only
19012 		 * respond to these class derived net broadcast.
19013 		 *
19014 		 * But we should not create these net broadcast ire's if
19015 		 * the subnet_mask is shorter than the IP address class based
19016 		 * derived netmask.  Otherwise, we may create a net
19017 		 * broadcast address which is the same as an IP address
19018 		 * on the subnet.  Then TCP will refuse to talk to that
19019 		 * address.
19020 		 *
19021 		 * Nor do we need IRE_BROADCAST ire's for the interface
19022 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
19023 		 * interface is already created.  Creating these broadcast
19024 		 * ire's will only create confusion as the "addr" is going
19025 		 * to be same as that of the IP address of the interface.
19026 		 */
19027 		if (net_mask < subnet_mask) {
19028 			addr = net_mask & ipif->ipif_subnet;
19029 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19030 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19031 			irep = ire_check_and_create_bcast(ipif,
19032 			    ~net_mask | addr, irep,
19033 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19034 		}
19035 
19036 		if (subnet_mask != 0xFFFFFFFF) {
19037 			addr = ipif->ipif_subnet;
19038 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19039 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19040 			irep = ire_check_and_create_bcast(ipif,
19041 			    ~subnet_mask|addr, irep,
19042 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19043 		}
19044 	}
19045 
19046 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19047 
19048 	/* If an earlier ire_create failed, get out now */
19049 	for (irep1 = irep; irep1 > ire_array; ) {
19050 		irep1--;
19051 		if (*irep1 == NULL) {
19052 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
19053 			err = ENOMEM;
19054 			goto bad;
19055 		}
19056 	}
19057 
19058 	/*
19059 	 * Need to atomically check for ip_addr_availablity_check
19060 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
19061 	 * from group also.The ill_g_lock is grabbed as reader
19062 	 * just to make sure no new ills or new ipifs are being added
19063 	 * to the system while we are checking the uniqueness of addresses.
19064 	 */
19065 	rw_enter(&ill_g_lock, RW_READER);
19066 	mutex_enter(&ip_addr_avail_lock);
19067 	/* Mark it up, and increment counters. */
19068 	ill->ill_ipif_up_count++;
19069 	ipif->ipif_flags |= IPIF_UP;
19070 	err = ip_addr_availability_check(ipif);
19071 	mutex_exit(&ip_addr_avail_lock);
19072 	rw_exit(&ill_g_lock);
19073 
19074 	if (err != 0) {
19075 		/*
19076 		 * Our address may already be up on the same ill. In this case,
19077 		 * the ARP entry for our ipif replaced the one for the other
19078 		 * ipif. So we don't want to delete it (otherwise the other ipif
19079 		 * would be unable to send packets).
19080 		 * ip_addr_availability_check() identifies this case for us and
19081 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
19082 		 * which is the expected error code.
19083 		 */
19084 		if (err == EADDRINUSE) {
19085 			freemsg(ipif->ipif_arp_del_mp);
19086 			ipif->ipif_arp_del_mp = NULL;
19087 			err = EADDRNOTAVAIL;
19088 		}
19089 		ill->ill_ipif_up_count--;
19090 		ipif->ipif_flags &= ~IPIF_UP;
19091 		goto bad;
19092 	}
19093 
19094 	/*
19095 	 * Add in all newly created IREs.  ire_create_bcast() has
19096 	 * already checked for duplicates of the IRE_BROADCAST type.
19097 	 * We want to add before we call ifgrp_insert which wants
19098 	 * to know whether IRE_IF_RESOLVER exists or not.
19099 	 *
19100 	 * NOTE : We refrele the ire though we may branch to "bad"
19101 	 *	  later on where we do ire_delete. This is okay
19102 	 *	  because nobody can delete it as we are running
19103 	 *	  exclusively.
19104 	 */
19105 	for (irep1 = irep; irep1 > ire_array; ) {
19106 		irep1--;
19107 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19108 		/*
19109 		 * refheld by ire_add. refele towards the end of the func
19110 		 */
19111 		(void) ire_add(irep1, NULL, NULL, NULL);
19112 	}
19113 	ire_added = B_TRUE;
19114 	/*
19115 	 * Form groups if possible.
19116 	 *
19117 	 * If we are supposed to be in a ill_group with a name, insert it
19118 	 * now as we know that at least one ipif is UP. Otherwise form
19119 	 * nameless groups.
19120 	 *
19121 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19122 	 * this ipif into the appropriate interface group, or create a
19123 	 * new one. If this is already in a nameless group, we try to form
19124 	 * a bigger group looking at other ills potentially sharing this
19125 	 * ipif's prefix.
19126 	 */
19127 	phyi = ill->ill_phyint;
19128 	if (phyi->phyint_groupname_len != 0) {
19129 		ASSERT(phyi->phyint_groupname != NULL);
19130 		if (ill->ill_ipif_up_count == 1) {
19131 			ASSERT(ill->ill_group == NULL);
19132 			err = illgrp_insert(&illgrp_head_v4, ill,
19133 			    phyi->phyint_groupname, NULL, B_TRUE);
19134 			if (err != 0) {
19135 				ip1dbg(("ipif_up_done: illgrp allocation "
19136 				    "failed, error %d\n", err));
19137 				goto bad;
19138 			}
19139 		}
19140 		ASSERT(ill->ill_group != NULL);
19141 	}
19142 
19143 	/*
19144 	 * When this is part of group, we need to make sure that
19145 	 * any broadcast ires created because of this ipif coming
19146 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19147 	 * so that we don't receive duplicate broadcast packets.
19148 	 */
19149 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19150 		ipif_renominate_bcast(ipif);
19151 
19152 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19153 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19154 	ipif_saved_irep = ipif_recover_ire(ipif);
19155 
19156 	if (!loopback) {
19157 		/*
19158 		 * If the broadcast address has been set, make sure it makes
19159 		 * sense based on the interface address.
19160 		 * Only match on ill since we are sharing broadcast addresses.
19161 		 */
19162 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19163 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19164 			ire_t	*ire;
19165 
19166 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19167 			    IRE_BROADCAST, ipif, ALL_ZONES,
19168 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19169 
19170 			if (ire == NULL) {
19171 				/*
19172 				 * If there isn't a matching broadcast IRE,
19173 				 * revert to the default for this netmask.
19174 				 */
19175 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19176 				mutex_enter(&ipif->ipif_ill->ill_lock);
19177 				ipif_set_default(ipif);
19178 				mutex_exit(&ipif->ipif_ill->ill_lock);
19179 			} else {
19180 				ire_refrele(ire);
19181 			}
19182 		}
19183 
19184 	}
19185 
19186 
19187 	/* This is the first interface on this ill */
19188 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19189 		/*
19190 		 * Need to recover all multicast memberships in the driver.
19191 		 * This had to be deferred until we had attached.
19192 		 */
19193 		ill_recover_multicast(ill);
19194 	}
19195 	/* Join the allhosts multicast address */
19196 	ipif_multicast_up(ipif);
19197 
19198 	if (!loopback) {
19199 		/*
19200 		 * See whether anybody else would benefit from the
19201 		 * new ipif that we added. We call this always rather
19202 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19203 		 * ipif is for the benefit of illgrp_insert (done above)
19204 		 * which does not do source address selection as it does
19205 		 * not want to re-create interface routes that we are
19206 		 * having reference to it here.
19207 		 */
19208 		ill_update_source_selection(ill);
19209 	}
19210 
19211 	for (irep1 = irep; irep1 > ire_array; ) {
19212 		irep1--;
19213 		if (*irep1 != NULL) {
19214 			/* was held in ire_add */
19215 			ire_refrele(*irep1);
19216 		}
19217 	}
19218 
19219 	cnt = ipif_saved_ire_cnt;
19220 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19221 		if (*irep1 != NULL) {
19222 			/* was held in ire_add */
19223 			ire_refrele(*irep1);
19224 		}
19225 	}
19226 
19227 	/*
19228 	 * This had to be deferred until we had bound.
19229 	 * tell routing sockets that this interface is up
19230 	 */
19231 	ip_rts_ifmsg(ipif);
19232 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19233 
19234 	if (!loopback) {
19235 		/* Broadcast an address mask reply. */
19236 		ipif_mask_reply(ipif);
19237 	}
19238 	if (ipif_saved_irep != NULL) {
19239 		kmem_free(ipif_saved_irep,
19240 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19241 	}
19242 	if (src_ipif_held)
19243 		ipif_refrele(src_ipif);
19244 	/* Let SCTP update the status for this ipif */
19245 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19246 	return (0);
19247 
19248 bad:
19249 	ip1dbg(("ipif_up_done: FAILED \n"));
19250 	/*
19251 	 * We don't have to bother removing from ill groups because
19252 	 *
19253 	 * 1) For groups with names, we insert only when the first ipif
19254 	 *    comes up. In that case if it fails, it will not be in any
19255 	 *    group. So, we need not try to remove for that case.
19256 	 *
19257 	 * 2) For groups without names, either we tried to insert ipif_ill
19258 	 *    in a group as singleton or found some other group to become
19259 	 *    a bigger group. For the former, if it fails we don't have
19260 	 *    anything to do as ipif_ill is not in the group and for the
19261 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19262 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19263 	 */
19264 	while (irep > ire_array) {
19265 		irep--;
19266 		if (*irep != NULL) {
19267 			ire_delete(*irep);
19268 			if (ire_added)
19269 				ire_refrele(*irep);
19270 		}
19271 	}
19272 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19273 
19274 	if (ipif_saved_irep != NULL) {
19275 		kmem_free(ipif_saved_irep,
19276 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19277 	}
19278 	if (src_ipif_held)
19279 		ipif_refrele(src_ipif);
19280 
19281 	ipif_arp_down(ipif);
19282 	return (err);
19283 }
19284 
19285 /*
19286  * Turn off the ARP with the ILLF_NOARP flag.
19287  */
19288 static int
19289 ill_arp_off(ill_t *ill)
19290 {
19291 	mblk_t	*arp_off_mp = NULL;
19292 	mblk_t	*arp_on_mp = NULL;
19293 
19294 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19295 
19296 	ASSERT(IAM_WRITER_ILL(ill));
19297 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19298 
19299 	/*
19300 	 * If the on message is still around we've already done
19301 	 * an arp_off without doing an arp_on thus there is no
19302 	 * work needed.
19303 	 */
19304 	if (ill->ill_arp_on_mp != NULL)
19305 		return (0);
19306 
19307 	/*
19308 	 * Allocate an ARP on message (to be saved) and an ARP off message
19309 	 */
19310 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19311 	if (!arp_off_mp)
19312 		return (ENOMEM);
19313 
19314 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19315 	if (!arp_on_mp)
19316 		goto failed;
19317 
19318 	ASSERT(ill->ill_arp_on_mp == NULL);
19319 	ill->ill_arp_on_mp = arp_on_mp;
19320 
19321 	/* Send an AR_INTERFACE_OFF request */
19322 	putnext(ill->ill_rq, arp_off_mp);
19323 	return (0);
19324 failed:
19325 
19326 	if (arp_off_mp)
19327 		freemsg(arp_off_mp);
19328 	return (ENOMEM);
19329 }
19330 
19331 /*
19332  * Turn on ARP by turning off the ILLF_NOARP flag.
19333  */
19334 static int
19335 ill_arp_on(ill_t *ill)
19336 {
19337 	mblk_t	*mp;
19338 
19339 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19340 
19341 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19342 
19343 	ASSERT(IAM_WRITER_ILL(ill));
19344 	/*
19345 	 * Send an AR_INTERFACE_ON request if we have already done
19346 	 * an arp_off (which allocated the message).
19347 	 */
19348 	if (ill->ill_arp_on_mp != NULL) {
19349 		mp = ill->ill_arp_on_mp;
19350 		ill->ill_arp_on_mp = NULL;
19351 		putnext(ill->ill_rq, mp);
19352 	}
19353 	return (0);
19354 }
19355 
19356 /*
19357  * Called after either deleting ill from the group or when setting
19358  * FAILED or STANDBY on the interface.
19359  */
19360 static void
19361 illgrp_reset_schednext(ill_t *ill)
19362 {
19363 	ill_group_t *illgrp;
19364 	ill_t *save_ill;
19365 
19366 	ASSERT(IAM_WRITER_ILL(ill));
19367 	/*
19368 	 * When called from illgrp_delete, ill_group will be non-NULL.
19369 	 * But when called from ip_sioctl_flags, it could be NULL if
19370 	 * somebody is setting FAILED/INACTIVE on some interface which
19371 	 * is not part of a group.
19372 	 */
19373 	illgrp = ill->ill_group;
19374 	if (illgrp == NULL)
19375 		return;
19376 	if (illgrp->illgrp_ill_schednext != ill)
19377 		return;
19378 
19379 	illgrp->illgrp_ill_schednext = NULL;
19380 	save_ill = ill;
19381 	/*
19382 	 * Choose a good ill to be the next one for
19383 	 * outbound traffic. As the flags FAILED/STANDBY is
19384 	 * not yet marked when called from ip_sioctl_flags,
19385 	 * we check for ill separately.
19386 	 */
19387 	for (ill = illgrp->illgrp_ill; ill != NULL;
19388 	    ill = ill->ill_group_next) {
19389 		if ((ill != save_ill) &&
19390 		    !(ill->ill_phyint->phyint_flags &
19391 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19392 			illgrp->illgrp_ill_schednext = ill;
19393 			return;
19394 		}
19395 	}
19396 }
19397 
19398 /*
19399  * Given an ill, find the next ill in the group to be scheduled.
19400  * (This should be called by ip_newroute() before ire_create().)
19401  * The passed in ill may be pulled out of the group, after we have picked
19402  * up a different outgoing ill from the same group. However ire add will
19403  * atomically check this.
19404  */
19405 ill_t *
19406 illgrp_scheduler(ill_t *ill)
19407 {
19408 	ill_t *retill;
19409 	ill_group_t *illgrp;
19410 	int illcnt;
19411 	int i;
19412 	uint64_t flags;
19413 
19414 	/*
19415 	 * We don't use a lock to check for the ill_group. If this ill
19416 	 * is currently being inserted we may end up just returning this
19417 	 * ill itself. That is ok.
19418 	 */
19419 	if (ill->ill_group == NULL) {
19420 		ill_refhold(ill);
19421 		return (ill);
19422 	}
19423 
19424 	/*
19425 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19426 	 * a set of stable ills. No ill can be added or deleted or change
19427 	 * group while we hold the reader lock.
19428 	 */
19429 	rw_enter(&ill_g_lock, RW_READER);
19430 	if ((illgrp = ill->ill_group) == NULL) {
19431 		rw_exit(&ill_g_lock);
19432 		ill_refhold(ill);
19433 		return (ill);
19434 	}
19435 
19436 	illcnt = illgrp->illgrp_ill_count;
19437 	mutex_enter(&illgrp->illgrp_lock);
19438 	retill = illgrp->illgrp_ill_schednext;
19439 
19440 	if (retill == NULL)
19441 		retill = illgrp->illgrp_ill;
19442 
19443 	/*
19444 	 * We do a circular search beginning at illgrp_ill_schednext
19445 	 * or illgrp_ill. We don't check the flags against the ill lock
19446 	 * since it can change anytime. The ire creation will be atomic
19447 	 * and will fail if the ill is FAILED or OFFLINE.
19448 	 */
19449 	for (i = 0; i < illcnt; i++) {
19450 		flags = retill->ill_phyint->phyint_flags;
19451 
19452 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19453 		    ILL_CAN_LOOKUP(retill)) {
19454 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19455 			ill_refhold(retill);
19456 			break;
19457 		}
19458 		retill = retill->ill_group_next;
19459 		if (retill == NULL)
19460 			retill = illgrp->illgrp_ill;
19461 	}
19462 	mutex_exit(&illgrp->illgrp_lock);
19463 	rw_exit(&ill_g_lock);
19464 
19465 	return (i == illcnt ? NULL : retill);
19466 }
19467 
19468 /*
19469  * Checks for availbility of a usable source address (if there is one) when the
19470  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19471  * this selection is done regardless of the destination.
19472  */
19473 boolean_t
19474 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19475 {
19476 	uint_t	ifindex;
19477 	ipif_t	*ipif = NULL;
19478 	ill_t	*uill;
19479 	boolean_t isv6;
19480 
19481 	ASSERT(ill != NULL);
19482 
19483 	isv6 = ill->ill_isv6;
19484 	ifindex = ill->ill_usesrc_ifindex;
19485 	if (ifindex != 0) {
19486 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19487 		    NULL);
19488 		if (uill == NULL)
19489 			return (NULL);
19490 		mutex_enter(&uill->ill_lock);
19491 		for (ipif = uill->ill_ipif; ipif != NULL;
19492 		    ipif = ipif->ipif_next) {
19493 			if (!IPIF_CAN_LOOKUP(ipif))
19494 				continue;
19495 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19496 				continue;
19497 			if (!(ipif->ipif_flags & IPIF_UP))
19498 				continue;
19499 			if (ipif->ipif_zoneid != zoneid)
19500 				continue;
19501 			if ((isv6 &&
19502 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19503 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19504 				continue;
19505 			mutex_exit(&uill->ill_lock);
19506 			ill_refrele(uill);
19507 			return (B_TRUE);
19508 		}
19509 		mutex_exit(&uill->ill_lock);
19510 		ill_refrele(uill);
19511 	}
19512 	return (B_FALSE);
19513 }
19514 
19515 /*
19516  * Determine the best source address given a destination address and an ill.
19517  * Prefers non-deprecated over deprecated but will return a deprecated
19518  * address if there is no other choice. If there is a usable source address
19519  * on the interface pointed to by ill_usesrc_ifindex then that is given
19520  * first preference.
19521  *
19522  * Returns NULL if there is no suitable source address for the ill.
19523  * This only occurs when there is no valid source address for the ill.
19524  */
19525 ipif_t *
19526 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19527 {
19528 	ipif_t *ipif;
19529 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19530 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19531 	int index = 0;
19532 	boolean_t wrapped = B_FALSE;
19533 	boolean_t same_subnet_only = B_FALSE;
19534 	boolean_t ipif_same_found, ipif_other_found;
19535 	ill_t	*till, *usill = NULL;
19536 
19537 	if (ill->ill_usesrc_ifindex != 0) {
19538 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19539 		    NULL, NULL, NULL, NULL);
19540 		if (usill != NULL)
19541 			ill = usill;	/* Select source from usesrc ILL */
19542 		else
19543 			return (NULL);
19544 	}
19545 
19546 	/*
19547 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19548 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19549 	 * After selecting the right ipif, under ill_lock make sure ipif is
19550 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19551 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19552 	 * but not under a lock.
19553 	 */
19554 	rw_enter(&ill_g_lock, RW_READER);
19555 
19556 retry:
19557 	till = ill;
19558 	ipif_arr[0] = NULL;
19559 
19560 	if (till->ill_group != NULL)
19561 		till = till->ill_group->illgrp_ill;
19562 
19563 	/*
19564 	 * Choose one good source address from each ill across the group.
19565 	 * If possible choose a source address in the same subnet as
19566 	 * the destination address.
19567 	 *
19568 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19569 	 * This is okay because of the following.
19570 	 *
19571 	 *    If PHYI_FAILED is set and we still have non-deprecated
19572 	 *    addresses, it means the addresses have not yet been
19573 	 *    failed over to a different interface. We potentially
19574 	 *    select them to create IRE_CACHES, which will be later
19575 	 *    flushed when the addresses move over.
19576 	 *
19577 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19578 	 *    addresses, it means either the user has configured them
19579 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19580 	 *    been moved over. For the former, in.mpathd does a failover
19581 	 *    when the interface becomes INACTIVE and hence we should
19582 	 *    not find them. Once INACTIVE is set, we don't allow them
19583 	 *    to create logical interfaces anymore. For the latter, a
19584 	 *    flush will happen when INACTIVE is cleared which will
19585 	 *    flush the IRE_CACHES.
19586 	 *
19587 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19588 	 *    over soon. We potentially select them to create IRE_CACHEs,
19589 	 *    which will be later flushed when the addresses move over.
19590 	 *
19591 	 * NOTE : As ipif_select_source is called to borrow source address
19592 	 * for an ipif that is part of a group, source address selection
19593 	 * will be re-done whenever the group changes i.e either an
19594 	 * insertion/deletion in the group.
19595 	 *
19596 	 * Fill ipif_arr[] with source addresses, using these rules:
19597 	 *
19598 	 *	1. At most one source address from a given ill ends up
19599 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19600 	 *	   associated with a given ill ends up in ipif_arr[].
19601 	 *
19602 	 *	2. If there is at least one non-deprecated ipif in the
19603 	 *	   IPMP group with a source address on the same subnet as
19604 	 *	   our destination, then fill ipif_arr[] only with
19605 	 *	   source addresses on the same subnet as our destination.
19606 	 *	   Note that because of (1), only the first
19607 	 *	   non-deprecated ipif found with a source address
19608 	 *	   matching the destination ends up in ipif_arr[].
19609 	 *
19610 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19611 	 *	   addresses not in the same subnet as our destination.
19612 	 *	   Again, because of (1), only the first off-subnet source
19613 	 *	   address will be chosen.
19614 	 *
19615 	 *	4. If there are no non-deprecated ipifs, then just use
19616 	 *	   the source address associated with the last deprecated
19617 	 *	   one we find that happens to be on the same subnet,
19618 	 *	   otherwise the first one not in the same subnet.
19619 	 */
19620 	for (; till != NULL; till = till->ill_group_next) {
19621 		ipif_same_found = B_FALSE;
19622 		ipif_other_found = B_FALSE;
19623 		for (ipif = till->ill_ipif; ipif != NULL;
19624 		    ipif = ipif->ipif_next) {
19625 			if (!IPIF_CAN_LOOKUP(ipif))
19626 				continue;
19627 			/* Always skip NOLOCAL and ANYCAST interfaces */
19628 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19629 				continue;
19630 			if (!(ipif->ipif_flags & IPIF_UP))
19631 				continue;
19632 			if (ipif->ipif_zoneid != zoneid)
19633 				continue;
19634 			/*
19635 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19636 			 * but are not valid as source addresses.
19637 			 */
19638 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19639 				continue;
19640 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19641 				if (ipif_dep == NULL ||
19642 				    (ipif->ipif_net_mask & dst) ==
19643 				    ipif->ipif_subnet)
19644 					ipif_dep = ipif;
19645 				continue;
19646 			}
19647 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19648 				/* found a source address in the same subnet */
19649 				if (same_subnet_only == B_FALSE) {
19650 					same_subnet_only = B_TRUE;
19651 					index = 0;
19652 				}
19653 				ipif_same_found = B_TRUE;
19654 			} else {
19655 				if (same_subnet_only == B_TRUE ||
19656 				    ipif_other_found == B_TRUE)
19657 					continue;
19658 				ipif_other_found = B_TRUE;
19659 			}
19660 			ipif_arr[index++] = ipif;
19661 			if (index == MAX_IPIF_SELECT_SOURCE) {
19662 				wrapped = B_TRUE;
19663 				index = 0;
19664 			}
19665 			if (ipif_same_found == B_TRUE)
19666 				break;
19667 		}
19668 	}
19669 
19670 	if (ipif_arr[0] == NULL) {
19671 		ipif = ipif_dep;
19672 	} else {
19673 		if (wrapped)
19674 			index = MAX_IPIF_SELECT_SOURCE;
19675 		ipif = ipif_arr[ipif_rand() % index];
19676 		ASSERT(ipif != NULL);
19677 	}
19678 
19679 	if (ipif != NULL) {
19680 		mutex_enter(&ipif->ipif_ill->ill_lock);
19681 		if (!IPIF_CAN_LOOKUP(ipif)) {
19682 			mutex_exit(&ipif->ipif_ill->ill_lock);
19683 			goto retry;
19684 		}
19685 		ipif_refhold_locked(ipif);
19686 		mutex_exit(&ipif->ipif_ill->ill_lock);
19687 	}
19688 
19689 	rw_exit(&ill_g_lock);
19690 	if (usill != NULL)
19691 		ill_refrele(usill);
19692 
19693 #ifdef DEBUG
19694 	if (ipif == NULL) {
19695 		char buf1[INET6_ADDRSTRLEN];
19696 
19697 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
19698 		    ill->ill_name,
19699 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
19700 	} else {
19701 		char buf1[INET6_ADDRSTRLEN];
19702 		char buf2[INET6_ADDRSTRLEN];
19703 
19704 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
19705 		    ipif->ipif_ill->ill_name,
19706 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
19707 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
19708 		    buf2, sizeof (buf2))));
19709 	}
19710 #endif /* DEBUG */
19711 	return (ipif);
19712 }
19713 
19714 
19715 /*
19716  * If old_ipif is not NULL, see if ipif was derived from old
19717  * ipif and if so, recreate the interface route by re-doing
19718  * source address selection. This happens when ipif_down ->
19719  * ipif_update_other_ipifs calls us.
19720  *
19721  * If old_ipif is NULL, just redo the source address selection
19722  * if needed. This happens when illgrp_insert or ipif_up_done
19723  * calls us.
19724  */
19725 static void
19726 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
19727 {
19728 	ire_t *ire;
19729 	ire_t *ipif_ire;
19730 	queue_t *stq;
19731 	ipif_t *nipif;
19732 	ill_t *ill;
19733 	boolean_t need_rele = B_FALSE;
19734 
19735 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
19736 	ASSERT(IAM_WRITER_IPIF(ipif));
19737 
19738 	ill = ipif->ipif_ill;
19739 	if (!(ipif->ipif_flags &
19740 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
19741 		/*
19742 		 * Can't possibly have borrowed the source
19743 		 * from old_ipif.
19744 		 */
19745 		return;
19746 	}
19747 
19748 	/*
19749 	 * Is there any work to be done? No work if the address
19750 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
19751 	 * ipif_select_source() does not borrow addresses from
19752 	 * NOLOCAL and ANYCAST interfaces).
19753 	 */
19754 	if ((old_ipif != NULL) &&
19755 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
19756 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
19757 	    (old_ipif->ipif_flags &
19758 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
19759 		return;
19760 	}
19761 
19762 	/*
19763 	 * Perform the same checks as when creating the
19764 	 * IRE_INTERFACE in ipif_up_done.
19765 	 */
19766 	if (!(ipif->ipif_flags & IPIF_UP))
19767 		return;
19768 
19769 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
19770 	    (ipif->ipif_subnet == INADDR_ANY))
19771 		return;
19772 
19773 	ipif_ire = ipif_to_ire(ipif);
19774 	if (ipif_ire == NULL)
19775 		return;
19776 
19777 	/*
19778 	 * We know that ipif uses some other source for its
19779 	 * IRE_INTERFACE. Is it using the source of this
19780 	 * old_ipif?
19781 	 */
19782 	if (old_ipif != NULL &&
19783 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
19784 		ire_refrele(ipif_ire);
19785 		return;
19786 	}
19787 	if (ip_debug > 2) {
19788 		/* ip1dbg */
19789 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
19790 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
19791 	}
19792 
19793 	stq = ipif_ire->ire_stq;
19794 
19795 	/*
19796 	 * Can't use our source address. Select a different
19797 	 * source address for the IRE_INTERFACE.
19798 	 */
19799 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
19800 	if (nipif == NULL) {
19801 		/* Last resort - all ipif's have IPIF_NOLOCAL */
19802 		nipif = ipif;
19803 	} else {
19804 		need_rele = B_TRUE;
19805 	}
19806 
19807 	ire = ire_create(
19808 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
19809 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
19810 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
19811 	    NULL,				/* no gateway */
19812 	    NULL,
19813 	    &ipif->ipif_mtu,			/* max frag */
19814 	    NULL,				/* fast path header */
19815 	    NULL,				/* no recv from queue */
19816 	    stq,				/* send-to queue */
19817 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19818 	    ill->ill_resolver_mp,		/* xmit header */
19819 	    ipif,
19820 	    NULL,
19821 	    0,
19822 	    0,
19823 	    0,
19824 	    0,
19825 	    &ire_uinfo_null);
19826 
19827 	if (ire != NULL) {
19828 		ire_t *ret_ire;
19829 		int error;
19830 
19831 		/*
19832 		 * We don't need ipif_ire anymore. We need to delete
19833 		 * before we add so that ire_add does not detect
19834 		 * duplicates.
19835 		 */
19836 		ire_delete(ipif_ire);
19837 		ret_ire = ire;
19838 		error = ire_add(&ret_ire, NULL, NULL, NULL);
19839 		ASSERT(error == 0);
19840 		ASSERT(ire == ret_ire);
19841 		/* Held in ire_add */
19842 		ire_refrele(ret_ire);
19843 	}
19844 	/*
19845 	 * Either we are falling through from above or could not
19846 	 * allocate a replacement.
19847 	 */
19848 	ire_refrele(ipif_ire);
19849 	if (need_rele)
19850 		ipif_refrele(nipif);
19851 }
19852 
19853 /*
19854  * This old_ipif is going away.
19855  *
19856  * Determine if any other ipif's is using our address as
19857  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
19858  * IPIF_DEPRECATED).
19859  * Find the IRE_INTERFACE for such ipifs and recreate them
19860  * to use an different source address following the rules in
19861  * ipif_up_done.
19862  *
19863  * This function takes an illgrp as an argument so that illgrp_delete
19864  * can call this to update source address even after deleting the
19865  * old_ipif->ipif_ill from the ill group.
19866  */
19867 static void
19868 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
19869 {
19870 	ipif_t *ipif;
19871 	ill_t *ill;
19872 	char	buf[INET6_ADDRSTRLEN];
19873 
19874 	ASSERT(IAM_WRITER_IPIF(old_ipif));
19875 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
19876 
19877 	ill = old_ipif->ipif_ill;
19878 
19879 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
19880 	    ill->ill_name,
19881 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
19882 	    buf, sizeof (buf))));
19883 	/*
19884 	 * If this part of a group, look at all ills as ipif_select_source
19885 	 * borrows source address across all the ills in the group.
19886 	 */
19887 	if (illgrp != NULL)
19888 		ill = illgrp->illgrp_ill;
19889 
19890 	for (; ill != NULL; ill = ill->ill_group_next) {
19891 		for (ipif = ill->ill_ipif; ipif != NULL;
19892 		    ipif = ipif->ipif_next) {
19893 
19894 			if (ipif == old_ipif)
19895 				continue;
19896 
19897 			ipif_recreate_interface_routes(old_ipif, ipif);
19898 		}
19899 	}
19900 }
19901 
19902 /* ARGSUSED */
19903 int
19904 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19905 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19906 {
19907 	/*
19908 	 * ill_phyint_reinit merged the v4 and v6 into a single
19909 	 * ipsq. Could also have become part of a ipmp group in the
19910 	 * process, and we might not have been able to complete the
19911 	 * operation in ipif_set_values, if we could not become
19912 	 * exclusive.  If so restart it here.
19913 	 */
19914 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
19915 }
19916 
19917 
19918 /* ARGSUSED */
19919 int
19920 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19921     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19922 {
19923 	queue_t		*q1 = q;
19924 	char 		*cp;
19925 	char		interf_name[LIFNAMSIZ];
19926 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
19927 
19928 	if (!q->q_next) {
19929 		ip1dbg((
19930 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
19931 		return (EINVAL);
19932 	}
19933 
19934 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
19935 		return (EALREADY);
19936 
19937 	do {
19938 		q1 = q1->q_next;
19939 	} while (q1->q_next);
19940 	cp = q1->q_qinfo->qi_minfo->mi_idname;
19941 	(void) sprintf(interf_name, "%s%d", cp, ppa);
19942 
19943 	/*
19944 	 * Here we are not going to delay the ioack until after
19945 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
19946 	 * original ioctl message before sending the requests.
19947 	 */
19948 	return (ipif_set_values(q, mp, interf_name, &ppa));
19949 }
19950 
19951 /* ARGSUSED */
19952 int
19953 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19954     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19955 {
19956 	return (ENXIO);
19957 }
19958 
19959 /*
19960  * Net and subnet broadcast ire's are now specific to the particular
19961  * physical interface (ill) and not to any one locigal interface (ipif).
19962  * However, if a particular logical interface is being taken down, it's
19963  * associated ire's will be taken down as well.  Hence, when we go to
19964  * take down or change the local address, broadcast address or netmask
19965  * of a specific logical interface, we must check to make sure that we
19966  * have valid net and subnet broadcast ire's for the other logical
19967  * interfaces which may have been shared with the logical interface
19968  * being brought down or changed.
19969  *
19970  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
19971  * is tied to the first interface coming UP. If that ipif is going down,
19972  * we need to recreate them on the next valid ipif.
19973  *
19974  * Note: assume that the ipif passed in is still up so that it's IRE
19975  * entries are still valid.
19976  */
19977 static void
19978 ipif_check_bcast_ires(ipif_t *test_ipif)
19979 {
19980 	ipif_t	*ipif;
19981 	ire_t	*test_subnet_ire, *test_net_ire;
19982 	ire_t	*test_allzero_ire, *test_allone_ire;
19983 	ire_t	*ire_array[12];
19984 	ire_t	**irep = &ire_array[0];
19985 	ire_t	**irep1;
19986 
19987 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
19988 	ipaddr_t test_net_addr, test_subnet_addr;
19989 	ipaddr_t test_net_mask, test_subnet_mask;
19990 	boolean_t need_net_bcast_ire = B_FALSE;
19991 	boolean_t need_subnet_bcast_ire = B_FALSE;
19992 	boolean_t allzero_bcast_ire_created = B_FALSE;
19993 	boolean_t allone_bcast_ire_created = B_FALSE;
19994 	boolean_t net_bcast_ire_created = B_FALSE;
19995 	boolean_t subnet_bcast_ire_created = B_FALSE;
19996 
19997 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
19998 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
19999 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
20000 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
20001 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
20002 
20003 	ASSERT(!test_ipif->ipif_isv6);
20004 	ASSERT(IAM_WRITER_IPIF(test_ipif));
20005 
20006 	/*
20007 	 * No broadcast IREs for the LOOPBACK interface
20008 	 * or others such as point to point and IPIF_NOXMIT.
20009 	 */
20010 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
20011 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
20012 		return;
20013 
20014 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
20015 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20016 
20017 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
20018 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20019 
20020 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
20021 	test_subnet_mask = test_ipif->ipif_net_mask;
20022 
20023 	/*
20024 	 * If no net mask set, assume the default based on net class.
20025 	 */
20026 	if (test_subnet_mask == 0)
20027 		test_subnet_mask = test_net_mask;
20028 
20029 	/*
20030 	 * Check if there is a network broadcast ire associated with this ipif
20031 	 */
20032 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
20033 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
20034 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20035 
20036 	/*
20037 	 * Check if there is a subnet broadcast IRE associated with this ipif
20038 	 */
20039 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
20040 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
20041 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20042 
20043 	/*
20044 	 * No broadcast ire's associated with this ipif.
20045 	 */
20046 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
20047 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
20048 		return;
20049 	}
20050 
20051 	/*
20052 	 * We have established which bcast ires have to be replaced.
20053 	 * Next we try to locate ipifs that match there ires.
20054 	 * The rules are simple: If we find an ipif that matches on the subnet
20055 	 * address it will also match on the net address, the allzeros and
20056 	 * allones address. Any ipif that matches only on the net address will
20057 	 * also match the allzeros and allones addresses.
20058 	 * The other criterion is the ipif_flags. We look for non-deprecated
20059 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
20060 	 * ipifs with check_flags matching (deprecated, etc) are used only
20061 	 * if good ipifs are not available. While looping, we save existing
20062 	 * deprecated ipifs as backup_ipif.
20063 	 * We loop through all the ipifs for this ill looking for ipifs
20064 	 * whose broadcast addr match the ipif passed in, but do not have
20065 	 * their own broadcast ires. For creating 0.0.0.0 and
20066 	 * 255.255.255.255 we just need an ipif on this ill to create.
20067 	 */
20068 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
20069 	    ipif = ipif->ipif_next) {
20070 
20071 		ASSERT(!ipif->ipif_isv6);
20072 		/*
20073 		 * Already checked the ipif passed in.
20074 		 */
20075 		if (ipif == test_ipif) {
20076 			continue;
20077 		}
20078 
20079 		/*
20080 		 * We only need to recreate broadcast ires if another ipif in
20081 		 * the same zone uses them. The new ires must be created in the
20082 		 * same zone.
20083 		 */
20084 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20085 			continue;
20086 		}
20087 
20088 		/*
20089 		 * Only interested in logical interfaces with valid local
20090 		 * addresses or with the ability to broadcast.
20091 		 */
20092 		if ((ipif->ipif_subnet == 0) ||
20093 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20094 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20095 		    !(ipif->ipif_flags & IPIF_UP)) {
20096 			continue;
20097 		}
20098 		/*
20099 		 * Check if there is a net broadcast ire for this
20100 		 * net address.  If it turns out that the ipif we are
20101 		 * about to take down owns this ire, we must make a
20102 		 * new one because it is potentially going away.
20103 		 */
20104 		if (test_net_ire && (!net_bcast_ire_created)) {
20105 			net_mask = ip_net_mask(ipif->ipif_subnet);
20106 			net_addr = net_mask & ipif->ipif_subnet;
20107 			if (net_addr == test_net_addr) {
20108 				need_net_bcast_ire = B_TRUE;
20109 				/*
20110 				 * Use DEPRECATED ipif only if no good
20111 				 * ires are available. subnet_addr is
20112 				 * a better match than net_addr.
20113 				 */
20114 				if ((ipif->ipif_flags & check_flags) &&
20115 				    (backup_ipif_net == NULL)) {
20116 					backup_ipif_net = ipif;
20117 				}
20118 			}
20119 		}
20120 		/*
20121 		 * Check if there is a subnet broadcast ire for this
20122 		 * net address.  If it turns out that the ipif we are
20123 		 * about to take down owns this ire, we must make a
20124 		 * new one because it is potentially going away.
20125 		 */
20126 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20127 			subnet_mask = ipif->ipif_net_mask;
20128 			subnet_addr = ipif->ipif_subnet;
20129 			if (subnet_addr == test_subnet_addr) {
20130 				need_subnet_bcast_ire = B_TRUE;
20131 				if ((ipif->ipif_flags & check_flags) &&
20132 				    (backup_ipif_subnet == NULL)) {
20133 					backup_ipif_subnet = ipif;
20134 				}
20135 			}
20136 		}
20137 
20138 
20139 		/* Short circuit here if this ipif is deprecated */
20140 		if (ipif->ipif_flags & check_flags) {
20141 			if ((test_allzero_ire != NULL) &&
20142 			    (!allzero_bcast_ire_created) &&
20143 			    (backup_ipif_allzeros == NULL)) {
20144 				backup_ipif_allzeros = ipif;
20145 			}
20146 			if ((test_allone_ire != NULL) &&
20147 			    (!allone_bcast_ire_created) &&
20148 			    (backup_ipif_allones == NULL)) {
20149 				backup_ipif_allones = ipif;
20150 			}
20151 			continue;
20152 		}
20153 
20154 		/*
20155 		 * Found an ipif which has the same broadcast ire as the
20156 		 * ipif passed in and the ipif passed in "owns" the ire.
20157 		 * Create new broadcast ire's for this broadcast addr.
20158 		 */
20159 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20160 			irep = ire_create_bcast(ipif, net_addr, irep);
20161 			irep = ire_create_bcast(ipif,
20162 			    ~net_mask | net_addr, irep);
20163 			net_bcast_ire_created = B_TRUE;
20164 		}
20165 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20166 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20167 			irep = ire_create_bcast(ipif,
20168 			    ~subnet_mask | subnet_addr, irep);
20169 			subnet_bcast_ire_created = B_TRUE;
20170 		}
20171 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20172 			irep = ire_create_bcast(ipif, 0, irep);
20173 			allzero_bcast_ire_created = B_TRUE;
20174 		}
20175 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20176 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20177 			allone_bcast_ire_created = B_TRUE;
20178 		}
20179 		/*
20180 		 * Once we have created all the appropriate ires, we
20181 		 * just break out of this loop to add what we have created.
20182 		 * This has been indented similar to ire_match_args for
20183 		 * readability.
20184 		 */
20185 		if (((test_net_ire == NULL) ||
20186 			(net_bcast_ire_created)) &&
20187 		    ((test_subnet_ire == NULL) ||
20188 			(subnet_bcast_ire_created)) &&
20189 		    ((test_allzero_ire == NULL) ||
20190 			(allzero_bcast_ire_created)) &&
20191 		    ((test_allone_ire == NULL) ||
20192 			(allone_bcast_ire_created))) {
20193 			break;
20194 		}
20195 	}
20196 
20197 	/*
20198 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20199 	 * exist. 6 pairs of bcast ires are needed.
20200 	 * Note - the old ires are deleted in ipif_down.
20201 	 */
20202 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20203 		ipif = backup_ipif_net;
20204 		irep = ire_create_bcast(ipif, net_addr, irep);
20205 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20206 		net_bcast_ire_created = B_TRUE;
20207 	}
20208 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20209 	    backup_ipif_subnet) {
20210 		ipif = backup_ipif_subnet;
20211 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20212 		irep = ire_create_bcast(ipif,
20213 		    ~subnet_mask | subnet_addr, irep);
20214 		subnet_bcast_ire_created = B_TRUE;
20215 	}
20216 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20217 	    backup_ipif_allzeros) {
20218 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20219 		allzero_bcast_ire_created = B_TRUE;
20220 	}
20221 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20222 	    backup_ipif_allones) {
20223 		irep = ire_create_bcast(backup_ipif_allones,
20224 		    INADDR_BROADCAST, irep);
20225 		allone_bcast_ire_created = B_TRUE;
20226 	}
20227 
20228 	/*
20229 	 * If we can't create all of them, don't add any of them.
20230 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20231 	 * always have a non-loopback copy and loopback copy
20232 	 * for a given address.
20233 	 */
20234 	for (irep1 = irep; irep1 > ire_array; ) {
20235 		irep1--;
20236 		if (*irep1 == NULL) {
20237 			ip0dbg(("ipif_check_bcast_ires: can't create "
20238 			    "IRE_BROADCAST, memory allocation failure\n"));
20239 			while (irep > ire_array) {
20240 				irep--;
20241 				if (*irep != NULL)
20242 					ire_delete(*irep);
20243 			}
20244 			goto bad;
20245 		}
20246 	}
20247 	for (irep1 = irep; irep1 > ire_array; ) {
20248 		int error;
20249 
20250 		irep1--;
20251 		error = ire_add(irep1, NULL, NULL, NULL);
20252 		if (error == 0) {
20253 			ire_refrele(*irep1);		/* Held in ire_add */
20254 		}
20255 	}
20256 bad:
20257 	if (test_allzero_ire != NULL)
20258 		ire_refrele(test_allzero_ire);
20259 	if (test_allone_ire != NULL)
20260 		ire_refrele(test_allone_ire);
20261 	if (test_net_ire != NULL)
20262 		ire_refrele(test_net_ire);
20263 	if (test_subnet_ire != NULL)
20264 		ire_refrele(test_subnet_ire);
20265 }
20266 
20267 /*
20268  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20269  * from lifr_flags and the name from lifr_name.
20270  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20271  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20272  * Returns EINPROGRESS when mp has been consumed by queueing it on
20273  * ill_pending_mp and the ioctl will complete in ip_rput.
20274  */
20275 /* ARGSUSED */
20276 int
20277 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20278     ip_ioctl_cmd_t *ipip, void *if_req)
20279 {
20280 	int	err;
20281 	ill_t	*ill;
20282 	struct lifreq *lifr = (struct lifreq *)if_req;
20283 
20284 	ASSERT(ipif != NULL);
20285 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20286 	ASSERT(q->q_next != NULL);
20287 
20288 	ill = (ill_t *)q->q_ptr;
20289 	/*
20290 	 * If we are not writer on 'q' then this interface exists already
20291 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20292 	 * So return EALREADY
20293 	 */
20294 	if (ill != ipif->ipif_ill)
20295 		return (EALREADY);
20296 
20297 	if (ill->ill_name[0] != '\0')
20298 		return (EALREADY);
20299 
20300 	/*
20301 	 * Set all the flags. Allows all kinds of override. Provide some
20302 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20303 	 * unless there is either multicast/broadcast support in the driver
20304 	 * or it is a pt-pt link.
20305 	 */
20306 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20307 		/* Meaningless to IP thus don't allow them to be set. */
20308 		ip1dbg(("ip_setname: EINVAL 1\n"));
20309 		return (EINVAL);
20310 	}
20311 	/*
20312 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20313 	 * ill_bcast_addr_length info.
20314 	 */
20315 	if (!ill->ill_needs_attach &&
20316 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20317 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20318 	    ill->ill_bcast_addr_length == 0)) {
20319 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20320 		ip1dbg(("ip_setname: EINVAL 2\n"));
20321 		return (EINVAL);
20322 	}
20323 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20324 	    ((lifr->lifr_flags & IFF_IPV6) ||
20325 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20326 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20327 		ip1dbg(("ip_setname: EINVAL 3\n"));
20328 		return (EINVAL);
20329 	}
20330 	if (lifr->lifr_flags & IFF_UP) {
20331 		/* Can only be set with SIOCSLIFFLAGS */
20332 		ip1dbg(("ip_setname: EINVAL 4\n"));
20333 		return (EINVAL);
20334 	}
20335 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20336 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20337 		ip1dbg(("ip_setname: EINVAL 5\n"));
20338 		return (EINVAL);
20339 	}
20340 	/*
20341 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20342 	 */
20343 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20344 	    !(lifr->lifr_flags & IFF_IPV6) &&
20345 	    !(ipif->ipif_isv6)) {
20346 		ip1dbg(("ip_setname: EINVAL 6\n"));
20347 		return (EINVAL);
20348 	}
20349 
20350 	/*
20351 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20352 	 * we have all the flags here. So, we assign rather than we OR.
20353 	 * We can't OR the flags here because we don't want to set
20354 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20355 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20356 	 * on lifr_flags value here.
20357 	 */
20358 	/*
20359 	 * This ill has not been inserted into the global list.
20360 	 * So we are still single threaded and don't need any lock
20361 	 */
20362 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20363 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20364 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20365 
20366 	/* We started off as V4. */
20367 	if (ill->ill_flags & ILLF_IPV6) {
20368 		ill->ill_phyint->phyint_illv6 = ill;
20369 		ill->ill_phyint->phyint_illv4 = NULL;
20370 	}
20371 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20372 	return (err);
20373 }
20374 
20375 /* ARGSUSED */
20376 int
20377 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20378     ip_ioctl_cmd_t *ipip, void *if_req)
20379 {
20380 	/*
20381 	 * ill_phyint_reinit merged the v4 and v6 into a single
20382 	 * ipsq. Could also have become part of a ipmp group in the
20383 	 * process, and we might not have been able to complete the
20384 	 * slifname in ipif_set_values, if we could not become
20385 	 * exclusive.  If so restart it here
20386 	 */
20387 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20388 }
20389 
20390 /*
20391  * Return a pointer to the ipif which matches the index, IP version type and
20392  * zoneid.
20393  */
20394 ipif_t *
20395 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20396     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20397 {
20398 	ill_t	*ill;
20399 	ipsq_t  *ipsq;
20400 	phyint_t *phyi;
20401 	ipif_t	*ipif;
20402 
20403 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20404 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20405 
20406 	if (err != NULL)
20407 		*err = 0;
20408 
20409 	/*
20410 	 * Indexes are stored in the phyint - a common structure
20411 	 * to both IPv4 and IPv6.
20412 	 */
20413 
20414 	rw_enter(&ill_g_lock, RW_READER);
20415 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20416 	    (void *) &index, NULL);
20417 	if (phyi != NULL) {
20418 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20419 		if (ill == NULL) {
20420 			rw_exit(&ill_g_lock);
20421 			if (err != NULL)
20422 				*err = ENXIO;
20423 			return (NULL);
20424 		}
20425 		GRAB_CONN_LOCK(q);
20426 		mutex_enter(&ill->ill_lock);
20427 		if (ILL_CAN_LOOKUP(ill)) {
20428 			for (ipif = ill->ill_ipif; ipif != NULL;
20429 			    ipif = ipif->ipif_next) {
20430 				if (IPIF_CAN_LOOKUP(ipif) &&
20431 				    (zoneid == ALL_ZONES ||
20432 				    zoneid == ipif->ipif_zoneid)) {
20433 					ipif_refhold_locked(ipif);
20434 					mutex_exit(&ill->ill_lock);
20435 					RELEASE_CONN_LOCK(q);
20436 					rw_exit(&ill_g_lock);
20437 					return (ipif);
20438 				}
20439 			}
20440 		} else if (ILL_CAN_WAIT(ill, q)) {
20441 			ipsq = ill->ill_phyint->phyint_ipsq;
20442 			mutex_enter(&ipsq->ipsq_lock);
20443 			rw_exit(&ill_g_lock);
20444 			mutex_exit(&ill->ill_lock);
20445 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20446 			mutex_exit(&ipsq->ipsq_lock);
20447 			RELEASE_CONN_LOCK(q);
20448 			*err = EINPROGRESS;
20449 			return (NULL);
20450 		}
20451 		mutex_exit(&ill->ill_lock);
20452 		RELEASE_CONN_LOCK(q);
20453 	}
20454 	rw_exit(&ill_g_lock);
20455 	if (err != NULL)
20456 		*err = ENXIO;
20457 	return (NULL);
20458 }
20459 
20460 typedef struct conn_change_s {
20461 	uint_t cc_old_ifindex;
20462 	uint_t cc_new_ifindex;
20463 } conn_change_t;
20464 
20465 /*
20466  * ipcl_walk function for changing interface index.
20467  */
20468 static void
20469 conn_change_ifindex(conn_t *connp, caddr_t arg)
20470 {
20471 	conn_change_t *connc;
20472 	uint_t old_ifindex;
20473 	uint_t new_ifindex;
20474 	int i;
20475 	ilg_t *ilg;
20476 
20477 	connc = (conn_change_t *)arg;
20478 	old_ifindex = connc->cc_old_ifindex;
20479 	new_ifindex = connc->cc_new_ifindex;
20480 
20481 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20482 		connp->conn_orig_bound_ifindex = new_ifindex;
20483 
20484 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20485 		connp->conn_orig_multicast_ifindex = new_ifindex;
20486 
20487 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20488 		connp->conn_orig_xmit_ifindex = new_ifindex;
20489 
20490 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20491 		ilg = &connp->conn_ilg[i];
20492 		if (ilg->ilg_orig_ifindex == old_ifindex)
20493 			ilg->ilg_orig_ifindex = new_ifindex;
20494 	}
20495 }
20496 
20497 /*
20498  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20499  * to new_index if it matches the old_index.
20500  *
20501  * Failovers typically happen within a group of ills. But somebody
20502  * can remove an ill from the group after a failover happened. If
20503  * we are setting the ifindex after this, we potentially need to
20504  * look at all the ills rather than just the ones in the group.
20505  * We cut down the work by looking at matching ill_net_types
20506  * and ill_types as we could not possibly grouped them together.
20507  */
20508 static void
20509 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20510 {
20511 	ill_t *ill;
20512 	ipif_t *ipif;
20513 	uint_t old_ifindex;
20514 	uint_t new_ifindex;
20515 	ilm_t *ilm;
20516 	ill_walk_context_t ctx;
20517 
20518 	old_ifindex = connc->cc_old_ifindex;
20519 	new_ifindex = connc->cc_new_ifindex;
20520 
20521 	rw_enter(&ill_g_lock, RW_READER);
20522 	ill = ILL_START_WALK_ALL(&ctx);
20523 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20524 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20525 			(ill_orig->ill_type != ill->ill_type)) {
20526 			continue;
20527 		}
20528 		for (ipif = ill->ill_ipif; ipif != NULL;
20529 				ipif = ipif->ipif_next) {
20530 			if (ipif->ipif_orig_ifindex == old_ifindex)
20531 				ipif->ipif_orig_ifindex = new_ifindex;
20532 		}
20533 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20534 			if (ilm->ilm_orig_ifindex == old_ifindex)
20535 				ilm->ilm_orig_ifindex = new_ifindex;
20536 		}
20537 	}
20538 	rw_exit(&ill_g_lock);
20539 }
20540 
20541 /*
20542  * We first need to ensure that the new index is unique, and
20543  * then carry the change across both v4 and v6 ill representation
20544  * of the physical interface.
20545  */
20546 /* ARGSUSED */
20547 int
20548 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20549     ip_ioctl_cmd_t *ipip, void *ifreq)
20550 {
20551 	ill_t		*ill;
20552 	ill_t		*ill_other;
20553 	phyint_t	*phyi;
20554 	int		old_index;
20555 	conn_change_t	connc;
20556 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20557 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20558 	uint_t	index;
20559 	ill_t	*ill_v4;
20560 	ill_t	*ill_v6;
20561 
20562 	if (ipip->ipi_cmd_type == IF_CMD)
20563 		index = ifr->ifr_index;
20564 	else
20565 		index = lifr->lifr_index;
20566 
20567 	/*
20568 	 * Only allow on physical interface. Also, index zero is illegal.
20569 	 *
20570 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20571 	 *
20572 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20573 	 *    implies a possible failback might have to happen. As failback
20574 	 *    depends on the old index, we should fail setting the index.
20575 	 *
20576 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20577 	 *    any addresses or multicast memberships are failed over to
20578 	 *    a non-STANDBY interface. As failback depends on the old
20579 	 *    index, we should fail setting the index for this case also.
20580 	 *
20581 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20582 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20583 	 */
20584 	ill = ipif->ipif_ill;
20585 	phyi = ill->ill_phyint;
20586 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20587 	    ipif->ipif_id != 0 || index == 0) {
20588 		return (EINVAL);
20589 	}
20590 	old_index = phyi->phyint_ifindex;
20591 
20592 	/* If the index is not changing, no work to do */
20593 	if (old_index == index)
20594 		return (0);
20595 
20596 	/*
20597 	 * Use ill_lookup_on_ifindex to determine if the
20598 	 * new index is unused and if so allow the change.
20599 	 */
20600 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20601 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20602 	if (ill_v6 != NULL || ill_v4 != NULL) {
20603 		if (ill_v4 != NULL)
20604 			ill_refrele(ill_v4);
20605 		if (ill_v6 != NULL)
20606 			ill_refrele(ill_v6);
20607 		return (EBUSY);
20608 	}
20609 
20610 	/*
20611 	 * The new index is unused. Set it in the phyint.
20612 	 * Locate the other ill so that we can send a routing
20613 	 * sockets message.
20614 	 */
20615 	if (ill->ill_isv6) {
20616 		ill_other = phyi->phyint_illv4;
20617 	} else {
20618 		ill_other = phyi->phyint_illv6;
20619 	}
20620 
20621 	phyi->phyint_ifindex = index;
20622 
20623 	connc.cc_old_ifindex = old_index;
20624 	connc.cc_new_ifindex = index;
20625 	ip_change_ifindex(ill, &connc);
20626 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20627 
20628 	/* Send the routing sockets message */
20629 	ip_rts_ifmsg(ipif);
20630 	if (ill_other != NULL)
20631 		ip_rts_ifmsg(ill_other->ill_ipif);
20632 
20633 	return (0);
20634 }
20635 
20636 /* ARGSUSED */
20637 int
20638 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20639     ip_ioctl_cmd_t *ipip, void *ifreq)
20640 {
20641 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20642 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20643 
20644 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20645 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20646 	/* Get the interface index */
20647 	if (ipip->ipi_cmd_type == IF_CMD) {
20648 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20649 	} else {
20650 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20651 	}
20652 	return (0);
20653 }
20654 
20655 /* ARGSUSED */
20656 int
20657 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20658     ip_ioctl_cmd_t *ipip, void *ifreq)
20659 {
20660 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20661 
20662 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20663 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20664 	/* Get the interface zone */
20665 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20666 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20667 	return (0);
20668 }
20669 
20670 /*
20671  * Set the zoneid of an interface.
20672  */
20673 /* ARGSUSED */
20674 int
20675 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20676     ip_ioctl_cmd_t *ipip, void *ifreq)
20677 {
20678 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20679 	int err = 0;
20680 	boolean_t need_up = B_FALSE;
20681 	zone_t *zptr;
20682 	zone_status_t status;
20683 	zoneid_t zoneid;
20684 
20685 	/* cannot assign instance zero to a non-global zone */
20686 	if (ipif->ipif_id == 0)
20687 		return (ENOTSUP);
20688 
20689 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20690 	zoneid = lifr->lifr_zoneid;
20691 
20692 	/*
20693 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
20694 	 * the event of a race with the zone shutdown processing, since IP
20695 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
20696 	 * interface will be cleaned up even if the zone is shut down
20697 	 * immediately after the status check. If the interface can't be brought
20698 	 * down right away, and the zone is shut down before the restart
20699 	 * function is called, we resolve the possible races by rechecking the
20700 	 * zone status in the restart function.
20701 	 */
20702 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
20703 		return (EINVAL);
20704 	status = zone_status_get(zptr);
20705 	zone_rele(zptr);
20706 
20707 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
20708 		return (EINVAL);
20709 
20710 	if (ipif->ipif_flags & IPIF_UP) {
20711 		/*
20712 		 * If the interface is already marked up,
20713 		 * we call ipif_down which will take care
20714 		 * of ditching any IREs that have been set
20715 		 * up based on the old interface address.
20716 		 */
20717 		err = ipif_logical_down(ipif, q, mp);
20718 		if (err == EINPROGRESS)
20719 			return (err);
20720 		ipif_down_tail(ipif);
20721 		need_up = B_TRUE;
20722 	}
20723 
20724 	err = ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, need_up);
20725 	return (err);
20726 }
20727 
20728 static int
20729 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
20730     queue_t *q, mblk_t *mp, boolean_t need_up)
20731 {
20732 	int	err = 0;
20733 
20734 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
20735 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20736 
20737 	/* Set the new zone id. */
20738 	ipif->ipif_zoneid = zoneid;
20739 
20740 	/* Update sctp list */
20741 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
20742 
20743 	if (need_up) {
20744 		/*
20745 		 * Now bring the interface back up.  If this
20746 		 * is the only IPIF for the ILL, ipif_up
20747 		 * will have to re-bind to the device, so
20748 		 * we may get back EINPROGRESS, in which
20749 		 * case, this IOCTL will get completed in
20750 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
20751 		 */
20752 		err = ipif_up(ipif, q, mp);
20753 	}
20754 	return (err);
20755 }
20756 
20757 /* ARGSUSED */
20758 int
20759 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20760     ip_ioctl_cmd_t *ipip, void *if_req)
20761 {
20762 	struct lifreq *lifr = (struct lifreq *)if_req;
20763 	zoneid_t zoneid;
20764 	zone_t *zptr;
20765 	zone_status_t status;
20766 
20767 	ASSERT(ipif->ipif_id != 0);
20768 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20769 	zoneid = lifr->lifr_zoneid;
20770 
20771 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
20772 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20773 
20774 	/*
20775 	 * We recheck the zone status to resolve the following race condition:
20776 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
20777 	 * 2) hme0:1 is up and can't be brought down right away;
20778 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
20779 	 * 3) zone "myzone" is halted; the zone status switches to
20780 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
20781 	 * the interfaces to remove - hme0:1 is not returned because it's not
20782 	 * yet in "myzone", so it won't be removed;
20783 	 * 4) the restart function for SIOCSLIFZONE is called; without the
20784 	 * status check here, we would have hme0:1 in "myzone" after it's been
20785 	 * destroyed.
20786 	 * Note that if the status check fails, we need to bring the interface
20787 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
20788 	 * ipif_up_done[_v6]().
20789 	 */
20790 	status = ZONE_IS_UNINITIALIZED;
20791 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
20792 		status = zone_status_get(zptr);
20793 		zone_rele(zptr);
20794 	}
20795 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
20796 		if (ipif->ipif_isv6) {
20797 			(void) ipif_up_done_v6(ipif);
20798 		} else {
20799 			(void) ipif_up_done(ipif);
20800 		}
20801 		return (EINVAL);
20802 	}
20803 
20804 	ipif_down_tail(ipif);
20805 
20806 	return (ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, B_TRUE));
20807 }
20808 
20809 /* ARGSUSED */
20810 int
20811 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20812 	ip_ioctl_cmd_t *ipip, void *ifreq)
20813 {
20814 	struct lifreq	*lifr = ifreq;
20815 
20816 	ASSERT(q->q_next == NULL);
20817 	ASSERT(CONN_Q(q));
20818 
20819 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
20820 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20821 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
20822 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
20823 
20824 	return (0);
20825 }
20826 
20827 
20828 /* Find the previous ILL in this usesrc group */
20829 static ill_t *
20830 ill_prev_usesrc(ill_t *uill)
20831 {
20832 	ill_t *ill;
20833 
20834 	for (ill = uill->ill_usesrc_grp_next;
20835 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
20836 	    ill = ill->ill_usesrc_grp_next)
20837 		/* do nothing */;
20838 	return (ill);
20839 }
20840 
20841 /*
20842  * Release all members of the usesrc group. This routine is called
20843  * from ill_delete when the interface being unplumbed is the
20844  * group head.
20845  */
20846 static void
20847 ill_disband_usesrc_group(ill_t *uill)
20848 {
20849 	ill_t *next_ill, *tmp_ill;
20850 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
20851 	next_ill = uill->ill_usesrc_grp_next;
20852 
20853 	do {
20854 		ASSERT(next_ill != NULL);
20855 		tmp_ill = next_ill->ill_usesrc_grp_next;
20856 		ASSERT(tmp_ill != NULL);
20857 		next_ill->ill_usesrc_grp_next = NULL;
20858 		next_ill->ill_usesrc_ifindex = 0;
20859 		next_ill = tmp_ill;
20860 	} while (next_ill->ill_usesrc_ifindex != 0);
20861 	uill->ill_usesrc_grp_next = NULL;
20862 }
20863 
20864 /*
20865  * Remove the client usesrc ILL from the list and relink to a new list
20866  */
20867 int
20868 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
20869 {
20870 	ill_t *ill, *tmp_ill;
20871 
20872 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
20873 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
20874 
20875 	/*
20876 	 * Check if the usesrc client ILL passed in is not already
20877 	 * in use as a usesrc ILL i.e one whose source address is
20878 	 * in use OR a usesrc ILL is not already in use as a usesrc
20879 	 * client ILL
20880 	 */
20881 	if ((ucill->ill_usesrc_ifindex == 0) ||
20882 	    (uill->ill_usesrc_ifindex != 0)) {
20883 		return (-1);
20884 	}
20885 
20886 	ill = ill_prev_usesrc(ucill);
20887 	ASSERT(ill->ill_usesrc_grp_next != NULL);
20888 
20889 	/* Remove from the current list */
20890 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
20891 		/* Only two elements in the list */
20892 		ASSERT(ill->ill_usesrc_ifindex == 0);
20893 		ill->ill_usesrc_grp_next = NULL;
20894 	} else {
20895 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
20896 	}
20897 
20898 	if (ifindex == 0) {
20899 		ucill->ill_usesrc_ifindex = 0;
20900 		ucill->ill_usesrc_grp_next = NULL;
20901 		return (0);
20902 	}
20903 
20904 	ucill->ill_usesrc_ifindex = ifindex;
20905 	tmp_ill = uill->ill_usesrc_grp_next;
20906 	uill->ill_usesrc_grp_next = ucill;
20907 	ucill->ill_usesrc_grp_next =
20908 	    (tmp_ill != NULL) ? tmp_ill : uill;
20909 	return (0);
20910 }
20911 
20912 /*
20913  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
20914  * ip.c for locking details.
20915  */
20916 /* ARGSUSED */
20917 int
20918 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20919     ip_ioctl_cmd_t *ipip, void *ifreq)
20920 {
20921 	struct lifreq *lifr = (struct lifreq *)ifreq;
20922 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
20923 	    ill_flag_changed = B_FALSE;
20924 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
20925 	int err = 0, ret;
20926 	uint_t ifindex;
20927 	phyint_t *us_phyint, *us_cli_phyint;
20928 	ipsq_t *ipsq = NULL;
20929 
20930 	ASSERT(IAM_WRITER_IPIF(ipif));
20931 	ASSERT(q->q_next == NULL);
20932 	ASSERT(CONN_Q(q));
20933 
20934 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
20935 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
20936 
20937 	ASSERT(us_cli_phyint != NULL);
20938 
20939 	/*
20940 	 * If the client ILL is being used for IPMP, abort.
20941 	 * Note, this can be done before ipsq_try_enter since we are already
20942 	 * exclusive on this ILL
20943 	 */
20944 	if ((us_cli_phyint->phyint_groupname != NULL) ||
20945 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
20946 		return (EINVAL);
20947 	}
20948 
20949 	ifindex = lifr->lifr_index;
20950 	if (ifindex == 0) {
20951 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
20952 			/* non usesrc group interface, nothing to reset */
20953 			return (0);
20954 		}
20955 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
20956 		/* valid reset request */
20957 		reset_flg = B_TRUE;
20958 	}
20959 
20960 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
20961 	    ip_process_ioctl, &err);
20962 
20963 	if (usesrc_ill == NULL) {
20964 		return (err);
20965 	}
20966 
20967 	/*
20968 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
20969 	 * group nor can either of the interfaces be used for standy. So
20970 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
20971 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
20972 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
20973 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
20974 	 * the usesrc_cli_ill
20975 	 */
20976 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
20977 	    NEW_OP, B_TRUE);
20978 	if (ipsq == NULL) {
20979 		err = EINPROGRESS;
20980 		/* Operation enqueued on the ipsq of the usesrc ILL */
20981 		goto done;
20982 	}
20983 
20984 	/* Check if the usesrc_ill is used for IPMP */
20985 	us_phyint = usesrc_ill->ill_phyint;
20986 	if ((us_phyint->phyint_groupname != NULL) ||
20987 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
20988 		err = EINVAL;
20989 		goto done;
20990 	}
20991 
20992 	/*
20993 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
20994 	 * already a client then return EINVAL
20995 	 */
20996 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
20997 		err = EINVAL;
20998 		goto done;
20999 	}
21000 
21001 	/*
21002 	 * If the ill_usesrc_ifindex field is already set to what it needs to
21003 	 * be then this is a duplicate operation.
21004 	 */
21005 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
21006 		err = 0;
21007 		goto done;
21008 	}
21009 
21010 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
21011 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
21012 	    usesrc_ill->ill_isv6));
21013 
21014 	/*
21015 	 * The next step ensures that no new ires will be created referencing
21016 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
21017 	 * we go through an ire walk deleting all ire caches that reference
21018 	 * the client ill. New ires referencing the client ill that are added
21019 	 * to the ire table before the ILL_CHANGING flag is set, will be
21020 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
21021 	 * the client ill while the ILL_CHANGING flag is set will be failed
21022 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
21023 	 * checks (under the ill_g_usesrc_lock) that the ire being added
21024 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
21025 	 * belong to the same usesrc group.
21026 	 */
21027 	mutex_enter(&usesrc_cli_ill->ill_lock);
21028 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
21029 	mutex_exit(&usesrc_cli_ill->ill_lock);
21030 	ill_flag_changed = B_TRUE;
21031 
21032 	if (ipif->ipif_isv6)
21033 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21034 		    ALL_ZONES);
21035 	else
21036 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21037 		    ALL_ZONES);
21038 
21039 	/*
21040 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
21041 	 * and the ill_usesrc_ifindex fields
21042 	 */
21043 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
21044 
21045 	if (reset_flg) {
21046 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
21047 		if (ret != 0) {
21048 			err = EINVAL;
21049 		}
21050 		rw_exit(&ill_g_usesrc_lock);
21051 		goto done;
21052 	}
21053 
21054 	/*
21055 	 * Four possibilities to consider:
21056 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
21057 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
21058 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
21059 	 * 4. Both are part of their respective usesrc groups
21060 	 */
21061 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
21062 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21063 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
21064 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21065 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21066 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
21067 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
21068 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21069 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21070 		/* Insert at head of list */
21071 		usesrc_cli_ill->ill_usesrc_grp_next =
21072 		    usesrc_ill->ill_usesrc_grp_next;
21073 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21074 	} else {
21075 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
21076 		    ifindex);
21077 		if (ret != 0)
21078 			err = EINVAL;
21079 	}
21080 	rw_exit(&ill_g_usesrc_lock);
21081 
21082 done:
21083 	if (ill_flag_changed) {
21084 		mutex_enter(&usesrc_cli_ill->ill_lock);
21085 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21086 		mutex_exit(&usesrc_cli_ill->ill_lock);
21087 	}
21088 	if (ipsq != NULL)
21089 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21090 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21091 	ill_refrele(usesrc_ill);
21092 	return (err);
21093 }
21094 
21095 /*
21096  * comparison function used by avl.
21097  */
21098 static int
21099 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21100 {
21101 
21102 	uint_t index;
21103 
21104 	ASSERT(phyip != NULL && index_ptr != NULL);
21105 
21106 	index = *((uint_t *)index_ptr);
21107 	/*
21108 	 * let the phyint with the lowest index be on top.
21109 	 */
21110 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21111 		return (1);
21112 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21113 		return (-1);
21114 	return (0);
21115 }
21116 
21117 /*
21118  * comparison function used by avl.
21119  */
21120 static int
21121 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21122 {
21123 	ill_t *ill;
21124 	int res = 0;
21125 
21126 	ASSERT(phyip != NULL && name_ptr != NULL);
21127 
21128 	if (((phyint_t *)phyip)->phyint_illv4)
21129 		ill = ((phyint_t *)phyip)->phyint_illv4;
21130 	else
21131 		ill = ((phyint_t *)phyip)->phyint_illv6;
21132 	ASSERT(ill != NULL);
21133 
21134 	res = strcmp(ill->ill_name, (char *)name_ptr);
21135 	if (res > 0)
21136 		return (1);
21137 	else if (res < 0)
21138 		return (-1);
21139 	return (0);
21140 }
21141 /*
21142  * This function is called from ill_delete when the ill is being
21143  * unplumbed. We remove the reference from the phyint and we also
21144  * free the phyint when there are no more references to it.
21145  */
21146 static void
21147 ill_phyint_free(ill_t *ill)
21148 {
21149 	phyint_t *phyi;
21150 	phyint_t *next_phyint;
21151 	ipsq_t *cur_ipsq;
21152 
21153 	ASSERT(ill->ill_phyint != NULL);
21154 
21155 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21156 	phyi = ill->ill_phyint;
21157 	ill->ill_phyint = NULL;
21158 	/*
21159 	 * ill_init allocates a phyint always to store the copy
21160 	 * of flags relevant to phyint. At that point in time, we could
21161 	 * not assign the name and hence phyint_illv4/v6 could not be
21162 	 * initialized. Later in ipif_set_values, we assign the name to
21163 	 * the ill, at which point in time we assign phyint_illv4/v6.
21164 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21165 	 */
21166 	if (ill->ill_flags & ILLF_IPV6) {
21167 		phyi->phyint_illv6 = NULL;
21168 	} else {
21169 		phyi->phyint_illv4 = NULL;
21170 	}
21171 	/*
21172 	 * ipif_down removes it from the group when the last ipif goes
21173 	 * down.
21174 	 */
21175 	ASSERT(ill->ill_group == NULL);
21176 
21177 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21178 		return;
21179 
21180 	/*
21181 	 * Make sure this phyint was put in the list.
21182 	 */
21183 	if (phyi->phyint_ifindex > 0) {
21184 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21185 		    phyi);
21186 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21187 		    phyi);
21188 	}
21189 	/*
21190 	 * remove phyint from the ipsq list.
21191 	 */
21192 	cur_ipsq = phyi->phyint_ipsq;
21193 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21194 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21195 	} else {
21196 		next_phyint = cur_ipsq->ipsq_phyint_list;
21197 		while (next_phyint != NULL) {
21198 			if (next_phyint->phyint_ipsq_next == phyi) {
21199 				next_phyint->phyint_ipsq_next =
21200 					phyi->phyint_ipsq_next;
21201 				break;
21202 			}
21203 			next_phyint = next_phyint->phyint_ipsq_next;
21204 		}
21205 		ASSERT(next_phyint != NULL);
21206 	}
21207 	IPSQ_DEC_REF(cur_ipsq);
21208 
21209 	if (phyi->phyint_groupname_len != 0) {
21210 		ASSERT(phyi->phyint_groupname != NULL);
21211 		mi_free(phyi->phyint_groupname);
21212 	}
21213 	mi_free(phyi);
21214 }
21215 
21216 /*
21217  * Attach the ill to the phyint structure which can be shared by both
21218  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21219  * function is called from ipif_set_values and ill_lookup_on_name (for
21220  * loopback) where we know the name of the ill. We lookup the ill and if
21221  * there is one present already with the name use that phyint. Otherwise
21222  * reuse the one allocated by ill_init.
21223  */
21224 static void
21225 ill_phyint_reinit(ill_t *ill)
21226 {
21227 	boolean_t isv6 = ill->ill_isv6;
21228 	phyint_t *phyi_old;
21229 	phyint_t *phyi;
21230 	avl_index_t where = 0;
21231 	ill_t	*ill_other = NULL;
21232 	ipsq_t	*ipsq;
21233 
21234 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21235 
21236 	phyi_old = ill->ill_phyint;
21237 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21238 	    phyi_old->phyint_illv6 == NULL));
21239 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21240 	    phyi_old->phyint_illv4 == NULL));
21241 	ASSERT(phyi_old->phyint_ifindex == 0);
21242 
21243 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21244 	    ill->ill_name, &where);
21245 
21246 	/*
21247 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21248 	 *    the global list of ills. So no other thread could have located
21249 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21250 	 * 2. Now locate the other protocol instance of this ill.
21251 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21252 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21253 	 *    of neither ill can change.
21254 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21255 	 *    other ill.
21256 	 * 5. Release all locks.
21257 	 */
21258 
21259 	/*
21260 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21261 	 * we are initializing IPv4.
21262 	 */
21263 	if (phyi != NULL) {
21264 		ill_other = (isv6) ? phyi->phyint_illv4 :
21265 		    phyi->phyint_illv6;
21266 		ASSERT(ill_other->ill_phyint != NULL);
21267 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21268 		    (!isv6 && ill_other->ill_isv6));
21269 		GRAB_ILL_LOCKS(ill, ill_other);
21270 		/*
21271 		 * We are potentially throwing away phyint_flags which
21272 		 * could be different from the one that we obtain from
21273 		 * ill_other->ill_phyint. But it is okay as we are assuming
21274 		 * that the state maintained within IP is correct.
21275 		 */
21276 		mutex_enter(&phyi->phyint_lock);
21277 		if (isv6) {
21278 			ASSERT(phyi->phyint_illv6 == NULL);
21279 			phyi->phyint_illv6 = ill;
21280 		} else {
21281 			ASSERT(phyi->phyint_illv4 == NULL);
21282 			phyi->phyint_illv4 = ill;
21283 		}
21284 		/*
21285 		 * This is a new ill, currently undergoing SLIFNAME
21286 		 * So we could not have joined an IPMP group until now.
21287 		 */
21288 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21289 		    phyi_old->phyint_groupname == NULL);
21290 
21291 		/*
21292 		 * This phyi_old is going away. Decref ipsq_refs and
21293 		 * assert it is zero. The ipsq itself will be freed in
21294 		 * ipsq_exit
21295 		 */
21296 		ipsq = phyi_old->phyint_ipsq;
21297 		IPSQ_DEC_REF(ipsq);
21298 		ASSERT(ipsq->ipsq_refs == 0);
21299 		/* Get the singleton phyint out of the ipsq list */
21300 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21301 		ipsq->ipsq_phyint_list = NULL;
21302 		phyi_old->phyint_illv4 = NULL;
21303 		phyi_old->phyint_illv6 = NULL;
21304 		mi_free(phyi_old);
21305 	} else {
21306 		mutex_enter(&ill->ill_lock);
21307 		/*
21308 		 * We don't need to acquire any lock, since
21309 		 * the ill is not yet visible globally  and we
21310 		 * have not yet released the ill_g_lock.
21311 		 */
21312 		phyi = phyi_old;
21313 		mutex_enter(&phyi->phyint_lock);
21314 		/* XXX We need a recovery strategy here. */
21315 		if (!phyint_assign_ifindex(phyi))
21316 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21317 
21318 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21319 		    (void *)phyi, where);
21320 
21321 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21322 		    &phyi->phyint_ifindex, &where);
21323 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21324 		    (void *)phyi, where);
21325 	}
21326 
21327 	/*
21328 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21329 	 * pending mp is not affected because that is per ill basis.
21330 	 */
21331 	ill->ill_phyint = phyi;
21332 
21333 	/*
21334 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21335 	 * We do this here as when the first ipif was allocated,
21336 	 * ipif_allocate does not know the right interface index.
21337 	 */
21338 
21339 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21340 	/*
21341 	 * Now that the phyint's ifindex has been assigned, complete the
21342 	 * remaining
21343 	 */
21344 	if (ill->ill_isv6) {
21345 		ill->ill_ip6_mib->ipv6IfIndex =
21346 		    ill->ill_phyint->phyint_ifindex;
21347 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21348 		    ill->ill_phyint->phyint_ifindex;
21349 	}
21350 
21351 	RELEASE_ILL_LOCKS(ill, ill_other);
21352 	mutex_exit(&phyi->phyint_lock);
21353 }
21354 
21355 /*
21356  * Notify any downstream modules of the name of this interface.
21357  * An M_IOCTL is used even though we don't expect a successful reply.
21358  * Any reply message from the driver (presumably an M_IOCNAK) will
21359  * eventually get discarded somewhere upstream.  The message format is
21360  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21361  * to IP.
21362  */
21363 static void
21364 ip_ifname_notify(ill_t *ill, queue_t *q)
21365 {
21366 	mblk_t *mp1, *mp2;
21367 	struct iocblk *iocp;
21368 	struct lifreq *lifr;
21369 
21370 	mp1 = mkiocb(SIOCSLIFNAME);
21371 	if (mp1 == NULL)
21372 		return;
21373 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21374 	if (mp2 == NULL) {
21375 		freeb(mp1);
21376 		return;
21377 	}
21378 
21379 	mp1->b_cont = mp2;
21380 	iocp = (struct iocblk *)mp1->b_rptr;
21381 	iocp->ioc_count = sizeof (struct lifreq);
21382 
21383 	lifr = (struct lifreq *)mp2->b_rptr;
21384 	mp2->b_wptr += sizeof (struct lifreq);
21385 	bzero(lifr, sizeof (struct lifreq));
21386 
21387 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21388 	lifr->lifr_ppa = ill->ill_ppa;
21389 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21390 
21391 	putnext(q, mp1);
21392 }
21393 
21394 static boolean_t ip_trash_timer_started = B_FALSE;
21395 
21396 static int
21397 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21398 {
21399 	int err;
21400 
21401 	/* Set the obsolete NDD per-interface forwarding name. */
21402 	err = ill_set_ndd_name(ill);
21403 	if (err != 0) {
21404 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21405 		    err);
21406 	}
21407 
21408 	/* Tell downstream modules where they are. */
21409 	ip_ifname_notify(ill, q);
21410 
21411 	/*
21412 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21413 	 * Error cases are ENOMEM ...
21414 	 */
21415 	err = ill_dl_phys(ill, ipif, mp, q);
21416 
21417 	/*
21418 	 * If there is no IRE expiration timer running, get one started.
21419 	 * igmp and mld timers will be triggered by the first multicast
21420 	 */
21421 	if (!ip_trash_timer_started) {
21422 		/*
21423 		 * acquire the lock and check again.
21424 		 */
21425 		mutex_enter(&ip_trash_timer_lock);
21426 		if (!ip_trash_timer_started) {
21427 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21428 			    MSEC_TO_TICK(ip_timer_interval));
21429 			ip_trash_timer_started = B_TRUE;
21430 		}
21431 		mutex_exit(&ip_trash_timer_lock);
21432 	}
21433 
21434 	if (ill->ill_isv6) {
21435 		mutex_enter(&mld_slowtimeout_lock);
21436 		if (mld_slowtimeout_id == 0) {
21437 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21438 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21439 		}
21440 		mutex_exit(&mld_slowtimeout_lock);
21441 	} else {
21442 		mutex_enter(&igmp_slowtimeout_lock);
21443 		if (igmp_slowtimeout_id == 0) {
21444 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21445 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21446 		}
21447 		mutex_exit(&igmp_slowtimeout_lock);
21448 	}
21449 
21450 	return (err);
21451 }
21452 
21453 /*
21454  * Common routine for ppa and ifname setting. Should be called exclusive.
21455  *
21456  * Returns EINPROGRESS when mp has been consumed by queueing it on
21457  * ill_pending_mp and the ioctl will complete in ip_rput.
21458  *
21459  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21460  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21461  * For SLIFNAME, we pass these values back to the userland.
21462  */
21463 static int
21464 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21465 {
21466 	ill_t	*ill;
21467 	ipif_t	*ipif;
21468 	ipsq_t	*ipsq;
21469 	char	*ppa_ptr;
21470 	char	*old_ptr;
21471 	char	old_char;
21472 	int	error;
21473 
21474 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21475 	ASSERT(q->q_next != NULL);
21476 	ASSERT(interf_name != NULL);
21477 
21478 	ill = (ill_t *)q->q_ptr;
21479 
21480 	ASSERT(ill->ill_name[0] == '\0');
21481 	ASSERT(IAM_WRITER_ILL(ill));
21482 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21483 	ASSERT(ill->ill_ppa == UINT_MAX);
21484 
21485 	/* The ppa is sent down by ifconfig or is chosen */
21486 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21487 		return (EINVAL);
21488 	}
21489 
21490 	/*
21491 	 * make sure ppa passed in is same as ppa in the name.
21492 	 * This check is not made when ppa == UINT_MAX in that case ppa
21493 	 * in the name could be anything. System will choose a ppa and
21494 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21495 	 */
21496 	if (*new_ppa_ptr != UINT_MAX) {
21497 		/* stoi changes the pointer */
21498 		old_ptr = ppa_ptr;
21499 		/*
21500 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21501 		 * (they don't have an externally visible ppa).  We assign one
21502 		 * here so that we can manage the interface.  Note that in
21503 		 * the past this value was always 0 for DLPI 1 drivers.
21504 		 */
21505 		if (*new_ppa_ptr == 0)
21506 			*new_ppa_ptr = stoi(&old_ptr);
21507 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21508 			return (EINVAL);
21509 	}
21510 	/*
21511 	 * terminate string before ppa
21512 	 * save char at that location.
21513 	 */
21514 	old_char = ppa_ptr[0];
21515 	ppa_ptr[0] = '\0';
21516 
21517 	ill->ill_ppa = *new_ppa_ptr;
21518 	/*
21519 	 * Finish as much work now as possible before calling ill_glist_insert
21520 	 * which makes the ill globally visible and also merges it with the
21521 	 * other protocol instance of this phyint. The remaining work is
21522 	 * done after entering the ipsq which may happen sometime later.
21523 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21524 	 */
21525 	ipif = ill->ill_ipif;
21526 
21527 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21528 	ipif_assign_seqid(ipif);
21529 
21530 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21531 		ill->ill_flags |= ILLF_IPV4;
21532 
21533 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21534 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21535 
21536 	if (ill->ill_flags & ILLF_IPV6) {
21537 
21538 		ill->ill_isv6 = B_TRUE;
21539 		if (ill->ill_rq != NULL) {
21540 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21541 			ill->ill_wq->q_qinfo = &winit_ipv6;
21542 		}
21543 
21544 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21545 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21546 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21547 		ipif->ipif_v6subnet = ipv6_all_zeros;
21548 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21549 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21550 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21551 		/*
21552 		 * point-to-point or Non-mulicast capable
21553 		 * interfaces won't do NUD unless explicitly
21554 		 * configured to do so.
21555 		 */
21556 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21557 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21558 			ill->ill_flags |= ILLF_NONUD;
21559 		}
21560 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21561 		if (ill->ill_flags & ILLF_NOARP) {
21562 			/*
21563 			 * Note: xresolv interfaces will eventually need
21564 			 * NOARP set here as well, but that will require
21565 			 * those external resolvers to have some
21566 			 * knowledge of that flag and act appropriately.
21567 			 * Not to be changed at present.
21568 			 */
21569 			ill->ill_flags &= ~ILLF_NOARP;
21570 		}
21571 		/*
21572 		 * Set the ILLF_ROUTER flag according to the global
21573 		 * IPv6 forwarding policy.
21574 		 */
21575 		if (ipv6_forward != 0)
21576 			ill->ill_flags |= ILLF_ROUTER;
21577 	} else if (ill->ill_flags & ILLF_IPV4) {
21578 		ill->ill_isv6 = B_FALSE;
21579 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21580 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21581 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21582 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21583 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21584 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21585 		/*
21586 		 * Set the ILLF_ROUTER flag according to the global
21587 		 * IPv4 forwarding policy.
21588 		 */
21589 		if (ip_g_forward != 0)
21590 			ill->ill_flags |= ILLF_ROUTER;
21591 	}
21592 
21593 	ASSERT(ill->ill_phyint != NULL);
21594 
21595 	/*
21596 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21597 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21598 	 */
21599 	if (ill->ill_isv6) {
21600 		/* allocate v6 mib */
21601 		if (!ill_allocate_mibs(ill))
21602 			return (ENOMEM);
21603 	}
21604 
21605 	/*
21606 	 * Pick a default sap until we get the DL_INFO_ACK back from
21607 	 * the driver.
21608 	 */
21609 	if (ill->ill_sap == 0) {
21610 		if (ill->ill_isv6)
21611 			ill->ill_sap  = IP6_DL_SAP;
21612 		else
21613 			ill->ill_sap  = IP_DL_SAP;
21614 	}
21615 
21616 	ill->ill_ifname_pending = 1;
21617 	ill->ill_ifname_pending_err = 0;
21618 
21619 	ill_refhold(ill);
21620 	rw_enter(&ill_g_lock, RW_WRITER);
21621 	if ((error = ill_glist_insert(ill, interf_name,
21622 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21623 		ill->ill_ppa = UINT_MAX;
21624 		ill->ill_name[0] = '\0';
21625 		/*
21626 		 * undo null termination done above.
21627 		 */
21628 		ppa_ptr[0] = old_char;
21629 		rw_exit(&ill_g_lock);
21630 		ill_refrele(ill);
21631 		return (error);
21632 	}
21633 
21634 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21635 
21636 	/*
21637 	 * When we return the buffer pointed to by interf_name should contain
21638 	 * the same name as in ill_name.
21639 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21640 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21641 	 * so copy full name and update the ppa ptr.
21642 	 * When ppa passed in != UINT_MAX all values are correct just undo
21643 	 * null termination, this saves a bcopy.
21644 	 */
21645 	if (*new_ppa_ptr == UINT_MAX) {
21646 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21647 		*new_ppa_ptr = ill->ill_ppa;
21648 	} else {
21649 		/*
21650 		 * undo null termination done above.
21651 		 */
21652 		ppa_ptr[0] = old_char;
21653 	}
21654 
21655 	/* Let SCTP know about this ILL */
21656 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21657 
21658 	/* and also about the first ipif */
21659 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21660 
21661 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21662 	    B_TRUE);
21663 
21664 	rw_exit(&ill_g_lock);
21665 	ill_refrele(ill);
21666 	if (ipsq == NULL)
21667 		return (EINPROGRESS);
21668 
21669 	/*
21670 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21671 	 * due to the phyint merge in ill_phyint_reinit.
21672 	 */
21673 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21674 		ipsq->ipsq_current_ipif == ipif);
21675 	ipsq->ipsq_current_ipif = ipif;
21676 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21677 	error = ipif_set_values_tail(ill, ipif, mp, q);
21678 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21679 	if (error != 0 && error != EINPROGRESS) {
21680 		/*
21681 		 * restore previous values
21682 		 */
21683 		ill->ill_isv6 = B_FALSE;
21684 	}
21685 	return (error);
21686 }
21687 
21688 
21689 extern void (*ip_cleanup_func)(void);
21690 
21691 void
21692 ipif_init(void)
21693 {
21694 	hrtime_t hrt;
21695 	int i;
21696 
21697 	/*
21698 	 * Can't call drv_getparm here as it is too early in the boot.
21699 	 * As we use ipif_src_random just for picking a different
21700 	 * source address everytime, this need not be really random.
21701 	 */
21702 	hrt = gethrtime();
21703 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
21704 
21705 	for (i = 0; i < MAX_G_HEADS; i++) {
21706 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
21707 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
21708 	}
21709 
21710 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
21711 	    ill_phyint_compare_index,
21712 	    sizeof (phyint_t),
21713 	    offsetof(struct phyint, phyint_avl_by_index));
21714 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
21715 	    ill_phyint_compare_name,
21716 	    sizeof (phyint_t),
21717 	    offsetof(struct phyint, phyint_avl_by_name));
21718 
21719 	ip_cleanup_func = ip_thread_exit;
21720 }
21721 
21722 /*
21723  * This is called by ip_rt_add when src_addr value is other than zero.
21724  * src_addr signifies the source address of the incoming packet. For
21725  * reverse tunnel route we need to create a source addr based routing
21726  * table. This routine creates ip_mrtun_table if it's empty and then
21727  * it adds the route entry hashed by source address. It verifies that
21728  * the outgoing interface is always a non-resolver interface (tunnel).
21729  */
21730 int
21731 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
21732     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
21733 {
21734 	ire_t   *ire;
21735 	ire_t	*save_ire;
21736 	ipif_t  *ipif;
21737 	ill_t   *in_ill = NULL;
21738 	ill_t	*out_ill;
21739 	queue_t	*stq;
21740 	mblk_t	*dlureq_mp;
21741 	int	error;
21742 
21743 	if (ire_arg != NULL)
21744 		*ire_arg = NULL;
21745 	ASSERT(in_src_addr != INADDR_ANY);
21746 
21747 	ipif = ipif_arg;
21748 	if (ipif != NULL) {
21749 		out_ill = ipif->ipif_ill;
21750 	} else {
21751 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
21752 		return (EINVAL);
21753 	}
21754 
21755 	if (src_ipif == NULL) {
21756 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
21757 		return (EINVAL);
21758 	}
21759 	in_ill = src_ipif->ipif_ill;
21760 
21761 	/*
21762 	 * Check for duplicates. We don't need to
21763 	 * match out_ill, because the uniqueness of
21764 	 * a route is only dependent on src_addr and
21765 	 * in_ill.
21766 	 */
21767 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
21768 	if (ire != NULL) {
21769 		ire_refrele(ire);
21770 		return (EEXIST);
21771 	}
21772 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
21773 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
21774 		    ipif->ipif_net_type));
21775 		return (EINVAL);
21776 	}
21777 
21778 	stq = ipif->ipif_wq;
21779 	ASSERT(stq != NULL);
21780 
21781 	/*
21782 	 * The outgoing interface must be non-resolver
21783 	 * interface.
21784 	 */
21785 	dlureq_mp = ill_dlur_gen(NULL,
21786 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
21787 	    out_ill->ill_sap_length);
21788 
21789 	if (dlureq_mp == NULL) {
21790 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
21791 		return (ENOMEM);
21792 	}
21793 
21794 	/* Create the IRE. */
21795 
21796 	ire = ire_create(
21797 	    NULL,				/* Zero dst addr */
21798 	    NULL,				/* Zero mask */
21799 	    NULL,				/* Zero gateway addr */
21800 	    NULL,				/* Zero ipif_src addr */
21801 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
21802 	    &ipif->ipif_mtu,
21803 	    NULL,
21804 	    NULL,				/* rfq */
21805 	    stq,
21806 	    IRE_MIPRTUN,
21807 	    dlureq_mp,
21808 	    ipif,
21809 	    in_ill,
21810 	    0,
21811 	    0,
21812 	    0,
21813 	    flags,
21814 	    &ire_uinfo_null);
21815 
21816 	if (ire == NULL)
21817 		return (ENOMEM);
21818 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
21819 	    ire->ire_type));
21820 	save_ire = ire;
21821 	ASSERT(save_ire != NULL);
21822 	error = ire_add_mrtun(&ire, q, mp, func);
21823 	/*
21824 	 * If ire_add_mrtun() failed, the ire passed in was freed
21825 	 * so there is no need to do so here.
21826 	 */
21827 	if (error != 0) {
21828 		return (error);
21829 	}
21830 
21831 	/* Duplicate check */
21832 	if (ire != save_ire) {
21833 		/* route already exists by now */
21834 		ire_refrele(ire);
21835 		return (EEXIST);
21836 	}
21837 
21838 	if (ire_arg != NULL) {
21839 		/*
21840 		 * Store the ire that was just added. the caller
21841 		 * ip_rts_request responsible for doing ire_refrele()
21842 		 * on it.
21843 		 */
21844 		*ire_arg = ire;
21845 	} else {
21846 		ire_refrele(ire);	/* held in ire_add_mrtun */
21847 	}
21848 
21849 	return (0);
21850 }
21851 
21852 /*
21853  * It is called by ip_rt_delete() only when mipagent requests to delete
21854  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
21855  */
21856 
21857 int
21858 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
21859 {
21860 	ire_t   *ire = NULL;
21861 
21862 	if (in_src_addr == INADDR_ANY)
21863 		return (EINVAL);
21864 	if (src_ipif == NULL)
21865 		return (EINVAL);
21866 
21867 	/* search if this route exists in the ip_mrtun_table */
21868 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
21869 	if (ire == NULL) {
21870 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
21871 		return (ESRCH);
21872 	}
21873 	ire_delete(ire);
21874 	ire_refrele(ire);
21875 	return (0);
21876 }
21877 
21878 /*
21879  * Lookup the ipif corresponding to the onlink destination address. For
21880  * point-to-point interfaces, it matches with remote endpoint destination
21881  * address. For point-to-multipoint interfaces it only tries to match the
21882  * destination with the interface's subnet address. The longest, most specific
21883  * match is found to take care of such rare network configurations like -
21884  * le0: 129.146.1.1/16
21885  * le1: 129.146.2.2/24
21886  * It is used only by SO_DONTROUTE at the moment.
21887  */
21888 ipif_t *
21889 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
21890 {
21891 	ipif_t	*ipif, *best_ipif;
21892 	ill_t	*ill;
21893 	ill_walk_context_t ctx;
21894 
21895 	ASSERT(zoneid != ALL_ZONES);
21896 	best_ipif = NULL;
21897 
21898 	rw_enter(&ill_g_lock, RW_READER);
21899 	ill = ILL_START_WALK_V4(&ctx);
21900 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21901 		mutex_enter(&ill->ill_lock);
21902 		for (ipif = ill->ill_ipif; ipif != NULL;
21903 		    ipif = ipif->ipif_next) {
21904 			if (!IPIF_CAN_LOOKUP(ipif))
21905 				continue;
21906 			if (ipif->ipif_zoneid != zoneid)
21907 				continue;
21908 			/*
21909 			 * Point-to-point case. Look for exact match with
21910 			 * destination address.
21911 			 */
21912 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
21913 				if (ipif->ipif_pp_dst_addr == addr) {
21914 					ipif_refhold_locked(ipif);
21915 					mutex_exit(&ill->ill_lock);
21916 					rw_exit(&ill_g_lock);
21917 					if (best_ipif != NULL)
21918 						ipif_refrele(best_ipif);
21919 					return (ipif);
21920 				}
21921 			} else if (ipif->ipif_subnet == (addr &
21922 			    ipif->ipif_net_mask)) {
21923 				/*
21924 				 * Point-to-multipoint case. Looping through to
21925 				 * find the most specific match. If there are
21926 				 * multiple best match ipif's then prefer ipif's
21927 				 * that are UP. If there is only one best match
21928 				 * ipif and it is DOWN we must still return it.
21929 				 */
21930 				if ((best_ipif == NULL) ||
21931 				    (ipif->ipif_net_mask >
21932 				    best_ipif->ipif_net_mask) ||
21933 				    ((ipif->ipif_net_mask ==
21934 				    best_ipif->ipif_net_mask) &&
21935 				    ((ipif->ipif_flags & IPIF_UP) &&
21936 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
21937 					ipif_refhold_locked(ipif);
21938 					mutex_exit(&ill->ill_lock);
21939 					rw_exit(&ill_g_lock);
21940 					if (best_ipif != NULL)
21941 						ipif_refrele(best_ipif);
21942 					best_ipif = ipif;
21943 					rw_enter(&ill_g_lock, RW_READER);
21944 					mutex_enter(&ill->ill_lock);
21945 				}
21946 			}
21947 		}
21948 		mutex_exit(&ill->ill_lock);
21949 	}
21950 	rw_exit(&ill_g_lock);
21951 	return (best_ipif);
21952 }
21953 
21954 
21955 /*
21956  * Save enough information so that we can recreate the IRE if
21957  * the interface goes down and then up.
21958  */
21959 static void
21960 ipif_save_ire(ipif_t *ipif, ire_t *ire)
21961 {
21962 	mblk_t	*save_mp;
21963 
21964 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
21965 	if (save_mp != NULL) {
21966 		ifrt_t	*ifrt;
21967 
21968 		save_mp->b_wptr += sizeof (ifrt_t);
21969 		ifrt = (ifrt_t *)save_mp->b_rptr;
21970 		bzero(ifrt, sizeof (ifrt_t));
21971 		ifrt->ifrt_type = ire->ire_type;
21972 		ifrt->ifrt_addr = ire->ire_addr;
21973 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
21974 		ifrt->ifrt_src_addr = ire->ire_src_addr;
21975 		ifrt->ifrt_mask = ire->ire_mask;
21976 		ifrt->ifrt_flags = ire->ire_flags;
21977 		ifrt->ifrt_max_frag = ire->ire_max_frag;
21978 		mutex_enter(&ipif->ipif_saved_ire_lock);
21979 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
21980 		ipif->ipif_saved_ire_mp = save_mp;
21981 		ipif->ipif_saved_ire_cnt++;
21982 		mutex_exit(&ipif->ipif_saved_ire_lock);
21983 	}
21984 }
21985 
21986 
21987 static void
21988 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
21989 {
21990 	mblk_t	**mpp;
21991 	mblk_t	*mp;
21992 	ifrt_t	*ifrt;
21993 
21994 	/* Remove from ipif_saved_ire_mp list if it is there */
21995 	mutex_enter(&ipif->ipif_saved_ire_lock);
21996 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
21997 	    mpp = &(*mpp)->b_cont) {
21998 		/*
21999 		 * On a given ipif, the triple of address, gateway and
22000 		 * mask is unique for each saved IRE (in the case of
22001 		 * ordinary interface routes, the gateway address is
22002 		 * all-zeroes).
22003 		 */
22004 		mp = *mpp;
22005 		ifrt = (ifrt_t *)mp->b_rptr;
22006 		if (ifrt->ifrt_addr == ire->ire_addr &&
22007 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
22008 		    ifrt->ifrt_mask == ire->ire_mask) {
22009 			*mpp = mp->b_cont;
22010 			ipif->ipif_saved_ire_cnt--;
22011 			freeb(mp);
22012 			break;
22013 		}
22014 	}
22015 	mutex_exit(&ipif->ipif_saved_ire_lock);
22016 }
22017 
22018 
22019 /*
22020  * IP multirouting broadcast routes handling
22021  * Append CGTP broadcast IREs to regular ones created
22022  * at ifconfig time.
22023  */
22024 static void
22025 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
22026 {
22027 	ire_t *ire_prim;
22028 
22029 	ASSERT(ire != NULL);
22030 	ASSERT(ire_dst != NULL);
22031 
22032 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22033 	    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22034 	if (ire_prim != NULL) {
22035 		/*
22036 		 * We are in the special case of broadcasts for
22037 		 * CGTP. We add an IRE_BROADCAST that holds
22038 		 * the RTF_MULTIRT flag, the destination
22039 		 * address of ire_dst and the low level
22040 		 * info of ire_prim. In other words, CGTP
22041 		 * broadcast is added to the redundant ipif.
22042 		 */
22043 		ipif_t *ipif_prim;
22044 		ire_t  *bcast_ire;
22045 
22046 		ipif_prim = ire_prim->ire_ipif;
22047 
22048 		ip2dbg(("ip_cgtp_filter_bcast_add: "
22049 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22050 		    (void *)ire_dst, (void *)ire_prim,
22051 		    (void *)ipif_prim));
22052 
22053 		bcast_ire = ire_create(
22054 		    (uchar_t *)&ire->ire_addr,
22055 		    (uchar_t *)&ip_g_all_ones,
22056 		    (uchar_t *)&ire_dst->ire_src_addr,
22057 		    (uchar_t *)&ire->ire_gateway_addr,
22058 		    NULL,
22059 		    &ipif_prim->ipif_mtu,
22060 		    NULL,
22061 		    ipif_prim->ipif_rq,
22062 		    ipif_prim->ipif_wq,
22063 		    IRE_BROADCAST,
22064 		    ipif_prim->ipif_bcast_mp,
22065 		    ipif_prim,
22066 		    NULL,
22067 		    0,
22068 		    0,
22069 		    0,
22070 		    ire->ire_flags,
22071 		    &ire_uinfo_null);
22072 
22073 		if (bcast_ire != NULL) {
22074 
22075 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
22076 				ip2dbg(("ip_cgtp_filter_bcast_add: "
22077 				    "added bcast_ire %p\n",
22078 				    (void *)bcast_ire));
22079 
22080 				ipif_save_ire(bcast_ire->ire_ipif,
22081 				    bcast_ire);
22082 				ire_refrele(bcast_ire);
22083 			}
22084 		}
22085 		ire_refrele(ire_prim);
22086 	}
22087 }
22088 
22089 
22090 /*
22091  * IP multirouting broadcast routes handling
22092  * Remove the broadcast ire
22093  */
22094 static void
22095 ip_cgtp_bcast_delete(ire_t *ire)
22096 {
22097 	ire_t *ire_dst;
22098 
22099 	ASSERT(ire != NULL);
22100 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22101 	    NULL, NULL, MATCH_IRE_TYPE);
22102 	if (ire_dst != NULL) {
22103 		ire_t *ire_prim;
22104 
22105 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22106 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22107 		if (ire_prim != NULL) {
22108 			ipif_t *ipif_prim;
22109 			ire_t  *bcast_ire;
22110 
22111 			ipif_prim = ire_prim->ire_ipif;
22112 
22113 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22114 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22115 			    (void *)ire_dst, (void *)ire_prim,
22116 			    (void *)ipif_prim));
22117 
22118 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22119 			    ire->ire_gateway_addr,
22120 			    IRE_BROADCAST,
22121 			    ipif_prim,
22122 			    NULL,
22123 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22124 			    MATCH_IRE_MASK);
22125 
22126 			if (bcast_ire != NULL) {
22127 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22128 				    "looked up bcast_ire %p\n",
22129 				    (void *)bcast_ire));
22130 				ipif_remove_ire(bcast_ire->ire_ipif,
22131 					bcast_ire);
22132 				ire_delete(bcast_ire);
22133 			}
22134 			ire_refrele(ire_prim);
22135 		}
22136 		ire_refrele(ire_dst);
22137 	}
22138 }
22139 
22140 /*
22141  * IPsec hardware acceleration capabilities related functions.
22142  */
22143 
22144 /*
22145  * Free a per-ill IPsec capabilities structure.
22146  */
22147 static void
22148 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22149 {
22150 	if (capab->auth_hw_algs != NULL)
22151 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22152 	if (capab->encr_hw_algs != NULL)
22153 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22154 	if (capab->encr_algparm != NULL)
22155 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22156 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22157 }
22158 
22159 /*
22160  * Allocate a new per-ill IPsec capabilities structure. This structure
22161  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22162  * an array which specifies, for each algorithm, whether this algorithm
22163  * is supported by the ill or not.
22164  */
22165 static ill_ipsec_capab_t *
22166 ill_ipsec_capab_alloc(void)
22167 {
22168 	ill_ipsec_capab_t *capab;
22169 	uint_t nelems;
22170 
22171 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22172 	if (capab == NULL)
22173 		return (NULL);
22174 
22175 	/* we need one bit per algorithm */
22176 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22177 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22178 
22179 	/* allocate memory to store algorithm flags */
22180 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22181 	if (capab->encr_hw_algs == NULL)
22182 		goto nomem;
22183 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22184 	if (capab->auth_hw_algs == NULL)
22185 		goto nomem;
22186 	/*
22187 	 * Leave encr_algparm NULL for now since we won't need it half
22188 	 * the time
22189 	 */
22190 	return (capab);
22191 
22192 nomem:
22193 	ill_ipsec_capab_free(capab);
22194 	return (NULL);
22195 }
22196 
22197 /*
22198  * Resize capability array.  Since we're exclusive, this is OK.
22199  */
22200 static boolean_t
22201 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22202 {
22203 	ipsec_capab_algparm_t *nalp, *oalp;
22204 	uint32_t olen, nlen;
22205 
22206 	oalp = capab->encr_algparm;
22207 	olen = capab->encr_algparm_size;
22208 
22209 	if (oalp != NULL) {
22210 		if (algid < capab->encr_algparm_end)
22211 			return (B_TRUE);
22212 	}
22213 
22214 	nlen = (algid + 1) * sizeof (*nalp);
22215 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22216 	if (nalp == NULL)
22217 		return (B_FALSE);
22218 
22219 	if (oalp != NULL) {
22220 		bcopy(oalp, nalp, olen);
22221 		kmem_free(oalp, olen);
22222 	}
22223 	capab->encr_algparm = nalp;
22224 	capab->encr_algparm_size = nlen;
22225 	capab->encr_algparm_end = algid + 1;
22226 
22227 	return (B_TRUE);
22228 }
22229 
22230 /*
22231  * Compare the capabilities of the specified ill with the protocol
22232  * and algorithms specified by the SA passed as argument.
22233  * If they match, returns B_TRUE, B_FALSE if they do not match.
22234  *
22235  * The ill can be passed as a pointer to it, or by specifying its index
22236  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22237  *
22238  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22239  * packet is eligible for hardware acceleration, and by
22240  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22241  * to a particular ill.
22242  */
22243 boolean_t
22244 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22245     ipsa_t *sa)
22246 {
22247 	boolean_t sa_isv6;
22248 	uint_t algid;
22249 	struct ill_ipsec_capab_s *cpp;
22250 	boolean_t need_refrele = B_FALSE;
22251 
22252 	if (ill == NULL) {
22253 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22254 		    NULL, NULL, NULL);
22255 		if (ill == NULL) {
22256 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22257 			return (B_FALSE);
22258 		}
22259 		need_refrele = B_TRUE;
22260 	}
22261 
22262 	/*
22263 	 * Use the address length specified by the SA to determine
22264 	 * if it corresponds to a IPv6 address, and fail the matching
22265 	 * if the isv6 flag passed as argument does not match.
22266 	 * Note: this check is used for SADB capability checking before
22267 	 * sending SA information to an ill.
22268 	 */
22269 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22270 	if (sa_isv6 != ill_isv6)
22271 		/* protocol mismatch */
22272 		goto done;
22273 
22274 	/*
22275 	 * Check if the ill supports the protocol, algorithm(s) and
22276 	 * key size(s) specified by the SA, and get the pointers to
22277 	 * the algorithms supported by the ill.
22278 	 */
22279 	switch (sa->ipsa_type) {
22280 
22281 	case SADB_SATYPE_ESP:
22282 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22283 			/* ill does not support ESP acceleration */
22284 			goto done;
22285 		cpp = ill->ill_ipsec_capab_esp;
22286 		algid = sa->ipsa_auth_alg;
22287 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22288 			goto done;
22289 		algid = sa->ipsa_encr_alg;
22290 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22291 			goto done;
22292 		if (algid < cpp->encr_algparm_end) {
22293 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22294 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22295 				goto done;
22296 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22297 				goto done;
22298 		}
22299 		break;
22300 
22301 	case SADB_SATYPE_AH:
22302 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22303 			/* ill does not support AH acceleration */
22304 			goto done;
22305 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22306 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22307 			goto done;
22308 		break;
22309 	}
22310 
22311 	if (need_refrele)
22312 		ill_refrele(ill);
22313 	return (B_TRUE);
22314 done:
22315 	if (need_refrele)
22316 		ill_refrele(ill);
22317 	return (B_FALSE);
22318 }
22319 
22320 
22321 /*
22322  * Add a new ill to the list of IPsec capable ills.
22323  * Called from ill_capability_ipsec_ack() when an ACK was received
22324  * indicating that IPsec hardware processing was enabled for an ill.
22325  *
22326  * ill must point to the ill for which acceleration was enabled.
22327  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22328  */
22329 static void
22330 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22331 {
22332 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22333 	uint_t sa_type;
22334 	uint_t ipproto;
22335 
22336 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22337 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22338 
22339 	switch (dl_cap) {
22340 	case DL_CAPAB_IPSEC_AH:
22341 		sa_type = SADB_SATYPE_AH;
22342 		ills = &ipsec_capab_ills_ah;
22343 		ipproto = IPPROTO_AH;
22344 		break;
22345 	case DL_CAPAB_IPSEC_ESP:
22346 		sa_type = SADB_SATYPE_ESP;
22347 		ills = &ipsec_capab_ills_esp;
22348 		ipproto = IPPROTO_ESP;
22349 		break;
22350 	}
22351 
22352 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22353 
22354 	/*
22355 	 * Add ill index to list of hardware accelerators. If
22356 	 * already in list, do nothing.
22357 	 */
22358 	for (cur_ill = *ills; cur_ill != NULL &&
22359 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22360 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22361 		;
22362 
22363 	if (cur_ill == NULL) {
22364 		/* if this is a new entry for this ill */
22365 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22366 		if (new_ill == NULL) {
22367 			rw_exit(&ipsec_capab_ills_lock);
22368 			return;
22369 		}
22370 
22371 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22372 		new_ill->ill_isv6 = ill->ill_isv6;
22373 		new_ill->next = *ills;
22374 		*ills = new_ill;
22375 	} else if (!sadb_resync) {
22376 		/* not resync'ing SADB and an entry exists for this ill */
22377 		rw_exit(&ipsec_capab_ills_lock);
22378 		return;
22379 	}
22380 
22381 	rw_exit(&ipsec_capab_ills_lock);
22382 
22383 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22384 		/*
22385 		 * IPsec module for protocol loaded, initiate dump
22386 		 * of the SADB to this ill.
22387 		 */
22388 		sadb_ill_download(ill, sa_type);
22389 }
22390 
22391 /*
22392  * Remove an ill from the list of IPsec capable ills.
22393  */
22394 static void
22395 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22396 {
22397 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22398 
22399 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22400 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22401 
22402 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22403 	    &ipsec_capab_ills_esp;
22404 
22405 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22406 
22407 	prev_ill = NULL;
22408 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22409 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22410 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22411 		;
22412 	if (cur_ill == NULL) {
22413 		/* entry not found */
22414 		rw_exit(&ipsec_capab_ills_lock);
22415 		return;
22416 	}
22417 	if (prev_ill == NULL) {
22418 		/* entry at front of list */
22419 		*ills = NULL;
22420 	} else {
22421 		prev_ill->next = cur_ill->next;
22422 	}
22423 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22424 	rw_exit(&ipsec_capab_ills_lock);
22425 }
22426 
22427 
22428 /*
22429  * Handling of DL_CONTROL_REQ messages that must be sent down to
22430  * an ill while having exclusive access.
22431  */
22432 /* ARGSUSED */
22433 static void
22434 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22435 {
22436 	ill_t *ill = (ill_t *)q->q_ptr;
22437 
22438 	ill_dlpi_send(ill, mp);
22439 }
22440 
22441 
22442 /*
22443  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22444  * supporting the specified IPsec protocol acceleration.
22445  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22446  * We free the mblk and, if sa is non-null, release the held referece.
22447  */
22448 void
22449 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22450 {
22451 	ipsec_capab_ill_t *ici, *cur_ici;
22452 	ill_t *ill;
22453 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22454 
22455 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22456 	    ipsec_capab_ills_esp;
22457 
22458 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22459 
22460 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22461 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22462 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22463 
22464 		/*
22465 		 * Handle the case where the ill goes away while the SADB is
22466 		 * attempting to send messages.  If it's going away, it's
22467 		 * nuking its shadow SADB, so we don't care..
22468 		 */
22469 
22470 		if (ill == NULL)
22471 			continue;
22472 
22473 		if (sa != NULL) {
22474 			/*
22475 			 * Make sure capabilities match before
22476 			 * sending SA to ill.
22477 			 */
22478 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22479 			    cur_ici->ill_isv6, sa)) {
22480 				ill_refrele(ill);
22481 				continue;
22482 			}
22483 
22484 			mutex_enter(&sa->ipsa_lock);
22485 			sa->ipsa_flags |= IPSA_F_HW;
22486 			mutex_exit(&sa->ipsa_lock);
22487 		}
22488 
22489 		/*
22490 		 * Copy template message, and add it to the front
22491 		 * of the mblk ship list. We want to avoid holding
22492 		 * the ipsec_capab_ills_lock while sending the
22493 		 * message to the ills.
22494 		 *
22495 		 * The b_next and b_prev are temporarily used
22496 		 * to build a list of mblks to be sent down, and to
22497 		 * save the ill to which they must be sent.
22498 		 */
22499 		nmp = copymsg(mp);
22500 		if (nmp == NULL) {
22501 			ill_refrele(ill);
22502 			continue;
22503 		}
22504 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22505 		nmp->b_next = mp_ship_list;
22506 		mp_ship_list = nmp;
22507 		nmp->b_prev = (mblk_t *)ill;
22508 	}
22509 
22510 	rw_exit(&ipsec_capab_ills_lock);
22511 
22512 	nmp = mp_ship_list;
22513 	while (nmp != NULL) {
22514 		/* restore the mblk to a sane state */
22515 		next_mp = nmp->b_next;
22516 		nmp->b_next = NULL;
22517 		ill = (ill_t *)nmp->b_prev;
22518 		nmp->b_prev = NULL;
22519 
22520 		/*
22521 		 * Ship the mblk to the ill, must be exclusive. Keep the
22522 		 * reference to the ill as qwriter_ip() does a ill_referele().
22523 		 */
22524 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22525 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22526 
22527 		nmp = next_mp;
22528 	}
22529 
22530 	if (sa != NULL)
22531 		IPSA_REFRELE(sa);
22532 	freemsg(mp);
22533 }
22534 
22535 
22536 /*
22537  * Derive an interface id from the link layer address.
22538  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22539  */
22540 static boolean_t
22541 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22542 {
22543 	char		*addr;
22544 
22545 	if (phys_length != ETHERADDRL)
22546 		return (B_FALSE);
22547 
22548 	/* Form EUI-64 like address */
22549 	addr = (char *)&v6addr->s6_addr32[2];
22550 	bcopy((char *)phys_addr, addr, 3);
22551 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22552 	addr[3] = (char)0xff;
22553 	addr[4] = (char)0xfe;
22554 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22555 	return (B_TRUE);
22556 }
22557 
22558 /* ARGSUSED */
22559 static boolean_t
22560 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22561 {
22562 	return (B_FALSE);
22563 }
22564 
22565 /* ARGSUSED */
22566 static boolean_t
22567 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22568     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22569 {
22570 	/*
22571 	 * Multicast address mappings used over Ethernet/802.X.
22572 	 * This address is used as a base for mappings.
22573 	 */
22574 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22575 	    0x00, 0x00, 0x00};
22576 
22577 	/*
22578 	 * Extract low order 32 bits from IPv6 multicast address.
22579 	 * Or that into the link layer address, starting from the
22580 	 * second byte.
22581 	 */
22582 	*hw_start = 2;
22583 	v6_extract_mask->s6_addr32[0] = 0;
22584 	v6_extract_mask->s6_addr32[1] = 0;
22585 	v6_extract_mask->s6_addr32[2] = 0;
22586 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22587 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22588 	return (B_TRUE);
22589 }
22590 
22591 /*
22592  * Indicate by return value whether multicast is supported. If not,
22593  * this code should not touch/change any parameters.
22594  */
22595 /* ARGSUSED */
22596 static boolean_t
22597 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22598     uint32_t *hw_start, ipaddr_t *extract_mask)
22599 {
22600 	/*
22601 	 * Multicast address mappings used over Ethernet/802.X.
22602 	 * This address is used as a base for mappings.
22603 	 */
22604 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22605 	    0x00, 0x00, 0x00 };
22606 
22607 	if (phys_length != ETHERADDRL)
22608 		return (B_FALSE);
22609 
22610 	*extract_mask = htonl(0x007fffff);
22611 	*hw_start = 2;
22612 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22613 	return (B_TRUE);
22614 }
22615 
22616 /*
22617  * Derive IPoIB interface id from the link layer address.
22618  */
22619 static boolean_t
22620 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22621 {
22622 	char		*addr;
22623 
22624 	if (phys_length != 20)
22625 		return (B_FALSE);
22626 	addr = (char *)&v6addr->s6_addr32[2];
22627 	bcopy(phys_addr + 12, addr, 8);
22628 	/*
22629 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22630 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22631 	 * rules. In these cases, the IBA considers these GUIDs to be in
22632 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22633 	 * required; vendors are required not to assign global EUI-64's
22634 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22635 	 * of the interface identifier. Whether the GUID is in modified
22636 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22637 	 * bit set to 1.
22638 	 */
22639 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22640 	return (B_TRUE);
22641 }
22642 
22643 /*
22644  * Note on mapping from multicast IP addresses to IPoIB multicast link
22645  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22646  * The format of an IPoIB multicast address is:
22647  *
22648  *  4 byte QPN      Scope Sign.  Pkey
22649  * +--------------------------------------------+
22650  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22651  * +--------------------------------------------+
22652  *
22653  * The Scope and Pkey components are properties of the IBA port and
22654  * network interface. They can be ascertained from the broadcast address.
22655  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22656  */
22657 
22658 static boolean_t
22659 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22660     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22661 {
22662 	/*
22663 	 * Base IPoIB IPv6 multicast address used for mappings.
22664 	 * Does not contain the IBA scope/Pkey values.
22665 	 */
22666 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22667 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22668 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22669 
22670 	/*
22671 	 * Extract low order 80 bits from IPv6 multicast address.
22672 	 * Or that into the link layer address, starting from the
22673 	 * sixth byte.
22674 	 */
22675 	*hw_start = 6;
22676 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22677 
22678 	/*
22679 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22680 	 */
22681 	*(maddr + 5) = *(bphys_addr + 5);
22682 	*(maddr + 8) = *(bphys_addr + 8);
22683 	*(maddr + 9) = *(bphys_addr + 9);
22684 
22685 	v6_extract_mask->s6_addr32[0] = 0;
22686 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
22687 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
22688 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22689 	return (B_TRUE);
22690 }
22691 
22692 static boolean_t
22693 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22694     uint32_t *hw_start, ipaddr_t *extract_mask)
22695 {
22696 	/*
22697 	 * Base IPoIB IPv4 multicast address used for mappings.
22698 	 * Does not contain the IBA scope/Pkey values.
22699 	 */
22700 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22701 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
22702 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22703 
22704 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
22705 		return (B_FALSE);
22706 
22707 	/*
22708 	 * Extract low order 28 bits from IPv4 multicast address.
22709 	 * Or that into the link layer address, starting from the
22710 	 * sixteenth byte.
22711 	 */
22712 	*extract_mask = htonl(0x0fffffff);
22713 	*hw_start = 16;
22714 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
22715 
22716 	/*
22717 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22718 	 */
22719 	*(maddr + 5) = *(bphys_addr + 5);
22720 	*(maddr + 8) = *(bphys_addr + 8);
22721 	*(maddr + 9) = *(bphys_addr + 9);
22722 	return (B_TRUE);
22723 }
22724 
22725 /*
22726  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
22727  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
22728  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
22729  * the link-local address is preferred.
22730  */
22731 boolean_t
22732 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22733 {
22734 	ipif_t	*ipif;
22735 	ipif_t	*maybe_ipif = NULL;
22736 
22737 	mutex_enter(&ill->ill_lock);
22738 	if (ill->ill_state_flags & ILL_CONDEMNED) {
22739 		mutex_exit(&ill->ill_lock);
22740 		if (ipifp != NULL)
22741 			*ipifp = NULL;
22742 		return (B_FALSE);
22743 	}
22744 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
22745 		if (!IPIF_CAN_LOOKUP(ipif))
22746 			continue;
22747 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid)
22748 			continue;
22749 		if ((ipif->ipif_flags & flags) != flags)
22750 			continue;
22751 
22752 		if (ipifp == NULL) {
22753 			mutex_exit(&ill->ill_lock);
22754 			ASSERT(maybe_ipif == NULL);
22755 			return (B_TRUE);
22756 		}
22757 		if (!ill->ill_isv6 ||
22758 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
22759 			ipif_refhold_locked(ipif);
22760 			mutex_exit(&ill->ill_lock);
22761 			*ipifp = ipif;
22762 			return (B_TRUE);
22763 		}
22764 		if (maybe_ipif == NULL)
22765 			maybe_ipif = ipif;
22766 	}
22767 	if (ipifp != NULL) {
22768 		if (maybe_ipif != NULL)
22769 			ipif_refhold_locked(maybe_ipif);
22770 		*ipifp = maybe_ipif;
22771 	}
22772 	mutex_exit(&ill->ill_lock);
22773 	return (maybe_ipif != NULL);
22774 }
22775 
22776 /*
22777  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
22778  */
22779 boolean_t
22780 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22781 {
22782 	ill_t *illg;
22783 
22784 	/*
22785 	 * We look at the passed-in ill first without grabbing ill_g_lock.
22786 	 */
22787 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
22788 		return (B_TRUE);
22789 	}
22790 	rw_enter(&ill_g_lock, RW_READER);
22791 	if (ill->ill_group == NULL) {
22792 		/* ill not in a group */
22793 		rw_exit(&ill_g_lock);
22794 		return (B_FALSE);
22795 	}
22796 
22797 	/*
22798 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
22799 	 * group. We need to look for an ipif in the zone on all the ills in the
22800 	 * group.
22801 	 */
22802 	illg = ill->ill_group->illgrp_ill;
22803 	do {
22804 		/*
22805 		 * We don't call ipif_lookup_zoneid() on ill as we already know
22806 		 * that it's not there.
22807 		 */
22808 		if (illg != ill &&
22809 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
22810 			break;
22811 		}
22812 	} while ((illg = illg->ill_group_next) != NULL);
22813 	rw_exit(&ill_g_lock);
22814 	return (illg != NULL);
22815 }
22816 
22817 /*
22818  * Check if this ill is only being used to send ICMP probes for IPMP
22819  */
22820 boolean_t
22821 ill_is_probeonly(ill_t *ill)
22822 {
22823 	/*
22824 	 * Check if the interface is FAILED, or INACTIVE
22825 	 */
22826 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
22827 		return (B_TRUE);
22828 
22829 	return (B_FALSE);
22830 }
22831