xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 75ce41a57ff334bd8fe2cb9ed51eea835892f944)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strsubr.h>
38 #include <sys/strlog.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/cmn_err.h>
42 #include <sys/kstat.h>
43 #include <sys/debug.h>
44 #include <sys/zone.h>
45 #include <sys/sunldi.h>
46 #include <sys/file.h>
47 #include <sys/bitmap.h>
48 #include <sys/cpuvar.h>
49 #include <sys/time.h>
50 #include <sys/ctype.h>
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/isa_defs.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet/igmp_var.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 #include <sys/callb.h>
69 #include <sys/md5.h>
70 
71 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
72 #include <inet/mi.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 #include <inet/mib2.h>
76 #include <inet/ip.h>
77 #include <inet/ip6.h>
78 #include <inet/ip6_asp.h>
79 #include <inet/tcp.h>
80 #include <inet/ip_multi.h>
81 #include <inet/ip_ire.h>
82 #include <inet/ip_ftable.h>
83 #include <inet/ip_rts.h>
84 #include <inet/ip_ndp.h>
85 #include <inet/ip_if.h>
86 #include <inet/ip_impl.h>
87 #include <inet/tun.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 
91 #include <net/pfkeyv2.h>
92 #include <inet/ipsec_info.h>
93 #include <inet/sadb.h>
94 #include <inet/ipsec_impl.h>
95 #include <sys/iphada.h>
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac_client.h>
101 #include <sys/dld.h>
102 
103 #include <sys/systeminfo.h>
104 #include <sys/bootconf.h>
105 
106 #include <sys/tsol/tndb.h>
107 #include <sys/tsol/tnet.h>
108 
109 /* The character which tells where the ill_name ends */
110 #define	IPIF_SEPARATOR_CHAR	':'
111 
112 /* IP ioctl function table entry */
113 typedef struct ipft_s {
114 	int	ipft_cmd;
115 	pfi_t	ipft_pfi;
116 	int	ipft_min_size;
117 	int	ipft_flags;
118 } ipft_t;
119 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
120 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
121 
122 typedef struct ip_sock_ar_s {
123 	union {
124 		area_t	ip_sock_area;
125 		ared_t	ip_sock_ared;
126 		areq_t	ip_sock_areq;
127 	} ip_sock_ar_u;
128 	queue_t	*ip_sock_ar_q;
129 } ip_sock_ar_t;
130 
131 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
132 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
133 		    char *value, caddr_t cp, cred_t *ioc_cr);
134 
135 static boolean_t ill_is_quiescent(ill_t *);
136 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
137 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
138 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
141     mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
147     mblk_t *mp);
148 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
149     queue_t *q, mblk_t *mp, boolean_t need_up);
150 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
151     int ioccmd, struct linkblk *li, boolean_t doconsist);
152 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
153 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
154 static void	ipsq_flush(ill_t *ill);
155 
156 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
157     queue_t *q, mblk_t *mp, boolean_t need_up);
158 static void	ipsq_delete(ipsq_t *);
159 
160 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
161     boolean_t initialize, boolean_t insert);
162 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
163 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
164 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
165 		    boolean_t isv6);
166 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
167 static void	ipif_delete_cache_ire(ire_t *, char *);
168 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
169 static void	ipif_free(ipif_t *ipif);
170 static void	ipif_free_tail(ipif_t *ipif);
171 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static void	ipif_update_other_ipifs(ipif_t *old_ipif);
182 
183 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
184 static int	ill_arp_off(ill_t *ill);
185 static int	ill_arp_on(ill_t *ill);
186 static void	ill_delete_interface_type(ill_if_t *);
187 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
188 static void	ill_dl_down(ill_t *ill);
189 static void	ill_down(ill_t *ill);
190 static void	ill_downi(ire_t *ire, char *ill_arg);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
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_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
196 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
197 
198 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
199 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
200 static ip_v6mapinfo_func_t ip_ether_v6mapinfo, ip_ib_v6mapinfo;
201 static ip_v4mapinfo_func_t ip_ether_v4mapinfo, ip_ib_v4mapinfo;
202 static void	ipif_save_ire(ipif_t *, ire_t *);
203 static void	ipif_remove_ire(ipif_t *, ire_t *);
204 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
205 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
206 static void	phyint_free(phyint_t *);
207 
208 /*
209  * Per-ill IPsec capabilities management.
210  */
211 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
212 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
213 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
214 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
215 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
216 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
217     boolean_t);
218 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
219 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
220 static void ill_capability_mdt_reset_fill(ill_t *, mblk_t *);
221 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
222 static void ill_capability_ipsec_reset_fill(ill_t *, mblk_t *);
223 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
224 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
225 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
226     dl_capability_sub_t *);
227 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
228 static int  ill_capability_ipsec_reset_size(ill_t *, int *, int *, int *,
229     int *);
230 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
231 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
232 		    dl_capability_sub_t *);
233 static void	ill_capability_dld_enable(ill_t *);
234 static void	ill_capability_ack_thr(void *);
235 static void	ill_capability_lso_enable(ill_t *);
236 static void	ill_capability_send(ill_t *, mblk_t *);
237 
238 static ill_t	*ill_prev_usesrc(ill_t *);
239 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
240 static void	ill_disband_usesrc_group(ill_t *);
241 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
242 
243 #ifdef DEBUG
244 static  void    ill_trace_cleanup(const ill_t *);
245 static  void    ipif_trace_cleanup(const ipif_t *);
246 #endif
247 
248 /*
249  * if we go over the memory footprint limit more than once in this msec
250  * interval, we'll start pruning aggressively.
251  */
252 int ip_min_frag_prune_time = 0;
253 
254 /*
255  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
256  * and the IPsec DOI
257  */
258 #define	MAX_IPSEC_ALGS	256
259 
260 #define	BITSPERBYTE	8
261 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
262 
263 #define	IPSEC_ALG_ENABLE(algs, algid) \
264 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
265 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
266 
267 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
268 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
269 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
270 
271 typedef uint8_t ipsec_capab_elem_t;
272 
273 /*
274  * Per-algorithm parameters.  Note that at present, only encryption
275  * algorithms have variable keysize (IKE does not provide a way to negotiate
276  * auth algorithm keysize).
277  *
278  * All sizes here are in bits.
279  */
280 typedef struct
281 {
282 	uint16_t	minkeylen;
283 	uint16_t	maxkeylen;
284 } ipsec_capab_algparm_t;
285 
286 /*
287  * Per-ill capabilities.
288  */
289 struct ill_ipsec_capab_s {
290 	ipsec_capab_elem_t *encr_hw_algs;
291 	ipsec_capab_elem_t *auth_hw_algs;
292 	uint32_t algs_size;	/* size of _hw_algs in bytes */
293 	/* algorithm key lengths */
294 	ipsec_capab_algparm_t *encr_algparm;
295 	uint32_t encr_algparm_size;
296 	uint32_t encr_algparm_end;
297 };
298 
299 /*
300  * The field values are larger than strictly necessary for simple
301  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
302  */
303 static area_t	ip_area_template = {
304 	AR_ENTRY_ADD,			/* area_cmd */
305 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
306 					/* area_name_offset */
307 	/* area_name_length temporarily holds this structure length */
308 	sizeof (area_t),			/* area_name_length */
309 	IP_ARP_PROTO_TYPE,		/* area_proto */
310 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
311 	IP_ADDR_LEN,			/* area_proto_addr_length */
312 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
313 					/* area_proto_mask_offset */
314 	0,				/* area_flags */
315 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
316 					/* area_hw_addr_offset */
317 	/* Zero length hw_addr_length means 'use your idea of the address' */
318 	0				/* area_hw_addr_length */
319 };
320 
321 /*
322  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
323  * support
324  */
325 static area_t	ip6_area_template = {
326 	AR_ENTRY_ADD,			/* area_cmd */
327 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
328 					/* area_name_offset */
329 	/* area_name_length temporarily holds this structure length */
330 	sizeof (area_t),			/* area_name_length */
331 	IP_ARP_PROTO_TYPE,		/* area_proto */
332 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
333 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
334 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
335 					/* area_proto_mask_offset */
336 	0,				/* area_flags */
337 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
338 					/* area_hw_addr_offset */
339 	/* Zero length hw_addr_length means 'use your idea of the address' */
340 	0				/* area_hw_addr_length */
341 };
342 
343 static ared_t	ip_ared_template = {
344 	AR_ENTRY_DELETE,
345 	sizeof (ared_t) + IP_ADDR_LEN,
346 	sizeof (ared_t),
347 	IP_ARP_PROTO_TYPE,
348 	sizeof (ared_t),
349 	IP_ADDR_LEN,
350 	0
351 };
352 
353 static ared_t	ip6_ared_template = {
354 	AR_ENTRY_DELETE,
355 	sizeof (ared_t) + IPV6_ADDR_LEN,
356 	sizeof (ared_t),
357 	IP_ARP_PROTO_TYPE,
358 	sizeof (ared_t),
359 	IPV6_ADDR_LEN,
360 	0
361 };
362 
363 /*
364  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
365  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
366  * areq is used).
367  */
368 static areq_t	ip_areq_template = {
369 	AR_ENTRY_QUERY,			/* cmd */
370 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
371 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
372 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
373 	sizeof (areq_t),			/* target addr offset */
374 	IP_ADDR_LEN,			/* target addr_length */
375 	0,				/* flags */
376 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
377 	IP_ADDR_LEN,			/* sender addr length */
378 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
379 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
380 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
381 	/* anything else filled in by the code */
382 };
383 
384 static arc_t	ip_aru_template = {
385 	AR_INTERFACE_UP,
386 	sizeof (arc_t),		/* Name offset */
387 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
388 };
389 
390 static arc_t	ip_ard_template = {
391 	AR_INTERFACE_DOWN,
392 	sizeof (arc_t),		/* Name offset */
393 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
394 };
395 
396 static arc_t	ip_aron_template = {
397 	AR_INTERFACE_ON,
398 	sizeof (arc_t),		/* Name offset */
399 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
400 };
401 
402 static arc_t	ip_aroff_template = {
403 	AR_INTERFACE_OFF,
404 	sizeof (arc_t),		/* Name offset */
405 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
406 };
407 
408 static arma_t	ip_arma_multi_template = {
409 	AR_MAPPING_ADD,
410 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
411 				/* Name offset */
412 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
413 	IP_ARP_PROTO_TYPE,
414 	sizeof (arma_t),			/* proto_addr_offset */
415 	IP_ADDR_LEN,				/* proto_addr_length */
416 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
417 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
418 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
419 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
420 	IP_MAX_HW_LEN,				/* hw_addr_length */
421 	0,					/* hw_mapping_start */
422 };
423 
424 static ipft_t	ip_ioctl_ftbl[] = {
425 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
426 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
427 		IPFT_F_NO_REPLY },
428 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
429 		IPFT_F_NO_REPLY },
430 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
431 	{ 0 }
432 };
433 
434 /* Simple ICMP IP Header Template */
435 static ipha_t icmp_ipha = {
436 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
437 };
438 
439 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
440 
441 static ip_m_t   ip_m_tbl[] = {
442 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
443 	    ip_ether_v6intfid },
444 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
445 	    ip_nodef_v6intfid },
446 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
447 	    ip_nodef_v6intfid },
448 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
449 	    ip_nodef_v6intfid },
450 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
451 	    ip_ether_v6intfid },
452 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
453 	    ip_ib_v6intfid },
454 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL },
455 	{ SUNW_DL_IPMP, IFT_OTHER, NULL, NULL, ip_ipmp_v6intfid },
456 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
457 	    ip_nodef_v6intfid }
458 };
459 
460 static ill_t	ill_null;		/* Empty ILL for init. */
461 char	ipif_loopback_name[] = "lo0";
462 static char *ipv4_forward_suffix = ":ip_forwarding";
463 static char *ipv6_forward_suffix = ":ip6_forwarding";
464 static	sin6_t	sin6_null;	/* Zero address for quick clears */
465 static	sin_t	sin_null;	/* Zero address for quick clears */
466 
467 /* When set search for unused ipif_seqid */
468 static ipif_t	ipif_zero;
469 
470 /*
471  * ppa arena is created after these many
472  * interfaces have been plumbed.
473  */
474 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
475 
476 /*
477  * Allocate per-interface mibs.
478  * Returns true if ok. False otherwise.
479  *  ipsq  may not yet be allocated (loopback case ).
480  */
481 static boolean_t
482 ill_allocate_mibs(ill_t *ill)
483 {
484 	/* Already allocated? */
485 	if (ill->ill_ip_mib != NULL) {
486 		if (ill->ill_isv6)
487 			ASSERT(ill->ill_icmp6_mib != NULL);
488 		return (B_TRUE);
489 	}
490 
491 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
492 	    KM_NOSLEEP);
493 	if (ill->ill_ip_mib == NULL) {
494 		return (B_FALSE);
495 	}
496 
497 	/* Setup static information */
498 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
499 	    sizeof (mib2_ipIfStatsEntry_t));
500 	if (ill->ill_isv6) {
501 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
502 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
503 		    sizeof (mib2_ipv6AddrEntry_t));
504 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
505 		    sizeof (mib2_ipv6RouteEntry_t));
506 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
507 		    sizeof (mib2_ipv6NetToMediaEntry_t));
508 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
509 		    sizeof (ipv6_member_t));
510 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
511 		    sizeof (ipv6_grpsrc_t));
512 	} else {
513 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
514 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
515 		    sizeof (mib2_ipAddrEntry_t));
516 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
517 		    sizeof (mib2_ipRouteEntry_t));
518 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
519 		    sizeof (mib2_ipNetToMediaEntry_t));
520 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
521 		    sizeof (ip_member_t));
522 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
523 		    sizeof (ip_grpsrc_t));
524 
525 		/*
526 		 * For a v4 ill, we are done at this point, because per ill
527 		 * icmp mibs are only used for v6.
528 		 */
529 		return (B_TRUE);
530 	}
531 
532 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
533 	    KM_NOSLEEP);
534 	if (ill->ill_icmp6_mib == NULL) {
535 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
536 		ill->ill_ip_mib = NULL;
537 		return (B_FALSE);
538 	}
539 	/* static icmp info */
540 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
541 	    sizeof (mib2_ipv6IfIcmpEntry_t);
542 	/*
543 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
544 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
545 	 * -> ill_phyint_reinit
546 	 */
547 	return (B_TRUE);
548 }
549 
550 /*
551  * Common code for preparation of ARP commands.  Two points to remember:
552  * 	1) The ill_name is tacked on at the end of the allocated space so
553  *	   the templates name_offset field must contain the total space
554  *	   to allocate less the name length.
555  *
556  *	2) The templates name_length field should contain the *template*
557  *	   length.  We use it as a parameter to bcopy() and then write
558  *	   the real ill_name_length into the name_length field of the copy.
559  * (Always called as writer.)
560  */
561 mblk_t *
562 ill_arp_alloc(ill_t *ill, const uchar_t *template, caddr_t addr)
563 {
564 	arc_t	*arc = (arc_t *)template;
565 	char	*cp;
566 	int	len;
567 	mblk_t	*mp;
568 	uint_t	name_length = ill->ill_name_length;
569 	uint_t	template_len = arc->arc_name_length;
570 
571 	len = arc->arc_name_offset + name_length;
572 	mp = allocb(len, BPRI_HI);
573 	if (mp == NULL)
574 		return (NULL);
575 	cp = (char *)mp->b_rptr;
576 	mp->b_wptr = (uchar_t *)&cp[len];
577 	if (template_len)
578 		bcopy(template, cp, template_len);
579 	if (len > template_len)
580 		bzero(&cp[template_len], len - template_len);
581 	mp->b_datap->db_type = M_PROTO;
582 
583 	arc = (arc_t *)cp;
584 	arc->arc_name_length = name_length;
585 	cp = (char *)arc + arc->arc_name_offset;
586 	bcopy(ill->ill_name, cp, name_length);
587 
588 	if (addr) {
589 		area_t	*area = (area_t *)mp->b_rptr;
590 
591 		cp = (char *)area + area->area_proto_addr_offset;
592 		bcopy(addr, cp, area->area_proto_addr_length);
593 		if (area->area_cmd == AR_ENTRY_ADD) {
594 			cp = (char *)area;
595 			len = area->area_proto_addr_length;
596 			if (area->area_proto_mask_offset)
597 				cp += area->area_proto_mask_offset;
598 			else
599 				cp += area->area_proto_addr_offset + len;
600 			while (len-- > 0)
601 				*cp++ = (char)~0;
602 		}
603 	}
604 	return (mp);
605 }
606 
607 mblk_t *
608 ipif_area_alloc(ipif_t *ipif, uint_t optflags)
609 {
610 	caddr_t	addr;
611 	mblk_t 	*mp;
612 	area_t	*area;
613 	uchar_t	*areap;
614 	ill_t	*ill = ipif->ipif_ill;
615 
616 	if (ill->ill_isv6) {
617 		ASSERT(ill->ill_flags & ILLF_XRESOLV);
618 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
619 		areap = (uchar_t *)&ip6_area_template;
620 	} else {
621 		addr = (caddr_t)&ipif->ipif_lcl_addr;
622 		areap = (uchar_t *)&ip_area_template;
623 	}
624 
625 	if ((mp = ill_arp_alloc(ill, areap, addr)) == NULL)
626 		return (NULL);
627 
628 	/*
629 	 * IPMP requires that the hardware address be included in all
630 	 * AR_ENTRY_ADD requests so that ARP can deduce the arl to send on.
631 	 * If there are no active underlying ills in the group (and thus no
632 	 * hardware address, DAD will be deferred until an underlying ill
633 	 * becomes active.
634 	 */
635 	if (IS_IPMP(ill)) {
636 		if ((ill = ipmp_ipif_hold_bound_ill(ipif)) == NULL) {
637 			freemsg(mp);
638 			return (NULL);
639 		}
640 	} else {
641 		ill_refhold(ill);
642 	}
643 
644 	area = (area_t *)mp->b_rptr;
645 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
646 	area->area_flags |= optflags;
647 	area->area_hw_addr_length = ill->ill_phys_addr_length;
648 	bcopy(ill->ill_phys_addr, mp->b_rptr + area->area_hw_addr_offset,
649 	    area->area_hw_addr_length);
650 
651 	ill_refrele(ill);
652 	return (mp);
653 }
654 
655 mblk_t *
656 ipif_ared_alloc(ipif_t *ipif)
657 {
658 	caddr_t	addr;
659 	uchar_t	*aredp;
660 
661 	if (ipif->ipif_ill->ill_isv6) {
662 		ASSERT(ipif->ipif_ill->ill_flags & ILLF_XRESOLV);
663 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
664 		aredp = (uchar_t *)&ip6_ared_template;
665 	} else {
666 		addr = (caddr_t)&ipif->ipif_lcl_addr;
667 		aredp = (uchar_t *)&ip_ared_template;
668 	}
669 
670 	return (ill_arp_alloc(ipif->ipif_ill, aredp, addr));
671 }
672 
673 mblk_t *
674 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
675 {
676 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
677 	    (char *)&addr));
678 }
679 
680 mblk_t *
681 ill_arie_alloc(ill_t *ill, const char *grifname, const void *template)
682 {
683 	mblk_t	*mp = ill_arp_alloc(ill, template, 0);
684 	arie_t	*arie;
685 
686 	if (mp != NULL) {
687 		arie = (arie_t *)mp->b_rptr;
688 		(void) strlcpy(arie->arie_grifname, grifname, LIFNAMSIZ);
689 	}
690 	return (mp);
691 }
692 
693 /*
694  * Completely vaporize a lower level tap and all associated interfaces.
695  * ill_delete is called only out of ip_close when the device control
696  * stream is being closed.
697  */
698 void
699 ill_delete(ill_t *ill)
700 {
701 	ipif_t	*ipif;
702 	ill_t	*prev_ill;
703 	ip_stack_t	*ipst = ill->ill_ipst;
704 
705 	/*
706 	 * ill_delete may be forcibly entering the ipsq. The previous
707 	 * ioctl may not have completed and may need to be aborted.
708 	 * ipsq_flush takes care of it. If we don't need to enter the
709 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
710 	 * ill_delete_tail is sufficient.
711 	 */
712 	ipsq_flush(ill);
713 
714 	/*
715 	 * Nuke all interfaces.  ipif_free will take down the interface,
716 	 * remove it from the list, and free the data structure.
717 	 * Walk down the ipif list and remove the logical interfaces
718 	 * first before removing the main ipif. We can't unplumb
719 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
720 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
721 	 * POINTOPOINT.
722 	 *
723 	 * If ill_ipif was not properly initialized (i.e low on memory),
724 	 * then no interfaces to clean up. In this case just clean up the
725 	 * ill.
726 	 */
727 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
728 		ipif_free(ipif);
729 
730 	/*
731 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
732 	 * So nobody can be using this mp now. Free the mp allocated for
733 	 * honoring ILLF_NOARP
734 	 */
735 	freemsg(ill->ill_arp_on_mp);
736 	ill->ill_arp_on_mp = NULL;
737 
738 	/* Clean up msgs on pending upcalls for mrouted */
739 	reset_mrt_ill(ill);
740 
741 	/*
742 	 * ipif_free -> reset_conn_ipif will remove all multicast
743 	 * references for IPv4. For IPv6, we need to do it here as
744 	 * it points only at ills.
745 	 */
746 	reset_conn_ill(ill);
747 
748 	/*
749 	 * Remove multicast references added as a result of calls to
750 	 * ip_join_allmulti().
751 	 */
752 	ip_purge_allmulti(ill);
753 
754 	/*
755 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
756 	 */
757 	if (IS_UNDER_IPMP(ill))
758 		ipmp_ill_leave_illgrp(ill);
759 
760 	/*
761 	 * ill_down will arrange to blow off any IRE's dependent on this
762 	 * ILL, and shut down fragmentation reassembly.
763 	 */
764 	ill_down(ill);
765 
766 	/* Let SCTP know, so that it can remove this from its list. */
767 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
768 
769 	/*
770 	 * If an address on this ILL is being used as a source address then
771 	 * clear out the pointers in other ILLs that point to this ILL.
772 	 */
773 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
774 	if (ill->ill_usesrc_grp_next != NULL) {
775 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
776 			ill_disband_usesrc_group(ill);
777 		} else {	/* consumer of the usesrc ILL */
778 			prev_ill = ill_prev_usesrc(ill);
779 			prev_ill->ill_usesrc_grp_next =
780 			    ill->ill_usesrc_grp_next;
781 		}
782 	}
783 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
784 }
785 
786 static void
787 ipif_non_duplicate(ipif_t *ipif)
788 {
789 	ill_t *ill = ipif->ipif_ill;
790 	mutex_enter(&ill->ill_lock);
791 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
792 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
793 		ASSERT(ill->ill_ipif_dup_count > 0);
794 		ill->ill_ipif_dup_count--;
795 	}
796 	mutex_exit(&ill->ill_lock);
797 }
798 
799 /*
800  * ill_delete_tail is called from ip_modclose after all references
801  * to the closing ill are gone. The wait is done in ip_modclose
802  */
803 void
804 ill_delete_tail(ill_t *ill)
805 {
806 	mblk_t	**mpp;
807 	ipif_t	*ipif;
808 	ip_stack_t	*ipst = ill->ill_ipst;
809 
810 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
811 		ipif_non_duplicate(ipif);
812 		ipif_down_tail(ipif);
813 	}
814 
815 	ASSERT(ill->ill_ipif_dup_count == 0 &&
816 	    ill->ill_arp_down_mp == NULL &&
817 	    ill->ill_arp_del_mapping_mp == NULL);
818 
819 	/*
820 	 * If polling capability is enabled (which signifies direct
821 	 * upcall into IP and driver has ill saved as a handle),
822 	 * we need to make sure that unbind has completed before we
823 	 * let the ill disappear and driver no longer has any reference
824 	 * to this ill.
825 	 */
826 	mutex_enter(&ill->ill_lock);
827 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
828 		cv_wait(&ill->ill_cv, &ill->ill_lock);
829 	mutex_exit(&ill->ill_lock);
830 	ASSERT(!(ill->ill_capabilities &
831 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
832 
833 	if (ill->ill_net_type != IRE_LOOPBACK)
834 		qprocsoff(ill->ill_rq);
835 
836 	/*
837 	 * We do an ipsq_flush once again now. New messages could have
838 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
839 	 * could also have landed up if an ioctl thread had looked up
840 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
841 	 * enqueued the ioctl when we did the ipsq_flush last time.
842 	 */
843 	ipsq_flush(ill);
844 
845 	/*
846 	 * Free capabilities.
847 	 */
848 	if (ill->ill_ipsec_capab_ah != NULL) {
849 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
850 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
851 		ill->ill_ipsec_capab_ah = NULL;
852 	}
853 
854 	if (ill->ill_ipsec_capab_esp != NULL) {
855 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
856 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
857 		ill->ill_ipsec_capab_esp = NULL;
858 	}
859 
860 	if (ill->ill_mdt_capab != NULL) {
861 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
862 		ill->ill_mdt_capab = NULL;
863 	}
864 
865 	if (ill->ill_hcksum_capab != NULL) {
866 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
867 		ill->ill_hcksum_capab = NULL;
868 	}
869 
870 	if (ill->ill_zerocopy_capab != NULL) {
871 		kmem_free(ill->ill_zerocopy_capab,
872 		    sizeof (ill_zerocopy_capab_t));
873 		ill->ill_zerocopy_capab = NULL;
874 	}
875 
876 	if (ill->ill_lso_capab != NULL) {
877 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
878 		ill->ill_lso_capab = NULL;
879 	}
880 
881 	if (ill->ill_dld_capab != NULL) {
882 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
883 		ill->ill_dld_capab = NULL;
884 	}
885 
886 	while (ill->ill_ipif != NULL)
887 		ipif_free_tail(ill->ill_ipif);
888 
889 	/*
890 	 * We have removed all references to ilm from conn and the ones joined
891 	 * within the kernel.
892 	 *
893 	 * We don't walk conns, mrts and ires because
894 	 *
895 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
896 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
897 	 *    ill references.
898 	 */
899 	ASSERT(ilm_walk_ill(ill) == 0);
900 
901 	/*
902 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
903 	 * is safe to do because the illgrp has already been unlinked from the
904 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
905 	 */
906 	if (IS_IPMP(ill)) {
907 		ipmp_illgrp_destroy(ill->ill_grp);
908 		ill->ill_grp = NULL;
909 	}
910 
911 	/*
912 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
913 	 * could free the phyint. No more reference to the phyint after this
914 	 * point.
915 	 */
916 	(void) ill_glist_delete(ill);
917 
918 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
919 	if (ill->ill_ndd_name != NULL)
920 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
921 	rw_exit(&ipst->ips_ip_g_nd_lock);
922 
923 	if (ill->ill_frag_ptr != NULL) {
924 		uint_t count;
925 
926 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
927 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
928 		}
929 		mi_free(ill->ill_frag_ptr);
930 		ill->ill_frag_ptr = NULL;
931 		ill->ill_frag_hash_tbl = NULL;
932 	}
933 
934 	freemsg(ill->ill_nd_lla_mp);
935 	/* Free all retained control messages. */
936 	mpp = &ill->ill_first_mp_to_free;
937 	do {
938 		while (mpp[0]) {
939 			mblk_t  *mp;
940 			mblk_t  *mp1;
941 
942 			mp = mpp[0];
943 			mpp[0] = mp->b_next;
944 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
945 				mp1->b_next = NULL;
946 				mp1->b_prev = NULL;
947 			}
948 			freemsg(mp);
949 		}
950 	} while (mpp++ != &ill->ill_last_mp_to_free);
951 
952 	ill_free_mib(ill);
953 
954 #ifdef DEBUG
955 	ill_trace_cleanup(ill);
956 #endif
957 
958 	/* Drop refcnt here */
959 	netstack_rele(ill->ill_ipst->ips_netstack);
960 	ill->ill_ipst = NULL;
961 }
962 
963 static void
964 ill_free_mib(ill_t *ill)
965 {
966 	ip_stack_t *ipst = ill->ill_ipst;
967 
968 	/*
969 	 * MIB statistics must not be lost, so when an interface
970 	 * goes away the counter values will be added to the global
971 	 * MIBs.
972 	 */
973 	if (ill->ill_ip_mib != NULL) {
974 		if (ill->ill_isv6) {
975 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
976 			    ill->ill_ip_mib);
977 		} else {
978 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
979 			    ill->ill_ip_mib);
980 		}
981 
982 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
983 		ill->ill_ip_mib = NULL;
984 	}
985 	if (ill->ill_icmp6_mib != NULL) {
986 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
987 		    ill->ill_icmp6_mib);
988 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
989 		ill->ill_icmp6_mib = NULL;
990 	}
991 }
992 
993 /*
994  * Concatenate together a physical address and a sap.
995  *
996  * Sap_lengths are interpreted as follows:
997  *   sap_length == 0	==>	no sap
998  *   sap_length > 0	==>	sap is at the head of the dlpi address
999  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1000  */
1001 static void
1002 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1003     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1004 {
1005 	uint16_t sap_addr = (uint16_t)sap_src;
1006 
1007 	if (sap_length == 0) {
1008 		if (phys_src == NULL)
1009 			bzero(dst, phys_length);
1010 		else
1011 			bcopy(phys_src, dst, phys_length);
1012 	} else if (sap_length < 0) {
1013 		if (phys_src == NULL)
1014 			bzero(dst, phys_length);
1015 		else
1016 			bcopy(phys_src, dst, phys_length);
1017 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1018 	} else {
1019 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1020 		if (phys_src == NULL)
1021 			bzero((char *)dst + sap_length, phys_length);
1022 		else
1023 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1024 	}
1025 }
1026 
1027 /*
1028  * Generate a dl_unitdata_req mblk for the device and address given.
1029  * addr_length is the length of the physical portion of the address.
1030  * If addr is NULL include an all zero address of the specified length.
1031  * TRUE? In any case, addr_length is taken to be the entire length of the
1032  * dlpi address, including the absolute value of sap_length.
1033  */
1034 mblk_t *
1035 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1036 		t_scalar_t sap_length)
1037 {
1038 	dl_unitdata_req_t *dlur;
1039 	mblk_t	*mp;
1040 	t_scalar_t	abs_sap_length;		/* absolute value */
1041 
1042 	abs_sap_length = ABS(sap_length);
1043 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1044 	    DL_UNITDATA_REQ);
1045 	if (mp == NULL)
1046 		return (NULL);
1047 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1048 	/* HACK: accomodate incompatible DLPI drivers */
1049 	if (addr_length == 8)
1050 		addr_length = 6;
1051 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1052 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1053 	dlur->dl_priority.dl_min = 0;
1054 	dlur->dl_priority.dl_max = 0;
1055 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1056 	    (uchar_t *)&dlur[1]);
1057 	return (mp);
1058 }
1059 
1060 /*
1061  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1062  * Return an error if we already have 1 or more ioctls in progress.
1063  * This is used only for non-exclusive ioctls. Currently this is used
1064  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1065  * and thus need to use ipsq_pending_mp_add.
1066  */
1067 boolean_t
1068 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1069 {
1070 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1071 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1072 	/*
1073 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1074 	 */
1075 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1076 	    (add_mp->b_datap->db_type == M_IOCTL));
1077 
1078 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1079 	/*
1080 	 * Return error if the conn has started closing. The conn
1081 	 * could have finished cleaning up the pending mp list,
1082 	 * If so we should not add another mp to the list negating
1083 	 * the cleanup.
1084 	 */
1085 	if (connp->conn_state_flags & CONN_CLOSING)
1086 		return (B_FALSE);
1087 	/*
1088 	 * Add the pending mp to the head of the list, chained by b_next.
1089 	 * Note down the conn on which the ioctl request came, in b_prev.
1090 	 * This will be used to later get the conn, when we get a response
1091 	 * on the ill queue, from some other module (typically arp)
1092 	 */
1093 	add_mp->b_next = (void *)ill->ill_pending_mp;
1094 	add_mp->b_queue = CONNP_TO_WQ(connp);
1095 	ill->ill_pending_mp = add_mp;
1096 	if (connp != NULL)
1097 		connp->conn_oper_pending_ill = ill;
1098 	return (B_TRUE);
1099 }
1100 
1101 /*
1102  * Retrieve the ill_pending_mp and return it. We have to walk the list
1103  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1104  */
1105 mblk_t *
1106 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1107 {
1108 	mblk_t	*prev = NULL;
1109 	mblk_t	*curr = NULL;
1110 	uint_t	id;
1111 	conn_t	*connp;
1112 
1113 	/*
1114 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1115 	 * up the pending mp, but it does not know the ioc_id and
1116 	 * passes in a zero for it.
1117 	 */
1118 	mutex_enter(&ill->ill_lock);
1119 	if (ioc_id != 0)
1120 		*connpp = NULL;
1121 
1122 	/* Search the list for the appropriate ioctl based on ioc_id */
1123 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1124 	    prev = curr, curr = curr->b_next) {
1125 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1126 		connp = Q_TO_CONN(curr->b_queue);
1127 		/* Match based on the ioc_id or based on the conn */
1128 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1129 			break;
1130 	}
1131 
1132 	if (curr != NULL) {
1133 		/* Unlink the mblk from the pending mp list */
1134 		if (prev != NULL) {
1135 			prev->b_next = curr->b_next;
1136 		} else {
1137 			ASSERT(ill->ill_pending_mp == curr);
1138 			ill->ill_pending_mp = curr->b_next;
1139 		}
1140 
1141 		/*
1142 		 * conn refcnt must have been bumped up at the start of
1143 		 * the ioctl. So we can safely access the conn.
1144 		 */
1145 		ASSERT(CONN_Q(curr->b_queue));
1146 		*connpp = Q_TO_CONN(curr->b_queue);
1147 		curr->b_next = NULL;
1148 		curr->b_queue = NULL;
1149 	}
1150 
1151 	mutex_exit(&ill->ill_lock);
1152 
1153 	return (curr);
1154 }
1155 
1156 /*
1157  * Add the pending mp to the list. There can be only 1 pending mp
1158  * in the list. Any exclusive ioctl that needs to wait for a response
1159  * from another module or driver needs to use this function to set
1160  * the ipx_pending_mp to the ioctl mblk and wait for the response from
1161  * the other module/driver. This is also used while waiting for the
1162  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1163  */
1164 boolean_t
1165 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1166     int waitfor)
1167 {
1168 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
1169 
1170 	ASSERT(IAM_WRITER_IPIF(ipif));
1171 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1172 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1173 	ASSERT(ipx->ipx_pending_mp == NULL);
1174 	/*
1175 	 * The caller may be using a different ipif than the one passed into
1176 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1177 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1178 	 * that `ipx_current_ipif == ipif'.
1179 	 */
1180 	ASSERT(ipx->ipx_current_ipif != NULL);
1181 
1182 	/*
1183 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1184 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1185 	 */
1186 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1187 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1188 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1189 
1190 	if (connp != NULL) {
1191 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1192 		/*
1193 		 * Return error if the conn has started closing. The conn
1194 		 * could have finished cleaning up the pending mp list,
1195 		 * If so we should not add another mp to the list negating
1196 		 * the cleanup.
1197 		 */
1198 		if (connp->conn_state_flags & CONN_CLOSING)
1199 			return (B_FALSE);
1200 	}
1201 	mutex_enter(&ipx->ipx_lock);
1202 	ipx->ipx_pending_ipif = ipif;
1203 	/*
1204 	 * Note down the queue in b_queue. This will be returned by
1205 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1206 	 * the processing
1207 	 */
1208 	add_mp->b_next = NULL;
1209 	add_mp->b_queue = q;
1210 	ipx->ipx_pending_mp = add_mp;
1211 	ipx->ipx_waitfor = waitfor;
1212 	mutex_exit(&ipx->ipx_lock);
1213 
1214 	if (connp != NULL)
1215 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1216 
1217 	return (B_TRUE);
1218 }
1219 
1220 /*
1221  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
1222  * queued in the list.
1223  */
1224 mblk_t *
1225 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1226 {
1227 	mblk_t	*curr = NULL;
1228 	ipxop_t	*ipx = ipsq->ipsq_xop;
1229 
1230 	*connpp = NULL;
1231 	mutex_enter(&ipx->ipx_lock);
1232 	if (ipx->ipx_pending_mp == NULL) {
1233 		mutex_exit(&ipx->ipx_lock);
1234 		return (NULL);
1235 	}
1236 
1237 	/* There can be only 1 such excl message */
1238 	curr = ipx->ipx_pending_mp;
1239 	ASSERT(curr->b_next == NULL);
1240 	ipx->ipx_pending_ipif = NULL;
1241 	ipx->ipx_pending_mp = NULL;
1242 	ipx->ipx_waitfor = 0;
1243 	mutex_exit(&ipx->ipx_lock);
1244 
1245 	if (CONN_Q(curr->b_queue)) {
1246 		/*
1247 		 * This mp did a refhold on the conn, at the start of the ioctl.
1248 		 * So we can safely return a pointer to the conn to the caller.
1249 		 */
1250 		*connpp = Q_TO_CONN(curr->b_queue);
1251 	} else {
1252 		*connpp = NULL;
1253 	}
1254 	curr->b_next = NULL;
1255 	curr->b_prev = NULL;
1256 	return (curr);
1257 }
1258 
1259 /*
1260  * Cleanup the ioctl mp queued in ipx_pending_mp
1261  * - Called in the ill_delete path
1262  * - Called in the M_ERROR or M_HANGUP path on the ill.
1263  * - Called in the conn close path.
1264  */
1265 boolean_t
1266 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1267 {
1268 	mblk_t	*mp;
1269 	ipxop_t	*ipx;
1270 	queue_t	*q;
1271 	ipif_t	*ipif;
1272 
1273 	ASSERT(IAM_WRITER_ILL(ill));
1274 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
1275 
1276 	/*
1277 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
1278 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1279 	 * even if it is meant for another ill, since we have to enqueue
1280 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
1281 	 * If connp is non-null we are called from the conn close path.
1282 	 */
1283 	mutex_enter(&ipx->ipx_lock);
1284 	mp = ipx->ipx_pending_mp;
1285 	if (mp == NULL || (connp != NULL &&
1286 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1287 		mutex_exit(&ipx->ipx_lock);
1288 		return (B_FALSE);
1289 	}
1290 	/* Now remove from the ipx_pending_mp */
1291 	ipx->ipx_pending_mp = NULL;
1292 	q = mp->b_queue;
1293 	mp->b_next = NULL;
1294 	mp->b_prev = NULL;
1295 	mp->b_queue = NULL;
1296 
1297 	ipif = ipx->ipx_pending_ipif;
1298 	ipx->ipx_pending_ipif = NULL;
1299 	ipx->ipx_waitfor = 0;
1300 	ipx->ipx_current_ipif = NULL;
1301 	ipx->ipx_current_ioctl = 0;
1302 	ipx->ipx_current_done = B_TRUE;
1303 	mutex_exit(&ipx->ipx_lock);
1304 
1305 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1306 		if (connp == NULL) {
1307 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1308 		} else {
1309 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1310 			mutex_enter(&ipif->ipif_ill->ill_lock);
1311 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1312 			mutex_exit(&ipif->ipif_ill->ill_lock);
1313 		}
1314 	} else {
1315 		/*
1316 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1317 		 * be just inet_freemsg. we have to restart it
1318 		 * otherwise the thread will be stuck.
1319 		 */
1320 		inet_freemsg(mp);
1321 	}
1322 	return (B_TRUE);
1323 }
1324 
1325 /*
1326  * The ill is closing. Cleanup all the pending mps. Called exclusively
1327  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1328  * knows this ill, and hence nobody can add an mp to this list
1329  */
1330 static void
1331 ill_pending_mp_cleanup(ill_t *ill)
1332 {
1333 	mblk_t	*mp;
1334 	queue_t	*q;
1335 
1336 	ASSERT(IAM_WRITER_ILL(ill));
1337 
1338 	mutex_enter(&ill->ill_lock);
1339 	/*
1340 	 * Every mp on the pending mp list originating from an ioctl
1341 	 * added 1 to the conn refcnt, at the start of the ioctl.
1342 	 * So bump it down now.  See comments in ip_wput_nondata()
1343 	 */
1344 	while (ill->ill_pending_mp != NULL) {
1345 		mp = ill->ill_pending_mp;
1346 		ill->ill_pending_mp = mp->b_next;
1347 		mutex_exit(&ill->ill_lock);
1348 
1349 		q = mp->b_queue;
1350 		ASSERT(CONN_Q(q));
1351 		mp->b_next = NULL;
1352 		mp->b_prev = NULL;
1353 		mp->b_queue = NULL;
1354 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1355 		mutex_enter(&ill->ill_lock);
1356 	}
1357 	ill->ill_pending_ipif = NULL;
1358 
1359 	mutex_exit(&ill->ill_lock);
1360 }
1361 
1362 /*
1363  * Called in the conn close path and ill delete path
1364  */
1365 static void
1366 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1367 {
1368 	ipsq_t	*ipsq;
1369 	mblk_t	*prev;
1370 	mblk_t	*curr;
1371 	mblk_t	*next;
1372 	queue_t	*q;
1373 	mblk_t	*tmp_list = NULL;
1374 
1375 	ASSERT(IAM_WRITER_ILL(ill));
1376 	if (connp != NULL)
1377 		q = CONNP_TO_WQ(connp);
1378 	else
1379 		q = ill->ill_wq;
1380 
1381 	ipsq = ill->ill_phyint->phyint_ipsq;
1382 	/*
1383 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1384 	 * In the case of ioctl from a conn, there can be only 1 mp
1385 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1386 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1387 	 * ioctls meant for this ill form conn's are not flushed. They will
1388 	 * be processed during ipsq_exit and will not find the ill and will
1389 	 * return error.
1390 	 */
1391 	mutex_enter(&ipsq->ipsq_lock);
1392 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1393 	    curr = next) {
1394 		next = curr->b_next;
1395 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1396 			/* Unlink the mblk from the pending mp list */
1397 			if (prev != NULL) {
1398 				prev->b_next = curr->b_next;
1399 			} else {
1400 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1401 				ipsq->ipsq_xopq_mphead = curr->b_next;
1402 			}
1403 			if (ipsq->ipsq_xopq_mptail == curr)
1404 				ipsq->ipsq_xopq_mptail = prev;
1405 			/*
1406 			 * Create a temporary list and release the ipsq lock
1407 			 * New elements are added to the head of the tmp_list
1408 			 */
1409 			curr->b_next = tmp_list;
1410 			tmp_list = curr;
1411 		} else {
1412 			prev = curr;
1413 		}
1414 	}
1415 	mutex_exit(&ipsq->ipsq_lock);
1416 
1417 	while (tmp_list != NULL) {
1418 		curr = tmp_list;
1419 		tmp_list = curr->b_next;
1420 		curr->b_next = NULL;
1421 		curr->b_prev = NULL;
1422 		curr->b_queue = NULL;
1423 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1424 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1425 			    CONN_CLOSE : NO_COPYOUT, NULL);
1426 		} else {
1427 			/*
1428 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1429 			 * this can't be just inet_freemsg. we have to
1430 			 * restart it otherwise the thread will be stuck.
1431 			 */
1432 			inet_freemsg(curr);
1433 		}
1434 	}
1435 }
1436 
1437 /*
1438  * This conn has started closing. Cleanup any pending ioctl from this conn.
1439  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1440  */
1441 void
1442 conn_ioctl_cleanup(conn_t *connp)
1443 {
1444 	mblk_t *curr;
1445 	ipsq_t	*ipsq;
1446 	ill_t	*ill;
1447 	boolean_t refheld;
1448 
1449 	/*
1450 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1451 	 * ioctl has not yet started, the mp is pending in the list headed by
1452 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1453 	 * ipx_pending_mp. If the ioctl timed out in the streamhead but
1454 	 * is currently executing now the mp is not queued anywhere but
1455 	 * conn_oper_pending_ill is null. The conn close will wait
1456 	 * till the conn_ref drops to zero.
1457 	 */
1458 	mutex_enter(&connp->conn_lock);
1459 	ill = connp->conn_oper_pending_ill;
1460 	if (ill == NULL) {
1461 		mutex_exit(&connp->conn_lock);
1462 		return;
1463 	}
1464 
1465 	curr = ill_pending_mp_get(ill, &connp, 0);
1466 	if (curr != NULL) {
1467 		mutex_exit(&connp->conn_lock);
1468 		CONN_DEC_REF(connp);
1469 		inet_freemsg(curr);
1470 		return;
1471 	}
1472 	/*
1473 	 * We may not be able to refhold the ill if the ill/ipif
1474 	 * is changing. But we need to make sure that the ill will
1475 	 * not vanish. So we just bump up the ill_waiter count.
1476 	 */
1477 	refheld = ill_waiter_inc(ill);
1478 	mutex_exit(&connp->conn_lock);
1479 	if (refheld) {
1480 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1481 			ill_waiter_dcr(ill);
1482 			/*
1483 			 * Check whether this ioctl has started and is
1484 			 * pending. If it is not found there then check
1485 			 * whether this ioctl has not even started and is in
1486 			 * the ipsq_xopq list.
1487 			 */
1488 			if (!ipsq_pending_mp_cleanup(ill, connp))
1489 				ipsq_xopq_mp_cleanup(ill, connp);
1490 			ipsq = ill->ill_phyint->phyint_ipsq;
1491 			ipsq_exit(ipsq);
1492 			return;
1493 		}
1494 	}
1495 
1496 	/*
1497 	 * The ill is also closing and we could not bump up the
1498 	 * ill_waiter_count or we could not enter the ipsq. Leave
1499 	 * the cleanup to ill_delete
1500 	 */
1501 	mutex_enter(&connp->conn_lock);
1502 	while (connp->conn_oper_pending_ill != NULL)
1503 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1504 	mutex_exit(&connp->conn_lock);
1505 	if (refheld)
1506 		ill_waiter_dcr(ill);
1507 }
1508 
1509 /*
1510  * ipcl_walk function for cleaning up conn_*_ill fields.
1511  */
1512 static void
1513 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1514 {
1515 	ill_t	*ill = (ill_t *)arg;
1516 	ire_t	*ire;
1517 
1518 	mutex_enter(&connp->conn_lock);
1519 	if (connp->conn_multicast_ill == ill) {
1520 		/* Revert to late binding */
1521 		connp->conn_multicast_ill = NULL;
1522 	}
1523 	if (connp->conn_incoming_ill == ill)
1524 		connp->conn_incoming_ill = NULL;
1525 	if (connp->conn_outgoing_ill == ill)
1526 		connp->conn_outgoing_ill = NULL;
1527 	if (connp->conn_dhcpinit_ill == ill) {
1528 		connp->conn_dhcpinit_ill = NULL;
1529 		ASSERT(ill->ill_dhcpinit != 0);
1530 		atomic_dec_32(&ill->ill_dhcpinit);
1531 	}
1532 	if (connp->conn_ire_cache != NULL) {
1533 		ire = connp->conn_ire_cache;
1534 		/*
1535 		 * Source address selection makes it possible for IRE_CACHE
1536 		 * entries to be created with ire_stq coming from interface X
1537 		 * and ipif coming from interface Y.  Thus whenever interface
1538 		 * X goes down, remove all references to it by checking both
1539 		 * on ire_ipif and ire_stq.
1540 		 */
1541 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1542 		    (ire->ire_type == IRE_CACHE &&
1543 		    ire->ire_stq == ill->ill_wq)) {
1544 			connp->conn_ire_cache = NULL;
1545 			mutex_exit(&connp->conn_lock);
1546 			ire_refrele_notr(ire);
1547 			return;
1548 		}
1549 	}
1550 	mutex_exit(&connp->conn_lock);
1551 }
1552 
1553 static void
1554 ill_down_ipifs_tail(ill_t *ill)
1555 {
1556 	ipif_t	*ipif;
1557 
1558 	ASSERT(IAM_WRITER_ILL(ill));
1559 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1560 		ipif_non_duplicate(ipif);
1561 		ipif_down_tail(ipif);
1562 	}
1563 }
1564 
1565 /* ARGSUSED */
1566 void
1567 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1568 {
1569 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1570 	ill_down_ipifs_tail(q->q_ptr);
1571 	freemsg(mp);
1572 	ipsq_current_finish(ipsq);
1573 }
1574 
1575 /*
1576  * ill_down_start is called when we want to down this ill and bring it up again
1577  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1578  * all interfaces, but don't tear down any plumbing.
1579  */
1580 boolean_t
1581 ill_down_start(queue_t *q, mblk_t *mp)
1582 {
1583 	ill_t	*ill = q->q_ptr;
1584 	ipif_t	*ipif;
1585 
1586 	ASSERT(IAM_WRITER_ILL(ill));
1587 
1588 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1589 		(void) ipif_down(ipif, NULL, NULL);
1590 
1591 	ill_down(ill);
1592 
1593 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1594 
1595 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1596 
1597 	/*
1598 	 * Atomically test and add the pending mp if references are active.
1599 	 */
1600 	mutex_enter(&ill->ill_lock);
1601 	if (!ill_is_quiescent(ill)) {
1602 		/* call cannot fail since `conn_t *' argument is NULL */
1603 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1604 		    mp, ILL_DOWN);
1605 		mutex_exit(&ill->ill_lock);
1606 		return (B_FALSE);
1607 	}
1608 	mutex_exit(&ill->ill_lock);
1609 	return (B_TRUE);
1610 }
1611 
1612 static void
1613 ill_down(ill_t *ill)
1614 {
1615 	ip_stack_t	*ipst = ill->ill_ipst;
1616 
1617 	/* Blow off any IREs dependent on this ILL. */
1618 	ire_walk(ill_downi, ill, ipst);
1619 
1620 	/* Remove any conn_*_ill depending on this ill */
1621 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1622 }
1623 
1624 /*
1625  * ire_walk routine used to delete every IRE that depends on queues
1626  * associated with 'ill'.  (Always called as writer.)
1627  */
1628 static void
1629 ill_downi(ire_t *ire, char *ill_arg)
1630 {
1631 	ill_t	*ill = (ill_t *)ill_arg;
1632 
1633 	/*
1634 	 * Source address selection makes it possible for IRE_CACHE
1635 	 * entries to be created with ire_stq coming from interface X
1636 	 * and ipif coming from interface Y.  Thus whenever interface
1637 	 * X goes down, remove all references to it by checking both
1638 	 * on ire_ipif and ire_stq.
1639 	 */
1640 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1641 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1642 		ire_delete(ire);
1643 	}
1644 }
1645 
1646 /*
1647  * Remove ire/nce from the fastpath list.
1648  */
1649 void
1650 ill_fastpath_nack(ill_t *ill)
1651 {
1652 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1653 }
1654 
1655 /* Consume an M_IOCACK of the fastpath probe. */
1656 void
1657 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1658 {
1659 	mblk_t	*mp1 = mp;
1660 
1661 	/*
1662 	 * If this was the first attempt turn on the fastpath probing.
1663 	 */
1664 	mutex_enter(&ill->ill_lock);
1665 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1666 		ill->ill_dlpi_fastpath_state = IDS_OK;
1667 	mutex_exit(&ill->ill_lock);
1668 
1669 	/* Free the M_IOCACK mblk, hold on to the data */
1670 	mp = mp->b_cont;
1671 	freeb(mp1);
1672 	if (mp == NULL)
1673 		return;
1674 	if (mp->b_cont != NULL) {
1675 		/*
1676 		 * Update all IRE's or NCE's that are waiting for
1677 		 * fastpath update.
1678 		 */
1679 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1680 		mp1 = mp->b_cont;
1681 		freeb(mp);
1682 		mp = mp1;
1683 	} else {
1684 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1685 	}
1686 
1687 	freeb(mp);
1688 }
1689 
1690 /*
1691  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1692  * The data portion of the request is a dl_unitdata_req_t template for
1693  * what we would send downstream in the absence of a fastpath confirmation.
1694  */
1695 int
1696 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1697 {
1698 	struct iocblk	*ioc;
1699 	mblk_t	*mp;
1700 
1701 	if (dlur_mp == NULL)
1702 		return (EINVAL);
1703 
1704 	mutex_enter(&ill->ill_lock);
1705 	switch (ill->ill_dlpi_fastpath_state) {
1706 	case IDS_FAILED:
1707 		/*
1708 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1709 		 * support it.
1710 		 */
1711 		mutex_exit(&ill->ill_lock);
1712 		return (ENOTSUP);
1713 	case IDS_UNKNOWN:
1714 		/* This is the first probe */
1715 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1716 		break;
1717 	default:
1718 		break;
1719 	}
1720 	mutex_exit(&ill->ill_lock);
1721 
1722 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1723 		return (EAGAIN);
1724 
1725 	mp->b_cont = copyb(dlur_mp);
1726 	if (mp->b_cont == NULL) {
1727 		freeb(mp);
1728 		return (EAGAIN);
1729 	}
1730 
1731 	ioc = (struct iocblk *)mp->b_rptr;
1732 	ioc->ioc_count = msgdsize(mp->b_cont);
1733 
1734 	putnext(ill->ill_wq, mp);
1735 	return (0);
1736 }
1737 
1738 void
1739 ill_capability_probe(ill_t *ill)
1740 {
1741 	mblk_t	*mp;
1742 
1743 	ASSERT(IAM_WRITER_ILL(ill));
1744 
1745 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1746 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1747 		return;
1748 
1749 	/*
1750 	 * We are starting a new cycle of capability negotiation.
1751 	 * Free up the capab reset messages of any previous incarnation.
1752 	 * We will do a fresh allocation when we get the response to our probe
1753 	 */
1754 	if (ill->ill_capab_reset_mp != NULL) {
1755 		freemsg(ill->ill_capab_reset_mp);
1756 		ill->ill_capab_reset_mp = NULL;
1757 	}
1758 
1759 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1760 
1761 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1762 	if (mp == NULL)
1763 		return;
1764 
1765 	ill_capability_send(ill, mp);
1766 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1767 }
1768 
1769 void
1770 ill_capability_reset(ill_t *ill, boolean_t reneg)
1771 {
1772 	ASSERT(IAM_WRITER_ILL(ill));
1773 
1774 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1775 		return;
1776 
1777 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1778 
1779 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1780 	ill->ill_capab_reset_mp = NULL;
1781 	/*
1782 	 * We turn off all capabilities except those pertaining to
1783 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1784 	 * which will be turned off by the corresponding reset functions.
1785 	 */
1786 	ill->ill_capabilities &= ~(ILL_CAPAB_MDT | ILL_CAPAB_HCKSUM  |
1787 	    ILL_CAPAB_ZEROCOPY | ILL_CAPAB_AH | ILL_CAPAB_ESP);
1788 }
1789 
1790 static void
1791 ill_capability_reset_alloc(ill_t *ill)
1792 {
1793 	mblk_t *mp;
1794 	size_t	size = 0;
1795 	int	err;
1796 	dl_capability_req_t	*capb;
1797 
1798 	ASSERT(IAM_WRITER_ILL(ill));
1799 	ASSERT(ill->ill_capab_reset_mp == NULL);
1800 
1801 	if (ILL_MDT_CAPABLE(ill))
1802 		size += sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1803 
1804 	if (ILL_HCKSUM_CAPABLE(ill)) {
1805 		size += sizeof (dl_capability_sub_t) +
1806 		    sizeof (dl_capab_hcksum_t);
1807 	}
1808 
1809 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1810 		size += sizeof (dl_capability_sub_t) +
1811 		    sizeof (dl_capab_zerocopy_t);
1812 	}
1813 
1814 	if (ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) {
1815 		size += sizeof (dl_capability_sub_t);
1816 		size += ill_capability_ipsec_reset_size(ill, NULL, NULL,
1817 		    NULL, NULL);
1818 	}
1819 
1820 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1821 		size += sizeof (dl_capability_sub_t) +
1822 		    sizeof (dl_capab_dld_t);
1823 	}
1824 
1825 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1826 	    STR_NOSIG, &err);
1827 
1828 	mp->b_datap->db_type = M_PROTO;
1829 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1830 
1831 	capb = (dl_capability_req_t *)mp->b_rptr;
1832 	capb->dl_primitive = DL_CAPABILITY_REQ;
1833 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1834 	capb->dl_sub_length = size;
1835 
1836 	mp->b_wptr += sizeof (dl_capability_req_t);
1837 
1838 	/*
1839 	 * Each handler fills in the corresponding dl_capability_sub_t
1840 	 * inside the mblk,
1841 	 */
1842 	ill_capability_mdt_reset_fill(ill, mp);
1843 	ill_capability_hcksum_reset_fill(ill, mp);
1844 	ill_capability_zerocopy_reset_fill(ill, mp);
1845 	ill_capability_ipsec_reset_fill(ill, mp);
1846 	ill_capability_dld_reset_fill(ill, mp);
1847 
1848 	ill->ill_capab_reset_mp = mp;
1849 }
1850 
1851 static void
1852 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1853 {
1854 	dl_capab_id_t *id_ic;
1855 	uint_t sub_dl_cap = outers->dl_cap;
1856 	dl_capability_sub_t *inners;
1857 	uint8_t *capend;
1858 
1859 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1860 
1861 	/*
1862 	 * Note: range checks here are not absolutely sufficient to
1863 	 * make us robust against malformed messages sent by drivers;
1864 	 * this is in keeping with the rest of IP's dlpi handling.
1865 	 * (Remember, it's coming from something else in the kernel
1866 	 * address space)
1867 	 */
1868 
1869 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1870 	if (capend > mp->b_wptr) {
1871 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1872 		    "malformed sub-capability too long for mblk");
1873 		return;
1874 	}
1875 
1876 	id_ic = (dl_capab_id_t *)(outers + 1);
1877 
1878 	if (outers->dl_length < sizeof (*id_ic) ||
1879 	    (inners = &id_ic->id_subcap,
1880 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1881 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1882 		    "encapsulated capab type %d too long for mblk",
1883 		    inners->dl_cap);
1884 		return;
1885 	}
1886 
1887 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1888 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1889 		    "isn't as expected; pass-thru module(s) detected, "
1890 		    "discarding capability\n", inners->dl_cap));
1891 		return;
1892 	}
1893 
1894 	/* Process the encapsulated sub-capability */
1895 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1896 }
1897 
1898 /*
1899  * Process Multidata Transmit capability negotiation ack received from a
1900  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1901  * DL_CAPABILITY_ACK message.
1902  */
1903 static void
1904 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1905 {
1906 	mblk_t *nmp = NULL;
1907 	dl_capability_req_t *oc;
1908 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1909 	ill_mdt_capab_t **ill_mdt_capab;
1910 	uint_t sub_dl_cap = isub->dl_cap;
1911 	uint8_t *capend;
1912 
1913 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1914 
1915 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1916 
1917 	/*
1918 	 * Note: range checks here are not absolutely sufficient to
1919 	 * make us robust against malformed messages sent by drivers;
1920 	 * this is in keeping with the rest of IP's dlpi handling.
1921 	 * (Remember, it's coming from something else in the kernel
1922 	 * address space)
1923 	 */
1924 
1925 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1926 	if (capend > mp->b_wptr) {
1927 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1928 		    "malformed sub-capability too long for mblk");
1929 		return;
1930 	}
1931 
1932 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1933 
1934 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1935 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1936 		    "unsupported MDT sub-capability (version %d, expected %d)",
1937 		    mdt_ic->mdt_version, MDT_VERSION_2);
1938 		return;
1939 	}
1940 
1941 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1942 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1943 		    "capability isn't as expected; pass-thru module(s) "
1944 		    "detected, discarding capability\n"));
1945 		return;
1946 	}
1947 
1948 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1949 
1950 		if (*ill_mdt_capab == NULL) {
1951 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1952 			    KM_NOSLEEP);
1953 			if (*ill_mdt_capab == NULL) {
1954 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1955 				    "could not enable MDT version %d "
1956 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1957 				    ill->ill_name);
1958 				return;
1959 			}
1960 		}
1961 
1962 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1963 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1964 		    "header spaces, %d max pld bufs, %d span limit)\n",
1965 		    ill->ill_name, MDT_VERSION_2,
1966 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1967 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1968 
1969 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1970 		(*ill_mdt_capab)->ill_mdt_on = 1;
1971 		/*
1972 		 * Round the following values to the nearest 32-bit; ULP
1973 		 * may further adjust them to accomodate for additional
1974 		 * protocol headers.  We pass these values to ULP during
1975 		 * bind time.
1976 		 */
1977 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1978 		    roundup(mdt_ic->mdt_hdr_head, 4);
1979 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1980 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1981 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1982 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1983 
1984 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1985 	} else {
1986 		uint_t size;
1987 		uchar_t *rptr;
1988 
1989 		size = sizeof (dl_capability_req_t) +
1990 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1991 
1992 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1993 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1994 			    "could not enable MDT for %s (ENOMEM)\n",
1995 			    ill->ill_name);
1996 			return;
1997 		}
1998 
1999 		rptr = nmp->b_rptr;
2000 		/* initialize dl_capability_req_t */
2001 		oc = (dl_capability_req_t *)nmp->b_rptr;
2002 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2003 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2004 		    sizeof (dl_capab_mdt_t);
2005 		nmp->b_rptr += sizeof (dl_capability_req_t);
2006 
2007 		/* initialize dl_capability_sub_t */
2008 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2009 		nmp->b_rptr += sizeof (*isub);
2010 
2011 		/* initialize dl_capab_mdt_t */
2012 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2013 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2014 
2015 		nmp->b_rptr = rptr;
2016 
2017 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2018 		    "to enable MDT version %d\n", ill->ill_name,
2019 		    MDT_VERSION_2));
2020 
2021 		/* set ENABLE flag */
2022 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2023 
2024 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2025 		ill_capability_send(ill, nmp);
2026 	}
2027 }
2028 
2029 static void
2030 ill_capability_mdt_reset_fill(ill_t *ill, mblk_t *mp)
2031 {
2032 	dl_capab_mdt_t *mdt_subcap;
2033 	dl_capability_sub_t *dl_subcap;
2034 
2035 	if (!ILL_MDT_CAPABLE(ill))
2036 		return;
2037 
2038 	ASSERT(ill->ill_mdt_capab != NULL);
2039 
2040 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2041 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2042 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2043 
2044 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2045 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2046 	mdt_subcap->mdt_flags = 0;
2047 	mdt_subcap->mdt_hdr_head = 0;
2048 	mdt_subcap->mdt_hdr_tail = 0;
2049 
2050 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2051 }
2052 
2053 static void
2054 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
2055 {
2056 	dl_capability_sub_t *dl_subcap;
2057 
2058 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2059 		return;
2060 
2061 	/*
2062 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
2063 	 * initialized below since it is not used by DLD.
2064 	 */
2065 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2066 	dl_subcap->dl_cap = DL_CAPAB_DLD;
2067 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
2068 
2069 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
2070 }
2071 
2072 /*
2073  * Allocate an IPsec capability request which will be filled by our
2074  * caller to turn on support for one or more algorithms.
2075  */
2076 /* ARGSUSED */
2077 static mblk_t *
2078 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2079 {
2080 	mblk_t *nmp;
2081 	dl_capability_req_t	*ocap;
2082 	dl_capab_ipsec_t	*ocip;
2083 	dl_capab_ipsec_t	*icip;
2084 	uint8_t			*ptr;
2085 	icip = (dl_capab_ipsec_t *)(isub + 1);
2086 
2087 	/*
2088 	 * Allocate new mblk which will contain a new capability
2089 	 * request to enable the capabilities.
2090 	 */
2091 
2092 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2093 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2094 	if (nmp == NULL)
2095 		return (NULL);
2096 
2097 	ptr = nmp->b_rptr;
2098 
2099 	/* initialize dl_capability_req_t */
2100 	ocap = (dl_capability_req_t *)ptr;
2101 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2102 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2103 	ptr += sizeof (dl_capability_req_t);
2104 
2105 	/* initialize dl_capability_sub_t */
2106 	bcopy(isub, ptr, sizeof (*isub));
2107 	ptr += sizeof (*isub);
2108 
2109 	/* initialize dl_capab_ipsec_t */
2110 	ocip = (dl_capab_ipsec_t *)ptr;
2111 	bcopy(icip, ocip, sizeof (*icip));
2112 
2113 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2114 	return (nmp);
2115 }
2116 
2117 /*
2118  * Process an IPsec capability negotiation ack received from a DLS Provider.
2119  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2120  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2121  */
2122 static void
2123 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2124 {
2125 	dl_capab_ipsec_t	*icip;
2126 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2127 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2128 	uint_t cipher, nciphers;
2129 	mblk_t *nmp;
2130 	uint_t alg_len;
2131 	boolean_t need_sadb_dump;
2132 	uint_t sub_dl_cap = isub->dl_cap;
2133 	ill_ipsec_capab_t **ill_capab;
2134 	uint64_t ill_capab_flag;
2135 	uint8_t *capend, *ciphend;
2136 	boolean_t sadb_resync;
2137 
2138 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2139 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2140 
2141 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2142 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2143 		ill_capab_flag = ILL_CAPAB_AH;
2144 	} else {
2145 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2146 		ill_capab_flag = ILL_CAPAB_ESP;
2147 	}
2148 
2149 	/*
2150 	 * If the ill capability structure exists, then this incoming
2151 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2152 	 * If this is so, then we'd need to resynchronize the SADB
2153 	 * after re-enabling the offloaded ciphers.
2154 	 */
2155 	sadb_resync = (*ill_capab != NULL);
2156 
2157 	/*
2158 	 * Note: range checks here are not absolutely sufficient to
2159 	 * make us robust against malformed messages sent by drivers;
2160 	 * this is in keeping with the rest of IP's dlpi handling.
2161 	 * (Remember, it's coming from something else in the kernel
2162 	 * address space)
2163 	 */
2164 
2165 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2166 	if (capend > mp->b_wptr) {
2167 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2168 		    "malformed sub-capability too long for mblk");
2169 		return;
2170 	}
2171 
2172 	/*
2173 	 * There are two types of acks we process here:
2174 	 * 1. acks in reply to a (first form) generic capability req
2175 	 *    (no ENABLE flag set)
2176 	 * 2. acks in reply to a ENABLE capability req.
2177 	 *    (ENABLE flag set)
2178 	 *
2179 	 * We process the subcapability passed as argument as follows:
2180 	 * 1 do initializations
2181 	 *   1.1 initialize nmp = NULL
2182 	 *   1.2 set need_sadb_dump to B_FALSE
2183 	 * 2 for each cipher in subcapability:
2184 	 *   2.1 if ENABLE flag is set:
2185 	 *	2.1.1 update per-ill ipsec capabilities info
2186 	 *	2.1.2 set need_sadb_dump to B_TRUE
2187 	 *   2.2 if ENABLE flag is not set:
2188 	 *	2.2.1 if nmp is NULL:
2189 	 *		2.2.1.1 allocate and initialize nmp
2190 	 *		2.2.1.2 init current pos in nmp
2191 	 *	2.2.2 copy current cipher to current pos in nmp
2192 	 *	2.2.3 set ENABLE flag in nmp
2193 	 *	2.2.4 update current pos
2194 	 * 3 if nmp is not equal to NULL, send enable request
2195 	 *   3.1 send capability request
2196 	 * 4 if need_sadb_dump is B_TRUE
2197 	 *   4.1 enable promiscuous on/off notifications
2198 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2199 	 *	AH or ESP SA's to interface.
2200 	 */
2201 
2202 	nmp = NULL;
2203 	oalg = NULL;
2204 	need_sadb_dump = B_FALSE;
2205 	icip = (dl_capab_ipsec_t *)(isub + 1);
2206 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2207 
2208 	nciphers = icip->cip_nciphers;
2209 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2210 
2211 	if (ciphend > capend) {
2212 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2213 		    "too many ciphers for sub-capability len");
2214 		return;
2215 	}
2216 
2217 	for (cipher = 0; cipher < nciphers; cipher++) {
2218 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2219 
2220 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2221 			/*
2222 			 * TBD: when we provide a way to disable capabilities
2223 			 * from above, need to manage the request-pending state
2224 			 * and fail if we were not expecting this ACK.
2225 			 */
2226 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2227 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2228 
2229 			/*
2230 			 * Update IPsec capabilities for this ill
2231 			 */
2232 
2233 			if (*ill_capab == NULL) {
2234 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2235 				    ("ill_capability_ipsec_ack: "
2236 				    "allocating ipsec_capab for ill\n"));
2237 				*ill_capab = ill_ipsec_capab_alloc();
2238 
2239 				if (*ill_capab == NULL) {
2240 					cmn_err(CE_WARN,
2241 					    "ill_capability_ipsec_ack: "
2242 					    "could not enable IPsec Hardware "
2243 					    "acceleration for %s (ENOMEM)\n",
2244 					    ill->ill_name);
2245 					return;
2246 				}
2247 			}
2248 
2249 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2250 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2251 
2252 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2253 				cmn_err(CE_WARN,
2254 				    "ill_capability_ipsec_ack: "
2255 				    "malformed IPsec algorithm id %d",
2256 				    ialg->alg_prim);
2257 				continue;
2258 			}
2259 
2260 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2261 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2262 				    ialg->alg_prim);
2263 			} else {
2264 				ipsec_capab_algparm_t *alp;
2265 
2266 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2267 				    ialg->alg_prim);
2268 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2269 				    ialg->alg_prim)) {
2270 					cmn_err(CE_WARN,
2271 					    "ill_capability_ipsec_ack: "
2272 					    "no space for IPsec alg id %d",
2273 					    ialg->alg_prim);
2274 					continue;
2275 				}
2276 				alp = &((*ill_capab)->encr_algparm[
2277 				    ialg->alg_prim]);
2278 				alp->minkeylen = ialg->alg_minbits;
2279 				alp->maxkeylen = ialg->alg_maxbits;
2280 			}
2281 			ill->ill_capabilities |= ill_capab_flag;
2282 			/*
2283 			 * indicate that a capability was enabled, which
2284 			 * will be used below to kick off a SADB dump
2285 			 * to the ill.
2286 			 */
2287 			need_sadb_dump = B_TRUE;
2288 		} else {
2289 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2290 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2291 			    ialg->alg_prim));
2292 
2293 			if (nmp == NULL) {
2294 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2295 				if (nmp == NULL) {
2296 					/*
2297 					 * Sending the PROMISC_ON/OFF
2298 					 * notification request failed.
2299 					 * We cannot enable the algorithms
2300 					 * since the Provider will not
2301 					 * notify IP of promiscous mode
2302 					 * changes, which could lead
2303 					 * to leakage of packets.
2304 					 */
2305 					cmn_err(CE_WARN,
2306 					    "ill_capability_ipsec_ack: "
2307 					    "could not enable IPsec Hardware "
2308 					    "acceleration for %s (ENOMEM)\n",
2309 					    ill->ill_name);
2310 					return;
2311 				}
2312 				/* ptr to current output alg specifier */
2313 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2314 			}
2315 
2316 			/*
2317 			 * Copy current alg specifier, set ENABLE
2318 			 * flag, and advance to next output alg.
2319 			 * For now we enable all IPsec capabilities.
2320 			 */
2321 			ASSERT(oalg != NULL);
2322 			bcopy(ialg, oalg, alg_len);
2323 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2324 			nmp->b_wptr += alg_len;
2325 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2326 		}
2327 
2328 		/* move to next input algorithm specifier */
2329 		ialg = (dl_capab_ipsec_alg_t *)
2330 		    ((char *)ialg + alg_len);
2331 	}
2332 
2333 	if (nmp != NULL)
2334 		/*
2335 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2336 		 * IPsec hardware acceleration.
2337 		 */
2338 		ill_capability_send(ill, nmp);
2339 
2340 	if (need_sadb_dump)
2341 		/*
2342 		 * An acknowledgement corresponding to a request to
2343 		 * enable acceleration was received, notify SADB.
2344 		 */
2345 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2346 }
2347 
2348 /*
2349  * Given an mblk with enough space in it, create sub-capability entries for
2350  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2351  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2352  * in preparation for the reset the DL_CAPABILITY_REQ message.
2353  */
2354 static void
2355 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2356     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2357 {
2358 	dl_capab_ipsec_t *oipsec;
2359 	dl_capab_ipsec_alg_t *oalg;
2360 	dl_capability_sub_t *dl_subcap;
2361 	int i, k;
2362 
2363 	ASSERT(nciphers > 0);
2364 	ASSERT(ill_cap != NULL);
2365 	ASSERT(mp != NULL);
2366 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2367 
2368 	/* dl_capability_sub_t for "stype" */
2369 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2370 	dl_subcap->dl_cap = stype;
2371 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2372 	mp->b_wptr += sizeof (dl_capability_sub_t);
2373 
2374 	/* dl_capab_ipsec_t for "stype" */
2375 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2376 	oipsec->cip_version = 1;
2377 	oipsec->cip_nciphers = nciphers;
2378 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2379 
2380 	/* create entries for "stype" AUTH ciphers */
2381 	for (i = 0; i < ill_cap->algs_size; i++) {
2382 		for (k = 0; k < BITSPERBYTE; k++) {
2383 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2384 				continue;
2385 
2386 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2387 			bzero((void *)oalg, sizeof (*oalg));
2388 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2389 			oalg->alg_prim = k + (BITSPERBYTE * i);
2390 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2391 		}
2392 	}
2393 	/* create entries for "stype" ENCR ciphers */
2394 	for (i = 0; i < ill_cap->algs_size; i++) {
2395 		for (k = 0; k < BITSPERBYTE; k++) {
2396 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2397 				continue;
2398 
2399 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2400 			bzero((void *)oalg, sizeof (*oalg));
2401 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2402 			oalg->alg_prim = k + (BITSPERBYTE * i);
2403 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2404 		}
2405 	}
2406 }
2407 
2408 /*
2409  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2410  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2411  * POPC instruction, but our macro is more flexible for an arbitrary length
2412  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2413  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2414  * stays that way, we can reduce the number of iterations required.
2415  */
2416 #define	COUNT_1S(val, sum) {					\
2417 	uint8_t x = val & 0xff;					\
2418 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2419 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2420 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2421 }
2422 
2423 /* ARGSUSED */
2424 static int
2425 ill_capability_ipsec_reset_size(ill_t *ill, int *ah_cntp, int *ah_lenp,
2426     int *esp_cntp, int *esp_lenp)
2427 {
2428 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2429 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2430 	uint64_t ill_capabilities = ill->ill_capabilities;
2431 	int ah_cnt = 0, esp_cnt = 0;
2432 	int ah_len = 0, esp_len = 0;
2433 	int i, size = 0;
2434 
2435 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2436 		return (0);
2437 
2438 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2439 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2440 
2441 	/* Find out the number of ciphers for AH */
2442 	if (cap_ah != NULL) {
2443 		for (i = 0; i < cap_ah->algs_size; i++) {
2444 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2445 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2446 		}
2447 		if (ah_cnt > 0) {
2448 			size += sizeof (dl_capability_sub_t) +
2449 			    sizeof (dl_capab_ipsec_t);
2450 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2451 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2452 			size += ah_len;
2453 		}
2454 	}
2455 
2456 	/* Find out the number of ciphers for ESP */
2457 	if (cap_esp != NULL) {
2458 		for (i = 0; i < cap_esp->algs_size; i++) {
2459 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2460 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2461 		}
2462 		if (esp_cnt > 0) {
2463 			size += sizeof (dl_capability_sub_t) +
2464 			    sizeof (dl_capab_ipsec_t);
2465 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2466 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2467 			size += esp_len;
2468 		}
2469 	}
2470 
2471 	if (ah_cntp != NULL)
2472 		*ah_cntp = ah_cnt;
2473 	if (ah_lenp != NULL)
2474 		*ah_lenp = ah_len;
2475 	if (esp_cntp != NULL)
2476 		*esp_cntp = esp_cnt;
2477 	if (esp_lenp != NULL)
2478 		*esp_lenp = esp_len;
2479 
2480 	return (size);
2481 }
2482 
2483 /* ARGSUSED */
2484 static void
2485 ill_capability_ipsec_reset_fill(ill_t *ill, mblk_t *mp)
2486 {
2487 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2488 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2489 	int ah_cnt = 0, esp_cnt = 0;
2490 	int ah_len = 0, esp_len = 0;
2491 	int size;
2492 
2493 	size = ill_capability_ipsec_reset_size(ill, &ah_cnt, &ah_len,
2494 	    &esp_cnt, &esp_len);
2495 	if (size == 0)
2496 		return;
2497 
2498 	/*
2499 	 * Clear the capability flags for IPsec HA but retain the ill
2500 	 * capability structures since it's possible that another thread
2501 	 * is still referring to them.  The structures only get deallocated
2502 	 * when we destroy the ill.
2503 	 *
2504 	 * Various places check the flags to see if the ill is capable of
2505 	 * hardware acceleration, and by clearing them we ensure that new
2506 	 * outbound IPsec packets are sent down encrypted.
2507 	 */
2508 
2509 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2510 	if (ah_cnt > 0) {
2511 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2512 		    cap_ah, mp);
2513 	}
2514 
2515 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2516 	if (esp_cnt > 0) {
2517 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2518 		    cap_esp, mp);
2519 	}
2520 
2521 	/*
2522 	 * At this point we've composed a bunch of sub-capabilities to be
2523 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2524 	 * by the caller.  Upon receiving this reset message, the driver
2525 	 * must stop inbound decryption (by destroying all inbound SAs)
2526 	 * and let the corresponding packets come in encrypted.
2527 	 */
2528 }
2529 
2530 static void
2531 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2532     boolean_t encapsulated)
2533 {
2534 	boolean_t legacy = B_FALSE;
2535 
2536 	/*
2537 	 * Note that only the following two sub-capabilities may be
2538 	 * considered as "legacy", since their original definitions
2539 	 * do not incorporate the dl_mid_t module ID token, and hence
2540 	 * may require the use of the wrapper sub-capability.
2541 	 */
2542 	switch (subp->dl_cap) {
2543 	case DL_CAPAB_IPSEC_AH:
2544 	case DL_CAPAB_IPSEC_ESP:
2545 		legacy = B_TRUE;
2546 		break;
2547 	}
2548 
2549 	/*
2550 	 * For legacy sub-capabilities which don't incorporate a queue_t
2551 	 * pointer in their structures, discard them if we detect that
2552 	 * there are intermediate modules in between IP and the driver.
2553 	 */
2554 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2555 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2556 		    "%d discarded; %d module(s) present below IP\n",
2557 		    subp->dl_cap, ill->ill_lmod_cnt));
2558 		return;
2559 	}
2560 
2561 	switch (subp->dl_cap) {
2562 	case DL_CAPAB_IPSEC_AH:
2563 	case DL_CAPAB_IPSEC_ESP:
2564 		ill_capability_ipsec_ack(ill, mp, subp);
2565 		break;
2566 	case DL_CAPAB_MDT:
2567 		ill_capability_mdt_ack(ill, mp, subp);
2568 		break;
2569 	case DL_CAPAB_HCKSUM:
2570 		ill_capability_hcksum_ack(ill, mp, subp);
2571 		break;
2572 	case DL_CAPAB_ZEROCOPY:
2573 		ill_capability_zerocopy_ack(ill, mp, subp);
2574 		break;
2575 	case DL_CAPAB_DLD:
2576 		ill_capability_dld_ack(ill, mp, subp);
2577 		break;
2578 	default:
2579 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2580 		    subp->dl_cap));
2581 	}
2582 }
2583 
2584 /*
2585  * Process a hardware checksum offload capability negotiation ack received
2586  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2587  * of a DL_CAPABILITY_ACK message.
2588  */
2589 static void
2590 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2591 {
2592 	dl_capability_req_t	*ocap;
2593 	dl_capab_hcksum_t	*ihck, *ohck;
2594 	ill_hcksum_capab_t	**ill_hcksum;
2595 	mblk_t			*nmp = NULL;
2596 	uint_t			sub_dl_cap = isub->dl_cap;
2597 	uint8_t			*capend;
2598 
2599 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
2600 
2601 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
2602 
2603 	/*
2604 	 * Note: range checks here are not absolutely sufficient to
2605 	 * make us robust against malformed messages sent by drivers;
2606 	 * this is in keeping with the rest of IP's dlpi handling.
2607 	 * (Remember, it's coming from something else in the kernel
2608 	 * address space)
2609 	 */
2610 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2611 	if (capend > mp->b_wptr) {
2612 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2613 		    "malformed sub-capability too long for mblk");
2614 		return;
2615 	}
2616 
2617 	/*
2618 	 * There are two types of acks we process here:
2619 	 * 1. acks in reply to a (first form) generic capability req
2620 	 *    (no ENABLE flag set)
2621 	 * 2. acks in reply to a ENABLE capability req.
2622 	 *    (ENABLE flag set)
2623 	 */
2624 	ihck = (dl_capab_hcksum_t *)(isub + 1);
2625 
2626 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
2627 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
2628 		    "unsupported hardware checksum "
2629 		    "sub-capability (version %d, expected %d)",
2630 		    ihck->hcksum_version, HCKSUM_VERSION_1);
2631 		return;
2632 	}
2633 
2634 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
2635 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
2636 		    "checksum capability isn't as expected; pass-thru "
2637 		    "module(s) detected, discarding capability\n"));
2638 		return;
2639 	}
2640 
2641 #define	CURR_HCKSUM_CAPAB				\
2642 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
2643 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
2644 
2645 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
2646 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
2647 		/* do ENABLE processing */
2648 		if (*ill_hcksum == NULL) {
2649 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
2650 			    KM_NOSLEEP);
2651 
2652 			if (*ill_hcksum == NULL) {
2653 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2654 				    "could not enable hcksum version %d "
2655 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
2656 				    ill->ill_name);
2657 				return;
2658 			}
2659 		}
2660 
2661 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
2662 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
2663 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
2664 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
2665 		    "has enabled hardware checksumming\n ",
2666 		    ill->ill_name));
2667 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
2668 		/*
2669 		 * Enabling hardware checksum offload
2670 		 * Currently IP supports {TCP,UDP}/IPv4
2671 		 * partial and full cksum offload and
2672 		 * IPv4 header checksum offload.
2673 		 * Allocate new mblk which will
2674 		 * contain a new capability request
2675 		 * to enable hardware checksum offload.
2676 		 */
2677 		uint_t	size;
2678 		uchar_t	*rptr;
2679 
2680 		size = sizeof (dl_capability_req_t) +
2681 		    sizeof (dl_capability_sub_t) + isub->dl_length;
2682 
2683 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2684 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2685 			    "could not enable hardware cksum for %s (ENOMEM)\n",
2686 			    ill->ill_name);
2687 			return;
2688 		}
2689 
2690 		rptr = nmp->b_rptr;
2691 		/* initialize dl_capability_req_t */
2692 		ocap = (dl_capability_req_t *)nmp->b_rptr;
2693 		ocap->dl_sub_offset =
2694 		    sizeof (dl_capability_req_t);
2695 		ocap->dl_sub_length =
2696 		    sizeof (dl_capability_sub_t) +
2697 		    isub->dl_length;
2698 		nmp->b_rptr += sizeof (dl_capability_req_t);
2699 
2700 		/* initialize dl_capability_sub_t */
2701 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2702 		nmp->b_rptr += sizeof (*isub);
2703 
2704 		/* initialize dl_capab_hcksum_t */
2705 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
2706 		bcopy(ihck, ohck, sizeof (*ihck));
2707 
2708 		nmp->b_rptr = rptr;
2709 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2710 
2711 		/* Set ENABLE flag */
2712 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
2713 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
2714 
2715 		/*
2716 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2717 		 * hardware checksum acceleration.
2718 		 */
2719 		ill_capability_send(ill, nmp);
2720 	} else {
2721 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
2722 		    "advertised %x hardware checksum capability flags\n",
2723 		    ill->ill_name, ihck->hcksum_txflags));
2724 	}
2725 }
2726 
2727 static void
2728 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
2729 {
2730 	dl_capab_hcksum_t *hck_subcap;
2731 	dl_capability_sub_t *dl_subcap;
2732 
2733 	if (!ILL_HCKSUM_CAPABLE(ill))
2734 		return;
2735 
2736 	ASSERT(ill->ill_hcksum_capab != NULL);
2737 
2738 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2739 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
2740 	dl_subcap->dl_length = sizeof (*hck_subcap);
2741 
2742 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
2743 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
2744 	hck_subcap->hcksum_txflags = 0;
2745 
2746 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
2747 }
2748 
2749 static void
2750 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2751 {
2752 	mblk_t *nmp = NULL;
2753 	dl_capability_req_t *oc;
2754 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
2755 	ill_zerocopy_capab_t **ill_zerocopy_capab;
2756 	uint_t sub_dl_cap = isub->dl_cap;
2757 	uint8_t *capend;
2758 
2759 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
2760 
2761 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
2762 
2763 	/*
2764 	 * Note: range checks here are not absolutely sufficient to
2765 	 * make us robust against malformed messages sent by drivers;
2766 	 * this is in keeping with the rest of IP's dlpi handling.
2767 	 * (Remember, it's coming from something else in the kernel
2768 	 * address space)
2769 	 */
2770 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2771 	if (capend > mp->b_wptr) {
2772 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2773 		    "malformed sub-capability too long for mblk");
2774 		return;
2775 	}
2776 
2777 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
2778 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
2779 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
2780 		    "unsupported ZEROCOPY sub-capability (version %d, "
2781 		    "expected %d)", zc_ic->zerocopy_version,
2782 		    ZEROCOPY_VERSION_1);
2783 		return;
2784 	}
2785 
2786 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
2787 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
2788 		    "capability isn't as expected; pass-thru module(s) "
2789 		    "detected, discarding capability\n"));
2790 		return;
2791 	}
2792 
2793 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
2794 		if (*ill_zerocopy_capab == NULL) {
2795 			*ill_zerocopy_capab =
2796 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
2797 			    KM_NOSLEEP);
2798 
2799 			if (*ill_zerocopy_capab == NULL) {
2800 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2801 				    "could not enable Zero-copy version %d "
2802 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
2803 				    ill->ill_name);
2804 				return;
2805 			}
2806 		}
2807 
2808 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
2809 		    "supports Zero-copy version %d\n", ill->ill_name,
2810 		    ZEROCOPY_VERSION_1));
2811 
2812 		(*ill_zerocopy_capab)->ill_zerocopy_version =
2813 		    zc_ic->zerocopy_version;
2814 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
2815 		    zc_ic->zerocopy_flags;
2816 
2817 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
2818 	} else {
2819 		uint_t size;
2820 		uchar_t *rptr;
2821 
2822 		size = sizeof (dl_capability_req_t) +
2823 		    sizeof (dl_capability_sub_t) +
2824 		    sizeof (dl_capab_zerocopy_t);
2825 
2826 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2827 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2828 			    "could not enable zerocopy for %s (ENOMEM)\n",
2829 			    ill->ill_name);
2830 			return;
2831 		}
2832 
2833 		rptr = nmp->b_rptr;
2834 		/* initialize dl_capability_req_t */
2835 		oc = (dl_capability_req_t *)rptr;
2836 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2837 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2838 		    sizeof (dl_capab_zerocopy_t);
2839 		rptr += sizeof (dl_capability_req_t);
2840 
2841 		/* initialize dl_capability_sub_t */
2842 		bcopy(isub, rptr, sizeof (*isub));
2843 		rptr += sizeof (*isub);
2844 
2845 		/* initialize dl_capab_zerocopy_t */
2846 		zc_oc = (dl_capab_zerocopy_t *)rptr;
2847 		*zc_oc = *zc_ic;
2848 
2849 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
2850 		    "to enable zero-copy version %d\n", ill->ill_name,
2851 		    ZEROCOPY_VERSION_1));
2852 
2853 		/* set VMSAFE_MEM flag */
2854 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
2855 
2856 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
2857 		ill_capability_send(ill, nmp);
2858 	}
2859 }
2860 
2861 static void
2862 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
2863 {
2864 	dl_capab_zerocopy_t *zerocopy_subcap;
2865 	dl_capability_sub_t *dl_subcap;
2866 
2867 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
2868 		return;
2869 
2870 	ASSERT(ill->ill_zerocopy_capab != NULL);
2871 
2872 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2873 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
2874 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
2875 
2876 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
2877 	zerocopy_subcap->zerocopy_version =
2878 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
2879 	zerocopy_subcap->zerocopy_flags = 0;
2880 
2881 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
2882 }
2883 
2884 /*
2885  * DLD capability
2886  * Refer to dld.h for more information regarding the purpose and usage
2887  * of this capability.
2888  */
2889 static void
2890 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2891 {
2892 	dl_capab_dld_t		*dld_ic, dld;
2893 	uint_t			sub_dl_cap = isub->dl_cap;
2894 	uint8_t			*capend;
2895 	ill_dld_capab_t		*idc;
2896 
2897 	ASSERT(IAM_WRITER_ILL(ill));
2898 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
2899 
2900 	/*
2901 	 * Note: range checks here are not absolutely sufficient to
2902 	 * make us robust against malformed messages sent by drivers;
2903 	 * this is in keeping with the rest of IP's dlpi handling.
2904 	 * (Remember, it's coming from something else in the kernel
2905 	 * address space)
2906 	 */
2907 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2908 	if (capend > mp->b_wptr) {
2909 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
2910 		    "malformed sub-capability too long for mblk");
2911 		return;
2912 	}
2913 	dld_ic = (dl_capab_dld_t *)(isub + 1);
2914 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
2915 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
2916 		    "unsupported DLD sub-capability (version %d, "
2917 		    "expected %d)", dld_ic->dld_version,
2918 		    DLD_CURRENT_VERSION);
2919 		return;
2920 	}
2921 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
2922 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
2923 		    "capability isn't as expected; pass-thru module(s) "
2924 		    "detected, discarding capability\n"));
2925 		return;
2926 	}
2927 
2928 	/*
2929 	 * Copy locally to ensure alignment.
2930 	 */
2931 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
2932 
2933 	if ((idc = ill->ill_dld_capab) == NULL) {
2934 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
2935 		if (idc == NULL) {
2936 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
2937 			    "could not enable DLD version %d "
2938 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
2939 			    ill->ill_name);
2940 			return;
2941 		}
2942 		ill->ill_dld_capab = idc;
2943 	}
2944 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
2945 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
2946 	ip1dbg(("ill_capability_dld_ack: interface %s "
2947 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
2948 
2949 	ill_capability_dld_enable(ill);
2950 }
2951 
2952 /*
2953  * Typically capability negotiation between IP and the driver happens via
2954  * DLPI message exchange. However GLD also offers a direct function call
2955  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
2956  * But arbitrary function calls into IP or GLD are not permitted, since both
2957  * of them are protected by their own perimeter mechanism. The perimeter can
2958  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
2959  * these perimeters is IP -> MAC. Thus for example to enable the squeue
2960  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
2961  * to enter the mac perimeter and then do the direct function calls into
2962  * GLD to enable squeue polling. The ring related callbacks from the mac into
2963  * the stack to add, bind, quiesce, restart or cleanup a ring are all
2964  * protected by the mac perimeter.
2965  */
2966 static void
2967 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
2968 {
2969 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2970 	int			err;
2971 
2972 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
2973 	    DLD_ENABLE);
2974 	ASSERT(err == 0);
2975 }
2976 
2977 static void
2978 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
2979 {
2980 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2981 	int			err;
2982 
2983 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
2984 	    DLD_DISABLE);
2985 	ASSERT(err == 0);
2986 }
2987 
2988 boolean_t
2989 ill_mac_perim_held(ill_t *ill)
2990 {
2991 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2992 
2993 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
2994 	    DLD_QUERY));
2995 }
2996 
2997 static void
2998 ill_capability_direct_enable(ill_t *ill)
2999 {
3000 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3001 	ill_dld_direct_t	*idd = &idc->idc_direct;
3002 	dld_capab_direct_t	direct;
3003 	int			rc;
3004 
3005 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3006 
3007 	bzero(&direct, sizeof (direct));
3008 	direct.di_rx_cf = (uintptr_t)ip_input;
3009 	direct.di_rx_ch = ill;
3010 
3011 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
3012 	    DLD_ENABLE);
3013 	if (rc == 0) {
3014 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
3015 		idd->idd_tx_dh = direct.di_tx_dh;
3016 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
3017 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
3018 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
3019 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
3020 		ASSERT(idd->idd_tx_cb_df != NULL);
3021 		ASSERT(idd->idd_tx_fctl_df != NULL);
3022 		ASSERT(idd->idd_tx_df != NULL);
3023 		/*
3024 		 * One time registration of flow enable callback function
3025 		 */
3026 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
3027 		    ill_flow_enable, ill);
3028 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
3029 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
3030 	} else {
3031 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
3032 		    "capability, rc = %d\n", rc);
3033 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
3034 	}
3035 }
3036 
3037 static void
3038 ill_capability_poll_enable(ill_t *ill)
3039 {
3040 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3041 	dld_capab_poll_t	poll;
3042 	int			rc;
3043 
3044 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3045 
3046 	bzero(&poll, sizeof (poll));
3047 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
3048 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
3049 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
3050 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
3051 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
3052 	poll.poll_ring_ch = ill;
3053 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
3054 	    DLD_ENABLE);
3055 	if (rc == 0) {
3056 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
3057 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
3058 	} else {
3059 		ip1dbg(("warning: could not enable POLL "
3060 		    "capability, rc = %d\n", rc));
3061 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
3062 	}
3063 }
3064 
3065 /*
3066  * Enable the LSO capability.
3067  */
3068 static void
3069 ill_capability_lso_enable(ill_t *ill)
3070 {
3071 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
3072 	dld_capab_lso_t	lso;
3073 	int rc;
3074 
3075 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3076 
3077 	if (ill->ill_lso_capab == NULL) {
3078 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3079 		    KM_NOSLEEP);
3080 		if (ill->ill_lso_capab == NULL) {
3081 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
3082 			    "could not enable LSO for %s (ENOMEM)\n",
3083 			    ill->ill_name);
3084 			return;
3085 		}
3086 	}
3087 
3088 	bzero(&lso, sizeof (lso));
3089 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
3090 	    DLD_ENABLE)) == 0) {
3091 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
3092 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
3093 		ill->ill_capabilities |= ILL_CAPAB_DLD_LSO;
3094 		ip1dbg(("ill_capability_lso_enable: interface %s "
3095 		    "has enabled LSO\n ", ill->ill_name));
3096 	} else {
3097 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
3098 		ill->ill_lso_capab = NULL;
3099 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
3100 	}
3101 }
3102 
3103 static void
3104 ill_capability_dld_enable(ill_t *ill)
3105 {
3106 	mac_perim_handle_t mph;
3107 
3108 	ASSERT(IAM_WRITER_ILL(ill));
3109 
3110 	if (ill->ill_isv6)
3111 		return;
3112 
3113 	ill_mac_perim_enter(ill, &mph);
3114 	if (!ill->ill_isv6) {
3115 		ill_capability_direct_enable(ill);
3116 		ill_capability_poll_enable(ill);
3117 		ill_capability_lso_enable(ill);
3118 	}
3119 	ill->ill_capabilities |= ILL_CAPAB_DLD;
3120 	ill_mac_perim_exit(ill, mph);
3121 }
3122 
3123 static void
3124 ill_capability_dld_disable(ill_t *ill)
3125 {
3126 	ill_dld_capab_t	*idc;
3127 	ill_dld_direct_t *idd;
3128 	mac_perim_handle_t	mph;
3129 
3130 	ASSERT(IAM_WRITER_ILL(ill));
3131 
3132 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
3133 		return;
3134 
3135 	ill_mac_perim_enter(ill, &mph);
3136 
3137 	idc = ill->ill_dld_capab;
3138 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
3139 		/*
3140 		 * For performance we avoid locks in the transmit data path
3141 		 * and don't maintain a count of the number of threads using
3142 		 * direct calls. Thus some threads could be using direct
3143 		 * transmit calls to GLD, even after the capability mechanism
3144 		 * turns it off. This is still safe since the handles used in
3145 		 * the direct calls continue to be valid until the unplumb is
3146 		 * completed. Remove the callback that was added (1-time) at
3147 		 * capab enable time.
3148 		 */
3149 		mutex_enter(&ill->ill_lock);
3150 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
3151 		mutex_exit(&ill->ill_lock);
3152 		if (ill->ill_flownotify_mh != NULL) {
3153 			idd = &idc->idc_direct;
3154 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
3155 			    ill->ill_flownotify_mh);
3156 			ill->ill_flownotify_mh = NULL;
3157 		}
3158 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
3159 		    NULL, DLD_DISABLE);
3160 	}
3161 
3162 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
3163 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
3164 		ip_squeue_clean_all(ill);
3165 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
3166 		    NULL, DLD_DISABLE);
3167 	}
3168 
3169 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_LSO) != 0) {
3170 		ASSERT(ill->ill_lso_capab != NULL);
3171 		/*
3172 		 * Clear the capability flag for LSO but retain the
3173 		 * ill_lso_capab structure since it's possible that another
3174 		 * thread is still referring to it.  The structure only gets
3175 		 * deallocated when we destroy the ill.
3176 		 */
3177 
3178 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_LSO;
3179 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
3180 		    NULL, DLD_DISABLE);
3181 	}
3182 
3183 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
3184 	ill_mac_perim_exit(ill, mph);
3185 }
3186 
3187 /*
3188  * Capability Negotiation protocol
3189  *
3190  * We don't wait for DLPI capability operations to finish during interface
3191  * bringup or teardown. Doing so would introduce more asynchrony and the
3192  * interface up/down operations will need multiple return and restarts.
3193  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
3194  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
3195  * exclusive operation won't start until the DLPI operations of the previous
3196  * exclusive operation complete.
3197  *
3198  * The capability state machine is shown below.
3199  *
3200  * state		next state		event, action
3201  *
3202  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
3203  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
3204  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
3205  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
3206  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
3207  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
3208  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
3209  *						    ill_capability_probe.
3210  */
3211 
3212 /*
3213  * Dedicated thread started from ip_stack_init that handles capability
3214  * disable. This thread ensures the taskq dispatch does not fail by waiting
3215  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
3216  * that direct calls to DLD are done in a cv_waitable context.
3217  */
3218 void
3219 ill_taskq_dispatch(ip_stack_t *ipst)
3220 {
3221 	callb_cpr_t cprinfo;
3222 	char 	name[64];
3223 	mblk_t	*mp;
3224 
3225 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
3226 	    ipst->ips_netstack->netstack_stackid);
3227 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
3228 	    name);
3229 	mutex_enter(&ipst->ips_capab_taskq_lock);
3230 
3231 	for (;;) {
3232 		mp = ipst->ips_capab_taskq_head;
3233 		while (mp != NULL) {
3234 			ipst->ips_capab_taskq_head = mp->b_next;
3235 			if (ipst->ips_capab_taskq_head == NULL)
3236 				ipst->ips_capab_taskq_tail = NULL;
3237 			mutex_exit(&ipst->ips_capab_taskq_lock);
3238 			mp->b_next = NULL;
3239 
3240 			VERIFY(taskq_dispatch(system_taskq,
3241 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
3242 			mutex_enter(&ipst->ips_capab_taskq_lock);
3243 			mp = ipst->ips_capab_taskq_head;
3244 		}
3245 
3246 		if (ipst->ips_capab_taskq_quit)
3247 			break;
3248 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3249 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
3250 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
3251 	}
3252 	VERIFY(ipst->ips_capab_taskq_head == NULL);
3253 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
3254 	CALLB_CPR_EXIT(&cprinfo);
3255 	thread_exit();
3256 }
3257 
3258 /*
3259  * Consume a new-style hardware capabilities negotiation ack.
3260  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
3261  */
3262 static void
3263 ill_capability_ack_thr(void *arg)
3264 {
3265 	mblk_t	*mp = arg;
3266 	dl_capability_ack_t *capp;
3267 	dl_capability_sub_t *subp, *endp;
3268 	ill_t	*ill;
3269 	boolean_t reneg;
3270 
3271 	ill = (ill_t *)mp->b_prev;
3272 	mp->b_prev = NULL;
3273 
3274 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
3275 
3276 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
3277 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
3278 		/*
3279 		 * We have received the ack for our DL_CAPAB reset request.
3280 		 * There isnt' anything in the message that needs processing.
3281 		 * All message based capabilities have been disabled, now
3282 		 * do the function call based capability disable.
3283 		 */
3284 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
3285 		ill_capability_dld_disable(ill);
3286 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
3287 		if (reneg)
3288 			ill_capability_probe(ill);
3289 		goto done;
3290 	}
3291 
3292 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
3293 		ill->ill_dlpi_capab_state = IDCS_OK;
3294 
3295 	capp = (dl_capability_ack_t *)mp->b_rptr;
3296 
3297 	if (capp->dl_sub_length == 0) {
3298 		/* no new-style capabilities */
3299 		goto done;
3300 	}
3301 
3302 	/* make sure the driver supplied correct dl_sub_length */
3303 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3304 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3305 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3306 		goto done;
3307 	}
3308 
3309 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3310 	/*
3311 	 * There are sub-capabilities. Process the ones we know about.
3312 	 * Loop until we don't have room for another sub-cap header..
3313 	 */
3314 	for (subp = SC(capp, capp->dl_sub_offset),
3315 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3316 	    subp <= endp;
3317 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3318 
3319 		switch (subp->dl_cap) {
3320 		case DL_CAPAB_ID_WRAPPER:
3321 			ill_capability_id_ack(ill, mp, subp);
3322 			break;
3323 		default:
3324 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3325 			break;
3326 		}
3327 	}
3328 #undef SC
3329 done:
3330 	inet_freemsg(mp);
3331 	ill_capability_done(ill);
3332 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3333 }
3334 
3335 /*
3336  * This needs to be started in a taskq thread to provide a cv_waitable
3337  * context.
3338  */
3339 void
3340 ill_capability_ack(ill_t *ill, mblk_t *mp)
3341 {
3342 	ip_stack_t	*ipst = ill->ill_ipst;
3343 
3344 	mp->b_prev = (mblk_t *)ill;
3345 	ASSERT(mp->b_next == NULL);
3346 
3347 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3348 	    TQ_NOSLEEP) != 0)
3349 		return;
3350 
3351 	/*
3352 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3353 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3354 	 */
3355 	mutex_enter(&ipst->ips_capab_taskq_lock);
3356 	if (ipst->ips_capab_taskq_head == NULL) {
3357 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
3358 		ipst->ips_capab_taskq_head = mp;
3359 	} else {
3360 		ipst->ips_capab_taskq_tail->b_next = mp;
3361 	}
3362 	ipst->ips_capab_taskq_tail = mp;
3363 
3364 	cv_signal(&ipst->ips_capab_taskq_cv);
3365 	mutex_exit(&ipst->ips_capab_taskq_lock);
3366 }
3367 
3368 /*
3369  * This routine is called to scan the fragmentation reassembly table for
3370  * the specified ILL for any packets that are starting to smell.
3371  * dead_interval is the maximum time in seconds that will be tolerated.  It
3372  * will either be the value specified in ip_g_frag_timeout, or zero if the
3373  * ILL is shutting down and it is time to blow everything off.
3374  *
3375  * It returns the number of seconds (as a time_t) that the next frag timer
3376  * should be scheduled for, 0 meaning that the timer doesn't need to be
3377  * re-started.  Note that the method of calculating next_timeout isn't
3378  * entirely accurate since time will flow between the time we grab
3379  * current_time and the time we schedule the next timeout.  This isn't a
3380  * big problem since this is the timer for sending an ICMP reassembly time
3381  * exceeded messages, and it doesn't have to be exactly accurate.
3382  *
3383  * This function is
3384  * sometimes called as writer, although this is not required.
3385  */
3386 time_t
3387 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3388 {
3389 	ipfb_t	*ipfb;
3390 	ipfb_t	*endp;
3391 	ipf_t	*ipf;
3392 	ipf_t	*ipfnext;
3393 	mblk_t	*mp;
3394 	time_t	current_time = gethrestime_sec();
3395 	time_t	next_timeout = 0;
3396 	uint32_t	hdr_length;
3397 	mblk_t	*send_icmp_head;
3398 	mblk_t	*send_icmp_head_v6;
3399 	zoneid_t zoneid;
3400 	ip_stack_t *ipst = ill->ill_ipst;
3401 
3402 	ipfb = ill->ill_frag_hash_tbl;
3403 	if (ipfb == NULL)
3404 		return (B_FALSE);
3405 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3406 	/* Walk the frag hash table. */
3407 	for (; ipfb < endp; ipfb++) {
3408 		send_icmp_head = NULL;
3409 		send_icmp_head_v6 = NULL;
3410 		mutex_enter(&ipfb->ipfb_lock);
3411 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3412 			time_t frag_time = current_time - ipf->ipf_timestamp;
3413 			time_t frag_timeout;
3414 
3415 			if (frag_time < dead_interval) {
3416 				/*
3417 				 * There are some outstanding fragments
3418 				 * that will timeout later.  Make note of
3419 				 * the time so that we can reschedule the
3420 				 * next timeout appropriately.
3421 				 */
3422 				frag_timeout = dead_interval - frag_time;
3423 				if (next_timeout == 0 ||
3424 				    frag_timeout < next_timeout) {
3425 					next_timeout = frag_timeout;
3426 				}
3427 				break;
3428 			}
3429 			/* Time's up.  Get it out of here. */
3430 			hdr_length = ipf->ipf_nf_hdr_len;
3431 			ipfnext = ipf->ipf_hash_next;
3432 			if (ipfnext)
3433 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3434 			*ipf->ipf_ptphn = ipfnext;
3435 			mp = ipf->ipf_mp->b_cont;
3436 			for (; mp; mp = mp->b_cont) {
3437 				/* Extra points for neatness. */
3438 				IP_REASS_SET_START(mp, 0);
3439 				IP_REASS_SET_END(mp, 0);
3440 			}
3441 			mp = ipf->ipf_mp->b_cont;
3442 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3443 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3444 			ipfb->ipfb_count -= ipf->ipf_count;
3445 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3446 			ipfb->ipfb_frag_pkts--;
3447 			/*
3448 			 * We do not send any icmp message from here because
3449 			 * we currently are holding the ipfb_lock for this
3450 			 * hash chain. If we try and send any icmp messages
3451 			 * from here we may end up via a put back into ip
3452 			 * trying to get the same lock, causing a recursive
3453 			 * mutex panic. Instead we build a list and send all
3454 			 * the icmp messages after we have dropped the lock.
3455 			 */
3456 			if (ill->ill_isv6) {
3457 				if (hdr_length != 0) {
3458 					mp->b_next = send_icmp_head_v6;
3459 					send_icmp_head_v6 = mp;
3460 				} else {
3461 					freemsg(mp);
3462 				}
3463 			} else {
3464 				if (hdr_length != 0) {
3465 					mp->b_next = send_icmp_head;
3466 					send_icmp_head = mp;
3467 				} else {
3468 					freemsg(mp);
3469 				}
3470 			}
3471 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3472 			freeb(ipf->ipf_mp);
3473 		}
3474 		mutex_exit(&ipfb->ipfb_lock);
3475 		/*
3476 		 * Now need to send any icmp messages that we delayed from
3477 		 * above.
3478 		 */
3479 		while (send_icmp_head_v6 != NULL) {
3480 			ip6_t *ip6h;
3481 
3482 			mp = send_icmp_head_v6;
3483 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3484 			mp->b_next = NULL;
3485 			if (mp->b_datap->db_type == M_CTL)
3486 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3487 			else
3488 				ip6h = (ip6_t *)mp->b_rptr;
3489 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3490 			    ill, ipst);
3491 			if (zoneid == ALL_ZONES) {
3492 				freemsg(mp);
3493 			} else {
3494 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3495 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3496 				    B_FALSE, zoneid, ipst);
3497 			}
3498 		}
3499 		while (send_icmp_head != NULL) {
3500 			ipaddr_t dst;
3501 
3502 			mp = send_icmp_head;
3503 			send_icmp_head = send_icmp_head->b_next;
3504 			mp->b_next = NULL;
3505 
3506 			if (mp->b_datap->db_type == M_CTL)
3507 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3508 			else
3509 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3510 
3511 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3512 			if (zoneid == ALL_ZONES) {
3513 				freemsg(mp);
3514 			} else {
3515 				icmp_time_exceeded(ill->ill_wq, mp,
3516 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3517 				    ipst);
3518 			}
3519 		}
3520 	}
3521 	/*
3522 	 * A non-dying ILL will use the return value to decide whether to
3523 	 * restart the frag timer, and for how long.
3524 	 */
3525 	return (next_timeout);
3526 }
3527 
3528 /*
3529  * This routine is called when the approximate count of mblk memory used
3530  * for the specified ILL has exceeded max_count.
3531  */
3532 void
3533 ill_frag_prune(ill_t *ill, uint_t max_count)
3534 {
3535 	ipfb_t	*ipfb;
3536 	ipf_t	*ipf;
3537 	size_t	count;
3538 
3539 	/*
3540 	 * If we are here within ip_min_frag_prune_time msecs remove
3541 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3542 	 * ill_frag_free_num_pkts.
3543 	 */
3544 	mutex_enter(&ill->ill_lock);
3545 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3546 	    (ip_min_frag_prune_time != 0 ?
3547 	    ip_min_frag_prune_time : msec_per_tick)) {
3548 
3549 		ill->ill_frag_free_num_pkts++;
3550 
3551 	} else {
3552 		ill->ill_frag_free_num_pkts = 0;
3553 	}
3554 	ill->ill_last_frag_clean_time = lbolt;
3555 	mutex_exit(&ill->ill_lock);
3556 
3557 	/*
3558 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3559 	 */
3560 	if (ill->ill_frag_free_num_pkts != 0) {
3561 		int ix;
3562 
3563 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3564 			ipfb = &ill->ill_frag_hash_tbl[ix];
3565 			mutex_enter(&ipfb->ipfb_lock);
3566 			if (ipfb->ipfb_ipf != NULL) {
3567 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3568 				    ill->ill_frag_free_num_pkts);
3569 			}
3570 			mutex_exit(&ipfb->ipfb_lock);
3571 		}
3572 	}
3573 	/*
3574 	 * While the reassembly list for this ILL is too big, prune a fragment
3575 	 * queue by age, oldest first.
3576 	 */
3577 	while (ill->ill_frag_count > max_count) {
3578 		int	ix;
3579 		ipfb_t	*oipfb = NULL;
3580 		uint_t	oldest = UINT_MAX;
3581 
3582 		count = 0;
3583 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3584 			ipfb = &ill->ill_frag_hash_tbl[ix];
3585 			mutex_enter(&ipfb->ipfb_lock);
3586 			ipf = ipfb->ipfb_ipf;
3587 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3588 				oldest = ipf->ipf_gen;
3589 				oipfb = ipfb;
3590 			}
3591 			count += ipfb->ipfb_count;
3592 			mutex_exit(&ipfb->ipfb_lock);
3593 		}
3594 		if (oipfb == NULL)
3595 			break;
3596 
3597 		if (count <= max_count)
3598 			return;	/* Somebody beat us to it, nothing to do */
3599 		mutex_enter(&oipfb->ipfb_lock);
3600 		ipf = oipfb->ipfb_ipf;
3601 		if (ipf != NULL) {
3602 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3603 		}
3604 		mutex_exit(&oipfb->ipfb_lock);
3605 	}
3606 }
3607 
3608 /*
3609  * free 'free_cnt' fragmented packets starting at ipf.
3610  */
3611 void
3612 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3613 {
3614 	size_t	count;
3615 	mblk_t	*mp;
3616 	mblk_t	*tmp;
3617 	ipf_t **ipfp = ipf->ipf_ptphn;
3618 
3619 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3620 	ASSERT(ipfp != NULL);
3621 	ASSERT(ipf != NULL);
3622 
3623 	while (ipf != NULL && free_cnt-- > 0) {
3624 		count = ipf->ipf_count;
3625 		mp = ipf->ipf_mp;
3626 		ipf = ipf->ipf_hash_next;
3627 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3628 			IP_REASS_SET_START(tmp, 0);
3629 			IP_REASS_SET_END(tmp, 0);
3630 		}
3631 		atomic_add_32(&ill->ill_frag_count, -count);
3632 		ASSERT(ipfb->ipfb_count >= count);
3633 		ipfb->ipfb_count -= count;
3634 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3635 		ipfb->ipfb_frag_pkts--;
3636 		freemsg(mp);
3637 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3638 	}
3639 
3640 	if (ipf)
3641 		ipf->ipf_ptphn = ipfp;
3642 	ipfp[0] = ipf;
3643 }
3644 
3645 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3646 	"obsolete and may be removed in a future release of Solaris.  Use " \
3647 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3648 
3649 /*
3650  * For obsolete per-interface forwarding configuration;
3651  * called in response to ND_GET.
3652  */
3653 /* ARGSUSED */
3654 static int
3655 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3656 {
3657 	ill_t *ill = (ill_t *)cp;
3658 
3659 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3660 
3661 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3662 	return (0);
3663 }
3664 
3665 /*
3666  * For obsolete per-interface forwarding configuration;
3667  * called in response to ND_SET.
3668  */
3669 /* ARGSUSED */
3670 static int
3671 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3672     cred_t *ioc_cr)
3673 {
3674 	long value;
3675 	int retval;
3676 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3677 
3678 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3679 
3680 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3681 	    value < 0 || value > 1) {
3682 		return (EINVAL);
3683 	}
3684 
3685 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3686 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3687 	rw_exit(&ipst->ips_ill_g_lock);
3688 	return (retval);
3689 }
3690 
3691 /*
3692  * Helper function for ill_forward_set().
3693  */
3694 static void
3695 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
3696 {
3697 	ip_stack_t	*ipst = ill->ill_ipst;
3698 
3699 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3700 
3701 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3702 	    (enable ? "Enabling" : "Disabling"),
3703 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3704 	mutex_enter(&ill->ill_lock);
3705 	if (enable)
3706 		ill->ill_flags |= ILLF_ROUTER;
3707 	else
3708 		ill->ill_flags &= ~ILLF_ROUTER;
3709 	mutex_exit(&ill->ill_lock);
3710 	if (ill->ill_isv6)
3711 		ill_set_nce_router_flags(ill, enable);
3712 	/* Notify routing socket listeners of this change. */
3713 	if (ill->ill_ipif != NULL)
3714 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
3715 }
3716 
3717 /*
3718  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
3719  * socket messages for each interface whose flags we change.
3720  */
3721 int
3722 ill_forward_set(ill_t *ill, boolean_t enable)
3723 {
3724 	ipmp_illgrp_t *illg;
3725 	ip_stack_t *ipst = ill->ill_ipst;
3726 
3727 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3728 
3729 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3730 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3731 		return (0);
3732 
3733 	if (IS_LOOPBACK(ill))
3734 		return (EINVAL);
3735 
3736 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
3737 		/*
3738 		 * Update all of the interfaces in the group.
3739 		 */
3740 		illg = ill->ill_grp;
3741 		ill = list_head(&illg->ig_if);
3742 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
3743 			ill_forward_set_on_ill(ill, enable);
3744 
3745 		/*
3746 		 * Update the IPMP meta-interface.
3747 		 */
3748 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
3749 		return (0);
3750 	}
3751 
3752 	ill_forward_set_on_ill(ill, enable);
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 		/*
3769 		 * NOTE: we match across the illgrp because nce's for
3770 		 * addresses on IPMP interfaces have an nce_ill that points to
3771 		 * the bound underlying ill.
3772 		 */
3773 		nce = ndp_lookup_v6(ill, B_TRUE, &ipif->ipif_v6lcl_addr,
3774 		    B_FALSE);
3775 		if (nce != NULL) {
3776 			mutex_enter(&nce->nce_lock);
3777 			if (enable)
3778 				nce->nce_flags |= NCE_F_ISROUTER;
3779 			else
3780 				nce->nce_flags &= ~NCE_F_ISROUTER;
3781 			mutex_exit(&nce->nce_lock);
3782 			NCE_REFRELE(nce);
3783 		}
3784 	}
3785 }
3786 
3787 /*
3788  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3789  * for this ill.  Make sure the v6/v4 question has been answered about this
3790  * ill.  The creation of this ndd variable is only for backwards compatibility.
3791  * The preferred way to control per-interface IP forwarding is through the
3792  * ILLF_ROUTER interface flag.
3793  */
3794 static int
3795 ill_set_ndd_name(ill_t *ill)
3796 {
3797 	char *suffix;
3798 	ip_stack_t	*ipst = ill->ill_ipst;
3799 
3800 	ASSERT(IAM_WRITER_ILL(ill));
3801 
3802 	if (ill->ill_isv6)
3803 		suffix = ipv6_forward_suffix;
3804 	else
3805 		suffix = ipv4_forward_suffix;
3806 
3807 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3808 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3809 	/*
3810 	 * Copies over the '\0'.
3811 	 * Note that strlen(suffix) is always bounded.
3812 	 */
3813 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3814 	    strlen(suffix) + 1);
3815 
3816 	/*
3817 	 * Use of the nd table requires holding the reader lock.
3818 	 * Modifying the nd table thru nd_load/nd_unload requires
3819 	 * the writer lock.
3820 	 */
3821 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3822 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3823 	    nd_ill_forward_set, (caddr_t)ill)) {
3824 		/*
3825 		 * If the nd_load failed, it only meant that it could not
3826 		 * allocate a new bunch of room for further NDD expansion.
3827 		 * Because of that, the ill_ndd_name will be set to 0, and
3828 		 * this interface is at the mercy of the global ip_forwarding
3829 		 * variable.
3830 		 */
3831 		rw_exit(&ipst->ips_ip_g_nd_lock);
3832 		ill->ill_ndd_name = NULL;
3833 		return (ENOMEM);
3834 	}
3835 	rw_exit(&ipst->ips_ip_g_nd_lock);
3836 	return (0);
3837 }
3838 
3839 /*
3840  * Intializes the context structure and returns the first ill in the list
3841  * cuurently start_list and end_list can have values:
3842  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3843  * IP_V4_G_HEAD		Traverse IPV4 list only.
3844  * IP_V6_G_HEAD		Traverse IPV6 list only.
3845  */
3846 
3847 /*
3848  * We don't check for CONDEMNED ills here. Caller must do that if
3849  * necessary under the ill lock.
3850  */
3851 ill_t *
3852 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3853     ip_stack_t *ipst)
3854 {
3855 	ill_if_t *ifp;
3856 	ill_t *ill;
3857 	avl_tree_t *avl_tree;
3858 
3859 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3860 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3861 
3862 	/*
3863 	 * setup the lists to search
3864 	 */
3865 	if (end_list != MAX_G_HEADS) {
3866 		ctx->ctx_current_list = start_list;
3867 		ctx->ctx_last_list = end_list;
3868 	} else {
3869 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3870 		ctx->ctx_current_list = 0;
3871 	}
3872 
3873 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3874 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3875 		if (ifp != (ill_if_t *)
3876 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3877 			avl_tree = &ifp->illif_avl_by_ppa;
3878 			ill = avl_first(avl_tree);
3879 			/*
3880 			 * ill is guaranteed to be non NULL or ifp should have
3881 			 * not existed.
3882 			 */
3883 			ASSERT(ill != NULL);
3884 			return (ill);
3885 		}
3886 		ctx->ctx_current_list++;
3887 	}
3888 
3889 	return (NULL);
3890 }
3891 
3892 /*
3893  * returns the next ill in the list. ill_first() must have been called
3894  * before calling ill_next() or bad things will happen.
3895  */
3896 
3897 /*
3898  * We don't check for CONDEMNED ills here. Caller must do that if
3899  * necessary under the ill lock.
3900  */
3901 ill_t *
3902 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3903 {
3904 	ill_if_t *ifp;
3905 	ill_t *ill;
3906 	ip_stack_t	*ipst = lastill->ill_ipst;
3907 
3908 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3909 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3910 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3911 	    AVL_AFTER)) != NULL) {
3912 		return (ill);
3913 	}
3914 
3915 	/* goto next ill_ifp in the list. */
3916 	ifp = lastill->ill_ifptr->illif_next;
3917 
3918 	/* make sure not at end of circular list */
3919 	while (ifp ==
3920 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3921 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3922 			return (NULL);
3923 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3924 	}
3925 
3926 	return (avl_first(&ifp->illif_avl_by_ppa));
3927 }
3928 
3929 /*
3930  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
3931  * The final number (PPA) must not have any leading zeros.  Upon success, a
3932  * pointer to the start of the PPA is returned; otherwise NULL is returned.
3933  */
3934 static char *
3935 ill_get_ppa_ptr(char *name)
3936 {
3937 	int namelen = strlen(name);
3938 	int end_ndx = namelen - 1;
3939 	int ppa_ndx, i;
3940 
3941 	/*
3942 	 * Check that the first character is [a-zA-Z], and that the last
3943 	 * character is [0-9].
3944 	 */
3945 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
3946 		return (NULL);
3947 
3948 	/*
3949 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
3950 	 */
3951 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
3952 		if (!isdigit(name[ppa_ndx - 1]))
3953 			break;
3954 
3955 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
3956 		return (NULL);
3957 
3958 	/*
3959 	 * Check that the intermediate characters are [a-z0-9.]
3960 	 */
3961 	for (i = 1; i < ppa_ndx; i++) {
3962 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
3963 		    name[i] != '.' && name[i] != '_') {
3964 			return (NULL);
3965 		}
3966 	}
3967 
3968 	return (name + ppa_ndx);
3969 }
3970 
3971 /*
3972  * use avl tree to locate the ill.
3973  */
3974 static ill_t *
3975 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3976     ipsq_func_t func, int *error, ip_stack_t *ipst)
3977 {
3978 	char *ppa_ptr = NULL;
3979 	int len;
3980 	uint_t ppa;
3981 	ill_t *ill = NULL;
3982 	ill_if_t *ifp;
3983 	int list;
3984 	ipsq_t *ipsq;
3985 
3986 	if (error != NULL)
3987 		*error = 0;
3988 
3989 	/*
3990 	 * get ppa ptr
3991 	 */
3992 	if (isv6)
3993 		list = IP_V6_G_HEAD;
3994 	else
3995 		list = IP_V4_G_HEAD;
3996 
3997 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3998 		if (error != NULL)
3999 			*error = ENXIO;
4000 		return (NULL);
4001 	}
4002 
4003 	len = ppa_ptr - name + 1;
4004 
4005 	ppa = stoi(&ppa_ptr);
4006 
4007 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4008 
4009 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4010 		/*
4011 		 * match is done on len - 1 as the name is not null
4012 		 * terminated it contains ppa in addition to the interface
4013 		 * name.
4014 		 */
4015 		if ((ifp->illif_name_len == len) &&
4016 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4017 			break;
4018 		} else {
4019 			ifp = ifp->illif_next;
4020 		}
4021 	}
4022 
4023 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4024 		/*
4025 		 * Even the interface type does not exist.
4026 		 */
4027 		if (error != NULL)
4028 			*error = ENXIO;
4029 		return (NULL);
4030 	}
4031 
4032 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4033 	if (ill != NULL) {
4034 		/*
4035 		 * The block comment at the start of ipif_down
4036 		 * explains the use of the macros used below
4037 		 */
4038 		GRAB_CONN_LOCK(q);
4039 		mutex_enter(&ill->ill_lock);
4040 		if (ILL_CAN_LOOKUP(ill)) {
4041 			ill_refhold_locked(ill);
4042 			mutex_exit(&ill->ill_lock);
4043 			RELEASE_CONN_LOCK(q);
4044 			return (ill);
4045 		} else if (ILL_CAN_WAIT(ill, q)) {
4046 			ipsq = ill->ill_phyint->phyint_ipsq;
4047 			mutex_enter(&ipsq->ipsq_lock);
4048 			mutex_enter(&ipsq->ipsq_xop->ipx_lock);
4049 			mutex_exit(&ill->ill_lock);
4050 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4051 			mutex_exit(&ipsq->ipsq_xop->ipx_lock);
4052 			mutex_exit(&ipsq->ipsq_lock);
4053 			RELEASE_CONN_LOCK(q);
4054 			if (error != NULL)
4055 				*error = EINPROGRESS;
4056 			return (NULL);
4057 		}
4058 		mutex_exit(&ill->ill_lock);
4059 		RELEASE_CONN_LOCK(q);
4060 	}
4061 	if (error != NULL)
4062 		*error = ENXIO;
4063 	return (NULL);
4064 }
4065 
4066 /*
4067  * comparison function for use with avl.
4068  */
4069 static int
4070 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4071 {
4072 	uint_t ppa;
4073 	uint_t ill_ppa;
4074 
4075 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4076 
4077 	ppa = *((uint_t *)ppa_ptr);
4078 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4079 	/*
4080 	 * We want the ill with the lowest ppa to be on the
4081 	 * top.
4082 	 */
4083 	if (ill_ppa < ppa)
4084 		return (1);
4085 	if (ill_ppa > ppa)
4086 		return (-1);
4087 	return (0);
4088 }
4089 
4090 /*
4091  * remove an interface type from the global list.
4092  */
4093 static void
4094 ill_delete_interface_type(ill_if_t *interface)
4095 {
4096 	ASSERT(interface != NULL);
4097 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4098 
4099 	avl_destroy(&interface->illif_avl_by_ppa);
4100 	if (interface->illif_ppa_arena != NULL)
4101 		vmem_destroy(interface->illif_ppa_arena);
4102 
4103 	remque(interface);
4104 
4105 	mi_free(interface);
4106 }
4107 
4108 /*
4109  * remove ill from the global list.
4110  */
4111 static void
4112 ill_glist_delete(ill_t *ill)
4113 {
4114 	ip_stack_t	*ipst;
4115 	phyint_t	*phyi;
4116 
4117 	if (ill == NULL)
4118 		return;
4119 	ipst = ill->ill_ipst;
4120 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4121 
4122 	/*
4123 	 * If the ill was never inserted into the AVL tree
4124 	 * we skip the if branch.
4125 	 */
4126 	if (ill->ill_ifptr != NULL) {
4127 		/*
4128 		 * remove from AVL tree and free ppa number
4129 		 */
4130 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4131 
4132 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4133 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4134 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4135 		}
4136 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4137 			ill_delete_interface_type(ill->ill_ifptr);
4138 		}
4139 
4140 		/*
4141 		 * Indicate ill is no longer in the list.
4142 		 */
4143 		ill->ill_ifptr = NULL;
4144 		ill->ill_name_length = 0;
4145 		ill->ill_name[0] = '\0';
4146 		ill->ill_ppa = UINT_MAX;
4147 	}
4148 
4149 	/* Generate one last event for this ill. */
4150 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4151 	    ill->ill_name_length);
4152 
4153 	ASSERT(ill->ill_phyint != NULL);
4154 	phyi = ill->ill_phyint;
4155 	ill->ill_phyint = NULL;
4156 
4157 	/*
4158 	 * ill_init allocates a phyint always to store the copy
4159 	 * of flags relevant to phyint. At that point in time, we could
4160 	 * not assign the name and hence phyint_illv4/v6 could not be
4161 	 * initialized. Later in ipif_set_values, we assign the name to
4162 	 * the ill, at which point in time we assign phyint_illv4/v6.
4163 	 * Thus we don't rely on phyint_illv6 to be initialized always.
4164 	 */
4165 	if (ill->ill_flags & ILLF_IPV6)
4166 		phyi->phyint_illv6 = NULL;
4167 	else
4168 		phyi->phyint_illv4 = NULL;
4169 
4170 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
4171 		rw_exit(&ipst->ips_ill_g_lock);
4172 		return;
4173 	}
4174 
4175 	/*
4176 	 * There are no ills left on this phyint; pull it out of the phyint
4177 	 * avl trees, and free it.
4178 	 */
4179 	if (phyi->phyint_ifindex > 0) {
4180 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4181 		    phyi);
4182 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4183 		    phyi);
4184 	}
4185 	rw_exit(&ipst->ips_ill_g_lock);
4186 
4187 	phyint_free(phyi);
4188 }
4189 
4190 /*
4191  * allocate a ppa, if the number of plumbed interfaces of this type are
4192  * less than ill_no_arena do a linear search to find a unused ppa.
4193  * When the number goes beyond ill_no_arena switch to using an arena.
4194  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4195  * is the return value for an error condition, so allocation starts at one
4196  * and is decremented by one.
4197  */
4198 static int
4199 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4200 {
4201 	ill_t *tmp_ill;
4202 	uint_t start, end;
4203 	int ppa;
4204 
4205 	if (ifp->illif_ppa_arena == NULL &&
4206 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4207 		/*
4208 		 * Create an arena.
4209 		 */
4210 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4211 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4212 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4213 			/* allocate what has already been assigned */
4214 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4215 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4216 		    tmp_ill, AVL_AFTER)) {
4217 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4218 			    1,		/* size */
4219 			    1,		/* align/quantum */
4220 			    0,		/* phase */
4221 			    0,		/* nocross */
4222 			    /* minaddr */
4223 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4224 			    /* maxaddr */
4225 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4226 			    VM_NOSLEEP|VM_FIRSTFIT);
4227 			if (ppa == 0) {
4228 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4229 				    " failed while switching"));
4230 				vmem_destroy(ifp->illif_ppa_arena);
4231 				ifp->illif_ppa_arena = NULL;
4232 				break;
4233 			}
4234 		}
4235 	}
4236 
4237 	if (ifp->illif_ppa_arena != NULL) {
4238 		if (ill->ill_ppa == UINT_MAX) {
4239 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4240 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4241 			if (ppa == 0)
4242 				return (EAGAIN);
4243 			ill->ill_ppa = --ppa;
4244 		} else {
4245 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4246 			    1, 		/* size */
4247 			    1, 		/* align/quantum */
4248 			    0, 		/* phase */
4249 			    0, 		/* nocross */
4250 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4251 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4252 			    VM_NOSLEEP|VM_FIRSTFIT);
4253 			/*
4254 			 * Most likely the allocation failed because
4255 			 * the requested ppa was in use.
4256 			 */
4257 			if (ppa == 0)
4258 				return (EEXIST);
4259 		}
4260 		return (0);
4261 	}
4262 
4263 	/*
4264 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4265 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4266 	 */
4267 	if (ill->ill_ppa == UINT_MAX) {
4268 		end = UINT_MAX - 1;
4269 		start = 0;
4270 	} else {
4271 		end = start = ill->ill_ppa;
4272 	}
4273 
4274 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4275 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4276 		if (start++ >= end) {
4277 			if (ill->ill_ppa == UINT_MAX)
4278 				return (EAGAIN);
4279 			else
4280 				return (EEXIST);
4281 		}
4282 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4283 	}
4284 	ill->ill_ppa = start;
4285 	return (0);
4286 }
4287 
4288 /*
4289  * Insert ill into the list of configured ill's. Once this function completes,
4290  * the ill is globally visible and is available through lookups. More precisely
4291  * this happens after the caller drops the ill_g_lock.
4292  */
4293 static int
4294 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4295 {
4296 	ill_if_t *ill_interface;
4297 	avl_index_t where = 0;
4298 	int error;
4299 	int name_length;
4300 	int index;
4301 	boolean_t check_length = B_FALSE;
4302 	ip_stack_t	*ipst = ill->ill_ipst;
4303 
4304 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4305 
4306 	name_length = mi_strlen(name) + 1;
4307 
4308 	if (isv6)
4309 		index = IP_V6_G_HEAD;
4310 	else
4311 		index = IP_V4_G_HEAD;
4312 
4313 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4314 	/*
4315 	 * Search for interface type based on name
4316 	 */
4317 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4318 		if ((ill_interface->illif_name_len == name_length) &&
4319 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4320 			break;
4321 		}
4322 		ill_interface = ill_interface->illif_next;
4323 	}
4324 
4325 	/*
4326 	 * Interface type not found, create one.
4327 	 */
4328 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4329 		ill_g_head_t ghead;
4330 
4331 		/*
4332 		 * allocate ill_if_t structure
4333 		 */
4334 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4335 		if (ill_interface == NULL) {
4336 			return (ENOMEM);
4337 		}
4338 
4339 		(void) strcpy(ill_interface->illif_name, name);
4340 		ill_interface->illif_name_len = name_length;
4341 
4342 		avl_create(&ill_interface->illif_avl_by_ppa,
4343 		    ill_compare_ppa, sizeof (ill_t),
4344 		    offsetof(struct ill_s, ill_avl_byppa));
4345 
4346 		/*
4347 		 * link the structure in the back to maintain order
4348 		 * of configuration for ifconfig output.
4349 		 */
4350 		ghead = ipst->ips_ill_g_heads[index];
4351 		insque(ill_interface, ghead.ill_g_list_tail);
4352 	}
4353 
4354 	if (ill->ill_ppa == UINT_MAX)
4355 		check_length = B_TRUE;
4356 
4357 	error = ill_alloc_ppa(ill_interface, ill);
4358 	if (error != 0) {
4359 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4360 			ill_delete_interface_type(ill->ill_ifptr);
4361 		return (error);
4362 	}
4363 
4364 	/*
4365 	 * When the ppa is choosen by the system, check that there is
4366 	 * enough space to insert ppa. if a specific ppa was passed in this
4367 	 * check is not required as the interface name passed in will have
4368 	 * the right ppa in it.
4369 	 */
4370 	if (check_length) {
4371 		/*
4372 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4373 		 */
4374 		char buf[sizeof (uint_t) * 3];
4375 
4376 		/*
4377 		 * convert ppa to string to calculate the amount of space
4378 		 * required for it in the name.
4379 		 */
4380 		numtos(ill->ill_ppa, buf);
4381 
4382 		/* Do we have enough space to insert ppa ? */
4383 
4384 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4385 			/* Free ppa and interface type struct */
4386 			if (ill_interface->illif_ppa_arena != NULL) {
4387 				vmem_free(ill_interface->illif_ppa_arena,
4388 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4389 			}
4390 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4391 				ill_delete_interface_type(ill->ill_ifptr);
4392 
4393 			return (EINVAL);
4394 		}
4395 	}
4396 
4397 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4398 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4399 
4400 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4401 	    &where);
4402 	ill->ill_ifptr = ill_interface;
4403 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4404 
4405 	ill_phyint_reinit(ill);
4406 	return (0);
4407 }
4408 
4409 /* Initialize the per phyint ipsq used for serialization */
4410 static boolean_t
4411 ipsq_init(ill_t *ill, boolean_t enter)
4412 {
4413 	ipsq_t  *ipsq;
4414 	ipxop_t	*ipx;
4415 
4416 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
4417 		return (B_FALSE);
4418 
4419 	ill->ill_phyint->phyint_ipsq = ipsq;
4420 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
4421 	ipx->ipx_ipsq = ipsq;
4422 	ipsq->ipsq_next = ipsq;
4423 	ipsq->ipsq_phyint = ill->ill_phyint;
4424 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4425 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
4426 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4427 	if (enter) {
4428 		ipx->ipx_writer = curthread;
4429 		ipx->ipx_forced = B_FALSE;
4430 		ipx->ipx_reentry_cnt = 1;
4431 #ifdef DEBUG
4432 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
4433 #endif
4434 	}
4435 	return (B_TRUE);
4436 }
4437 
4438 /*
4439  * ill_init is called by ip_open when a device control stream is opened.
4440  * It does a few initializations, and shoots a DL_INFO_REQ message down
4441  * to the driver.  The response is later picked up in ip_rput_dlpi and
4442  * used to set up default mechanisms for talking to the driver.  (Always
4443  * called as writer.)
4444  *
4445  * If this function returns error, ip_open will call ip_close which in
4446  * turn will call ill_delete to clean up any memory allocated here that
4447  * is not yet freed.
4448  */
4449 int
4450 ill_init(queue_t *q, ill_t *ill)
4451 {
4452 	int	count;
4453 	dl_info_req_t	*dlir;
4454 	mblk_t	*info_mp;
4455 	uchar_t *frag_ptr;
4456 
4457 	/*
4458 	 * The ill is initialized to zero by mi_alloc*(). In addition
4459 	 * some fields already contain valid values, initialized in
4460 	 * ip_open(), before we reach here.
4461 	 */
4462 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4463 
4464 	ill->ill_rq = q;
4465 	ill->ill_wq = WR(q);
4466 
4467 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4468 	    BPRI_HI);
4469 	if (info_mp == NULL)
4470 		return (ENOMEM);
4471 
4472 	/*
4473 	 * Allocate sufficient space to contain our fragment hash table and
4474 	 * the device name.
4475 	 */
4476 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4477 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4478 	if (frag_ptr == NULL) {
4479 		freemsg(info_mp);
4480 		return (ENOMEM);
4481 	}
4482 	ill->ill_frag_ptr = frag_ptr;
4483 	ill->ill_frag_free_num_pkts = 0;
4484 	ill->ill_last_frag_clean_time = 0;
4485 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4486 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4487 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4488 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4489 		    NULL, MUTEX_DEFAULT, NULL);
4490 	}
4491 
4492 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4493 	if (ill->ill_phyint == NULL) {
4494 		freemsg(info_mp);
4495 		mi_free(frag_ptr);
4496 		return (ENOMEM);
4497 	}
4498 
4499 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4500 	/*
4501 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4502 	 * at this point because of the following reason. If we can't
4503 	 * enter the ipsq at some point and cv_wait, the writer that
4504 	 * wakes us up tries to locate us using the list of all phyints
4505 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4506 	 * If we don't set it now, we risk a missed wakeup.
4507 	 */
4508 	ill->ill_phyint->phyint_illv4 = ill;
4509 	ill->ill_ppa = UINT_MAX;
4510 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4511 
4512 	if (!ipsq_init(ill, B_TRUE)) {
4513 		freemsg(info_mp);
4514 		mi_free(frag_ptr);
4515 		mi_free(ill->ill_phyint);
4516 		return (ENOMEM);
4517 	}
4518 
4519 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4520 
4521 	/* Frag queue limit stuff */
4522 	ill->ill_frag_count = 0;
4523 	ill->ill_ipf_gen = 0;
4524 
4525 	ill->ill_global_timer = INFINITY;
4526 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4527 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4528 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4529 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4530 
4531 	/*
4532 	 * Initialize IPv6 configuration variables.  The IP module is always
4533 	 * opened as an IPv4 module.  Instead tracking down the cases where
4534 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4535 	 * here for convenience, this has no effect until the ill is set to do
4536 	 * IPv6.
4537 	 */
4538 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4539 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4540 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4541 	ill->ill_max_buf = ND_MAX_Q;
4542 	ill->ill_refcnt = 0;
4543 
4544 	/* Send down the Info Request to the driver. */
4545 	info_mp->b_datap->db_type = M_PCPROTO;
4546 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4547 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4548 	dlir->dl_primitive = DL_INFO_REQ;
4549 
4550 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4551 
4552 	qprocson(q);
4553 	ill_dlpi_send(ill, info_mp);
4554 
4555 	return (0);
4556 }
4557 
4558 /*
4559  * ill_dls_info
4560  * creates datalink socket info from the device.
4561  */
4562 int
4563 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4564 {
4565 	size_t	len;
4566 	ill_t	*ill = ipif->ipif_ill;
4567 
4568 	sdl->sdl_family = AF_LINK;
4569 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4570 	sdl->sdl_type = ill->ill_type;
4571 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4572 	len = strlen(sdl->sdl_data);
4573 	ASSERT(len < 256);
4574 	sdl->sdl_nlen = (uchar_t)len;
4575 	sdl->sdl_alen = ill->ill_phys_addr_length;
4576 	sdl->sdl_slen = 0;
4577 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4578 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4579 
4580 	return (sizeof (struct sockaddr_dl));
4581 }
4582 
4583 /*
4584  * ill_xarp_info
4585  * creates xarp info from the device.
4586  */
4587 static int
4588 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4589 {
4590 	sdl->sdl_family = AF_LINK;
4591 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4592 	sdl->sdl_type = ill->ill_type;
4593 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4594 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4595 	sdl->sdl_alen = ill->ill_phys_addr_length;
4596 	sdl->sdl_slen = 0;
4597 	return (sdl->sdl_nlen);
4598 }
4599 
4600 static int
4601 loopback_kstat_update(kstat_t *ksp, int rw)
4602 {
4603 	kstat_named_t *kn;
4604 	netstackid_t	stackid;
4605 	netstack_t	*ns;
4606 	ip_stack_t	*ipst;
4607 
4608 	if (ksp == NULL || ksp->ks_data == NULL)
4609 		return (EIO);
4610 
4611 	if (rw == KSTAT_WRITE)
4612 		return (EACCES);
4613 
4614 	kn = KSTAT_NAMED_PTR(ksp);
4615 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4616 
4617 	ns = netstack_find_by_stackid(stackid);
4618 	if (ns == NULL)
4619 		return (-1);
4620 
4621 	ipst = ns->netstack_ip;
4622 	if (ipst == NULL) {
4623 		netstack_rele(ns);
4624 		return (-1);
4625 	}
4626 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4627 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4628 	netstack_rele(ns);
4629 	return (0);
4630 }
4631 
4632 /*
4633  * Has ifindex been plumbed already?
4634  */
4635 boolean_t
4636 phyint_exists(uint_t index, ip_stack_t *ipst)
4637 {
4638 	ASSERT(index != 0);
4639 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4640 
4641 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4642 	    &index, NULL) != NULL);
4643 }
4644 
4645 /* Pick a unique ifindex */
4646 boolean_t
4647 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4648 {
4649 	uint_t starting_index;
4650 
4651 	if (!ipst->ips_ill_index_wrap) {
4652 		*indexp = ipst->ips_ill_index++;
4653 		if (ipst->ips_ill_index == 0) {
4654 			/* Reached the uint_t limit Next time wrap  */
4655 			ipst->ips_ill_index_wrap = B_TRUE;
4656 		}
4657 		return (B_TRUE);
4658 	}
4659 
4660 	/*
4661 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4662 	 * at this point and don't want to call any function that attempts
4663 	 * to get the lock again.
4664 	 */
4665 	starting_index = ipst->ips_ill_index++;
4666 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4667 		if (ipst->ips_ill_index != 0 &&
4668 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4669 			/* found unused index - use it */
4670 			*indexp = ipst->ips_ill_index;
4671 			return (B_TRUE);
4672 		}
4673 	}
4674 
4675 	/*
4676 	 * all interface indicies are inuse.
4677 	 */
4678 	return (B_FALSE);
4679 }
4680 
4681 /*
4682  * Assign a unique interface index for the phyint.
4683  */
4684 static boolean_t
4685 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4686 {
4687 	ASSERT(phyi->phyint_ifindex == 0);
4688 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4689 }
4690 
4691 /*
4692  * Initialize the flags on `phyi' as per the provided mactype.
4693  */
4694 static void
4695 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
4696 {
4697 	uint64_t flags = 0;
4698 
4699 	/*
4700 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
4701 	 * we always presume the underlying hardware is working and set
4702 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
4703 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
4704 	 * there are no active interfaces in the group so we set PHYI_FAILED.
4705 	 */
4706 	if (mactype == SUNW_DL_IPMP)
4707 		flags |= PHYI_FAILED;
4708 	else
4709 		flags |= PHYI_RUNNING;
4710 
4711 	switch (mactype) {
4712 	case SUNW_DL_VNI:
4713 		flags |= PHYI_VIRTUAL;
4714 		break;
4715 	case SUNW_DL_IPMP:
4716 		flags |= PHYI_IPMP;
4717 		break;
4718 	case DL_LOOP:
4719 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
4720 		break;
4721 	}
4722 
4723 	mutex_enter(&phyi->phyint_lock);
4724 	phyi->phyint_flags |= flags;
4725 	mutex_exit(&phyi->phyint_lock);
4726 }
4727 
4728 /*
4729  * Return a pointer to the ill which matches the supplied name.  Note that
4730  * the ill name length includes the null termination character.  (May be
4731  * called as writer.)
4732  * If do_alloc and the interface is "lo0" it will be automatically created.
4733  * Cannot bump up reference on condemned ills. So dup detect can't be done
4734  * using this func.
4735  */
4736 ill_t *
4737 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4738     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4739     ip_stack_t *ipst)
4740 {
4741 	ill_t	*ill;
4742 	ipif_t	*ipif;
4743 	ipsq_t	*ipsq;
4744 	kstat_named_t	*kn;
4745 	boolean_t isloopback;
4746 	in6_addr_t ov6addr;
4747 
4748 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4749 
4750 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4751 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4752 	rw_exit(&ipst->ips_ill_g_lock);
4753 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4754 		return (ill);
4755 
4756 	/*
4757 	 * Couldn't find it.  Does this happen to be a lookup for the
4758 	 * loopback device and are we allowed to allocate it?
4759 	 */
4760 	if (!isloopback || !do_alloc)
4761 		return (NULL);
4762 
4763 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4764 
4765 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4766 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4767 		rw_exit(&ipst->ips_ill_g_lock);
4768 		return (ill);
4769 	}
4770 
4771 	/* Create the loopback device on demand */
4772 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4773 	    sizeof (ipif_loopback_name), BPRI_MED));
4774 	if (ill == NULL)
4775 		goto done;
4776 
4777 	*ill = ill_null;
4778 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4779 	ill->ill_ipst = ipst;
4780 	netstack_hold(ipst->ips_netstack);
4781 	/*
4782 	 * For exclusive stacks we set the zoneid to zero
4783 	 * to make IP operate as if in the global zone.
4784 	 */
4785 	ill->ill_zoneid = GLOBAL_ZONEID;
4786 
4787 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4788 	if (ill->ill_phyint == NULL)
4789 		goto done;
4790 
4791 	if (isv6)
4792 		ill->ill_phyint->phyint_illv6 = ill;
4793 	else
4794 		ill->ill_phyint->phyint_illv4 = ill;
4795 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4796 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
4797 
4798 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4799 	/* Add room for tcp+ip headers */
4800 	if (isv6) {
4801 		ill->ill_isv6 = B_TRUE;
4802 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4803 	} else {
4804 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4805 	}
4806 	if (!ill_allocate_mibs(ill))
4807 		goto done;
4808 	ill->ill_max_mtu = ill->ill_max_frag;
4809 	/*
4810 	 * ipif_loopback_name can't be pointed at directly because its used
4811 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4812 	 * from the glist, ill_glist_delete() sets the first character of
4813 	 * ill_name to '\0'.
4814 	 */
4815 	ill->ill_name = (char *)ill + sizeof (*ill);
4816 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4817 	ill->ill_name_length = sizeof (ipif_loopback_name);
4818 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4819 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4820 
4821 	ill->ill_global_timer = INFINITY;
4822 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4823 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4824 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4825 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4826 
4827 	/* No resolver here. */
4828 	ill->ill_net_type = IRE_LOOPBACK;
4829 
4830 	/* Initialize the ipsq */
4831 	if (!ipsq_init(ill, B_FALSE))
4832 		goto done;
4833 
4834 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4835 	if (ipif == NULL)
4836 		goto done;
4837 
4838 	ill->ill_flags = ILLF_MULTICAST;
4839 
4840 	ov6addr = ipif->ipif_v6lcl_addr;
4841 	/* Set up default loopback address and mask. */
4842 	if (!isv6) {
4843 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4844 
4845 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4846 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4847 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4848 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4849 		    ipif->ipif_v6subnet);
4850 		ill->ill_flags |= ILLF_IPV4;
4851 	} else {
4852 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4853 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4854 		ipif->ipif_v6net_mask = ipv6_all_ones;
4855 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4856 		    ipif->ipif_v6subnet);
4857 		ill->ill_flags |= ILLF_IPV6;
4858 	}
4859 
4860 	/*
4861 	 * Chain us in at the end of the ill list. hold the ill
4862 	 * before we make it globally visible. 1 for the lookup.
4863 	 */
4864 	ill->ill_refcnt = 0;
4865 	ill_refhold(ill);
4866 
4867 	ill->ill_frag_count = 0;
4868 	ill->ill_frag_free_num_pkts = 0;
4869 	ill->ill_last_frag_clean_time = 0;
4870 
4871 	ipsq = ill->ill_phyint->phyint_ipsq;
4872 
4873 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4874 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4875 
4876 	/* Let SCTP know so that it can add this to its list */
4877 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4878 
4879 	/*
4880 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4881 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4882 	 * requires to be after ill_glist_insert() since we need the
4883 	 * ill_index set. Pass on ipv6_loopback as the old address.
4884 	 */
4885 	sctp_update_ipif_addr(ipif, ov6addr);
4886 
4887 	/*
4888 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4889 	 * If so, free our original one.
4890 	 */
4891 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4892 		ipsq_delete(ipsq);
4893 
4894 	if (ipst->ips_loopback_ksp == NULL) {
4895 		/* Export loopback interface statistics */
4896 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4897 		    ipif_loopback_name, "net",
4898 		    KSTAT_TYPE_NAMED, 2, 0,
4899 		    ipst->ips_netstack->netstack_stackid);
4900 		if (ipst->ips_loopback_ksp != NULL) {
4901 			ipst->ips_loopback_ksp->ks_update =
4902 			    loopback_kstat_update;
4903 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4904 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4905 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4906 			ipst->ips_loopback_ksp->ks_private =
4907 			    (void *)(uintptr_t)ipst->ips_netstack->
4908 			    netstack_stackid;
4909 			kstat_install(ipst->ips_loopback_ksp);
4910 		}
4911 	}
4912 
4913 	if (error != NULL)
4914 		*error = 0;
4915 	*did_alloc = B_TRUE;
4916 	rw_exit(&ipst->ips_ill_g_lock);
4917 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4918 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4919 	return (ill);
4920 done:
4921 	if (ill != NULL) {
4922 		if (ill->ill_phyint != NULL) {
4923 			ipsq = ill->ill_phyint->phyint_ipsq;
4924 			if (ipsq != NULL) {
4925 				ipsq->ipsq_phyint = NULL;
4926 				ipsq_delete(ipsq);
4927 			}
4928 			mi_free(ill->ill_phyint);
4929 		}
4930 		ill_free_mib(ill);
4931 		if (ill->ill_ipst != NULL)
4932 			netstack_rele(ill->ill_ipst->ips_netstack);
4933 		mi_free(ill);
4934 	}
4935 	rw_exit(&ipst->ips_ill_g_lock);
4936 	if (error != NULL)
4937 		*error = ENOMEM;
4938 	return (NULL);
4939 }
4940 
4941 /*
4942  * For IPP calls - use the ip_stack_t for global stack.
4943  */
4944 ill_t *
4945 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4946     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4947 {
4948 	ip_stack_t	*ipst;
4949 	ill_t		*ill;
4950 
4951 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4952 	if (ipst == NULL) {
4953 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4954 		return (NULL);
4955 	}
4956 
4957 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4958 	netstack_rele(ipst->ips_netstack);
4959 	return (ill);
4960 }
4961 
4962 /*
4963  * Return a pointer to the ill which matches the index and IP version type.
4964  */
4965 ill_t *
4966 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4967     ipsq_func_t func, int *err, ip_stack_t *ipst)
4968 {
4969 	ill_t	*ill;
4970 	ipsq_t  *ipsq;
4971 	phyint_t *phyi;
4972 
4973 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4974 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4975 
4976 	if (err != NULL)
4977 		*err = 0;
4978 
4979 	/*
4980 	 * Indexes are stored in the phyint - a common structure
4981 	 * to both IPv4 and IPv6.
4982 	 */
4983 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4984 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4985 	    (void *) &index, NULL);
4986 	if (phyi != NULL) {
4987 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4988 		if (ill != NULL) {
4989 			/*
4990 			 * The block comment at the start of ipif_down
4991 			 * explains the use of the macros used below
4992 			 */
4993 			GRAB_CONN_LOCK(q);
4994 			mutex_enter(&ill->ill_lock);
4995 			if (ILL_CAN_LOOKUP(ill)) {
4996 				ill_refhold_locked(ill);
4997 				mutex_exit(&ill->ill_lock);
4998 				RELEASE_CONN_LOCK(q);
4999 				rw_exit(&ipst->ips_ill_g_lock);
5000 				return (ill);
5001 			} else if (ILL_CAN_WAIT(ill, q)) {
5002 				ipsq = ill->ill_phyint->phyint_ipsq;
5003 				mutex_enter(&ipsq->ipsq_lock);
5004 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5005 				rw_exit(&ipst->ips_ill_g_lock);
5006 				mutex_exit(&ill->ill_lock);
5007 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5008 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5009 				mutex_exit(&ipsq->ipsq_lock);
5010 				RELEASE_CONN_LOCK(q);
5011 				if (err != NULL)
5012 					*err = EINPROGRESS;
5013 				return (NULL);
5014 			}
5015 			RELEASE_CONN_LOCK(q);
5016 			mutex_exit(&ill->ill_lock);
5017 		}
5018 	}
5019 	rw_exit(&ipst->ips_ill_g_lock);
5020 	if (err != NULL)
5021 		*err = ENXIO;
5022 	return (NULL);
5023 }
5024 
5025 /*
5026  * Return the ifindex next in sequence after the passed in ifindex.
5027  * If there is no next ifindex for the given protocol, return 0.
5028  */
5029 uint_t
5030 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5031 {
5032 	phyint_t *phyi;
5033 	phyint_t *phyi_initial;
5034 	uint_t   ifindex;
5035 
5036 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5037 
5038 	if (index == 0) {
5039 		phyi = avl_first(
5040 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5041 	} else {
5042 		phyi = phyi_initial = avl_find(
5043 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5044 		    (void *) &index, NULL);
5045 	}
5046 
5047 	for (; phyi != NULL;
5048 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5049 	    phyi, AVL_AFTER)) {
5050 		/*
5051 		 * If we're not returning the first interface in the tree
5052 		 * and we still haven't moved past the phyint_t that
5053 		 * corresponds to index, avl_walk needs to be called again
5054 		 */
5055 		if (!((index != 0) && (phyi == phyi_initial))) {
5056 			if (isv6) {
5057 				if ((phyi->phyint_illv6) &&
5058 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5059 				    (phyi->phyint_illv6->ill_isv6 == 1))
5060 					break;
5061 			} else {
5062 				if ((phyi->phyint_illv4) &&
5063 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5064 				    (phyi->phyint_illv4->ill_isv6 == 0))
5065 					break;
5066 			}
5067 		}
5068 	}
5069 
5070 	rw_exit(&ipst->ips_ill_g_lock);
5071 
5072 	if (phyi != NULL)
5073 		ifindex = phyi->phyint_ifindex;
5074 	else
5075 		ifindex = 0;
5076 
5077 	return (ifindex);
5078 }
5079 
5080 /*
5081  * Return the ifindex for the named interface.
5082  * If there is no next ifindex for the interface, return 0.
5083  */
5084 uint_t
5085 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5086 {
5087 	phyint_t	*phyi;
5088 	avl_index_t	where = 0;
5089 	uint_t		ifindex;
5090 
5091 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5092 
5093 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5094 	    name, &where)) == NULL) {
5095 		rw_exit(&ipst->ips_ill_g_lock);
5096 		return (0);
5097 	}
5098 
5099 	ifindex = phyi->phyint_ifindex;
5100 
5101 	rw_exit(&ipst->ips_ill_g_lock);
5102 
5103 	return (ifindex);
5104 }
5105 
5106 /*
5107  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5108  * that gives a running thread a reference to the ill. This reference must be
5109  * released by the thread when it is done accessing the ill and related
5110  * objects. ill_refcnt can not be used to account for static references
5111  * such as other structures pointing to an ill. Callers must generally
5112  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5113  * or be sure that the ill is not being deleted or changing state before
5114  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5115  * ill won't change any of its critical state such as address, netmask etc.
5116  */
5117 void
5118 ill_refhold(ill_t *ill)
5119 {
5120 	mutex_enter(&ill->ill_lock);
5121 	ill->ill_refcnt++;
5122 	ILL_TRACE_REF(ill);
5123 	mutex_exit(&ill->ill_lock);
5124 }
5125 
5126 void
5127 ill_refhold_locked(ill_t *ill)
5128 {
5129 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5130 	ill->ill_refcnt++;
5131 	ILL_TRACE_REF(ill);
5132 }
5133 
5134 int
5135 ill_check_and_refhold(ill_t *ill)
5136 {
5137 	mutex_enter(&ill->ill_lock);
5138 	if (ILL_CAN_LOOKUP(ill)) {
5139 		ill_refhold_locked(ill);
5140 		mutex_exit(&ill->ill_lock);
5141 		return (0);
5142 	}
5143 	mutex_exit(&ill->ill_lock);
5144 	return (ILL_LOOKUP_FAILED);
5145 }
5146 
5147 /*
5148  * Must not be called while holding any locks. Otherwise if this is
5149  * the last reference to be released, there is a chance of recursive mutex
5150  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5151  * to restart an ioctl.
5152  */
5153 void
5154 ill_refrele(ill_t *ill)
5155 {
5156 	mutex_enter(&ill->ill_lock);
5157 	ASSERT(ill->ill_refcnt != 0);
5158 	ill->ill_refcnt--;
5159 	ILL_UNTRACE_REF(ill);
5160 	if (ill->ill_refcnt != 0) {
5161 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5162 		mutex_exit(&ill->ill_lock);
5163 		return;
5164 	}
5165 
5166 	/* Drops the ill_lock */
5167 	ipif_ill_refrele_tail(ill);
5168 }
5169 
5170 /*
5171  * Obtain a weak reference count on the ill. This reference ensures the
5172  * ill won't be freed, but the ill may change any of its critical state
5173  * such as netmask, address etc. Returns an error if the ill has started
5174  * closing.
5175  */
5176 boolean_t
5177 ill_waiter_inc(ill_t *ill)
5178 {
5179 	mutex_enter(&ill->ill_lock);
5180 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5181 		mutex_exit(&ill->ill_lock);
5182 		return (B_FALSE);
5183 	}
5184 	ill->ill_waiters++;
5185 	mutex_exit(&ill->ill_lock);
5186 	return (B_TRUE);
5187 }
5188 
5189 void
5190 ill_waiter_dcr(ill_t *ill)
5191 {
5192 	mutex_enter(&ill->ill_lock);
5193 	ill->ill_waiters--;
5194 	if (ill->ill_waiters == 0)
5195 		cv_broadcast(&ill->ill_cv);
5196 	mutex_exit(&ill->ill_lock);
5197 }
5198 
5199 /*
5200  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5201  * driver.  We construct best guess defaults for lower level information that
5202  * we need.  If an interface is brought up without injection of any overriding
5203  * information from outside, we have to be ready to go with these defaults.
5204  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5205  * we primarely want the dl_provider_style.
5206  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5207  * at which point we assume the other part of the information is valid.
5208  */
5209 void
5210 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5211 {
5212 	uchar_t		*brdcst_addr;
5213 	uint_t		brdcst_addr_length, phys_addr_length;
5214 	t_scalar_t	sap_length;
5215 	dl_info_ack_t	*dlia;
5216 	ip_m_t		*ipm;
5217 	dl_qos_cl_sel1_t *sel1;
5218 	int		min_mtu;
5219 
5220 	ASSERT(IAM_WRITER_ILL(ill));
5221 
5222 	/*
5223 	 * Till the ill is fully up ILL_CHANGING will be set and
5224 	 * the ill is not globally visible. So no need for a lock.
5225 	 */
5226 	dlia = (dl_info_ack_t *)mp->b_rptr;
5227 	ill->ill_mactype = dlia->dl_mac_type;
5228 
5229 	ipm = ip_m_lookup(dlia->dl_mac_type);
5230 	if (ipm == NULL) {
5231 		ipm = ip_m_lookup(DL_OTHER);
5232 		ASSERT(ipm != NULL);
5233 	}
5234 	ill->ill_media = ipm;
5235 
5236 	/*
5237 	 * When the new DLPI stuff is ready we'll pull lengths
5238 	 * from dlia.
5239 	 */
5240 	if (dlia->dl_version == DL_VERSION_2) {
5241 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5242 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5243 		    brdcst_addr_length);
5244 		if (brdcst_addr == NULL) {
5245 			brdcst_addr_length = 0;
5246 		}
5247 		sap_length = dlia->dl_sap_length;
5248 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5249 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5250 		    brdcst_addr_length, sap_length, phys_addr_length));
5251 	} else {
5252 		brdcst_addr_length = 6;
5253 		brdcst_addr = ip_six_byte_all_ones;
5254 		sap_length = -2;
5255 		phys_addr_length = brdcst_addr_length;
5256 	}
5257 
5258 	ill->ill_bcast_addr_length = brdcst_addr_length;
5259 	ill->ill_phys_addr_length = phys_addr_length;
5260 	ill->ill_sap_length = sap_length;
5261 
5262 	/*
5263 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5264 	 * but we must ensure a minimum IP MTU is used since other bits of
5265 	 * IP will fly apart otherwise.
5266 	 */
5267 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5268 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5269 	ill->ill_max_mtu = ill->ill_max_frag;
5270 
5271 	ill->ill_type = ipm->ip_m_type;
5272 
5273 	if (!ill->ill_dlpi_style_set) {
5274 		if (dlia->dl_provider_style == DL_STYLE2)
5275 			ill->ill_needs_attach = 1;
5276 
5277 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
5278 
5279 		/*
5280 		 * Allocate the first ipif on this ill.  We don't delay it
5281 		 * further as ioctl handling assumes at least one ipif exists.
5282 		 *
5283 		 * At this point we don't know whether the ill is v4 or v6.
5284 		 * We will know this whan the SIOCSLIFNAME happens and
5285 		 * the correct value for ill_isv6 will be assigned in
5286 		 * ipif_set_values(). We need to hold the ill lock and
5287 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5288 		 * the wakeup.
5289 		 */
5290 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5291 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5292 		mutex_enter(&ill->ill_lock);
5293 		ASSERT(ill->ill_dlpi_style_set == 0);
5294 		ill->ill_dlpi_style_set = 1;
5295 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5296 		cv_broadcast(&ill->ill_cv);
5297 		mutex_exit(&ill->ill_lock);
5298 		freemsg(mp);
5299 		return;
5300 	}
5301 	ASSERT(ill->ill_ipif != NULL);
5302 	/*
5303 	 * We know whether it is IPv4 or IPv6 now, as this is the
5304 	 * second DL_INFO_ACK we are recieving in response to the
5305 	 * DL_INFO_REQ sent in ipif_set_values.
5306 	 */
5307 	if (ill->ill_isv6)
5308 		ill->ill_sap = IP6_DL_SAP;
5309 	else
5310 		ill->ill_sap = IP_DL_SAP;
5311 	/*
5312 	 * Set ipif_mtu which is used to set the IRE's
5313 	 * ire_max_frag value. The driver could have sent
5314 	 * a different mtu from what it sent last time. No
5315 	 * need to call ipif_mtu_change because IREs have
5316 	 * not yet been created.
5317 	 */
5318 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5319 	/*
5320 	 * Clear all the flags that were set based on ill_bcast_addr_length
5321 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5322 	 * changed now and we need to re-evaluate.
5323 	 */
5324 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5325 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5326 
5327 	/*
5328 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5329 	 * changed now.
5330 	 *
5331 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5332 	 * with no underlying interfaces (and thus an unknown broadcast
5333 	 * address length), but we enforce that an interface is broadcast-
5334 	 * capable as part of allowing it to join a group.
5335 	 */
5336 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5337 		if (ill->ill_resolver_mp != NULL)
5338 			freemsg(ill->ill_resolver_mp);
5339 		if (ill->ill_bcast_mp != NULL)
5340 			freemsg(ill->ill_bcast_mp);
5341 		if (ill->ill_flags & ILLF_XRESOLV)
5342 			ill->ill_net_type = IRE_IF_RESOLVER;
5343 		else
5344 			ill->ill_net_type = IRE_IF_NORESOLVER;
5345 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5346 		    ill->ill_phys_addr_length,
5347 		    ill->ill_sap,
5348 		    ill->ill_sap_length);
5349 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5350 
5351 		if (ill->ill_isv6)
5352 			/*
5353 			 * Note: xresolv interfaces will eventually need NOARP
5354 			 * set here as well, but that will require those
5355 			 * external resolvers to have some knowledge of
5356 			 * that flag and act appropriately. Not to be changed
5357 			 * at present.
5358 			 */
5359 			ill->ill_flags |= ILLF_NONUD;
5360 		else
5361 			ill->ill_flags |= ILLF_NOARP;
5362 
5363 		if (ill->ill_phys_addr_length == 0) {
5364 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5365 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5366 			} else {
5367 				/* pt-pt supports multicast. */
5368 				ill->ill_flags |= ILLF_MULTICAST;
5369 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5370 			}
5371 		}
5372 	} else {
5373 		ill->ill_net_type = IRE_IF_RESOLVER;
5374 		if (ill->ill_bcast_mp != NULL)
5375 			freemsg(ill->ill_bcast_mp);
5376 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5377 		    ill->ill_bcast_addr_length, ill->ill_sap,
5378 		    ill->ill_sap_length);
5379 		/*
5380 		 * Later detect lack of DLPI driver multicast
5381 		 * capability by catching DL_ENABMULTI errors in
5382 		 * ip_rput_dlpi.
5383 		 */
5384 		ill->ill_flags |= ILLF_MULTICAST;
5385 		if (!ill->ill_isv6)
5386 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5387 	}
5388 
5389 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
5390 	if (ill->ill_mactype == SUNW_DL_IPMP)
5391 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5392 
5393 	/* By default an interface does not support any CoS marking */
5394 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5395 
5396 	/*
5397 	 * If we get QoS information in DL_INFO_ACK, the device supports
5398 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5399 	 */
5400 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5401 	    dlia->dl_qos_length);
5402 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5403 		ill->ill_flags |= ILLF_COS_ENABLED;
5404 	}
5405 
5406 	/* Clear any previous error indication. */
5407 	ill->ill_error = 0;
5408 	freemsg(mp);
5409 }
5410 
5411 /*
5412  * Perform various checks to verify that an address would make sense as a
5413  * local, remote, or subnet interface address.
5414  */
5415 static boolean_t
5416 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5417 {
5418 	ipaddr_t	net_mask;
5419 
5420 	/*
5421 	 * Don't allow all zeroes, or all ones, but allow
5422 	 * all ones netmask.
5423 	 */
5424 	if ((net_mask = ip_net_mask(addr)) == 0)
5425 		return (B_FALSE);
5426 	/* A given netmask overrides the "guess" netmask */
5427 	if (subnet_mask != 0)
5428 		net_mask = subnet_mask;
5429 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5430 	    (addr == (addr | ~net_mask)))) {
5431 		return (B_FALSE);
5432 	}
5433 
5434 	/*
5435 	 * Even if the netmask is all ones, we do not allow address to be
5436 	 * 255.255.255.255
5437 	 */
5438 	if (addr == INADDR_BROADCAST)
5439 		return (B_FALSE);
5440 
5441 	if (CLASSD(addr))
5442 		return (B_FALSE);
5443 
5444 	return (B_TRUE);
5445 }
5446 
5447 #define	V6_IPIF_LINKLOCAL(p)	\
5448 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5449 
5450 /*
5451  * Compare two given ipifs and check if the second one is better than
5452  * the first one using the order of preference (not taking deprecated
5453  * into acount) specified in ipif_lookup_multicast().
5454  */
5455 static boolean_t
5456 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5457 {
5458 	/* Check the least preferred first. */
5459 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5460 		/* If both ipifs are the same, use the first one. */
5461 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5462 			return (B_FALSE);
5463 		else
5464 			return (B_TRUE);
5465 	}
5466 
5467 	/* For IPv6, check for link local address. */
5468 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5469 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5470 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5471 			/* The second one is equal or less preferred. */
5472 			return (B_FALSE);
5473 		} else {
5474 			return (B_TRUE);
5475 		}
5476 	}
5477 
5478 	/* Then check for point to point interface. */
5479 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5480 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5481 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5482 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5483 			return (B_FALSE);
5484 		} else {
5485 			return (B_TRUE);
5486 		}
5487 	}
5488 
5489 	/* old_ipif is a normal interface, so no need to use the new one. */
5490 	return (B_FALSE);
5491 }
5492 
5493 /*
5494  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5495  * The ipif must be up, and its ill must multicast-capable, not
5496  * condemned, not an underlying interface in an IPMP group, and
5497  * not a VNI interface.  Order of preference:
5498  *
5499  * 	1a. normal
5500  * 	1b. normal, but deprecated
5501  * 	2a. point to point
5502  * 	2b. point to point, but deprecated
5503  * 	3a. link local
5504  * 	3b. link local, but deprecated
5505  * 	4. loopback.
5506  */
5507 ipif_t *
5508 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5509 {
5510 	ill_t			*ill;
5511 	ill_walk_context_t	ctx;
5512 	ipif_t			*ipif;
5513 	ipif_t			*saved_ipif = NULL;
5514 	ipif_t			*dep_ipif = NULL;
5515 
5516 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5517 	if (isv6)
5518 		ill = ILL_START_WALK_V6(&ctx, ipst);
5519 	else
5520 		ill = ILL_START_WALK_V4(&ctx, ipst);
5521 
5522 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5523 		mutex_enter(&ill->ill_lock);
5524 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5525 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5526 			mutex_exit(&ill->ill_lock);
5527 			continue;
5528 		}
5529 		for (ipif = ill->ill_ipif; ipif != NULL;
5530 		    ipif = ipif->ipif_next) {
5531 			if (zoneid != ipif->ipif_zoneid &&
5532 			    zoneid != ALL_ZONES &&
5533 			    ipif->ipif_zoneid != ALL_ZONES) {
5534 				continue;
5535 			}
5536 			if (!(ipif->ipif_flags & IPIF_UP) ||
5537 			    !IPIF_CAN_LOOKUP(ipif)) {
5538 				continue;
5539 			}
5540 
5541 			/*
5542 			 * Found one candidate.  If it is deprecated,
5543 			 * remember it in dep_ipif.  If it is not deprecated,
5544 			 * remember it in saved_ipif.
5545 			 */
5546 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5547 				if (dep_ipif == NULL) {
5548 					dep_ipif = ipif;
5549 				} else if (ipif_comp_multi(dep_ipif, ipif,
5550 				    isv6)) {
5551 					/*
5552 					 * If the previous dep_ipif does not
5553 					 * belong to the same ill, we've done
5554 					 * a ipif_refhold() on it.  So we need
5555 					 * to release it.
5556 					 */
5557 					if (dep_ipif->ipif_ill != ill)
5558 						ipif_refrele(dep_ipif);
5559 					dep_ipif = ipif;
5560 				}
5561 				continue;
5562 			}
5563 			if (saved_ipif == NULL) {
5564 				saved_ipif = ipif;
5565 			} else {
5566 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5567 					if (saved_ipif->ipif_ill != ill)
5568 						ipif_refrele(saved_ipif);
5569 					saved_ipif = ipif;
5570 				}
5571 			}
5572 		}
5573 		/*
5574 		 * Before going to the next ill, do a ipif_refhold() on the
5575 		 * saved ones.
5576 		 */
5577 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5578 			ipif_refhold_locked(saved_ipif);
5579 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5580 			ipif_refhold_locked(dep_ipif);
5581 		mutex_exit(&ill->ill_lock);
5582 	}
5583 	rw_exit(&ipst->ips_ill_g_lock);
5584 
5585 	/*
5586 	 * If we have only the saved_ipif, return it.  But if we have both
5587 	 * saved_ipif and dep_ipif, check to see which one is better.
5588 	 */
5589 	if (saved_ipif != NULL) {
5590 		if (dep_ipif != NULL) {
5591 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5592 				ipif_refrele(saved_ipif);
5593 				return (dep_ipif);
5594 			} else {
5595 				ipif_refrele(dep_ipif);
5596 				return (saved_ipif);
5597 			}
5598 		}
5599 		return (saved_ipif);
5600 	} else {
5601 		return (dep_ipif);
5602 	}
5603 }
5604 
5605 /*
5606  * This function is called when an application does not specify an interface
5607  * to be used for multicast traffic (joining a group/sending data).  It
5608  * calls ire_lookup_multi() to look for an interface route for the
5609  * specified multicast group.  Doing this allows the administrator to add
5610  * prefix routes for multicast to indicate which interface to be used for
5611  * multicast traffic in the above scenario.  The route could be for all
5612  * multicast (224.0/4), for a single multicast group (a /32 route) or
5613  * anything in between.  If there is no such multicast route, we just find
5614  * any multicast capable interface and return it.  The returned ipif
5615  * is refhold'ed.
5616  */
5617 ipif_t *
5618 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5619 {
5620 	ire_t			*ire;
5621 	ipif_t			*ipif;
5622 
5623 	ire = ire_lookup_multi(group, zoneid, ipst);
5624 	if (ire != NULL) {
5625 		ipif = ire->ire_ipif;
5626 		ipif_refhold(ipif);
5627 		ire_refrele(ire);
5628 		return (ipif);
5629 	}
5630 
5631 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5632 }
5633 
5634 /*
5635  * Look for an ipif with the specified interface address and destination.
5636  * The destination address is used only for matching point-to-point interfaces.
5637  */
5638 ipif_t *
5639 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5640     ipsq_func_t func, int *error, ip_stack_t *ipst)
5641 {
5642 	ipif_t	*ipif;
5643 	ill_t	*ill;
5644 	ill_walk_context_t ctx;
5645 	ipsq_t	*ipsq;
5646 
5647 	if (error != NULL)
5648 		*error = 0;
5649 
5650 	/*
5651 	 * First match all the point-to-point interfaces
5652 	 * before looking at non-point-to-point interfaces.
5653 	 * This is done to avoid returning non-point-to-point
5654 	 * ipif instead of unnumbered point-to-point ipif.
5655 	 */
5656 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5657 	ill = ILL_START_WALK_V4(&ctx, ipst);
5658 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5659 		GRAB_CONN_LOCK(q);
5660 		mutex_enter(&ill->ill_lock);
5661 		for (ipif = ill->ill_ipif; ipif != NULL;
5662 		    ipif = ipif->ipif_next) {
5663 			/* Allow the ipif to be down */
5664 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5665 			    (ipif->ipif_lcl_addr == if_addr) &&
5666 			    (ipif->ipif_pp_dst_addr == dst)) {
5667 				/*
5668 				 * The block comment at the start of ipif_down
5669 				 * explains the use of the macros used below
5670 				 */
5671 				if (IPIF_CAN_LOOKUP(ipif)) {
5672 					ipif_refhold_locked(ipif);
5673 					mutex_exit(&ill->ill_lock);
5674 					RELEASE_CONN_LOCK(q);
5675 					rw_exit(&ipst->ips_ill_g_lock);
5676 					return (ipif);
5677 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5678 					ipsq = ill->ill_phyint->phyint_ipsq;
5679 					mutex_enter(&ipsq->ipsq_lock);
5680 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5681 					mutex_exit(&ill->ill_lock);
5682 					rw_exit(&ipst->ips_ill_g_lock);
5683 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5684 					    ill);
5685 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5686 					mutex_exit(&ipsq->ipsq_lock);
5687 					RELEASE_CONN_LOCK(q);
5688 					if (error != NULL)
5689 						*error = EINPROGRESS;
5690 					return (NULL);
5691 				}
5692 			}
5693 		}
5694 		mutex_exit(&ill->ill_lock);
5695 		RELEASE_CONN_LOCK(q);
5696 	}
5697 	rw_exit(&ipst->ips_ill_g_lock);
5698 
5699 	/* lookup the ipif based on interface address */
5700 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5701 	    ipst);
5702 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5703 	return (ipif);
5704 }
5705 
5706 /*
5707  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5708  */
5709 static ipif_t *
5710 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5711     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5712     ip_stack_t *ipst)
5713 {
5714 	ipif_t  *ipif;
5715 	ill_t   *ill;
5716 	boolean_t ptp = B_FALSE;
5717 	ipsq_t	*ipsq;
5718 	ill_walk_context_t	ctx;
5719 
5720 	if (error != NULL)
5721 		*error = 0;
5722 
5723 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5724 	/*
5725 	 * Repeat twice, first based on local addresses and
5726 	 * next time for pointopoint.
5727 	 */
5728 repeat:
5729 	ill = ILL_START_WALK_V4(&ctx, ipst);
5730 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5731 		if (match_ill != NULL && ill != match_ill &&
5732 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5733 			continue;
5734 		}
5735 		GRAB_CONN_LOCK(q);
5736 		mutex_enter(&ill->ill_lock);
5737 		for (ipif = ill->ill_ipif; ipif != NULL;
5738 		    ipif = ipif->ipif_next) {
5739 			if (zoneid != ALL_ZONES &&
5740 			    zoneid != ipif->ipif_zoneid &&
5741 			    ipif->ipif_zoneid != ALL_ZONES)
5742 				continue;
5743 			/* Allow the ipif to be down */
5744 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5745 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5746 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5747 			    (ipif->ipif_pp_dst_addr == addr))) {
5748 				/*
5749 				 * The block comment at the start of ipif_down
5750 				 * explains the use of the macros used below
5751 				 */
5752 				if (IPIF_CAN_LOOKUP(ipif)) {
5753 					ipif_refhold_locked(ipif);
5754 					mutex_exit(&ill->ill_lock);
5755 					RELEASE_CONN_LOCK(q);
5756 					rw_exit(&ipst->ips_ill_g_lock);
5757 					return (ipif);
5758 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5759 					ipsq = ill->ill_phyint->phyint_ipsq;
5760 					mutex_enter(&ipsq->ipsq_lock);
5761 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5762 					mutex_exit(&ill->ill_lock);
5763 					rw_exit(&ipst->ips_ill_g_lock);
5764 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5765 					    ill);
5766 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5767 					mutex_exit(&ipsq->ipsq_lock);
5768 					RELEASE_CONN_LOCK(q);
5769 					if (error != NULL)
5770 						*error = EINPROGRESS;
5771 					return (NULL);
5772 				}
5773 			}
5774 		}
5775 		mutex_exit(&ill->ill_lock);
5776 		RELEASE_CONN_LOCK(q);
5777 	}
5778 
5779 	/* If we already did the ptp case, then we are done */
5780 	if (ptp) {
5781 		rw_exit(&ipst->ips_ill_g_lock);
5782 		if (error != NULL)
5783 			*error = ENXIO;
5784 		return (NULL);
5785 	}
5786 	ptp = B_TRUE;
5787 	goto repeat;
5788 }
5789 
5790 /*
5791  * Check if the address exists in the system.
5792  * We don't hold the conn_lock as we will not perform defered ipsqueue
5793  * operation.
5794  */
5795 boolean_t
5796 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5797 {
5798 	ipif_t  *ipif;
5799 	ill_t   *ill;
5800 	ill_walk_context_t	ctx;
5801 
5802 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5803 
5804 	ill = ILL_START_WALK_V4(&ctx, ipst);
5805 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5806 		mutex_enter(&ill->ill_lock);
5807 		for (ipif = ill->ill_ipif; ipif != NULL;
5808 		    ipif = ipif->ipif_next) {
5809 			if (zoneid != ALL_ZONES &&
5810 			    zoneid != ipif->ipif_zoneid &&
5811 			    ipif->ipif_zoneid != ALL_ZONES)
5812 				continue;
5813 			/* Allow the ipif to be down */
5814 			/*
5815 			 * XXX Different from ipif_lookup_addr(), we don't do
5816 			 * twice lookups. As from bind()'s point of view, we
5817 			 * may return once we find a match.
5818 			 */
5819 			if (((ipif->ipif_lcl_addr == addr) &&
5820 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5821 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5822 			    (ipif->ipif_pp_dst_addr == addr))) {
5823 				/*
5824 				 * Allow bind() to be successful even if the
5825 				 * ipif is with IPIF_CHANGING bit set.
5826 				 */
5827 				mutex_exit(&ill->ill_lock);
5828 				rw_exit(&ipst->ips_ill_g_lock);
5829 				return (B_TRUE);
5830 			}
5831 		}
5832 		mutex_exit(&ill->ill_lock);
5833 	}
5834 
5835 	rw_exit(&ipst->ips_ill_g_lock);
5836 	return (B_FALSE);
5837 }
5838 
5839 /*
5840  * Lookup an ipif with the specified address.  For point-to-point links we
5841  * look for matches on either the destination address or the local address,
5842  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5843  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5844  * (or illgrp if `match_ill' is in an IPMP group).
5845  */
5846 ipif_t *
5847 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5848     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5849 {
5850 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
5851 	    func, error, ipst));
5852 }
5853 
5854 /*
5855  * Special abbreviated version of ipif_lookup_addr() that doesn't match
5856  * `match_ill' across the IPMP group.  This function is only needed in some
5857  * corner-cases; almost everything should use ipif_lookup_addr().
5858  */
5859 static ipif_t *
5860 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5861 {
5862 	ASSERT(match_ill != NULL);
5863 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
5864 	    NULL, NULL, NULL, NULL, ipst));
5865 }
5866 
5867 /*
5868  * Look for an ipif with the specified address. For point-point links
5869  * we look for matches on either the destination address and the local
5870  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5871  * is set.
5872  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
5873  * ill (or illgrp if `match_ill' is in an IPMP group).
5874  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5875  */
5876 zoneid_t
5877 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5878 {
5879 	zoneid_t zoneid;
5880 	ipif_t  *ipif;
5881 	ill_t   *ill;
5882 	boolean_t ptp = B_FALSE;
5883 	ill_walk_context_t	ctx;
5884 
5885 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5886 	/*
5887 	 * Repeat twice, first based on local addresses and
5888 	 * next time for pointopoint.
5889 	 */
5890 repeat:
5891 	ill = ILL_START_WALK_V4(&ctx, ipst);
5892 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5893 		if (match_ill != NULL && ill != match_ill &&
5894 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
5895 			continue;
5896 		}
5897 		mutex_enter(&ill->ill_lock);
5898 		for (ipif = ill->ill_ipif; ipif != NULL;
5899 		    ipif = ipif->ipif_next) {
5900 			/* Allow the ipif to be down */
5901 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5902 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5903 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5904 			    (ipif->ipif_pp_dst_addr == addr)) &&
5905 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5906 				zoneid = ipif->ipif_zoneid;
5907 				mutex_exit(&ill->ill_lock);
5908 				rw_exit(&ipst->ips_ill_g_lock);
5909 				/*
5910 				 * If ipif_zoneid was ALL_ZONES then we have
5911 				 * a trusted extensions shared IP address.
5912 				 * In that case GLOBAL_ZONEID works to send.
5913 				 */
5914 				if (zoneid == ALL_ZONES)
5915 					zoneid = GLOBAL_ZONEID;
5916 				return (zoneid);
5917 			}
5918 		}
5919 		mutex_exit(&ill->ill_lock);
5920 	}
5921 
5922 	/* If we already did the ptp case, then we are done */
5923 	if (ptp) {
5924 		rw_exit(&ipst->ips_ill_g_lock);
5925 		return (ALL_ZONES);
5926 	}
5927 	ptp = B_TRUE;
5928 	goto repeat;
5929 }
5930 
5931 /*
5932  * Look for an ipif that matches the specified remote address i.e. the
5933  * ipif that would receive the specified packet.
5934  * First look for directly connected interfaces and then do a recursive
5935  * IRE lookup and pick the first ipif corresponding to the source address in the
5936  * ire.
5937  * Returns: held ipif
5938  */
5939 ipif_t *
5940 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5941 {
5942 	ipif_t	*ipif;
5943 	ire_t	*ire;
5944 	ip_stack_t	*ipst = ill->ill_ipst;
5945 
5946 	ASSERT(!ill->ill_isv6);
5947 
5948 	/*
5949 	 * Someone could be changing this ipif currently or change it
5950 	 * after we return this. Thus  a few packets could use the old
5951 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5952 	 * will atomically be updated or cleaned up with the new value
5953 	 * Thus we don't need a lock to check the flags or other attrs below.
5954 	 */
5955 	mutex_enter(&ill->ill_lock);
5956 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5957 		if (!IPIF_CAN_LOOKUP(ipif))
5958 			continue;
5959 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5960 		    ipif->ipif_zoneid != ALL_ZONES)
5961 			continue;
5962 		/* Allow the ipif to be down */
5963 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5964 			if ((ipif->ipif_pp_dst_addr == addr) ||
5965 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5966 			    ipif->ipif_lcl_addr == addr)) {
5967 				ipif_refhold_locked(ipif);
5968 				mutex_exit(&ill->ill_lock);
5969 				return (ipif);
5970 			}
5971 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5972 			ipif_refhold_locked(ipif);
5973 			mutex_exit(&ill->ill_lock);
5974 			return (ipif);
5975 		}
5976 	}
5977 	mutex_exit(&ill->ill_lock);
5978 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5979 	    NULL, MATCH_IRE_RECURSIVE, ipst);
5980 	if (ire != NULL) {
5981 		/*
5982 		 * The callers of this function wants to know the
5983 		 * interface on which they have to send the replies
5984 		 * back. For IREs that have ire_stq and ire_ipif
5985 		 * derived from different ills, we really don't care
5986 		 * what we return here.
5987 		 */
5988 		ipif = ire->ire_ipif;
5989 		if (ipif != NULL) {
5990 			ipif_refhold(ipif);
5991 			ire_refrele(ire);
5992 			return (ipif);
5993 		}
5994 		ire_refrele(ire);
5995 	}
5996 	/* Pick the first interface */
5997 	ipif = ipif_get_next_ipif(NULL, ill);
5998 	return (ipif);
5999 }
6000 
6001 /*
6002  * This func does not prevent refcnt from increasing. But if
6003  * the caller has taken steps to that effect, then this func
6004  * can be used to determine whether the ill has become quiescent
6005  */
6006 static boolean_t
6007 ill_is_quiescent(ill_t *ill)
6008 {
6009 	ipif_t	*ipif;
6010 
6011 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6012 
6013 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6014 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6015 			return (B_FALSE);
6016 		}
6017 	}
6018 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6019 		return (B_FALSE);
6020 	}
6021 	return (B_TRUE);
6022 }
6023 
6024 boolean_t
6025 ill_is_freeable(ill_t *ill)
6026 {
6027 	ipif_t	*ipif;
6028 
6029 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6030 
6031 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6032 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6033 			return (B_FALSE);
6034 		}
6035 	}
6036 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6037 		return (B_FALSE);
6038 	}
6039 	return (B_TRUE);
6040 }
6041 
6042 /*
6043  * This func does not prevent refcnt from increasing. But if
6044  * the caller has taken steps to that effect, then this func
6045  * can be used to determine whether the ipif has become quiescent
6046  */
6047 static boolean_t
6048 ipif_is_quiescent(ipif_t *ipif)
6049 {
6050 	ill_t *ill;
6051 
6052 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6053 
6054 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6055 		return (B_FALSE);
6056 	}
6057 
6058 	ill = ipif->ipif_ill;
6059 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6060 	    ill->ill_logical_down) {
6061 		return (B_TRUE);
6062 	}
6063 
6064 	/* This is the last ipif going down or being deleted on this ill */
6065 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6066 		return (B_FALSE);
6067 	}
6068 
6069 	return (B_TRUE);
6070 }
6071 
6072 /*
6073  * return true if the ipif can be destroyed: the ipif has to be quiescent
6074  * with zero references from ire/nce/ilm to it.
6075  */
6076 static boolean_t
6077 ipif_is_freeable(ipif_t *ipif)
6078 {
6079 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6080 	ASSERT(ipif->ipif_id != 0);
6081 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6082 }
6083 
6084 /*
6085  * The ipif/ill/ire has been refreled. Do the tail processing.
6086  * Determine if the ipif or ill in question has become quiescent and if so
6087  * wakeup close and/or restart any queued pending ioctl that is waiting
6088  * for the ipif_down (or ill_down)
6089  */
6090 void
6091 ipif_ill_refrele_tail(ill_t *ill)
6092 {
6093 	mblk_t	*mp;
6094 	conn_t	*connp;
6095 	ipsq_t	*ipsq;
6096 	ipxop_t	*ipx;
6097 	ipif_t	*ipif;
6098 	dl_notify_ind_t *dlindp;
6099 
6100 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6101 
6102 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6103 		/* ip_modclose() may be waiting */
6104 		cv_broadcast(&ill->ill_cv);
6105 	}
6106 
6107 	ipsq = ill->ill_phyint->phyint_ipsq;
6108 	mutex_enter(&ipsq->ipsq_lock);
6109 	ipx = ipsq->ipsq_xop;
6110 	mutex_enter(&ipx->ipx_lock);
6111 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6112 		goto unlock;
6113 
6114 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6115 
6116 	ipif = ipx->ipx_pending_ipif;
6117 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6118 		goto unlock;
6119 
6120 	switch (ipx->ipx_waitfor) {
6121 	case IPIF_DOWN:
6122 		if (!ipif_is_quiescent(ipif))
6123 			goto unlock;
6124 		break;
6125 	case IPIF_FREE:
6126 		if (!ipif_is_freeable(ipif))
6127 			goto unlock;
6128 		break;
6129 	case ILL_DOWN:
6130 		if (!ill_is_quiescent(ill))
6131 			goto unlock;
6132 		break;
6133 	case ILL_FREE:
6134 		/*
6135 		 * ILL_FREE is only for loopback; normal ill teardown waits
6136 		 * synchronously in ip_modclose() without using ipx_waitfor,
6137 		 * handled by the cv_broadcast() at the top of this function.
6138 		 */
6139 		if (!ill_is_freeable(ill))
6140 			goto unlock;
6141 		break;
6142 	default:
6143 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6144 		    (void *)ipsq, ipx->ipx_waitfor);
6145 	}
6146 
6147 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6148 	mutex_exit(&ipx->ipx_lock);
6149 	mp = ipsq_pending_mp_get(ipsq, &connp);
6150 	mutex_exit(&ipsq->ipsq_lock);
6151 	mutex_exit(&ill->ill_lock);
6152 
6153 	ASSERT(mp != NULL);
6154 	/*
6155 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6156 	 * we can only get here when the current operation decides it
6157 	 * it needs to quiesce via ipsq_pending_mp_add().
6158 	 */
6159 	switch (mp->b_datap->db_type) {
6160 	case M_PCPROTO:
6161 	case M_PROTO:
6162 		/*
6163 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6164 		 */
6165 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6166 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6167 
6168 		switch (dlindp->dl_notification) {
6169 		case DL_NOTE_PHYS_ADDR:
6170 			qwriter_ip(ill, ill->ill_rq, mp,
6171 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6172 			return;
6173 		case DL_NOTE_REPLUMB:
6174 			qwriter_ip(ill, ill->ill_rq, mp,
6175 			    ill_replumb_tail, CUR_OP, B_TRUE);
6176 			return;
6177 		default:
6178 			ASSERT(0);
6179 			ill_refrele(ill);
6180 		}
6181 		break;
6182 
6183 	case M_ERROR:
6184 	case M_HANGUP:
6185 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6186 		    B_TRUE);
6187 		return;
6188 
6189 	case M_IOCTL:
6190 	case M_IOCDATA:
6191 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6192 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6193 		return;
6194 
6195 	default:
6196 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6197 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6198 	}
6199 	return;
6200 unlock:
6201 	mutex_exit(&ipsq->ipsq_lock);
6202 	mutex_exit(&ipx->ipx_lock);
6203 	mutex_exit(&ill->ill_lock);
6204 }
6205 
6206 #ifdef DEBUG
6207 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6208 static void
6209 th_trace_rrecord(th_trace_t *th_trace)
6210 {
6211 	tr_buf_t *tr_buf;
6212 	uint_t lastref;
6213 
6214 	lastref = th_trace->th_trace_lastref;
6215 	lastref++;
6216 	if (lastref == TR_BUF_MAX)
6217 		lastref = 0;
6218 	th_trace->th_trace_lastref = lastref;
6219 	tr_buf = &th_trace->th_trbuf[lastref];
6220 	tr_buf->tr_time = lbolt;
6221 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6222 }
6223 
6224 static void
6225 th_trace_free(void *value)
6226 {
6227 	th_trace_t *th_trace = value;
6228 
6229 	ASSERT(th_trace->th_refcnt == 0);
6230 	kmem_free(th_trace, sizeof (*th_trace));
6231 }
6232 
6233 /*
6234  * Find or create the per-thread hash table used to track object references.
6235  * The ipst argument is NULL if we shouldn't allocate.
6236  *
6237  * Accesses per-thread data, so there's no need to lock here.
6238  */
6239 static mod_hash_t *
6240 th_trace_gethash(ip_stack_t *ipst)
6241 {
6242 	th_hash_t *thh;
6243 
6244 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6245 		mod_hash_t *mh;
6246 		char name[256];
6247 		size_t objsize, rshift;
6248 		int retv;
6249 
6250 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6251 			return (NULL);
6252 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6253 		    (void *)curthread);
6254 
6255 		/*
6256 		 * We use mod_hash_create_extended here rather than the more
6257 		 * obvious mod_hash_create_ptrhash because the latter has a
6258 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6259 		 * block.
6260 		 */
6261 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6262 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6263 		rshift = highbit(objsize);
6264 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6265 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6266 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6267 		if (mh == NULL) {
6268 			kmem_free(thh, sizeof (*thh));
6269 			return (NULL);
6270 		}
6271 		thh->thh_hash = mh;
6272 		thh->thh_ipst = ipst;
6273 		/*
6274 		 * We trace ills, ipifs, ires, and nces.  All of these are
6275 		 * per-IP-stack, so the lock on the thread list is as well.
6276 		 */
6277 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6278 		list_insert_tail(&ip_thread_list, thh);
6279 		rw_exit(&ip_thread_rwlock);
6280 		retv = tsd_set(ip_thread_data, thh);
6281 		ASSERT(retv == 0);
6282 	}
6283 	return (thh != NULL ? thh->thh_hash : NULL);
6284 }
6285 
6286 boolean_t
6287 th_trace_ref(const void *obj, ip_stack_t *ipst)
6288 {
6289 	th_trace_t *th_trace;
6290 	mod_hash_t *mh;
6291 	mod_hash_val_t val;
6292 
6293 	if ((mh = th_trace_gethash(ipst)) == NULL)
6294 		return (B_FALSE);
6295 
6296 	/*
6297 	 * Attempt to locate the trace buffer for this obj and thread.
6298 	 * If it does not exist, then allocate a new trace buffer and
6299 	 * insert into the hash.
6300 	 */
6301 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6302 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6303 		if (th_trace == NULL)
6304 			return (B_FALSE);
6305 
6306 		th_trace->th_id = curthread;
6307 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6308 		    (mod_hash_val_t)th_trace) != 0) {
6309 			kmem_free(th_trace, sizeof (th_trace_t));
6310 			return (B_FALSE);
6311 		}
6312 	} else {
6313 		th_trace = (th_trace_t *)val;
6314 	}
6315 
6316 	ASSERT(th_trace->th_refcnt >= 0 &&
6317 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6318 
6319 	th_trace->th_refcnt++;
6320 	th_trace_rrecord(th_trace);
6321 	return (B_TRUE);
6322 }
6323 
6324 /*
6325  * For the purpose of tracing a reference release, we assume that global
6326  * tracing is always on and that the same thread initiated the reference hold
6327  * is releasing.
6328  */
6329 void
6330 th_trace_unref(const void *obj)
6331 {
6332 	int retv;
6333 	mod_hash_t *mh;
6334 	th_trace_t *th_trace;
6335 	mod_hash_val_t val;
6336 
6337 	mh = th_trace_gethash(NULL);
6338 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6339 	ASSERT(retv == 0);
6340 	th_trace = (th_trace_t *)val;
6341 
6342 	ASSERT(th_trace->th_refcnt > 0);
6343 	th_trace->th_refcnt--;
6344 	th_trace_rrecord(th_trace);
6345 }
6346 
6347 /*
6348  * If tracing has been disabled, then we assume that the reference counts are
6349  * now useless, and we clear them out before destroying the entries.
6350  */
6351 void
6352 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6353 {
6354 	th_hash_t	*thh;
6355 	mod_hash_t	*mh;
6356 	mod_hash_val_t	val;
6357 	th_trace_t	*th_trace;
6358 	int		retv;
6359 
6360 	rw_enter(&ip_thread_rwlock, RW_READER);
6361 	for (thh = list_head(&ip_thread_list); thh != NULL;
6362 	    thh = list_next(&ip_thread_list, thh)) {
6363 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6364 		    &val) == 0) {
6365 			th_trace = (th_trace_t *)val;
6366 			if (trace_disable)
6367 				th_trace->th_refcnt = 0;
6368 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6369 			ASSERT(retv == 0);
6370 		}
6371 	}
6372 	rw_exit(&ip_thread_rwlock);
6373 }
6374 
6375 void
6376 ipif_trace_ref(ipif_t *ipif)
6377 {
6378 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6379 
6380 	if (ipif->ipif_trace_disable)
6381 		return;
6382 
6383 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6384 		ipif->ipif_trace_disable = B_TRUE;
6385 		ipif_trace_cleanup(ipif);
6386 	}
6387 }
6388 
6389 void
6390 ipif_untrace_ref(ipif_t *ipif)
6391 {
6392 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6393 
6394 	if (!ipif->ipif_trace_disable)
6395 		th_trace_unref(ipif);
6396 }
6397 
6398 void
6399 ill_trace_ref(ill_t *ill)
6400 {
6401 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6402 
6403 	if (ill->ill_trace_disable)
6404 		return;
6405 
6406 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6407 		ill->ill_trace_disable = B_TRUE;
6408 		ill_trace_cleanup(ill);
6409 	}
6410 }
6411 
6412 void
6413 ill_untrace_ref(ill_t *ill)
6414 {
6415 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6416 
6417 	if (!ill->ill_trace_disable)
6418 		th_trace_unref(ill);
6419 }
6420 
6421 /*
6422  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6423  * failure, ipif_trace_disable is set.
6424  */
6425 static void
6426 ipif_trace_cleanup(const ipif_t *ipif)
6427 {
6428 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6429 }
6430 
6431 /*
6432  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6433  * failure, ill_trace_disable is set.
6434  */
6435 static void
6436 ill_trace_cleanup(const ill_t *ill)
6437 {
6438 	th_trace_cleanup(ill, ill->ill_trace_disable);
6439 }
6440 #endif /* DEBUG */
6441 
6442 void
6443 ipif_refhold_locked(ipif_t *ipif)
6444 {
6445 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6446 	ipif->ipif_refcnt++;
6447 	IPIF_TRACE_REF(ipif);
6448 }
6449 
6450 void
6451 ipif_refhold(ipif_t *ipif)
6452 {
6453 	ill_t	*ill;
6454 
6455 	ill = ipif->ipif_ill;
6456 	mutex_enter(&ill->ill_lock);
6457 	ipif->ipif_refcnt++;
6458 	IPIF_TRACE_REF(ipif);
6459 	mutex_exit(&ill->ill_lock);
6460 }
6461 
6462 /*
6463  * Must not be called while holding any locks. Otherwise if this is
6464  * the last reference to be released there is a chance of recursive mutex
6465  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6466  * to restart an ioctl.
6467  */
6468 void
6469 ipif_refrele(ipif_t *ipif)
6470 {
6471 	ill_t	*ill;
6472 
6473 	ill = ipif->ipif_ill;
6474 
6475 	mutex_enter(&ill->ill_lock);
6476 	ASSERT(ipif->ipif_refcnt != 0);
6477 	ipif->ipif_refcnt--;
6478 	IPIF_UNTRACE_REF(ipif);
6479 	if (ipif->ipif_refcnt != 0) {
6480 		mutex_exit(&ill->ill_lock);
6481 		return;
6482 	}
6483 
6484 	/* Drops the ill_lock */
6485 	ipif_ill_refrele_tail(ill);
6486 }
6487 
6488 ipif_t *
6489 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6490 {
6491 	ipif_t	*ipif;
6492 
6493 	mutex_enter(&ill->ill_lock);
6494 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6495 	    ipif != NULL; ipif = ipif->ipif_next) {
6496 		if (!IPIF_CAN_LOOKUP(ipif))
6497 			continue;
6498 		ipif_refhold_locked(ipif);
6499 		mutex_exit(&ill->ill_lock);
6500 		return (ipif);
6501 	}
6502 	mutex_exit(&ill->ill_lock);
6503 	return (NULL);
6504 }
6505 
6506 /*
6507  * TODO: make this table extendible at run time
6508  * Return a pointer to the mac type info for 'mac_type'
6509  */
6510 static ip_m_t *
6511 ip_m_lookup(t_uscalar_t mac_type)
6512 {
6513 	ip_m_t	*ipm;
6514 
6515 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6516 		if (ipm->ip_m_mac_type == mac_type)
6517 			return (ipm);
6518 	return (NULL);
6519 }
6520 
6521 /*
6522  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6523  * ipif_arg is passed in to associate it with the correct interface.
6524  * We may need to restart this operation if the ipif cannot be looked up
6525  * due to an exclusive operation that is currently in progress. The restart
6526  * entry point is specified by 'func'
6527  */
6528 int
6529 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6530     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6531     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6532     struct rtsa_s *sp, ip_stack_t *ipst)
6533 {
6534 	ire_t	*ire;
6535 	ire_t	*gw_ire = NULL;
6536 	ipif_t	*ipif = NULL;
6537 	boolean_t ipif_refheld = B_FALSE;
6538 	uint_t	type;
6539 	int	match_flags = MATCH_IRE_TYPE;
6540 	int	error;
6541 	tsol_gc_t *gc = NULL;
6542 	tsol_gcgrp_t *gcgrp = NULL;
6543 	boolean_t gcgrp_xtraref = B_FALSE;
6544 
6545 	ip1dbg(("ip_rt_add:"));
6546 
6547 	if (ire_arg != NULL)
6548 		*ire_arg = NULL;
6549 
6550 	/*
6551 	 * If this is the case of RTF_HOST being set, then we set the netmask
6552 	 * to all ones (regardless if one was supplied).
6553 	 */
6554 	if (flags & RTF_HOST)
6555 		mask = IP_HOST_MASK;
6556 
6557 	/*
6558 	 * Prevent routes with a zero gateway from being created (since
6559 	 * interfaces can currently be plumbed and brought up no assigned
6560 	 * address).
6561 	 */
6562 	if (gw_addr == 0)
6563 		return (ENETUNREACH);
6564 	/*
6565 	 * Get the ipif, if any, corresponding to the gw_addr
6566 	 */
6567 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6568 	    ipst);
6569 	if (ipif != NULL) {
6570 		if (IS_VNI(ipif->ipif_ill)) {
6571 			ipif_refrele(ipif);
6572 			return (EINVAL);
6573 		}
6574 		ipif_refheld = B_TRUE;
6575 	} else if (error == EINPROGRESS) {
6576 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6577 		return (EINPROGRESS);
6578 	} else {
6579 		error = 0;
6580 	}
6581 
6582 	if (ipif != NULL) {
6583 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6584 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6585 	} else {
6586 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6587 	}
6588 
6589 	/*
6590 	 * GateD will attempt to create routes with a loopback interface
6591 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6592 	 * these routes to be added, but create them as interface routes
6593 	 * since the gateway is an interface address.
6594 	 */
6595 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6596 		flags &= ~RTF_GATEWAY;
6597 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6598 		    mask == IP_HOST_MASK) {
6599 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6600 			    ALL_ZONES, NULL, match_flags, ipst);
6601 			if (ire != NULL) {
6602 				ire_refrele(ire);
6603 				if (ipif_refheld)
6604 					ipif_refrele(ipif);
6605 				return (EEXIST);
6606 			}
6607 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
6608 			    "for 0x%x\n", (void *)ipif,
6609 			    ipif->ipif_ire_type,
6610 			    ntohl(ipif->ipif_lcl_addr)));
6611 			ire = ire_create(
6612 			    (uchar_t *)&dst_addr,	/* dest address */
6613 			    (uchar_t *)&mask,		/* mask */
6614 			    (uchar_t *)&ipif->ipif_src_addr,
6615 			    NULL,			/* no gateway */
6616 			    &ipif->ipif_mtu,
6617 			    NULL,
6618 			    ipif->ipif_rq,		/* recv-from queue */
6619 			    NULL,			/* no send-to queue */
6620 			    ipif->ipif_ire_type,	/* LOOPBACK */
6621 			    ipif,
6622 			    0,
6623 			    0,
6624 			    0,
6625 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6626 			    RTF_PRIVATE : 0,
6627 			    &ire_uinfo_null,
6628 			    NULL,
6629 			    NULL,
6630 			    ipst);
6631 
6632 			if (ire == NULL) {
6633 				if (ipif_refheld)
6634 					ipif_refrele(ipif);
6635 				return (ENOMEM);
6636 			}
6637 			error = ire_add(&ire, q, mp, func, B_FALSE);
6638 			if (error == 0)
6639 				goto save_ire;
6640 			if (ipif_refheld)
6641 				ipif_refrele(ipif);
6642 			return (error);
6643 
6644 		}
6645 	}
6646 
6647 	/*
6648 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6649 	 * and the gateway address provided is one of the system's interface
6650 	 * addresses.  By using the routing socket interface and supplying an
6651 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6652 	 * specifying an interface route to be created is available which uses
6653 	 * the interface index that specifies the outgoing interface rather than
6654 	 * the address of an outgoing interface (which may not be able to
6655 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6656 	 * flag, routes can be specified which not only specify the next-hop to
6657 	 * be used when routing to a certain prefix, but also which outgoing
6658 	 * interface should be used.
6659 	 *
6660 	 * Previously, interfaces would have unique addresses assigned to them
6661 	 * and so the address assigned to a particular interface could be used
6662 	 * to identify a particular interface.  One exception to this was the
6663 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6664 	 *
6665 	 * With the advent of IPv6 and its link-local addresses, this
6666 	 * restriction was relaxed and interfaces could share addresses between
6667 	 * themselves.  In fact, typically all of the link-local interfaces on
6668 	 * an IPv6 node or router will have the same link-local address.  In
6669 	 * order to differentiate between these interfaces, the use of an
6670 	 * interface index is necessary and this index can be carried inside a
6671 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6672 	 * of using the interface index, however, is that all of the ipif's that
6673 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6674 	 * cannot be used to differentiate between ipif's (or logical
6675 	 * interfaces) that belong to the same ill (physical interface).
6676 	 *
6677 	 * For example, in the following case involving IPv4 interfaces and
6678 	 * logical interfaces
6679 	 *
6680 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6681 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6682 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6683 	 *
6684 	 * the ipif's corresponding to each of these interface routes can be
6685 	 * uniquely identified by the "gateway" (actually interface address).
6686 	 *
6687 	 * In this case involving multiple IPv6 default routes to a particular
6688 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6689 	 * default route is of interest:
6690 	 *
6691 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6692 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6693 	 */
6694 
6695 	/* RTF_GATEWAY not set */
6696 	if (!(flags & RTF_GATEWAY)) {
6697 		queue_t	*stq;
6698 
6699 		if (sp != NULL) {
6700 			ip2dbg(("ip_rt_add: gateway security attributes "
6701 			    "cannot be set with interface route\n"));
6702 			if (ipif_refheld)
6703 				ipif_refrele(ipif);
6704 			return (EINVAL);
6705 		}
6706 
6707 		/*
6708 		 * As the interface index specified with the RTA_IFP sockaddr is
6709 		 * the same for all ipif's off of an ill, the matching logic
6710 		 * below uses MATCH_IRE_ILL if such an index was specified.
6711 		 * This means that routes sharing the same prefix when added
6712 		 * using a RTA_IFP sockaddr must have distinct interface
6713 		 * indices (namely, they must be on distinct ill's).
6714 		 *
6715 		 * On the other hand, since the gateway address will usually be
6716 		 * different for each ipif on the system, the matching logic
6717 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6718 		 * route.  This means that interface routes for the same prefix
6719 		 * can be created if they belong to distinct ipif's and if a
6720 		 * RTA_IFP sockaddr is not present.
6721 		 */
6722 		if (ipif_arg != NULL) {
6723 			if (ipif_refheld)  {
6724 				ipif_refrele(ipif);
6725 				ipif_refheld = B_FALSE;
6726 			}
6727 			ipif = ipif_arg;
6728 			match_flags |= MATCH_IRE_ILL;
6729 		} else {
6730 			/*
6731 			 * Check the ipif corresponding to the gw_addr
6732 			 */
6733 			if (ipif == NULL)
6734 				return (ENETUNREACH);
6735 			match_flags |= MATCH_IRE_IPIF;
6736 		}
6737 		ASSERT(ipif != NULL);
6738 
6739 		/*
6740 		 * We check for an existing entry at this point.
6741 		 *
6742 		 * Since a netmask isn't passed in via the ioctl interface
6743 		 * (SIOCADDRT), we don't check for a matching netmask in that
6744 		 * case.
6745 		 */
6746 		if (!ioctl_msg)
6747 			match_flags |= MATCH_IRE_MASK;
6748 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6749 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6750 		if (ire != NULL) {
6751 			ire_refrele(ire);
6752 			if (ipif_refheld)
6753 				ipif_refrele(ipif);
6754 			return (EEXIST);
6755 		}
6756 
6757 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6758 		    ? ipif->ipif_rq : ipif->ipif_wq;
6759 
6760 		/*
6761 		 * Create a copy of the IRE_LOOPBACK,
6762 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6763 		 * the modified address and netmask.
6764 		 */
6765 		ire = ire_create(
6766 		    (uchar_t *)&dst_addr,
6767 		    (uint8_t *)&mask,
6768 		    (uint8_t *)&ipif->ipif_src_addr,
6769 		    NULL,
6770 		    &ipif->ipif_mtu,
6771 		    NULL,
6772 		    NULL,
6773 		    stq,
6774 		    ipif->ipif_net_type,
6775 		    ipif,
6776 		    0,
6777 		    0,
6778 		    0,
6779 		    flags,
6780 		    &ire_uinfo_null,
6781 		    NULL,
6782 		    NULL,
6783 		    ipst);
6784 		if (ire == NULL) {
6785 			if (ipif_refheld)
6786 				ipif_refrele(ipif);
6787 			return (ENOMEM);
6788 		}
6789 
6790 		/*
6791 		 * Some software (for example, GateD and Sun Cluster) attempts
6792 		 * to create (what amount to) IRE_PREFIX routes with the
6793 		 * loopback address as the gateway.  This is primarily done to
6794 		 * set up prefixes with the RTF_REJECT flag set (for example,
6795 		 * when generating aggregate routes.)
6796 		 *
6797 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6798 		 * IRE_LOOPBACK, then we map the request into a
6799 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6800 		 * these interface routes, by definition, can only be that.
6801 		 *
6802 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6803 		 * routine, but rather using ire_create() directly.
6804 		 *
6805 		 */
6806 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6807 			ire->ire_type = IRE_IF_NORESOLVER;
6808 			ire->ire_flags |= RTF_BLACKHOLE;
6809 		}
6810 
6811 		error = ire_add(&ire, q, mp, func, B_FALSE);
6812 		if (error == 0)
6813 			goto save_ire;
6814 
6815 		/*
6816 		 * In the result of failure, ire_add() will have already
6817 		 * deleted the ire in question, so there is no need to
6818 		 * do that here.
6819 		 */
6820 		if (ipif_refheld)
6821 			ipif_refrele(ipif);
6822 		return (error);
6823 	}
6824 	if (ipif_refheld) {
6825 		ipif_refrele(ipif);
6826 		ipif_refheld = B_FALSE;
6827 	}
6828 
6829 	/*
6830 	 * Get an interface IRE for the specified gateway.
6831 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6832 	 * gateway, it is currently unreachable and we fail the request
6833 	 * accordingly.
6834 	 */
6835 	ipif = ipif_arg;
6836 	if (ipif_arg != NULL)
6837 		match_flags |= MATCH_IRE_ILL;
6838 again:
6839 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6840 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6841 	if (gw_ire == NULL) {
6842 		/*
6843 		 * With IPMP, we allow host routes to influence in.mpathd's
6844 		 * target selection.  However, if the test addresses are on
6845 		 * their own network, the above lookup will fail since the
6846 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6847 		 * hidden test IREs to be found and try again.
6848 		 */
6849 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6850 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6851 			goto again;
6852 		}
6853 		return (ENETUNREACH);
6854 	}
6855 
6856 	/*
6857 	 * We create one of three types of IREs as a result of this request
6858 	 * based on the netmask.  A netmask of all ones (which is automatically
6859 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6860 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6861 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6862 	 * destination prefix.
6863 	 */
6864 	if (mask == IP_HOST_MASK)
6865 		type = IRE_HOST;
6866 	else if (mask == 0)
6867 		type = IRE_DEFAULT;
6868 	else
6869 		type = IRE_PREFIX;
6870 
6871 	/* check for a duplicate entry */
6872 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6873 	    NULL, ALL_ZONES, 0, NULL,
6874 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
6875 	if (ire != NULL) {
6876 		ire_refrele(gw_ire);
6877 		ire_refrele(ire);
6878 		return (EEXIST);
6879 	}
6880 
6881 	/* Security attribute exists */
6882 	if (sp != NULL) {
6883 		tsol_gcgrp_addr_t ga;
6884 
6885 		/* find or create the gateway credentials group */
6886 		ga.ga_af = AF_INET;
6887 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6888 
6889 		/* we hold reference to it upon success */
6890 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6891 		if (gcgrp == NULL) {
6892 			ire_refrele(gw_ire);
6893 			return (ENOMEM);
6894 		}
6895 
6896 		/*
6897 		 * Create and add the security attribute to the group; a
6898 		 * reference to the group is made upon allocating a new
6899 		 * entry successfully.  If it finds an already-existing
6900 		 * entry for the security attribute in the group, it simply
6901 		 * returns it and no new reference is made to the group.
6902 		 */
6903 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6904 		if (gc == NULL) {
6905 			/* release reference held by gcgrp_lookup */
6906 			GCGRP_REFRELE(gcgrp);
6907 			ire_refrele(gw_ire);
6908 			return (ENOMEM);
6909 		}
6910 	}
6911 
6912 	/* Create the IRE. */
6913 	ire = ire_create(
6914 	    (uchar_t *)&dst_addr,		/* dest address */
6915 	    (uchar_t *)&mask,			/* mask */
6916 	    /* src address assigned by the caller? */
6917 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6918 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
6919 	    (uchar_t *)&gw_addr,		/* gateway address */
6920 	    &gw_ire->ire_max_frag,
6921 	    NULL,				/* no src nce */
6922 	    NULL,				/* no recv-from queue */
6923 	    NULL,				/* no send-to queue */
6924 	    (ushort_t)type,			/* IRE type */
6925 	    ipif_arg,
6926 	    0,
6927 	    0,
6928 	    0,
6929 	    flags,
6930 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6931 	    gc,					/* security attribute */
6932 	    NULL,
6933 	    ipst);
6934 
6935 	/*
6936 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6937 	 * reference to the 'gcgrp'. We can now release the extra reference
6938 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6939 	 */
6940 	if (gcgrp_xtraref)
6941 		GCGRP_REFRELE(gcgrp);
6942 	if (ire == NULL) {
6943 		if (gc != NULL)
6944 			GC_REFRELE(gc);
6945 		ire_refrele(gw_ire);
6946 		return (ENOMEM);
6947 	}
6948 
6949 	/*
6950 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6951 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6952 	 */
6953 
6954 	/* Add the new IRE. */
6955 	error = ire_add(&ire, q, mp, func, B_FALSE);
6956 	if (error != 0) {
6957 		/*
6958 		 * In the result of failure, ire_add() will have already
6959 		 * deleted the ire in question, so there is no need to
6960 		 * do that here.
6961 		 */
6962 		ire_refrele(gw_ire);
6963 		return (error);
6964 	}
6965 
6966 	if (flags & RTF_MULTIRT) {
6967 		/*
6968 		 * Invoke the CGTP (multirouting) filtering module
6969 		 * to add the dst address in the filtering database.
6970 		 * Replicated inbound packets coming from that address
6971 		 * will be filtered to discard the duplicates.
6972 		 * It is not necessary to call the CGTP filter hook
6973 		 * when the dst address is a broadcast or multicast,
6974 		 * because an IP source address cannot be a broadcast
6975 		 * or a multicast.
6976 		 */
6977 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6978 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
6979 		if (ire_dst != NULL) {
6980 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
6981 			ire_refrele(ire_dst);
6982 			goto save_ire;
6983 		}
6984 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
6985 		    !CLASSD(ire->ire_addr)) {
6986 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
6987 			    ipst->ips_netstack->netstack_stackid,
6988 			    ire->ire_addr,
6989 			    ire->ire_gateway_addr,
6990 			    ire->ire_src_addr,
6991 			    gw_ire->ire_src_addr);
6992 			if (res != 0) {
6993 				ire_refrele(gw_ire);
6994 				ire_delete(ire);
6995 				return (res);
6996 			}
6997 		}
6998 	}
6999 
7000 	/*
7001 	 * Now that the prefix IRE entry has been created, delete any
7002 	 * existing gateway IRE cache entries as well as any IRE caches
7003 	 * using the gateway, and force them to be created through
7004 	 * ip_newroute.
7005 	 */
7006 	if (gc != NULL) {
7007 		ASSERT(gcgrp != NULL);
7008 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7009 	}
7010 
7011 save_ire:
7012 	if (gw_ire != NULL) {
7013 		ire_refrele(gw_ire);
7014 	}
7015 	if (ipif != NULL) {
7016 		/*
7017 		 * Save enough information so that we can recreate the IRE if
7018 		 * the interface goes down and then up.  The metrics associated
7019 		 * with the route will be saved as well when rts_setmetrics() is
7020 		 * called after the IRE has been created.  In the case where
7021 		 * memory cannot be allocated, none of this information will be
7022 		 * saved.
7023 		 */
7024 		ipif_save_ire(ipif, ire);
7025 	}
7026 	if (ioctl_msg)
7027 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7028 	if (ire_arg != NULL) {
7029 		/*
7030 		 * Store the ire that was successfully added into where ire_arg
7031 		 * points to so that callers don't have to look it up
7032 		 * themselves (but they are responsible for ire_refrele()ing
7033 		 * the ire when they are finished with it).
7034 		 */
7035 		*ire_arg = ire;
7036 	} else {
7037 		ire_refrele(ire);		/* Held in ire_add */
7038 	}
7039 	if (ipif_refheld)
7040 		ipif_refrele(ipif);
7041 	return (0);
7042 }
7043 
7044 /*
7045  * ip_rt_delete is called to delete an IPv4 route.
7046  * ipif_arg is passed in to associate it with the correct interface.
7047  * We may need to restart this operation if the ipif cannot be looked up
7048  * due to an exclusive operation that is currently in progress. The restart
7049  * entry point is specified by 'func'
7050  */
7051 /* ARGSUSED4 */
7052 int
7053 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7054     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7055     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7056 {
7057 	ire_t	*ire = NULL;
7058 	ipif_t	*ipif;
7059 	boolean_t ipif_refheld = B_FALSE;
7060 	uint_t	type;
7061 	uint_t	match_flags = MATCH_IRE_TYPE;
7062 	int	err = 0;
7063 
7064 	ip1dbg(("ip_rt_delete:"));
7065 	/*
7066 	 * If this is the case of RTF_HOST being set, then we set the netmask
7067 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7068 	 */
7069 	if (flags & RTF_HOST) {
7070 		mask = IP_HOST_MASK;
7071 		match_flags |= MATCH_IRE_MASK;
7072 	} else if (rtm_addrs & RTA_NETMASK) {
7073 		match_flags |= MATCH_IRE_MASK;
7074 	}
7075 
7076 	/*
7077 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7078 	 * we check if the gateway address is one of our interfaces first,
7079 	 * and fall back on RTF_GATEWAY routes.
7080 	 *
7081 	 * This makes it possible to delete an original
7082 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7083 	 *
7084 	 * As the interface index specified with the RTA_IFP sockaddr is the
7085 	 * same for all ipif's off of an ill, the matching logic below uses
7086 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7087 	 * sharing the same prefix and interface index as the the route
7088 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7089 	 * is specified in the request.
7090 	 *
7091 	 * On the other hand, since the gateway address will usually be
7092 	 * different for each ipif on the system, the matching logic
7093 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7094 	 * route.  This means that interface routes for the same prefix can be
7095 	 * uniquely identified if they belong to distinct ipif's and if a
7096 	 * RTA_IFP sockaddr is not present.
7097 	 *
7098 	 * For more detail on specifying routes by gateway address and by
7099 	 * interface index, see the comments in ip_rt_add().
7100 	 */
7101 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7102 	    ipst);
7103 	if (ipif != NULL)
7104 		ipif_refheld = B_TRUE;
7105 	else if (err == EINPROGRESS)
7106 		return (err);
7107 	else
7108 		err = 0;
7109 	if (ipif != NULL) {
7110 		if (ipif_arg != NULL) {
7111 			if (ipif_refheld) {
7112 				ipif_refrele(ipif);
7113 				ipif_refheld = B_FALSE;
7114 			}
7115 			ipif = ipif_arg;
7116 			match_flags |= MATCH_IRE_ILL;
7117 		} else {
7118 			match_flags |= MATCH_IRE_IPIF;
7119 		}
7120 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7121 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7122 			    ALL_ZONES, NULL, match_flags, ipst);
7123 		}
7124 		if (ire == NULL) {
7125 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7126 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7127 			    match_flags, ipst);
7128 		}
7129 	}
7130 
7131 	if (ire == NULL) {
7132 		/*
7133 		 * At this point, the gateway address is not one of our own
7134 		 * addresses or a matching interface route was not found.  We
7135 		 * set the IRE type to lookup based on whether
7136 		 * this is a host route, a default route or just a prefix.
7137 		 *
7138 		 * If an ipif_arg was passed in, then the lookup is based on an
7139 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7140 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7141 		 * set as the route being looked up is not a traditional
7142 		 * interface route.
7143 		 */
7144 		match_flags &= ~MATCH_IRE_IPIF;
7145 		match_flags |= MATCH_IRE_GW;
7146 		if (ipif_arg != NULL)
7147 			match_flags |= MATCH_IRE_ILL;
7148 		if (mask == IP_HOST_MASK)
7149 			type = IRE_HOST;
7150 		else if (mask == 0)
7151 			type = IRE_DEFAULT;
7152 		else
7153 			type = IRE_PREFIX;
7154 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7155 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7156 	}
7157 
7158 	if (ipif_refheld)
7159 		ipif_refrele(ipif);
7160 
7161 	/* ipif is not refheld anymore */
7162 	if (ire == NULL)
7163 		return (ESRCH);
7164 
7165 	if (ire->ire_flags & RTF_MULTIRT) {
7166 		/*
7167 		 * Invoke the CGTP (multirouting) filtering module
7168 		 * to remove the dst address from the filtering database.
7169 		 * Packets coming from that address will no longer be
7170 		 * filtered to remove duplicates.
7171 		 */
7172 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7173 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7174 			    ipst->ips_netstack->netstack_stackid,
7175 			    ire->ire_addr, ire->ire_gateway_addr);
7176 		}
7177 		ip_cgtp_bcast_delete(ire, ipst);
7178 	}
7179 
7180 	ipif = ire->ire_ipif;
7181 	if (ipif != NULL)
7182 		ipif_remove_ire(ipif, ire);
7183 	if (ioctl_msg)
7184 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7185 	ire_delete(ire);
7186 	ire_refrele(ire);
7187 	return (err);
7188 }
7189 
7190 /*
7191  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7192  */
7193 /* ARGSUSED */
7194 int
7195 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7196     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7197 {
7198 	ipaddr_t dst_addr;
7199 	ipaddr_t gw_addr;
7200 	ipaddr_t mask;
7201 	int error = 0;
7202 	mblk_t *mp1;
7203 	struct rtentry *rt;
7204 	ipif_t *ipif = NULL;
7205 	ip_stack_t	*ipst;
7206 
7207 	ASSERT(q->q_next == NULL);
7208 	ipst = CONNQ_TO_IPST(q);
7209 
7210 	ip1dbg(("ip_siocaddrt:"));
7211 	/* Existence of mp1 verified in ip_wput_nondata */
7212 	mp1 = mp->b_cont->b_cont;
7213 	rt = (struct rtentry *)mp1->b_rptr;
7214 
7215 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7216 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7217 
7218 	/*
7219 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7220 	 * to a particular host address.  In this case, we set the netmask to
7221 	 * all ones for the particular destination address.  Otherwise,
7222 	 * determine the netmask to be used based on dst_addr and the interfaces
7223 	 * in use.
7224 	 */
7225 	if (rt->rt_flags & RTF_HOST) {
7226 		mask = IP_HOST_MASK;
7227 	} else {
7228 		/*
7229 		 * Note that ip_subnet_mask returns a zero mask in the case of
7230 		 * default (an all-zeroes address).
7231 		 */
7232 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7233 	}
7234 
7235 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7236 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7237 	if (ipif != NULL)
7238 		ipif_refrele(ipif);
7239 	return (error);
7240 }
7241 
7242 /*
7243  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7244  */
7245 /* ARGSUSED */
7246 int
7247 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7248     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7249 {
7250 	ipaddr_t dst_addr;
7251 	ipaddr_t gw_addr;
7252 	ipaddr_t mask;
7253 	int error;
7254 	mblk_t *mp1;
7255 	struct rtentry *rt;
7256 	ipif_t *ipif = NULL;
7257 	ip_stack_t	*ipst;
7258 
7259 	ASSERT(q->q_next == NULL);
7260 	ipst = CONNQ_TO_IPST(q);
7261 
7262 	ip1dbg(("ip_siocdelrt:"));
7263 	/* Existence of mp1 verified in ip_wput_nondata */
7264 	mp1 = mp->b_cont->b_cont;
7265 	rt = (struct rtentry *)mp1->b_rptr;
7266 
7267 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7268 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7269 
7270 	/*
7271 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7272 	 * to a particular host address.  In this case, we set the netmask to
7273 	 * all ones for the particular destination address.  Otherwise,
7274 	 * determine the netmask to be used based on dst_addr and the interfaces
7275 	 * in use.
7276 	 */
7277 	if (rt->rt_flags & RTF_HOST) {
7278 		mask = IP_HOST_MASK;
7279 	} else {
7280 		/*
7281 		 * Note that ip_subnet_mask returns a zero mask in the case of
7282 		 * default (an all-zeroes address).
7283 		 */
7284 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7285 	}
7286 
7287 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7288 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7289 	    mp, ip_process_ioctl, ipst);
7290 	if (ipif != NULL)
7291 		ipif_refrele(ipif);
7292 	return (error);
7293 }
7294 
7295 /*
7296  * Enqueue the mp onto the ipsq, chained by b_next.
7297  * b_prev stores the function to be executed later, and b_queue the queue
7298  * where this mp originated.
7299  */
7300 void
7301 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7302     ill_t *pending_ill)
7303 {
7304 	conn_t	*connp;
7305 	ipxop_t *ipx = ipsq->ipsq_xop;
7306 
7307 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7308 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7309 	ASSERT(func != NULL);
7310 
7311 	mp->b_queue = q;
7312 	mp->b_prev = (void *)func;
7313 	mp->b_next = NULL;
7314 
7315 	switch (type) {
7316 	case CUR_OP:
7317 		if (ipx->ipx_mptail != NULL) {
7318 			ASSERT(ipx->ipx_mphead != NULL);
7319 			ipx->ipx_mptail->b_next = mp;
7320 		} else {
7321 			ASSERT(ipx->ipx_mphead == NULL);
7322 			ipx->ipx_mphead = mp;
7323 		}
7324 		ipx->ipx_mptail = mp;
7325 		break;
7326 
7327 	case NEW_OP:
7328 		if (ipsq->ipsq_xopq_mptail != NULL) {
7329 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7330 			ipsq->ipsq_xopq_mptail->b_next = mp;
7331 		} else {
7332 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7333 			ipsq->ipsq_xopq_mphead = mp;
7334 		}
7335 		ipsq->ipsq_xopq_mptail = mp;
7336 		ipx->ipx_ipsq_queued = B_TRUE;
7337 		break;
7338 
7339 	case SWITCH_OP:
7340 		ASSERT(ipsq->ipsq_swxop != NULL);
7341 		/* only one switch operation is currently allowed */
7342 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7343 		ipsq->ipsq_switch_mp = mp;
7344 		ipx->ipx_ipsq_queued = B_TRUE;
7345 		break;
7346 	default:
7347 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7348 	}
7349 
7350 	if (CONN_Q(q) && pending_ill != NULL) {
7351 		connp = Q_TO_CONN(q);
7352 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7353 		connp->conn_oper_pending_ill = pending_ill;
7354 	}
7355 }
7356 
7357 /*
7358  * Dequeue the next message that requested exclusive access to this IPSQ's
7359  * xop.  Specifically:
7360  *
7361  *  1. If we're still processing the current operation on `ipsq', then
7362  *     dequeue the next message for the operation (from ipx_mphead), or
7363  *     return NULL if there are no queued messages for the operation.
7364  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7365  *
7366  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7367  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7368  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7369  *     leaving IPMP groups and require a careful dance -- see the comments
7370  *     in-line below for details.  If we're leaving a group xop or if we're
7371  *     joining a group xop and become writer on it, then we proceed to (3).
7372  *     Otherwise, we return NULL and exit the xop.
7373  *
7374  *  3. For each IPSQ in the xop, return any switch operation stored on
7375  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7376  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7377  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7378  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7379  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7380  *     each phyint in the group, including the IPMP meta-interface phyint.
7381  */
7382 static mblk_t *
7383 ipsq_dq(ipsq_t *ipsq)
7384 {
7385 	ill_t	*illv4, *illv6;
7386 	mblk_t	*mp;
7387 	ipsq_t	*xopipsq;
7388 	ipsq_t	*leftipsq = NULL;
7389 	ipxop_t *ipx;
7390 	phyint_t *phyi = ipsq->ipsq_phyint;
7391 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7392 	boolean_t emptied = B_FALSE;
7393 
7394 	/*
7395 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7396 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7397 	 */
7398 	rw_enter(&ipst->ips_ill_g_lock,
7399 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7400 	mutex_enter(&ipsq->ipsq_lock);
7401 	ipx = ipsq->ipsq_xop;
7402 	mutex_enter(&ipx->ipx_lock);
7403 
7404 	/*
7405 	 * Dequeue the next message associated with the current exclusive
7406 	 * operation, if any.
7407 	 */
7408 	if ((mp = ipx->ipx_mphead) != NULL) {
7409 		ipx->ipx_mphead = mp->b_next;
7410 		if (ipx->ipx_mphead == NULL)
7411 			ipx->ipx_mptail = NULL;
7412 		mp->b_next = (void *)ipsq;
7413 		goto out;
7414 	}
7415 
7416 	if (ipx->ipx_current_ipif != NULL)
7417 		goto empty;
7418 
7419 	if (ipsq->ipsq_swxop != NULL) {
7420 		/*
7421 		 * The exclusive operation that is now being completed has
7422 		 * requested a switch to a different xop.  This happens
7423 		 * when an interface joins or leaves an IPMP group.  Joins
7424 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7425 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7426 		 * (phyint_free()), or interface plumb for an ill type
7427 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7428 		 *
7429 		 * Xop switches are not allowed on the IPMP meta-interface.
7430 		 */
7431 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7432 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7433 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7434 
7435 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7436 			/*
7437 			 * We're switching back to our own xop, so we have two
7438 			 * xop's to drain/exit: our own, and the group xop
7439 			 * that we are leaving.
7440 			 *
7441 			 * First, pull ourselves out of the group ipsq list.
7442 			 * This is safe since we're writer on ill_g_lock.
7443 			 */
7444 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7445 
7446 			xopipsq = ipx->ipx_ipsq;
7447 			while (xopipsq->ipsq_next != ipsq)
7448 				xopipsq = xopipsq->ipsq_next;
7449 
7450 			xopipsq->ipsq_next = ipsq->ipsq_next;
7451 			ipsq->ipsq_next = ipsq;
7452 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7453 			ipsq->ipsq_swxop = NULL;
7454 
7455 			/*
7456 			 * Second, prepare to exit the group xop.  The actual
7457 			 * ipsq_exit() is done at the end of this function
7458 			 * since we cannot hold any locks across ipsq_exit().
7459 			 * Note that although we drop the group's ipx_lock, no
7460 			 * threads can proceed since we're still ipx_writer.
7461 			 */
7462 			leftipsq = xopipsq;
7463 			mutex_exit(&ipx->ipx_lock);
7464 
7465 			/*
7466 			 * Third, set ipx to point to our own xop (which was
7467 			 * inactive and therefore can be entered).
7468 			 */
7469 			ipx = ipsq->ipsq_xop;
7470 			mutex_enter(&ipx->ipx_lock);
7471 			ASSERT(ipx->ipx_writer == NULL);
7472 			ASSERT(ipx->ipx_current_ipif == NULL);
7473 		} else {
7474 			/*
7475 			 * We're switching from our own xop to a group xop.
7476 			 * The requestor of the switch must ensure that the
7477 			 * group xop cannot go away (e.g. by ensuring the
7478 			 * phyint associated with the xop cannot go away).
7479 			 *
7480 			 * If we can become writer on our new xop, then we'll
7481 			 * do the drain.  Otherwise, the current writer of our
7482 			 * new xop will do the drain when it exits.
7483 			 *
7484 			 * First, splice ourselves into the group IPSQ list.
7485 			 * This is safe since we're writer on ill_g_lock.
7486 			 */
7487 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7488 
7489 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7490 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7491 				xopipsq = xopipsq->ipsq_next;
7492 
7493 			xopipsq->ipsq_next = ipsq;
7494 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7495 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7496 			ipsq->ipsq_swxop = NULL;
7497 
7498 			/*
7499 			 * Second, exit our own xop, since it's now unused.
7500 			 * This is safe since we've got the only reference.
7501 			 */
7502 			ASSERT(ipx->ipx_writer == curthread);
7503 			ipx->ipx_writer = NULL;
7504 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7505 			ipx->ipx_ipsq_queued = B_FALSE;
7506 			mutex_exit(&ipx->ipx_lock);
7507 
7508 			/*
7509 			 * Third, set ipx to point to our new xop, and check
7510 			 * if we can become writer on it.  If we cannot, then
7511 			 * the current writer will drain the IPSQ group when
7512 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7513 			 * because we're still holding ipsq_lock.
7514 			 */
7515 			ipx = ipsq->ipsq_xop;
7516 			mutex_enter(&ipx->ipx_lock);
7517 			if (ipx->ipx_writer != NULL ||
7518 			    ipx->ipx_current_ipif != NULL) {
7519 				goto out;
7520 			}
7521 		}
7522 
7523 		/*
7524 		 * Fourth, become writer on our new ipx before we continue
7525 		 * with the drain.  Note that we never dropped ipsq_lock
7526 		 * above, so no other thread could've raced with us to
7527 		 * become writer first.  Also, we're holding ipx_lock, so
7528 		 * no other thread can examine the ipx right now.
7529 		 */
7530 		ASSERT(ipx->ipx_current_ipif == NULL);
7531 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7532 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7533 		ipx->ipx_writer = curthread;
7534 		ipx->ipx_forced = B_FALSE;
7535 #ifdef DEBUG
7536 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7537 #endif
7538 	}
7539 
7540 	xopipsq = ipsq;
7541 	do {
7542 		/*
7543 		 * So that other operations operate on a consistent and
7544 		 * complete phyint, a switch message on an IPSQ must be
7545 		 * handled prior to any other operations on that IPSQ.
7546 		 */
7547 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7548 			xopipsq->ipsq_switch_mp = NULL;
7549 			ASSERT(mp->b_next == NULL);
7550 			mp->b_next = (void *)xopipsq;
7551 			goto out;
7552 		}
7553 
7554 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7555 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7556 			if (xopipsq->ipsq_xopq_mphead == NULL)
7557 				xopipsq->ipsq_xopq_mptail = NULL;
7558 			mp->b_next = (void *)xopipsq;
7559 			goto out;
7560 		}
7561 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7562 empty:
7563 	/*
7564 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7565 	 * new messages can end up on any IPSQ in the xop.
7566 	 */
7567 	ipx->ipx_writer = NULL;
7568 	ipx->ipx_forced = B_FALSE;
7569 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7570 	ipx->ipx_ipsq_queued = B_FALSE;
7571 	emptied = B_TRUE;
7572 #ifdef	DEBUG
7573 	ipx->ipx_depth = 0;
7574 #endif
7575 out:
7576 	mutex_exit(&ipx->ipx_lock);
7577 	mutex_exit(&ipsq->ipsq_lock);
7578 
7579 	/*
7580 	 * If we completely emptied the xop, then wake up any threads waiting
7581 	 * to enter any of the IPSQ's associated with it.
7582 	 */
7583 	if (emptied) {
7584 		xopipsq = ipsq;
7585 		do {
7586 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7587 				continue;
7588 
7589 			illv4 = phyi->phyint_illv4;
7590 			illv6 = phyi->phyint_illv6;
7591 
7592 			GRAB_ILL_LOCKS(illv4, illv6);
7593 			if (illv4 != NULL)
7594 				cv_broadcast(&illv4->ill_cv);
7595 			if (illv6 != NULL)
7596 				cv_broadcast(&illv6->ill_cv);
7597 			RELEASE_ILL_LOCKS(illv4, illv6);
7598 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7599 	}
7600 	rw_exit(&ipst->ips_ill_g_lock);
7601 
7602 	/*
7603 	 * Now that all locks are dropped, exit the IPSQ we left.
7604 	 */
7605 	if (leftipsq != NULL)
7606 		ipsq_exit(leftipsq);
7607 
7608 	return (mp);
7609 }
7610 
7611 /*
7612  * Return completion status of previously initiated DLPI operations on
7613  * ills in the purview of an ipsq.
7614  */
7615 static boolean_t
7616 ipsq_dlpi_done(ipsq_t *ipsq)
7617 {
7618 	ipsq_t		*ipsq_start;
7619 	phyint_t	*phyi;
7620 	ill_t		*ill;
7621 
7622 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
7623 	ipsq_start = ipsq;
7624 
7625 	do {
7626 		/*
7627 		 * The only current users of this function are ipsq_try_enter
7628 		 * and ipsq_enter which have made sure that ipsq_writer is
7629 		 * NULL before we reach here. ill_dlpi_pending is modified
7630 		 * only by an ipsq writer
7631 		 */
7632 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
7633 		phyi = ipsq->ipsq_phyint;
7634 		/*
7635 		 * phyi could be NULL if a phyint that is part of an
7636 		 * IPMP group is being unplumbed. A more detailed
7637 		 * comment is in ipmp_grp_update_kstats()
7638 		 */
7639 		if (phyi != NULL) {
7640 			ill = phyi->phyint_illv4;
7641 			if (ill != NULL &&
7642 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
7643 				return (B_FALSE);
7644 
7645 			ill = phyi->phyint_illv6;
7646 			if (ill != NULL &&
7647 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
7648 				return (B_FALSE);
7649 		}
7650 
7651 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
7652 
7653 	return (B_TRUE);
7654 }
7655 
7656 /*
7657  * Enter the ipsq corresponding to ill, by waiting synchronously till
7658  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7659  * will have to drain completely before ipsq_enter returns success.
7660  * ipx_current_ipif will be set if some exclusive op is in progress,
7661  * and the ipsq_exit logic will start the next enqueued op after
7662  * completion of the current op. If 'force' is used, we don't wait
7663  * for the enqueued ops. This is needed when a conn_close wants to
7664  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7665  * of an ill can also use this option. But we dont' use it currently.
7666  */
7667 #define	ENTER_SQ_WAIT_TICKS 100
7668 boolean_t
7669 ipsq_enter(ill_t *ill, boolean_t force, int type)
7670 {
7671 	ipsq_t	*ipsq;
7672 	ipxop_t *ipx;
7673 	boolean_t waited_enough = B_FALSE;
7674 	ip_stack_t *ipst = ill->ill_ipst;
7675 
7676 	/*
7677 	 * Note that the relationship between ill and ipsq is fixed as long as
7678 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7679 	 * relationship between the IPSQ and xop cannot change.  However,
7680 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7681 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7682 	 * waking up all ills in the xop when it becomes available.
7683 	 */
7684 	for (;;) {
7685 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7686 		mutex_enter(&ill->ill_lock);
7687 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7688 			mutex_exit(&ill->ill_lock);
7689 			rw_exit(&ipst->ips_ill_g_lock);
7690 			return (B_FALSE);
7691 		}
7692 
7693 		ipsq = ill->ill_phyint->phyint_ipsq;
7694 		mutex_enter(&ipsq->ipsq_lock);
7695 		ipx = ipsq->ipsq_xop;
7696 		mutex_enter(&ipx->ipx_lock);
7697 
7698 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7699 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
7700 		    waited_enough))
7701 			break;
7702 
7703 		rw_exit(&ipst->ips_ill_g_lock);
7704 
7705 		if (!force || ipx->ipx_writer != NULL) {
7706 			mutex_exit(&ipx->ipx_lock);
7707 			mutex_exit(&ipsq->ipsq_lock);
7708 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7709 		} else {
7710 			mutex_exit(&ipx->ipx_lock);
7711 			mutex_exit(&ipsq->ipsq_lock);
7712 			(void) cv_timedwait(&ill->ill_cv,
7713 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7714 			waited_enough = B_TRUE;
7715 		}
7716 		mutex_exit(&ill->ill_lock);
7717 	}
7718 
7719 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7720 	ASSERT(ipx->ipx_reentry_cnt == 0);
7721 	ipx->ipx_writer = curthread;
7722 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7723 	ipx->ipx_reentry_cnt++;
7724 #ifdef DEBUG
7725 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7726 #endif
7727 	mutex_exit(&ipx->ipx_lock);
7728 	mutex_exit(&ipsq->ipsq_lock);
7729 	mutex_exit(&ill->ill_lock);
7730 	rw_exit(&ipst->ips_ill_g_lock);
7731 
7732 	return (B_TRUE);
7733 }
7734 
7735 /*
7736  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
7737  * across the call to the core interface ipsq_try_enter() and hence calls this
7738  * function directly. This is explained more fully in ipif_set_values().
7739  * In order to support the above constraint, ipsq_try_enter is implemented as
7740  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
7741  */
7742 static ipsq_t *
7743 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
7744     int type, boolean_t reentry_ok)
7745 {
7746 	ipsq_t	*ipsq;
7747 	ipxop_t	*ipx;
7748 	ip_stack_t *ipst = ill->ill_ipst;
7749 
7750 	/*
7751 	 * lock ordering:
7752 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7753 	 *
7754 	 * ipx of an ipsq can't change when ipsq_lock is held.
7755 	 */
7756 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
7757 	GRAB_CONN_LOCK(q);
7758 	mutex_enter(&ill->ill_lock);
7759 	ipsq = ill->ill_phyint->phyint_ipsq;
7760 	mutex_enter(&ipsq->ipsq_lock);
7761 	ipx = ipsq->ipsq_xop;
7762 	mutex_enter(&ipx->ipx_lock);
7763 
7764 	/*
7765 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7766 	 *    (Note: If the caller does not specify reentry_ok then neither
7767 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7768 	 *    again. Otherwise it can lead to an infinite loop
7769 	 * 2. Enter the ipsq if there is no current writer and this attempted
7770 	 *    entry is part of the current operation
7771 	 * 3. Enter the ipsq if there is no current writer and this is a new
7772 	 *    operation and the operation queue is empty and there is no
7773 	 *    operation currently in progress and if all previously initiated
7774 	 *    DLPI operations have completed.
7775 	 */
7776 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7777 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7778 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
7779 	    ipsq_dlpi_done(ipsq))))) {
7780 		/* Success. */
7781 		ipx->ipx_reentry_cnt++;
7782 		ipx->ipx_writer = curthread;
7783 		ipx->ipx_forced = B_FALSE;
7784 		mutex_exit(&ipx->ipx_lock);
7785 		mutex_exit(&ipsq->ipsq_lock);
7786 		mutex_exit(&ill->ill_lock);
7787 		RELEASE_CONN_LOCK(q);
7788 #ifdef DEBUG
7789 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7790 #endif
7791 		return (ipsq);
7792 	}
7793 
7794 	if (func != NULL)
7795 		ipsq_enq(ipsq, q, mp, func, type, ill);
7796 
7797 	mutex_exit(&ipx->ipx_lock);
7798 	mutex_exit(&ipsq->ipsq_lock);
7799 	mutex_exit(&ill->ill_lock);
7800 	RELEASE_CONN_LOCK(q);
7801 	return (NULL);
7802 }
7803 
7804 /*
7805  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7806  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7807  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7808  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7809  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7810  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7811  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7812  * up the interface) and are enqueued in ipx_mphead.
7813  *
7814  * If a thread does not want to reenter the ipsq when it is already writer,
7815  * it must make sure that the specified reentry point to be called later
7816  * when the ipsq is empty, nor any code path starting from the specified reentry
7817  * point must never ever try to enter the ipsq again. Otherwise it can lead
7818  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7819  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7820  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7821  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7822  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7823  * ioctl if the current ioctl has completed. If the current ioctl is still
7824  * in progress it simply returns. The current ioctl could be waiting for
7825  * a response from another module (arp or the driver or could be waiting for
7826  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7827  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7828  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7829  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7830  * all associated DLPI operations have completed.
7831  */
7832 
7833 /*
7834  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7835  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7836  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7837  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7838  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7839  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7840  */
7841 ipsq_t *
7842 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7843     ipsq_func_t func, int type, boolean_t reentry_ok)
7844 {
7845 	ip_stack_t	*ipst;
7846 	ipsq_t		*ipsq;
7847 
7848 	/* Only 1 of ipif or ill can be specified */
7849 	ASSERT((ipif != NULL) ^ (ill != NULL));
7850 
7851 	if (ipif != NULL)
7852 		ill = ipif->ipif_ill;
7853 	ipst = ill->ill_ipst;
7854 
7855 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7856 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
7857 	rw_exit(&ipst->ips_ill_g_lock);
7858 
7859 	return (ipsq);
7860 }
7861 
7862 /*
7863  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7864  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7865  * cannot be entered, the mp is queued for completion.
7866  */
7867 void
7868 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7869     boolean_t reentry_ok)
7870 {
7871 	ipsq_t	*ipsq;
7872 
7873 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7874 
7875 	/*
7876 	 * Drop the caller's refhold on the ill.  This is safe since we either
7877 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7878 	 * IPSQ, in which case we return without accessing ill anymore.  This
7879 	 * is needed because func needs to see the correct refcount.
7880 	 * e.g. removeif can work only then.
7881 	 */
7882 	ill_refrele(ill);
7883 	if (ipsq != NULL) {
7884 		(*func)(ipsq, q, mp, NULL);
7885 		ipsq_exit(ipsq);
7886 	}
7887 }
7888 
7889 /*
7890  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7891  * prior to exiting.  Caller must be writer on the specified IPSQ.
7892  */
7893 void
7894 ipsq_exit(ipsq_t *ipsq)
7895 {
7896 	mblk_t *mp;
7897 	ipsq_t *mp_ipsq;
7898 	queue_t	*q;
7899 	phyint_t *phyi;
7900 	ipsq_func_t func;
7901 
7902 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7903 
7904 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7905 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7906 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7907 		return;
7908 	}
7909 
7910 	for (;;) {
7911 		phyi = ipsq->ipsq_phyint;
7912 		mp = ipsq_dq(ipsq);
7913 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7914 
7915 		/*
7916 		 * If we've changed to a new IPSQ, and the phyint associated
7917 		 * with the old one has gone away, free the old IPSQ.  Note
7918 		 * that this cannot happen while the IPSQ is in a group.
7919 		 */
7920 		if (mp_ipsq != ipsq && phyi == NULL) {
7921 			ASSERT(ipsq->ipsq_next == ipsq);
7922 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7923 			ipsq_delete(ipsq);
7924 		}
7925 
7926 		if (mp == NULL)
7927 			break;
7928 
7929 		q = mp->b_queue;
7930 		func = (ipsq_func_t)mp->b_prev;
7931 		ipsq = mp_ipsq;
7932 		mp->b_next = mp->b_prev = NULL;
7933 		mp->b_queue = NULL;
7934 
7935 		/*
7936 		 * If 'q' is an conn queue, it is valid, since we did a
7937 		 * a refhold on the conn at the start of the ioctl.
7938 		 * If 'q' is an ill queue, it is valid, since close of an
7939 		 * ill will clean up its IPSQ.
7940 		 */
7941 		(*func)(ipsq, q, mp, NULL);
7942 	}
7943 }
7944 
7945 /*
7946  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7947  * and `ioccmd'.
7948  */
7949 void
7950 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7951 {
7952 	ill_t *ill = ipif->ipif_ill;
7953 	ipxop_t *ipx = ipsq->ipsq_xop;
7954 
7955 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7956 	ASSERT(ipx->ipx_current_ipif == NULL);
7957 	ASSERT(ipx->ipx_current_ioctl == 0);
7958 
7959 	ipx->ipx_current_done = B_FALSE;
7960 	ipx->ipx_current_ioctl = ioccmd;
7961 	mutex_enter(&ipx->ipx_lock);
7962 	ipx->ipx_current_ipif = ipif;
7963 	mutex_exit(&ipx->ipx_lock);
7964 
7965 	/*
7966 	 * Set IPIF_CHANGING on one or more ipifs associated with the
7967 	 * current exclusive operation.  IPIF_CHANGING prevents any new
7968 	 * references to the ipif (so that the references will eventually
7969 	 * drop to zero) and also prevents any "get" operations (e.g.,
7970 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
7971 	 * operation has completed and the ipif is again in a stable state.
7972 	 *
7973 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
7974 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
7975 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
7976 	 * ipifs will be affected.
7977 	 *
7978 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
7979 	 * IPIF_CONDEMNED internally after identifying the right ipif to
7980 	 * operate on.
7981 	 */
7982 	switch (ioccmd) {
7983 	case SIOCLIFREMOVEIF:
7984 		break;
7985 	case 0:
7986 		mutex_enter(&ill->ill_lock);
7987 		ipif = ipif->ipif_ill->ill_ipif;
7988 		for (; ipif != NULL; ipif = ipif->ipif_next)
7989 			ipif->ipif_state_flags |= IPIF_CHANGING;
7990 		mutex_exit(&ill->ill_lock);
7991 		break;
7992 	default:
7993 		mutex_enter(&ill->ill_lock);
7994 		ipif->ipif_state_flags |= IPIF_CHANGING;
7995 		mutex_exit(&ill->ill_lock);
7996 	}
7997 }
7998 
7999 /*
8000  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8001  * the next exclusive operation to begin once we ipsq_exit().  However, if
8002  * pending DLPI operations remain, then we will wait for the queue to drain
8003  * before allowing the next exclusive operation to begin.  This ensures that
8004  * DLPI operations from one exclusive operation are never improperly processed
8005  * as part of a subsequent exclusive operation.
8006  */
8007 void
8008 ipsq_current_finish(ipsq_t *ipsq)
8009 {
8010 	ipxop_t	*ipx = ipsq->ipsq_xop;
8011 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8012 	ipif_t	*ipif = ipx->ipx_current_ipif;
8013 
8014 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8015 
8016 	/*
8017 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
8018 	 * (but in that case, IPIF_CHANGING will already be clear and no
8019 	 * pending DLPI messages can remain).
8020 	 */
8021 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
8022 		ill_t *ill = ipif->ipif_ill;
8023 
8024 		mutex_enter(&ill->ill_lock);
8025 		dlpi_pending = ill->ill_dlpi_pending;
8026 		if (ipx->ipx_current_ioctl == 0) {
8027 			ipif = ill->ill_ipif;
8028 			for (; ipif != NULL; ipif = ipif->ipif_next)
8029 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
8030 		} else {
8031 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
8032 		}
8033 		mutex_exit(&ill->ill_lock);
8034 	}
8035 
8036 	ASSERT(!ipx->ipx_current_done);
8037 	ipx->ipx_current_done = B_TRUE;
8038 	ipx->ipx_current_ioctl = 0;
8039 	if (dlpi_pending == DL_PRIM_INVAL) {
8040 		mutex_enter(&ipx->ipx_lock);
8041 		ipx->ipx_current_ipif = NULL;
8042 		mutex_exit(&ipx->ipx_lock);
8043 	}
8044 }
8045 
8046 /*
8047  * The ill is closing. Flush all messages on the ipsq that originated
8048  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8049  * for this ill since ipsq_enter could not have entered until then.
8050  * New messages can't be queued since the CONDEMNED flag is set.
8051  */
8052 static void
8053 ipsq_flush(ill_t *ill)
8054 {
8055 	queue_t	*q;
8056 	mblk_t	*prev;
8057 	mblk_t	*mp;
8058 	mblk_t	*mp_next;
8059 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
8060 
8061 	ASSERT(IAM_WRITER_ILL(ill));
8062 
8063 	/*
8064 	 * Flush any messages sent up by the driver.
8065 	 */
8066 	mutex_enter(&ipx->ipx_lock);
8067 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
8068 		mp_next = mp->b_next;
8069 		q = mp->b_queue;
8070 		if (q == ill->ill_rq || q == ill->ill_wq) {
8071 			/* dequeue mp */
8072 			if (prev == NULL)
8073 				ipx->ipx_mphead = mp->b_next;
8074 			else
8075 				prev->b_next = mp->b_next;
8076 			if (ipx->ipx_mptail == mp) {
8077 				ASSERT(mp_next == NULL);
8078 				ipx->ipx_mptail = prev;
8079 			}
8080 			inet_freemsg(mp);
8081 		} else {
8082 			prev = mp;
8083 		}
8084 	}
8085 	mutex_exit(&ipx->ipx_lock);
8086 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8087 	ipsq_xopq_mp_cleanup(ill, NULL);
8088 	ill_pending_mp_cleanup(ill);
8089 }
8090 
8091 /*
8092  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8093  * refhold and return the associated ipif
8094  */
8095 /* ARGSUSED */
8096 int
8097 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8098     cmd_info_t *ci, ipsq_func_t func)
8099 {
8100 	boolean_t exists;
8101 	struct iftun_req *ta;
8102 	ipif_t  *ipif;
8103 	ill_t   *ill;
8104 	boolean_t isv6;
8105 	mblk_t  *mp1;
8106 	int error;
8107 	conn_t  *connp;
8108 	ip_stack_t  *ipst;
8109 
8110 	/* Existence verified in ip_wput_nondata */
8111 	mp1 = mp->b_cont->b_cont;
8112 	ta = (struct iftun_req *)mp1->b_rptr;
8113 	/*
8114 	 * Null terminate the string to protect against buffer
8115 	 * overrun. String was generated by user code and may not
8116 	 * be trusted.
8117 	 */
8118 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8119 
8120 	connp = Q_TO_CONN(q);
8121 	isv6 = connp->conn_af_isv6;
8122 	ipst = connp->conn_netstack->netstack_ip;
8123 
8124 	/* Disallows implicit create */
8125 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8126 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8127 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8128 	if (ipif == NULL)
8129 		return (error);
8130 
8131 	if (ipif->ipif_id != 0) {
8132 		/*
8133 		 * We really don't want to set/get tunnel parameters
8134 		 * on virtual tunnel interfaces.  Only allow the
8135 		 * base tunnel to do these.
8136 		 */
8137 		ipif_refrele(ipif);
8138 		return (EINVAL);
8139 	}
8140 
8141 	/*
8142 	 * Send down to tunnel mod for ioctl processing.
8143 	 * Will finish ioctl in ip_rput_other().
8144 	 */
8145 	ill = ipif->ipif_ill;
8146 	if (ill->ill_net_type == IRE_LOOPBACK) {
8147 		ipif_refrele(ipif);
8148 		return (EOPNOTSUPP);
8149 	}
8150 
8151 	if (ill->ill_wq == NULL) {
8152 		ipif_refrele(ipif);
8153 		return (ENXIO);
8154 	}
8155 	/*
8156 	 * Mark the ioctl as coming from an IPv6 interface for
8157 	 * tun's convenience.
8158 	 */
8159 	if (ill->ill_isv6)
8160 		ta->ifta_flags |= 0x80000000;
8161 	ci->ci_ipif = ipif;
8162 	return (0);
8163 }
8164 
8165 /*
8166  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8167  * and return the associated ipif.
8168  * Return value:
8169  *	Non zero: An error has occurred. ci may not be filled out.
8170  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8171  *	a held ipif in ci.ci_ipif.
8172  */
8173 int
8174 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8175     cmd_info_t *ci, ipsq_func_t func)
8176 {
8177 	char		*name;
8178 	struct ifreq    *ifr;
8179 	struct lifreq    *lifr;
8180 	ipif_t		*ipif = NULL;
8181 	ill_t		*ill;
8182 	conn_t		*connp;
8183 	boolean_t	isv6;
8184 	boolean_t	exists;
8185 	int		err;
8186 	mblk_t		*mp1;
8187 	zoneid_t	zoneid;
8188 	ip_stack_t	*ipst;
8189 
8190 	if (q->q_next != NULL) {
8191 		ill = (ill_t *)q->q_ptr;
8192 		isv6 = ill->ill_isv6;
8193 		connp = NULL;
8194 		zoneid = ALL_ZONES;
8195 		ipst = ill->ill_ipst;
8196 	} else {
8197 		ill = NULL;
8198 		connp = Q_TO_CONN(q);
8199 		isv6 = connp->conn_af_isv6;
8200 		zoneid = connp->conn_zoneid;
8201 		if (zoneid == GLOBAL_ZONEID) {
8202 			/* global zone can access ipifs in all zones */
8203 			zoneid = ALL_ZONES;
8204 		}
8205 		ipst = connp->conn_netstack->netstack_ip;
8206 	}
8207 
8208 	/* Has been checked in ip_wput_nondata */
8209 	mp1 = mp->b_cont->b_cont;
8210 
8211 	if (ipip->ipi_cmd_type == IF_CMD) {
8212 		/* This a old style SIOC[GS]IF* command */
8213 		ifr = (struct ifreq *)mp1->b_rptr;
8214 		/*
8215 		 * Null terminate the string to protect against buffer
8216 		 * overrun. String was generated by user code and may not
8217 		 * be trusted.
8218 		 */
8219 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8220 		name = ifr->ifr_name;
8221 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8222 		ci->ci_sin6 = NULL;
8223 		ci->ci_lifr = (struct lifreq *)ifr;
8224 	} else {
8225 		/* This a new style SIOC[GS]LIF* command */
8226 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8227 		lifr = (struct lifreq *)mp1->b_rptr;
8228 		/*
8229 		 * Null terminate the string to protect against buffer
8230 		 * overrun. String was generated by user code and may not
8231 		 * be trusted.
8232 		 */
8233 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8234 		name = lifr->lifr_name;
8235 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8236 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8237 		ci->ci_lifr = lifr;
8238 	}
8239 
8240 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8241 		/*
8242 		 * The ioctl will be failed if the ioctl comes down
8243 		 * an conn stream
8244 		 */
8245 		if (ill == NULL) {
8246 			/*
8247 			 * Not an ill queue, return EINVAL same as the
8248 			 * old error code.
8249 			 */
8250 			return (ENXIO);
8251 		}
8252 		ipif = ill->ill_ipif;
8253 		ipif_refhold(ipif);
8254 	} else {
8255 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8256 		    &exists, isv6, zoneid,
8257 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8258 		    ipst);
8259 		if (ipif == NULL) {
8260 			if (err == EINPROGRESS)
8261 				return (err);
8262 			err = 0;	/* Ensure we don't use it below */
8263 		}
8264 	}
8265 
8266 	/*
8267 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8268 	 */
8269 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8270 		ipif_refrele(ipif);
8271 		return (ENXIO);
8272 	}
8273 
8274 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8275 	    name[0] == '\0') {
8276 		/*
8277 		 * Handle a or a SIOC?IF* with a null name
8278 		 * during plumb (on the ill queue before the I_PLINK).
8279 		 */
8280 		ipif = ill->ill_ipif;
8281 		ipif_refhold(ipif);
8282 	}
8283 
8284 	if (ipif == NULL)
8285 		return (ENXIO);
8286 
8287 	ci->ci_ipif = ipif;
8288 	return (0);
8289 }
8290 
8291 /*
8292  * Return the total number of ipifs.
8293  */
8294 static uint_t
8295 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8296 {
8297 	uint_t numifs = 0;
8298 	ill_t	*ill;
8299 	ill_walk_context_t	ctx;
8300 	ipif_t	*ipif;
8301 
8302 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8303 	ill = ILL_START_WALK_V4(&ctx, ipst);
8304 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8305 		if (IS_UNDER_IPMP(ill))
8306 			continue;
8307 		for (ipif = ill->ill_ipif; ipif != NULL;
8308 		    ipif = ipif->ipif_next) {
8309 			if (ipif->ipif_zoneid == zoneid ||
8310 			    ipif->ipif_zoneid == ALL_ZONES)
8311 				numifs++;
8312 		}
8313 	}
8314 	rw_exit(&ipst->ips_ill_g_lock);
8315 	return (numifs);
8316 }
8317 
8318 /*
8319  * Return the total number of ipifs.
8320  */
8321 static uint_t
8322 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8323 {
8324 	uint_t numifs = 0;
8325 	ill_t	*ill;
8326 	ipif_t	*ipif;
8327 	ill_walk_context_t	ctx;
8328 
8329 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8330 
8331 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8332 	if (family == AF_INET)
8333 		ill = ILL_START_WALK_V4(&ctx, ipst);
8334 	else if (family == AF_INET6)
8335 		ill = ILL_START_WALK_V6(&ctx, ipst);
8336 	else
8337 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8338 
8339 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8340 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8341 			continue;
8342 
8343 		for (ipif = ill->ill_ipif; ipif != NULL;
8344 		    ipif = ipif->ipif_next) {
8345 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8346 			    !(lifn_flags & LIFC_NOXMIT))
8347 				continue;
8348 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8349 			    !(lifn_flags & LIFC_TEMPORARY))
8350 				continue;
8351 			if (((ipif->ipif_flags &
8352 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8353 			    IPIF_DEPRECATED)) ||
8354 			    IS_LOOPBACK(ill) ||
8355 			    !(ipif->ipif_flags & IPIF_UP)) &&
8356 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8357 				continue;
8358 
8359 			if (zoneid != ipif->ipif_zoneid &&
8360 			    ipif->ipif_zoneid != ALL_ZONES &&
8361 			    (zoneid != GLOBAL_ZONEID ||
8362 			    !(lifn_flags & LIFC_ALLZONES)))
8363 				continue;
8364 
8365 			numifs++;
8366 		}
8367 	}
8368 	rw_exit(&ipst->ips_ill_g_lock);
8369 	return (numifs);
8370 }
8371 
8372 uint_t
8373 ip_get_lifsrcofnum(ill_t *ill)
8374 {
8375 	uint_t numifs = 0;
8376 	ill_t	*ill_head = ill;
8377 	ip_stack_t	*ipst = ill->ill_ipst;
8378 
8379 	/*
8380 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8381 	 * other thread may be trying to relink the ILLs in this usesrc group
8382 	 * and adjusting the ill_usesrc_grp_next pointers
8383 	 */
8384 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8385 	if ((ill->ill_usesrc_ifindex == 0) &&
8386 	    (ill->ill_usesrc_grp_next != NULL)) {
8387 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8388 		    ill = ill->ill_usesrc_grp_next)
8389 			numifs++;
8390 	}
8391 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8392 
8393 	return (numifs);
8394 }
8395 
8396 /* Null values are passed in for ipif, sin, and ifreq */
8397 /* ARGSUSED */
8398 int
8399 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8400     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8401 {
8402 	int *nump;
8403 	conn_t *connp = Q_TO_CONN(q);
8404 
8405 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8406 
8407 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8408 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8409 
8410 	*nump = ip_get_numifs(connp->conn_zoneid,
8411 	    connp->conn_netstack->netstack_ip);
8412 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8413 	return (0);
8414 }
8415 
8416 /* Null values are passed in for ipif, sin, and ifreq */
8417 /* ARGSUSED */
8418 int
8419 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8420     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8421 {
8422 	struct lifnum *lifn;
8423 	mblk_t	*mp1;
8424 	conn_t *connp = Q_TO_CONN(q);
8425 
8426 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8427 
8428 	/* Existence checked in ip_wput_nondata */
8429 	mp1 = mp->b_cont->b_cont;
8430 
8431 	lifn = (struct lifnum *)mp1->b_rptr;
8432 	switch (lifn->lifn_family) {
8433 	case AF_UNSPEC:
8434 	case AF_INET:
8435 	case AF_INET6:
8436 		break;
8437 	default:
8438 		return (EAFNOSUPPORT);
8439 	}
8440 
8441 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8442 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8443 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8444 	return (0);
8445 }
8446 
8447 /* ARGSUSED */
8448 int
8449 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8450     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8451 {
8452 	STRUCT_HANDLE(ifconf, ifc);
8453 	mblk_t *mp1;
8454 	struct iocblk *iocp;
8455 	struct ifreq *ifr;
8456 	ill_walk_context_t	ctx;
8457 	ill_t	*ill;
8458 	ipif_t	*ipif;
8459 	struct sockaddr_in *sin;
8460 	int32_t	ifclen;
8461 	zoneid_t zoneid;
8462 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8463 
8464 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8465 
8466 	ip1dbg(("ip_sioctl_get_ifconf"));
8467 	/* Existence verified in ip_wput_nondata */
8468 	mp1 = mp->b_cont->b_cont;
8469 	iocp = (struct iocblk *)mp->b_rptr;
8470 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8471 
8472 	/*
8473 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8474 	 * the user buffer address and length into which the list of struct
8475 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8476 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8477 	 * the SIOCGIFCONF operation was redefined to simply provide
8478 	 * a large output buffer into which we are supposed to jam the ifreq
8479 	 * array.  The same ioctl command code was used, despite the fact that
8480 	 * both the applications and the kernel code had to change, thus making
8481 	 * it impossible to support both interfaces.
8482 	 *
8483 	 * For reasons not good enough to try to explain, the following
8484 	 * algorithm is used for deciding what to do with one of these:
8485 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8486 	 * form with the output buffer coming down as the continuation message.
8487 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8488 	 * and we have to copy in the ifconf structure to find out how big the
8489 	 * output buffer is and where to copy out to.  Sure no problem...
8490 	 *
8491 	 */
8492 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8493 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8494 		int numifs = 0;
8495 		size_t ifc_bufsize;
8496 
8497 		/*
8498 		 * Must be (better be!) continuation of a TRANSPARENT
8499 		 * IOCTL.  We just copied in the ifconf structure.
8500 		 */
8501 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8502 		    (struct ifconf *)mp1->b_rptr);
8503 
8504 		/*
8505 		 * Allocate a buffer to hold requested information.
8506 		 *
8507 		 * If ifc_len is larger than what is needed, we only
8508 		 * allocate what we will use.
8509 		 *
8510 		 * If ifc_len is smaller than what is needed, return
8511 		 * EINVAL.
8512 		 *
8513 		 * XXX: the ill_t structure can hava 2 counters, for
8514 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8515 		 * number of interfaces for a device, so we don't need
8516 		 * to count them here...
8517 		 */
8518 		numifs = ip_get_numifs(zoneid, ipst);
8519 
8520 		ifclen = STRUCT_FGET(ifc, ifc_len);
8521 		ifc_bufsize = numifs * sizeof (struct ifreq);
8522 		if (ifc_bufsize > ifclen) {
8523 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8524 				/* old behaviour */
8525 				return (EINVAL);
8526 			} else {
8527 				ifc_bufsize = ifclen;
8528 			}
8529 		}
8530 
8531 		mp1 = mi_copyout_alloc(q, mp,
8532 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8533 		if (mp1 == NULL)
8534 			return (ENOMEM);
8535 
8536 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8537 	}
8538 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8539 	/*
8540 	 * the SIOCGIFCONF ioctl only knows about
8541 	 * IPv4 addresses, so don't try to tell
8542 	 * it about interfaces with IPv6-only
8543 	 * addresses. (Last parm 'isv6' is B_FALSE)
8544 	 */
8545 
8546 	ifr = (struct ifreq *)mp1->b_rptr;
8547 
8548 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8549 	ill = ILL_START_WALK_V4(&ctx, ipst);
8550 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8551 		if (IS_UNDER_IPMP(ill))
8552 			continue;
8553 		for (ipif = ill->ill_ipif; ipif != NULL;
8554 		    ipif = ipif->ipif_next) {
8555 			if (zoneid != ipif->ipif_zoneid &&
8556 			    ipif->ipif_zoneid != ALL_ZONES)
8557 				continue;
8558 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8559 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8560 					/* old behaviour */
8561 					rw_exit(&ipst->ips_ill_g_lock);
8562 					return (EINVAL);
8563 				} else {
8564 					goto if_copydone;
8565 				}
8566 			}
8567 			ipif_get_name(ipif, ifr->ifr_name,
8568 			    sizeof (ifr->ifr_name));
8569 			sin = (sin_t *)&ifr->ifr_addr;
8570 			*sin = sin_null;
8571 			sin->sin_family = AF_INET;
8572 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8573 			ifr++;
8574 		}
8575 	}
8576 if_copydone:
8577 	rw_exit(&ipst->ips_ill_g_lock);
8578 	mp1->b_wptr = (uchar_t *)ifr;
8579 
8580 	if (STRUCT_BUF(ifc) != NULL) {
8581 		STRUCT_FSET(ifc, ifc_len,
8582 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8583 	}
8584 	return (0);
8585 }
8586 
8587 /*
8588  * Get the interfaces using the address hosted on the interface passed in,
8589  * as a source adddress
8590  */
8591 /* ARGSUSED */
8592 int
8593 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8594     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8595 {
8596 	mblk_t *mp1;
8597 	ill_t	*ill, *ill_head;
8598 	ipif_t	*ipif, *orig_ipif;
8599 	int	numlifs = 0;
8600 	size_t	lifs_bufsize, lifsmaxlen;
8601 	struct	lifreq *lifr;
8602 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8603 	uint_t	ifindex;
8604 	zoneid_t zoneid;
8605 	int err = 0;
8606 	boolean_t isv6 = B_FALSE;
8607 	struct	sockaddr_in	*sin;
8608 	struct	sockaddr_in6	*sin6;
8609 	STRUCT_HANDLE(lifsrcof, lifs);
8610 	ip_stack_t		*ipst;
8611 
8612 	ipst = CONNQ_TO_IPST(q);
8613 
8614 	ASSERT(q->q_next == NULL);
8615 
8616 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8617 
8618 	/* Existence verified in ip_wput_nondata */
8619 	mp1 = mp->b_cont->b_cont;
8620 
8621 	/*
8622 	 * Must be (better be!) continuation of a TRANSPARENT
8623 	 * IOCTL.  We just copied in the lifsrcof structure.
8624 	 */
8625 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8626 	    (struct lifsrcof *)mp1->b_rptr);
8627 
8628 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8629 		return (EINVAL);
8630 
8631 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8632 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8633 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8634 	    ip_process_ioctl, &err, ipst);
8635 	if (ipif == NULL) {
8636 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8637 		    ifindex));
8638 		return (err);
8639 	}
8640 
8641 	/* Allocate a buffer to hold requested information */
8642 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8643 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8644 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8645 	/* The actual size needed is always returned in lifs_len */
8646 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8647 
8648 	/* If the amount we need is more than what is passed in, abort */
8649 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8650 		ipif_refrele(ipif);
8651 		return (0);
8652 	}
8653 
8654 	mp1 = mi_copyout_alloc(q, mp,
8655 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8656 	if (mp1 == NULL) {
8657 		ipif_refrele(ipif);
8658 		return (ENOMEM);
8659 	}
8660 
8661 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8662 	bzero(mp1->b_rptr, lifs_bufsize);
8663 
8664 	lifr = (struct lifreq *)mp1->b_rptr;
8665 
8666 	ill = ill_head = ipif->ipif_ill;
8667 	orig_ipif = ipif;
8668 
8669 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8670 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8671 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8672 
8673 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8674 	for (; (ill != NULL) && (ill != ill_head);
8675 	    ill = ill->ill_usesrc_grp_next) {
8676 
8677 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8678 			break;
8679 
8680 		ipif = ill->ill_ipif;
8681 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8682 		if (ipif->ipif_isv6) {
8683 			sin6 = (sin6_t *)&lifr->lifr_addr;
8684 			*sin6 = sin6_null;
8685 			sin6->sin6_family = AF_INET6;
8686 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8687 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8688 			    &ipif->ipif_v6net_mask);
8689 		} else {
8690 			sin = (sin_t *)&lifr->lifr_addr;
8691 			*sin = sin_null;
8692 			sin->sin_family = AF_INET;
8693 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8694 			lifr->lifr_addrlen = ip_mask_to_plen(
8695 			    ipif->ipif_net_mask);
8696 		}
8697 		lifr++;
8698 	}
8699 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8700 	rw_exit(&ipst->ips_ill_g_lock);
8701 	ipif_refrele(orig_ipif);
8702 	mp1->b_wptr = (uchar_t *)lifr;
8703 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8704 
8705 	return (0);
8706 }
8707 
8708 /* ARGSUSED */
8709 int
8710 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8711     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8712 {
8713 	mblk_t *mp1;
8714 	int	list;
8715 	ill_t	*ill;
8716 	ipif_t	*ipif;
8717 	int	flags;
8718 	int	numlifs = 0;
8719 	size_t	lifc_bufsize;
8720 	struct	lifreq *lifr;
8721 	sa_family_t	family;
8722 	struct	sockaddr_in	*sin;
8723 	struct	sockaddr_in6	*sin6;
8724 	ill_walk_context_t	ctx;
8725 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8726 	int32_t	lifclen;
8727 	zoneid_t zoneid;
8728 	STRUCT_HANDLE(lifconf, lifc);
8729 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8730 
8731 	ip1dbg(("ip_sioctl_get_lifconf"));
8732 
8733 	ASSERT(q->q_next == NULL);
8734 
8735 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8736 
8737 	/* Existence verified in ip_wput_nondata */
8738 	mp1 = mp->b_cont->b_cont;
8739 
8740 	/*
8741 	 * An extended version of SIOCGIFCONF that takes an
8742 	 * additional address family and flags field.
8743 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8744 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8745 	 * interfaces are omitted.
8746 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8747 	 * unless LIFC_TEMPORARY is specified.
8748 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8749 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8750 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8751 	 * has priority over LIFC_NOXMIT.
8752 	 */
8753 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8754 
8755 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8756 		return (EINVAL);
8757 
8758 	/*
8759 	 * Must be (better be!) continuation of a TRANSPARENT
8760 	 * IOCTL.  We just copied in the lifconf structure.
8761 	 */
8762 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8763 
8764 	family = STRUCT_FGET(lifc, lifc_family);
8765 	flags = STRUCT_FGET(lifc, lifc_flags);
8766 
8767 	switch (family) {
8768 	case AF_UNSPEC:
8769 		/*
8770 		 * walk all ILL's.
8771 		 */
8772 		list = MAX_G_HEADS;
8773 		break;
8774 	case AF_INET:
8775 		/*
8776 		 * walk only IPV4 ILL's.
8777 		 */
8778 		list = IP_V4_G_HEAD;
8779 		break;
8780 	case AF_INET6:
8781 		/*
8782 		 * walk only IPV6 ILL's.
8783 		 */
8784 		list = IP_V6_G_HEAD;
8785 		break;
8786 	default:
8787 		return (EAFNOSUPPORT);
8788 	}
8789 
8790 	/*
8791 	 * Allocate a buffer to hold requested information.
8792 	 *
8793 	 * If lifc_len is larger than what is needed, we only
8794 	 * allocate what we will use.
8795 	 *
8796 	 * If lifc_len is smaller than what is needed, return
8797 	 * EINVAL.
8798 	 */
8799 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8800 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8801 	lifclen = STRUCT_FGET(lifc, lifc_len);
8802 	if (lifc_bufsize > lifclen) {
8803 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8804 			return (EINVAL);
8805 		else
8806 			lifc_bufsize = lifclen;
8807 	}
8808 
8809 	mp1 = mi_copyout_alloc(q, mp,
8810 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8811 	if (mp1 == NULL)
8812 		return (ENOMEM);
8813 
8814 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8815 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8816 
8817 	lifr = (struct lifreq *)mp1->b_rptr;
8818 
8819 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8820 	ill = ill_first(list, list, &ctx, ipst);
8821 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8822 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8823 			continue;
8824 
8825 		for (ipif = ill->ill_ipif; ipif != NULL;
8826 		    ipif = ipif->ipif_next) {
8827 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8828 			    !(flags & LIFC_NOXMIT))
8829 				continue;
8830 
8831 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8832 			    !(flags & LIFC_TEMPORARY))
8833 				continue;
8834 
8835 			if (((ipif->ipif_flags &
8836 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8837 			    IPIF_DEPRECATED)) ||
8838 			    IS_LOOPBACK(ill) ||
8839 			    !(ipif->ipif_flags & IPIF_UP)) &&
8840 			    (flags & LIFC_EXTERNAL_SOURCE))
8841 				continue;
8842 
8843 			if (zoneid != ipif->ipif_zoneid &&
8844 			    ipif->ipif_zoneid != ALL_ZONES &&
8845 			    (zoneid != GLOBAL_ZONEID ||
8846 			    !(flags & LIFC_ALLZONES)))
8847 				continue;
8848 
8849 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8850 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8851 					rw_exit(&ipst->ips_ill_g_lock);
8852 					return (EINVAL);
8853 				} else {
8854 					goto lif_copydone;
8855 				}
8856 			}
8857 
8858 			ipif_get_name(ipif, lifr->lifr_name,
8859 			    sizeof (lifr->lifr_name));
8860 			lifr->lifr_type = ill->ill_type;
8861 			if (ipif->ipif_isv6) {
8862 				sin6 = (sin6_t *)&lifr->lifr_addr;
8863 				*sin6 = sin6_null;
8864 				sin6->sin6_family = AF_INET6;
8865 				sin6->sin6_addr =
8866 				    ipif->ipif_v6lcl_addr;
8867 				lifr->lifr_addrlen =
8868 				    ip_mask_to_plen_v6(
8869 				    &ipif->ipif_v6net_mask);
8870 			} else {
8871 				sin = (sin_t *)&lifr->lifr_addr;
8872 				*sin = sin_null;
8873 				sin->sin_family = AF_INET;
8874 				sin->sin_addr.s_addr =
8875 				    ipif->ipif_lcl_addr;
8876 				lifr->lifr_addrlen =
8877 				    ip_mask_to_plen(
8878 				    ipif->ipif_net_mask);
8879 			}
8880 			lifr++;
8881 		}
8882 	}
8883 lif_copydone:
8884 	rw_exit(&ipst->ips_ill_g_lock);
8885 
8886 	mp1->b_wptr = (uchar_t *)lifr;
8887 	if (STRUCT_BUF(lifc) != NULL) {
8888 		STRUCT_FSET(lifc, lifc_len,
8889 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8890 	}
8891 	return (0);
8892 }
8893 
8894 static void
8895 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8896 {
8897 	ip6_asp_t *table;
8898 	size_t table_size;
8899 	mblk_t *data_mp;
8900 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8901 	ip_stack_t	*ipst;
8902 
8903 	if (q->q_next == NULL)
8904 		ipst = CONNQ_TO_IPST(q);
8905 	else
8906 		ipst = ILLQ_TO_IPST(q);
8907 
8908 	/* These two ioctls are I_STR only */
8909 	if (iocp->ioc_count == TRANSPARENT) {
8910 		miocnak(q, mp, 0, EINVAL);
8911 		return;
8912 	}
8913 
8914 	data_mp = mp->b_cont;
8915 	if (data_mp == NULL) {
8916 		/* The user passed us a NULL argument */
8917 		table = NULL;
8918 		table_size = iocp->ioc_count;
8919 	} else {
8920 		/*
8921 		 * The user provided a table.  The stream head
8922 		 * may have copied in the user data in chunks,
8923 		 * so make sure everything is pulled up
8924 		 * properly.
8925 		 */
8926 		if (MBLKL(data_mp) < iocp->ioc_count) {
8927 			mblk_t *new_data_mp;
8928 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8929 			    NULL) {
8930 				miocnak(q, mp, 0, ENOMEM);
8931 				return;
8932 			}
8933 			freemsg(data_mp);
8934 			data_mp = new_data_mp;
8935 			mp->b_cont = data_mp;
8936 		}
8937 		table = (ip6_asp_t *)data_mp->b_rptr;
8938 		table_size = iocp->ioc_count;
8939 	}
8940 
8941 	switch (iocp->ioc_cmd) {
8942 	case SIOCGIP6ADDRPOLICY:
8943 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8944 		if (iocp->ioc_rval == -1)
8945 			iocp->ioc_error = EINVAL;
8946 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8947 		else if (table != NULL &&
8948 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8949 			ip6_asp_t *src = table;
8950 			ip6_asp32_t *dst = (void *)table;
8951 			int count = table_size / sizeof (ip6_asp_t);
8952 			int i;
8953 
8954 			/*
8955 			 * We need to do an in-place shrink of the array
8956 			 * to match the alignment attributes of the
8957 			 * 32-bit ABI looking at it.
8958 			 */
8959 			/* LINTED: logical expression always true: op "||" */
8960 			ASSERT(sizeof (*src) > sizeof (*dst));
8961 			for (i = 1; i < count; i++)
8962 				bcopy(src + i, dst + i, sizeof (*dst));
8963 		}
8964 #endif
8965 		break;
8966 
8967 	case SIOCSIP6ADDRPOLICY:
8968 		ASSERT(mp->b_prev == NULL);
8969 		mp->b_prev = (void *)q;
8970 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8971 		/*
8972 		 * We pass in the datamodel here so that the ip6_asp_replace()
8973 		 * routine can handle converting from 32-bit to native formats
8974 		 * where necessary.
8975 		 *
8976 		 * A better way to handle this might be to convert the inbound
8977 		 * data structure here, and hang it off a new 'mp'; thus the
8978 		 * ip6_asp_replace() logic would always be dealing with native
8979 		 * format data structures..
8980 		 *
8981 		 * (An even simpler way to handle these ioctls is to just
8982 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8983 		 * and just recompile everything that depends on it.)
8984 		 */
8985 #endif
8986 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
8987 		    iocp->ioc_flag & IOC_MODELS);
8988 		return;
8989 	}
8990 
8991 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8992 	qreply(q, mp);
8993 }
8994 
8995 static void
8996 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8997 {
8998 	mblk_t 		*data_mp;
8999 	struct dstinforeq	*dir;
9000 	uint8_t		*end, *cur;
9001 	in6_addr_t	*daddr, *saddr;
9002 	ipaddr_t	v4daddr;
9003 	ire_t		*ire;
9004 	char		*slabel, *dlabel;
9005 	boolean_t	isipv4;
9006 	int		match_ire;
9007 	ill_t		*dst_ill;
9008 	ipif_t		*src_ipif, *ire_ipif;
9009 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9010 	zoneid_t	zoneid;
9011 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9012 
9013 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9014 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9015 
9016 	/*
9017 	 * This ioctl is I_STR only, and must have a
9018 	 * data mblk following the M_IOCTL mblk.
9019 	 */
9020 	data_mp = mp->b_cont;
9021 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9022 		miocnak(q, mp, 0, EINVAL);
9023 		return;
9024 	}
9025 
9026 	if (MBLKL(data_mp) < iocp->ioc_count) {
9027 		mblk_t *new_data_mp;
9028 
9029 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9030 			miocnak(q, mp, 0, ENOMEM);
9031 			return;
9032 		}
9033 		freemsg(data_mp);
9034 		data_mp = new_data_mp;
9035 		mp->b_cont = data_mp;
9036 	}
9037 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9038 
9039 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9040 	    end - cur >= sizeof (struct dstinforeq);
9041 	    cur += sizeof (struct dstinforeq)) {
9042 		dir = (struct dstinforeq *)cur;
9043 		daddr = &dir->dir_daddr;
9044 		saddr = &dir->dir_saddr;
9045 
9046 		/*
9047 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9048 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9049 		 * and ipif_select_source[_v6]() do not.
9050 		 */
9051 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9052 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9053 
9054 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9055 		if (isipv4) {
9056 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9057 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9058 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9059 		} else {
9060 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9061 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9062 		}
9063 		if (ire == NULL) {
9064 			dir->dir_dreachable = 0;
9065 
9066 			/* move on to next dst addr */
9067 			continue;
9068 		}
9069 		dir->dir_dreachable = 1;
9070 
9071 		ire_ipif = ire->ire_ipif;
9072 		if (ire_ipif == NULL)
9073 			goto next_dst;
9074 
9075 		/*
9076 		 * We expect to get back an interface ire or a
9077 		 * gateway ire cache entry.  For both types, the
9078 		 * output interface is ire_ipif->ipif_ill.
9079 		 */
9080 		dst_ill = ire_ipif->ipif_ill;
9081 		dir->dir_dmactype = dst_ill->ill_mactype;
9082 
9083 		if (isipv4) {
9084 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9085 		} else {
9086 			src_ipif = ipif_select_source_v6(dst_ill,
9087 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9088 		}
9089 		if (src_ipif == NULL)
9090 			goto next_dst;
9091 
9092 		*saddr = src_ipif->ipif_v6lcl_addr;
9093 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9094 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9095 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9096 		dir->dir_sdeprecated =
9097 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9098 		ipif_refrele(src_ipif);
9099 next_dst:
9100 		ire_refrele(ire);
9101 	}
9102 	miocack(q, mp, iocp->ioc_count, 0);
9103 }
9104 
9105 /*
9106  * Check if this is an address assigned to this machine.
9107  * Skips interfaces that are down by using ire checks.
9108  * Translates mapped addresses to v4 addresses and then
9109  * treats them as such, returning true if the v4 address
9110  * associated with this mapped address is configured.
9111  * Note: Applications will have to be careful what they do
9112  * with the response; use of mapped addresses limits
9113  * what can be done with the socket, especially with
9114  * respect to socket options and ioctls - neither IPv4
9115  * options nor IPv6 sticky options/ancillary data options
9116  * may be used.
9117  */
9118 /* ARGSUSED */
9119 int
9120 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9121     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9122 {
9123 	struct sioc_addrreq *sia;
9124 	sin_t *sin;
9125 	ire_t *ire;
9126 	mblk_t *mp1;
9127 	zoneid_t zoneid;
9128 	ip_stack_t	*ipst;
9129 
9130 	ip1dbg(("ip_sioctl_tmyaddr"));
9131 
9132 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9133 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9134 	ipst = CONNQ_TO_IPST(q);
9135 
9136 	/* Existence verified in ip_wput_nondata */
9137 	mp1 = mp->b_cont->b_cont;
9138 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9139 	sin = (sin_t *)&sia->sa_addr;
9140 	switch (sin->sin_family) {
9141 	case AF_INET6: {
9142 		sin6_t *sin6 = (sin6_t *)sin;
9143 
9144 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9145 			ipaddr_t v4_addr;
9146 
9147 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9148 			    v4_addr);
9149 			ire = ire_ctable_lookup(v4_addr, 0,
9150 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9151 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9152 		} else {
9153 			in6_addr_t v6addr;
9154 
9155 			v6addr = sin6->sin6_addr;
9156 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9157 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9158 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9159 		}
9160 		break;
9161 	}
9162 	case AF_INET: {
9163 		ipaddr_t v4addr;
9164 
9165 		v4addr = sin->sin_addr.s_addr;
9166 		ire = ire_ctable_lookup(v4addr, 0,
9167 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9168 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9169 		break;
9170 	}
9171 	default:
9172 		return (EAFNOSUPPORT);
9173 	}
9174 	if (ire != NULL) {
9175 		sia->sa_res = 1;
9176 		ire_refrele(ire);
9177 	} else {
9178 		sia->sa_res = 0;
9179 	}
9180 	return (0);
9181 }
9182 
9183 /*
9184  * Check if this is an address assigned on-link i.e. neighbor,
9185  * and makes sure it's reachable from the current zone.
9186  * Returns true for my addresses as well.
9187  * Translates mapped addresses to v4 addresses and then
9188  * treats them as such, returning true if the v4 address
9189  * associated with this mapped address is configured.
9190  * Note: Applications will have to be careful what they do
9191  * with the response; use of mapped addresses limits
9192  * what can be done with the socket, especially with
9193  * respect to socket options and ioctls - neither IPv4
9194  * options nor IPv6 sticky options/ancillary data options
9195  * may be used.
9196  */
9197 /* ARGSUSED */
9198 int
9199 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9200     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9201 {
9202 	struct sioc_addrreq *sia;
9203 	sin_t *sin;
9204 	mblk_t	*mp1;
9205 	ire_t *ire = NULL;
9206 	zoneid_t zoneid;
9207 	ip_stack_t	*ipst;
9208 
9209 	ip1dbg(("ip_sioctl_tonlink"));
9210 
9211 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9212 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9213 	ipst = CONNQ_TO_IPST(q);
9214 
9215 	/* Existence verified in ip_wput_nondata */
9216 	mp1 = mp->b_cont->b_cont;
9217 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9218 	sin = (sin_t *)&sia->sa_addr;
9219 
9220 	/*
9221 	 * Match addresses with a zero gateway field to avoid
9222 	 * routes going through a router.
9223 	 * Exclude broadcast and multicast addresses.
9224 	 */
9225 	switch (sin->sin_family) {
9226 	case AF_INET6: {
9227 		sin6_t *sin6 = (sin6_t *)sin;
9228 
9229 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9230 			ipaddr_t v4_addr;
9231 
9232 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9233 			    v4_addr);
9234 			if (!CLASSD(v4_addr)) {
9235 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9236 				    NULL, NULL, zoneid, NULL,
9237 				    MATCH_IRE_GW, ipst);
9238 			}
9239 		} else {
9240 			in6_addr_t v6addr;
9241 			in6_addr_t v6gw;
9242 
9243 			v6addr = sin6->sin6_addr;
9244 			v6gw = ipv6_all_zeros;
9245 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9246 				ire = ire_route_lookup_v6(&v6addr, 0,
9247 				    &v6gw, 0, NULL, NULL, zoneid,
9248 				    NULL, MATCH_IRE_GW, ipst);
9249 			}
9250 		}
9251 		break;
9252 	}
9253 	case AF_INET: {
9254 		ipaddr_t v4addr;
9255 
9256 		v4addr = sin->sin_addr.s_addr;
9257 		if (!CLASSD(v4addr)) {
9258 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9259 			    NULL, NULL, zoneid, NULL,
9260 			    MATCH_IRE_GW, ipst);
9261 		}
9262 		break;
9263 	}
9264 	default:
9265 		return (EAFNOSUPPORT);
9266 	}
9267 	sia->sa_res = 0;
9268 	if (ire != NULL) {
9269 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9270 		    IRE_LOCAL|IRE_LOOPBACK)) {
9271 			sia->sa_res = 1;
9272 		}
9273 		ire_refrele(ire);
9274 	}
9275 	return (0);
9276 }
9277 
9278 /*
9279  * TBD: implement when kernel maintaines a list of site prefixes.
9280  */
9281 /* ARGSUSED */
9282 int
9283 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9284     ip_ioctl_cmd_t *ipip, void *ifreq)
9285 {
9286 	return (ENXIO);
9287 }
9288 
9289 /* ARGSUSED */
9290 int
9291 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9292     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9293 {
9294 	ill_t		*ill;
9295 	mblk_t		*mp1;
9296 	conn_t		*connp;
9297 	boolean_t	success;
9298 
9299 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9300 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9301 	/* ioctl comes down on an conn */
9302 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9303 	connp = Q_TO_CONN(q);
9304 
9305 	mp->b_datap->db_type = M_IOCTL;
9306 
9307 	/*
9308 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9309 	 * The original mp contains contaminated b_next values due to 'mi',
9310 	 * which is needed to do the mi_copy_done. Unfortunately if we
9311 	 * send down the original mblk itself and if we are popped due to an
9312 	 * an unplumb before the response comes back from tunnel,
9313 	 * the streamhead (which does a freemsg) will see this contaminated
9314 	 * message and the assertion in freemsg about non-null b_next/b_prev
9315 	 * will panic a DEBUG kernel.
9316 	 */
9317 	mp1 = copymsg(mp);
9318 	if (mp1 == NULL)
9319 		return (ENOMEM);
9320 
9321 	ill = ipif->ipif_ill;
9322 	mutex_enter(&connp->conn_lock);
9323 	mutex_enter(&ill->ill_lock);
9324 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9325 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9326 		    mp, 0);
9327 	} else {
9328 		success = ill_pending_mp_add(ill, connp, mp);
9329 	}
9330 	mutex_exit(&ill->ill_lock);
9331 	mutex_exit(&connp->conn_lock);
9332 
9333 	if (success) {
9334 		ip1dbg(("sending down tunparam request "));
9335 		putnext(ill->ill_wq, mp1);
9336 		return (EINPROGRESS);
9337 	} else {
9338 		/* The conn has started closing */
9339 		freemsg(mp1);
9340 		return (EINTR);
9341 	}
9342 }
9343 
9344 /*
9345  * ARP IOCTLs.
9346  * How does IP get in the business of fronting ARP configuration/queries?
9347  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9348  * are by tradition passed in through a datagram socket.  That lands in IP.
9349  * As it happens, this is just as well since the interface is quite crude in
9350  * that it passes in no information about protocol or hardware types, or
9351  * interface association.  After making the protocol assumption, IP is in
9352  * the position to look up the name of the ILL, which ARP will need, and
9353  * format a request that can be handled by ARP.  The request is passed up
9354  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9355  * back a response.  ARP supports its own set of more general IOCTLs, in
9356  * case anyone is interested.
9357  */
9358 /* ARGSUSED */
9359 int
9360 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9361     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9362 {
9363 	mblk_t *mp1;
9364 	mblk_t *mp2;
9365 	mblk_t *pending_mp;
9366 	ipaddr_t ipaddr;
9367 	area_t *area;
9368 	struct iocblk *iocp;
9369 	conn_t *connp;
9370 	struct arpreq *ar;
9371 	struct xarpreq *xar;
9372 	int flags, alength;
9373 	uchar_t *lladdr;
9374 	ire_t *ire;
9375 	ip_stack_t *ipst;
9376 	ill_t *ill = ipif->ipif_ill;
9377 	ill_t *proxy_ill = NULL;
9378 	ipmp_arpent_t *entp = NULL;
9379 	boolean_t if_arp_ioctl = B_FALSE;
9380 	boolean_t proxyarp = B_FALSE;
9381 
9382 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9383 	connp = Q_TO_CONN(q);
9384 	ipst = connp->conn_netstack->netstack_ip;
9385 
9386 	if (ipip->ipi_cmd_type == XARP_CMD) {
9387 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9388 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9389 		ar = NULL;
9390 
9391 		flags = xar->xarp_flags;
9392 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9393 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9394 		/*
9395 		 * Validate against user's link layer address length
9396 		 * input and name and addr length limits.
9397 		 */
9398 		alength = ill->ill_phys_addr_length;
9399 		if (ipip->ipi_cmd == SIOCSXARP) {
9400 			if (alength != xar->xarp_ha.sdl_alen ||
9401 			    (alength + xar->xarp_ha.sdl_nlen >
9402 			    sizeof (xar->xarp_ha.sdl_data)))
9403 				return (EINVAL);
9404 		}
9405 	} else {
9406 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9407 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9408 		xar = NULL;
9409 
9410 		flags = ar->arp_flags;
9411 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9412 		/*
9413 		 * Theoretically, the sa_family could tell us what link
9414 		 * layer type this operation is trying to deal with. By
9415 		 * common usage AF_UNSPEC means ethernet. We'll assume
9416 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9417 		 * for now. Our new SIOC*XARP ioctls can be used more
9418 		 * generally.
9419 		 *
9420 		 * If the underlying media happens to have a non 6 byte
9421 		 * address, arp module will fail set/get, but the del
9422 		 * operation will succeed.
9423 		 */
9424 		alength = 6;
9425 		if ((ipip->ipi_cmd != SIOCDARP) &&
9426 		    (alength != ill->ill_phys_addr_length)) {
9427 			return (EINVAL);
9428 		}
9429 	}
9430 
9431 	ipaddr = sin->sin_addr.s_addr;
9432 
9433 	/*
9434 	 * IPMP ARP special handling:
9435 	 *
9436 	 * 1. Since ARP mappings must appear consistent across the group,
9437 	 *    prohibit changing ARP mappings on the underlying interfaces.
9438 	 *
9439 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9440 	 *    IP itself, prohibit changing them.
9441 	 *
9442 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9443 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9444 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9445 	 */
9446 	if (IS_UNDER_IPMP(ill)) {
9447 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9448 			return (EPERM);
9449 	}
9450 	if (IS_IPMP(ill)) {
9451 		ipmp_illgrp_t *illg = ill->ill_grp;
9452 
9453 		switch (ipip->ipi_cmd) {
9454 		case SIOCSARP:
9455 		case SIOCSXARP:
9456 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9457 			if (proxy_ill != NULL) {
9458 				proxyarp = B_TRUE;
9459 				if (!ipmp_ill_is_active(proxy_ill))
9460 					proxy_ill = ipmp_illgrp_next_ill(illg);
9461 				if (proxy_ill != NULL)
9462 					lladdr = proxy_ill->ill_phys_addr;
9463 			}
9464 			/* FALLTHRU */
9465 		case SIOCDARP:
9466 		case SIOCDXARP:
9467 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9468 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9469 			if (ire != NULL) {
9470 				ire_refrele(ire);
9471 				return (EPERM);
9472 			}
9473 		}
9474 	}
9475 
9476 	/*
9477 	 * We are going to pass up to ARP a packet chain that looks
9478 	 * like:
9479 	 *
9480 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9481 	 *
9482 	 * Get a copy of the original IOCTL mblk to head the chain,
9483 	 * to be sent up (in mp1). Also get another copy to store
9484 	 * in the ill_pending_mp list, for matching the response
9485 	 * when it comes back from ARP.
9486 	 */
9487 	mp1 = copyb(mp);
9488 	pending_mp = copymsg(mp);
9489 	if (mp1 == NULL || pending_mp == NULL) {
9490 		if (mp1 != NULL)
9491 			freeb(mp1);
9492 		if (pending_mp != NULL)
9493 			inet_freemsg(pending_mp);
9494 		return (ENOMEM);
9495 	}
9496 
9497 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9498 	    (caddr_t)&ipaddr);
9499 	if (mp2 == NULL) {
9500 		freeb(mp1);
9501 		inet_freemsg(pending_mp);
9502 		return (ENOMEM);
9503 	}
9504 	/* Put together the chain. */
9505 	mp1->b_cont = mp2;
9506 	mp1->b_datap->db_type = M_IOCTL;
9507 	mp2->b_cont = mp;
9508 	mp2->b_datap->db_type = M_DATA;
9509 
9510 	iocp = (struct iocblk *)mp1->b_rptr;
9511 
9512 	/*
9513 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9514 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9515 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9516 	 * ioc_count field; set ioc_count to be correct.
9517 	 */
9518 	iocp->ioc_count = MBLKL(mp1->b_cont);
9519 
9520 	/*
9521 	 * Set the proper command in the ARP message.
9522 	 * Convert the SIOC{G|S|D}ARP calls into our
9523 	 * AR_ENTRY_xxx calls.
9524 	 */
9525 	area = (area_t *)mp2->b_rptr;
9526 	switch (iocp->ioc_cmd) {
9527 	case SIOCDARP:
9528 	case SIOCDXARP:
9529 		/*
9530 		 * We defer deleting the corresponding IRE until
9531 		 * we return from arp.
9532 		 */
9533 		area->area_cmd = AR_ENTRY_DELETE;
9534 		area->area_proto_mask_offset = 0;
9535 		break;
9536 	case SIOCGARP:
9537 	case SIOCGXARP:
9538 		area->area_cmd = AR_ENTRY_SQUERY;
9539 		area->area_proto_mask_offset = 0;
9540 		break;
9541 	case SIOCSARP:
9542 	case SIOCSXARP:
9543 		/*
9544 		 * Delete the corresponding ire to make sure IP will
9545 		 * pick up any change from arp.
9546 		 */
9547 		if (!if_arp_ioctl) {
9548 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9549 		} else {
9550 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9551 			if (ipif != NULL) {
9552 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9553 				    ipst);
9554 				ipif_refrele(ipif);
9555 			}
9556 		}
9557 		break;
9558 	}
9559 	iocp->ioc_cmd = area->area_cmd;
9560 
9561 	/*
9562 	 * Fill in the rest of the ARP operation fields.
9563 	 */
9564 	area->area_hw_addr_length = alength;
9565 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9566 
9567 	/* Translate the flags. */
9568 	if (flags & ATF_PERM)
9569 		area->area_flags |= ACE_F_PERMANENT;
9570 	if (flags & ATF_PUBL)
9571 		area->area_flags |= ACE_F_PUBLISH;
9572 	if (flags & ATF_AUTHORITY)
9573 		area->area_flags |= ACE_F_AUTHORITY;
9574 
9575 	/*
9576 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9577 	 * so that IP can update ARP as the active ills in the group change.
9578 	 */
9579 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9580 	    (area->area_flags & ACE_F_PERMANENT)) {
9581 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9582 
9583 		/*
9584 		 * The second part of the conditional below handles a corner
9585 		 * case: if this is proxy ARP and the IPMP group has no active
9586 		 * interfaces, we can't send the request to ARP now since it
9587 		 * won't be able to build an ACE.  So we return success and
9588 		 * notify ARP about the proxy ARP entry once an interface
9589 		 * becomes active.
9590 		 */
9591 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9592 			mp2->b_cont = NULL;
9593 			inet_freemsg(mp1);
9594 			inet_freemsg(pending_mp);
9595 			return (entp == NULL ? ENOMEM : 0);
9596 		}
9597 	}
9598 
9599 	/*
9600 	 * Before sending 'mp' to ARP, we have to clear the b_next
9601 	 * and b_prev. Otherwise if STREAMS encounters such a message
9602 	 * in freemsg(), (because ARP can close any time) it can cause
9603 	 * a panic. But mi code needs the b_next and b_prev values of
9604 	 * mp->b_cont, to complete the ioctl. So we store it here
9605 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9606 	 * when the response comes down from ARP.
9607 	 */
9608 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9609 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9610 	mp->b_cont->b_next = NULL;
9611 	mp->b_cont->b_prev = NULL;
9612 
9613 	mutex_enter(&connp->conn_lock);
9614 	mutex_enter(&ill->ill_lock);
9615 	/* conn has not yet started closing, hence this can't fail */
9616 	if (ipip->ipi_flags & IPI_WR) {
9617 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9618 		    pending_mp, 0) != 0);
9619 	} else {
9620 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9621 	}
9622 	mutex_exit(&ill->ill_lock);
9623 	mutex_exit(&connp->conn_lock);
9624 
9625 	/*
9626 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9627 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9628 	 */
9629 	putnext(ill->ill_rq, mp1);
9630 
9631 	/*
9632 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9633 	 */
9634 	if (entp != NULL)
9635 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9636 
9637 	return (EINPROGRESS);
9638 }
9639 
9640 /*
9641  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9642  * the associated sin and refhold and return the associated ipif via `ci'.
9643  */
9644 int
9645 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9646     cmd_info_t *ci, ipsq_func_t func)
9647 {
9648 	mblk_t	*mp1;
9649 	int	err;
9650 	sin_t	*sin;
9651 	conn_t	*connp;
9652 	ipif_t	*ipif;
9653 	ire_t	*ire = NULL;
9654 	ill_t	*ill = NULL;
9655 	boolean_t exists;
9656 	ip_stack_t *ipst;
9657 	struct arpreq *ar;
9658 	struct xarpreq *xar;
9659 	struct sockaddr_dl *sdl;
9660 
9661 	/* ioctl comes down on a conn */
9662 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9663 	connp = Q_TO_CONN(q);
9664 	if (connp->conn_af_isv6)
9665 		return (ENXIO);
9666 
9667 	ipst = connp->conn_netstack->netstack_ip;
9668 
9669 	/* Verified in ip_wput_nondata */
9670 	mp1 = mp->b_cont->b_cont;
9671 
9672 	if (ipip->ipi_cmd_type == XARP_CMD) {
9673 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9674 		xar = (struct xarpreq *)mp1->b_rptr;
9675 		sin = (sin_t *)&xar->xarp_pa;
9676 		sdl = &xar->xarp_ha;
9677 
9678 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9679 			return (ENXIO);
9680 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9681 			return (EINVAL);
9682 	} else {
9683 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9684 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9685 		ar = (struct arpreq *)mp1->b_rptr;
9686 		sin = (sin_t *)&ar->arp_pa;
9687 	}
9688 
9689 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9690 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9691 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9692 		    mp, func, &err, ipst);
9693 		if (ipif == NULL)
9694 			return (err);
9695 		if (ipif->ipif_id != 0) {
9696 			ipif_refrele(ipif);
9697 			return (ENXIO);
9698 		}
9699 	} else {
9700 		/*
9701 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9702 		 * of 0: use the IP address to find the ipif.  If the IP
9703 		 * address is an IPMP test address, ire_ftable_lookup() will
9704 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9705 		 */
9706 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9707 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9708 		if (ipif == NULL) {
9709 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9710 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9711 			    MATCH_IRE_TYPE, ipst);
9712 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9713 				if (ire != NULL)
9714 					ire_refrele(ire);
9715 				return (ENXIO);
9716 			}
9717 			ipif = ill->ill_ipif;
9718 			ipif_refhold(ipif);
9719 			ire_refrele(ire);
9720 		}
9721 	}
9722 
9723 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9724 		ipif_refrele(ipif);
9725 		return (ENXIO);
9726 	}
9727 
9728 	ci->ci_sin = sin;
9729 	ci->ci_ipif = ipif;
9730 	return (0);
9731 }
9732 
9733 /*
9734  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9735  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9736  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9737  * up and thus an ill can join that illgrp.
9738  *
9739  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9740  * open()/close() primarily because close() is not allowed to fail or block
9741  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9742  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9743  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9744  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9745  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9746  * state if I_UNLINK didn't occur.
9747  *
9748  * Note that for each plumb/unplumb operation, we may end up here more than
9749  * once because of the way ifconfig works.  However, it's OK to link the same
9750  * illgrp more than once, or unlink an illgrp that's already unlinked.
9751  */
9752 static int
9753 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9754 {
9755 	int err;
9756 	ip_stack_t *ipst = ill->ill_ipst;
9757 
9758 	ASSERT(IS_IPMP(ill));
9759 	ASSERT(IAM_WRITER_ILL(ill));
9760 
9761 	switch (ioccmd) {
9762 	case I_LINK:
9763 		return (ENOTSUP);
9764 
9765 	case I_PLINK:
9766 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9767 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9768 		rw_exit(&ipst->ips_ipmp_lock);
9769 		break;
9770 
9771 	case I_PUNLINK:
9772 		/*
9773 		 * Require all UP ipifs be brought down prior to unlinking the
9774 		 * illgrp so any associated IREs (and other state) is torched.
9775 		 */
9776 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9777 			return (EBUSY);
9778 
9779 		/*
9780 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9781 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9782 		 * join this group.  Specifically: ills trying to join grab
9783 		 * ipmp_lock and bump a "pending join" counter checked by
9784 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9785 		 * joins can occur (since we have ipmp_lock).  Once we drop
9786 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9787 		 * find the illgrp (since we unlinked it) and will return
9788 		 * EAFNOSUPPORT.  This will then take them back through the
9789 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9790 		 * back through I_PLINK above.
9791 		 */
9792 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9793 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9794 		rw_exit(&ipst->ips_ipmp_lock);
9795 		return (err);
9796 	default:
9797 		break;
9798 	}
9799 	return (0);
9800 }
9801 
9802 /*
9803  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9804  * atomically set/clear the muxids. Also complete the ioctl by acking or
9805  * naking it.  Note that the code is structured such that the link type,
9806  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9807  * its clones use the persistent link, while pppd(1M) and perhaps many
9808  * other daemons may use non-persistent link.  When combined with some
9809  * ill_t states, linking and unlinking lower streams may be used as
9810  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9811  */
9812 /* ARGSUSED */
9813 void
9814 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9815 {
9816 	mblk_t		*mp1, *mp2;
9817 	struct linkblk	*li;
9818 	struct ipmx_s	*ipmxp;
9819 	ill_t		*ill;
9820 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9821 	int		err = 0;
9822 	boolean_t	entered_ipsq = B_FALSE;
9823 	boolean_t	islink;
9824 	ip_stack_t	*ipst;
9825 
9826 	if (CONN_Q(q))
9827 		ipst = CONNQ_TO_IPST(q);
9828 	else
9829 		ipst = ILLQ_TO_IPST(q);
9830 
9831 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9832 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9833 
9834 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9835 
9836 	mp1 = mp->b_cont;	/* This is the linkblk info */
9837 	li = (struct linkblk *)mp1->b_rptr;
9838 
9839 	/*
9840 	 * ARP has added this special mblk, and the utility is asking us
9841 	 * to perform consistency checks, and also atomically set the
9842 	 * muxid. Ifconfig is an example.  It achieves this by using
9843 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9844 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9845 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9846 	 * and other comments in this routine for more details.
9847 	 */
9848 	mp2 = mp1->b_cont;	/* This is added by ARP */
9849 
9850 	/*
9851 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9852 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9853 	 * get the special mblk above.  For backward compatibility, we
9854 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9855 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9856 	 * not atomic, and can leave the streams unplumbable if the utility
9857 	 * is interrupted before it does the SIOCSLIFMUXID.
9858 	 */
9859 	if (mp2 == NULL) {
9860 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9861 		if (err == EINPROGRESS)
9862 			return;
9863 		goto done;
9864 	}
9865 
9866 	/*
9867 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9868 	 * ARP has appended this last mblk to tell us whether the lower stream
9869 	 * is an arp-dev stream or an IP module stream.
9870 	 */
9871 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9872 	if (ipmxp->ipmx_arpdev_stream) {
9873 		/*
9874 		 * The lower stream is the arp-dev stream.
9875 		 */
9876 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9877 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9878 		if (ill == NULL) {
9879 			if (err == EINPROGRESS)
9880 				return;
9881 			err = EINVAL;
9882 			goto done;
9883 		}
9884 
9885 		if (ipsq == NULL) {
9886 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9887 			    NEW_OP, B_FALSE);
9888 			if (ipsq == NULL) {
9889 				ill_refrele(ill);
9890 				return;
9891 			}
9892 			entered_ipsq = B_TRUE;
9893 		}
9894 		ASSERT(IAM_WRITER_ILL(ill));
9895 		ill_refrele(ill);
9896 
9897 		/*
9898 		 * To ensure consistency between IP and ARP, the following
9899 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9900 		 * This is because the muxid's are stored in the IP stream on
9901 		 * the ill.
9902 		 *
9903 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9904 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9905 		 * not yet plinked, and it also checks that the corresponding
9906 		 * IP stream is already plinked.
9907 		 *
9908 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9909 		 * punlinking the IP stream. IP does not allow punlink of the
9910 		 * IP stream unless the arp stream has been punlinked.
9911 		 */
9912 		if ((islink &&
9913 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9914 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9915 			err = EINVAL;
9916 			goto done;
9917 		}
9918 
9919 		if (IS_IPMP(ill) &&
9920 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9921 			goto done;
9922 
9923 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9924 	} else {
9925 		/*
9926 		 * The lower stream is probably an IP module stream.  Do
9927 		 * consistency checking.
9928 		 */
9929 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9930 		if (err == EINPROGRESS)
9931 			return;
9932 	}
9933 done:
9934 	if (err == 0)
9935 		miocack(q, mp, 0, 0);
9936 	else
9937 		miocnak(q, mp, 0, err);
9938 
9939 	/* Conn was refheld in ip_sioctl_copyin_setup */
9940 	if (CONN_Q(q))
9941 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9942 	if (entered_ipsq)
9943 		ipsq_exit(ipsq);
9944 }
9945 
9946 /*
9947  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9948  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9949  * module stream).  If `doconsist' is set, then do the extended consistency
9950  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9951  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9952  * an error code on failure.
9953  */
9954 static int
9955 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9956     struct linkblk *li, boolean_t doconsist)
9957 {
9958 	int		err = 0;
9959 	ill_t  		*ill;
9960 	queue_t		*ipwq, *dwq;
9961 	const char	*name;
9962 	struct qinit	*qinfo;
9963 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9964 	boolean_t	entered_ipsq = B_FALSE;
9965 
9966 	/*
9967 	 * Walk the lower stream to verify it's the IP module stream.
9968 	 * The IP module is identified by its name, wput function,
9969 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9970 	 * (li->l_qbot) will not vanish until this ioctl completes.
9971 	 */
9972 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9973 		qinfo = ipwq->q_qinfo;
9974 		name = qinfo->qi_minfo->mi_idname;
9975 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9976 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9977 			break;
9978 		}
9979 	}
9980 
9981 	/*
9982 	 * If this isn't an IP module stream, bail.
9983 	 */
9984 	if (ipwq == NULL)
9985 		return (0);
9986 
9987 	ill = ipwq->q_ptr;
9988 	ASSERT(ill != NULL);
9989 
9990 	if (ipsq == NULL) {
9991 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9992 		    NEW_OP, B_FALSE);
9993 		if (ipsq == NULL)
9994 			return (EINPROGRESS);
9995 		entered_ipsq = B_TRUE;
9996 	}
9997 	ASSERT(IAM_WRITER_ILL(ill));
9998 
9999 	if (doconsist) {
10000 		/*
10001 		 * Consistency checking requires that I_{P}LINK occurs
10002 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10003 		 * occurs prior to clearing ill_arp_muxid.
10004 		 */
10005 		if ((islink && ill->ill_ip_muxid != 0) ||
10006 		    (!islink && ill->ill_arp_muxid != 0)) {
10007 			err = EINVAL;
10008 			goto done;
10009 		}
10010 	}
10011 
10012 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
10013 		goto done;
10014 
10015 	/*
10016 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10017 	 * the read queue of the module immediately below IP in the ill.
10018 	 * These are used during the capability negotiation below.
10019 	 */
10020 	ill->ill_lmod_rq = NULL;
10021 	ill->ill_lmod_cnt = 0;
10022 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10023 		ill->ill_lmod_rq = RD(dwq);
10024 		for (; dwq != NULL; dwq = dwq->q_next)
10025 			ill->ill_lmod_cnt++;
10026 	}
10027 
10028 	if (doconsist)
10029 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10030 
10031 	/*
10032 	 * Mark the ipsq busy until the capability operations initiated below
10033 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
10034 	 * returns, but the capability operation may complete asynchronously
10035 	 * much later.
10036 	 */
10037 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
10038 	/*
10039 	 * If there's at least one up ipif on this ill, then we're bound to
10040 	 * the underlying driver via DLPI.  In that case, renegotiate
10041 	 * capabilities to account for any possible change in modules
10042 	 * interposed between IP and the driver.
10043 	 */
10044 	if (ill->ill_ipif_up_count > 0) {
10045 		if (islink)
10046 			ill_capability_probe(ill);
10047 		else
10048 			ill_capability_reset(ill, B_FALSE);
10049 	}
10050 	ipsq_current_finish(ipsq);
10051 done:
10052 	if (entered_ipsq)
10053 		ipsq_exit(ipsq);
10054 
10055 	return (err);
10056 }
10057 
10058 /*
10059  * Search the ioctl command in the ioctl tables and return a pointer
10060  * to the ioctl command information. The ioctl command tables are
10061  * static and fully populated at compile time.
10062  */
10063 ip_ioctl_cmd_t *
10064 ip_sioctl_lookup(int ioc_cmd)
10065 {
10066 	int index;
10067 	ip_ioctl_cmd_t *ipip;
10068 	ip_ioctl_cmd_t *ipip_end;
10069 
10070 	if (ioc_cmd == IPI_DONTCARE)
10071 		return (NULL);
10072 
10073 	/*
10074 	 * Do a 2 step search. First search the indexed table
10075 	 * based on the least significant byte of the ioctl cmd.
10076 	 * If we don't find a match, then search the misc table
10077 	 * serially.
10078 	 */
10079 	index = ioc_cmd & 0xFF;
10080 	if (index < ip_ndx_ioctl_count) {
10081 		ipip = &ip_ndx_ioctl_table[index];
10082 		if (ipip->ipi_cmd == ioc_cmd) {
10083 			/* Found a match in the ndx table */
10084 			return (ipip);
10085 		}
10086 	}
10087 
10088 	/* Search the misc table */
10089 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10090 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10091 		if (ipip->ipi_cmd == ioc_cmd)
10092 			/* Found a match in the misc table */
10093 			return (ipip);
10094 	}
10095 
10096 	return (NULL);
10097 }
10098 
10099 /*
10100  * Wrapper function for resuming deferred ioctl processing
10101  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10102  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10103  */
10104 /* ARGSUSED */
10105 void
10106 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10107     void *dummy_arg)
10108 {
10109 	ip_sioctl_copyin_setup(q, mp);
10110 }
10111 
10112 /*
10113  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10114  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10115  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10116  * We establish here the size of the block to be copied in.  mi_copyin
10117  * arranges for this to happen, an processing continues in ip_wput with
10118  * an M_IOCDATA message.
10119  */
10120 void
10121 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10122 {
10123 	int	copyin_size;
10124 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10125 	ip_ioctl_cmd_t *ipip;
10126 	cred_t *cr;
10127 	ip_stack_t	*ipst;
10128 
10129 	if (CONN_Q(q))
10130 		ipst = CONNQ_TO_IPST(q);
10131 	else
10132 		ipst = ILLQ_TO_IPST(q);
10133 
10134 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10135 	if (ipip == NULL) {
10136 		/*
10137 		 * The ioctl is not one we understand or own.
10138 		 * Pass it along to be processed down stream,
10139 		 * if this is a module instance of IP, else nak
10140 		 * the ioctl.
10141 		 */
10142 		if (q->q_next == NULL) {
10143 			goto nak;
10144 		} else {
10145 			putnext(q, mp);
10146 			return;
10147 		}
10148 	}
10149 
10150 	/*
10151 	 * If this is deferred, then we will do all the checks when we
10152 	 * come back.
10153 	 */
10154 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10155 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10156 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10157 		return;
10158 	}
10159 
10160 	/*
10161 	 * Only allow a very small subset of IP ioctls on this stream if
10162 	 * IP is a module and not a driver. Allowing ioctls to be processed
10163 	 * in this case may cause assert failures or data corruption.
10164 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10165 	 * ioctls allowed on an IP module stream, after which this stream
10166 	 * normally becomes a multiplexor (at which time the stream head
10167 	 * will fail all ioctls).
10168 	 */
10169 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10170 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10171 			/*
10172 			 * Pass common Streams ioctls which the IP
10173 			 * module does not own or consume along to
10174 			 * be processed down stream.
10175 			 */
10176 			putnext(q, mp);
10177 			return;
10178 		} else {
10179 			goto nak;
10180 		}
10181 	}
10182 
10183 	/* Make sure we have ioctl data to process. */
10184 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10185 		goto nak;
10186 
10187 	/*
10188 	 * Prefer dblk credential over ioctl credential; some synthesized
10189 	 * ioctls have kcred set because there's no way to crhold()
10190 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10191 	 * the framework; the caller of ioctl needs to hold the reference
10192 	 * for the duration of the call).
10193 	 */
10194 	cr = msg_getcred(mp, NULL);
10195 	if (cr == NULL)
10196 		cr = iocp->ioc_cr;
10197 
10198 	/* Make sure normal users don't send down privileged ioctls */
10199 	if ((ipip->ipi_flags & IPI_PRIV) &&
10200 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10201 		/* We checked the privilege earlier but log it here */
10202 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10203 		return;
10204 	}
10205 
10206 	/*
10207 	 * The ioctl command tables can only encode fixed length
10208 	 * ioctl data. If the length is variable, the table will
10209 	 * encode the length as zero. Such special cases are handled
10210 	 * below in the switch.
10211 	 */
10212 	if (ipip->ipi_copyin_size != 0) {
10213 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10214 		return;
10215 	}
10216 
10217 	switch (iocp->ioc_cmd) {
10218 	case O_SIOCGIFCONF:
10219 	case SIOCGIFCONF:
10220 		/*
10221 		 * This IOCTL is hilarious.  See comments in
10222 		 * ip_sioctl_get_ifconf for the story.
10223 		 */
10224 		if (iocp->ioc_count == TRANSPARENT)
10225 			copyin_size = SIZEOF_STRUCT(ifconf,
10226 			    iocp->ioc_flag);
10227 		else
10228 			copyin_size = iocp->ioc_count;
10229 		mi_copyin(q, mp, NULL, copyin_size);
10230 		return;
10231 
10232 	case O_SIOCGLIFCONF:
10233 	case SIOCGLIFCONF:
10234 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10235 		mi_copyin(q, mp, NULL, copyin_size);
10236 		return;
10237 
10238 	case SIOCGLIFSRCOF:
10239 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10240 		mi_copyin(q, mp, NULL, copyin_size);
10241 		return;
10242 	case SIOCGIP6ADDRPOLICY:
10243 		ip_sioctl_ip6addrpolicy(q, mp);
10244 		ip6_asp_table_refrele(ipst);
10245 		return;
10246 
10247 	case SIOCSIP6ADDRPOLICY:
10248 		ip_sioctl_ip6addrpolicy(q, mp);
10249 		return;
10250 
10251 	case SIOCGDSTINFO:
10252 		ip_sioctl_dstinfo(q, mp);
10253 		ip6_asp_table_refrele(ipst);
10254 		return;
10255 
10256 	case I_PLINK:
10257 	case I_PUNLINK:
10258 	case I_LINK:
10259 	case I_UNLINK:
10260 		/*
10261 		 * We treat non-persistent link similarly as the persistent
10262 		 * link case, in terms of plumbing/unplumbing, as well as
10263 		 * dynamic re-plumbing events indicator.  See comments
10264 		 * in ip_sioctl_plink() for more.
10265 		 *
10266 		 * Request can be enqueued in the 'ipsq' while waiting
10267 		 * to become exclusive. So bump up the conn ref.
10268 		 */
10269 		if (CONN_Q(q))
10270 			CONN_INC_REF(Q_TO_CONN(q));
10271 		ip_sioctl_plink(NULL, q, mp, NULL);
10272 		return;
10273 
10274 	case ND_GET:
10275 	case ND_SET:
10276 		/*
10277 		 * Use of the nd table requires holding the reader lock.
10278 		 * Modifying the nd table thru nd_load/nd_unload requires
10279 		 * the writer lock.
10280 		 */
10281 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10282 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10283 			rw_exit(&ipst->ips_ip_g_nd_lock);
10284 
10285 			if (iocp->ioc_error)
10286 				iocp->ioc_count = 0;
10287 			mp->b_datap->db_type = M_IOCACK;
10288 			qreply(q, mp);
10289 			return;
10290 		}
10291 		rw_exit(&ipst->ips_ip_g_nd_lock);
10292 		/*
10293 		 * We don't understand this subioctl of ND_GET / ND_SET.
10294 		 * Maybe intended for some driver / module below us
10295 		 */
10296 		if (q->q_next) {
10297 			putnext(q, mp);
10298 		} else {
10299 			iocp->ioc_error = ENOENT;
10300 			mp->b_datap->db_type = M_IOCNAK;
10301 			iocp->ioc_count = 0;
10302 			qreply(q, mp);
10303 		}
10304 		return;
10305 
10306 	case IP_IOCTL:
10307 		ip_wput_ioctl(q, mp);
10308 		return;
10309 	default:
10310 		cmn_err(CE_PANIC, "should not happen ");
10311 	}
10312 nak:
10313 	if (mp->b_cont != NULL) {
10314 		freemsg(mp->b_cont);
10315 		mp->b_cont = NULL;
10316 	}
10317 	iocp->ioc_error = EINVAL;
10318 	mp->b_datap->db_type = M_IOCNAK;
10319 	iocp->ioc_count = 0;
10320 	qreply(q, mp);
10321 }
10322 
10323 /* ip_wput hands off ARP IOCTL responses to us */
10324 /* ARGSUSED3 */
10325 void
10326 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10327 {
10328 	struct arpreq *ar;
10329 	struct xarpreq *xar;
10330 	area_t	*area;
10331 	mblk_t	*area_mp;
10332 	struct iocblk *iocp;
10333 	mblk_t	*orig_ioc_mp, *tmp;
10334 	struct iocblk	*orig_iocp;
10335 	ill_t *ill;
10336 	conn_t *connp = NULL;
10337 	mblk_t *pending_mp;
10338 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10339 	int *flagsp;
10340 	char *storage = NULL;
10341 	sin_t *sin;
10342 	ipaddr_t addr;
10343 	int err;
10344 	ip_stack_t *ipst;
10345 
10346 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10347 	ill = q->q_ptr;
10348 	ASSERT(ill != NULL);
10349 	ipst = ill->ill_ipst;
10350 
10351 	/*
10352 	 * We should get back from ARP a packet chain that looks like:
10353 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10354 	 */
10355 	if (!(area_mp = mp->b_cont) ||
10356 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10357 	    !(orig_ioc_mp = area_mp->b_cont) ||
10358 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10359 		freemsg(mp);
10360 		return;
10361 	}
10362 
10363 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10364 
10365 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10366 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10367 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10368 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10369 		x_arp_ioctl = B_TRUE;
10370 		xar = (struct xarpreq *)tmp->b_rptr;
10371 		sin = (sin_t *)&xar->xarp_pa;
10372 		flagsp = &xar->xarp_flags;
10373 		storage = xar->xarp_ha.sdl_data;
10374 		if (xar->xarp_ha.sdl_nlen != 0)
10375 			ifx_arp_ioctl = B_TRUE;
10376 	} else {
10377 		ar = (struct arpreq *)tmp->b_rptr;
10378 		sin = (sin_t *)&ar->arp_pa;
10379 		flagsp = &ar->arp_flags;
10380 		storage = ar->arp_ha.sa_data;
10381 	}
10382 
10383 	iocp = (struct iocblk *)mp->b_rptr;
10384 
10385 	/*
10386 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10387 	 * Otherwise, we can find it from our ioc_id.
10388 	 */
10389 	if (ipsq != NULL)
10390 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10391 	else
10392 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10393 
10394 	if (pending_mp == NULL) {
10395 		ASSERT(connp == NULL);
10396 		inet_freemsg(mp);
10397 		return;
10398 	}
10399 	ASSERT(connp != NULL);
10400 	q = CONNP_TO_WQ(connp);
10401 
10402 	/* Uncouple the internally generated IOCTL from the original one */
10403 	area = (area_t *)area_mp->b_rptr;
10404 	area_mp->b_cont = NULL;
10405 
10406 	/*
10407 	 * Restore the b_next and b_prev used by mi code. This is needed
10408 	 * to complete the ioctl using mi* functions. We stored them in
10409 	 * the pending mp prior to sending the request to ARP.
10410 	 */
10411 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10412 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10413 	inet_freemsg(pending_mp);
10414 
10415 	/*
10416 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10417 	 * Catch the case where there is an IRE_CACHE by no entry in the
10418 	 * arp table.
10419 	 */
10420 	addr = sin->sin_addr.s_addr;
10421 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10422 		ire_t			*ire;
10423 		dl_unitdata_req_t	*dlup;
10424 		mblk_t			*llmp;
10425 		int			addr_len;
10426 		ill_t			*ipsqill = NULL;
10427 
10428 		if (ifx_arp_ioctl) {
10429 			/*
10430 			 * There's no need to lookup the ill, since
10431 			 * we've already done that when we started
10432 			 * processing the ioctl and sent the message
10433 			 * to ARP on that ill.  So use the ill that
10434 			 * is stored in q->q_ptr.
10435 			 */
10436 			ipsqill = ill;
10437 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10438 			    ipsqill->ill_ipif, ALL_ZONES,
10439 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10440 		} else {
10441 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10442 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10443 			if (ire != NULL)
10444 				ipsqill = ire_to_ill(ire);
10445 		}
10446 
10447 		if ((x_arp_ioctl) && (ipsqill != NULL))
10448 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10449 
10450 		if (ire != NULL) {
10451 			/*
10452 			 * Since the ire obtained from cachetable is used for
10453 			 * mac addr copying below, treat an incomplete ire as if
10454 			 * as if we never found it.
10455 			 */
10456 			if (ire->ire_nce != NULL &&
10457 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10458 				ire_refrele(ire);
10459 				ire = NULL;
10460 				ipsqill = NULL;
10461 				goto errack;
10462 			}
10463 			*flagsp = ATF_INUSE;
10464 			llmp = (ire->ire_nce != NULL ?
10465 			    ire->ire_nce->nce_res_mp : NULL);
10466 			if (llmp != NULL && ipsqill != NULL) {
10467 				uchar_t *macaddr;
10468 
10469 				addr_len = ipsqill->ill_phys_addr_length;
10470 				if (x_arp_ioctl && ((addr_len +
10471 				    ipsqill->ill_name_length) >
10472 				    sizeof (xar->xarp_ha.sdl_data))) {
10473 					ire_refrele(ire);
10474 					freemsg(mp);
10475 					ip_ioctl_finish(q, orig_ioc_mp,
10476 					    EINVAL, NO_COPYOUT, ipsq);
10477 					return;
10478 				}
10479 				*flagsp |= ATF_COM;
10480 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10481 				if (ipsqill->ill_sap_length < 0)
10482 					macaddr = llmp->b_rptr +
10483 					    dlup->dl_dest_addr_offset;
10484 				else
10485 					macaddr = llmp->b_rptr +
10486 					    dlup->dl_dest_addr_offset +
10487 					    ipsqill->ill_sap_length;
10488 				/*
10489 				 * For SIOCGARP, MAC address length
10490 				 * validation has already been done
10491 				 * before the ioctl was issued to ARP to
10492 				 * allow it to progress only on 6 byte
10493 				 * addressable (ethernet like) media. Thus
10494 				 * the mac address copying can not overwrite
10495 				 * the sa_data area below.
10496 				 */
10497 				bcopy(macaddr, storage, addr_len);
10498 			}
10499 			/* Ditch the internal IOCTL. */
10500 			freemsg(mp);
10501 			ire_refrele(ire);
10502 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10503 			return;
10504 		}
10505 	}
10506 
10507 	/*
10508 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10509 	 * on the IPMP meta-interface, ensure any ARP entries added in
10510 	 * ip_sioctl_arp() are deleted.
10511 	 */
10512 	if (IS_IPMP(ill) &&
10513 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10514 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10515 		ipmp_illgrp_t *illg = ill->ill_grp;
10516 		ipmp_arpent_t *entp;
10517 
10518 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10519 			ipmp_illgrp_destroy_arpent(illg, entp);
10520 	}
10521 
10522 	/*
10523 	 * Delete the coresponding IRE_CACHE if any.
10524 	 * Reset the error if there was one (in case there was no entry
10525 	 * in arp.)
10526 	 */
10527 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10528 		ipif_t *ipintf = NULL;
10529 
10530 		if (ifx_arp_ioctl) {
10531 			/*
10532 			 * There's no need to lookup the ill, since
10533 			 * we've already done that when we started
10534 			 * processing the ioctl and sent the message
10535 			 * to ARP on that ill.  So use the ill that
10536 			 * is stored in q->q_ptr.
10537 			 */
10538 			ipintf = ill->ill_ipif;
10539 		}
10540 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10541 			/*
10542 			 * The address in "addr" may be an entry for a
10543 			 * router. If that's true, then any off-net
10544 			 * IRE_CACHE entries that go through the router
10545 			 * with address "addr" must be clobbered. Use
10546 			 * ire_walk to achieve this goal.
10547 			 */
10548 			if (ifx_arp_ioctl)
10549 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10550 				    ire_delete_cache_gw, (char *)&addr, ill);
10551 			else
10552 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10553 				    ALL_ZONES, ipst);
10554 			iocp->ioc_error = 0;
10555 		}
10556 	}
10557 errack:
10558 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10559 		err = iocp->ioc_error;
10560 		freemsg(mp);
10561 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10562 		return;
10563 	}
10564 
10565 	/*
10566 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10567 	 * the area_t into the struct {x}arpreq.
10568 	 */
10569 	if (x_arp_ioctl) {
10570 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10571 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10572 		    sizeof (xar->xarp_ha.sdl_data)) {
10573 			freemsg(mp);
10574 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10575 			    ipsq);
10576 			return;
10577 		}
10578 	}
10579 	*flagsp = ATF_INUSE;
10580 	if (area->area_flags & ACE_F_PERMANENT)
10581 		*flagsp |= ATF_PERM;
10582 	if (area->area_flags & ACE_F_PUBLISH)
10583 		*flagsp |= ATF_PUBL;
10584 	if (area->area_flags & ACE_F_AUTHORITY)
10585 		*flagsp |= ATF_AUTHORITY;
10586 	if (area->area_hw_addr_length != 0) {
10587 		*flagsp |= ATF_COM;
10588 		/*
10589 		 * For SIOCGARP, MAC address length validation has
10590 		 * already been done before the ioctl was issued to ARP
10591 		 * to allow it to progress only on 6 byte addressable
10592 		 * (ethernet like) media. Thus the mac address copying
10593 		 * can not overwrite the sa_data area below.
10594 		 */
10595 		bcopy((char *)area + area->area_hw_addr_offset,
10596 		    storage, area->area_hw_addr_length);
10597 	}
10598 
10599 	/* Ditch the internal IOCTL. */
10600 	freemsg(mp);
10601 	/* Complete the original. */
10602 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10603 }
10604 
10605 /*
10606  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10607  * interface) create the next available logical interface for this
10608  * physical interface.
10609  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10610  * ipif with the specified name.
10611  *
10612  * If the address family is not AF_UNSPEC then set the address as well.
10613  *
10614  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10615  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10616  *
10617  * Executed as a writer on the ill.
10618  * So no lock is needed to traverse the ipif chain, or examine the
10619  * phyint flags.
10620  */
10621 /* ARGSUSED */
10622 int
10623 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10624     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10625 {
10626 	mblk_t	*mp1;
10627 	struct lifreq *lifr;
10628 	boolean_t	isv6;
10629 	boolean_t	exists;
10630 	char 	*name;
10631 	char	*endp;
10632 	char	*cp;
10633 	int	namelen;
10634 	ipif_t	*ipif;
10635 	long	id;
10636 	ipsq_t	*ipsq;
10637 	ill_t	*ill;
10638 	sin_t	*sin;
10639 	int	err = 0;
10640 	boolean_t found_sep = B_FALSE;
10641 	conn_t	*connp;
10642 	zoneid_t zoneid;
10643 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10644 
10645 	ASSERT(q->q_next == NULL);
10646 	ip1dbg(("ip_sioctl_addif\n"));
10647 	/* Existence of mp1 has been checked in ip_wput_nondata */
10648 	mp1 = mp->b_cont->b_cont;
10649 	/*
10650 	 * Null terminate the string to protect against buffer
10651 	 * overrun. String was generated by user code and may not
10652 	 * be trusted.
10653 	 */
10654 	lifr = (struct lifreq *)mp1->b_rptr;
10655 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10656 	name = lifr->lifr_name;
10657 	ASSERT(CONN_Q(q));
10658 	connp = Q_TO_CONN(q);
10659 	isv6 = connp->conn_af_isv6;
10660 	zoneid = connp->conn_zoneid;
10661 	namelen = mi_strlen(name);
10662 	if (namelen == 0)
10663 		return (EINVAL);
10664 
10665 	exists = B_FALSE;
10666 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10667 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10668 		/*
10669 		 * Allow creating lo0 using SIOCLIFADDIF.
10670 		 * can't be any other writer thread. So can pass null below
10671 		 * for the last 4 args to ipif_lookup_name.
10672 		 */
10673 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10674 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10675 		/* Prevent any further action */
10676 		if (ipif == NULL) {
10677 			return (ENOBUFS);
10678 		} else if (!exists) {
10679 			/* We created the ipif now and as writer */
10680 			ipif_refrele(ipif);
10681 			return (0);
10682 		} else {
10683 			ill = ipif->ipif_ill;
10684 			ill_refhold(ill);
10685 			ipif_refrele(ipif);
10686 		}
10687 	} else {
10688 		/* Look for a colon in the name. */
10689 		endp = &name[namelen];
10690 		for (cp = endp; --cp > name; ) {
10691 			if (*cp == IPIF_SEPARATOR_CHAR) {
10692 				found_sep = B_TRUE;
10693 				/*
10694 				 * Reject any non-decimal aliases for plumbing
10695 				 * of logical interfaces. Aliases with leading
10696 				 * zeroes are also rejected as they introduce
10697 				 * ambiguity in the naming of the interfaces.
10698 				 * Comparing with "0" takes care of all such
10699 				 * cases.
10700 				 */
10701 				if ((strncmp("0", cp+1, 1)) == 0)
10702 					return (EINVAL);
10703 
10704 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10705 				    id <= 0 || *endp != '\0') {
10706 					return (EINVAL);
10707 				}
10708 				*cp = '\0';
10709 				break;
10710 			}
10711 		}
10712 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10713 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10714 		if (found_sep)
10715 			*cp = IPIF_SEPARATOR_CHAR;
10716 		if (ill == NULL)
10717 			return (err);
10718 	}
10719 
10720 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10721 	    B_TRUE);
10722 
10723 	/*
10724 	 * Release the refhold due to the lookup, now that we are excl
10725 	 * or we are just returning
10726 	 */
10727 	ill_refrele(ill);
10728 
10729 	if (ipsq == NULL)
10730 		return (EINPROGRESS);
10731 
10732 	/* We are now exclusive on the IPSQ */
10733 	ASSERT(IAM_WRITER_ILL(ill));
10734 
10735 	if (found_sep) {
10736 		/* Now see if there is an IPIF with this unit number. */
10737 		for (ipif = ill->ill_ipif; ipif != NULL;
10738 		    ipif = ipif->ipif_next) {
10739 			if (ipif->ipif_id == id) {
10740 				err = EEXIST;
10741 				goto done;
10742 			}
10743 		}
10744 	}
10745 
10746 	/*
10747 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10748 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10749 	 * instead.
10750 	 */
10751 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10752 	    B_TRUE, B_TRUE)) == NULL) {
10753 		err = ENOBUFS;
10754 		goto done;
10755 	}
10756 
10757 	/* Return created name with ioctl */
10758 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10759 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10760 	ip1dbg(("created %s\n", lifr->lifr_name));
10761 
10762 	/* Set address */
10763 	sin = (sin_t *)&lifr->lifr_addr;
10764 	if (sin->sin_family != AF_UNSPEC) {
10765 		err = ip_sioctl_addr(ipif, sin, q, mp,
10766 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10767 	}
10768 
10769 done:
10770 	ipsq_exit(ipsq);
10771 	return (err);
10772 }
10773 
10774 /*
10775  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10776  * interface) delete it based on the IP address (on this physical interface).
10777  * Otherwise delete it based on the ipif_id.
10778  * Also, special handling to allow a removeif of lo0.
10779  */
10780 /* ARGSUSED */
10781 int
10782 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10783     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10784 {
10785 	conn_t		*connp;
10786 	ill_t		*ill = ipif->ipif_ill;
10787 	boolean_t	 success;
10788 	ip_stack_t	*ipst;
10789 
10790 	ipst = CONNQ_TO_IPST(q);
10791 
10792 	ASSERT(q->q_next == NULL);
10793 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10794 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10795 	ASSERT(IAM_WRITER_IPIF(ipif));
10796 
10797 	connp = Q_TO_CONN(q);
10798 	/*
10799 	 * Special case for unplumbing lo0 (the loopback physical interface).
10800 	 * If unplumbing lo0, the incoming address structure has been
10801 	 * initialized to all zeros. When unplumbing lo0, all its logical
10802 	 * interfaces must be removed too.
10803 	 *
10804 	 * Note that this interface may be called to remove a specific
10805 	 * loopback logical interface (eg, lo0:1). But in that case
10806 	 * ipif->ipif_id != 0 so that the code path for that case is the
10807 	 * same as any other interface (meaning it skips the code directly
10808 	 * below).
10809 	 */
10810 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10811 		if (sin->sin_family == AF_UNSPEC &&
10812 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10813 			/*
10814 			 * Mark it condemned. No new ref. will be made to ill.
10815 			 */
10816 			mutex_enter(&ill->ill_lock);
10817 			ill->ill_state_flags |= ILL_CONDEMNED;
10818 			for (ipif = ill->ill_ipif; ipif != NULL;
10819 			    ipif = ipif->ipif_next) {
10820 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10821 			}
10822 			mutex_exit(&ill->ill_lock);
10823 
10824 			ipif = ill->ill_ipif;
10825 			/* unplumb the loopback interface */
10826 			ill_delete(ill);
10827 			mutex_enter(&connp->conn_lock);
10828 			mutex_enter(&ill->ill_lock);
10829 
10830 			/* Are any references to this ill active */
10831 			if (ill_is_freeable(ill)) {
10832 				mutex_exit(&ill->ill_lock);
10833 				mutex_exit(&connp->conn_lock);
10834 				ill_delete_tail(ill);
10835 				mi_free(ill);
10836 				return (0);
10837 			}
10838 			success = ipsq_pending_mp_add(connp, ipif,
10839 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10840 			mutex_exit(&connp->conn_lock);
10841 			mutex_exit(&ill->ill_lock);
10842 			if (success)
10843 				return (EINPROGRESS);
10844 			else
10845 				return (EINTR);
10846 		}
10847 	}
10848 
10849 	if (ipif->ipif_id == 0) {
10850 		ipsq_t *ipsq;
10851 
10852 		/* Find based on address */
10853 		if (ipif->ipif_isv6) {
10854 			sin6_t *sin6;
10855 
10856 			if (sin->sin_family != AF_INET6)
10857 				return (EAFNOSUPPORT);
10858 
10859 			sin6 = (sin6_t *)sin;
10860 			/* We are a writer, so we should be able to lookup */
10861 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10862 			    ipst);
10863 		} else {
10864 			if (sin->sin_family != AF_INET)
10865 				return (EAFNOSUPPORT);
10866 
10867 			/* We are a writer, so we should be able to lookup */
10868 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10869 			    ipst);
10870 		}
10871 		if (ipif == NULL) {
10872 			return (EADDRNOTAVAIL);
10873 		}
10874 
10875 		/*
10876 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10877 		 * lifr_name of the physical interface but with an ip address
10878 		 * lifr_addr of a logical interface plumbed over it.
10879 		 * So update ipx_current_ipif now that ipif points to the
10880 		 * correct one.
10881 		 */
10882 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10883 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10884 
10885 		/* This is a writer */
10886 		ipif_refrele(ipif);
10887 	}
10888 
10889 	/*
10890 	 * Can not delete instance zero since it is tied to the ill.
10891 	 */
10892 	if (ipif->ipif_id == 0)
10893 		return (EBUSY);
10894 
10895 	mutex_enter(&ill->ill_lock);
10896 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10897 	mutex_exit(&ill->ill_lock);
10898 
10899 	ipif_free(ipif);
10900 
10901 	mutex_enter(&connp->conn_lock);
10902 	mutex_enter(&ill->ill_lock);
10903 
10904 	/* Are any references to this ipif active */
10905 	if (ipif_is_freeable(ipif)) {
10906 		mutex_exit(&ill->ill_lock);
10907 		mutex_exit(&connp->conn_lock);
10908 		ipif_non_duplicate(ipif);
10909 		ipif_down_tail(ipif);
10910 		ipif_free_tail(ipif); /* frees ipif */
10911 		return (0);
10912 	}
10913 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10914 	    IPIF_FREE);
10915 	mutex_exit(&ill->ill_lock);
10916 	mutex_exit(&connp->conn_lock);
10917 	if (success)
10918 		return (EINPROGRESS);
10919 	else
10920 		return (EINTR);
10921 }
10922 
10923 /*
10924  * Restart the removeif ioctl. The refcnt has gone down to 0.
10925  * The ipif is already condemned. So can't find it thru lookups.
10926  */
10927 /* ARGSUSED */
10928 int
10929 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10930     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10931 {
10932 	ill_t *ill = ipif->ipif_ill;
10933 
10934 	ASSERT(IAM_WRITER_IPIF(ipif));
10935 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10936 
10937 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10938 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10939 
10940 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10941 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10942 		ill_delete_tail(ill);
10943 		mi_free(ill);
10944 		return (0);
10945 	}
10946 
10947 	ipif_non_duplicate(ipif);
10948 	ipif_down_tail(ipif);
10949 	ipif_free_tail(ipif);
10950 
10951 	ILL_UNMARK_CHANGING(ill);
10952 	return (0);
10953 }
10954 
10955 /*
10956  * Set the local interface address.
10957  * Allow an address of all zero when the interface is down.
10958  */
10959 /* ARGSUSED */
10960 int
10961 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10962     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10963 {
10964 	int err = 0;
10965 	in6_addr_t v6addr;
10966 	boolean_t need_up = B_FALSE;
10967 
10968 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10969 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10970 
10971 	ASSERT(IAM_WRITER_IPIF(ipif));
10972 
10973 	if (ipif->ipif_isv6) {
10974 		sin6_t *sin6;
10975 		ill_t *ill;
10976 		phyint_t *phyi;
10977 
10978 		if (sin->sin_family != AF_INET6)
10979 			return (EAFNOSUPPORT);
10980 
10981 		sin6 = (sin6_t *)sin;
10982 		v6addr = sin6->sin6_addr;
10983 		ill = ipif->ipif_ill;
10984 		phyi = ill->ill_phyint;
10985 
10986 		/*
10987 		 * Enforce that true multicast interfaces have a link-local
10988 		 * address for logical unit 0.
10989 		 */
10990 		if (ipif->ipif_id == 0 &&
10991 		    (ill->ill_flags & ILLF_MULTICAST) &&
10992 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10993 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10994 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10995 			return (EADDRNOTAVAIL);
10996 		}
10997 
10998 		/*
10999 		 * up interfaces shouldn't have the unspecified address
11000 		 * unless they also have the IPIF_NOLOCAL flags set and
11001 		 * have a subnet assigned.
11002 		 */
11003 		if ((ipif->ipif_flags & IPIF_UP) &&
11004 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11005 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11006 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11007 			return (EADDRNOTAVAIL);
11008 		}
11009 
11010 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11011 			return (EADDRNOTAVAIL);
11012 	} else {
11013 		ipaddr_t addr;
11014 
11015 		if (sin->sin_family != AF_INET)
11016 			return (EAFNOSUPPORT);
11017 
11018 		addr = sin->sin_addr.s_addr;
11019 
11020 		/* Allow 0 as the local address. */
11021 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11022 			return (EADDRNOTAVAIL);
11023 
11024 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11025 	}
11026 
11027 	/*
11028 	 * Even if there is no change we redo things just to rerun
11029 	 * ipif_set_default.
11030 	 */
11031 	if (ipif->ipif_flags & IPIF_UP) {
11032 		/*
11033 		 * Setting a new local address, make sure
11034 		 * we have net and subnet bcast ire's for
11035 		 * the old address if we need them.
11036 		 */
11037 		if (!ipif->ipif_isv6)
11038 			ipif_check_bcast_ires(ipif);
11039 		/*
11040 		 * If the interface is already marked up,
11041 		 * we call ipif_down which will take care
11042 		 * of ditching any IREs that have been set
11043 		 * up based on the old interface address.
11044 		 */
11045 		err = ipif_logical_down(ipif, q, mp);
11046 		if (err == EINPROGRESS)
11047 			return (err);
11048 		ipif_down_tail(ipif);
11049 		need_up = 1;
11050 	}
11051 
11052 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11053 	return (err);
11054 }
11055 
11056 int
11057 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11058     boolean_t need_up)
11059 {
11060 	in6_addr_t v6addr;
11061 	in6_addr_t ov6addr;
11062 	ipaddr_t addr;
11063 	sin6_t	*sin6;
11064 	int	sinlen;
11065 	int	err = 0;
11066 	ill_t	*ill = ipif->ipif_ill;
11067 	boolean_t need_dl_down;
11068 	boolean_t need_arp_down;
11069 	struct iocblk *iocp;
11070 
11071 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11072 
11073 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11074 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11075 	ASSERT(IAM_WRITER_IPIF(ipif));
11076 
11077 	/* Must cancel any pending timer before taking the ill_lock */
11078 	if (ipif->ipif_recovery_id != 0)
11079 		(void) untimeout(ipif->ipif_recovery_id);
11080 	ipif->ipif_recovery_id = 0;
11081 
11082 	if (ipif->ipif_isv6) {
11083 		sin6 = (sin6_t *)sin;
11084 		v6addr = sin6->sin6_addr;
11085 		sinlen = sizeof (struct sockaddr_in6);
11086 	} else {
11087 		addr = sin->sin_addr.s_addr;
11088 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11089 		sinlen = sizeof (struct sockaddr_in);
11090 	}
11091 	mutex_enter(&ill->ill_lock);
11092 	ov6addr = ipif->ipif_v6lcl_addr;
11093 	ipif->ipif_v6lcl_addr = v6addr;
11094 	sctp_update_ipif_addr(ipif, ov6addr);
11095 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11096 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11097 	} else {
11098 		ipif->ipif_v6src_addr = v6addr;
11099 	}
11100 	ipif->ipif_addr_ready = 0;
11101 
11102 	/*
11103 	 * If the interface was previously marked as a duplicate, then since
11104 	 * we've now got a "new" address, it should no longer be considered a
11105 	 * duplicate -- even if the "new" address is the same as the old one.
11106 	 * Note that if all ipifs are down, we may have a pending ARP down
11107 	 * event to handle.  This is because we want to recover from duplicates
11108 	 * and thus delay tearing down ARP until the duplicates have been
11109 	 * removed or disabled.
11110 	 */
11111 	need_dl_down = need_arp_down = B_FALSE;
11112 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11113 		need_arp_down = !need_up;
11114 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11115 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11116 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11117 			need_dl_down = B_TRUE;
11118 		}
11119 	}
11120 
11121 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11122 	    !ill->ill_is_6to4tun) {
11123 		queue_t *wqp = ill->ill_wq;
11124 
11125 		/*
11126 		 * The local address of this interface is a 6to4 address,
11127 		 * check if this interface is in fact a 6to4 tunnel or just
11128 		 * an interface configured with a 6to4 address.  We are only
11129 		 * interested in the former.
11130 		 */
11131 		if (wqp != NULL) {
11132 			while ((wqp->q_next != NULL) &&
11133 			    (wqp->q_next->q_qinfo != NULL) &&
11134 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11135 
11136 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11137 				    == TUN6TO4_MODID) {
11138 					/* set for use in IP */
11139 					ill->ill_is_6to4tun = 1;
11140 					break;
11141 				}
11142 				wqp = wqp->q_next;
11143 			}
11144 		}
11145 	}
11146 
11147 	ipif_set_default(ipif);
11148 
11149 	/*
11150 	 * When publishing an interface address change event, we only notify
11151 	 * the event listeners of the new address.  It is assumed that if they
11152 	 * actively care about the addresses assigned that they will have
11153 	 * already discovered the previous address assigned (if there was one.)
11154 	 *
11155 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11156 	 */
11157 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11158 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11159 		    NE_ADDRESS_CHANGE, sin, sinlen);
11160 	}
11161 
11162 	mutex_exit(&ill->ill_lock);
11163 
11164 	if (need_up) {
11165 		/*
11166 		 * Now bring the interface back up.  If this
11167 		 * is the only IPIF for the ILL, ipif_up
11168 		 * will have to re-bind to the device, so
11169 		 * we may get back EINPROGRESS, in which
11170 		 * case, this IOCTL will get completed in
11171 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11172 		 */
11173 		err = ipif_up(ipif, q, mp);
11174 	}
11175 
11176 	if (need_dl_down)
11177 		ill_dl_down(ill);
11178 	if (need_arp_down)
11179 		ipif_resolver_down(ipif);
11180 
11181 	return (err);
11182 }
11183 
11184 /*
11185  * Restart entry point to restart the address set operation after the
11186  * refcounts have dropped to zero.
11187  */
11188 /* ARGSUSED */
11189 int
11190 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11191     ip_ioctl_cmd_t *ipip, void *ifreq)
11192 {
11193 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11194 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11195 	ASSERT(IAM_WRITER_IPIF(ipif));
11196 	ipif_down_tail(ipif);
11197 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11198 }
11199 
11200 /* ARGSUSED */
11201 int
11202 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11203     ip_ioctl_cmd_t *ipip, void *if_req)
11204 {
11205 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11206 	struct lifreq *lifr = (struct lifreq *)if_req;
11207 
11208 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11209 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11210 	/*
11211 	 * The net mask and address can't change since we have a
11212 	 * reference to the ipif. So no lock is necessary.
11213 	 */
11214 	if (ipif->ipif_isv6) {
11215 		*sin6 = sin6_null;
11216 		sin6->sin6_family = AF_INET6;
11217 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11218 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11219 		lifr->lifr_addrlen =
11220 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11221 	} else {
11222 		*sin = sin_null;
11223 		sin->sin_family = AF_INET;
11224 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11225 		if (ipip->ipi_cmd_type == LIF_CMD) {
11226 			lifr->lifr_addrlen =
11227 			    ip_mask_to_plen(ipif->ipif_net_mask);
11228 		}
11229 	}
11230 	return (0);
11231 }
11232 
11233 /*
11234  * Set the destination address for a pt-pt interface.
11235  */
11236 /* ARGSUSED */
11237 int
11238 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11239     ip_ioctl_cmd_t *ipip, void *if_req)
11240 {
11241 	int err = 0;
11242 	in6_addr_t v6addr;
11243 	boolean_t need_up = B_FALSE;
11244 
11245 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11246 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11247 	ASSERT(IAM_WRITER_IPIF(ipif));
11248 
11249 	if (ipif->ipif_isv6) {
11250 		sin6_t *sin6;
11251 
11252 		if (sin->sin_family != AF_INET6)
11253 			return (EAFNOSUPPORT);
11254 
11255 		sin6 = (sin6_t *)sin;
11256 		v6addr = sin6->sin6_addr;
11257 
11258 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11259 			return (EADDRNOTAVAIL);
11260 	} else {
11261 		ipaddr_t addr;
11262 
11263 		if (sin->sin_family != AF_INET)
11264 			return (EAFNOSUPPORT);
11265 
11266 		addr = sin->sin_addr.s_addr;
11267 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11268 			return (EADDRNOTAVAIL);
11269 
11270 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11271 	}
11272 
11273 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11274 		return (0);	/* No change */
11275 
11276 	if (ipif->ipif_flags & IPIF_UP) {
11277 		/*
11278 		 * If the interface is already marked up,
11279 		 * we call ipif_down which will take care
11280 		 * of ditching any IREs that have been set
11281 		 * up based on the old pp dst address.
11282 		 */
11283 		err = ipif_logical_down(ipif, q, mp);
11284 		if (err == EINPROGRESS)
11285 			return (err);
11286 		ipif_down_tail(ipif);
11287 		need_up = B_TRUE;
11288 	}
11289 	/*
11290 	 * could return EINPROGRESS. If so ioctl will complete in
11291 	 * ip_rput_dlpi_writer
11292 	 */
11293 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11294 	return (err);
11295 }
11296 
11297 static int
11298 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11299     boolean_t need_up)
11300 {
11301 	in6_addr_t v6addr;
11302 	ill_t	*ill = ipif->ipif_ill;
11303 	int	err = 0;
11304 	boolean_t need_dl_down;
11305 	boolean_t need_arp_down;
11306 
11307 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11308 	    ipif->ipif_id, (void *)ipif));
11309 
11310 	/* Must cancel any pending timer before taking the ill_lock */
11311 	if (ipif->ipif_recovery_id != 0)
11312 		(void) untimeout(ipif->ipif_recovery_id);
11313 	ipif->ipif_recovery_id = 0;
11314 
11315 	if (ipif->ipif_isv6) {
11316 		sin6_t *sin6;
11317 
11318 		sin6 = (sin6_t *)sin;
11319 		v6addr = sin6->sin6_addr;
11320 	} else {
11321 		ipaddr_t addr;
11322 
11323 		addr = sin->sin_addr.s_addr;
11324 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11325 	}
11326 	mutex_enter(&ill->ill_lock);
11327 	/* Set point to point destination address. */
11328 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11329 		/*
11330 		 * Allow this as a means of creating logical
11331 		 * pt-pt interfaces on top of e.g. an Ethernet.
11332 		 * XXX Undocumented HACK for testing.
11333 		 * pt-pt interfaces are created with NUD disabled.
11334 		 */
11335 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11336 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11337 		if (ipif->ipif_isv6)
11338 			ill->ill_flags |= ILLF_NONUD;
11339 	}
11340 
11341 	/*
11342 	 * If the interface was previously marked as a duplicate, then since
11343 	 * we've now got a "new" address, it should no longer be considered a
11344 	 * duplicate -- even if the "new" address is the same as the old one.
11345 	 * Note that if all ipifs are down, we may have a pending ARP down
11346 	 * event to handle.
11347 	 */
11348 	need_dl_down = need_arp_down = B_FALSE;
11349 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11350 		need_arp_down = !need_up;
11351 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11352 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11353 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11354 			need_dl_down = B_TRUE;
11355 		}
11356 	}
11357 
11358 	/* Set the new address. */
11359 	ipif->ipif_v6pp_dst_addr = v6addr;
11360 	/* Make sure subnet tracks pp_dst */
11361 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11362 	mutex_exit(&ill->ill_lock);
11363 
11364 	if (need_up) {
11365 		/*
11366 		 * Now bring the interface back up.  If this
11367 		 * is the only IPIF for the ILL, ipif_up
11368 		 * will have to re-bind to the device, so
11369 		 * we may get back EINPROGRESS, in which
11370 		 * case, this IOCTL will get completed in
11371 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11372 		 */
11373 		err = ipif_up(ipif, q, mp);
11374 	}
11375 
11376 	if (need_dl_down)
11377 		ill_dl_down(ill);
11378 	if (need_arp_down)
11379 		ipif_resolver_down(ipif);
11380 
11381 	return (err);
11382 }
11383 
11384 /*
11385  * Restart entry point to restart the dstaddress set operation after the
11386  * refcounts have dropped to zero.
11387  */
11388 /* ARGSUSED */
11389 int
11390 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11391     ip_ioctl_cmd_t *ipip, void *ifreq)
11392 {
11393 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11394 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11395 	ipif_down_tail(ipif);
11396 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11397 }
11398 
11399 /* ARGSUSED */
11400 int
11401 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11402     ip_ioctl_cmd_t *ipip, void *if_req)
11403 {
11404 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11405 
11406 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11407 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11408 	/*
11409 	 * Get point to point destination address. The addresses can't
11410 	 * change since we hold a reference to the ipif.
11411 	 */
11412 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11413 		return (EADDRNOTAVAIL);
11414 
11415 	if (ipif->ipif_isv6) {
11416 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11417 		*sin6 = sin6_null;
11418 		sin6->sin6_family = AF_INET6;
11419 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11420 	} else {
11421 		*sin = sin_null;
11422 		sin->sin_family = AF_INET;
11423 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11424 	}
11425 	return (0);
11426 }
11427 
11428 /*
11429  * Set interface flags.  Many flags require special handling (e.g.,
11430  * bringing the interface down); see below for details.
11431  *
11432  * NOTE : We really don't enforce that ipif_id zero should be used
11433  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11434  *	  is because applications generally does SICGLIFFLAGS and
11435  *	  ORs in the new flags (that affects the logical) and does a
11436  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11437  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11438  *	  flags that will be turned on is correct with respect to
11439  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11440  */
11441 /* ARGSUSED */
11442 int
11443 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11444     ip_ioctl_cmd_t *ipip, void *if_req)
11445 {
11446 	uint64_t turn_on;
11447 	uint64_t turn_off;
11448 	int	err = 0;
11449 	phyint_t *phyi;
11450 	ill_t *ill;
11451 	uint64_t intf_flags, cantchange_flags;
11452 	boolean_t phyint_flags_modified = B_FALSE;
11453 	uint64_t flags;
11454 	struct ifreq *ifr;
11455 	struct lifreq *lifr;
11456 	boolean_t set_linklocal = B_FALSE;
11457 	boolean_t zero_source = B_FALSE;
11458 
11459 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11460 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11461 
11462 	ASSERT(IAM_WRITER_IPIF(ipif));
11463 
11464 	ill = ipif->ipif_ill;
11465 	phyi = ill->ill_phyint;
11466 
11467 	if (ipip->ipi_cmd_type == IF_CMD) {
11468 		ifr = (struct ifreq *)if_req;
11469 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11470 	} else {
11471 		lifr = (struct lifreq *)if_req;
11472 		flags = lifr->lifr_flags;
11473 	}
11474 
11475 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11476 
11477 	/*
11478 	 * Have the flags been set correctly until now?
11479 	 */
11480 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11481 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11482 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11483 	/*
11484 	 * Compare the new flags to the old, and partition
11485 	 * into those coming on and those going off.
11486 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11487 	 */
11488 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11489 		flags |= intf_flags & ~0xFFFF;
11490 
11491 	/*
11492 	 * Explicitly fail attempts to change flags that are always invalid on
11493 	 * an IPMP meta-interface.
11494 	 */
11495 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11496 		return (EINVAL);
11497 
11498 	/*
11499 	 * Check which flags will change; silently ignore flags which userland
11500 	 * is not allowed to control.  (Because these flags may change between
11501 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11502 	 * control, we need to silently ignore them rather than fail.)
11503 	 */
11504 	cantchange_flags = IFF_CANTCHANGE;
11505 	if (IS_IPMP(ill))
11506 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11507 
11508 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11509 	if (turn_on == 0)
11510 		return (0);	/* No change */
11511 
11512 	turn_off = intf_flags & turn_on;
11513 	turn_on ^= turn_off;
11514 
11515 	/*
11516 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11517 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11518 	 * allow it to be turned off.
11519 	 */
11520 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11521 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11522 		return (EINVAL);
11523 
11524 	if (turn_on & IFF_NOFAILOVER) {
11525 		turn_on |= IFF_DEPRECATED;
11526 		flags |= IFF_DEPRECATED;
11527 	}
11528 
11529 	/*
11530 	 * On underlying interfaces, only allow applications to manage test
11531 	 * addresses -- otherwise, they may get confused when the address
11532 	 * moves as part of being brought up.  Likewise, prevent an
11533 	 * application-managed test address from being converted to a data
11534 	 * address.  To prevent migration of administratively up addresses in
11535 	 * the kernel, we don't allow them to be converted either.
11536 	 */
11537 	if (IS_UNDER_IPMP(ill)) {
11538 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11539 
11540 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11541 			return (EINVAL);
11542 
11543 		if ((turn_off & IFF_NOFAILOVER) &&
11544 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11545 			return (EINVAL);
11546 	}
11547 
11548 	/*
11549 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11550 	 * IPv6 interfaces.
11551 	 */
11552 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11553 		return (EINVAL);
11554 
11555 	/*
11556 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11557 	 */
11558 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11559 		return (EINVAL);
11560 
11561 	/*
11562 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11563 	 * interfaces.  It makes no sense in that context.
11564 	 */
11565 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11566 		return (EINVAL);
11567 
11568 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11569 		zero_source = B_TRUE;
11570 
11571 	/*
11572 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11573 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11574 	 * If the link local address isn't set, and can be set, it will get
11575 	 * set later on in this function.
11576 	 */
11577 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11578 	    (flags & IFF_UP) && !zero_source &&
11579 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11580 		if (ipif_cant_setlinklocal(ipif))
11581 			return (EINVAL);
11582 		set_linklocal = B_TRUE;
11583 	}
11584 
11585 	/*
11586 	 * If we modify physical interface flags, we'll potentially need to
11587 	 * send up two routing socket messages for the changes (one for the
11588 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11589 	 */
11590 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11591 		phyint_flags_modified = B_TRUE;
11592 
11593 	/*
11594 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11595 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11596 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11597 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11598 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11599 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11600 	 * will not be honored.
11601 	 */
11602 	if (turn_on & PHYI_STANDBY) {
11603 		/*
11604 		 * No need to grab ill_g_usesrc_lock here; see the
11605 		 * synchronization notes in ip.c.
11606 		 */
11607 		if (ill->ill_usesrc_grp_next != NULL ||
11608 		    intf_flags & PHYI_INACTIVE)
11609 			return (EINVAL);
11610 		if (!(flags & PHYI_FAILED)) {
11611 			flags |= PHYI_INACTIVE;
11612 			turn_on |= PHYI_INACTIVE;
11613 		}
11614 	}
11615 
11616 	if (turn_off & PHYI_STANDBY) {
11617 		flags &= ~PHYI_INACTIVE;
11618 		turn_off |= PHYI_INACTIVE;
11619 	}
11620 
11621 	/*
11622 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11623 	 * would end up on.
11624 	 */
11625 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11626 	    (PHYI_FAILED | PHYI_INACTIVE))
11627 		return (EINVAL);
11628 
11629 	/*
11630 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11631 	 * status of the interface.
11632 	 */
11633 	if ((turn_on | turn_off) & ILLF_ROUTER)
11634 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11635 
11636 	/*
11637 	 * If the interface is not UP and we are not going to
11638 	 * bring it UP, record the flags and return. When the
11639 	 * interface comes UP later, the right actions will be
11640 	 * taken.
11641 	 */
11642 	if (!(ipif->ipif_flags & IPIF_UP) &&
11643 	    !(turn_on & IPIF_UP)) {
11644 		/* Record new flags in their respective places. */
11645 		mutex_enter(&ill->ill_lock);
11646 		mutex_enter(&ill->ill_phyint->phyint_lock);
11647 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11648 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11649 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11650 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11651 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11652 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11653 		mutex_exit(&ill->ill_lock);
11654 		mutex_exit(&ill->ill_phyint->phyint_lock);
11655 
11656 		/*
11657 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11658 		 * same to the kernel: if any of them has been set by
11659 		 * userland, the interface cannot be used for data traffic.
11660 		 */
11661 		if ((turn_on|turn_off) &
11662 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11663 			ASSERT(!IS_IPMP(ill));
11664 			/*
11665 			 * It's possible the ill is part of an "anonymous"
11666 			 * IPMP group rather than a real group.  In that case,
11667 			 * there are no other interfaces in the group and thus
11668 			 * no need to call ipmp_phyint_refresh_active().
11669 			 */
11670 			if (IS_UNDER_IPMP(ill))
11671 				ipmp_phyint_refresh_active(phyi);
11672 		}
11673 
11674 		if (phyint_flags_modified) {
11675 			if (phyi->phyint_illv4 != NULL) {
11676 				ip_rts_ifmsg(phyi->phyint_illv4->
11677 				    ill_ipif, RTSQ_DEFAULT);
11678 			}
11679 			if (phyi->phyint_illv6 != NULL) {
11680 				ip_rts_ifmsg(phyi->phyint_illv6->
11681 				    ill_ipif, RTSQ_DEFAULT);
11682 			}
11683 		}
11684 		return (0);
11685 	} else if (set_linklocal || zero_source) {
11686 		mutex_enter(&ill->ill_lock);
11687 		if (set_linklocal)
11688 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11689 		if (zero_source)
11690 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11691 		mutex_exit(&ill->ill_lock);
11692 	}
11693 
11694 	/*
11695 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11696 	 * or point-to-point interfaces with an unspecified destination. We do
11697 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11698 	 * have a subnet assigned, which is how in.ndpd currently manages its
11699 	 * onlink prefix list when no addresses are configured with those
11700 	 * prefixes.
11701 	 */
11702 	if (ipif->ipif_isv6 &&
11703 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11704 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11705 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11706 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11707 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11708 		return (EINVAL);
11709 	}
11710 
11711 	/*
11712 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11713 	 * from being brought up.
11714 	 */
11715 	if (!ipif->ipif_isv6 &&
11716 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11717 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11718 		return (EINVAL);
11719 	}
11720 
11721 	/*
11722 	 * The only flag changes that we currently take specific action on are
11723 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11724 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11725 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11726 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11727 	 * of bringing it back up will trigger the address to be moved.
11728 	 */
11729 	if ((turn_on|turn_off) &
11730 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11731 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11732 	    IPIF_NOFAILOVER)) {
11733 		/*
11734 		 * Taking this ipif down, make sure we have
11735 		 * valid net and subnet bcast ire's for other
11736 		 * logical interfaces, if we need them.
11737 		 */
11738 		if (!ipif->ipif_isv6)
11739 			ipif_check_bcast_ires(ipif);
11740 
11741 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11742 		    !(turn_off & IPIF_UP)) {
11743 			if (ipif->ipif_flags & IPIF_UP)
11744 				ill->ill_logical_down = 1;
11745 			turn_on &= ~IPIF_UP;
11746 		}
11747 		err = ipif_down(ipif, q, mp);
11748 		ip1dbg(("ipif_down returns %d err ", err));
11749 		if (err == EINPROGRESS)
11750 			return (err);
11751 		ipif_down_tail(ipif);
11752 	}
11753 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11754 }
11755 
11756 static int
11757 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11758 {
11759 	ill_t	*ill;
11760 	phyint_t *phyi;
11761 	uint64_t turn_on, turn_off;
11762 	uint64_t intf_flags, cantchange_flags;
11763 	boolean_t phyint_flags_modified = B_FALSE;
11764 	int	err = 0;
11765 	boolean_t set_linklocal = B_FALSE;
11766 	boolean_t zero_source = B_FALSE;
11767 
11768 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11769 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11770 
11771 	ASSERT(IAM_WRITER_IPIF(ipif));
11772 
11773 	ill = ipif->ipif_ill;
11774 	phyi = ill->ill_phyint;
11775 
11776 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11777 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11778 	if (IS_IPMP(ill))
11779 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11780 
11781 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11782 	turn_off = intf_flags & turn_on;
11783 	turn_on ^= turn_off;
11784 
11785 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11786 		phyint_flags_modified = B_TRUE;
11787 
11788 	/*
11789 	 * Now we change the flags. Track current value of
11790 	 * other flags in their respective places.
11791 	 */
11792 	mutex_enter(&ill->ill_lock);
11793 	mutex_enter(&phyi->phyint_lock);
11794 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11795 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11796 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11797 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11798 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11799 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11800 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11801 		set_linklocal = B_TRUE;
11802 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11803 	}
11804 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11805 		zero_source = B_TRUE;
11806 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11807 	}
11808 	mutex_exit(&ill->ill_lock);
11809 	mutex_exit(&phyi->phyint_lock);
11810 
11811 	if (set_linklocal)
11812 		(void) ipif_setlinklocal(ipif);
11813 
11814 	if (zero_source)
11815 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11816 	else
11817 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11818 
11819 	/*
11820 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11821 	 * the kernel: if any of them has been set by userland, the interface
11822 	 * cannot be used for data traffic.
11823 	 */
11824 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11825 		ASSERT(!IS_IPMP(ill));
11826 		/*
11827 		 * It's possible the ill is part of an "anonymous" IPMP group
11828 		 * rather than a real group.  In that case, there are no other
11829 		 * interfaces in the group and thus no need for us to call
11830 		 * ipmp_phyint_refresh_active().
11831 		 */
11832 		if (IS_UNDER_IPMP(ill))
11833 			ipmp_phyint_refresh_active(phyi);
11834 	}
11835 
11836 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11837 		/*
11838 		 * XXX ipif_up really does not know whether a phyint flags
11839 		 * was modified or not. So, it sends up information on
11840 		 * only one routing sockets message. As we don't bring up
11841 		 * the interface and also set PHYI_ flags simultaneously
11842 		 * it should be okay.
11843 		 */
11844 		err = ipif_up(ipif, q, mp);
11845 	} else {
11846 		/*
11847 		 * Make sure routing socket sees all changes to the flags.
11848 		 * ipif_up_done* handles this when we use ipif_up.
11849 		 */
11850 		if (phyint_flags_modified) {
11851 			if (phyi->phyint_illv4 != NULL) {
11852 				ip_rts_ifmsg(phyi->phyint_illv4->
11853 				    ill_ipif, RTSQ_DEFAULT);
11854 			}
11855 			if (phyi->phyint_illv6 != NULL) {
11856 				ip_rts_ifmsg(phyi->phyint_illv6->
11857 				    ill_ipif, RTSQ_DEFAULT);
11858 			}
11859 		} else {
11860 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11861 		}
11862 		/*
11863 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11864 		 * this in need_up case.
11865 		 */
11866 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11867 	}
11868 	return (err);
11869 }
11870 
11871 /*
11872  * Restart the flags operation now that the refcounts have dropped to zero.
11873  */
11874 /* ARGSUSED */
11875 int
11876 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11877     ip_ioctl_cmd_t *ipip, void *if_req)
11878 {
11879 	uint64_t flags;
11880 	struct ifreq *ifr = if_req;
11881 	struct lifreq *lifr = if_req;
11882 
11883 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11884 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11885 
11886 	ipif_down_tail(ipif);
11887 	if (ipip->ipi_cmd_type == IF_CMD) {
11888 		/* cast to uint16_t prevents unwanted sign extension */
11889 		flags = (uint16_t)ifr->ifr_flags;
11890 	} else {
11891 		flags = lifr->lifr_flags;
11892 	}
11893 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11894 }
11895 
11896 /*
11897  * Can operate on either a module or a driver queue.
11898  */
11899 /* ARGSUSED */
11900 int
11901 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11902     ip_ioctl_cmd_t *ipip, void *if_req)
11903 {
11904 	/*
11905 	 * Has the flags been set correctly till now ?
11906 	 */
11907 	ill_t *ill = ipif->ipif_ill;
11908 	phyint_t *phyi = ill->ill_phyint;
11909 
11910 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11911 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11912 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11913 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11914 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11915 
11916 	/*
11917 	 * Need a lock since some flags can be set even when there are
11918 	 * references to the ipif.
11919 	 */
11920 	mutex_enter(&ill->ill_lock);
11921 	if (ipip->ipi_cmd_type == IF_CMD) {
11922 		struct ifreq *ifr = (struct ifreq *)if_req;
11923 
11924 		/* Get interface flags (low 16 only). */
11925 		ifr->ifr_flags = ((ipif->ipif_flags |
11926 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11927 	} else {
11928 		struct lifreq *lifr = (struct lifreq *)if_req;
11929 
11930 		/* Get interface flags. */
11931 		lifr->lifr_flags = ipif->ipif_flags |
11932 		    ill->ill_flags | phyi->phyint_flags;
11933 	}
11934 	mutex_exit(&ill->ill_lock);
11935 	return (0);
11936 }
11937 
11938 /* ARGSUSED */
11939 int
11940 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11941     ip_ioctl_cmd_t *ipip, void *if_req)
11942 {
11943 	int mtu;
11944 	int ip_min_mtu;
11945 	struct ifreq	*ifr;
11946 	struct lifreq *lifr;
11947 	ire_t	*ire;
11948 	ip_stack_t *ipst;
11949 
11950 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11951 	    ipif->ipif_id, (void *)ipif));
11952 	if (ipip->ipi_cmd_type == IF_CMD) {
11953 		ifr = (struct ifreq *)if_req;
11954 		mtu = ifr->ifr_metric;
11955 	} else {
11956 		lifr = (struct lifreq *)if_req;
11957 		mtu = lifr->lifr_mtu;
11958 	}
11959 
11960 	if (ipif->ipif_isv6)
11961 		ip_min_mtu = IPV6_MIN_MTU;
11962 	else
11963 		ip_min_mtu = IP_MIN_MTU;
11964 
11965 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11966 		return (EINVAL);
11967 
11968 	/*
11969 	 * Change the MTU size in all relevant ire's.
11970 	 * Mtu change Vs. new ire creation - protocol below.
11971 	 * First change ipif_mtu and the ire_max_frag of the
11972 	 * interface ire. Then do an ire walk and change the
11973 	 * ire_max_frag of all affected ires. During ire_add
11974 	 * under the bucket lock, set the ire_max_frag of the
11975 	 * new ire being created from the ipif/ire from which
11976 	 * it is being derived. If an mtu change happens after
11977 	 * the ire is added, the new ire will be cleaned up.
11978 	 * Conversely if the mtu change happens before the ire
11979 	 * is added, ire_add will see the new value of the mtu.
11980 	 */
11981 	ipif->ipif_mtu = mtu;
11982 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11983 
11984 	if (ipif->ipif_isv6)
11985 		ire = ipif_to_ire_v6(ipif);
11986 	else
11987 		ire = ipif_to_ire(ipif);
11988 	if (ire != NULL) {
11989 		ire->ire_max_frag = ipif->ipif_mtu;
11990 		ire_refrele(ire);
11991 	}
11992 	ipst = ipif->ipif_ill->ill_ipst;
11993 	if (ipif->ipif_flags & IPIF_UP) {
11994 		if (ipif->ipif_isv6)
11995 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11996 			    ipst);
11997 		else
11998 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11999 			    ipst);
12000 	}
12001 	/* Update the MTU in SCTP's list */
12002 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12003 	return (0);
12004 }
12005 
12006 /* Get interface MTU. */
12007 /* ARGSUSED */
12008 int
12009 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12010 	ip_ioctl_cmd_t *ipip, void *if_req)
12011 {
12012 	struct ifreq	*ifr;
12013 	struct lifreq	*lifr;
12014 
12015 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12016 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12017 	if (ipip->ipi_cmd_type == IF_CMD) {
12018 		ifr = (struct ifreq *)if_req;
12019 		ifr->ifr_metric = ipif->ipif_mtu;
12020 	} else {
12021 		lifr = (struct lifreq *)if_req;
12022 		lifr->lifr_mtu = ipif->ipif_mtu;
12023 	}
12024 	return (0);
12025 }
12026 
12027 /* Set interface broadcast address. */
12028 /* ARGSUSED2 */
12029 int
12030 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12031 	ip_ioctl_cmd_t *ipip, void *if_req)
12032 {
12033 	ipaddr_t addr;
12034 	ire_t	*ire;
12035 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12036 
12037 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12038 	    ipif->ipif_id));
12039 
12040 	ASSERT(IAM_WRITER_IPIF(ipif));
12041 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12042 		return (EADDRNOTAVAIL);
12043 
12044 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12045 
12046 	if (sin->sin_family != AF_INET)
12047 		return (EAFNOSUPPORT);
12048 
12049 	addr = sin->sin_addr.s_addr;
12050 	if (ipif->ipif_flags & IPIF_UP) {
12051 		/*
12052 		 * If we are already up, make sure the new
12053 		 * broadcast address makes sense.  If it does,
12054 		 * there should be an IRE for it already.
12055 		 * Don't match on ipif, only on the ill
12056 		 * since we are sharing these now.
12057 		 */
12058 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12059 		    ipif, ALL_ZONES, NULL,
12060 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12061 		if (ire == NULL) {
12062 			return (EINVAL);
12063 		} else {
12064 			ire_refrele(ire);
12065 		}
12066 	}
12067 	/*
12068 	 * Changing the broadcast addr for this ipif.
12069 	 * Make sure we have valid net and subnet bcast
12070 	 * ire's for other logical interfaces, if needed.
12071 	 */
12072 	if (addr != ipif->ipif_brd_addr)
12073 		ipif_check_bcast_ires(ipif);
12074 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12075 	return (0);
12076 }
12077 
12078 /* Get interface broadcast address. */
12079 /* ARGSUSED */
12080 int
12081 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12082     ip_ioctl_cmd_t *ipip, void *if_req)
12083 {
12084 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12085 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12086 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12087 		return (EADDRNOTAVAIL);
12088 
12089 	/* IPIF_BROADCAST not possible with IPv6 */
12090 	ASSERT(!ipif->ipif_isv6);
12091 	*sin = sin_null;
12092 	sin->sin_family = AF_INET;
12093 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12094 	return (0);
12095 }
12096 
12097 /*
12098  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12099  */
12100 /* ARGSUSED */
12101 int
12102 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12103     ip_ioctl_cmd_t *ipip, void *if_req)
12104 {
12105 	int err = 0;
12106 	in6_addr_t v6mask;
12107 
12108 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12109 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12110 
12111 	ASSERT(IAM_WRITER_IPIF(ipif));
12112 
12113 	if (ipif->ipif_isv6) {
12114 		sin6_t *sin6;
12115 
12116 		if (sin->sin_family != AF_INET6)
12117 			return (EAFNOSUPPORT);
12118 
12119 		sin6 = (sin6_t *)sin;
12120 		v6mask = sin6->sin6_addr;
12121 	} else {
12122 		ipaddr_t mask;
12123 
12124 		if (sin->sin_family != AF_INET)
12125 			return (EAFNOSUPPORT);
12126 
12127 		mask = sin->sin_addr.s_addr;
12128 		V4MASK_TO_V6(mask, v6mask);
12129 	}
12130 
12131 	/*
12132 	 * No big deal if the interface isn't already up, or the mask
12133 	 * isn't really changing, or this is pt-pt.
12134 	 */
12135 	if (!(ipif->ipif_flags & IPIF_UP) ||
12136 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12137 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12138 		ipif->ipif_v6net_mask = v6mask;
12139 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12140 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12141 			    ipif->ipif_v6net_mask,
12142 			    ipif->ipif_v6subnet);
12143 		}
12144 		return (0);
12145 	}
12146 	/*
12147 	 * Make sure we have valid net and subnet broadcast ire's
12148 	 * for the old netmask, if needed by other logical interfaces.
12149 	 */
12150 	if (!ipif->ipif_isv6)
12151 		ipif_check_bcast_ires(ipif);
12152 
12153 	err = ipif_logical_down(ipif, q, mp);
12154 	if (err == EINPROGRESS)
12155 		return (err);
12156 	ipif_down_tail(ipif);
12157 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12158 	return (err);
12159 }
12160 
12161 static int
12162 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12163 {
12164 	in6_addr_t v6mask;
12165 	int err = 0;
12166 
12167 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12168 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12169 
12170 	if (ipif->ipif_isv6) {
12171 		sin6_t *sin6;
12172 
12173 		sin6 = (sin6_t *)sin;
12174 		v6mask = sin6->sin6_addr;
12175 	} else {
12176 		ipaddr_t mask;
12177 
12178 		mask = sin->sin_addr.s_addr;
12179 		V4MASK_TO_V6(mask, v6mask);
12180 	}
12181 
12182 	ipif->ipif_v6net_mask = v6mask;
12183 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12184 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12185 		    ipif->ipif_v6subnet);
12186 	}
12187 	err = ipif_up(ipif, q, mp);
12188 
12189 	if (err == 0 || err == EINPROGRESS) {
12190 		/*
12191 		 * The interface must be DL_BOUND if this packet has to
12192 		 * go out on the wire. Since we only go through a logical
12193 		 * down and are bound with the driver during an internal
12194 		 * down/up that is satisfied.
12195 		 */
12196 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12197 			/* Potentially broadcast an address mask reply. */
12198 			ipif_mask_reply(ipif);
12199 		}
12200 	}
12201 	return (err);
12202 }
12203 
12204 /* ARGSUSED */
12205 int
12206 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12207     ip_ioctl_cmd_t *ipip, void *if_req)
12208 {
12209 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12210 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12211 	ipif_down_tail(ipif);
12212 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12213 }
12214 
12215 /* Get interface net mask. */
12216 /* ARGSUSED */
12217 int
12218 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12219     ip_ioctl_cmd_t *ipip, void *if_req)
12220 {
12221 	struct lifreq *lifr = (struct lifreq *)if_req;
12222 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12223 
12224 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12225 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12226 
12227 	/*
12228 	 * net mask can't change since we have a reference to the ipif.
12229 	 */
12230 	if (ipif->ipif_isv6) {
12231 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12232 		*sin6 = sin6_null;
12233 		sin6->sin6_family = AF_INET6;
12234 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12235 		lifr->lifr_addrlen =
12236 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12237 	} else {
12238 		*sin = sin_null;
12239 		sin->sin_family = AF_INET;
12240 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12241 		if (ipip->ipi_cmd_type == LIF_CMD) {
12242 			lifr->lifr_addrlen =
12243 			    ip_mask_to_plen(ipif->ipif_net_mask);
12244 		}
12245 	}
12246 	return (0);
12247 }
12248 
12249 /* ARGSUSED */
12250 int
12251 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12252     ip_ioctl_cmd_t *ipip, void *if_req)
12253 {
12254 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12255 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12256 
12257 	/*
12258 	 * Since no applications should ever be setting metrics on underlying
12259 	 * interfaces, we explicitly fail to smoke 'em out.
12260 	 */
12261 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12262 		return (EINVAL);
12263 
12264 	/*
12265 	 * Set interface metric.  We don't use this for
12266 	 * anything but we keep track of it in case it is
12267 	 * important to routing applications or such.
12268 	 */
12269 	if (ipip->ipi_cmd_type == IF_CMD) {
12270 		struct ifreq    *ifr;
12271 
12272 		ifr = (struct ifreq *)if_req;
12273 		ipif->ipif_metric = ifr->ifr_metric;
12274 	} else {
12275 		struct lifreq   *lifr;
12276 
12277 		lifr = (struct lifreq *)if_req;
12278 		ipif->ipif_metric = lifr->lifr_metric;
12279 	}
12280 	return (0);
12281 }
12282 
12283 /* ARGSUSED */
12284 int
12285 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12286     ip_ioctl_cmd_t *ipip, void *if_req)
12287 {
12288 	/* Get interface metric. */
12289 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12290 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12291 
12292 	if (ipip->ipi_cmd_type == IF_CMD) {
12293 		struct ifreq    *ifr;
12294 
12295 		ifr = (struct ifreq *)if_req;
12296 		ifr->ifr_metric = ipif->ipif_metric;
12297 	} else {
12298 		struct lifreq   *lifr;
12299 
12300 		lifr = (struct lifreq *)if_req;
12301 		lifr->lifr_metric = ipif->ipif_metric;
12302 	}
12303 
12304 	return (0);
12305 }
12306 
12307 /* ARGSUSED */
12308 int
12309 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12310     ip_ioctl_cmd_t *ipip, void *if_req)
12311 {
12312 
12313 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12314 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12315 	/*
12316 	 * Set the muxid returned from I_PLINK.
12317 	 */
12318 	if (ipip->ipi_cmd_type == IF_CMD) {
12319 		struct ifreq *ifr = (struct ifreq *)if_req;
12320 
12321 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12322 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12323 	} else {
12324 		struct lifreq *lifr = (struct lifreq *)if_req;
12325 
12326 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12327 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12328 	}
12329 	return (0);
12330 }
12331 
12332 /* ARGSUSED */
12333 int
12334 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12335     ip_ioctl_cmd_t *ipip, void *if_req)
12336 {
12337 
12338 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12339 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12340 	/*
12341 	 * Get the muxid saved in ill for I_PUNLINK.
12342 	 */
12343 	if (ipip->ipi_cmd_type == IF_CMD) {
12344 		struct ifreq *ifr = (struct ifreq *)if_req;
12345 
12346 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12347 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12348 	} else {
12349 		struct lifreq *lifr = (struct lifreq *)if_req;
12350 
12351 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12352 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12353 	}
12354 	return (0);
12355 }
12356 
12357 /*
12358  * Set the subnet prefix. Does not modify the broadcast address.
12359  */
12360 /* ARGSUSED */
12361 int
12362 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12363     ip_ioctl_cmd_t *ipip, void *if_req)
12364 {
12365 	int err = 0;
12366 	in6_addr_t v6addr;
12367 	in6_addr_t v6mask;
12368 	boolean_t need_up = B_FALSE;
12369 	int addrlen;
12370 
12371 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12372 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12373 
12374 	ASSERT(IAM_WRITER_IPIF(ipif));
12375 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12376 
12377 	if (ipif->ipif_isv6) {
12378 		sin6_t *sin6;
12379 
12380 		if (sin->sin_family != AF_INET6)
12381 			return (EAFNOSUPPORT);
12382 
12383 		sin6 = (sin6_t *)sin;
12384 		v6addr = sin6->sin6_addr;
12385 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12386 			return (EADDRNOTAVAIL);
12387 	} else {
12388 		ipaddr_t addr;
12389 
12390 		if (sin->sin_family != AF_INET)
12391 			return (EAFNOSUPPORT);
12392 
12393 		addr = sin->sin_addr.s_addr;
12394 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12395 			return (EADDRNOTAVAIL);
12396 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12397 		/* Add 96 bits */
12398 		addrlen += IPV6_ABITS - IP_ABITS;
12399 	}
12400 
12401 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12402 		return (EINVAL);
12403 
12404 	/* Check if bits in the address is set past the mask */
12405 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12406 		return (EINVAL);
12407 
12408 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12409 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12410 		return (0);	/* No change */
12411 
12412 	if (ipif->ipif_flags & IPIF_UP) {
12413 		/*
12414 		 * If the interface is already marked up,
12415 		 * we call ipif_down which will take care
12416 		 * of ditching any IREs that have been set
12417 		 * up based on the old interface address.
12418 		 */
12419 		err = ipif_logical_down(ipif, q, mp);
12420 		if (err == EINPROGRESS)
12421 			return (err);
12422 		ipif_down_tail(ipif);
12423 		need_up = B_TRUE;
12424 	}
12425 
12426 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12427 	return (err);
12428 }
12429 
12430 static int
12431 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12432     queue_t *q, mblk_t *mp, boolean_t need_up)
12433 {
12434 	ill_t	*ill = ipif->ipif_ill;
12435 	int	err = 0;
12436 
12437 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12438 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12439 
12440 	/* Set the new address. */
12441 	mutex_enter(&ill->ill_lock);
12442 	ipif->ipif_v6net_mask = v6mask;
12443 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12444 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12445 		    ipif->ipif_v6subnet);
12446 	}
12447 	mutex_exit(&ill->ill_lock);
12448 
12449 	if (need_up) {
12450 		/*
12451 		 * Now bring the interface back up.  If this
12452 		 * is the only IPIF for the ILL, ipif_up
12453 		 * will have to re-bind to the device, so
12454 		 * we may get back EINPROGRESS, in which
12455 		 * case, this IOCTL will get completed in
12456 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12457 		 */
12458 		err = ipif_up(ipif, q, mp);
12459 		if (err == EINPROGRESS)
12460 			return (err);
12461 	}
12462 	return (err);
12463 }
12464 
12465 /* ARGSUSED */
12466 int
12467 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12468     ip_ioctl_cmd_t *ipip, void *if_req)
12469 {
12470 	int	addrlen;
12471 	in6_addr_t v6addr;
12472 	in6_addr_t v6mask;
12473 	struct lifreq *lifr = (struct lifreq *)if_req;
12474 
12475 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12476 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12477 	ipif_down_tail(ipif);
12478 
12479 	addrlen = lifr->lifr_addrlen;
12480 	if (ipif->ipif_isv6) {
12481 		sin6_t *sin6;
12482 
12483 		sin6 = (sin6_t *)sin;
12484 		v6addr = sin6->sin6_addr;
12485 	} else {
12486 		ipaddr_t addr;
12487 
12488 		addr = sin->sin_addr.s_addr;
12489 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12490 		addrlen += IPV6_ABITS - IP_ABITS;
12491 	}
12492 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12493 
12494 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12495 }
12496 
12497 /* ARGSUSED */
12498 int
12499 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12500     ip_ioctl_cmd_t *ipip, void *if_req)
12501 {
12502 	struct lifreq *lifr = (struct lifreq *)if_req;
12503 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12504 
12505 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12506 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12507 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12508 
12509 	if (ipif->ipif_isv6) {
12510 		*sin6 = sin6_null;
12511 		sin6->sin6_family = AF_INET6;
12512 		sin6->sin6_addr = ipif->ipif_v6subnet;
12513 		lifr->lifr_addrlen =
12514 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12515 	} else {
12516 		*sin = sin_null;
12517 		sin->sin_family = AF_INET;
12518 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12519 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12520 	}
12521 	return (0);
12522 }
12523 
12524 /*
12525  * Set the IPv6 address token.
12526  */
12527 /* ARGSUSED */
12528 int
12529 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12530     ip_ioctl_cmd_t *ipi, void *if_req)
12531 {
12532 	ill_t *ill = ipif->ipif_ill;
12533 	int err;
12534 	in6_addr_t v6addr;
12535 	in6_addr_t v6mask;
12536 	boolean_t need_up = B_FALSE;
12537 	int i;
12538 	sin6_t *sin6 = (sin6_t *)sin;
12539 	struct lifreq *lifr = (struct lifreq *)if_req;
12540 	int addrlen;
12541 
12542 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12543 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12544 	ASSERT(IAM_WRITER_IPIF(ipif));
12545 
12546 	addrlen = lifr->lifr_addrlen;
12547 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12548 	if (ipif->ipif_id != 0)
12549 		return (EINVAL);
12550 
12551 	if (!ipif->ipif_isv6)
12552 		return (EINVAL);
12553 
12554 	if (addrlen > IPV6_ABITS)
12555 		return (EINVAL);
12556 
12557 	v6addr = sin6->sin6_addr;
12558 
12559 	/*
12560 	 * The length of the token is the length from the end.  To get
12561 	 * the proper mask for this, compute the mask of the bits not
12562 	 * in the token; ie. the prefix, and then xor to get the mask.
12563 	 */
12564 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12565 		return (EINVAL);
12566 	for (i = 0; i < 4; i++) {
12567 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12568 	}
12569 
12570 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12571 	    ill->ill_token_length == addrlen)
12572 		return (0);	/* No change */
12573 
12574 	if (ipif->ipif_flags & IPIF_UP) {
12575 		err = ipif_logical_down(ipif, q, mp);
12576 		if (err == EINPROGRESS)
12577 			return (err);
12578 		ipif_down_tail(ipif);
12579 		need_up = B_TRUE;
12580 	}
12581 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12582 	return (err);
12583 }
12584 
12585 static int
12586 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12587     mblk_t *mp, boolean_t need_up)
12588 {
12589 	in6_addr_t v6addr;
12590 	in6_addr_t v6mask;
12591 	ill_t	*ill = ipif->ipif_ill;
12592 	int	i;
12593 	int	err = 0;
12594 
12595 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12596 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12597 	v6addr = sin6->sin6_addr;
12598 	/*
12599 	 * The length of the token is the length from the end.  To get
12600 	 * the proper mask for this, compute the mask of the bits not
12601 	 * in the token; ie. the prefix, and then xor to get the mask.
12602 	 */
12603 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12604 	for (i = 0; i < 4; i++)
12605 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12606 
12607 	mutex_enter(&ill->ill_lock);
12608 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12609 	ill->ill_token_length = addrlen;
12610 	mutex_exit(&ill->ill_lock);
12611 
12612 	if (need_up) {
12613 		/*
12614 		 * Now bring the interface back up.  If this
12615 		 * is the only IPIF for the ILL, ipif_up
12616 		 * will have to re-bind to the device, so
12617 		 * we may get back EINPROGRESS, in which
12618 		 * case, this IOCTL will get completed in
12619 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12620 		 */
12621 		err = ipif_up(ipif, q, mp);
12622 		if (err == EINPROGRESS)
12623 			return (err);
12624 	}
12625 	return (err);
12626 }
12627 
12628 /* ARGSUSED */
12629 int
12630 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12631     ip_ioctl_cmd_t *ipi, void *if_req)
12632 {
12633 	ill_t *ill;
12634 	sin6_t *sin6 = (sin6_t *)sin;
12635 	struct lifreq *lifr = (struct lifreq *)if_req;
12636 
12637 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12638 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12639 	if (ipif->ipif_id != 0)
12640 		return (EINVAL);
12641 
12642 	ill = ipif->ipif_ill;
12643 	if (!ill->ill_isv6)
12644 		return (ENXIO);
12645 
12646 	*sin6 = sin6_null;
12647 	sin6->sin6_family = AF_INET6;
12648 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12649 	sin6->sin6_addr = ill->ill_token;
12650 	lifr->lifr_addrlen = ill->ill_token_length;
12651 	return (0);
12652 }
12653 
12654 /*
12655  * Set (hardware) link specific information that might override
12656  * what was acquired through the DL_INFO_ACK.
12657  * The logic is as follows.
12658  *
12659  * become exclusive
12660  * set CHANGING flag
12661  * change mtu on affected IREs
12662  * clear CHANGING flag
12663  *
12664  * An ire add that occurs before the CHANGING flag is set will have its mtu
12665  * changed by the ip_sioctl_lnkinfo.
12666  *
12667  * During the time the CHANGING flag is set, no new ires will be added to the
12668  * bucket, and ire add will fail (due the CHANGING flag).
12669  *
12670  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12671  * before it is added to the bucket.
12672  *
12673  * Obviously only 1 thread can set the CHANGING flag and we need to become
12674  * exclusive to set the flag.
12675  */
12676 /* ARGSUSED */
12677 int
12678 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12679     ip_ioctl_cmd_t *ipi, void *if_req)
12680 {
12681 	ill_t		*ill = ipif->ipif_ill;
12682 	ipif_t		*nipif;
12683 	int		ip_min_mtu;
12684 	boolean_t	mtu_walk = B_FALSE;
12685 	struct lifreq	*lifr = (struct lifreq *)if_req;
12686 	lif_ifinfo_req_t *lir;
12687 	ire_t		*ire;
12688 
12689 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12690 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12691 	lir = &lifr->lifr_ifinfo;
12692 	ASSERT(IAM_WRITER_IPIF(ipif));
12693 
12694 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12695 	if (ipif->ipif_id != 0)
12696 		return (EINVAL);
12697 
12698 	/* Set interface MTU. */
12699 	if (ipif->ipif_isv6)
12700 		ip_min_mtu = IPV6_MIN_MTU;
12701 	else
12702 		ip_min_mtu = IP_MIN_MTU;
12703 
12704 	/*
12705 	 * Verify values before we set anything. Allow zero to
12706 	 * mean unspecified.
12707 	 */
12708 	if (lir->lir_maxmtu != 0 &&
12709 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12710 	    lir->lir_maxmtu < ip_min_mtu))
12711 		return (EINVAL);
12712 	if (lir->lir_reachtime != 0 &&
12713 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12714 		return (EINVAL);
12715 	if (lir->lir_reachretrans != 0 &&
12716 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12717 		return (EINVAL);
12718 
12719 	mutex_enter(&ill->ill_lock);
12720 	ill->ill_state_flags |= ILL_CHANGING;
12721 	for (nipif = ill->ill_ipif; nipif != NULL;
12722 	    nipif = nipif->ipif_next) {
12723 		nipif->ipif_state_flags |= IPIF_CHANGING;
12724 	}
12725 
12726 	if (lir->lir_maxmtu != 0) {
12727 		ill->ill_max_mtu = lir->lir_maxmtu;
12728 		ill->ill_user_mtu = lir->lir_maxmtu;
12729 		mtu_walk = B_TRUE;
12730 	}
12731 	mutex_exit(&ill->ill_lock);
12732 
12733 	if (lir->lir_reachtime != 0)
12734 		ill->ill_reachable_time = lir->lir_reachtime;
12735 
12736 	if (lir->lir_reachretrans != 0)
12737 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12738 
12739 	ill->ill_max_hops = lir->lir_maxhops;
12740 
12741 	ill->ill_max_buf = ND_MAX_Q;
12742 
12743 	if (mtu_walk) {
12744 		/*
12745 		 * Set the MTU on all ipifs associated with this ill except
12746 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12747 		 */
12748 		for (nipif = ill->ill_ipif; nipif != NULL;
12749 		    nipif = nipif->ipif_next) {
12750 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12751 				continue;
12752 
12753 			nipif->ipif_mtu = ill->ill_max_mtu;
12754 
12755 			if (!(nipif->ipif_flags & IPIF_UP))
12756 				continue;
12757 
12758 			if (nipif->ipif_isv6)
12759 				ire = ipif_to_ire_v6(nipif);
12760 			else
12761 				ire = ipif_to_ire(nipif);
12762 			if (ire != NULL) {
12763 				ire->ire_max_frag = ipif->ipif_mtu;
12764 				ire_refrele(ire);
12765 			}
12766 
12767 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12768 			    nipif, ill);
12769 		}
12770 	}
12771 
12772 	mutex_enter(&ill->ill_lock);
12773 	for (nipif = ill->ill_ipif; nipif != NULL;
12774 	    nipif = nipif->ipif_next) {
12775 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12776 	}
12777 	ILL_UNMARK_CHANGING(ill);
12778 	mutex_exit(&ill->ill_lock);
12779 
12780 	/*
12781 	 * Refresh IPMP meta-interface MTU if necessary.
12782 	 */
12783 	if (IS_UNDER_IPMP(ill))
12784 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12785 
12786 	return (0);
12787 }
12788 
12789 /* ARGSUSED */
12790 int
12791 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12792     ip_ioctl_cmd_t *ipi, void *if_req)
12793 {
12794 	struct lif_ifinfo_req *lir;
12795 	ill_t *ill = ipif->ipif_ill;
12796 
12797 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12798 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12799 	if (ipif->ipif_id != 0)
12800 		return (EINVAL);
12801 
12802 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12803 	lir->lir_maxhops = ill->ill_max_hops;
12804 	lir->lir_reachtime = ill->ill_reachable_time;
12805 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12806 	lir->lir_maxmtu = ill->ill_max_mtu;
12807 
12808 	return (0);
12809 }
12810 
12811 /*
12812  * Return best guess as to the subnet mask for the specified address.
12813  * Based on the subnet masks for all the configured interfaces.
12814  *
12815  * We end up returning a zero mask in the case of default, multicast or
12816  * experimental.
12817  */
12818 static ipaddr_t
12819 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12820 {
12821 	ipaddr_t net_mask;
12822 	ill_t	*ill;
12823 	ipif_t	*ipif;
12824 	ill_walk_context_t ctx;
12825 	ipif_t	*fallback_ipif = NULL;
12826 
12827 	net_mask = ip_net_mask(addr);
12828 	if (net_mask == 0) {
12829 		*ipifp = NULL;
12830 		return (0);
12831 	}
12832 
12833 	/* Let's check to see if this is maybe a local subnet route. */
12834 	/* this function only applies to IPv4 interfaces */
12835 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12836 	ill = ILL_START_WALK_V4(&ctx, ipst);
12837 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12838 		mutex_enter(&ill->ill_lock);
12839 		for (ipif = ill->ill_ipif; ipif != NULL;
12840 		    ipif = ipif->ipif_next) {
12841 			if (!IPIF_CAN_LOOKUP(ipif))
12842 				continue;
12843 			if (!(ipif->ipif_flags & IPIF_UP))
12844 				continue;
12845 			if ((ipif->ipif_subnet & net_mask) ==
12846 			    (addr & net_mask)) {
12847 				/*
12848 				 * Don't trust pt-pt interfaces if there are
12849 				 * other interfaces.
12850 				 */
12851 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12852 					if (fallback_ipif == NULL) {
12853 						ipif_refhold_locked(ipif);
12854 						fallback_ipif = ipif;
12855 					}
12856 					continue;
12857 				}
12858 
12859 				/*
12860 				 * Fine. Just assume the same net mask as the
12861 				 * directly attached subnet interface is using.
12862 				 */
12863 				ipif_refhold_locked(ipif);
12864 				mutex_exit(&ill->ill_lock);
12865 				rw_exit(&ipst->ips_ill_g_lock);
12866 				if (fallback_ipif != NULL)
12867 					ipif_refrele(fallback_ipif);
12868 				*ipifp = ipif;
12869 				return (ipif->ipif_net_mask);
12870 			}
12871 		}
12872 		mutex_exit(&ill->ill_lock);
12873 	}
12874 	rw_exit(&ipst->ips_ill_g_lock);
12875 
12876 	*ipifp = fallback_ipif;
12877 	return ((fallback_ipif != NULL) ?
12878 	    fallback_ipif->ipif_net_mask : net_mask);
12879 }
12880 
12881 /*
12882  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12883  */
12884 static void
12885 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12886 {
12887 	IOCP	iocp;
12888 	ipft_t	*ipft;
12889 	ipllc_t	*ipllc;
12890 	mblk_t	*mp1;
12891 	cred_t	*cr;
12892 	int	error = 0;
12893 	conn_t	*connp;
12894 
12895 	ip1dbg(("ip_wput_ioctl"));
12896 	iocp = (IOCP)mp->b_rptr;
12897 	mp1 = mp->b_cont;
12898 	if (mp1 == NULL) {
12899 		iocp->ioc_error = EINVAL;
12900 		mp->b_datap->db_type = M_IOCNAK;
12901 		iocp->ioc_count = 0;
12902 		qreply(q, mp);
12903 		return;
12904 	}
12905 
12906 	/*
12907 	 * These IOCTLs provide various control capabilities to
12908 	 * upstream agents such as ULPs and processes.	There
12909 	 * are currently two such IOCTLs implemented.  They
12910 	 * are used by TCP to provide update information for
12911 	 * existing IREs and to forcibly delete an IRE for a
12912 	 * host that is not responding, thereby forcing an
12913 	 * attempt at a new route.
12914 	 */
12915 	iocp->ioc_error = EINVAL;
12916 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12917 		goto done;
12918 
12919 	ipllc = (ipllc_t *)mp1->b_rptr;
12920 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12921 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12922 			break;
12923 	}
12924 	/*
12925 	 * prefer credential from mblk over ioctl;
12926 	 * see ip_sioctl_copyin_setup
12927 	 */
12928 	cr = msg_getcred(mp, NULL);
12929 	if (cr == NULL)
12930 		cr = iocp->ioc_cr;
12931 
12932 	/*
12933 	 * Refhold the conn in case the request gets queued up in some lookup
12934 	 */
12935 	ASSERT(CONN_Q(q));
12936 	connp = Q_TO_CONN(q);
12937 	CONN_INC_REF(connp);
12938 	if (ipft->ipft_pfi &&
12939 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12940 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12941 		error = (*ipft->ipft_pfi)(q,
12942 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12943 	}
12944 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12945 		/*
12946 		 * CONN_OPER_PENDING_DONE happens in the function called
12947 		 * through ipft_pfi above.
12948 		 */
12949 		return;
12950 	}
12951 
12952 	CONN_OPER_PENDING_DONE(connp);
12953 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12954 		freemsg(mp);
12955 		return;
12956 	}
12957 	iocp->ioc_error = error;
12958 
12959 done:
12960 	mp->b_datap->db_type = M_IOCACK;
12961 	if (iocp->ioc_error)
12962 		iocp->ioc_count = 0;
12963 	qreply(q, mp);
12964 }
12965 
12966 /*
12967  * Lookup an ipif using the sequence id (ipif_seqid)
12968  */
12969 ipif_t *
12970 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12971 {
12972 	ipif_t *ipif;
12973 
12974 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12975 
12976 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12977 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12978 			return (ipif);
12979 	}
12980 	return (NULL);
12981 }
12982 
12983 /*
12984  * Assign a unique id for the ipif. This is used later when we send
12985  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12986  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12987  * IRE is added, we verify that ipif has not disappeared.
12988  */
12989 
12990 static void
12991 ipif_assign_seqid(ipif_t *ipif)
12992 {
12993 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12994 
12995 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
12996 }
12997 
12998 /*
12999  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
13000  * administratively down (i.e., no DAD), of the same type, and locked.  Note
13001  * that the clone is complete -- including the seqid -- and the expectation is
13002  * that the caller will either free or overwrite `sipif' before it's unlocked.
13003  */
13004 static void
13005 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
13006 {
13007 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
13008 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
13009 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13010 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13011 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
13012 	ASSERT(sipif->ipif_arp_del_mp == NULL);
13013 	ASSERT(dipif->ipif_arp_del_mp == NULL);
13014 	ASSERT(sipif->ipif_igmp_rpt == NULL);
13015 	ASSERT(dipif->ipif_igmp_rpt == NULL);
13016 	ASSERT(sipif->ipif_multicast_up == 0);
13017 	ASSERT(dipif->ipif_multicast_up == 0);
13018 	ASSERT(sipif->ipif_joined_allhosts == 0);
13019 	ASSERT(dipif->ipif_joined_allhosts == 0);
13020 
13021 	dipif->ipif_mtu = sipif->ipif_mtu;
13022 	dipif->ipif_flags = sipif->ipif_flags;
13023 	dipif->ipif_metric = sipif->ipif_metric;
13024 	dipif->ipif_zoneid = sipif->ipif_zoneid;
13025 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
13026 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
13027 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
13028 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
13029 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
13030 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
13031 
13032 	/*
13033 	 * While dipif is down right now, it might've been up before.  Since
13034 	 * it's changing identity, its packet counters need to be reset.
13035 	 */
13036 	dipif->ipif_ib_pkt_count = 0;
13037 	dipif->ipif_ob_pkt_count = 0;
13038 	dipif->ipif_fo_pkt_count = 0;
13039 
13040 	/*
13041 	 * As per the comment atop the function, we assume that these sipif
13042 	 * fields will be changed before sipif is unlocked.
13043 	 */
13044 	dipif->ipif_seqid = sipif->ipif_seqid;
13045 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
13046 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
13047 	dipif->ipif_state_flags = sipif->ipif_state_flags;
13048 }
13049 
13050 /*
13051  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
13052  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
13053  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
13054  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
13055  * down (i.e., no DAD), of the same type, and unlocked.
13056  */
13057 static void
13058 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
13059 {
13060 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
13061 	ipxop_t *ipx = ipsq->ipsq_xop;
13062 
13063 	ASSERT(sipif != dipif);
13064 	ASSERT(sipif != virgipif);
13065 
13066 	/*
13067 	 * Grab all of the locks that protect the ipif in a defined order.
13068 	 */
13069 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13070 	if (sipif > dipif) {
13071 		mutex_enter(&sipif->ipif_saved_ire_lock);
13072 		mutex_enter(&dipif->ipif_saved_ire_lock);
13073 	} else {
13074 		mutex_enter(&dipif->ipif_saved_ire_lock);
13075 		mutex_enter(&sipif->ipif_saved_ire_lock);
13076 	}
13077 
13078 	ipif_clone(sipif, dipif);
13079 	if (virgipif != NULL) {
13080 		ipif_clone(virgipif, sipif);
13081 		mi_free(virgipif);
13082 	}
13083 
13084 	mutex_exit(&sipif->ipif_saved_ire_lock);
13085 	mutex_exit(&dipif->ipif_saved_ire_lock);
13086 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13087 
13088 	/*
13089 	 * Transfer ownership of the current xop, if necessary.
13090 	 */
13091 	if (ipx->ipx_current_ipif == sipif) {
13092 		ASSERT(ipx->ipx_pending_ipif == NULL);
13093 		mutex_enter(&ipx->ipx_lock);
13094 		ipx->ipx_current_ipif = dipif;
13095 		mutex_exit(&ipx->ipx_lock);
13096 	}
13097 
13098 	if (virgipif == NULL)
13099 		mi_free(sipif);
13100 }
13101 
13102 /*
13103  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13104  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13105  * be inserted into the first space available in the list. The value of
13106  * ipif_id will then be set to the appropriate value for its position.
13107  */
13108 static int
13109 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13110 {
13111 	ill_t *ill;
13112 	ipif_t *tipif;
13113 	ipif_t **tipifp;
13114 	int id;
13115 	ip_stack_t	*ipst;
13116 
13117 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13118 	    IAM_WRITER_IPIF(ipif));
13119 
13120 	ill = ipif->ipif_ill;
13121 	ASSERT(ill != NULL);
13122 	ipst = ill->ill_ipst;
13123 
13124 	/*
13125 	 * In the case of lo0:0 we already hold the ill_g_lock.
13126 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13127 	 * ipif_insert.
13128 	 */
13129 	if (acquire_g_lock)
13130 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13131 	mutex_enter(&ill->ill_lock);
13132 	id = ipif->ipif_id;
13133 	tipifp = &(ill->ill_ipif);
13134 	if (id == -1) {	/* need to find a real id */
13135 		id = 0;
13136 		while ((tipif = *tipifp) != NULL) {
13137 			ASSERT(tipif->ipif_id >= id);
13138 			if (tipif->ipif_id != id)
13139 				break; /* non-consecutive id */
13140 			id++;
13141 			tipifp = &(tipif->ipif_next);
13142 		}
13143 		/* limit number of logical interfaces */
13144 		if (id >= ipst->ips_ip_addrs_per_if) {
13145 			mutex_exit(&ill->ill_lock);
13146 			if (acquire_g_lock)
13147 				rw_exit(&ipst->ips_ill_g_lock);
13148 			return (-1);
13149 		}
13150 		ipif->ipif_id = id; /* assign new id */
13151 	} else if (id < ipst->ips_ip_addrs_per_if) {
13152 		/* we have a real id; insert ipif in the right place */
13153 		while ((tipif = *tipifp) != NULL) {
13154 			ASSERT(tipif->ipif_id != id);
13155 			if (tipif->ipif_id > id)
13156 				break; /* found correct location */
13157 			tipifp = &(tipif->ipif_next);
13158 		}
13159 	} else {
13160 		mutex_exit(&ill->ill_lock);
13161 		if (acquire_g_lock)
13162 			rw_exit(&ipst->ips_ill_g_lock);
13163 		return (-1);
13164 	}
13165 
13166 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13167 
13168 	ipif->ipif_next = tipif;
13169 	*tipifp = ipif;
13170 	mutex_exit(&ill->ill_lock);
13171 	if (acquire_g_lock)
13172 		rw_exit(&ipst->ips_ill_g_lock);
13173 
13174 	return (0);
13175 }
13176 
13177 static void
13178 ipif_remove(ipif_t *ipif)
13179 {
13180 	ipif_t	**ipifp;
13181 	ill_t	*ill = ipif->ipif_ill;
13182 
13183 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13184 
13185 	mutex_enter(&ill->ill_lock);
13186 	ipifp = &ill->ill_ipif;
13187 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13188 		if (*ipifp == ipif) {
13189 			*ipifp = ipif->ipif_next;
13190 			break;
13191 		}
13192 	}
13193 	mutex_exit(&ill->ill_lock);
13194 }
13195 
13196 /*
13197  * Allocate and initialize a new interface control structure.  (Always
13198  * called as writer.)
13199  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13200  * is not part of the global linked list of ills. ipif_seqid is unique
13201  * in the system and to preserve the uniqueness, it is assigned only
13202  * when ill becomes part of the global list. At that point ill will
13203  * have a name. If it doesn't get assigned here, it will get assigned
13204  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13205  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13206  * the interface flags or any other information from the DL_INFO_ACK for
13207  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13208  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13209  * second DL_INFO_ACK comes in from the driver.
13210  */
13211 static ipif_t *
13212 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13213     boolean_t insert)
13214 {
13215 	ipif_t	*ipif;
13216 	ip_stack_t *ipst = ill->ill_ipst;
13217 
13218 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13219 	    ill->ill_name, id, (void *)ill));
13220 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13221 
13222 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13223 		return (NULL);
13224 	*ipif = ipif_zero;	/* start clean */
13225 
13226 	ipif->ipif_ill = ill;
13227 	ipif->ipif_id = id;	/* could be -1 */
13228 	/*
13229 	 * Inherit the zoneid from the ill; for the shared stack instance
13230 	 * this is always the global zone
13231 	 */
13232 	ipif->ipif_zoneid = ill->ill_zoneid;
13233 
13234 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13235 
13236 	ipif->ipif_refcnt = 0;
13237 	ipif->ipif_saved_ire_cnt = 0;
13238 
13239 	if (insert) {
13240 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13241 			mi_free(ipif);
13242 			return (NULL);
13243 		}
13244 		/* -1 id should have been replaced by real id */
13245 		id = ipif->ipif_id;
13246 		ASSERT(id >= 0);
13247 	}
13248 
13249 	if (ill->ill_name[0] != '\0')
13250 		ipif_assign_seqid(ipif);
13251 
13252 	/*
13253 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
13254 	 * (which must not exist yet because the zeroth ipif is created once
13255 	 * per ill).  However, do not not link it to the ipmp_grp_t until
13256 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
13257 	 */
13258 	if (id == 0 && IS_IPMP(ill)) {
13259 		if (ipmp_illgrp_create(ill) == NULL) {
13260 			if (insert) {
13261 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13262 				ipif_remove(ipif);
13263 				rw_exit(&ipst->ips_ill_g_lock);
13264 			}
13265 			mi_free(ipif);
13266 			return (NULL);
13267 		}
13268 	}
13269 
13270 	/*
13271 	 * We grab ill_lock to protect the flag changes.  The ipif is still
13272 	 * not up and can't be looked up until the ioctl completes and the
13273 	 * IPIF_CHANGING flag is cleared.
13274 	 */
13275 	mutex_enter(&ill->ill_lock);
13276 
13277 	ipif->ipif_ire_type = ire_type;
13278 
13279 	if (ipif->ipif_isv6) {
13280 		ill->ill_flags |= ILLF_IPV6;
13281 	} else {
13282 		ipaddr_t inaddr_any = INADDR_ANY;
13283 
13284 		ill->ill_flags |= ILLF_IPV4;
13285 
13286 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13287 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13288 		    &ipif->ipif_v6lcl_addr);
13289 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13290 		    &ipif->ipif_v6src_addr);
13291 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13292 		    &ipif->ipif_v6subnet);
13293 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13294 		    &ipif->ipif_v6net_mask);
13295 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13296 		    &ipif->ipif_v6brd_addr);
13297 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13298 		    &ipif->ipif_v6pp_dst_addr);
13299 	}
13300 
13301 	/*
13302 	 * Don't set the interface flags etc. now, will do it in
13303 	 * ip_ll_subnet_defaults.
13304 	 */
13305 	if (!initialize)
13306 		goto out;
13307 
13308 	ipif->ipif_mtu = ill->ill_max_mtu;
13309 
13310 	/*
13311 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13312 	 * with no underlying interfaces (and thus an unknown broadcast
13313 	 * address length), but all interfaces that can be placed into an IPMP
13314 	 * group are required to be broadcast-capable.
13315 	 */
13316 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13317 		/*
13318 		 * Later detect lack of DLPI driver multicast capability by
13319 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
13320 		 */
13321 		ill->ill_flags |= ILLF_MULTICAST;
13322 		if (!ipif->ipif_isv6)
13323 			ipif->ipif_flags |= IPIF_BROADCAST;
13324 	} else {
13325 		if (ill->ill_net_type != IRE_LOOPBACK) {
13326 			if (ipif->ipif_isv6)
13327 				/*
13328 				 * Note: xresolv interfaces will eventually need
13329 				 * NOARP set here as well, but that will require
13330 				 * those external resolvers to have some
13331 				 * knowledge of that flag and act appropriately.
13332 				 * Not to be changed at present.
13333 				 */
13334 				ill->ill_flags |= ILLF_NONUD;
13335 			else
13336 				ill->ill_flags |= ILLF_NOARP;
13337 		}
13338 		if (ill->ill_phys_addr_length == 0) {
13339 			if (IS_VNI(ill)) {
13340 				ipif->ipif_flags |= IPIF_NOXMIT;
13341 			} else {
13342 				/* pt-pt supports multicast. */
13343 				ill->ill_flags |= ILLF_MULTICAST;
13344 				if (ill->ill_net_type != IRE_LOOPBACK)
13345 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13346 			}
13347 		}
13348 	}
13349 out:
13350 	mutex_exit(&ill->ill_lock);
13351 	return (ipif);
13352 }
13353 
13354 /*
13355  * If appropriate, send a message up to the resolver delete the entry
13356  * for the address of this interface which is going out of business.
13357  * (Always called as writer).
13358  *
13359  * NOTE : We need to check for NULL mps as some of the fields are
13360  *	  initialized only for some interface types. See ipif_resolver_up()
13361  *	  for details.
13362  */
13363 void
13364 ipif_resolver_down(ipif_t *ipif)
13365 {
13366 	mblk_t	*mp;
13367 	ill_t	*ill = ipif->ipif_ill;
13368 
13369 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13370 	ASSERT(IAM_WRITER_IPIF(ipif));
13371 
13372 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13373 		return;
13374 
13375 	/* Delete the mapping for the local address */
13376 	mp = ipif->ipif_arp_del_mp;
13377 	if (mp != NULL) {
13378 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13379 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13380 		putnext(ill->ill_rq, mp);
13381 		ipif->ipif_arp_del_mp = NULL;
13382 	}
13383 
13384 	/*
13385 	 * Make IPMP aware of the deleted data address.
13386 	 */
13387 	if (IS_IPMP(ill))
13388 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13389 
13390 	/*
13391 	 * If this is the last ipif that is going down and there are no
13392 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13393 	 * clean up ARP completely.
13394 	 */
13395 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13396 		/*
13397 		 * If this was the last ipif on an IPMP interface, purge any
13398 		 * IPMP ARP entries associated with it.
13399 		 */
13400 		if (IS_IPMP(ill))
13401 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13402 
13403 		/* Send up AR_INTERFACE_DOWN message */
13404 		mp = ill->ill_arp_down_mp;
13405 		if (mp != NULL) {
13406 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13407 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13408 			    ipif->ipif_id));
13409 			putnext(ill->ill_rq, mp);
13410 			ill->ill_arp_down_mp = NULL;
13411 		}
13412 
13413 		/* Tell ARP to delete the multicast mappings */
13414 		mp = ill->ill_arp_del_mapping_mp;
13415 		if (mp != NULL) {
13416 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13417 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13418 			    ipif->ipif_id));
13419 			putnext(ill->ill_rq, mp);
13420 			ill->ill_arp_del_mapping_mp = NULL;
13421 		}
13422 	}
13423 }
13424 
13425 /*
13426  * Set up the multicast mappings for `ipif' in ARP.  If `arp_add_mapping_mp'
13427  * is non-NULL, then upon success it will contain an mblk that can be passed
13428  * to ARP to create the mapping.  Otherwise, if it's NULL, upon success ARP
13429  * will have already been notified to create the mapping.  Returns zero on
13430  * success, -1 upon failure.
13431  */
13432 int
13433 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13434 {
13435 	mblk_t	*del_mp = NULL;
13436 	mblk_t *add_mp = NULL;
13437 	mblk_t *mp;
13438 	ill_t	*ill = ipif->ipif_ill;
13439 	phyint_t *phyi = ill->ill_phyint;
13440 	ipaddr_t addr, mask, extract_mask = 0;
13441 	arma_t	*arma;
13442 	uint8_t *maddr, *bphys_addr;
13443 	uint32_t hw_start;
13444 	dl_unitdata_req_t *dlur;
13445 
13446 	ASSERT(IAM_WRITER_IPIF(ipif));
13447 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13448 		return (0);
13449 
13450 	/*
13451 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13452 	 * and instead use the ones on the underlying interfaces.
13453 	 */
13454 	if (IS_IPMP(ill))
13455 		return (0);
13456 
13457 	/*
13458 	 * Delete the existing mapping from ARP.  Normally, ipif_down() ->
13459 	 * ipif_resolver_down() will send this up to ARP, but it may be that
13460 	 * we are enabling PHYI_MULTI_BCAST via ip_rput_dlpi_writer().
13461 	 */
13462 	mp = ill->ill_arp_del_mapping_mp;
13463 	if (mp != NULL) {
13464 		ip1dbg(("ipif_arp_setup_multicast: arp cmd %x for %s:%u\n",
13465 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13466 		putnext(ill->ill_rq, mp);
13467 		ill->ill_arp_del_mapping_mp = NULL;
13468 	}
13469 
13470 	if (arp_add_mapping_mp != NULL)
13471 		*arp_add_mapping_mp = NULL;
13472 
13473 	/*
13474 	 * Check that the address is not to long for the constant
13475 	 * length reserved in the template arma_t.
13476 	 */
13477 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13478 		return (-1);
13479 
13480 	/* Add mapping mblk */
13481 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13482 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13483 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13484 	    (caddr_t)&addr);
13485 	if (add_mp == NULL)
13486 		return (-1);
13487 	arma = (arma_t *)add_mp->b_rptr;
13488 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13489 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13490 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13491 
13492 	/*
13493 	 * Determine the broadcast address.
13494 	 */
13495 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13496 	if (ill->ill_sap_length < 0)
13497 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13498 	else
13499 		bphys_addr = (uchar_t *)dlur +
13500 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13501 	/*
13502 	 * Check PHYI_MULTI_BCAST and length of physical
13503 	 * address to determine if we use the mapping or the
13504 	 * broadcast address.
13505 	 */
13506 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13507 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13508 		    bphys_addr, maddr, &hw_start, &extract_mask))
13509 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13510 
13511 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13512 	    (ill->ill_flags & ILLF_MULTICAST)) {
13513 		/* Make sure this will not match the "exact" entry. */
13514 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13515 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13516 		    (caddr_t)&addr);
13517 		if (del_mp == NULL) {
13518 			freemsg(add_mp);
13519 			return (-1);
13520 		}
13521 		bcopy(&extract_mask, (char *)arma +
13522 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13523 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13524 			/* Use link-layer broadcast address for MULTI_BCAST */
13525 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13526 			ip2dbg(("ipif_arp_setup_multicast: adding"
13527 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13528 		} else {
13529 			arma->arma_hw_mapping_start = hw_start;
13530 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13531 			    " ARP setup for %s\n", ill->ill_name));
13532 		}
13533 	} else {
13534 		freemsg(add_mp);
13535 		ASSERT(del_mp == NULL);
13536 		/* It is neither MULTICAST nor MULTI_BCAST */
13537 		return (0);
13538 	}
13539 	ASSERT(add_mp != NULL && del_mp != NULL);
13540 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13541 	ill->ill_arp_del_mapping_mp = del_mp;
13542 	if (arp_add_mapping_mp != NULL) {
13543 		/* The caller just wants the mblks allocated */
13544 		*arp_add_mapping_mp = add_mp;
13545 	} else {
13546 		/* The caller wants us to send it to arp */
13547 		putnext(ill->ill_rq, add_mp);
13548 	}
13549 	return (0);
13550 }
13551 
13552 /*
13553  * Get the resolver set up for a new IP address.  (Always called as writer.)
13554  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13555  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13556  *
13557  * The enumerated value res_act tunes the behavior:
13558  * 	* Res_act_initial: set up all the resolver structures for a new
13559  *	  IP address.
13560  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13561  *	  ARP message in defense of the address.
13562  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13563  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13564  *
13565  * Returns zero on success, or an errno upon failure.
13566  */
13567 int
13568 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13569 {
13570 	mblk_t	*arp_up_mp = NULL;
13571 	mblk_t	*arp_down_mp = NULL;
13572 	mblk_t	*arp_add_mp = NULL;
13573 	mblk_t	*arp_del_mp = NULL;
13574 	mblk_t	*arp_add_mapping_mp = NULL;
13575 	mblk_t	*arp_del_mapping_mp = NULL;
13576 	ill_t	*ill = ipif->ipif_ill;
13577 	int	err = ENOMEM;
13578 	boolean_t added_ipif = B_FALSE;
13579 	boolean_t publish;
13580 	boolean_t was_dup;
13581 
13582 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13583 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13584 	ASSERT(IAM_WRITER_IPIF(ipif));
13585 
13586 	was_dup = B_FALSE;
13587 	if (res_act == Res_act_initial) {
13588 		ipif->ipif_addr_ready = 0;
13589 		/*
13590 		 * We're bringing an interface up here.  There's no way that we
13591 		 * should need to shut down ARP now.
13592 		 */
13593 		mutex_enter(&ill->ill_lock);
13594 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13595 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13596 			ill->ill_ipif_dup_count--;
13597 			was_dup = B_TRUE;
13598 		}
13599 		mutex_exit(&ill->ill_lock);
13600 	}
13601 	if (ipif->ipif_recovery_id != 0)
13602 		(void) untimeout(ipif->ipif_recovery_id);
13603 	ipif->ipif_recovery_id = 0;
13604 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13605 		ipif->ipif_addr_ready = 1;
13606 		return (0);
13607 	}
13608 	/* NDP will set the ipif_addr_ready flag when it's ready */
13609 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13610 		return (0);
13611 
13612 	if (ill->ill_isv6) {
13613 		/*
13614 		 * External resolver for IPv6
13615 		 */
13616 		ASSERT(res_act == Res_act_initial);
13617 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13618 	} else {
13619 		/*
13620 		 * IPv4 arp case. If the ARP stream has already started
13621 		 * closing, fail this request for ARP bringup. Else
13622 		 * record the fact that an ARP bringup is pending.
13623 		 */
13624 		mutex_enter(&ill->ill_lock);
13625 		if (ill->ill_arp_closing) {
13626 			mutex_exit(&ill->ill_lock);
13627 			err = EINVAL;
13628 			goto failed;
13629 		} else {
13630 			if (ill->ill_ipif_up_count == 0 &&
13631 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13632 				ill->ill_arp_bringup_pending = 1;
13633 			mutex_exit(&ill->ill_lock);
13634 		}
13635 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13636 	}
13637 
13638 	if (IS_IPMP(ill) && publish) {
13639 		/*
13640 		 * If we're here via ipif_up(), then the ipif won't be bound
13641 		 * yet -- add it to the group, which will bind it if possible.
13642 		 * (We would add it in ipif_up(), but deleting on failure
13643 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13644 		 * then the ipif has already been added to the group and we
13645 		 * just need to use the binding.
13646 		 */
13647 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13648 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13649 				/*
13650 				 * We couldn't bind the ipif to an ill yet,
13651 				 * so we have nothing to publish.
13652 				 */
13653 				publish = B_FALSE;
13654 			}
13655 			added_ipif = B_TRUE;
13656 		}
13657 	}
13658 
13659 	/*
13660 	 * Add an entry for the local address in ARP only if it
13661 	 * is not UNNUMBERED and it is suitable for publishing.
13662 	 */
13663 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13664 		if (res_act == Res_act_defend) {
13665 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13666 			if (arp_add_mp == NULL)
13667 				goto failed;
13668 			/*
13669 			 * If we're just defending our address now, then
13670 			 * there's no need to set up ARP multicast mappings.
13671 			 * The publish command is enough.
13672 			 */
13673 			goto done;
13674 		}
13675 
13676 		/*
13677 		 * Allocate an ARP add message and an ARP delete message (the
13678 		 * latter is saved for use when the address goes down).
13679 		 */
13680 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13681 			goto failed;
13682 
13683 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13684 			goto failed;
13685 
13686 		if (res_act != Res_act_initial)
13687 			goto arp_setup_multicast;
13688 	} else {
13689 		if (res_act != Res_act_initial)
13690 			goto done;
13691 	}
13692 	/*
13693 	 * Need to bring up ARP or setup multicast mapping only
13694 	 * when the first interface is coming UP.
13695 	 */
13696 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13697 		goto done;
13698 
13699 	/*
13700 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13701 	 */
13702 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13703 	if (arp_down_mp == NULL)
13704 		goto failed;
13705 
13706 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13707 	if (arp_up_mp == NULL)
13708 		goto failed;
13709 
13710 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13711 		goto done;
13712 
13713 arp_setup_multicast:
13714 	/*
13715 	 * Setup the multicast mappings. This function initializes
13716 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13717 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13718 	 */
13719 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13720 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13721 		if (err != 0)
13722 			goto failed;
13723 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13724 		ASSERT(arp_add_mapping_mp != NULL);
13725 	}
13726 done:
13727 	if (arp_up_mp != NULL) {
13728 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13729 		    ill->ill_name, ipif->ipif_id));
13730 		putnext(ill->ill_rq, arp_up_mp);
13731 		arp_up_mp = NULL;
13732 	}
13733 	if (arp_add_mp != NULL) {
13734 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13735 		    ill->ill_name, ipif->ipif_id));
13736 		/*
13737 		 * If it's an extended ARP implementation, then we'll wait to
13738 		 * hear that DAD has finished before using the interface.
13739 		 */
13740 		if (!ill->ill_arp_extend)
13741 			ipif->ipif_addr_ready = 1;
13742 		putnext(ill->ill_rq, arp_add_mp);
13743 		arp_add_mp = NULL;
13744 	} else {
13745 		ipif->ipif_addr_ready = 1;
13746 	}
13747 	if (arp_add_mapping_mp != NULL) {
13748 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13749 		    ill->ill_name, ipif->ipif_id));
13750 		putnext(ill->ill_rq, arp_add_mapping_mp);
13751 		arp_add_mapping_mp = NULL;
13752 	}
13753 
13754 	if (res_act == Res_act_initial) {
13755 		if (ill->ill_flags & ILLF_NOARP)
13756 			err = ill_arp_off(ill);
13757 		else
13758 			err = ill_arp_on(ill);
13759 		if (err != 0) {
13760 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13761 			    err));
13762 			goto failed;
13763 		}
13764 	}
13765 
13766 	if (arp_del_mp != NULL) {
13767 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13768 		ipif->ipif_arp_del_mp = arp_del_mp;
13769 	}
13770 	if (arp_down_mp != NULL) {
13771 		ASSERT(ill->ill_arp_down_mp == NULL);
13772 		ill->ill_arp_down_mp = arp_down_mp;
13773 	}
13774 	if (arp_del_mapping_mp != NULL) {
13775 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13776 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13777 	}
13778 
13779 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13780 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13781 failed:
13782 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13783 	if (added_ipif)
13784 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13785 	freemsg(arp_add_mp);
13786 	freemsg(arp_del_mp);
13787 	freemsg(arp_add_mapping_mp);
13788 	freemsg(arp_up_mp);
13789 	freemsg(arp_down_mp);
13790 	ill->ill_arp_bringup_pending = 0;
13791 	return (err);
13792 }
13793 
13794 /*
13795  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13796  * just gone back up.
13797  */
13798 static void
13799 ipif_arp_start_dad(ipif_t *ipif)
13800 {
13801 	ill_t *ill = ipif->ipif_ill;
13802 	mblk_t *arp_add_mp;
13803 
13804 	/* ACE_F_UNVERIFIED restarts DAD */
13805 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13806 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13807 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13808 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13809 		/*
13810 		 * If we can't contact ARP for some reason, that's not really a
13811 		 * problem.  Just send out the routing socket notification that
13812 		 * DAD completion would have done, and continue.
13813 		 */
13814 		ipif_mask_reply(ipif);
13815 		ipif_up_notify(ipif);
13816 		ipif->ipif_addr_ready = 1;
13817 		return;
13818 	}
13819 
13820 	putnext(ill->ill_rq, arp_add_mp);
13821 }
13822 
13823 static void
13824 ipif_ndp_start_dad(ipif_t *ipif)
13825 {
13826 	nce_t *nce;
13827 
13828 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13829 	    B_FALSE);
13830 	if (nce == NULL)
13831 		return;
13832 
13833 	if (!ndp_restart_dad(nce)) {
13834 		/*
13835 		 * If we can't restart DAD for some reason, that's not really a
13836 		 * problem.  Just send out the routing socket notification that
13837 		 * DAD completion would have done, and continue.
13838 		 */
13839 		ipif_up_notify(ipif);
13840 		ipif->ipif_addr_ready = 1;
13841 	}
13842 	NCE_REFRELE(nce);
13843 }
13844 
13845 /*
13846  * Restart duplicate address detection on all interfaces on the given ill.
13847  *
13848  * This is called when an interface transitions from down to up
13849  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13850  *
13851  * Note that since the underlying physical link has transitioned, we must cause
13852  * at least one routing socket message to be sent here, either via DAD
13853  * completion or just by default on the first ipif.  (If we don't do this, then
13854  * in.mpathd will see long delays when doing link-based failure recovery.)
13855  */
13856 void
13857 ill_restart_dad(ill_t *ill, boolean_t went_up)
13858 {
13859 	ipif_t *ipif;
13860 
13861 	if (ill == NULL)
13862 		return;
13863 
13864 	/*
13865 	 * If layer two doesn't support duplicate address detection, then just
13866 	 * send the routing socket message now and be done with it.
13867 	 */
13868 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13869 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13870 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13871 		return;
13872 	}
13873 
13874 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13875 		if (went_up) {
13876 			if (ipif->ipif_flags & IPIF_UP) {
13877 				if (ill->ill_isv6)
13878 					ipif_ndp_start_dad(ipif);
13879 				else
13880 					ipif_arp_start_dad(ipif);
13881 			} else if (ill->ill_isv6 &&
13882 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13883 				/*
13884 				 * For IPv4, the ARP module itself will
13885 				 * automatically start the DAD process when it
13886 				 * sees DL_NOTE_LINK_UP.  We respond to the
13887 				 * AR_CN_READY at the completion of that task.
13888 				 * For IPv6, we must kick off the bring-up
13889 				 * process now.
13890 				 */
13891 				ndp_do_recovery(ipif);
13892 			} else {
13893 				/*
13894 				 * Unfortunately, the first ipif is "special"
13895 				 * and represents the underlying ill in the
13896 				 * routing socket messages.  Thus, when this
13897 				 * one ipif is down, we must still notify so
13898 				 * that the user knows the IFF_RUNNING status
13899 				 * change.  (If the first ipif is up, then
13900 				 * we'll handle eventual routing socket
13901 				 * notification via DAD completion.)
13902 				 */
13903 				if (ipif == ill->ill_ipif) {
13904 					ip_rts_ifmsg(ill->ill_ipif,
13905 					    RTSQ_DEFAULT);
13906 				}
13907 			}
13908 		} else {
13909 			/*
13910 			 * After link down, we'll need to send a new routing
13911 			 * message when the link comes back, so clear
13912 			 * ipif_addr_ready.
13913 			 */
13914 			ipif->ipif_addr_ready = 0;
13915 		}
13916 	}
13917 
13918 	/*
13919 	 * If we've torn down links, then notify the user right away.
13920 	 */
13921 	if (!went_up)
13922 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13923 }
13924 
13925 static void
13926 ipsq_delete(ipsq_t *ipsq)
13927 {
13928 	ipxop_t *ipx = ipsq->ipsq_xop;
13929 
13930 	ipsq->ipsq_ipst = NULL;
13931 	ASSERT(ipsq->ipsq_phyint == NULL);
13932 	ASSERT(ipsq->ipsq_xop != NULL);
13933 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
13934 	ASSERT(ipx->ipx_pending_mp == NULL);
13935 	kmem_free(ipsq, sizeof (ipsq_t));
13936 }
13937 
13938 static int
13939 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
13940 {
13941 	int err;
13942 	ipif_t *ipif;
13943 
13944 	if (ill == NULL)
13945 		return (0);
13946 
13947 	ASSERT(IAM_WRITER_ILL(ill));
13948 	ill->ill_up_ipifs = B_TRUE;
13949 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13950 		if (ipif->ipif_was_up) {
13951 			if (!(ipif->ipif_flags & IPIF_UP))
13952 				err = ipif_up(ipif, q, mp);
13953 			ipif->ipif_was_up = B_FALSE;
13954 			if (err != 0) {
13955 				ASSERT(err == EINPROGRESS);
13956 				return (err);
13957 			}
13958 		}
13959 	}
13960 	mutex_enter(&ill->ill_lock);
13961 	ill->ill_state_flags &= ~ILL_CHANGING;
13962 	mutex_exit(&ill->ill_lock);
13963 	ill->ill_up_ipifs = B_FALSE;
13964 	return (0);
13965 }
13966 
13967 /*
13968  * This function is called to bring up all the ipifs that were up before
13969  * bringing the ill down via ill_down_ipifs().
13970  */
13971 int
13972 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13973 {
13974 	int err;
13975 
13976 	ASSERT(IAM_WRITER_ILL(ill));
13977 
13978 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
13979 	if (err != 0)
13980 		return (err);
13981 
13982 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
13983 }
13984 
13985 /*
13986  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
13987  * down the ipifs without sending DL_UNBIND_REQ to the driver.
13988  */
13989 static void
13990 ill_down_ipifs(ill_t *ill, boolean_t logical)
13991 {
13992 	ipif_t *ipif;
13993 
13994 	ASSERT(IAM_WRITER_ILL(ill));
13995 
13996 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13997 		/*
13998 		 * We go through the ipif_down logic even if the ipif
13999 		 * is already down, since routes can be added based
14000 		 * on down ipifs. Going through ipif_down once again
14001 		 * will delete any IREs created based on these routes.
14002 		 */
14003 		if (ipif->ipif_flags & IPIF_UP)
14004 			ipif->ipif_was_up = B_TRUE;
14005 
14006 		/*
14007 		 * Need to re-create net/subnet bcast ires if
14008 		 * they are dependent on ipif.
14009 		 */
14010 		if (!ipif->ipif_isv6)
14011 			ipif_check_bcast_ires(ipif);
14012 		if (logical) {
14013 			(void) ipif_logical_down(ipif, NULL, NULL);
14014 			ipif_non_duplicate(ipif);
14015 			ipif_down_tail(ipif);
14016 		} else {
14017 			(void) ipif_down(ipif, NULL, NULL);
14018 		}
14019 	}
14020 }
14021 
14022 /*
14023  * Redo source address selection.  This is called when a
14024  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
14025  */
14026 void
14027 ill_update_source_selection(ill_t *ill)
14028 {
14029 	ipif_t *ipif;
14030 
14031 	ASSERT(IAM_WRITER_ILL(ill));
14032 
14033 	/*
14034 	 * Underlying interfaces are only used for test traffic and thus
14035 	 * should always send with their (deprecated) source addresses.
14036 	 */
14037 	if (IS_UNDER_IPMP(ill))
14038 		return;
14039 
14040 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14041 		if (ill->ill_isv6)
14042 			ipif_recreate_interface_routes_v6(NULL, ipif);
14043 		else
14044 			ipif_recreate_interface_routes(NULL, ipif);
14045 	}
14046 }
14047 
14048 /*
14049  * Finish the group join started in ip_sioctl_groupname().
14050  */
14051 /* ARGSUSED */
14052 static void
14053 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14054 {
14055 	ill_t		*ill = q->q_ptr;
14056 	phyint_t	*phyi = ill->ill_phyint;
14057 	ipmp_grp_t	*grp = phyi->phyint_grp;
14058 	ip_stack_t	*ipst = ill->ill_ipst;
14059 
14060 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14061 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14062 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14063 
14064 	if (phyi->phyint_illv4 != NULL) {
14065 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14066 		VERIFY(grp->gr_pendv4-- > 0);
14067 		rw_exit(&ipst->ips_ipmp_lock);
14068 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14069 	}
14070 	if (phyi->phyint_illv6 != NULL) {
14071 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14072 		VERIFY(grp->gr_pendv6-- > 0);
14073 		rw_exit(&ipst->ips_ipmp_lock);
14074 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14075 	}
14076 	freemsg(mp);
14077 }
14078 
14079 /*
14080  * Process an SIOCSLIFGROUPNAME request.
14081  */
14082 /* ARGSUSED */
14083 int
14084 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14085     ip_ioctl_cmd_t *ipip, void *ifreq)
14086 {
14087 	struct lifreq	*lifr = ifreq;
14088 	ill_t		*ill = ipif->ipif_ill;
14089 	ip_stack_t	*ipst = ill->ill_ipst;
14090 	phyint_t	*phyi = ill->ill_phyint;
14091 	ipmp_grp_t	*grp = phyi->phyint_grp;
14092 	mblk_t		*ipsq_mp;
14093 	int		err = 0;
14094 
14095 	/*
14096 	 * Note that phyint_grp can only change here, where we're exclusive.
14097 	 */
14098 	ASSERT(IAM_WRITER_ILL(ill));
14099 
14100 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14101 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14102 		return (EINVAL);
14103 
14104 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14105 
14106 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14107 
14108 	/*
14109 	 * If the name hasn't changed, there's nothing to do.
14110 	 */
14111 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14112 		goto unlock;
14113 
14114 	/*
14115 	 * Handle requests to rename an IPMP meta-interface.
14116 	 *
14117 	 * Note that creation of the IPMP meta-interface is handled in
14118 	 * userland through the standard plumbing sequence.  As part of the
14119 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14120 	 * the name of the interface (see ipif_set_values_tail()).
14121 	 */
14122 	if (IS_IPMP(ill)) {
14123 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14124 		goto unlock;
14125 	}
14126 
14127 	/*
14128 	 * Handle requests to add or remove an IP interface from a group.
14129 	 */
14130 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14131 		/*
14132 		 * Moves are handled by first removing the interface from
14133 		 * its existing group, and then adding it to another group.
14134 		 * So, fail if it's already in a group.
14135 		 */
14136 		if (IS_UNDER_IPMP(ill)) {
14137 			err = EALREADY;
14138 			goto unlock;
14139 		}
14140 
14141 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14142 		if (grp == NULL) {
14143 			err = ENOENT;
14144 			goto unlock;
14145 		}
14146 
14147 		/*
14148 		 * Check if the phyint and its ills are suitable for
14149 		 * inclusion into the group.
14150 		 */
14151 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14152 			goto unlock;
14153 
14154 		/*
14155 		 * Checks pass; join the group, and enqueue the remaining
14156 		 * illgrp joins for when we've become part of the group xop
14157 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14158 		 * requires an mblk_t to scribble on, and since `mp' will be
14159 		 * freed as part of completing the ioctl, allocate another.
14160 		 */
14161 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14162 			err = ENOMEM;
14163 			goto unlock;
14164 		}
14165 
14166 		/*
14167 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14168 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14169 		 * before ip_join_illgrps() is called back.  See the comments
14170 		 * in ip_sioctl_plink_ipmp() for more.
14171 		 */
14172 		if (phyi->phyint_illv4 != NULL)
14173 			grp->gr_pendv4++;
14174 		if (phyi->phyint_illv6 != NULL)
14175 			grp->gr_pendv6++;
14176 
14177 		rw_exit(&ipst->ips_ipmp_lock);
14178 
14179 		ipmp_phyint_join_grp(phyi, grp);
14180 		ill_refhold(ill);
14181 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14182 		    SWITCH_OP, B_FALSE);
14183 		return (0);
14184 	} else {
14185 		/*
14186 		 * Request to remove the interface from a group.  If the
14187 		 * interface is not in a group, this trivially succeeds.
14188 		 */
14189 		rw_exit(&ipst->ips_ipmp_lock);
14190 		if (IS_UNDER_IPMP(ill))
14191 			ipmp_phyint_leave_grp(phyi);
14192 		return (0);
14193 	}
14194 unlock:
14195 	rw_exit(&ipst->ips_ipmp_lock);
14196 	return (err);
14197 }
14198 
14199 /*
14200  * Process an SIOCGLIFBINDING request.
14201  */
14202 /* ARGSUSED */
14203 int
14204 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14205     ip_ioctl_cmd_t *ipip, void *ifreq)
14206 {
14207 	ill_t		*ill;
14208 	struct lifreq	*lifr = ifreq;
14209 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14210 
14211 	if (!IS_IPMP(ipif->ipif_ill))
14212 		return (EINVAL);
14213 
14214 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14215 	if ((ill = ipif->ipif_bound_ill) == NULL)
14216 		lifr->lifr_binding[0] = '\0';
14217 	else
14218 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
14219 	rw_exit(&ipst->ips_ipmp_lock);
14220 	return (0);
14221 }
14222 
14223 /*
14224  * Process an SIOCGLIFGROUPNAME request.
14225  */
14226 /* ARGSUSED */
14227 int
14228 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14229     ip_ioctl_cmd_t *ipip, void *ifreq)
14230 {
14231 	ipmp_grp_t	*grp;
14232 	struct lifreq	*lifr = ifreq;
14233 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14234 
14235 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14236 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14237 		lifr->lifr_groupname[0] = '\0';
14238 	else
14239 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14240 	rw_exit(&ipst->ips_ipmp_lock);
14241 	return (0);
14242 }
14243 
14244 /*
14245  * Process an SIOCGLIFGROUPINFO request.
14246  */
14247 /* ARGSUSED */
14248 int
14249 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14250     ip_ioctl_cmd_t *ipip, void *dummy)
14251 {
14252 	ipmp_grp_t	*grp;
14253 	lifgroupinfo_t	*lifgr;
14254 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14255 
14256 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14257 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14258 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14259 
14260 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14261 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14262 		rw_exit(&ipst->ips_ipmp_lock);
14263 		return (ENOENT);
14264 	}
14265 	ipmp_grp_info(grp, lifgr);
14266 	rw_exit(&ipst->ips_ipmp_lock);
14267 	return (0);
14268 }
14269 
14270 static void
14271 ill_dl_down(ill_t *ill)
14272 {
14273 	/*
14274 	 * The ill is down; unbind but stay attached since we're still
14275 	 * associated with a PPA. If we have negotiated DLPI capabilites
14276 	 * with the data link service provider (IDS_OK) then reset them.
14277 	 * The interval between unbinding and rebinding is potentially
14278 	 * unbounded hence we cannot assume things will be the same.
14279 	 * The DLPI capabilities will be probed again when the data link
14280 	 * is brought up.
14281 	 */
14282 	mblk_t	*mp = ill->ill_unbind_mp;
14283 
14284 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14285 
14286 	ill->ill_unbind_mp = NULL;
14287 	if (mp != NULL) {
14288 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14289 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14290 		    ill->ill_name));
14291 		mutex_enter(&ill->ill_lock);
14292 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14293 		mutex_exit(&ill->ill_lock);
14294 		/*
14295 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14296 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14297 		 * ill_capability_dld_disable disable rightaway. If this is not
14298 		 * an unplumb operation then the disable happens on receipt of
14299 		 * the capab ack via ip_rput_dlpi_writer ->
14300 		 * ill_capability_ack_thr. In both cases the order of
14301 		 * the operations seen by DLD is capability disable followed
14302 		 * by DL_UNBIND. Also the DLD capability disable needs a
14303 		 * cv_wait'able context.
14304 		 */
14305 		if (ill->ill_state_flags & ILL_CONDEMNED)
14306 			ill_capability_dld_disable(ill);
14307 		ill_capability_reset(ill, B_FALSE);
14308 		ill_dlpi_send(ill, mp);
14309 	}
14310 
14311 	/*
14312 	 * Toss all of our multicast memberships.  We could keep them, but
14313 	 * then we'd have to do bookkeeping of any joins and leaves performed
14314 	 * by the application while the the interface is down (we can't just
14315 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14316 	 * on a downed interface).
14317 	 */
14318 	ill_leave_multicast(ill);
14319 
14320 	mutex_enter(&ill->ill_lock);
14321 	ill->ill_dl_up = 0;
14322 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14323 	mutex_exit(&ill->ill_lock);
14324 }
14325 
14326 static void
14327 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14328 {
14329 	union DL_primitives *dlp;
14330 	t_uscalar_t prim;
14331 	boolean_t waitack = B_FALSE;
14332 
14333 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14334 
14335 	dlp = (union DL_primitives *)mp->b_rptr;
14336 	prim = dlp->dl_primitive;
14337 
14338 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14339 	    dl_primstr(prim), prim, ill->ill_name));
14340 
14341 	switch (prim) {
14342 	case DL_PHYS_ADDR_REQ:
14343 	{
14344 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14345 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14346 		break;
14347 	}
14348 	case DL_BIND_REQ:
14349 		mutex_enter(&ill->ill_lock);
14350 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14351 		mutex_exit(&ill->ill_lock);
14352 		break;
14353 	}
14354 
14355 	/*
14356 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14357 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14358 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14359 	 */
14360 	mutex_enter(&ill->ill_lock);
14361 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
14362 	    (prim == DL_UNBIND_REQ)) {
14363 		ill->ill_dlpi_pending = prim;
14364 		waitack = B_TRUE;
14365 	}
14366 
14367 	mutex_exit(&ill->ill_lock);
14368 	putnext(ill->ill_wq, mp);
14369 
14370 	/*
14371 	 * There is no ack for DL_NOTIFY_CONF messages
14372 	 */
14373 	if (waitack && prim == DL_NOTIFY_CONF)
14374 		ill_dlpi_done(ill, prim);
14375 }
14376 
14377 /*
14378  * Helper function for ill_dlpi_send().
14379  */
14380 /* ARGSUSED */
14381 static void
14382 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14383 {
14384 	ill_dlpi_send(q->q_ptr, mp);
14385 }
14386 
14387 /*
14388  * Send a DLPI control message to the driver but make sure there
14389  * is only one outstanding message. Uses ill_dlpi_pending to tell
14390  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14391  * when an ACK or a NAK is received to process the next queued message.
14392  */
14393 void
14394 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14395 {
14396 	mblk_t **mpp;
14397 
14398 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14399 
14400 	/*
14401 	 * To ensure that any DLPI requests for current exclusive operation
14402 	 * are always completely sent before any DLPI messages for other
14403 	 * operations, require writer access before enqueuing.
14404 	 */
14405 	if (!IAM_WRITER_ILL(ill)) {
14406 		ill_refhold(ill);
14407 		/* qwriter_ip() does the ill_refrele() */
14408 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14409 		    NEW_OP, B_TRUE);
14410 		return;
14411 	}
14412 
14413 	mutex_enter(&ill->ill_lock);
14414 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14415 		/* Must queue message. Tail insertion */
14416 		mpp = &ill->ill_dlpi_deferred;
14417 		while (*mpp != NULL)
14418 			mpp = &((*mpp)->b_next);
14419 
14420 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14421 		    ill->ill_name));
14422 
14423 		*mpp = mp;
14424 		mutex_exit(&ill->ill_lock);
14425 		return;
14426 	}
14427 	mutex_exit(&ill->ill_lock);
14428 	ill_dlpi_dispatch(ill, mp);
14429 }
14430 
14431 static void
14432 ill_capability_send(ill_t *ill, mblk_t *mp)
14433 {
14434 	ill->ill_capab_pending_cnt++;
14435 	ill_dlpi_send(ill, mp);
14436 }
14437 
14438 void
14439 ill_capability_done(ill_t *ill)
14440 {
14441 	ASSERT(ill->ill_capab_pending_cnt != 0);
14442 
14443 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14444 
14445 	ill->ill_capab_pending_cnt--;
14446 	if (ill->ill_capab_pending_cnt == 0 &&
14447 	    ill->ill_dlpi_capab_state == IDCS_OK)
14448 		ill_capability_reset_alloc(ill);
14449 }
14450 
14451 /*
14452  * Send all deferred DLPI messages without waiting for their ACKs.
14453  */
14454 void
14455 ill_dlpi_send_deferred(ill_t *ill)
14456 {
14457 	mblk_t *mp, *nextmp;
14458 
14459 	/*
14460 	 * Clear ill_dlpi_pending so that the message is not queued in
14461 	 * ill_dlpi_send().
14462 	 */
14463 	mutex_enter(&ill->ill_lock);
14464 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14465 	mp = ill->ill_dlpi_deferred;
14466 	ill->ill_dlpi_deferred = NULL;
14467 	mutex_exit(&ill->ill_lock);
14468 
14469 	for (; mp != NULL; mp = nextmp) {
14470 		nextmp = mp->b_next;
14471 		mp->b_next = NULL;
14472 		ill_dlpi_send(ill, mp);
14473 	}
14474 }
14475 
14476 /*
14477  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14478  */
14479 boolean_t
14480 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14481 {
14482 	t_uscalar_t pending;
14483 
14484 	mutex_enter(&ill->ill_lock);
14485 	if (ill->ill_dlpi_pending == prim) {
14486 		mutex_exit(&ill->ill_lock);
14487 		return (B_TRUE);
14488 	}
14489 
14490 	/*
14491 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14492 	 * without waiting, so don't print any warnings in that case.
14493 	 */
14494 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14495 		mutex_exit(&ill->ill_lock);
14496 		return (B_FALSE);
14497 	}
14498 	pending = ill->ill_dlpi_pending;
14499 	mutex_exit(&ill->ill_lock);
14500 
14501 	if (pending == DL_PRIM_INVAL) {
14502 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14503 		    "received unsolicited ack for %s on %s\n",
14504 		    dl_primstr(prim), ill->ill_name);
14505 	} else {
14506 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14507 		    "received unexpected ack for %s on %s (expecting %s)\n",
14508 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14509 	}
14510 	return (B_FALSE);
14511 }
14512 
14513 /*
14514  * Complete the current DLPI operation associated with `prim' on `ill' and
14515  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14516  * operations and the ill's current exclusive IPSQ operation has finished
14517  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14518  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14519  * the comments above ipsq_current_finish() for details.
14520  */
14521 void
14522 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14523 {
14524 	mblk_t *mp;
14525 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14526 	ipxop_t *ipx = ipsq->ipsq_xop;
14527 
14528 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14529 	mutex_enter(&ill->ill_lock);
14530 
14531 	ASSERT(prim != DL_PRIM_INVAL);
14532 	ASSERT(ill->ill_dlpi_pending == prim);
14533 
14534 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14535 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14536 
14537 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14538 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14539 		if (ipx->ipx_current_done) {
14540 			mutex_enter(&ipx->ipx_lock);
14541 			ipx->ipx_current_ipif = NULL;
14542 			mutex_exit(&ipx->ipx_lock);
14543 		}
14544 		cv_signal(&ill->ill_cv);
14545 		mutex_exit(&ill->ill_lock);
14546 		return;
14547 	}
14548 
14549 	ill->ill_dlpi_deferred = mp->b_next;
14550 	mp->b_next = NULL;
14551 	mutex_exit(&ill->ill_lock);
14552 
14553 	ill_dlpi_dispatch(ill, mp);
14554 }
14555 
14556 void
14557 conn_delete_ire(conn_t *connp, caddr_t arg)
14558 {
14559 	ipif_t	*ipif = (ipif_t *)arg;
14560 	ire_t	*ire;
14561 
14562 	/*
14563 	 * Look at the cached ires on conns which has pointers to ipifs.
14564 	 * We just call ire_refrele which clears up the reference
14565 	 * to ire. Called when a conn closes. Also called from ipif_free
14566 	 * to cleanup indirect references to the stale ipif via the cached ire.
14567 	 */
14568 	mutex_enter(&connp->conn_lock);
14569 	ire = connp->conn_ire_cache;
14570 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14571 		connp->conn_ire_cache = NULL;
14572 		mutex_exit(&connp->conn_lock);
14573 		IRE_REFRELE_NOTR(ire);
14574 		return;
14575 	}
14576 	mutex_exit(&connp->conn_lock);
14577 
14578 }
14579 
14580 /*
14581  * Some operations (e.g., ipif_down()) conditionally delete a number
14582  * of IREs. Those IREs may have been previously cached in the conn structure.
14583  * This ipcl_walk() walker function releases all references to such IREs based
14584  * on the condemned flag.
14585  */
14586 /* ARGSUSED */
14587 void
14588 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14589 {
14590 	ire_t	*ire;
14591 
14592 	mutex_enter(&connp->conn_lock);
14593 	ire = connp->conn_ire_cache;
14594 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14595 		connp->conn_ire_cache = NULL;
14596 		mutex_exit(&connp->conn_lock);
14597 		IRE_REFRELE_NOTR(ire);
14598 		return;
14599 	}
14600 	mutex_exit(&connp->conn_lock);
14601 }
14602 
14603 /*
14604  * Take down a specific interface, but don't lose any information about it.
14605  * (Always called as writer.)
14606  * This function goes through the down sequence even if the interface is
14607  * already down. There are 2 reasons.
14608  * a. Currently we permit interface routes that depend on down interfaces
14609  *    to be added. This behaviour itself is questionable. However it appears
14610  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14611  *    time. We go thru the cleanup in order to remove these routes.
14612  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14613  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14614  *    down, but we need to cleanup i.e. do ill_dl_down and
14615  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14616  *
14617  * IP-MT notes:
14618  *
14619  * Model of reference to interfaces.
14620  *
14621  * The following members in ipif_t track references to the ipif.
14622  *	int     ipif_refcnt;    Active reference count
14623  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14624  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14625  *
14626  * The following members in ill_t track references to the ill.
14627  *	int             ill_refcnt;     active refcnt
14628  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14629  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14630  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14631  *
14632  * Reference to an ipif or ill can be obtained in any of the following ways.
14633  *
14634  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14635  * Pointers to ipif / ill from other data structures viz ire and conn.
14636  * Implicit reference to the ipif / ill by holding a reference to the ire.
14637  *
14638  * The ipif/ill lookup functions return a reference held ipif / ill.
14639  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14640  * This is a purely dynamic reference count associated with threads holding
14641  * references to the ipif / ill. Pointers from other structures do not
14642  * count towards this reference count.
14643  *
14644  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14645  * associated with the ipif/ill. This is incremented whenever a new
14646  * ire is created referencing the ipif/ill. This is done atomically inside
14647  * ire_add_v[46] where the ire is actually added to the ire hash table.
14648  * The count is decremented in ire_inactive where the ire is destroyed.
14649  *
14650  * nce's reference ill's thru nce_ill and the count of nce's associated with
14651  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14652  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14653  * table. Similarly it is decremented in ndp_inactive() where the nce
14654  * is destroyed.
14655  *
14656  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14657  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14658  * in ilm_walker_cleanup() or ilm_delete().
14659  *
14660  * Flow of ioctls involving interface down/up
14661  *
14662  * The following is the sequence of an attempt to set some critical flags on an
14663  * up interface.
14664  * ip_sioctl_flags
14665  * ipif_down
14666  * wait for ipif to be quiescent
14667  * ipif_down_tail
14668  * ip_sioctl_flags_tail
14669  *
14670  * All set ioctls that involve down/up sequence would have a skeleton similar
14671  * to the above. All the *tail functions are called after the refcounts have
14672  * dropped to the appropriate values.
14673  *
14674  * The mechanism to quiesce an ipif is as follows.
14675  *
14676  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14677  * on the ipif. Callers either pass a flag requesting wait or the lookup
14678  *  functions will return NULL.
14679  *
14680  * Delete all ires referencing this ipif
14681  *
14682  * Any thread attempting to do an ipif_refhold on an ipif that has been
14683  * obtained thru a cached pointer will first make sure that
14684  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14685  * increment the refcount.
14686  *
14687  * The above guarantees that the ipif refcount will eventually come down to
14688  * zero and the ipif will quiesce, once all threads that currently hold a
14689  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14690  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14691  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14692  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14693  * in ip.h
14694  *
14695  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14696  *
14697  * Threads trying to lookup an ipif or ill can pass a flag requesting
14698  * wait and restart if the ipif / ill cannot be looked up currently.
14699  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14700  * failure if the ipif is currently undergoing an exclusive operation, and
14701  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14702  * is restarted by ipsq_exit() when the current exclusive operation completes.
14703  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14704  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14705  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14706  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14707  * until we release the ipsq_lock, even though the the ill/ipif state flags
14708  * can change after we drop the ill_lock.
14709  *
14710  * An attempt to send out a packet using an ipif that is currently
14711  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14712  * operation and restart it later when the exclusive condition on the ipif ends.
14713  * This is an example of not passing the wait flag to the lookup functions. For
14714  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14715  * out a multicast packet on that ipif will fail while the ipif is
14716  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14717  * currently IPIF_CHANGING will also fail.
14718  */
14719 int
14720 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14721 {
14722 	ill_t		*ill = ipif->ipif_ill;
14723 	conn_t		*connp;
14724 	boolean_t	success;
14725 	boolean_t	ipif_was_up = B_FALSE;
14726 	ip_stack_t	*ipst = ill->ill_ipst;
14727 
14728 	ASSERT(IAM_WRITER_IPIF(ipif));
14729 
14730 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14731 
14732 	if (ipif->ipif_flags & IPIF_UP) {
14733 		mutex_enter(&ill->ill_lock);
14734 		ipif->ipif_flags &= ~IPIF_UP;
14735 		ASSERT(ill->ill_ipif_up_count > 0);
14736 		--ill->ill_ipif_up_count;
14737 		mutex_exit(&ill->ill_lock);
14738 		ipif_was_up = B_TRUE;
14739 		/* Update status in SCTP's list */
14740 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14741 		ill_nic_event_dispatch(ipif->ipif_ill,
14742 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14743 	}
14744 
14745 	/*
14746 	 * Blow away memberships we established in ipif_multicast_up().
14747 	 */
14748 	ipif_multicast_down(ipif);
14749 
14750 	/*
14751 	 * Remove from the mapping for __sin6_src_id. We insert only
14752 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14753 	 * stored as mapped addresses, we need to check for mapped
14754 	 * INADDR_ANY also.
14755 	 */
14756 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14757 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14758 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14759 		int err;
14760 
14761 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14762 		    ipif->ipif_zoneid, ipst);
14763 		if (err != 0) {
14764 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14765 		}
14766 	}
14767 
14768 	/*
14769 	 * Delete all IRE's pointing at this ipif or its source address.
14770 	 */
14771 	if (ipif->ipif_isv6) {
14772 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14773 		    ipst);
14774 	} else {
14775 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14776 		    ipst);
14777 	}
14778 
14779 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14780 		/*
14781 		 * Since the interface is now down, it may have just become
14782 		 * inactive.  Note that this needs to be done even for a
14783 		 * lll_logical_down(), or ARP entries will not get correctly
14784 		 * restored when the interface comes back up.
14785 		 */
14786 		if (IS_UNDER_IPMP(ill))
14787 			ipmp_ill_refresh_active(ill);
14788 	}
14789 
14790 	/*
14791 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14792 	 * ires have been deleted above. Otherwise a thread could end up
14793 	 * caching an ire in a conn after we have finished the cleanup of the
14794 	 * conn. The caching is done after making sure that the ire is not yet
14795 	 * condemned. Also documented in the block comment above ip_output
14796 	 */
14797 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14798 	/* Also, delete the ires cached in SCTP */
14799 	sctp_ire_cache_flush(ipif);
14800 
14801 	/*
14802 	 * Update any other ipifs which have used "our" local address as
14803 	 * a source address. This entails removing and recreating IRE_INTERFACE
14804 	 * entries for such ipifs.
14805 	 */
14806 	if (ipif->ipif_isv6)
14807 		ipif_update_other_ipifs_v6(ipif);
14808 	else
14809 		ipif_update_other_ipifs(ipif);
14810 
14811 	/*
14812 	 * neighbor-discovery or arp entries for this interface.
14813 	 */
14814 	ipif_ndp_down(ipif);
14815 
14816 	/*
14817 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14818 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14819 	 * and ill_delete -> ipif_free -> ipif_down
14820 	 */
14821 	if (mp == NULL) {
14822 		ASSERT(q == NULL);
14823 		return (0);
14824 	}
14825 
14826 	if (CONN_Q(q)) {
14827 		connp = Q_TO_CONN(q);
14828 		mutex_enter(&connp->conn_lock);
14829 	} else {
14830 		connp = NULL;
14831 	}
14832 	mutex_enter(&ill->ill_lock);
14833 	/*
14834 	 * Are there any ire's pointing to this ipif that are still active ?
14835 	 * If this is the last ipif going down, are there any ire's pointing
14836 	 * to this ill that are still active ?
14837 	 */
14838 	if (ipif_is_quiescent(ipif)) {
14839 		mutex_exit(&ill->ill_lock);
14840 		if (connp != NULL)
14841 			mutex_exit(&connp->conn_lock);
14842 		return (0);
14843 	}
14844 
14845 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14846 	    ill->ill_name, (void *)ill));
14847 	/*
14848 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14849 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14850 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14851 	 */
14852 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14853 	if (!success) {
14854 		/* The conn is closing. So just return */
14855 		ASSERT(connp != NULL);
14856 		mutex_exit(&ill->ill_lock);
14857 		mutex_exit(&connp->conn_lock);
14858 		return (EINTR);
14859 	}
14860 
14861 	mutex_exit(&ill->ill_lock);
14862 	if (connp != NULL)
14863 		mutex_exit(&connp->conn_lock);
14864 	return (EINPROGRESS);
14865 }
14866 
14867 void
14868 ipif_down_tail(ipif_t *ipif)
14869 {
14870 	ill_t	*ill = ipif->ipif_ill;
14871 
14872 	/*
14873 	 * Skip any loopback interface (null wq).
14874 	 * If this is the last logical interface on the ill
14875 	 * have ill_dl_down tell the driver we are gone (unbind)
14876 	 * Note that lun 0 can ipif_down even though
14877 	 * there are other logical units that are up.
14878 	 * This occurs e.g. when we change a "significant" IFF_ flag.
14879 	 */
14880 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
14881 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
14882 	    ill->ill_dl_up) {
14883 		ill_dl_down(ill);
14884 	}
14885 	ill->ill_logical_down = 0;
14886 
14887 	/*
14888 	 * Has to be after removing the routes in ipif_down_delete_ire.
14889 	 */
14890 	ipif_resolver_down(ipif);
14891 
14892 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
14893 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
14894 }
14895 
14896 /*
14897  * Bring interface logically down without bringing the physical interface
14898  * down e.g. when the netmask is changed. This avoids long lasting link
14899  * negotiations between an ethernet interface and a certain switches.
14900  */
14901 static int
14902 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14903 {
14904 	/*
14905 	 * The ill_logical_down flag is a transient flag. It is set here
14906 	 * and is cleared once the down has completed in ipif_down_tail.
14907 	 * This flag does not indicate whether the ill stream is in the
14908 	 * DL_BOUND state with the driver. Instead this flag is used by
14909 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
14910 	 * the driver. The state of the ill stream i.e. whether it is
14911 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
14912 	 */
14913 	ipif->ipif_ill->ill_logical_down = 1;
14914 	return (ipif_down(ipif, q, mp));
14915 }
14916 
14917 /*
14918  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
14919  * If the usesrc client ILL is already part of a usesrc group or not,
14920  * in either case a ire_stq with the matching usesrc client ILL will
14921  * locate the IRE's that need to be deleted. We want IREs to be created
14922  * with the new source address.
14923  */
14924 static void
14925 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
14926 {
14927 	ill_t	*ucill = (ill_t *)ill_arg;
14928 
14929 	ASSERT(IAM_WRITER_ILL(ucill));
14930 
14931 	if (ire->ire_stq == NULL)
14932 		return;
14933 
14934 	if ((ire->ire_type == IRE_CACHE) &&
14935 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
14936 		ire_delete(ire);
14937 }
14938 
14939 /*
14940  * ire_walk routine to delete every IRE dependent on the interface
14941  * address that is going down.	(Always called as writer.)
14942  * Works for both v4 and v6.
14943  * In addition for checking for ire_ipif matches it also checks for
14944  * IRE_CACHE entries which have the same source address as the
14945  * disappearing ipif since ipif_select_source might have picked
14946  * that source. Note that ipif_down/ipif_update_other_ipifs takes
14947  * care of any IRE_INTERFACE with the disappearing source address.
14948  */
14949 static void
14950 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
14951 {
14952 	ipif_t	*ipif = (ipif_t *)ipif_arg;
14953 
14954 	ASSERT(IAM_WRITER_IPIF(ipif));
14955 	if (ire->ire_ipif == NULL)
14956 		return;
14957 
14958 	if (ire->ire_ipif != ipif) {
14959 		/*
14960 		 * Look for a matching source address.
14961 		 */
14962 		if (ire->ire_type != IRE_CACHE)
14963 			return;
14964 		if (ipif->ipif_flags & IPIF_NOLOCAL)
14965 			return;
14966 
14967 		if (ire->ire_ipversion == IPV4_VERSION) {
14968 			if (ire->ire_src_addr != ipif->ipif_src_addr)
14969 				return;
14970 		} else {
14971 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
14972 			    &ipif->ipif_v6lcl_addr))
14973 				return;
14974 		}
14975 		ire_delete(ire);
14976 		return;
14977 	}
14978 	/*
14979 	 * ire_delete() will do an ire_flush_cache which will delete
14980 	 * all ire_ipif matches
14981 	 */
14982 	ire_delete(ire);
14983 }
14984 
14985 /*
14986  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
14987  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
14988  * 2) when an interface is brought up or down (on that ill).
14989  * This ensures that the IRE_CACHE entries don't retain stale source
14990  * address selection results.
14991  */
14992 void
14993 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
14994 {
14995 	ill_t	*ill = (ill_t *)ill_arg;
14996 
14997 	ASSERT(IAM_WRITER_ILL(ill));
14998 	ASSERT(ire->ire_type == IRE_CACHE);
14999 
15000 	/*
15001 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15002 	 * ill, but we only want to delete the IRE if ire_ipif matches.
15003 	 */
15004 	ASSERT(ire->ire_ipif != NULL);
15005 	if (ill == ire->ire_ipif->ipif_ill)
15006 		ire_delete(ire);
15007 }
15008 
15009 /*
15010  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
15011  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
15012  * the IPMP ill.
15013  */
15014 void
15015 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
15016 {
15017 	ill_t	*ill = (ill_t *)ill_arg;
15018 
15019 	ASSERT(IAM_WRITER_ILL(ill));
15020 	ASSERT(ire->ire_type == IRE_CACHE);
15021 
15022 	/*
15023 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15024 	 * ill, but we only want to delete the IRE if ire_stq matches.
15025 	 */
15026 	if (ire->ire_stq->q_ptr == ill_arg)
15027 		ire_delete(ire);
15028 }
15029 
15030 /*
15031  * Delete all the IREs whose ire_stq's reference any ill in the same IPMP
15032  * group as `ill_arg'.  Used by ipmp_ill_deactivate() to flush all IRE_CACHE
15033  * entries for the illgrp.
15034  */
15035 void
15036 ill_grp_cache_delete(ire_t *ire, char *ill_arg)
15037 {
15038 	ill_t	*ill = (ill_t *)ill_arg;
15039 
15040 	ASSERT(IAM_WRITER_ILL(ill));
15041 
15042 	if (ire->ire_type == IRE_CACHE &&
15043 	    IS_IN_SAME_ILLGRP((ill_t *)ire->ire_stq->q_ptr, ill)) {
15044 		ire_delete(ire);
15045 	}
15046 }
15047 
15048 /*
15049  * Delete all broadcast IREs with a source address on `ill_arg'.
15050  */
15051 static void
15052 ill_broadcast_delete(ire_t *ire, char *ill_arg)
15053 {
15054 	ill_t *ill = (ill_t *)ill_arg;
15055 
15056 	ASSERT(IAM_WRITER_ILL(ill));
15057 	ASSERT(ire->ire_type == IRE_BROADCAST);
15058 
15059 	if (ire->ire_ipif->ipif_ill == ill)
15060 		ire_delete(ire);
15061 }
15062 
15063 /*
15064  * Initiate deallocate of an IPIF. Always called as writer. Called by
15065  * ill_delete or ip_sioctl_removeif.
15066  */
15067 static void
15068 ipif_free(ipif_t *ipif)
15069 {
15070 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15071 
15072 	ASSERT(IAM_WRITER_IPIF(ipif));
15073 
15074 	if (ipif->ipif_recovery_id != 0)
15075 		(void) untimeout(ipif->ipif_recovery_id);
15076 	ipif->ipif_recovery_id = 0;
15077 
15078 	/* Remove conn references */
15079 	reset_conn_ipif(ipif);
15080 
15081 	/*
15082 	 * Make sure we have valid net and subnet broadcast ire's for the
15083 	 * other ipif's which share them with this ipif.
15084 	 */
15085 	if (!ipif->ipif_isv6)
15086 		ipif_check_bcast_ires(ipif);
15087 
15088 	/*
15089 	 * Take down the interface. We can be called either from ill_delete
15090 	 * or from ip_sioctl_removeif.
15091 	 */
15092 	(void) ipif_down(ipif, NULL, NULL);
15093 
15094 	/*
15095 	 * Now that the interface is down, there's no chance it can still
15096 	 * become a duplicate.  Cancel any timer that may have been set while
15097 	 * tearing down.
15098 	 */
15099 	if (ipif->ipif_recovery_id != 0)
15100 		(void) untimeout(ipif->ipif_recovery_id);
15101 	ipif->ipif_recovery_id = 0;
15102 
15103 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15104 	/* Remove pointers to this ill in the multicast routing tables */
15105 	reset_mrt_vif_ipif(ipif);
15106 	/* If necessary, clear the cached source ipif rotor. */
15107 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15108 		ipif->ipif_ill->ill_src_ipif = NULL;
15109 	rw_exit(&ipst->ips_ill_g_lock);
15110 }
15111 
15112 static void
15113 ipif_free_tail(ipif_t *ipif)
15114 {
15115 	mblk_t	*mp;
15116 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15117 
15118 	/*
15119 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15120 	 */
15121 	mutex_enter(&ipif->ipif_saved_ire_lock);
15122 	mp = ipif->ipif_saved_ire_mp;
15123 	ipif->ipif_saved_ire_mp = NULL;
15124 	mutex_exit(&ipif->ipif_saved_ire_lock);
15125 	freemsg(mp);
15126 
15127 	/*
15128 	 * Need to hold both ill_g_lock and ill_lock while
15129 	 * inserting or removing an ipif from the linked list
15130 	 * of ipifs hanging off the ill.
15131 	 */
15132 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15133 
15134 	ASSERT(ilm_walk_ipif(ipif) == 0);
15135 
15136 #ifdef DEBUG
15137 	ipif_trace_cleanup(ipif);
15138 #endif
15139 
15140 	/* Ask SCTP to take it out of it list */
15141 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15142 
15143 	/* Get it out of the ILL interface list. */
15144 	ipif_remove(ipif);
15145 	rw_exit(&ipst->ips_ill_g_lock);
15146 
15147 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15148 
15149 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15150 	ASSERT(ipif->ipif_recovery_id == 0);
15151 
15152 	/* Free the memory. */
15153 	mi_free(ipif);
15154 }
15155 
15156 /*
15157  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15158  * is zero.
15159  */
15160 void
15161 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15162 {
15163 	char	lbuf[LIFNAMSIZ];
15164 	char	*name;
15165 	size_t	name_len;
15166 
15167 	buf[0] = '\0';
15168 	name = ipif->ipif_ill->ill_name;
15169 	name_len = ipif->ipif_ill->ill_name_length;
15170 	if (ipif->ipif_id != 0) {
15171 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15172 		    ipif->ipif_id);
15173 		name = lbuf;
15174 		name_len = mi_strlen(name) + 1;
15175 	}
15176 	len -= 1;
15177 	buf[len] = '\0';
15178 	len = MIN(len, name_len);
15179 	bcopy(name, buf, len);
15180 }
15181 
15182 /*
15183  * Find an IPIF based on the name passed in.  Names can be of the
15184  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15185  * The <phys> string can have forms like <dev><#> (e.g., le0),
15186  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15187  * When there is no colon, the implied unit id is zero. <phys> must
15188  * correspond to the name of an ILL.  (May be called as writer.)
15189  */
15190 static ipif_t *
15191 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15192     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15193     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15194 {
15195 	char	*cp;
15196 	char	*endp;
15197 	long	id;
15198 	ill_t	*ill;
15199 	ipif_t	*ipif;
15200 	uint_t	ire_type;
15201 	boolean_t did_alloc = B_FALSE;
15202 	ipsq_t	*ipsq;
15203 
15204 	if (error != NULL)
15205 		*error = 0;
15206 
15207 	/*
15208 	 * If the caller wants to us to create the ipif, make sure we have a
15209 	 * valid zoneid
15210 	 */
15211 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15212 
15213 	if (namelen == 0) {
15214 		if (error != NULL)
15215 			*error = ENXIO;
15216 		return (NULL);
15217 	}
15218 
15219 	*exists = B_FALSE;
15220 	/* Look for a colon in the name. */
15221 	endp = &name[namelen];
15222 	for (cp = endp; --cp > name; ) {
15223 		if (*cp == IPIF_SEPARATOR_CHAR)
15224 			break;
15225 	}
15226 
15227 	if (*cp == IPIF_SEPARATOR_CHAR) {
15228 		/*
15229 		 * Reject any non-decimal aliases for logical
15230 		 * interfaces. Aliases with leading zeroes
15231 		 * are also rejected as they introduce ambiguity
15232 		 * in the naming of the interfaces.
15233 		 * In order to confirm with existing semantics,
15234 		 * and to not break any programs/script relying
15235 		 * on that behaviour, if<0>:0 is considered to be
15236 		 * a valid interface.
15237 		 *
15238 		 * If alias has two or more digits and the first
15239 		 * is zero, fail.
15240 		 */
15241 		if (&cp[2] < endp && cp[1] == '0') {
15242 			if (error != NULL)
15243 				*error = EINVAL;
15244 			return (NULL);
15245 		}
15246 	}
15247 
15248 	if (cp <= name) {
15249 		cp = endp;
15250 	} else {
15251 		*cp = '\0';
15252 	}
15253 
15254 	/*
15255 	 * Look up the ILL, based on the portion of the name
15256 	 * before the slash. ill_lookup_on_name returns a held ill.
15257 	 * Temporary to check whether ill exists already. If so
15258 	 * ill_lookup_on_name will clear it.
15259 	 */
15260 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15261 	    q, mp, func, error, &did_alloc, ipst);
15262 	if (cp != endp)
15263 		*cp = IPIF_SEPARATOR_CHAR;
15264 	if (ill == NULL)
15265 		return (NULL);
15266 
15267 	/* Establish the unit number in the name. */
15268 	id = 0;
15269 	if (cp < endp && *endp == '\0') {
15270 		/* If there was a colon, the unit number follows. */
15271 		cp++;
15272 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15273 			ill_refrele(ill);
15274 			if (error != NULL)
15275 				*error = ENXIO;
15276 			return (NULL);
15277 		}
15278 	}
15279 
15280 	GRAB_CONN_LOCK(q);
15281 	mutex_enter(&ill->ill_lock);
15282 	/* Now see if there is an IPIF with this unit number. */
15283 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15284 		if (ipif->ipif_id == id) {
15285 			if (zoneid != ALL_ZONES &&
15286 			    zoneid != ipif->ipif_zoneid &&
15287 			    ipif->ipif_zoneid != ALL_ZONES) {
15288 				mutex_exit(&ill->ill_lock);
15289 				RELEASE_CONN_LOCK(q);
15290 				ill_refrele(ill);
15291 				if (error != NULL)
15292 					*error = ENXIO;
15293 				return (NULL);
15294 			}
15295 			/*
15296 			 * The block comment at the start of ipif_down
15297 			 * explains the use of the macros used below
15298 			 */
15299 			if (IPIF_CAN_LOOKUP(ipif)) {
15300 				ipif_refhold_locked(ipif);
15301 				mutex_exit(&ill->ill_lock);
15302 				if (!did_alloc)
15303 					*exists = B_TRUE;
15304 				/*
15305 				 * Drop locks before calling ill_refrele
15306 				 * since it can potentially call into
15307 				 * ipif_ill_refrele_tail which can end up
15308 				 * in trying to acquire any lock.
15309 				 */
15310 				RELEASE_CONN_LOCK(q);
15311 				ill_refrele(ill);
15312 				return (ipif);
15313 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15314 				ipsq = ill->ill_phyint->phyint_ipsq;
15315 				mutex_enter(&ipsq->ipsq_lock);
15316 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15317 				mutex_exit(&ill->ill_lock);
15318 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15319 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15320 				mutex_exit(&ipsq->ipsq_lock);
15321 				RELEASE_CONN_LOCK(q);
15322 				ill_refrele(ill);
15323 				if (error != NULL)
15324 					*error = EINPROGRESS;
15325 				return (NULL);
15326 			}
15327 		}
15328 	}
15329 	RELEASE_CONN_LOCK(q);
15330 
15331 	if (!do_alloc) {
15332 		mutex_exit(&ill->ill_lock);
15333 		ill_refrele(ill);
15334 		if (error != NULL)
15335 			*error = ENXIO;
15336 		return (NULL);
15337 	}
15338 
15339 	/*
15340 	 * If none found, atomically allocate and return a new one.
15341 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15342 	 * to support "receive only" use of lo0:1 etc. as is still done
15343 	 * below as an initial guess.
15344 	 * However, this is now likely to be overriden later in ipif_up_done()
15345 	 * when we know for sure what address has been configured on the
15346 	 * interface, since we might have more than one loopback interface
15347 	 * with a loopback address, e.g. in the case of zones, and all the
15348 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15349 	 */
15350 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15351 		ire_type = IRE_LOOPBACK;
15352 	else
15353 		ire_type = IRE_LOCAL;
15354 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15355 	if (ipif != NULL)
15356 		ipif_refhold_locked(ipif);
15357 	else if (error != NULL)
15358 		*error = ENOMEM;
15359 	mutex_exit(&ill->ill_lock);
15360 	ill_refrele(ill);
15361 	return (ipif);
15362 }
15363 
15364 /*
15365  * This routine is called whenever a new address comes up on an ipif.  If
15366  * we are configured to respond to address mask requests, then we are supposed
15367  * to broadcast an address mask reply at this time.  This routine is also
15368  * called if we are already up, but a netmask change is made.  This is legal
15369  * but might not make the system manager very popular.	(May be called
15370  * as writer.)
15371  */
15372 void
15373 ipif_mask_reply(ipif_t *ipif)
15374 {
15375 	icmph_t	*icmph;
15376 	ipha_t	*ipha;
15377 	mblk_t	*mp;
15378 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15379 
15380 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15381 
15382 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15383 		return;
15384 
15385 	/* ICMP mask reply is IPv4 only */
15386 	ASSERT(!ipif->ipif_isv6);
15387 	/* ICMP mask reply is not for a loopback interface */
15388 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15389 
15390 	mp = allocb(REPLY_LEN, BPRI_HI);
15391 	if (mp == NULL)
15392 		return;
15393 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15394 
15395 	ipha = (ipha_t *)mp->b_rptr;
15396 	bzero(ipha, REPLY_LEN);
15397 	*ipha = icmp_ipha;
15398 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15399 	ipha->ipha_src = ipif->ipif_src_addr;
15400 	ipha->ipha_dst = ipif->ipif_brd_addr;
15401 	ipha->ipha_length = htons(REPLY_LEN);
15402 	ipha->ipha_ident = 0;
15403 
15404 	icmph = (icmph_t *)&ipha[1];
15405 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15406 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15407 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15408 
15409 	put(ipif->ipif_wq, mp);
15410 
15411 #undef	REPLY_LEN
15412 }
15413 
15414 /*
15415  * When the mtu in the ipif changes, we call this routine through ire_walk
15416  * to update all the relevant IREs.
15417  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15418  */
15419 static void
15420 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15421 {
15422 	ipif_t *ipif = (ipif_t *)ipif_arg;
15423 
15424 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15425 		return;
15426 
15427 	mutex_enter(&ire->ire_lock);
15428 	if (ire->ire_marks & IRE_MARK_PMTU) {
15429 		/* Avoid increasing the PMTU */
15430 		ire->ire_max_frag = MIN(ipif->ipif_mtu, ire->ire_max_frag);
15431 		if (ire->ire_max_frag == ipif->ipif_mtu)
15432 			ire->ire_marks &= ~IRE_MARK_PMTU;
15433 	} else {
15434 		ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15435 	}
15436 	mutex_exit(&ire->ire_lock);
15437 }
15438 
15439 /*
15440  * When the mtu in the ill changes, we call this routine through ire_walk
15441  * to update all the relevant IREs.
15442  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15443  */
15444 void
15445 ill_mtu_change(ire_t *ire, char *ill_arg)
15446 {
15447 	ill_t	*ill = (ill_t *)ill_arg;
15448 
15449 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15450 		return;
15451 
15452 	mutex_enter(&ire->ire_lock);
15453 	if (ire->ire_marks & IRE_MARK_PMTU) {
15454 		/* Avoid increasing the PMTU */
15455 		ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
15456 		    ire->ire_max_frag);
15457 		if (ire->ire_max_frag == ire->ire_ipif->ipif_mtu) {
15458 			ire->ire_marks &= ~IRE_MARK_PMTU;
15459 		}
15460 	} else {
15461 		ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu, IP_MAXPACKET);
15462 	}
15463 	mutex_exit(&ire->ire_lock);
15464 }
15465 
15466 /*
15467  * Join the ipif specific multicast groups.
15468  * Must be called after a mapping has been set up in the resolver.  (Always
15469  * called as writer.)
15470  */
15471 void
15472 ipif_multicast_up(ipif_t *ipif)
15473 {
15474 	int err;
15475 	ill_t *ill;
15476 
15477 	ASSERT(IAM_WRITER_IPIF(ipif));
15478 
15479 	ill = ipif->ipif_ill;
15480 
15481 	ip1dbg(("ipif_multicast_up\n"));
15482 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15483 		return;
15484 
15485 	if (ipif->ipif_isv6) {
15486 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15487 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15488 
15489 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15490 
15491 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15492 			return;
15493 
15494 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15495 
15496 		/*
15497 		 * Join the all hosts multicast address.  We skip this for
15498 		 * underlying IPMP interfaces since they should be invisible.
15499 		 */
15500 		if (!IS_UNDER_IPMP(ill)) {
15501 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15502 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15503 			if (err != 0) {
15504 				ip0dbg(("ipif_multicast_up: "
15505 				    "all_hosts_mcast failed %d\n", err));
15506 				return;
15507 			}
15508 			ipif->ipif_joined_allhosts = 1;
15509 		}
15510 
15511 		/*
15512 		 * Enable multicast for the solicited node multicast address
15513 		 */
15514 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15515 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15516 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15517 			if (err != 0) {
15518 				ip0dbg(("ipif_multicast_up: solicited MC"
15519 				    " failed %d\n", err));
15520 				if (ipif->ipif_joined_allhosts) {
15521 					(void) ip_delmulti_v6(&v6allmc, ill,
15522 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15523 					ipif->ipif_joined_allhosts = 0;
15524 				}
15525 				return;
15526 			}
15527 		}
15528 	} else {
15529 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15530 			return;
15531 
15532 		/* Join the all hosts multicast address */
15533 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15534 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15535 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15536 		if (err) {
15537 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15538 			return;
15539 		}
15540 	}
15541 	ipif->ipif_multicast_up = 1;
15542 }
15543 
15544 /*
15545  * Blow away any multicast groups that we joined in ipif_multicast_up().
15546  * (Explicit memberships are blown away in ill_leave_multicast() when the
15547  * ill is brought down.)
15548  */
15549 void
15550 ipif_multicast_down(ipif_t *ipif)
15551 {
15552 	int err;
15553 
15554 	ASSERT(IAM_WRITER_IPIF(ipif));
15555 
15556 	ip1dbg(("ipif_multicast_down\n"));
15557 	if (!ipif->ipif_multicast_up)
15558 		return;
15559 
15560 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15561 
15562 	if (!ipif->ipif_isv6) {
15563 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15564 		    B_TRUE);
15565 		if (err != 0)
15566 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15567 
15568 		ipif->ipif_multicast_up = 0;
15569 		return;
15570 	}
15571 
15572 	/*
15573 	 * Leave the all-hosts multicast address.
15574 	 */
15575 	if (ipif->ipif_joined_allhosts) {
15576 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15577 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15578 		if (err != 0) {
15579 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15580 			    "failed %d\n", err));
15581 		}
15582 		ipif->ipif_joined_allhosts = 0;
15583 	}
15584 
15585 	/*
15586 	 * Disable multicast for the solicited node multicast address
15587 	 */
15588 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15589 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15590 
15591 		ipv6_multi.s6_addr32[3] |=
15592 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15593 
15594 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15595 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15596 		if (err != 0) {
15597 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15598 			    err));
15599 		}
15600 	}
15601 
15602 	ipif->ipif_multicast_up = 0;
15603 }
15604 
15605 /*
15606  * Used when an interface comes up to recreate any extra routes on this
15607  * interface.
15608  */
15609 static ire_t **
15610 ipif_recover_ire(ipif_t *ipif)
15611 {
15612 	mblk_t	*mp;
15613 	ire_t	**ipif_saved_irep;
15614 	ire_t	**irep;
15615 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15616 
15617 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15618 	    ipif->ipif_id));
15619 
15620 	mutex_enter(&ipif->ipif_saved_ire_lock);
15621 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15622 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15623 	if (ipif_saved_irep == NULL) {
15624 		mutex_exit(&ipif->ipif_saved_ire_lock);
15625 		return (NULL);
15626 	}
15627 
15628 	irep = ipif_saved_irep;
15629 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15630 		ire_t		*ire;
15631 		queue_t		*rfq;
15632 		queue_t		*stq;
15633 		ifrt_t		*ifrt;
15634 		uchar_t		*src_addr;
15635 		uchar_t		*gateway_addr;
15636 		ushort_t	type;
15637 
15638 		/*
15639 		 * When the ire was initially created and then added in
15640 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15641 		 * in the case of a traditional interface route, or as one of
15642 		 * the IRE_OFFSUBNET types (with the exception of
15643 		 * IRE_HOST types ire which is created by icmp_redirect() and
15644 		 * which we don't need to save or recover).  In the case where
15645 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15646 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15647 		 * to satisfy software like GateD and Sun Cluster which creates
15648 		 * routes using the the loopback interface's address as a
15649 		 * gateway.
15650 		 *
15651 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15652 		 * ire_create() will be called in the same way here as
15653 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15654 		 * the route looks like a traditional interface route (where
15655 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15656 		 * the saved ifrt->ifrt_type.  This means that in the case where
15657 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15658 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15659 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15660 		 */
15661 		ifrt = (ifrt_t *)mp->b_rptr;
15662 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15663 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15664 			rfq = NULL;
15665 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15666 			    ? ipif->ipif_rq : ipif->ipif_wq;
15667 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15668 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15669 			    : (uint8_t *)&ipif->ipif_src_addr;
15670 			gateway_addr = NULL;
15671 			type = ipif->ipif_net_type;
15672 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15673 			/* Recover multiroute broadcast IRE. */
15674 			rfq = ipif->ipif_rq;
15675 			stq = ipif->ipif_wq;
15676 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15677 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15678 			    : (uint8_t *)&ipif->ipif_src_addr;
15679 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15680 			type = ifrt->ifrt_type;
15681 		} else {
15682 			rfq = NULL;
15683 			stq = NULL;
15684 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15685 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15686 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15687 			type = ifrt->ifrt_type;
15688 		}
15689 
15690 		/*
15691 		 * Create a copy of the IRE with the saved address and netmask.
15692 		 */
15693 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15694 		    "0x%x/0x%x\n",
15695 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15696 		    ntohl(ifrt->ifrt_addr),
15697 		    ntohl(ifrt->ifrt_mask)));
15698 		ire = ire_create(
15699 		    (uint8_t *)&ifrt->ifrt_addr,
15700 		    (uint8_t *)&ifrt->ifrt_mask,
15701 		    src_addr,
15702 		    gateway_addr,
15703 		    &ifrt->ifrt_max_frag,
15704 		    NULL,
15705 		    rfq,
15706 		    stq,
15707 		    type,
15708 		    ipif,
15709 		    0,
15710 		    0,
15711 		    0,
15712 		    ifrt->ifrt_flags,
15713 		    &ifrt->ifrt_iulp_info,
15714 		    NULL,
15715 		    NULL,
15716 		    ipst);
15717 
15718 		if (ire == NULL) {
15719 			mutex_exit(&ipif->ipif_saved_ire_lock);
15720 			kmem_free(ipif_saved_irep,
15721 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15722 			return (NULL);
15723 		}
15724 
15725 		/*
15726 		 * Some software (for example, GateD and Sun Cluster) attempts
15727 		 * to create (what amount to) IRE_PREFIX routes with the
15728 		 * loopback address as the gateway.  This is primarily done to
15729 		 * set up prefixes with the RTF_REJECT flag set (for example,
15730 		 * when generating aggregate routes.)
15731 		 *
15732 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15733 		 * IRE_LOOPBACK, then we map the request into a
15734 		 * IRE_IF_NORESOLVER.
15735 		 */
15736 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15737 			ire->ire_type = IRE_IF_NORESOLVER;
15738 		/*
15739 		 * ire held by ire_add, will be refreled' towards the
15740 		 * the end of ipif_up_done
15741 		 */
15742 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15743 		*irep = ire;
15744 		irep++;
15745 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15746 	}
15747 	mutex_exit(&ipif->ipif_saved_ire_lock);
15748 	return (ipif_saved_irep);
15749 }
15750 
15751 /*
15752  * Used to set the netmask and broadcast address to default values when the
15753  * interface is brought up.  (Always called as writer.)
15754  */
15755 static void
15756 ipif_set_default(ipif_t *ipif)
15757 {
15758 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15759 
15760 	if (!ipif->ipif_isv6) {
15761 		/*
15762 		 * Interface holds an IPv4 address. Default
15763 		 * mask is the natural netmask.
15764 		 */
15765 		if (!ipif->ipif_net_mask) {
15766 			ipaddr_t	v4mask;
15767 
15768 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15769 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15770 		}
15771 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15772 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15773 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15774 		} else {
15775 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15776 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15777 		}
15778 		/*
15779 		 * NOTE: SunOS 4.X does this even if the broadcast address
15780 		 * has been already set thus we do the same here.
15781 		 */
15782 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15783 			ipaddr_t	v4addr;
15784 
15785 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15786 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15787 		}
15788 	} else {
15789 		/*
15790 		 * Interface holds an IPv6-only address.  Default
15791 		 * mask is all-ones.
15792 		 */
15793 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15794 			ipif->ipif_v6net_mask = ipv6_all_ones;
15795 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15796 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15797 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15798 		} else {
15799 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15800 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15801 		}
15802 	}
15803 }
15804 
15805 /*
15806  * Return 0 if this address can be used as local address without causing
15807  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15808  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15809  * Note that the same IPv6 link-local address is allowed as long as the ills
15810  * are not on the same link.
15811  */
15812 int
15813 ip_addr_availability_check(ipif_t *new_ipif)
15814 {
15815 	in6_addr_t our_v6addr;
15816 	ill_t *ill;
15817 	ipif_t *ipif;
15818 	ill_walk_context_t ctx;
15819 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15820 
15821 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15822 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15823 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15824 
15825 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15826 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15827 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15828 		return (0);
15829 
15830 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15831 
15832 	if (new_ipif->ipif_isv6)
15833 		ill = ILL_START_WALK_V6(&ctx, ipst);
15834 	else
15835 		ill = ILL_START_WALK_V4(&ctx, ipst);
15836 
15837 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15838 		for (ipif = ill->ill_ipif; ipif != NULL;
15839 		    ipif = ipif->ipif_next) {
15840 			if ((ipif == new_ipif) ||
15841 			    !(ipif->ipif_flags & IPIF_UP) ||
15842 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15843 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15844 			    &our_v6addr))
15845 				continue;
15846 
15847 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15848 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15849 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15850 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15851 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15852 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15853 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15854 				continue;
15855 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15856 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15857 				continue;
15858 			else if (new_ipif->ipif_ill == ill)
15859 				return (EADDRINUSE);
15860 			else
15861 				return (EADDRNOTAVAIL);
15862 		}
15863 	}
15864 
15865 	return (0);
15866 }
15867 
15868 /*
15869  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15870  * IREs for the ipif.
15871  * When the routine returns EINPROGRESS then mp has been consumed and
15872  * the ioctl will be acked from ip_rput_dlpi.
15873  */
15874 int
15875 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15876 {
15877 	ill_t		*ill = ipif->ipif_ill;
15878 	boolean_t 	isv6 = ipif->ipif_isv6;
15879 	int		err = 0;
15880 	boolean_t	success;
15881 	uint_t		ipif_orig_id;
15882 	ip_stack_t	*ipst = ill->ill_ipst;
15883 
15884 	ASSERT(IAM_WRITER_IPIF(ipif));
15885 
15886 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15887 
15888 	/* Shouldn't get here if it is already up. */
15889 	if (ipif->ipif_flags & IPIF_UP)
15890 		return (EALREADY);
15891 
15892 	/*
15893 	 * If this is a request to bring up a data address on an interface
15894 	 * under IPMP, then move the address to its IPMP meta-interface and
15895 	 * try to bring it up.  One complication is that the zeroth ipif for
15896 	 * an ill is special, in that every ill always has one, and that code
15897 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15898 	 */
15899 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15900 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15901 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15902 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15903 
15904 		/*
15905 		 * The ipif being brought up should be quiesced.  If it's not,
15906 		 * something has gone amiss and we need to bail out.  (If it's
15907 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15908 		 */
15909 		mutex_enter(&ill->ill_lock);
15910 		if (!ipif_is_quiescent(ipif)) {
15911 			mutex_exit(&ill->ill_lock);
15912 			return (EINVAL);
15913 		}
15914 		mutex_exit(&ill->ill_lock);
15915 
15916 		/*
15917 		 * If we're going to need to allocate ipifs, do it prior
15918 		 * to starting the move (and grabbing locks).
15919 		 */
15920 		if (ipif->ipif_id == 0) {
15921 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15922 			    B_FALSE);
15923 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15924 			    B_FALSE);
15925 			if (moveipif == NULL || stubipif == NULL) {
15926 				mi_free(moveipif);
15927 				mi_free(stubipif);
15928 				return (ENOMEM);
15929 			}
15930 		}
15931 
15932 		/*
15933 		 * Grab or transfer the ipif to move.  During the move, keep
15934 		 * ill_g_lock held to prevent any ill walker threads from
15935 		 * seeing things in an inconsistent state.
15936 		 */
15937 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15938 		if (ipif->ipif_id != 0) {
15939 			ipif_remove(ipif);
15940 		} else {
15941 			ipif_transfer(ipif, moveipif, stubipif);
15942 			ipif = moveipif;
15943 		}
15944 
15945 		/*
15946 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
15947 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
15948 		 * replace that one.  Otherwise, pick the next available slot.
15949 		 */
15950 		ipif->ipif_ill = ipmp_ill;
15951 		ipif_orig_id = ipif->ipif_id;
15952 
15953 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
15954 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
15955 			ipif = ipmp_ill->ill_ipif;
15956 		} else {
15957 			ipif->ipif_id = -1;
15958 			if (ipif_insert(ipif, B_FALSE) != 0) {
15959 				/*
15960 				 * No more available ipif_id's -- put it back
15961 				 * on the original ill and fail the operation.
15962 				 * Since we're writer on the ill, we can be
15963 				 * sure our old slot is still available.
15964 				 */
15965 				ipif->ipif_id = ipif_orig_id;
15966 				ipif->ipif_ill = ill;
15967 				if (ipif_orig_id == 0) {
15968 					ipif_transfer(ipif, ill->ill_ipif,
15969 					    NULL);
15970 				} else {
15971 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
15972 				}
15973 				rw_exit(&ipst->ips_ill_g_lock);
15974 				return (ENOMEM);
15975 			}
15976 		}
15977 		rw_exit(&ipst->ips_ill_g_lock);
15978 
15979 		/*
15980 		 * Tell SCTP that the ipif has moved.  Note that even if we
15981 		 * had to allocate a new ipif, the original sequence id was
15982 		 * preserved and therefore SCTP won't know.
15983 		 */
15984 		sctp_move_ipif(ipif, ill, ipmp_ill);
15985 
15986 		/*
15987 		 * If the ipif being brought up was on slot zero, then we
15988 		 * first need to bring up the placeholder we stuck there.  In
15989 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
15990 		 * to ipif_up() itself, if we successfully bring up the
15991 		 * placeholder, we'll check ill_move_ipif and bring it up too.
15992 		 */
15993 		if (ipif_orig_id == 0) {
15994 			ASSERT(ill->ill_move_ipif == NULL);
15995 			ill->ill_move_ipif = ipif;
15996 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
15997 				ASSERT(ill->ill_move_ipif == NULL);
15998 			if (err != EINPROGRESS)
15999 				ill->ill_move_ipif = NULL;
16000 			return (err);
16001 		}
16002 
16003 		/*
16004 		 * Bring it up on the IPMP ill.
16005 		 */
16006 		return (ipif_up(ipif, q, mp));
16007 	}
16008 
16009 	/* Skip arp/ndp for any loopback interface. */
16010 	if (ill->ill_wq != NULL) {
16011 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16012 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
16013 
16014 		if (!ill->ill_dl_up) {
16015 			/*
16016 			 * ill_dl_up is not yet set. i.e. we are yet to
16017 			 * DL_BIND with the driver and this is the first
16018 			 * logical interface on the ill to become "up".
16019 			 * Tell the driver to get going (via DL_BIND_REQ).
16020 			 * Note that changing "significant" IFF_ flags
16021 			 * address/netmask etc cause a down/up dance, but
16022 			 * does not cause an unbind (DL_UNBIND) with the driver
16023 			 */
16024 			return (ill_dl_up(ill, ipif, mp, q));
16025 		}
16026 
16027 		/*
16028 		 * ipif_resolver_up may end up sending an
16029 		 * AR_INTERFACE_UP message to ARP, which would, in
16030 		 * turn send a DLPI message to the driver. ioctls are
16031 		 * serialized and so we cannot send more than one
16032 		 * interface up message at a time. If ipif_resolver_up
16033 		 * does send an interface up message to ARP, we get
16034 		 * EINPROGRESS and we will complete in ip_arp_done.
16035 		 */
16036 
16037 		ASSERT(connp != NULL || !CONN_Q(q));
16038 		if (connp != NULL)
16039 			mutex_enter(&connp->conn_lock);
16040 		mutex_enter(&ill->ill_lock);
16041 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16042 		mutex_exit(&ill->ill_lock);
16043 		if (connp != NULL)
16044 			mutex_exit(&connp->conn_lock);
16045 		if (!success)
16046 			return (EINTR);
16047 
16048 		/*
16049 		 * Crank up the resolver.  For IPv6, this cranks up the
16050 		 * external resolver if one is configured, but even if an
16051 		 * external resolver isn't configured, it must be called to
16052 		 * reset DAD state.  For IPv6, if an external resolver is not
16053 		 * being used, ipif_resolver_up() will never return
16054 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
16055 		 * Note that if an external resolver is being used, there's no
16056 		 * need to call ipif_ndp_up() since it will do nothing.
16057 		 */
16058 		err = ipif_resolver_up(ipif, Res_act_initial);
16059 		if (err == EINPROGRESS) {
16060 			/* We will complete it in ip_arp_done() */
16061 			return (err);
16062 		}
16063 
16064 		if (isv6 && err == 0)
16065 			err = ipif_ndp_up(ipif, B_TRUE);
16066 
16067 		ASSERT(err != EINPROGRESS);
16068 		mp = ipsq_pending_mp_get(ipsq, &connp);
16069 		ASSERT(mp != NULL);
16070 		if (err != 0)
16071 			return (err);
16072 	} else {
16073 		/*
16074 		 * Interfaces without underlying hardware don't do duplicate
16075 		 * address detection.
16076 		 */
16077 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16078 		ipif->ipif_addr_ready = 1;
16079 	}
16080 
16081 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16082 	if (err == 0 && ill->ill_move_ipif != NULL) {
16083 		ipif = ill->ill_move_ipif;
16084 		ill->ill_move_ipif = NULL;
16085 		return (ipif_up(ipif, q, mp));
16086 	}
16087 	return (err);
16088 }
16089 
16090 /*
16091  * Perform a bind for the physical device.
16092  * When the routine returns EINPROGRESS then mp has been consumed and
16093  * the ioctl will be acked from ip_rput_dlpi.
16094  * Allocate an unbind message and save it until ipif_down.
16095  */
16096 static int
16097 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16098 {
16099 	areq_t	*areq;
16100 	mblk_t	*areq_mp = NULL;
16101 	mblk_t	*bind_mp = NULL;
16102 	mblk_t	*unbind_mp = NULL;
16103 	conn_t	*connp;
16104 	boolean_t success;
16105 	uint16_t sap_addr;
16106 
16107 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16108 	ASSERT(IAM_WRITER_ILL(ill));
16109 	ASSERT(mp != NULL);
16110 
16111 	/* Create a resolver cookie for ARP */
16112 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16113 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16114 		if (areq_mp == NULL)
16115 			return (ENOMEM);
16116 
16117 		freemsg(ill->ill_resolver_mp);
16118 		ill->ill_resolver_mp = areq_mp;
16119 		areq = (areq_t *)areq_mp->b_rptr;
16120 		sap_addr = ill->ill_sap;
16121 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16122 	}
16123 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16124 	    DL_BIND_REQ);
16125 	if (bind_mp == NULL)
16126 		goto bad;
16127 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16128 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16129 
16130 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16131 	if (unbind_mp == NULL)
16132 		goto bad;
16133 
16134 	/*
16135 	 * Record state needed to complete this operation when the
16136 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16137 	 */
16138 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16139 	ASSERT(connp != NULL || !CONN_Q(q));
16140 	GRAB_CONN_LOCK(q);
16141 	mutex_enter(&ipif->ipif_ill->ill_lock);
16142 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16143 	mutex_exit(&ipif->ipif_ill->ill_lock);
16144 	RELEASE_CONN_LOCK(q);
16145 	if (!success)
16146 		goto bad;
16147 
16148 	/*
16149 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16150 	 * the interface goes down.
16151 	 */
16152 	ASSERT(ill->ill_unbind_mp == NULL);
16153 	ill->ill_unbind_mp = unbind_mp;
16154 
16155 	ill_dlpi_send(ill, bind_mp);
16156 	/* Send down link-layer capabilities probe if not already done. */
16157 	ill_capability_probe(ill);
16158 
16159 	/*
16160 	 * Sysid used to rely on the fact that netboots set domainname
16161 	 * and the like. Now that miniroot boots aren't strictly netboots
16162 	 * and miniroot network configuration is driven from userland
16163 	 * these things still need to be set. This situation can be detected
16164 	 * by comparing the interface being configured here to the one
16165 	 * dhcifname was set to reference by the boot loader. Once sysid is
16166 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16167 	 */
16168 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16169 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16170 	    (strlen(srpc_domain) == 0)) {
16171 		if (dhcpinit() != 0)
16172 			cmn_err(CE_WARN, "no cached dhcp response");
16173 	}
16174 
16175 	/*
16176 	 * This operation will complete in ip_rput_dlpi with either
16177 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16178 	 */
16179 	return (EINPROGRESS);
16180 bad:
16181 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16182 
16183 	freemsg(bind_mp);
16184 	freemsg(unbind_mp);
16185 	return (ENOMEM);
16186 }
16187 
16188 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16189 
16190 /*
16191  * DLPI and ARP is up.
16192  * Create all the IREs associated with an interface bring up multicast.
16193  * Set the interface flag and finish other initialization
16194  * that potentially had to be differed to after DL_BIND_ACK.
16195  */
16196 int
16197 ipif_up_done(ipif_t *ipif)
16198 {
16199 	ire_t	*ire_array[20];
16200 	ire_t	**irep = ire_array;
16201 	ire_t	**irep1;
16202 	ipaddr_t net_mask = 0;
16203 	ipaddr_t subnet_mask, route_mask;
16204 	ill_t	*ill = ipif->ipif_ill;
16205 	queue_t	*stq;
16206 	ipif_t	 *src_ipif;
16207 	ipif_t   *tmp_ipif;
16208 	boolean_t	flush_ire_cache = B_TRUE;
16209 	int	err = 0;
16210 	ire_t	**ipif_saved_irep = NULL;
16211 	int ipif_saved_ire_cnt;
16212 	int	cnt;
16213 	boolean_t	src_ipif_held = B_FALSE;
16214 	boolean_t	loopback = B_FALSE;
16215 	ip_stack_t	*ipst = ill->ill_ipst;
16216 
16217 	ip1dbg(("ipif_up_done(%s:%u)\n",
16218 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16219 	/* Check if this is a loopback interface */
16220 	if (ipif->ipif_ill->ill_wq == NULL)
16221 		loopback = B_TRUE;
16222 
16223 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16224 	/*
16225 	 * If all other interfaces for this ill are down or DEPRECATED,
16226 	 * or otherwise unsuitable for source address selection, remove
16227 	 * any IRE_CACHE entries for this ill to make sure source
16228 	 * address selection gets to take this new ipif into account.
16229 	 * No need to hold ill_lock while traversing the ipif list since
16230 	 * we are writer
16231 	 */
16232 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16233 	    tmp_ipif = tmp_ipif->ipif_next) {
16234 		if (((tmp_ipif->ipif_flags &
16235 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16236 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16237 		    (tmp_ipif == ipif))
16238 			continue;
16239 		/* first useable pre-existing interface */
16240 		flush_ire_cache = B_FALSE;
16241 		break;
16242 	}
16243 	if (flush_ire_cache)
16244 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16245 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16246 
16247 	/*
16248 	 * Figure out which way the send-to queue should go.  Only
16249 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16250 	 * should show up here.
16251 	 */
16252 	switch (ill->ill_net_type) {
16253 	case IRE_IF_RESOLVER:
16254 		stq = ill->ill_rq;
16255 		break;
16256 	case IRE_IF_NORESOLVER:
16257 	case IRE_LOOPBACK:
16258 		stq = ill->ill_wq;
16259 		break;
16260 	default:
16261 		return (EINVAL);
16262 	}
16263 
16264 	if (IS_LOOPBACK(ill)) {
16265 		/*
16266 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16267 		 * ipif_lookup_on_name(), but in the case of zones we can have
16268 		 * several loopback addresses on lo0. So all the interfaces with
16269 		 * loopback addresses need to be marked IRE_LOOPBACK.
16270 		 */
16271 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16272 		    htonl(INADDR_LOOPBACK))
16273 			ipif->ipif_ire_type = IRE_LOOPBACK;
16274 		else
16275 			ipif->ipif_ire_type = IRE_LOCAL;
16276 	}
16277 
16278 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16279 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16280 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16281 		/*
16282 		 * Can't use our source address. Select a different
16283 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16284 		 */
16285 		src_ipif = ipif_select_source(ipif->ipif_ill,
16286 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16287 		if (src_ipif == NULL)
16288 			src_ipif = ipif;	/* Last resort */
16289 		else
16290 			src_ipif_held = B_TRUE;
16291 	} else {
16292 		src_ipif = ipif;
16293 	}
16294 
16295 	/* Create all the IREs associated with this interface */
16296 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16297 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16298 
16299 		/*
16300 		 * If we're on a labeled system then make sure that zone-
16301 		 * private addresses have proper remote host database entries.
16302 		 */
16303 		if (is_system_labeled() &&
16304 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16305 		    !tsol_check_interface_address(ipif))
16306 			return (EINVAL);
16307 
16308 		/* Register the source address for __sin6_src_id */
16309 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16310 		    ipif->ipif_zoneid, ipst);
16311 		if (err != 0) {
16312 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16313 			return (err);
16314 		}
16315 
16316 		/* If the interface address is set, create the local IRE. */
16317 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16318 		    (void *)ipif,
16319 		    ipif->ipif_ire_type,
16320 		    ntohl(ipif->ipif_lcl_addr)));
16321 		*irep++ = ire_create(
16322 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16323 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16324 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16325 		    NULL,				/* no gateway */
16326 		    &ip_loopback_mtuplus,		/* max frag size */
16327 		    NULL,
16328 		    ipif->ipif_rq,			/* recv-from queue */
16329 		    NULL,				/* no send-to queue */
16330 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16331 		    ipif,
16332 		    0,
16333 		    0,
16334 		    0,
16335 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16336 		    RTF_PRIVATE : 0,
16337 		    &ire_uinfo_null,
16338 		    NULL,
16339 		    NULL,
16340 		    ipst);
16341 	} else {
16342 		ip1dbg((
16343 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16344 		    ipif->ipif_ire_type,
16345 		    ntohl(ipif->ipif_lcl_addr),
16346 		    (uint_t)ipif->ipif_flags));
16347 	}
16348 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16349 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16350 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16351 	} else {
16352 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16353 	}
16354 
16355 	subnet_mask = ipif->ipif_net_mask;
16356 
16357 	/*
16358 	 * If mask was not specified, use natural netmask of
16359 	 * interface address. Also, store this mask back into the
16360 	 * ipif struct.
16361 	 */
16362 	if (subnet_mask == 0) {
16363 		subnet_mask = net_mask;
16364 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16365 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16366 		    ipif->ipif_v6subnet);
16367 	}
16368 
16369 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16370 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16371 	    ipif->ipif_subnet != INADDR_ANY) {
16372 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16373 
16374 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16375 			route_mask = IP_HOST_MASK;
16376 		} else {
16377 			route_mask = subnet_mask;
16378 		}
16379 
16380 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16381 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16382 		    (void *)ipif, (void *)ill,
16383 		    ill->ill_net_type,
16384 		    ntohl(ipif->ipif_subnet)));
16385 		*irep++ = ire_create(
16386 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16387 		    (uchar_t *)&route_mask,		/* mask */
16388 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16389 		    NULL,				/* no gateway */
16390 		    &ipif->ipif_mtu,			/* max frag */
16391 		    NULL,
16392 		    NULL,				/* no recv queue */
16393 		    stq,				/* send-to queue */
16394 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16395 		    ipif,
16396 		    0,
16397 		    0,
16398 		    0,
16399 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16400 		    &ire_uinfo_null,
16401 		    NULL,
16402 		    NULL,
16403 		    ipst);
16404 	}
16405 
16406 	/*
16407 	 * Create any necessary broadcast IREs.
16408 	 */
16409 	if (ipif->ipif_flags & IPIF_BROADCAST)
16410 		irep = ipif_create_bcast_ires(ipif, irep);
16411 
16412 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16413 
16414 	/* If an earlier ire_create failed, get out now */
16415 	for (irep1 = irep; irep1 > ire_array; ) {
16416 		irep1--;
16417 		if (*irep1 == NULL) {
16418 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16419 			err = ENOMEM;
16420 			goto bad;
16421 		}
16422 	}
16423 
16424 	/*
16425 	 * Need to atomically check for IP address availability under
16426 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16427 	 * ills or new ipifs can be added while we are checking availability.
16428 	 */
16429 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16430 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16431 	/* Mark it up, and increment counters. */
16432 	ipif->ipif_flags |= IPIF_UP;
16433 	ill->ill_ipif_up_count++;
16434 	err = ip_addr_availability_check(ipif);
16435 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16436 	rw_exit(&ipst->ips_ill_g_lock);
16437 
16438 	if (err != 0) {
16439 		/*
16440 		 * Our address may already be up on the same ill. In this case,
16441 		 * the ARP entry for our ipif replaced the one for the other
16442 		 * ipif. So we don't want to delete it (otherwise the other ipif
16443 		 * would be unable to send packets).
16444 		 * ip_addr_availability_check() identifies this case for us and
16445 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16446 		 * which is the expected error code.
16447 		 */
16448 		if (err == EADDRINUSE) {
16449 			freemsg(ipif->ipif_arp_del_mp);
16450 			ipif->ipif_arp_del_mp = NULL;
16451 			err = EADDRNOTAVAIL;
16452 		}
16453 		ill->ill_ipif_up_count--;
16454 		ipif->ipif_flags &= ~IPIF_UP;
16455 		goto bad;
16456 	}
16457 
16458 	/*
16459 	 * Add in all newly created IREs.  ire_create_bcast() has
16460 	 * already checked for duplicates of the IRE_BROADCAST type.
16461 	 */
16462 	for (irep1 = irep; irep1 > ire_array; ) {
16463 		irep1--;
16464 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16465 		/*
16466 		 * refheld by ire_add. refele towards the end of the func
16467 		 */
16468 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16469 	}
16470 
16471 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16472 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16473 	ipif_saved_irep = ipif_recover_ire(ipif);
16474 
16475 	if (!loopback) {
16476 		/*
16477 		 * If the broadcast address has been set, make sure it makes
16478 		 * sense based on the interface address.
16479 		 * Only match on ill since we are sharing broadcast addresses.
16480 		 */
16481 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16482 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16483 			ire_t	*ire;
16484 
16485 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16486 			    IRE_BROADCAST, ipif, ALL_ZONES,
16487 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16488 
16489 			if (ire == NULL) {
16490 				/*
16491 				 * If there isn't a matching broadcast IRE,
16492 				 * revert to the default for this netmask.
16493 				 */
16494 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16495 				mutex_enter(&ipif->ipif_ill->ill_lock);
16496 				ipif_set_default(ipif);
16497 				mutex_exit(&ipif->ipif_ill->ill_lock);
16498 			} else {
16499 				ire_refrele(ire);
16500 			}
16501 		}
16502 
16503 	}
16504 
16505 	if (ill->ill_need_recover_multicast) {
16506 		/*
16507 		 * Need to recover all multicast memberships in the driver.
16508 		 * This had to be deferred until we had attached.  The same
16509 		 * code exists in ipif_up_done_v6() to recover IPv6
16510 		 * memberships.
16511 		 *
16512 		 * Note that it would be preferable to unconditionally do the
16513 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16514 		 * that since ill_join_allmulti() depends on ill_dl_up being
16515 		 * set, and it is not set until we receive a DL_BIND_ACK after
16516 		 * having called ill_dl_up().
16517 		 */
16518 		ill_recover_multicast(ill);
16519 	}
16520 
16521 	if (ill->ill_ipif_up_count == 1) {
16522 		/*
16523 		 * Since the interface is now up, it may now be active.
16524 		 */
16525 		if (IS_UNDER_IPMP(ill))
16526 			ipmp_ill_refresh_active(ill);
16527 
16528 		/*
16529 		 * If this is an IPMP interface, we may now be able to
16530 		 * establish ARP entries.
16531 		 */
16532 		if (IS_IPMP(ill))
16533 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16534 	}
16535 
16536 	/* Join the allhosts multicast address */
16537 	ipif_multicast_up(ipif);
16538 
16539 	/*
16540 	 * See if anybody else would benefit from our new ipif.
16541 	 */
16542 	if (!loopback &&
16543 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16544 		ill_update_source_selection(ill);
16545 	}
16546 
16547 	for (irep1 = irep; irep1 > ire_array; ) {
16548 		irep1--;
16549 		if (*irep1 != NULL) {
16550 			/* was held in ire_add */
16551 			ire_refrele(*irep1);
16552 		}
16553 	}
16554 
16555 	cnt = ipif_saved_ire_cnt;
16556 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16557 		if (*irep1 != NULL) {
16558 			/* was held in ire_add */
16559 			ire_refrele(*irep1);
16560 		}
16561 	}
16562 
16563 	if (!loopback && ipif->ipif_addr_ready) {
16564 		/* Broadcast an address mask reply. */
16565 		ipif_mask_reply(ipif);
16566 	}
16567 	if (ipif_saved_irep != NULL) {
16568 		kmem_free(ipif_saved_irep,
16569 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16570 	}
16571 	if (src_ipif_held)
16572 		ipif_refrele(src_ipif);
16573 
16574 	/*
16575 	 * This had to be deferred until we had bound.  Tell routing sockets and
16576 	 * others that this interface is up if it looks like the address has
16577 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16578 	 * duplicate address detection to do its thing.
16579 	 */
16580 	if (ipif->ipif_addr_ready)
16581 		ipif_up_notify(ipif);
16582 	return (0);
16583 
16584 bad:
16585 	ip1dbg(("ipif_up_done: FAILED \n"));
16586 
16587 	while (irep > ire_array) {
16588 		irep--;
16589 		if (*irep != NULL)
16590 			ire_delete(*irep);
16591 	}
16592 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16593 
16594 	if (ipif_saved_irep != NULL) {
16595 		kmem_free(ipif_saved_irep,
16596 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16597 	}
16598 	if (src_ipif_held)
16599 		ipif_refrele(src_ipif);
16600 
16601 	ipif_resolver_down(ipif);
16602 	return (err);
16603 }
16604 
16605 /*
16606  * Turn off the ARP with the ILLF_NOARP flag.
16607  */
16608 static int
16609 ill_arp_off(ill_t *ill)
16610 {
16611 	mblk_t	*arp_off_mp = NULL;
16612 	mblk_t	*arp_on_mp = NULL;
16613 
16614 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16615 
16616 	ASSERT(IAM_WRITER_ILL(ill));
16617 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16618 
16619 	/*
16620 	 * If the on message is still around we've already done
16621 	 * an arp_off without doing an arp_on thus there is no
16622 	 * work needed.
16623 	 */
16624 	if (ill->ill_arp_on_mp != NULL)
16625 		return (0);
16626 
16627 	/*
16628 	 * Allocate an ARP on message (to be saved) and an ARP off message
16629 	 */
16630 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16631 	if (!arp_off_mp)
16632 		return (ENOMEM);
16633 
16634 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16635 	if (!arp_on_mp)
16636 		goto failed;
16637 
16638 	ASSERT(ill->ill_arp_on_mp == NULL);
16639 	ill->ill_arp_on_mp = arp_on_mp;
16640 
16641 	/* Send an AR_INTERFACE_OFF request */
16642 	putnext(ill->ill_rq, arp_off_mp);
16643 	return (0);
16644 failed:
16645 
16646 	if (arp_off_mp)
16647 		freemsg(arp_off_mp);
16648 	return (ENOMEM);
16649 }
16650 
16651 /*
16652  * Turn on ARP by turning off the ILLF_NOARP flag.
16653  */
16654 static int
16655 ill_arp_on(ill_t *ill)
16656 {
16657 	mblk_t	*mp;
16658 
16659 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16660 
16661 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16662 
16663 	ASSERT(IAM_WRITER_ILL(ill));
16664 	/*
16665 	 * Send an AR_INTERFACE_ON request if we have already done
16666 	 * an arp_off (which allocated the message).
16667 	 */
16668 	if (ill->ill_arp_on_mp != NULL) {
16669 		mp = ill->ill_arp_on_mp;
16670 		ill->ill_arp_on_mp = NULL;
16671 		putnext(ill->ill_rq, mp);
16672 	}
16673 	return (0);
16674 }
16675 
16676 /*
16677  * Checks for availbility of a usable source address (if there is one) when the
16678  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16679  * this selection is done regardless of the destination.
16680  */
16681 boolean_t
16682 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16683 {
16684 	uint_t	ifindex;
16685 	ipif_t	*ipif = NULL;
16686 	ill_t	*uill;
16687 	boolean_t isv6;
16688 	ip_stack_t	*ipst = ill->ill_ipst;
16689 
16690 	ASSERT(ill != NULL);
16691 
16692 	isv6 = ill->ill_isv6;
16693 	ifindex = ill->ill_usesrc_ifindex;
16694 	if (ifindex != 0) {
16695 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16696 		    NULL, ipst);
16697 		if (uill == NULL)
16698 			return (NULL);
16699 		mutex_enter(&uill->ill_lock);
16700 		for (ipif = uill->ill_ipif; ipif != NULL;
16701 		    ipif = ipif->ipif_next) {
16702 			if (!IPIF_CAN_LOOKUP(ipif))
16703 				continue;
16704 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16705 				continue;
16706 			if (!(ipif->ipif_flags & IPIF_UP))
16707 				continue;
16708 			if (ipif->ipif_zoneid != zoneid)
16709 				continue;
16710 			if ((isv6 &&
16711 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16712 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16713 				continue;
16714 			mutex_exit(&uill->ill_lock);
16715 			ill_refrele(uill);
16716 			return (B_TRUE);
16717 		}
16718 		mutex_exit(&uill->ill_lock);
16719 		ill_refrele(uill);
16720 	}
16721 	return (B_FALSE);
16722 }
16723 
16724 /*
16725  * IP source address type, sorted from worst to best.  For a given type,
16726  * always prefer IP addresses on the same subnet.  All-zones addresses are
16727  * suboptimal because they pose problems with unlabeled destinations.
16728  */
16729 typedef enum {
16730 	IPIF_NONE,
16731 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16732 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16733 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16734 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16735 	IPIF_DIFFNET,			/* normal and different subnet */
16736 	IPIF_SAMENET			/* normal and same subnet */
16737 } ipif_type_t;
16738 
16739 /*
16740  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16741  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16742  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16743  * the first one, unless IPMP is used in which case we round-robin among them;
16744  * see below for more.
16745  *
16746  * Returns NULL if there is no suitable source address for the ill.
16747  * This only occurs when there is no valid source address for the ill.
16748  */
16749 ipif_t *
16750 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16751 {
16752 	ill_t	*usill = NULL;
16753 	ill_t	*ipmp_ill = NULL;
16754 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16755 	ipif_type_t type, best_type;
16756 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16757 	ip_stack_t *ipst = ill->ill_ipst;
16758 	boolean_t samenet;
16759 
16760 	if (ill->ill_usesrc_ifindex != 0) {
16761 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16762 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16763 		if (usill != NULL)
16764 			ill = usill;	/* Select source from usesrc ILL */
16765 		else
16766 			return (NULL);
16767 	}
16768 
16769 	/*
16770 	 * Test addresses should never be used for source address selection,
16771 	 * so if we were passed one, switch to the IPMP meta-interface.
16772 	 */
16773 	if (IS_UNDER_IPMP(ill)) {
16774 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16775 			ill = ipmp_ill;	/* Select source from IPMP ill */
16776 		else
16777 			return (NULL);
16778 	}
16779 
16780 	/*
16781 	 * If we're dealing with an unlabeled destination on a labeled system,
16782 	 * make sure that we ignore source addresses that are incompatible with
16783 	 * the destination's default label.  That destination's default label
16784 	 * must dominate the minimum label on the source address.
16785 	 */
16786 	dst_rhtp = NULL;
16787 	if (is_system_labeled()) {
16788 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16789 		if (dst_rhtp == NULL)
16790 			return (NULL);
16791 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16792 			TPC_RELE(dst_rhtp);
16793 			dst_rhtp = NULL;
16794 		}
16795 	}
16796 
16797 	/*
16798 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16799 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16800 	 * After selecting the right ipif, under ill_lock make sure ipif is
16801 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16802 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16803 	 * but not under a lock.
16804 	 */
16805 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16806 retry:
16807 	/*
16808 	 * For source address selection, we treat the ipif list as circular
16809 	 * and continue until we get back to where we started.  This allows
16810 	 * IPMP to vary source address selection (which improves inbound load
16811 	 * spreading) by caching its last ending point and starting from
16812 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16813 	 * ills since that can't happen on the IPMP ill.
16814 	 */
16815 	start_ipif = ill->ill_ipif;
16816 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16817 		start_ipif = ill->ill_src_ipif;
16818 
16819 	ipif = start_ipif;
16820 	best_ipif = NULL;
16821 	best_type = IPIF_NONE;
16822 	do {
16823 		if ((next_ipif = ipif->ipif_next) == NULL)
16824 			next_ipif = ill->ill_ipif;
16825 
16826 		if (!IPIF_CAN_LOOKUP(ipif))
16827 			continue;
16828 		/* Always skip NOLOCAL and ANYCAST interfaces */
16829 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16830 			continue;
16831 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16832 			continue;
16833 		if (ipif->ipif_zoneid != zoneid &&
16834 		    ipif->ipif_zoneid != ALL_ZONES)
16835 			continue;
16836 
16837 		/*
16838 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16839 		 * are not valid as source addresses.
16840 		 */
16841 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16842 			continue;
16843 
16844 		/*
16845 		 * Check compatibility of local address for destination's
16846 		 * default label if we're on a labeled system.	Incompatible
16847 		 * addresses can't be used at all.
16848 		 */
16849 		if (dst_rhtp != NULL) {
16850 			boolean_t incompat;
16851 
16852 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16853 			    IPV4_VERSION, B_FALSE);
16854 			if (src_rhtp == NULL)
16855 				continue;
16856 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16857 			    src_rhtp->tpc_tp.tp_doi !=
16858 			    dst_rhtp->tpc_tp.tp_doi ||
16859 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16860 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16861 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16862 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16863 			TPC_RELE(src_rhtp);
16864 			if (incompat)
16865 				continue;
16866 		}
16867 
16868 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16869 
16870 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16871 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16872 			    IPIF_DIFFNET_DEPRECATED;
16873 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16874 			type = samenet ? IPIF_SAMENET_ALLZONES :
16875 			    IPIF_DIFFNET_ALLZONES;
16876 		} else {
16877 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16878 		}
16879 
16880 		if (type > best_type) {
16881 			best_type = type;
16882 			best_ipif = ipif;
16883 			if (best_type == IPIF_SAMENET)
16884 				break; /* can't get better */
16885 		}
16886 	} while ((ipif = next_ipif) != start_ipif);
16887 
16888 	if ((ipif = best_ipif) != NULL) {
16889 		mutex_enter(&ipif->ipif_ill->ill_lock);
16890 		if (!IPIF_CAN_LOOKUP(ipif)) {
16891 			mutex_exit(&ipif->ipif_ill->ill_lock);
16892 			goto retry;
16893 		}
16894 		ipif_refhold_locked(ipif);
16895 
16896 		/*
16897 		 * For IPMP, update the source ipif rotor to the next ipif,
16898 		 * provided we can look it up.  (We must not use it if it's
16899 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16900 		 * ipif_free() checked ill_src_ipif.)
16901 		 */
16902 		if (IS_IPMP(ill) && ipif != NULL) {
16903 			next_ipif = ipif->ipif_next;
16904 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16905 				ill->ill_src_ipif = next_ipif;
16906 			else
16907 				ill->ill_src_ipif = NULL;
16908 		}
16909 		mutex_exit(&ipif->ipif_ill->ill_lock);
16910 	}
16911 
16912 	rw_exit(&ipst->ips_ill_g_lock);
16913 	if (usill != NULL)
16914 		ill_refrele(usill);
16915 	if (ipmp_ill != NULL)
16916 		ill_refrele(ipmp_ill);
16917 	if (dst_rhtp != NULL)
16918 		TPC_RELE(dst_rhtp);
16919 
16920 #ifdef DEBUG
16921 	if (ipif == NULL) {
16922 		char buf1[INET6_ADDRSTRLEN];
16923 
16924 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
16925 		    ill->ill_name,
16926 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
16927 	} else {
16928 		char buf1[INET6_ADDRSTRLEN];
16929 		char buf2[INET6_ADDRSTRLEN];
16930 
16931 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
16932 		    ipif->ipif_ill->ill_name,
16933 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
16934 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
16935 		    buf2, sizeof (buf2))));
16936 	}
16937 #endif /* DEBUG */
16938 	return (ipif);
16939 }
16940 
16941 /*
16942  * If old_ipif is not NULL, see if ipif was derived from old
16943  * ipif and if so, recreate the interface route by re-doing
16944  * source address selection. This happens when ipif_down ->
16945  * ipif_update_other_ipifs calls us.
16946  *
16947  * If old_ipif is NULL, just redo the source address selection
16948  * if needed. This happens when ipif_up_done calls us.
16949  */
16950 static void
16951 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
16952 {
16953 	ire_t *ire;
16954 	ire_t *ipif_ire;
16955 	queue_t *stq;
16956 	ipif_t *nipif;
16957 	ill_t *ill;
16958 	boolean_t need_rele = B_FALSE;
16959 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16960 
16961 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
16962 	ASSERT(IAM_WRITER_IPIF(ipif));
16963 
16964 	ill = ipif->ipif_ill;
16965 	if (!(ipif->ipif_flags &
16966 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16967 		/*
16968 		 * Can't possibly have borrowed the source
16969 		 * from old_ipif.
16970 		 */
16971 		return;
16972 	}
16973 
16974 	/*
16975 	 * Is there any work to be done? No work if the address
16976 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
16977 	 * ipif_select_source() does not borrow addresses from
16978 	 * NOLOCAL and ANYCAST interfaces).
16979 	 */
16980 	if ((old_ipif != NULL) &&
16981 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
16982 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
16983 	    (old_ipif->ipif_flags &
16984 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
16985 		return;
16986 	}
16987 
16988 	/*
16989 	 * Perform the same checks as when creating the
16990 	 * IRE_INTERFACE in ipif_up_done.
16991 	 */
16992 	if (!(ipif->ipif_flags & IPIF_UP))
16993 		return;
16994 
16995 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
16996 	    (ipif->ipif_subnet == INADDR_ANY))
16997 		return;
16998 
16999 	ipif_ire = ipif_to_ire(ipif);
17000 	if (ipif_ire == NULL)
17001 		return;
17002 
17003 	/*
17004 	 * We know that ipif uses some other source for its
17005 	 * IRE_INTERFACE. Is it using the source of this
17006 	 * old_ipif?
17007 	 */
17008 	if (old_ipif != NULL &&
17009 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
17010 		ire_refrele(ipif_ire);
17011 		return;
17012 	}
17013 	if (ip_debug > 2) {
17014 		/* ip1dbg */
17015 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
17016 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
17017 	}
17018 
17019 	stq = ipif_ire->ire_stq;
17020 
17021 	/*
17022 	 * Can't use our source address. Select a different
17023 	 * source address for the IRE_INTERFACE.
17024 	 */
17025 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
17026 	if (nipif == NULL) {
17027 		/* Last resort - all ipif's have IPIF_NOLOCAL */
17028 		nipif = ipif;
17029 	} else {
17030 		need_rele = B_TRUE;
17031 	}
17032 
17033 	ire = ire_create(
17034 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
17035 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
17036 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
17037 	    NULL,				/* no gateway */
17038 	    &ipif->ipif_mtu,			/* max frag */
17039 	    NULL,				/* no src nce */
17040 	    NULL,				/* no recv from queue */
17041 	    stq,				/* send-to queue */
17042 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
17043 	    ipif,
17044 	    0,
17045 	    0,
17046 	    0,
17047 	    0,
17048 	    &ire_uinfo_null,
17049 	    NULL,
17050 	    NULL,
17051 	    ipst);
17052 
17053 	if (ire != NULL) {
17054 		ire_t *ret_ire;
17055 		int error;
17056 
17057 		/*
17058 		 * We don't need ipif_ire anymore. We need to delete
17059 		 * before we add so that ire_add does not detect
17060 		 * duplicates.
17061 		 */
17062 		ire_delete(ipif_ire);
17063 		ret_ire = ire;
17064 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
17065 		ASSERT(error == 0);
17066 		ASSERT(ire == ret_ire);
17067 		/* Held in ire_add */
17068 		ire_refrele(ret_ire);
17069 	}
17070 	/*
17071 	 * Either we are falling through from above or could not
17072 	 * allocate a replacement.
17073 	 */
17074 	ire_refrele(ipif_ire);
17075 	if (need_rele)
17076 		ipif_refrele(nipif);
17077 }
17078 
17079 /*
17080  * This old_ipif is going away.
17081  *
17082  * Determine if any other ipif's are using our address as
17083  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17084  * IPIF_DEPRECATED).
17085  * Find the IRE_INTERFACE for such ipifs and recreate them
17086  * to use an different source address following the rules in
17087  * ipif_up_done.
17088  */
17089 static void
17090 ipif_update_other_ipifs(ipif_t *old_ipif)
17091 {
17092 	ipif_t	*ipif;
17093 	ill_t	*ill;
17094 	char	buf[INET6_ADDRSTRLEN];
17095 
17096 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17097 
17098 	ill = old_ipif->ipif_ill;
17099 
17100 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17101 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17102 
17103 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17104 		if (ipif == old_ipif)
17105 			continue;
17106 		ipif_recreate_interface_routes(old_ipif, ipif);
17107 	}
17108 }
17109 
17110 /* ARGSUSED */
17111 int
17112 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17113 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17114 {
17115 	/*
17116 	 * ill_phyint_reinit merged the v4 and v6 into a single
17117 	 * ipsq.  We might not have been able to complete the
17118 	 * operation in ipif_set_values, if we could not become
17119 	 * exclusive.  If so restart it here.
17120 	 */
17121 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17122 }
17123 
17124 /*
17125  * Can operate on either a module or a driver queue.
17126  * Returns an error if not a module queue.
17127  */
17128 /* ARGSUSED */
17129 int
17130 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17131     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17132 {
17133 	queue_t		*q1 = q;
17134 	char 		*cp;
17135 	char		interf_name[LIFNAMSIZ];
17136 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17137 
17138 	if (q->q_next == NULL) {
17139 		ip1dbg((
17140 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17141 		return (EINVAL);
17142 	}
17143 
17144 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17145 		return (EALREADY);
17146 
17147 	do {
17148 		q1 = q1->q_next;
17149 	} while (q1->q_next);
17150 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17151 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17152 
17153 	/*
17154 	 * Here we are not going to delay the ioack until after
17155 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17156 	 * original ioctl message before sending the requests.
17157 	 */
17158 	return (ipif_set_values(q, mp, interf_name, &ppa));
17159 }
17160 
17161 /* ARGSUSED */
17162 int
17163 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17164     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17165 {
17166 	return (ENXIO);
17167 }
17168 
17169 /*
17170  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17171  * minimum (but complete) set exist.  This is necessary when adding or
17172  * removing an interface to/from an IPMP group, since interfaces in an
17173  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17174  * its test address subnets overlap with IPMP data addresses).	It's also
17175  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17176  * interface when the nominated broadcast interface changes.
17177  */
17178 void
17179 ill_refresh_bcast(ill_t *ill)
17180 {
17181 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17182 	ire_t **irep;
17183 	ipif_t *ipif;
17184 
17185 	ASSERT(!ill->ill_isv6);
17186 	ASSERT(IAM_WRITER_ILL(ill));
17187 
17188 	/*
17189 	 * Remove any old broadcast IREs.
17190 	 */
17191 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17192 	    ill_broadcast_delete, ill, ill);
17193 
17194 	/*
17195 	 * Create new ones for any ipifs that are up and broadcast-capable.
17196 	 */
17197 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17198 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17199 		    (IPIF_UP|IPIF_BROADCAST))
17200 			continue;
17201 
17202 		irep = ipif_create_bcast_ires(ipif, ire_array);
17203 		while (irep-- > ire_array) {
17204 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17205 			if (*irep != NULL)
17206 				ire_refrele(*irep);
17207 		}
17208 	}
17209 }
17210 
17211 /*
17212  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17213  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17214  * ire_check_and_create_bcast()).
17215  */
17216 static ire_t **
17217 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17218 {
17219 	ipaddr_t addr;
17220 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17221 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17222 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17223 
17224 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17225 
17226 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17227 
17228 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17229 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17230 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17231 
17232 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17233 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17234 
17235 	/*
17236 	 * For backward compatibility, we create net broadcast IREs based on
17237 	 * the old "IP address class system", since some old machines only
17238 	 * respond to these class derived net broadcast.  However, we must not
17239 	 * create these net broadcast IREs if the subnetmask is shorter than
17240 	 * the IP address class based derived netmask.  Otherwise, we may
17241 	 * create a net broadcast address which is the same as an IP address
17242 	 * on the subnet -- and then TCP will refuse to talk to that address.
17243 	 */
17244 	if (netmask < subnetmask) {
17245 		addr = netmask & ipif->ipif_subnet;
17246 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17247 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17248 		    flags);
17249 	}
17250 
17251 	/*
17252 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17253 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17254 	 * created.  Creating these broadcast IREs will only create confusion
17255 	 * as `addr' will be the same as the IP address.
17256 	 */
17257 	if (subnetmask != 0xFFFFFFFF) {
17258 		addr = ipif->ipif_subnet;
17259 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17260 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17261 		    irep, flags);
17262 	}
17263 
17264 	return (irep);
17265 }
17266 
17267 /*
17268  * Broadcast IRE info structure used in the functions below.  Since we
17269  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17270  */
17271 typedef struct bcast_ireinfo {
17272 	uchar_t		bi_type;	/* BCAST_* value from below */
17273 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17274 			bi_needrep:1,	/* do we need to replace it? */
17275 			bi_haverep:1,	/* have we replaced it? */
17276 			bi_pad:5;
17277 	ipaddr_t	bi_addr;	/* IRE address */
17278 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17279 } bcast_ireinfo_t;
17280 
17281 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17282 
17283 /*
17284  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17285  * return B_TRUE if it should immediately be used to recreate the IRE.
17286  */
17287 static boolean_t
17288 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17289 {
17290 	ipaddr_t addr;
17291 
17292 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17293 
17294 	switch (bireinfop->bi_type) {
17295 	case BCAST_NET:
17296 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17297 		if (addr != bireinfop->bi_addr)
17298 			return (B_FALSE);
17299 		break;
17300 	case BCAST_SUBNET:
17301 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17302 			return (B_FALSE);
17303 		break;
17304 	}
17305 
17306 	bireinfop->bi_needrep = 1;
17307 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17308 		if (bireinfop->bi_backup == NULL)
17309 			bireinfop->bi_backup = ipif;
17310 		return (B_FALSE);
17311 	}
17312 	return (B_TRUE);
17313 }
17314 
17315 /*
17316  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17317  * them ala ire_check_and_create_bcast().
17318  */
17319 static ire_t **
17320 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17321 {
17322 	ipaddr_t mask, addr;
17323 
17324 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17325 
17326 	addr = bireinfop->bi_addr;
17327 	irep = ire_create_bcast(ipif, addr, irep);
17328 
17329 	switch (bireinfop->bi_type) {
17330 	case BCAST_NET:
17331 		mask = ip_net_mask(ipif->ipif_subnet);
17332 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17333 		break;
17334 	case BCAST_SUBNET:
17335 		mask = ipif->ipif_net_mask;
17336 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17337 		break;
17338 	}
17339 
17340 	bireinfop->bi_haverep = 1;
17341 	return (irep);
17342 }
17343 
17344 /*
17345  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17346  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17347  * that are going away are still needed.  If so, have ipif_create_bcast()
17348  * recreate them (except for the deprecated case, as explained below).
17349  */
17350 static ire_t **
17351 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17352     ire_t **irep)
17353 {
17354 	int i;
17355 	ipif_t *ipif;
17356 
17357 	ASSERT(!ill->ill_isv6);
17358 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17359 		/*
17360 		 * Skip this ipif if it's (a) the one being taken down, (b)
17361 		 * not in the same zone, or (c) has no valid local address.
17362 		 */
17363 		if (ipif == test_ipif ||
17364 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17365 		    ipif->ipif_subnet == 0 ||
17366 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17367 		    (IPIF_UP|IPIF_BROADCAST))
17368 			continue;
17369 
17370 		/*
17371 		 * For each dying IRE that hasn't yet been replaced, see if
17372 		 * `ipif' needs it and whether the IRE should be recreated on
17373 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17374 		 * will return B_FALSE even if `ipif' needs the IRE on the
17375 		 * hopes that we'll later find a needy non-deprecated ipif.
17376 		 * However, the ipif is recorded in bi_backup for possible
17377 		 * subsequent use by ipif_check_bcast_ires().
17378 		 */
17379 		for (i = 0; i < BCAST_COUNT; i++) {
17380 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17381 				continue;
17382 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17383 				continue;
17384 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17385 		}
17386 
17387 		/*
17388 		 * If we've replaced all of the broadcast IREs that are going
17389 		 * to be taken down, we know we're done.
17390 		 */
17391 		for (i = 0; i < BCAST_COUNT; i++) {
17392 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17393 				break;
17394 		}
17395 		if (i == BCAST_COUNT)
17396 			break;
17397 	}
17398 	return (irep);
17399 }
17400 
17401 /*
17402  * Check if `test_ipif' (which is going away) is associated with any existing
17403  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17404  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17405  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17406  *
17407  * This is necessary because broadcast IREs are shared.  In particular, a
17408  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17409  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17410  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17411  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17412  * same zone, they will share the same set of broadcast IREs.
17413  *
17414  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17415  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17416  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17417  */
17418 static void
17419 ipif_check_bcast_ires(ipif_t *test_ipif)
17420 {
17421 	ill_t		*ill = test_ipif->ipif_ill;
17422 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17423 	ire_t		**irep1, **irep = &ire_array[0];
17424 	uint_t 		i, willdie;
17425 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17426 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17427 
17428 	ASSERT(!test_ipif->ipif_isv6);
17429 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17430 
17431 	/*
17432 	 * No broadcast IREs for the LOOPBACK interface
17433 	 * or others such as point to point and IPIF_NOXMIT.
17434 	 */
17435 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17436 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17437 		return;
17438 
17439 	bzero(bireinfo, sizeof (bireinfo));
17440 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17441 	bireinfo[0].bi_addr = 0;
17442 
17443 	bireinfo[1].bi_type = BCAST_ALLONES;
17444 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17445 
17446 	bireinfo[2].bi_type = BCAST_NET;
17447 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17448 
17449 	if (test_ipif->ipif_net_mask != 0)
17450 		mask = test_ipif->ipif_net_mask;
17451 	bireinfo[3].bi_type = BCAST_SUBNET;
17452 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17453 
17454 	/*
17455 	 * Figure out what (if any) broadcast IREs will die as a result of
17456 	 * `test_ipif' going away.  If none will die, we're done.
17457 	 */
17458 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17459 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17460 		    test_ipif, ALL_ZONES, NULL,
17461 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17462 		if (ire != NULL) {
17463 			willdie++;
17464 			bireinfo[i].bi_willdie = 1;
17465 			ire_refrele(ire);
17466 		}
17467 	}
17468 
17469 	if (willdie == 0)
17470 		return;
17471 
17472 	/*
17473 	 * Walk through all the ipifs that will be affected by the dying IREs,
17474 	 * and recreate the IREs as necessary. Note that all interfaces in an
17475 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17476 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17477 	 * that broadcast IREs end up on it whenever possible).
17478 	 */
17479 	if (IS_UNDER_IPMP(ill))
17480 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17481 
17482 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17483 
17484 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17485 		ipmp_illgrp_t *illg = ill->ill_grp;
17486 
17487 		ill = list_head(&illg->ig_if);
17488 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17489 			for (i = 0; i < BCAST_COUNT; i++) {
17490 				if (bireinfo[i].bi_willdie &&
17491 				    !bireinfo[i].bi_haverep)
17492 					break;
17493 			}
17494 			if (i == BCAST_COUNT)
17495 				break;
17496 
17497 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17498 		}
17499 	}
17500 
17501 	/*
17502 	 * Scan through the set of broadcast IREs and see if there are any
17503 	 * that we need to replace that have not yet been replaced.  If so,
17504 	 * replace them using the appropriate backup ipif.
17505 	 */
17506 	for (i = 0; i < BCAST_COUNT; i++) {
17507 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17508 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17509 			    &bireinfo[i], irep);
17510 	}
17511 
17512 	/*
17513 	 * If we can't create all of them, don't add any of them.  (Code in
17514 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17515 	 * non-loopback copy and loopback copy for a given address.)
17516 	 */
17517 	for (irep1 = irep; irep1 > ire_array; ) {
17518 		irep1--;
17519 		if (*irep1 == NULL) {
17520 			ip0dbg(("ipif_check_bcast_ires: can't create "
17521 			    "IRE_BROADCAST, memory allocation failure\n"));
17522 			while (irep > ire_array) {
17523 				irep--;
17524 				if (*irep != NULL)
17525 					ire_delete(*irep);
17526 			}
17527 			return;
17528 		}
17529 	}
17530 
17531 	for (irep1 = irep; irep1 > ire_array; ) {
17532 		irep1--;
17533 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17534 			ire_refrele(*irep1);		/* Held in ire_add */
17535 	}
17536 }
17537 
17538 /*
17539  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17540  * from lifr_flags and the name from lifr_name.
17541  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17542  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17543  * Returns EINPROGRESS when mp has been consumed by queueing it on
17544  * ill_pending_mp and the ioctl will complete in ip_rput.
17545  *
17546  * Can operate on either a module or a driver queue.
17547  * Returns an error if not a module queue.
17548  */
17549 /* ARGSUSED */
17550 int
17551 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17552     ip_ioctl_cmd_t *ipip, void *if_req)
17553 {
17554 	ill_t	*ill = q->q_ptr;
17555 	phyint_t *phyi;
17556 	ip_stack_t *ipst;
17557 	struct lifreq *lifr = if_req;
17558 	uint64_t new_flags;
17559 
17560 	ASSERT(ipif != NULL);
17561 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17562 
17563 	if (q->q_next == NULL) {
17564 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17565 		return (EINVAL);
17566 	}
17567 
17568 	/*
17569 	 * If we are not writer on 'q' then this interface exists already
17570 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17571 	 * so return EALREADY.
17572 	 */
17573 	if (ill != ipif->ipif_ill)
17574 		return (EALREADY);
17575 
17576 	if (ill->ill_name[0] != '\0')
17577 		return (EALREADY);
17578 
17579 	/*
17580 	 * If there's another ill already with the requested name, ensure
17581 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17582 	 * fuse together two unrelated ills, which will cause chaos.
17583 	 */
17584 	ipst = ill->ill_ipst;
17585 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17586 	    lifr->lifr_name, NULL);
17587 	if (phyi != NULL) {
17588 		ill_t *ill_mate = phyi->phyint_illv4;
17589 
17590 		if (ill_mate == NULL)
17591 			ill_mate = phyi->phyint_illv6;
17592 		ASSERT(ill_mate != NULL);
17593 
17594 		if (ill_mate->ill_media->ip_m_mac_type !=
17595 		    ill->ill_media->ip_m_mac_type) {
17596 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17597 			    "use the same ill name on differing media\n"));
17598 			return (EINVAL);
17599 		}
17600 	}
17601 
17602 	/*
17603 	 * We start off as IFF_IPV4 in ipif_allocate and become
17604 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
17605 	 * The only flags that we read from user space are IFF_IPV4,
17606 	 * IFF_IPV6, IFF_XRESOLV and IFF_BROADCAST.
17607 	 *
17608 	 * This ill has not been inserted into the global list.
17609 	 * So we are still single threaded and don't need any lock
17610 	 *
17611 	 * Saniy check the flags.
17612 	 */
17613 
17614 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17615 	    ((lifr->lifr_flags & IFF_IPV6) ||
17616 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17617 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
17618 		    "or IPv6 i.e., no broadcast \n"));
17619 		return (EINVAL);
17620 	}
17621 
17622 	new_flags =
17623 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_XRESOLV|IFF_BROADCAST);
17624 
17625 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
17626 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
17627 		    "IFF_IPV4 or IFF_IPV6\n"));
17628 		return (EINVAL);
17629 	}
17630 	/*
17631 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17632 	 */
17633 	if ((new_flags & IFF_XRESOLV) && !(new_flags & IFF_IPV6) &&
17634 	    !(ipif->ipif_isv6)) {
17635 		ip1dbg(("ip_sioctl_slifname: XRESOLV only allowed on "
17636 		    "IPv6 interface\n"));
17637 		return (EINVAL);
17638 	}
17639 
17640 	/*
17641 	 * We always start off as IPv4, so only need to check for IPv6.
17642 	 */
17643 	if ((new_flags & IFF_IPV6) != 0) {
17644 		ill->ill_flags |= ILLF_IPV6;
17645 		ill->ill_flags &= ~ILLF_IPV4;
17646 	}
17647 
17648 	if ((new_flags & IFF_BROADCAST) != 0)
17649 		ipif->ipif_flags |= IPIF_BROADCAST;
17650 	else
17651 		ipif->ipif_flags &= ~IPIF_BROADCAST;
17652 
17653 	if ((new_flags & IFF_XRESOLV) != 0)
17654 		ill->ill_flags |= ILLF_XRESOLV;
17655 	else
17656 		ill->ill_flags &= ~ILLF_XRESOLV;
17657 
17658 	/* We started off as V4. */
17659 	if (ill->ill_flags & ILLF_IPV6) {
17660 		ill->ill_phyint->phyint_illv6 = ill;
17661 		ill->ill_phyint->phyint_illv4 = NULL;
17662 	}
17663 
17664 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17665 }
17666 
17667 /* ARGSUSED */
17668 int
17669 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17670     ip_ioctl_cmd_t *ipip, void *if_req)
17671 {
17672 	/*
17673 	 * ill_phyint_reinit merged the v4 and v6 into a single
17674 	 * ipsq.  We might not have been able to complete the
17675 	 * slifname in ipif_set_values, if we could not become
17676 	 * exclusive.  If so restart it here
17677 	 */
17678 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17679 }
17680 
17681 /*
17682  * Return a pointer to the ipif which matches the index, IP version type and
17683  * zoneid.
17684  */
17685 ipif_t *
17686 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17687     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17688 {
17689 	ill_t	*ill;
17690 	ipif_t	*ipif = NULL;
17691 
17692 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17693 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17694 
17695 	if (err != NULL)
17696 		*err = 0;
17697 
17698 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17699 	if (ill != NULL) {
17700 		mutex_enter(&ill->ill_lock);
17701 		for (ipif = ill->ill_ipif; ipif != NULL;
17702 		    ipif = ipif->ipif_next) {
17703 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17704 			    zoneid == ipif->ipif_zoneid ||
17705 			    ipif->ipif_zoneid == ALL_ZONES)) {
17706 				ipif_refhold_locked(ipif);
17707 				break;
17708 			}
17709 		}
17710 		mutex_exit(&ill->ill_lock);
17711 		ill_refrele(ill);
17712 		if (ipif == NULL && err != NULL)
17713 			*err = ENXIO;
17714 	}
17715 	return (ipif);
17716 }
17717 
17718 /*
17719  * Change an existing physical interface's index. If the new index
17720  * is acceptable we update the index and the phyint_list_avl_by_index tree.
17721  * Finally, we update other systems which may have a dependence on the
17722  * index value.
17723  */
17724 /* ARGSUSED */
17725 int
17726 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17727     ip_ioctl_cmd_t *ipip, void *ifreq)
17728 {
17729 	ill_t		*ill;
17730 	phyint_t	*phyi;
17731 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17732 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17733 	uint_t	old_index, index;
17734 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17735 	avl_index_t	where;
17736 
17737 	if (ipip->ipi_cmd_type == IF_CMD)
17738 		index = ifr->ifr_index;
17739 	else
17740 		index = lifr->lifr_index;
17741 
17742 	/*
17743 	 * Only allow on physical interface. Also, index zero is illegal.
17744 	 */
17745 	ill = ipif->ipif_ill;
17746 	phyi = ill->ill_phyint;
17747 	if (ipif->ipif_id != 0 || index == 0) {
17748 		return (EINVAL);
17749 	}
17750 
17751 	/* If the index is not changing, no work to do */
17752 	if (phyi->phyint_ifindex == index)
17753 		return (0);
17754 
17755 	/*
17756 	 * Use phyint_exists() to determine if the new interface index
17757 	 * is already in use. If the index is unused then we need to
17758 	 * change the phyint's position in the phyint_list_avl_by_index
17759 	 * tree. If we do not do this, subsequent lookups (using the new
17760 	 * index value) will not find the phyint.
17761 	 */
17762 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17763 	if (phyint_exists(index, ipst)) {
17764 		rw_exit(&ipst->ips_ill_g_lock);
17765 		return (EEXIST);
17766 	}
17767 
17768 	/* The new index is unused. Set it in the phyint. */
17769 	old_index = phyi->phyint_ifindex;
17770 	phyi->phyint_ifindex = index;
17771 
17772 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
17773 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17774 	    &index, &where);
17775 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17776 	    phyi, where);
17777 	rw_exit(&ipst->ips_ill_g_lock);
17778 
17779 	/* Update SCTP's ILL list */
17780 	sctp_ill_reindex(ill, old_index);
17781 
17782 	/* Send the routing sockets message */
17783 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17784 	if (ILL_OTHER(ill))
17785 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17786 
17787 	return (0);
17788 }
17789 
17790 /* ARGSUSED */
17791 int
17792 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17793     ip_ioctl_cmd_t *ipip, void *ifreq)
17794 {
17795 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17796 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17797 
17798 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17799 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17800 	/* Get the interface index */
17801 	if (ipip->ipi_cmd_type == IF_CMD) {
17802 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17803 	} else {
17804 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17805 	}
17806 	return (0);
17807 }
17808 
17809 /* ARGSUSED */
17810 int
17811 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17812     ip_ioctl_cmd_t *ipip, void *ifreq)
17813 {
17814 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17815 
17816 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17817 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17818 	/* Get the interface zone */
17819 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17820 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17821 	return (0);
17822 }
17823 
17824 /*
17825  * Set the zoneid of an interface.
17826  */
17827 /* ARGSUSED */
17828 int
17829 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17830     ip_ioctl_cmd_t *ipip, void *ifreq)
17831 {
17832 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17833 	int err = 0;
17834 	boolean_t need_up = B_FALSE;
17835 	zone_t *zptr;
17836 	zone_status_t status;
17837 	zoneid_t zoneid;
17838 
17839 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17840 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17841 		if (!is_system_labeled())
17842 			return (ENOTSUP);
17843 		zoneid = GLOBAL_ZONEID;
17844 	}
17845 
17846 	/* cannot assign instance zero to a non-global zone */
17847 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17848 		return (ENOTSUP);
17849 
17850 	/*
17851 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17852 	 * the event of a race with the zone shutdown processing, since IP
17853 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17854 	 * interface will be cleaned up even if the zone is shut down
17855 	 * immediately after the status check. If the interface can't be brought
17856 	 * down right away, and the zone is shut down before the restart
17857 	 * function is called, we resolve the possible races by rechecking the
17858 	 * zone status in the restart function.
17859 	 */
17860 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17861 		return (EINVAL);
17862 	status = zone_status_get(zptr);
17863 	zone_rele(zptr);
17864 
17865 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17866 		return (EINVAL);
17867 
17868 	if (ipif->ipif_flags & IPIF_UP) {
17869 		/*
17870 		 * If the interface is already marked up,
17871 		 * we call ipif_down which will take care
17872 		 * of ditching any IREs that have been set
17873 		 * up based on the old interface address.
17874 		 */
17875 		err = ipif_logical_down(ipif, q, mp);
17876 		if (err == EINPROGRESS)
17877 			return (err);
17878 		ipif_down_tail(ipif);
17879 		need_up = B_TRUE;
17880 	}
17881 
17882 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17883 	return (err);
17884 }
17885 
17886 static int
17887 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17888     queue_t *q, mblk_t *mp, boolean_t need_up)
17889 {
17890 	int	err = 0;
17891 	ip_stack_t	*ipst;
17892 
17893 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17894 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17895 
17896 	if (CONN_Q(q))
17897 		ipst = CONNQ_TO_IPST(q);
17898 	else
17899 		ipst = ILLQ_TO_IPST(q);
17900 
17901 	/*
17902 	 * For exclusive stacks we don't allow a different zoneid than
17903 	 * global.
17904 	 */
17905 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17906 	    zoneid != GLOBAL_ZONEID)
17907 		return (EINVAL);
17908 
17909 	/* Set the new zone id. */
17910 	ipif->ipif_zoneid = zoneid;
17911 
17912 	/* Update sctp list */
17913 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
17914 
17915 	if (need_up) {
17916 		/*
17917 		 * Now bring the interface back up.  If this
17918 		 * is the only IPIF for the ILL, ipif_up
17919 		 * will have to re-bind to the device, so
17920 		 * we may get back EINPROGRESS, in which
17921 		 * case, this IOCTL will get completed in
17922 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
17923 		 */
17924 		err = ipif_up(ipif, q, mp);
17925 	}
17926 	return (err);
17927 }
17928 
17929 /* ARGSUSED */
17930 int
17931 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17932     ip_ioctl_cmd_t *ipip, void *if_req)
17933 {
17934 	struct lifreq *lifr = (struct lifreq *)if_req;
17935 	zoneid_t zoneid;
17936 	zone_t *zptr;
17937 	zone_status_t status;
17938 
17939 	ASSERT(ipif->ipif_id != 0);
17940 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17941 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
17942 		zoneid = GLOBAL_ZONEID;
17943 
17944 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
17945 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17946 
17947 	/*
17948 	 * We recheck the zone status to resolve the following race condition:
17949 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
17950 	 * 2) hme0:1 is up and can't be brought down right away;
17951 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
17952 	 * 3) zone "myzone" is halted; the zone status switches to
17953 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
17954 	 * the interfaces to remove - hme0:1 is not returned because it's not
17955 	 * yet in "myzone", so it won't be removed;
17956 	 * 4) the restart function for SIOCSLIFZONE is called; without the
17957 	 * status check here, we would have hme0:1 in "myzone" after it's been
17958 	 * destroyed.
17959 	 * Note that if the status check fails, we need to bring the interface
17960 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
17961 	 * ipif_up_done[_v6]().
17962 	 */
17963 	status = ZONE_IS_UNINITIALIZED;
17964 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
17965 		status = zone_status_get(zptr);
17966 		zone_rele(zptr);
17967 	}
17968 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
17969 		if (ipif->ipif_isv6) {
17970 			(void) ipif_up_done_v6(ipif);
17971 		} else {
17972 			(void) ipif_up_done(ipif);
17973 		}
17974 		return (EINVAL);
17975 	}
17976 
17977 	ipif_down_tail(ipif);
17978 
17979 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
17980 	    B_TRUE));
17981 }
17982 
17983 /*
17984  * Return the number of addresses on `ill' with one or more of the values
17985  * in `set' set and all of the values in `clear' clear.
17986  */
17987 static uint_t
17988 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
17989 {
17990 	ipif_t	*ipif;
17991 	uint_t	cnt = 0;
17992 
17993 	ASSERT(IAM_WRITER_ILL(ill));
17994 
17995 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
17996 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
17997 			cnt++;
17998 
17999 	return (cnt);
18000 }
18001 
18002 /*
18003  * Return the number of migratable addresses on `ill' that are under
18004  * application control.
18005  */
18006 uint_t
18007 ill_appaddr_cnt(const ill_t *ill)
18008 {
18009 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
18010 	    IPIF_NOFAILOVER));
18011 }
18012 
18013 /*
18014  * Return the number of point-to-point addresses on `ill'.
18015  */
18016 uint_t
18017 ill_ptpaddr_cnt(const ill_t *ill)
18018 {
18019 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
18020 }
18021 
18022 /* ARGSUSED */
18023 int
18024 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18025 	ip_ioctl_cmd_t *ipip, void *ifreq)
18026 {
18027 	struct lifreq	*lifr = ifreq;
18028 
18029 	ASSERT(q->q_next == NULL);
18030 	ASSERT(CONN_Q(q));
18031 
18032 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
18033 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
18034 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
18035 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
18036 
18037 	return (0);
18038 }
18039 
18040 /* Find the previous ILL in this usesrc group */
18041 static ill_t *
18042 ill_prev_usesrc(ill_t *uill)
18043 {
18044 	ill_t *ill;
18045 
18046 	for (ill = uill->ill_usesrc_grp_next;
18047 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
18048 	    ill = ill->ill_usesrc_grp_next)
18049 		/* do nothing */;
18050 	return (ill);
18051 }
18052 
18053 /*
18054  * Release all members of the usesrc group. This routine is called
18055  * from ill_delete when the interface being unplumbed is the
18056  * group head.
18057  */
18058 static void
18059 ill_disband_usesrc_group(ill_t *uill)
18060 {
18061 	ill_t *next_ill, *tmp_ill;
18062 	ip_stack_t	*ipst = uill->ill_ipst;
18063 
18064 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18065 	next_ill = uill->ill_usesrc_grp_next;
18066 
18067 	do {
18068 		ASSERT(next_ill != NULL);
18069 		tmp_ill = next_ill->ill_usesrc_grp_next;
18070 		ASSERT(tmp_ill != NULL);
18071 		next_ill->ill_usesrc_grp_next = NULL;
18072 		next_ill->ill_usesrc_ifindex = 0;
18073 		next_ill = tmp_ill;
18074 	} while (next_ill->ill_usesrc_ifindex != 0);
18075 	uill->ill_usesrc_grp_next = NULL;
18076 }
18077 
18078 /*
18079  * Remove the client usesrc ILL from the list and relink to a new list
18080  */
18081 int
18082 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18083 {
18084 	ill_t *ill, *tmp_ill;
18085 	ip_stack_t	*ipst = ucill->ill_ipst;
18086 
18087 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18088 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18089 
18090 	/*
18091 	 * Check if the usesrc client ILL passed in is not already
18092 	 * in use as a usesrc ILL i.e one whose source address is
18093 	 * in use OR a usesrc ILL is not already in use as a usesrc
18094 	 * client ILL
18095 	 */
18096 	if ((ucill->ill_usesrc_ifindex == 0) ||
18097 	    (uill->ill_usesrc_ifindex != 0)) {
18098 		return (-1);
18099 	}
18100 
18101 	ill = ill_prev_usesrc(ucill);
18102 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18103 
18104 	/* Remove from the current list */
18105 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18106 		/* Only two elements in the list */
18107 		ASSERT(ill->ill_usesrc_ifindex == 0);
18108 		ill->ill_usesrc_grp_next = NULL;
18109 	} else {
18110 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18111 	}
18112 
18113 	if (ifindex == 0) {
18114 		ucill->ill_usesrc_ifindex = 0;
18115 		ucill->ill_usesrc_grp_next = NULL;
18116 		return (0);
18117 	}
18118 
18119 	ucill->ill_usesrc_ifindex = ifindex;
18120 	tmp_ill = uill->ill_usesrc_grp_next;
18121 	uill->ill_usesrc_grp_next = ucill;
18122 	ucill->ill_usesrc_grp_next =
18123 	    (tmp_ill != NULL) ? tmp_ill : uill;
18124 	return (0);
18125 }
18126 
18127 /*
18128  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18129  * ip.c for locking details.
18130  */
18131 /* ARGSUSED */
18132 int
18133 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18134     ip_ioctl_cmd_t *ipip, void *ifreq)
18135 {
18136 	struct lifreq *lifr = (struct lifreq *)ifreq;
18137 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18138 	    ill_flag_changed = B_FALSE;
18139 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18140 	int err = 0, ret;
18141 	uint_t ifindex;
18142 	ipsq_t *ipsq = NULL;
18143 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18144 
18145 	ASSERT(IAM_WRITER_IPIF(ipif));
18146 	ASSERT(q->q_next == NULL);
18147 	ASSERT(CONN_Q(q));
18148 
18149 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18150 
18151 	ifindex = lifr->lifr_index;
18152 	if (ifindex == 0) {
18153 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18154 			/* non usesrc group interface, nothing to reset */
18155 			return (0);
18156 		}
18157 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18158 		/* valid reset request */
18159 		reset_flg = B_TRUE;
18160 	}
18161 
18162 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18163 	    ip_process_ioctl, &err, ipst);
18164 	if (usesrc_ill == NULL) {
18165 		return (err);
18166 	}
18167 
18168 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18169 	    NEW_OP, B_TRUE);
18170 	if (ipsq == NULL) {
18171 		err = EINPROGRESS;
18172 		/* Operation enqueued on the ipsq of the usesrc ILL */
18173 		goto done;
18174 	}
18175 
18176 	/* USESRC isn't currently supported with IPMP */
18177 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18178 		err = ENOTSUP;
18179 		goto done;
18180 	}
18181 
18182 	/*
18183 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18184 	 * used by IPMP underlying interfaces, but someone might think it's
18185 	 * more general and try to use it independently with VNI.)
18186 	 */
18187 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18188 		err = ENOTSUP;
18189 		goto done;
18190 	}
18191 
18192 	/*
18193 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18194 	 * already a client then return EINVAL
18195 	 */
18196 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18197 		err = EINVAL;
18198 		goto done;
18199 	}
18200 
18201 	/*
18202 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18203 	 * be then this is a duplicate operation.
18204 	 */
18205 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18206 		err = 0;
18207 		goto done;
18208 	}
18209 
18210 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18211 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18212 	    usesrc_ill->ill_isv6));
18213 
18214 	/*
18215 	 * The next step ensures that no new ires will be created referencing
18216 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18217 	 * we go through an ire walk deleting all ire caches that reference
18218 	 * the client ill. New ires referencing the client ill that are added
18219 	 * to the ire table before the ILL_CHANGING flag is set, will be
18220 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18221 	 * the client ill while the ILL_CHANGING flag is set will be failed
18222 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18223 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18224 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18225 	 * belong to the same usesrc group.
18226 	 */
18227 	mutex_enter(&usesrc_cli_ill->ill_lock);
18228 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18229 	mutex_exit(&usesrc_cli_ill->ill_lock);
18230 	ill_flag_changed = B_TRUE;
18231 
18232 	if (ipif->ipif_isv6)
18233 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18234 		    ALL_ZONES, ipst);
18235 	else
18236 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18237 		    ALL_ZONES, ipst);
18238 
18239 	/*
18240 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18241 	 * and the ill_usesrc_ifindex fields
18242 	 */
18243 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18244 
18245 	if (reset_flg) {
18246 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18247 		if (ret != 0) {
18248 			err = EINVAL;
18249 		}
18250 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18251 		goto done;
18252 	}
18253 
18254 	/*
18255 	 * Four possibilities to consider:
18256 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18257 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18258 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18259 	 * 4. Both are part of their respective usesrc groups
18260 	 */
18261 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18262 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18263 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18264 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18265 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18266 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18267 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18268 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18269 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18270 		/* Insert at head of list */
18271 		usesrc_cli_ill->ill_usesrc_grp_next =
18272 		    usesrc_ill->ill_usesrc_grp_next;
18273 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18274 	} else {
18275 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18276 		    ifindex);
18277 		if (ret != 0)
18278 			err = EINVAL;
18279 	}
18280 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18281 
18282 done:
18283 	if (ill_flag_changed) {
18284 		mutex_enter(&usesrc_cli_ill->ill_lock);
18285 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18286 		mutex_exit(&usesrc_cli_ill->ill_lock);
18287 	}
18288 	if (ipsq != NULL)
18289 		ipsq_exit(ipsq);
18290 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18291 	ill_refrele(usesrc_ill);
18292 	return (err);
18293 }
18294 
18295 /*
18296  * comparison function used by avl.
18297  */
18298 static int
18299 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18300 {
18301 
18302 	uint_t index;
18303 
18304 	ASSERT(phyip != NULL && index_ptr != NULL);
18305 
18306 	index = *((uint_t *)index_ptr);
18307 	/*
18308 	 * let the phyint with the lowest index be on top.
18309 	 */
18310 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18311 		return (1);
18312 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18313 		return (-1);
18314 	return (0);
18315 }
18316 
18317 /*
18318  * comparison function used by avl.
18319  */
18320 static int
18321 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18322 {
18323 	ill_t *ill;
18324 	int res = 0;
18325 
18326 	ASSERT(phyip != NULL && name_ptr != NULL);
18327 
18328 	if (((phyint_t *)phyip)->phyint_illv4)
18329 		ill = ((phyint_t *)phyip)->phyint_illv4;
18330 	else
18331 		ill = ((phyint_t *)phyip)->phyint_illv6;
18332 	ASSERT(ill != NULL);
18333 
18334 	res = strcmp(ill->ill_name, (char *)name_ptr);
18335 	if (res > 0)
18336 		return (1);
18337 	else if (res < 0)
18338 		return (-1);
18339 	return (0);
18340 }
18341 
18342 /*
18343  * This function is called on the unplumb path via ill_glist_delete() when
18344  * there are no ills left on the phyint and thus the phyint can be freed.
18345  */
18346 static void
18347 phyint_free(phyint_t *phyi)
18348 {
18349 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18350 
18351 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18352 
18353 	/*
18354 	 * If this phyint was an IPMP meta-interface, blow away the group.
18355 	 * This is safe to do because all of the illgrps have already been
18356 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18357 	 * If we're cleaning up as a result of failed initialization,
18358 	 * phyint_grp may be NULL.
18359 	 */
18360 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18361 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18362 		ipmp_grp_destroy(phyi->phyint_grp);
18363 		phyi->phyint_grp = NULL;
18364 		rw_exit(&ipst->ips_ipmp_lock);
18365 	}
18366 
18367 	/*
18368 	 * If this interface was under IPMP, take it out of the group.
18369 	 */
18370 	if (phyi->phyint_grp != NULL)
18371 		ipmp_phyint_leave_grp(phyi);
18372 
18373 	/*
18374 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18375 	 * will be freed in ipsq_exit().
18376 	 */
18377 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18378 	phyi->phyint_name[0] = '\0';
18379 
18380 	mi_free(phyi);
18381 }
18382 
18383 /*
18384  * Attach the ill to the phyint structure which can be shared by both
18385  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18386  * function is called from ipif_set_values and ill_lookup_on_name (for
18387  * loopback) where we know the name of the ill. We lookup the ill and if
18388  * there is one present already with the name use that phyint. Otherwise
18389  * reuse the one allocated by ill_init.
18390  */
18391 static void
18392 ill_phyint_reinit(ill_t *ill)
18393 {
18394 	boolean_t isv6 = ill->ill_isv6;
18395 	phyint_t *phyi_old;
18396 	phyint_t *phyi;
18397 	avl_index_t where = 0;
18398 	ill_t	*ill_other = NULL;
18399 	ip_stack_t	*ipst = ill->ill_ipst;
18400 
18401 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18402 
18403 	phyi_old = ill->ill_phyint;
18404 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18405 	    phyi_old->phyint_illv6 == NULL));
18406 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18407 	    phyi_old->phyint_illv4 == NULL));
18408 	ASSERT(phyi_old->phyint_ifindex == 0);
18409 
18410 	/*
18411 	 * Now that our ill has a name, set it in the phyint.
18412 	 */
18413 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18414 
18415 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18416 	    ill->ill_name, &where);
18417 
18418 	/*
18419 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18420 	 *    the global list of ills. So no other thread could have located
18421 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18422 	 * 2. Now locate the other protocol instance of this ill.
18423 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18424 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18425 	 *    of neither ill can change.
18426 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18427 	 *    other ill.
18428 	 * 5. Release all locks.
18429 	 */
18430 
18431 	/*
18432 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18433 	 * we are initializing IPv4.
18434 	 */
18435 	if (phyi != NULL) {
18436 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18437 		ASSERT(ill_other->ill_phyint != NULL);
18438 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18439 		    (!isv6 && ill_other->ill_isv6));
18440 		GRAB_ILL_LOCKS(ill, ill_other);
18441 		/*
18442 		 * We are potentially throwing away phyint_flags which
18443 		 * could be different from the one that we obtain from
18444 		 * ill_other->ill_phyint. But it is okay as we are assuming
18445 		 * that the state maintained within IP is correct.
18446 		 */
18447 		mutex_enter(&phyi->phyint_lock);
18448 		if (isv6) {
18449 			ASSERT(phyi->phyint_illv6 == NULL);
18450 			phyi->phyint_illv6 = ill;
18451 		} else {
18452 			ASSERT(phyi->phyint_illv4 == NULL);
18453 			phyi->phyint_illv4 = ill;
18454 		}
18455 
18456 		/*
18457 		 * Delete the old phyint and make its ipsq eligible
18458 		 * to be freed in ipsq_exit().
18459 		 */
18460 		phyi_old->phyint_illv4 = NULL;
18461 		phyi_old->phyint_illv6 = NULL;
18462 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18463 		phyi_old->phyint_name[0] = '\0';
18464 		mi_free(phyi_old);
18465 	} else {
18466 		mutex_enter(&ill->ill_lock);
18467 		/*
18468 		 * We don't need to acquire any lock, since
18469 		 * the ill is not yet visible globally  and we
18470 		 * have not yet released the ill_g_lock.
18471 		 */
18472 		phyi = phyi_old;
18473 		mutex_enter(&phyi->phyint_lock);
18474 		/* XXX We need a recovery strategy here. */
18475 		if (!phyint_assign_ifindex(phyi, ipst))
18476 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18477 
18478 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18479 		    (void *)phyi, where);
18480 
18481 		(void) avl_find(&ipst->ips_phyint_g_list->
18482 		    phyint_list_avl_by_index,
18483 		    &phyi->phyint_ifindex, &where);
18484 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18485 		    (void *)phyi, where);
18486 	}
18487 
18488 	/*
18489 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18490 	 * pending mp is not affected because that is per ill basis.
18491 	 */
18492 	ill->ill_phyint = phyi;
18493 
18494 	/*
18495 	 * Now that the phyint's ifindex has been assigned, complete the
18496 	 * remaining
18497 	 */
18498 
18499 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18500 	if (ill->ill_isv6) {
18501 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18502 		    ill->ill_phyint->phyint_ifindex;
18503 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18504 	} else {
18505 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18506 	}
18507 
18508 	/*
18509 	 * Generate an event within the hooks framework to indicate that
18510 	 * a new interface has just been added to IP.  For this event to
18511 	 * be generated, the network interface must, at least, have an
18512 	 * ifindex assigned to it.  (We don't generate the event for
18513 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
18514 	 *
18515 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18516 	 * that the ordering of delivered events to listeners matches the
18517 	 * order of them in the kernel.
18518 	 */
18519 	if (!IS_LOOPBACK(ill)) {
18520 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18521 		    ill->ill_name_length);
18522 	}
18523 	RELEASE_ILL_LOCKS(ill, ill_other);
18524 	mutex_exit(&phyi->phyint_lock);
18525 }
18526 
18527 /*
18528  * Notify any downstream modules of the name of this interface.
18529  * An M_IOCTL is used even though we don't expect a successful reply.
18530  * Any reply message from the driver (presumably an M_IOCNAK) will
18531  * eventually get discarded somewhere upstream.  The message format is
18532  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18533  * to IP.
18534  */
18535 static void
18536 ip_ifname_notify(ill_t *ill, queue_t *q)
18537 {
18538 	mblk_t *mp1, *mp2;
18539 	struct iocblk *iocp;
18540 	struct lifreq *lifr;
18541 
18542 	mp1 = mkiocb(SIOCSLIFNAME);
18543 	if (mp1 == NULL)
18544 		return;
18545 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18546 	if (mp2 == NULL) {
18547 		freeb(mp1);
18548 		return;
18549 	}
18550 
18551 	mp1->b_cont = mp2;
18552 	iocp = (struct iocblk *)mp1->b_rptr;
18553 	iocp->ioc_count = sizeof (struct lifreq);
18554 
18555 	lifr = (struct lifreq *)mp2->b_rptr;
18556 	mp2->b_wptr += sizeof (struct lifreq);
18557 	bzero(lifr, sizeof (struct lifreq));
18558 
18559 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18560 	lifr->lifr_ppa = ill->ill_ppa;
18561 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18562 
18563 	putnext(q, mp1);
18564 }
18565 
18566 static int
18567 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18568 {
18569 	int		err;
18570 	ip_stack_t	*ipst = ill->ill_ipst;
18571 	phyint_t	*phyi = ill->ill_phyint;
18572 
18573 	/* Set the obsolete NDD per-interface forwarding name. */
18574 	err = ill_set_ndd_name(ill);
18575 	if (err != 0) {
18576 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18577 		    err);
18578 	}
18579 
18580 	/*
18581 	 * Now that ill_name is set, the configuration for the IPMP
18582 	 * meta-interface can be performed.
18583 	 */
18584 	if (IS_IPMP(ill)) {
18585 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18586 		/*
18587 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18588 		 * meta-interface and we need to create the IPMP group.
18589 		 */
18590 		if (phyi->phyint_grp == NULL) {
18591 			/*
18592 			 * If someone has renamed another IPMP group to have
18593 			 * the same name as our interface, bail.
18594 			 */
18595 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18596 				rw_exit(&ipst->ips_ipmp_lock);
18597 				return (EEXIST);
18598 			}
18599 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18600 			if (phyi->phyint_grp == NULL) {
18601 				rw_exit(&ipst->ips_ipmp_lock);
18602 				return (ENOMEM);
18603 			}
18604 		}
18605 		rw_exit(&ipst->ips_ipmp_lock);
18606 	}
18607 
18608 	/* Tell downstream modules where they are. */
18609 	ip_ifname_notify(ill, q);
18610 
18611 	/*
18612 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18613 	 * Error cases are ENOMEM ...
18614 	 */
18615 	err = ill_dl_phys(ill, ipif, mp, q);
18616 
18617 	/*
18618 	 * If there is no IRE expiration timer running, get one started.
18619 	 * igmp and mld timers will be triggered by the first multicast
18620 	 */
18621 	if (ipst->ips_ip_ire_expire_id == 0) {
18622 		/*
18623 		 * acquire the lock and check again.
18624 		 */
18625 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18626 		if (ipst->ips_ip_ire_expire_id == 0) {
18627 			ipst->ips_ip_ire_expire_id = timeout(
18628 			    ip_trash_timer_expire, ipst,
18629 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18630 		}
18631 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18632 	}
18633 
18634 	if (ill->ill_isv6) {
18635 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18636 		if (ipst->ips_mld_slowtimeout_id == 0) {
18637 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18638 			    (void *)ipst,
18639 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18640 		}
18641 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18642 	} else {
18643 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18644 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18645 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18646 			    (void *)ipst,
18647 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18648 		}
18649 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18650 	}
18651 
18652 	return (err);
18653 }
18654 
18655 /*
18656  * Common routine for ppa and ifname setting. Should be called exclusive.
18657  *
18658  * Returns EINPROGRESS when mp has been consumed by queueing it on
18659  * ill_pending_mp and the ioctl will complete in ip_rput.
18660  *
18661  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18662  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18663  * For SLIFNAME, we pass these values back to the userland.
18664  */
18665 static int
18666 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18667 {
18668 	ill_t	*ill;
18669 	ipif_t	*ipif;
18670 	ipsq_t	*ipsq;
18671 	char	*ppa_ptr;
18672 	char	*old_ptr;
18673 	char	old_char;
18674 	int	error;
18675 	ip_stack_t	*ipst;
18676 
18677 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18678 	ASSERT(q->q_next != NULL);
18679 	ASSERT(interf_name != NULL);
18680 
18681 	ill = (ill_t *)q->q_ptr;
18682 	ipst = ill->ill_ipst;
18683 
18684 	ASSERT(ill->ill_ipst != NULL);
18685 	ASSERT(ill->ill_name[0] == '\0');
18686 	ASSERT(IAM_WRITER_ILL(ill));
18687 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18688 	ASSERT(ill->ill_ppa == UINT_MAX);
18689 
18690 	/* The ppa is sent down by ifconfig or is chosen */
18691 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18692 		return (EINVAL);
18693 	}
18694 
18695 	/*
18696 	 * make sure ppa passed in is same as ppa in the name.
18697 	 * This check is not made when ppa == UINT_MAX in that case ppa
18698 	 * in the name could be anything. System will choose a ppa and
18699 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18700 	 */
18701 	if (*new_ppa_ptr != UINT_MAX) {
18702 		/* stoi changes the pointer */
18703 		old_ptr = ppa_ptr;
18704 		/*
18705 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18706 		 * (they don't have an externally visible ppa).  We assign one
18707 		 * here so that we can manage the interface.  Note that in
18708 		 * the past this value was always 0 for DLPI 1 drivers.
18709 		 */
18710 		if (*new_ppa_ptr == 0)
18711 			*new_ppa_ptr = stoi(&old_ptr);
18712 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18713 			return (EINVAL);
18714 	}
18715 	/*
18716 	 * terminate string before ppa
18717 	 * save char at that location.
18718 	 */
18719 	old_char = ppa_ptr[0];
18720 	ppa_ptr[0] = '\0';
18721 
18722 	ill->ill_ppa = *new_ppa_ptr;
18723 	/*
18724 	 * Finish as much work now as possible before calling ill_glist_insert
18725 	 * which makes the ill globally visible and also merges it with the
18726 	 * other protocol instance of this phyint. The remaining work is
18727 	 * done after entering the ipsq which may happen sometime later.
18728 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18729 	 */
18730 	ipif = ill->ill_ipif;
18731 
18732 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18733 	ipif_assign_seqid(ipif);
18734 
18735 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18736 		ill->ill_flags |= ILLF_IPV4;
18737 
18738 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18739 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18740 
18741 	if (ill->ill_flags & ILLF_IPV6) {
18742 
18743 		ill->ill_isv6 = B_TRUE;
18744 		if (ill->ill_rq != NULL) {
18745 			ill->ill_rq->q_qinfo = &iprinitv6;
18746 			ill->ill_wq->q_qinfo = &ipwinitv6;
18747 		}
18748 
18749 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18750 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18751 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18752 		ipif->ipif_v6subnet = ipv6_all_zeros;
18753 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18754 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18755 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18756 		/*
18757 		 * point-to-point or Non-mulicast capable
18758 		 * interfaces won't do NUD unless explicitly
18759 		 * configured to do so.
18760 		 */
18761 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18762 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18763 			ill->ill_flags |= ILLF_NONUD;
18764 		}
18765 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18766 		if (ill->ill_flags & ILLF_NOARP) {
18767 			/*
18768 			 * Note: xresolv interfaces will eventually need
18769 			 * NOARP set here as well, but that will require
18770 			 * those external resolvers to have some
18771 			 * knowledge of that flag and act appropriately.
18772 			 * Not to be changed at present.
18773 			 */
18774 			ill->ill_flags &= ~ILLF_NOARP;
18775 		}
18776 		/*
18777 		 * Set the ILLF_ROUTER flag according to the global
18778 		 * IPv6 forwarding policy.
18779 		 */
18780 		if (ipst->ips_ipv6_forward != 0)
18781 			ill->ill_flags |= ILLF_ROUTER;
18782 	} else if (ill->ill_flags & ILLF_IPV4) {
18783 		ill->ill_isv6 = B_FALSE;
18784 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18785 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18786 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18787 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18788 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18789 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18790 		/*
18791 		 * Set the ILLF_ROUTER flag according to the global
18792 		 * IPv4 forwarding policy.
18793 		 */
18794 		if (ipst->ips_ip_g_forward != 0)
18795 			ill->ill_flags |= ILLF_ROUTER;
18796 	}
18797 
18798 	ASSERT(ill->ill_phyint != NULL);
18799 
18800 	/*
18801 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18802 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18803 	 */
18804 	if (!ill_allocate_mibs(ill))
18805 		return (ENOMEM);
18806 
18807 	/*
18808 	 * Pick a default sap until we get the DL_INFO_ACK back from
18809 	 * the driver.
18810 	 */
18811 	if (ill->ill_sap == 0) {
18812 		if (ill->ill_isv6)
18813 			ill->ill_sap = IP6_DL_SAP;
18814 		else
18815 			ill->ill_sap = IP_DL_SAP;
18816 	}
18817 
18818 	ill->ill_ifname_pending = 1;
18819 	ill->ill_ifname_pending_err = 0;
18820 
18821 	/*
18822 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18823 	 * that were joined while this ill was not bound to the DLPI link need
18824 	 * to be recovered by ill_recover_multicast().
18825 	 */
18826 	ill->ill_need_recover_multicast = 1;
18827 
18828 	ill_refhold(ill);
18829 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18830 	if ((error = ill_glist_insert(ill, interf_name,
18831 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18832 		ill->ill_ppa = UINT_MAX;
18833 		ill->ill_name[0] = '\0';
18834 		/*
18835 		 * undo null termination done above.
18836 		 */
18837 		ppa_ptr[0] = old_char;
18838 		rw_exit(&ipst->ips_ill_g_lock);
18839 		ill_refrele(ill);
18840 		return (error);
18841 	}
18842 
18843 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18844 
18845 	/*
18846 	 * When we return the buffer pointed to by interf_name should contain
18847 	 * the same name as in ill_name.
18848 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18849 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18850 	 * so copy full name and update the ppa ptr.
18851 	 * When ppa passed in != UINT_MAX all values are correct just undo
18852 	 * null termination, this saves a bcopy.
18853 	 */
18854 	if (*new_ppa_ptr == UINT_MAX) {
18855 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18856 		*new_ppa_ptr = ill->ill_ppa;
18857 	} else {
18858 		/*
18859 		 * undo null termination done above.
18860 		 */
18861 		ppa_ptr[0] = old_char;
18862 	}
18863 
18864 	/* Let SCTP know about this ILL */
18865 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18866 
18867 	/*
18868 	 * ill_glist_insert has made the ill visible globally, and
18869 	 * ill_phyint_reinit could have changed the ipsq. At this point,
18870 	 * we need to hold the ips_ill_g_lock across the call to enter the
18871 	 * ipsq to enforce atomicity and prevent reordering. In the event
18872 	 * the ipsq has changed, and if the new ipsq is currently busy,
18873 	 * we need to make sure that this half-completed ioctl is ahead of
18874 	 * any subsequent ioctl. We achieve this by not dropping the
18875 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
18876 	 * ensuring that new ioctls can't start.
18877 	 */
18878 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18879 	    B_TRUE);
18880 
18881 	rw_exit(&ipst->ips_ill_g_lock);
18882 	ill_refrele(ill);
18883 	if (ipsq == NULL)
18884 		return (EINPROGRESS);
18885 
18886 	/*
18887 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18888 	 */
18889 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18890 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18891 	else
18892 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18893 
18894 	error = ipif_set_values_tail(ill, ipif, mp, q);
18895 	ipsq_exit(ipsq);
18896 	if (error != 0 && error != EINPROGRESS) {
18897 		/*
18898 		 * restore previous values
18899 		 */
18900 		ill->ill_isv6 = B_FALSE;
18901 	}
18902 	return (error);
18903 }
18904 
18905 void
18906 ipif_init(ip_stack_t *ipst)
18907 {
18908 	int i;
18909 
18910 	for (i = 0; i < MAX_G_HEADS; i++) {
18911 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18912 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18913 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
18914 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18915 	}
18916 
18917 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18918 	    ill_phyint_compare_index,
18919 	    sizeof (phyint_t),
18920 	    offsetof(struct phyint, phyint_avl_by_index));
18921 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18922 	    ill_phyint_compare_name,
18923 	    sizeof (phyint_t),
18924 	    offsetof(struct phyint, phyint_avl_by_name));
18925 }
18926 
18927 /*
18928  * Lookup the ipif corresponding to the onlink destination address. For
18929  * point-to-point interfaces, it matches with remote endpoint destination
18930  * address. For point-to-multipoint interfaces it only tries to match the
18931  * destination with the interface's subnet address. The longest, most specific
18932  * match is found to take care of such rare network configurations like -
18933  * le0: 129.146.1.1/16
18934  * le1: 129.146.2.2/24
18935  *
18936  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
18937  * supported on underlying interfaces in an IPMP group, underlying interfaces
18938  * are ignored when looking up a match.  (If we didn't ignore them, we'd
18939  * risk using a test address as a source for outgoing traffic.)
18940  */
18941 ipif_t *
18942 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
18943 {
18944 	ipif_t	*ipif, *best_ipif;
18945 	ill_t	*ill;
18946 	ill_walk_context_t ctx;
18947 
18948 	ASSERT(zoneid != ALL_ZONES);
18949 	best_ipif = NULL;
18950 
18951 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18952 	ill = ILL_START_WALK_V4(&ctx, ipst);
18953 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18954 		if (IS_UNDER_IPMP(ill))
18955 			continue;
18956 		mutex_enter(&ill->ill_lock);
18957 		for (ipif = ill->ill_ipif; ipif != NULL;
18958 		    ipif = ipif->ipif_next) {
18959 			if (!IPIF_CAN_LOOKUP(ipif))
18960 				continue;
18961 			if (ipif->ipif_zoneid != zoneid &&
18962 			    ipif->ipif_zoneid != ALL_ZONES)
18963 				continue;
18964 			/*
18965 			 * Point-to-point case. Look for exact match with
18966 			 * destination address.
18967 			 */
18968 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18969 				if (ipif->ipif_pp_dst_addr == addr) {
18970 					ipif_refhold_locked(ipif);
18971 					mutex_exit(&ill->ill_lock);
18972 					rw_exit(&ipst->ips_ill_g_lock);
18973 					if (best_ipif != NULL)
18974 						ipif_refrele(best_ipif);
18975 					return (ipif);
18976 				}
18977 			} else if (ipif->ipif_subnet == (addr &
18978 			    ipif->ipif_net_mask)) {
18979 				/*
18980 				 * Point-to-multipoint case. Looping through to
18981 				 * find the most specific match. If there are
18982 				 * multiple best match ipif's then prefer ipif's
18983 				 * that are UP. If there is only one best match
18984 				 * ipif and it is DOWN we must still return it.
18985 				 */
18986 				if ((best_ipif == NULL) ||
18987 				    (ipif->ipif_net_mask >
18988 				    best_ipif->ipif_net_mask) ||
18989 				    ((ipif->ipif_net_mask ==
18990 				    best_ipif->ipif_net_mask) &&
18991 				    ((ipif->ipif_flags & IPIF_UP) &&
18992 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
18993 					ipif_refhold_locked(ipif);
18994 					mutex_exit(&ill->ill_lock);
18995 					rw_exit(&ipst->ips_ill_g_lock);
18996 					if (best_ipif != NULL)
18997 						ipif_refrele(best_ipif);
18998 					best_ipif = ipif;
18999 					rw_enter(&ipst->ips_ill_g_lock,
19000 					    RW_READER);
19001 					mutex_enter(&ill->ill_lock);
19002 				}
19003 			}
19004 		}
19005 		mutex_exit(&ill->ill_lock);
19006 	}
19007 	rw_exit(&ipst->ips_ill_g_lock);
19008 	return (best_ipif);
19009 }
19010 
19011 /*
19012  * Save enough information so that we can recreate the IRE if
19013  * the interface goes down and then up.
19014  */
19015 static void
19016 ipif_save_ire(ipif_t *ipif, ire_t *ire)
19017 {
19018 	mblk_t	*save_mp;
19019 
19020 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
19021 	if (save_mp != NULL) {
19022 		ifrt_t	*ifrt;
19023 
19024 		save_mp->b_wptr += sizeof (ifrt_t);
19025 		ifrt = (ifrt_t *)save_mp->b_rptr;
19026 		bzero(ifrt, sizeof (ifrt_t));
19027 		ifrt->ifrt_type = ire->ire_type;
19028 		ifrt->ifrt_addr = ire->ire_addr;
19029 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
19030 		ifrt->ifrt_src_addr = ire->ire_src_addr;
19031 		ifrt->ifrt_mask = ire->ire_mask;
19032 		ifrt->ifrt_flags = ire->ire_flags;
19033 		ifrt->ifrt_max_frag = ire->ire_max_frag;
19034 		mutex_enter(&ipif->ipif_saved_ire_lock);
19035 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
19036 		ipif->ipif_saved_ire_mp = save_mp;
19037 		ipif->ipif_saved_ire_cnt++;
19038 		mutex_exit(&ipif->ipif_saved_ire_lock);
19039 	}
19040 }
19041 
19042 static void
19043 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
19044 {
19045 	mblk_t	**mpp;
19046 	mblk_t	*mp;
19047 	ifrt_t	*ifrt;
19048 
19049 	/* Remove from ipif_saved_ire_mp list if it is there */
19050 	mutex_enter(&ipif->ipif_saved_ire_lock);
19051 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
19052 	    mpp = &(*mpp)->b_cont) {
19053 		/*
19054 		 * On a given ipif, the triple of address, gateway and
19055 		 * mask is unique for each saved IRE (in the case of
19056 		 * ordinary interface routes, the gateway address is
19057 		 * all-zeroes).
19058 		 */
19059 		mp = *mpp;
19060 		ifrt = (ifrt_t *)mp->b_rptr;
19061 		if (ifrt->ifrt_addr == ire->ire_addr &&
19062 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
19063 		    ifrt->ifrt_mask == ire->ire_mask) {
19064 			*mpp = mp->b_cont;
19065 			ipif->ipif_saved_ire_cnt--;
19066 			freeb(mp);
19067 			break;
19068 		}
19069 	}
19070 	mutex_exit(&ipif->ipif_saved_ire_lock);
19071 }
19072 
19073 /*
19074  * IP multirouting broadcast routes handling
19075  * Append CGTP broadcast IREs to regular ones created
19076  * at ifconfig time.
19077  */
19078 static void
19079 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
19080 {
19081 	ire_t *ire_prim;
19082 
19083 	ASSERT(ire != NULL);
19084 	ASSERT(ire_dst != NULL);
19085 
19086 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19087 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19088 	if (ire_prim != NULL) {
19089 		/*
19090 		 * We are in the special case of broadcasts for
19091 		 * CGTP. We add an IRE_BROADCAST that holds
19092 		 * the RTF_MULTIRT flag, the destination
19093 		 * address of ire_dst and the low level
19094 		 * info of ire_prim. In other words, CGTP
19095 		 * broadcast is added to the redundant ipif.
19096 		 */
19097 		ipif_t *ipif_prim;
19098 		ire_t  *bcast_ire;
19099 
19100 		ipif_prim = ire_prim->ire_ipif;
19101 
19102 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19103 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19104 		    (void *)ire_dst, (void *)ire_prim,
19105 		    (void *)ipif_prim));
19106 
19107 		bcast_ire = ire_create(
19108 		    (uchar_t *)&ire->ire_addr,
19109 		    (uchar_t *)&ip_g_all_ones,
19110 		    (uchar_t *)&ire_dst->ire_src_addr,
19111 		    (uchar_t *)&ire->ire_gateway_addr,
19112 		    &ipif_prim->ipif_mtu,
19113 		    NULL,
19114 		    ipif_prim->ipif_rq,
19115 		    ipif_prim->ipif_wq,
19116 		    IRE_BROADCAST,
19117 		    ipif_prim,
19118 		    0,
19119 		    0,
19120 		    0,
19121 		    ire->ire_flags,
19122 		    &ire_uinfo_null,
19123 		    NULL,
19124 		    NULL,
19125 		    ipst);
19126 
19127 		if (bcast_ire != NULL) {
19128 
19129 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19130 			    B_FALSE) == 0) {
19131 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19132 				    "added bcast_ire %p\n",
19133 				    (void *)bcast_ire));
19134 
19135 				ipif_save_ire(bcast_ire->ire_ipif,
19136 				    bcast_ire);
19137 				ire_refrele(bcast_ire);
19138 			}
19139 		}
19140 		ire_refrele(ire_prim);
19141 	}
19142 }
19143 
19144 /*
19145  * IP multirouting broadcast routes handling
19146  * Remove the broadcast ire
19147  */
19148 static void
19149 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19150 {
19151 	ire_t *ire_dst;
19152 
19153 	ASSERT(ire != NULL);
19154 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19155 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19156 	if (ire_dst != NULL) {
19157 		ire_t *ire_prim;
19158 
19159 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19160 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19161 		if (ire_prim != NULL) {
19162 			ipif_t *ipif_prim;
19163 			ire_t  *bcast_ire;
19164 
19165 			ipif_prim = ire_prim->ire_ipif;
19166 
19167 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19168 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19169 			    (void *)ire_dst, (void *)ire_prim,
19170 			    (void *)ipif_prim));
19171 
19172 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19173 			    ire->ire_gateway_addr,
19174 			    IRE_BROADCAST,
19175 			    ipif_prim, ALL_ZONES,
19176 			    NULL,
19177 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19178 			    MATCH_IRE_MASK, ipst);
19179 
19180 			if (bcast_ire != NULL) {
19181 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19182 				    "looked up bcast_ire %p\n",
19183 				    (void *)bcast_ire));
19184 				ipif_remove_ire(bcast_ire->ire_ipif,
19185 				    bcast_ire);
19186 				ire_delete(bcast_ire);
19187 				ire_refrele(bcast_ire);
19188 			}
19189 			ire_refrele(ire_prim);
19190 		}
19191 		ire_refrele(ire_dst);
19192 	}
19193 }
19194 
19195 /*
19196  * IPsec hardware acceleration capabilities related functions.
19197  */
19198 
19199 /*
19200  * Free a per-ill IPsec capabilities structure.
19201  */
19202 static void
19203 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19204 {
19205 	if (capab->auth_hw_algs != NULL)
19206 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19207 	if (capab->encr_hw_algs != NULL)
19208 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19209 	if (capab->encr_algparm != NULL)
19210 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19211 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19212 }
19213 
19214 /*
19215  * Allocate a new per-ill IPsec capabilities structure. This structure
19216  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19217  * an array which specifies, for each algorithm, whether this algorithm
19218  * is supported by the ill or not.
19219  */
19220 static ill_ipsec_capab_t *
19221 ill_ipsec_capab_alloc(void)
19222 {
19223 	ill_ipsec_capab_t *capab;
19224 	uint_t nelems;
19225 
19226 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19227 	if (capab == NULL)
19228 		return (NULL);
19229 
19230 	/* we need one bit per algorithm */
19231 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19232 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19233 
19234 	/* allocate memory to store algorithm flags */
19235 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19236 	if (capab->encr_hw_algs == NULL)
19237 		goto nomem;
19238 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19239 	if (capab->auth_hw_algs == NULL)
19240 		goto nomem;
19241 	/*
19242 	 * Leave encr_algparm NULL for now since we won't need it half
19243 	 * the time
19244 	 */
19245 	return (capab);
19246 
19247 nomem:
19248 	ill_ipsec_capab_free(capab);
19249 	return (NULL);
19250 }
19251 
19252 /*
19253  * Resize capability array.  Since we're exclusive, this is OK.
19254  */
19255 static boolean_t
19256 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19257 {
19258 	ipsec_capab_algparm_t *nalp, *oalp;
19259 	uint32_t olen, nlen;
19260 
19261 	oalp = capab->encr_algparm;
19262 	olen = capab->encr_algparm_size;
19263 
19264 	if (oalp != NULL) {
19265 		if (algid < capab->encr_algparm_end)
19266 			return (B_TRUE);
19267 	}
19268 
19269 	nlen = (algid + 1) * sizeof (*nalp);
19270 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19271 	if (nalp == NULL)
19272 		return (B_FALSE);
19273 
19274 	if (oalp != NULL) {
19275 		bcopy(oalp, nalp, olen);
19276 		kmem_free(oalp, olen);
19277 	}
19278 	capab->encr_algparm = nalp;
19279 	capab->encr_algparm_size = nlen;
19280 	capab->encr_algparm_end = algid + 1;
19281 
19282 	return (B_TRUE);
19283 }
19284 
19285 /*
19286  * Compare the capabilities of the specified ill with the protocol
19287  * and algorithms specified by the SA passed as argument.
19288  * If they match, returns B_TRUE, B_FALSE if they do not match.
19289  *
19290  * The ill can be passed as a pointer to it, or by specifying its index
19291  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19292  *
19293  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19294  * packet is eligible for hardware acceleration, and by
19295  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19296  * to a particular ill.
19297  */
19298 boolean_t
19299 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19300     ipsa_t *sa, netstack_t *ns)
19301 {
19302 	boolean_t sa_isv6;
19303 	uint_t algid;
19304 	struct ill_ipsec_capab_s *cpp;
19305 	boolean_t need_refrele = B_FALSE;
19306 	ip_stack_t	*ipst = ns->netstack_ip;
19307 
19308 	if (ill == NULL) {
19309 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19310 		    NULL, NULL, NULL, ipst);
19311 		if (ill == NULL) {
19312 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19313 			return (B_FALSE);
19314 		}
19315 		need_refrele = B_TRUE;
19316 	}
19317 
19318 	/*
19319 	 * Use the address length specified by the SA to determine
19320 	 * if it corresponds to a IPv6 address, and fail the matching
19321 	 * if the isv6 flag passed as argument does not match.
19322 	 * Note: this check is used for SADB capability checking before
19323 	 * sending SA information to an ill.
19324 	 */
19325 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19326 	if (sa_isv6 != ill_isv6)
19327 		/* protocol mismatch */
19328 		goto done;
19329 
19330 	/*
19331 	 * Check if the ill supports the protocol, algorithm(s) and
19332 	 * key size(s) specified by the SA, and get the pointers to
19333 	 * the algorithms supported by the ill.
19334 	 */
19335 	switch (sa->ipsa_type) {
19336 
19337 	case SADB_SATYPE_ESP:
19338 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19339 			/* ill does not support ESP acceleration */
19340 			goto done;
19341 		cpp = ill->ill_ipsec_capab_esp;
19342 		algid = sa->ipsa_auth_alg;
19343 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19344 			goto done;
19345 		algid = sa->ipsa_encr_alg;
19346 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19347 			goto done;
19348 		if (algid < cpp->encr_algparm_end) {
19349 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19350 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19351 				goto done;
19352 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19353 				goto done;
19354 		}
19355 		break;
19356 
19357 	case SADB_SATYPE_AH:
19358 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19359 			/* ill does not support AH acceleration */
19360 			goto done;
19361 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19362 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19363 			goto done;
19364 		break;
19365 	}
19366 
19367 	if (need_refrele)
19368 		ill_refrele(ill);
19369 	return (B_TRUE);
19370 done:
19371 	if (need_refrele)
19372 		ill_refrele(ill);
19373 	return (B_FALSE);
19374 }
19375 
19376 /*
19377  * Add a new ill to the list of IPsec capable ills.
19378  * Called from ill_capability_ipsec_ack() when an ACK was received
19379  * indicating that IPsec hardware processing was enabled for an ill.
19380  *
19381  * ill must point to the ill for which acceleration was enabled.
19382  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19383  */
19384 static void
19385 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19386 {
19387 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19388 	uint_t sa_type;
19389 	uint_t ipproto;
19390 	ip_stack_t	*ipst = ill->ill_ipst;
19391 
19392 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19393 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19394 
19395 	switch (dl_cap) {
19396 	case DL_CAPAB_IPSEC_AH:
19397 		sa_type = SADB_SATYPE_AH;
19398 		ills = &ipst->ips_ipsec_capab_ills_ah;
19399 		ipproto = IPPROTO_AH;
19400 		break;
19401 	case DL_CAPAB_IPSEC_ESP:
19402 		sa_type = SADB_SATYPE_ESP;
19403 		ills = &ipst->ips_ipsec_capab_ills_esp;
19404 		ipproto = IPPROTO_ESP;
19405 		break;
19406 	}
19407 
19408 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19409 
19410 	/*
19411 	 * Add ill index to list of hardware accelerators. If
19412 	 * already in list, do nothing.
19413 	 */
19414 	for (cur_ill = *ills; cur_ill != NULL &&
19415 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19416 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19417 		;
19418 
19419 	if (cur_ill == NULL) {
19420 		/* if this is a new entry for this ill */
19421 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19422 		if (new_ill == NULL) {
19423 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19424 			return;
19425 		}
19426 
19427 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19428 		new_ill->ill_isv6 = ill->ill_isv6;
19429 		new_ill->next = *ills;
19430 		*ills = new_ill;
19431 	} else if (!sadb_resync) {
19432 		/* not resync'ing SADB and an entry exists for this ill */
19433 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19434 		return;
19435 	}
19436 
19437 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19438 
19439 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19440 		/*
19441 		 * IPsec module for protocol loaded, initiate dump
19442 		 * of the SADB to this ill.
19443 		 */
19444 		sadb_ill_download(ill, sa_type);
19445 }
19446 
19447 /*
19448  * Remove an ill from the list of IPsec capable ills.
19449  */
19450 static void
19451 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19452 {
19453 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19454 	ip_stack_t	*ipst = ill->ill_ipst;
19455 
19456 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19457 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19458 
19459 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19460 	    &ipst->ips_ipsec_capab_ills_esp;
19461 
19462 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19463 
19464 	prev_ill = NULL;
19465 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19466 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19467 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19468 		;
19469 	if (cur_ill == NULL) {
19470 		/* entry not found */
19471 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19472 		return;
19473 	}
19474 	if (prev_ill == NULL) {
19475 		/* entry at front of list */
19476 		*ills = NULL;
19477 	} else {
19478 		prev_ill->next = cur_ill->next;
19479 	}
19480 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19481 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19482 }
19483 
19484 /*
19485  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19486  * supporting the specified IPsec protocol acceleration.
19487  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19488  * We free the mblk and, if sa is non-null, release the held referece.
19489  */
19490 void
19491 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19492     netstack_t *ns)
19493 {
19494 	ipsec_capab_ill_t *ici, *cur_ici;
19495 	ill_t *ill;
19496 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19497 	ip_stack_t	*ipst = ns->netstack_ip;
19498 
19499 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19500 	    ipst->ips_ipsec_capab_ills_esp;
19501 
19502 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19503 
19504 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19505 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19506 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19507 
19508 		/*
19509 		 * Handle the case where the ill goes away while the SADB is
19510 		 * attempting to send messages.  If it's going away, it's
19511 		 * nuking its shadow SADB, so we don't care..
19512 		 */
19513 
19514 		if (ill == NULL)
19515 			continue;
19516 
19517 		if (sa != NULL) {
19518 			/*
19519 			 * Make sure capabilities match before
19520 			 * sending SA to ill.
19521 			 */
19522 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19523 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19524 				ill_refrele(ill);
19525 				continue;
19526 			}
19527 
19528 			mutex_enter(&sa->ipsa_lock);
19529 			sa->ipsa_flags |= IPSA_F_HW;
19530 			mutex_exit(&sa->ipsa_lock);
19531 		}
19532 
19533 		/*
19534 		 * Copy template message, and add it to the front
19535 		 * of the mblk ship list. We want to avoid holding
19536 		 * the ipsec_capab_ills_lock while sending the
19537 		 * message to the ills.
19538 		 *
19539 		 * The b_next and b_prev are temporarily used
19540 		 * to build a list of mblks to be sent down, and to
19541 		 * save the ill to which they must be sent.
19542 		 */
19543 		nmp = copymsg(mp);
19544 		if (nmp == NULL) {
19545 			ill_refrele(ill);
19546 			continue;
19547 		}
19548 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19549 		nmp->b_next = mp_ship_list;
19550 		mp_ship_list = nmp;
19551 		nmp->b_prev = (mblk_t *)ill;
19552 	}
19553 
19554 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19555 
19556 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19557 		/* restore the mblk to a sane state */
19558 		next_mp = nmp->b_next;
19559 		nmp->b_next = NULL;
19560 		ill = (ill_t *)nmp->b_prev;
19561 		nmp->b_prev = NULL;
19562 
19563 		ill_dlpi_send(ill, nmp);
19564 		ill_refrele(ill);
19565 	}
19566 
19567 	if (sa != NULL)
19568 		IPSA_REFRELE(sa);
19569 	freemsg(mp);
19570 }
19571 
19572 /*
19573  * Derive an interface id from the link layer address.
19574  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19575  */
19576 static boolean_t
19577 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19578 {
19579 	char		*addr;
19580 
19581 	if (ill->ill_phys_addr_length != ETHERADDRL)
19582 		return (B_FALSE);
19583 
19584 	/* Form EUI-64 like address */
19585 	addr = (char *)&v6addr->s6_addr32[2];
19586 	bcopy(ill->ill_phys_addr, addr, 3);
19587 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19588 	addr[3] = (char)0xff;
19589 	addr[4] = (char)0xfe;
19590 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19591 	return (B_TRUE);
19592 }
19593 
19594 /* ARGSUSED */
19595 static boolean_t
19596 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19597 {
19598 	return (B_FALSE);
19599 }
19600 
19601 typedef struct ipmp_ifcookie {
19602 	uint32_t	ic_hostid;
19603 	char		ic_ifname[LIFNAMSIZ];
19604 	char		ic_zonename[ZONENAME_MAX];
19605 } ipmp_ifcookie_t;
19606 
19607 /*
19608  * Construct a pseudo-random interface ID for the IPMP interface that's both
19609  * predictable and (almost) guaranteed to be unique.
19610  */
19611 static boolean_t
19612 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19613 {
19614 	zone_t		*zp;
19615 	uint8_t		*addr;
19616 	uchar_t		hash[16];
19617 	ulong_t 	hostid;
19618 	MD5_CTX		ctx;
19619 	ipmp_ifcookie_t	ic = { 0 };
19620 
19621 	ASSERT(IS_IPMP(ill));
19622 
19623 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19624 	ic.ic_hostid = htonl((uint32_t)hostid);
19625 
19626 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19627 
19628 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19629 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19630 		zone_rele(zp);
19631 	}
19632 
19633 	MD5Init(&ctx);
19634 	MD5Update(&ctx, &ic, sizeof (ic));
19635 	MD5Final(hash, &ctx);
19636 
19637 	/*
19638 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19639 	 */
19640 	addr = &v6addr->s6_addr8[8];
19641 	bcopy(hash + 8, addr, sizeof (uint64_t));
19642 	addr[0] &= ~0x2;				/* set local bit */
19643 
19644 	return (B_TRUE);
19645 }
19646 
19647 /* ARGSUSED */
19648 static boolean_t
19649 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19650     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19651 {
19652 	/*
19653 	 * Multicast address mappings used over Ethernet/802.X.
19654 	 * This address is used as a base for mappings.
19655 	 */
19656 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19657 	    0x00, 0x00, 0x00};
19658 
19659 	/*
19660 	 * Extract low order 32 bits from IPv6 multicast address.
19661 	 * Or that into the link layer address, starting from the
19662 	 * second byte.
19663 	 */
19664 	*hw_start = 2;
19665 	v6_extract_mask->s6_addr32[0] = 0;
19666 	v6_extract_mask->s6_addr32[1] = 0;
19667 	v6_extract_mask->s6_addr32[2] = 0;
19668 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19669 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19670 	return (B_TRUE);
19671 }
19672 
19673 /*
19674  * Indicate by return value whether multicast is supported. If not,
19675  * this code should not touch/change any parameters.
19676  */
19677 /* ARGSUSED */
19678 static boolean_t
19679 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19680     uint32_t *hw_start, ipaddr_t *extract_mask)
19681 {
19682 	/*
19683 	 * Multicast address mappings used over Ethernet/802.X.
19684 	 * This address is used as a base for mappings.
19685 	 */
19686 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19687 	    0x00, 0x00, 0x00 };
19688 
19689 	if (phys_length != ETHERADDRL)
19690 		return (B_FALSE);
19691 
19692 	*extract_mask = htonl(0x007fffff);
19693 	*hw_start = 2;
19694 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19695 	return (B_TRUE);
19696 }
19697 
19698 /*
19699  * Derive IPoIB interface id from the link layer address.
19700  */
19701 static boolean_t
19702 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19703 {
19704 	char		*addr;
19705 
19706 	if (ill->ill_phys_addr_length != 20)
19707 		return (B_FALSE);
19708 	addr = (char *)&v6addr->s6_addr32[2];
19709 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19710 	/*
19711 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19712 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19713 	 * rules. In these cases, the IBA considers these GUIDs to be in
19714 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19715 	 * required; vendors are required not to assign global EUI-64's
19716 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19717 	 * of the interface identifier. Whether the GUID is in modified
19718 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19719 	 * bit set to 1.
19720 	 */
19721 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19722 	return (B_TRUE);
19723 }
19724 
19725 /*
19726  * Note on mapping from multicast IP addresses to IPoIB multicast link
19727  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19728  * The format of an IPoIB multicast address is:
19729  *
19730  *  4 byte QPN      Scope Sign.  Pkey
19731  * +--------------------------------------------+
19732  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19733  * +--------------------------------------------+
19734  *
19735  * The Scope and Pkey components are properties of the IBA port and
19736  * network interface. They can be ascertained from the broadcast address.
19737  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19738  */
19739 
19740 static boolean_t
19741 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19742     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19743 {
19744 	/*
19745 	 * Base IPoIB IPv6 multicast address used for mappings.
19746 	 * Does not contain the IBA scope/Pkey values.
19747 	 */
19748 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19749 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19750 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19751 
19752 	/*
19753 	 * Extract low order 80 bits from IPv6 multicast address.
19754 	 * Or that into the link layer address, starting from the
19755 	 * sixth byte.
19756 	 */
19757 	*hw_start = 6;
19758 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19759 
19760 	/*
19761 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19762 	 */
19763 	*(maddr + 5) = *(bphys_addr + 5);
19764 	*(maddr + 8) = *(bphys_addr + 8);
19765 	*(maddr + 9) = *(bphys_addr + 9);
19766 
19767 	v6_extract_mask->s6_addr32[0] = 0;
19768 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19769 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19770 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19771 	return (B_TRUE);
19772 }
19773 
19774 static boolean_t
19775 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19776     uint32_t *hw_start, ipaddr_t *extract_mask)
19777 {
19778 	/*
19779 	 * Base IPoIB IPv4 multicast address used for mappings.
19780 	 * Does not contain the IBA scope/Pkey values.
19781 	 */
19782 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19783 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19784 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19785 
19786 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19787 		return (B_FALSE);
19788 
19789 	/*
19790 	 * Extract low order 28 bits from IPv4 multicast address.
19791 	 * Or that into the link layer address, starting from the
19792 	 * sixteenth byte.
19793 	 */
19794 	*extract_mask = htonl(0x0fffffff);
19795 	*hw_start = 16;
19796 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19797 
19798 	/*
19799 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19800 	 */
19801 	*(maddr + 5) = *(bphys_addr + 5);
19802 	*(maddr + 8) = *(bphys_addr + 8);
19803 	*(maddr + 9) = *(bphys_addr + 9);
19804 	return (B_TRUE);
19805 }
19806 
19807 /*
19808  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19809  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19810  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19811  * the link-local address is preferred.
19812  */
19813 boolean_t
19814 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19815 {
19816 	ipif_t	*ipif;
19817 	ipif_t	*maybe_ipif = NULL;
19818 
19819 	mutex_enter(&ill->ill_lock);
19820 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19821 		mutex_exit(&ill->ill_lock);
19822 		if (ipifp != NULL)
19823 			*ipifp = NULL;
19824 		return (B_FALSE);
19825 	}
19826 
19827 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19828 		if (!IPIF_CAN_LOOKUP(ipif))
19829 			continue;
19830 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19831 		    ipif->ipif_zoneid != ALL_ZONES)
19832 			continue;
19833 		if ((ipif->ipif_flags & flags) != flags)
19834 			continue;
19835 
19836 		if (ipifp == NULL) {
19837 			mutex_exit(&ill->ill_lock);
19838 			ASSERT(maybe_ipif == NULL);
19839 			return (B_TRUE);
19840 		}
19841 		if (!ill->ill_isv6 ||
19842 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19843 			ipif_refhold_locked(ipif);
19844 			mutex_exit(&ill->ill_lock);
19845 			*ipifp = ipif;
19846 			return (B_TRUE);
19847 		}
19848 		if (maybe_ipif == NULL)
19849 			maybe_ipif = ipif;
19850 	}
19851 	if (ipifp != NULL) {
19852 		if (maybe_ipif != NULL)
19853 			ipif_refhold_locked(maybe_ipif);
19854 		*ipifp = maybe_ipif;
19855 	}
19856 	mutex_exit(&ill->ill_lock);
19857 	return (maybe_ipif != NULL);
19858 }
19859 
19860 /*
19861  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19862  * If a pointer to an ipif_t is returned then the caller will need to do
19863  * an ill_refrele().
19864  */
19865 ipif_t *
19866 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19867     ip_stack_t *ipst)
19868 {
19869 	ipif_t *ipif;
19870 	ill_t *ill;
19871 
19872 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19873 	    ipst);
19874 	if (ill == NULL)
19875 		return (NULL);
19876 
19877 	mutex_enter(&ill->ill_lock);
19878 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19879 		mutex_exit(&ill->ill_lock);
19880 		ill_refrele(ill);
19881 		return (NULL);
19882 	}
19883 
19884 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19885 		if (!IPIF_CAN_LOOKUP(ipif))
19886 			continue;
19887 		if (lifidx == ipif->ipif_id) {
19888 			ipif_refhold_locked(ipif);
19889 			break;
19890 		}
19891 	}
19892 
19893 	mutex_exit(&ill->ill_lock);
19894 	ill_refrele(ill);
19895 	return (ipif);
19896 }
19897 
19898 /*
19899  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19900  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19901  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19902  * for details.
19903  */
19904 void
19905 ill_fastpath_flush(ill_t *ill)
19906 {
19907 	ip_stack_t *ipst = ill->ill_ipst;
19908 
19909 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19910 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19911 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19912 }
19913 
19914 /*
19915  * Set the physical address information for `ill' to the contents of the
19916  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
19917  * asynchronous if `ill' cannot immediately be quiesced -- in which case
19918  * EINPROGRESS will be returned.
19919  */
19920 int
19921 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
19922 {
19923 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19924 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
19925 
19926 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19927 
19928 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
19929 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
19930 		/* Changing DL_IPV6_TOKEN is not yet supported */
19931 		return (0);
19932 	}
19933 
19934 	/*
19935 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
19936 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
19937 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
19938 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
19939 	 */
19940 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
19941 		freemsg(mp);
19942 		return (ENOMEM);
19943 	}
19944 
19945 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19946 
19947 	/*
19948 	 * If we can quiesce the ill, then set the address.  If not, then
19949 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
19950 	 */
19951 	ill_down_ipifs(ill, B_TRUE);
19952 	mutex_enter(&ill->ill_lock);
19953 	if (!ill_is_quiescent(ill)) {
19954 		/* call cannot fail since `conn_t *' argument is NULL */
19955 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19956 		    mp, ILL_DOWN);
19957 		mutex_exit(&ill->ill_lock);
19958 		return (EINPROGRESS);
19959 	}
19960 	mutex_exit(&ill->ill_lock);
19961 
19962 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
19963 	return (0);
19964 }
19965 
19966 /*
19967  * Once the ill associated with `q' has quiesced, set its physical address
19968  * information to the values in `addrmp'.  Note that two copies of `addrmp'
19969  * are passed (linked by b_cont), since we sometimes need to save two distinct
19970  * copies in the ill_t, and our context doesn't permit sleeping or allocation
19971  * failure (we'll free the other copy if it's not needed).  Since the ill_t
19972  * is quiesced, we know any stale IREs with the old address information have
19973  * already been removed, so we don't need to call ill_fastpath_flush().
19974  */
19975 /* ARGSUSED */
19976 static void
19977 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
19978 {
19979 	ill_t		*ill = q->q_ptr;
19980 	mblk_t		*addrmp2 = unlinkb(addrmp);
19981 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
19982 	uint_t		addrlen, addroff;
19983 
19984 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19985 
19986 	addroff	= dlindp->dl_addr_offset;
19987 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
19988 
19989 	switch (dlindp->dl_data) {
19990 	case DL_IPV6_LINK_LAYER_ADDR:
19991 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
19992 		freemsg(addrmp2);
19993 		break;
19994 
19995 	case DL_CURR_PHYS_ADDR:
19996 		freemsg(ill->ill_phys_addr_mp);
19997 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
19998 		ill->ill_phys_addr_mp = addrmp;
19999 		ill->ill_phys_addr_length = addrlen;
20000 
20001 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
20002 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
20003 		else
20004 			freemsg(addrmp2);
20005 		break;
20006 	default:
20007 		ASSERT(0);
20008 	}
20009 
20010 	/*
20011 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20012 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20013 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20014 	 * brought up.
20015 	 */
20016 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
20017 		ipsq_current_finish(ipsq);
20018 }
20019 
20020 /*
20021  * Helper routine for setting the ill_nd_lla fields.
20022  */
20023 void
20024 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
20025 {
20026 	freemsg(ill->ill_nd_lla_mp);
20027 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
20028 	ill->ill_nd_lla_mp = ndmp;
20029 	ill->ill_nd_lla_len = addrlen;
20030 }
20031 
20032 /*
20033  * Replumb the ill.
20034  */
20035 int
20036 ill_replumb(ill_t *ill, mblk_t *mp)
20037 {
20038 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
20039 
20040 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20041 
20042 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
20043 
20044 	/*
20045 	 * If we can quiesce the ill, then continue.  If not, then
20046 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
20047 	 */
20048 	ill_down_ipifs(ill, B_FALSE);
20049 
20050 	mutex_enter(&ill->ill_lock);
20051 	if (!ill_is_quiescent(ill)) {
20052 		/* call cannot fail since `conn_t *' argument is NULL */
20053 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
20054 		    mp, ILL_DOWN);
20055 		mutex_exit(&ill->ill_lock);
20056 		return (EINPROGRESS);
20057 	}
20058 	mutex_exit(&ill->ill_lock);
20059 
20060 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
20061 	return (0);
20062 }
20063 
20064 /* ARGSUSED */
20065 static void
20066 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
20067 {
20068 	ill_t *ill = q->q_ptr;
20069 
20070 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20071 
20072 	ill_down_ipifs_tail(ill);
20073 
20074 	freemsg(ill->ill_replumb_mp);
20075 	ill->ill_replumb_mp = copyb(mp);
20076 
20077 	/*
20078 	 * Successfully quiesced and brought down the interface, now we send
20079 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
20080 	 * DL_NOTE_REPLUMB message.
20081 	 */
20082 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
20083 	    DL_NOTIFY_CONF);
20084 	ASSERT(mp != NULL);
20085 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
20086 	    DL_NOTE_REPLUMB_DONE;
20087 	ill_dlpi_send(ill, mp);
20088 
20089 	/*
20090 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20091 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20092 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20093 	 * brought up.
20094 	 */
20095 	if (ill->ill_replumb_mp == NULL ||
20096 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) != EINPROGRESS) {
20097 		ipsq_current_finish(ipsq);
20098 	}
20099 }
20100 
20101 major_t IP_MAJ;
20102 #define	IP	"ip"
20103 
20104 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20105 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20106 
20107 /*
20108  * Issue REMOVEIF ioctls to have the loopback interfaces
20109  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20110  * the former going away when the user-level processes in the zone
20111  * are killed  * and the latter are cleaned up by the stream head
20112  * str_stack_shutdown callback that undoes all I_PLINKs.
20113  */
20114 void
20115 ip_loopback_cleanup(ip_stack_t *ipst)
20116 {
20117 	int error;
20118 	ldi_handle_t	lh = NULL;
20119 	ldi_ident_t	li = NULL;
20120 	int		rval;
20121 	cred_t		*cr;
20122 	struct strioctl iocb;
20123 	struct lifreq	lifreq;
20124 
20125 	IP_MAJ = ddi_name_to_major(IP);
20126 
20127 #ifdef NS_DEBUG
20128 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20129 	    ipst->ips_netstack->netstack_stackid);
20130 #endif
20131 
20132 	bzero(&lifreq, sizeof (lifreq));
20133 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20134 
20135 	error = ldi_ident_from_major(IP_MAJ, &li);
20136 	if (error) {
20137 #ifdef DEBUG
20138 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20139 		    error);
20140 #endif
20141 		return;
20142 	}
20143 
20144 	cr = zone_get_kcred(netstackid_to_zoneid(
20145 	    ipst->ips_netstack->netstack_stackid));
20146 	ASSERT(cr != NULL);
20147 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20148 	if (error) {
20149 #ifdef DEBUG
20150 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20151 		    error);
20152 #endif
20153 		goto out;
20154 	}
20155 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20156 	iocb.ic_timout = 15;
20157 	iocb.ic_len = sizeof (lifreq);
20158 	iocb.ic_dp = (char *)&lifreq;
20159 
20160 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20161 	/* LINTED - statement has no consequent */
20162 	if (error) {
20163 #ifdef NS_DEBUG
20164 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20165 		    "UDP6 error %d\n", error);
20166 #endif
20167 	}
20168 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20169 	lh = NULL;
20170 
20171 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20172 	if (error) {
20173 #ifdef NS_DEBUG
20174 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20175 		    error);
20176 #endif
20177 		goto out;
20178 	}
20179 
20180 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20181 	iocb.ic_timout = 15;
20182 	iocb.ic_len = sizeof (lifreq);
20183 	iocb.ic_dp = (char *)&lifreq;
20184 
20185 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20186 	/* LINTED - statement has no consequent */
20187 	if (error) {
20188 #ifdef NS_DEBUG
20189 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20190 		    "UDP error %d\n", error);
20191 #endif
20192 	}
20193 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20194 	lh = NULL;
20195 
20196 out:
20197 	/* Close layered handles */
20198 	if (lh)
20199 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20200 	if (li)
20201 		ldi_ident_release(li);
20202 
20203 	crfree(cr);
20204 }
20205 
20206 /*
20207  * This needs to be in-sync with nic_event_t definition
20208  */
20209 static const char *
20210 ill_hook_event2str(nic_event_t event)
20211 {
20212 	switch (event) {
20213 	case NE_PLUMB:
20214 		return ("PLUMB");
20215 	case NE_UNPLUMB:
20216 		return ("UNPLUMB");
20217 	case NE_UP:
20218 		return ("UP");
20219 	case NE_DOWN:
20220 		return ("DOWN");
20221 	case NE_ADDRESS_CHANGE:
20222 		return ("ADDRESS_CHANGE");
20223 	case NE_LIF_UP:
20224 		return ("LIF_UP");
20225 	case NE_LIF_DOWN:
20226 		return ("LIF_DOWN");
20227 	default:
20228 		return ("UNKNOWN");
20229 	}
20230 }
20231 
20232 void
20233 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20234     nic_event_data_t data, size_t datalen)
20235 {
20236 	ip_stack_t		*ipst = ill->ill_ipst;
20237 	hook_nic_event_int_t	*info;
20238 	const char		*str = NULL;
20239 
20240 	/* create a new nic event info */
20241 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20242 		goto fail;
20243 
20244 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20245 	info->hnei_event.hne_lif = lif;
20246 	info->hnei_event.hne_event = event;
20247 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20248 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20249 	info->hnei_event.hne_data = NULL;
20250 	info->hnei_event.hne_datalen = 0;
20251 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20252 
20253 	if (data != NULL && datalen != 0) {
20254 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20255 		if (info->hnei_event.hne_data == NULL)
20256 			goto fail;
20257 		bcopy(data, info->hnei_event.hne_data, datalen);
20258 		info->hnei_event.hne_datalen = datalen;
20259 	}
20260 
20261 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20262 	    DDI_NOSLEEP) == DDI_SUCCESS)
20263 		return;
20264 
20265 fail:
20266 	if (info != NULL) {
20267 		if (info->hnei_event.hne_data != NULL) {
20268 			kmem_free(info->hnei_event.hne_data,
20269 			    info->hnei_event.hne_datalen);
20270 		}
20271 		kmem_free(info, sizeof (hook_nic_event_t));
20272 	}
20273 	str = ill_hook_event2str(event);
20274 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20275 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20276 }
20277 
20278 void
20279 ipif_up_notify(ipif_t *ipif)
20280 {
20281 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20282 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20283 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20284 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20285 	    NE_LIF_UP, NULL, 0);
20286 }
20287