xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 4f0f5e5be9d3811b437d9156675d584e2a2f204a)
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  * Enter the ipsq corresponding to ill, by waiting synchronously till
7613  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7614  * will have to drain completely before ipsq_enter returns success.
7615  * ipx_current_ipif will be set if some exclusive op is in progress,
7616  * and the ipsq_exit logic will start the next enqueued op after
7617  * completion of the current op. If 'force' is used, we don't wait
7618  * for the enqueued ops. This is needed when a conn_close wants to
7619  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7620  * of an ill can also use this option. But we dont' use it currently.
7621  */
7622 #define	ENTER_SQ_WAIT_TICKS 100
7623 boolean_t
7624 ipsq_enter(ill_t *ill, boolean_t force, int type)
7625 {
7626 	ipsq_t	*ipsq;
7627 	ipxop_t *ipx;
7628 	boolean_t waited_enough = B_FALSE;
7629 
7630 	/*
7631 	 * Note that the relationship between ill and ipsq is fixed as long as
7632 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7633 	 * relationship between the IPSQ and xop cannot change.  However,
7634 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7635 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7636 	 * waking up all ills in the xop when it becomes available.
7637 	 */
7638 	mutex_enter(&ill->ill_lock);
7639 	for (;;) {
7640 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7641 			mutex_exit(&ill->ill_lock);
7642 			return (B_FALSE);
7643 		}
7644 
7645 		ipsq = ill->ill_phyint->phyint_ipsq;
7646 		mutex_enter(&ipsq->ipsq_lock);
7647 		ipx = ipsq->ipsq_xop;
7648 		mutex_enter(&ipx->ipx_lock);
7649 
7650 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7651 		    ipx->ipx_current_ipif == NULL || waited_enough))
7652 			break;
7653 
7654 		if (!force || ipx->ipx_writer != NULL) {
7655 			mutex_exit(&ipx->ipx_lock);
7656 			mutex_exit(&ipsq->ipsq_lock);
7657 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7658 		} else {
7659 			mutex_exit(&ipx->ipx_lock);
7660 			mutex_exit(&ipsq->ipsq_lock);
7661 			(void) cv_timedwait(&ill->ill_cv,
7662 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7663 			waited_enough = B_TRUE;
7664 		}
7665 	}
7666 
7667 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7668 	ASSERT(ipx->ipx_reentry_cnt == 0);
7669 	ipx->ipx_writer = curthread;
7670 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7671 	ipx->ipx_reentry_cnt++;
7672 #ifdef DEBUG
7673 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7674 #endif
7675 	mutex_exit(&ipx->ipx_lock);
7676 	mutex_exit(&ipsq->ipsq_lock);
7677 	mutex_exit(&ill->ill_lock);
7678 	return (B_TRUE);
7679 }
7680 
7681 boolean_t
7682 ill_perim_enter(ill_t *ill)
7683 {
7684 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7685 }
7686 
7687 void
7688 ill_perim_exit(ill_t *ill)
7689 {
7690 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7691 }
7692 
7693 /*
7694  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7695  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7696  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7697  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7698  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7699  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7700  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7701  * up the interface) and are enqueued in ipx_mphead.
7702  *
7703  * If a thread does not want to reenter the ipsq when it is already writer,
7704  * it must make sure that the specified reentry point to be called later
7705  * when the ipsq is empty, nor any code path starting from the specified reentry
7706  * point must never ever try to enter the ipsq again. Otherwise it can lead
7707  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7708  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7709  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7710  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7711  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7712  * ioctl if the current ioctl has completed. If the current ioctl is still
7713  * in progress it simply returns. The current ioctl could be waiting for
7714  * a response from another module (arp or the driver or could be waiting for
7715  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7716  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7717  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7718  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7719  * all associated DLPI operations have completed.
7720  */
7721 
7722 /*
7723  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7724  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7725  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7726  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7727  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7728  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7729  */
7730 ipsq_t *
7731 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7732     ipsq_func_t func, int type, boolean_t reentry_ok)
7733 {
7734 	ipsq_t	*ipsq;
7735 	ipxop_t	*ipx;
7736 
7737 	/* Only 1 of ipif or ill can be specified */
7738 	ASSERT((ipif != NULL) ^ (ill != NULL));
7739 	if (ipif != NULL)
7740 		ill = ipif->ipif_ill;
7741 
7742 	/*
7743 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7744 	 * ipx of an ipsq can't change when ipsq_lock is held.
7745 	 */
7746 	GRAB_CONN_LOCK(q);
7747 	mutex_enter(&ill->ill_lock);
7748 	ipsq = ill->ill_phyint->phyint_ipsq;
7749 	mutex_enter(&ipsq->ipsq_lock);
7750 	ipx = ipsq->ipsq_xop;
7751 	mutex_enter(&ipx->ipx_lock);
7752 
7753 	/*
7754 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7755 	 *    (Note: If the caller does not specify reentry_ok then neither
7756 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7757 	 *    again. Otherwise it can lead to an infinite loop
7758 	 * 2. Enter the ipsq if there is no current writer and this attempted
7759 	 *    entry is part of the current operation
7760 	 * 3. Enter the ipsq if there is no current writer and this is a new
7761 	 *    operation and the operation queue is empty and there is no
7762 	 *    operation currently in progress
7763 	 */
7764 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7765 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7766 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7767 		/* Success. */
7768 		ipx->ipx_reentry_cnt++;
7769 		ipx->ipx_writer = curthread;
7770 		ipx->ipx_forced = B_FALSE;
7771 		mutex_exit(&ipx->ipx_lock);
7772 		mutex_exit(&ipsq->ipsq_lock);
7773 		mutex_exit(&ill->ill_lock);
7774 		RELEASE_CONN_LOCK(q);
7775 #ifdef DEBUG
7776 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7777 #endif
7778 		return (ipsq);
7779 	}
7780 
7781 	if (func != NULL)
7782 		ipsq_enq(ipsq, q, mp, func, type, ill);
7783 
7784 	mutex_exit(&ipx->ipx_lock);
7785 	mutex_exit(&ipsq->ipsq_lock);
7786 	mutex_exit(&ill->ill_lock);
7787 	RELEASE_CONN_LOCK(q);
7788 	return (NULL);
7789 }
7790 
7791 /*
7792  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7793  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7794  * cannot be entered, the mp is queued for completion.
7795  */
7796 void
7797 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7798     boolean_t reentry_ok)
7799 {
7800 	ipsq_t	*ipsq;
7801 
7802 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7803 
7804 	/*
7805 	 * Drop the caller's refhold on the ill.  This is safe since we either
7806 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7807 	 * IPSQ, in which case we return without accessing ill anymore.  This
7808 	 * is needed because func needs to see the correct refcount.
7809 	 * e.g. removeif can work only then.
7810 	 */
7811 	ill_refrele(ill);
7812 	if (ipsq != NULL) {
7813 		(*func)(ipsq, q, mp, NULL);
7814 		ipsq_exit(ipsq);
7815 	}
7816 }
7817 
7818 /*
7819  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7820  * prior to exiting.  Caller must be writer on the specified IPSQ.
7821  */
7822 void
7823 ipsq_exit(ipsq_t *ipsq)
7824 {
7825 	mblk_t *mp;
7826 	ipsq_t *mp_ipsq;
7827 	queue_t	*q;
7828 	phyint_t *phyi;
7829 	ipsq_func_t func;
7830 
7831 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7832 
7833 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7834 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7835 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7836 		return;
7837 	}
7838 
7839 	for (;;) {
7840 		phyi = ipsq->ipsq_phyint;
7841 		mp = ipsq_dq(ipsq);
7842 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7843 
7844 		/*
7845 		 * If we've changed to a new IPSQ, and the phyint associated
7846 		 * with the old one has gone away, free the old IPSQ.  Note
7847 		 * that this cannot happen while the IPSQ is in a group.
7848 		 */
7849 		if (mp_ipsq != ipsq && phyi == NULL) {
7850 			ASSERT(ipsq->ipsq_next == ipsq);
7851 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7852 			ipsq_delete(ipsq);
7853 		}
7854 
7855 		if (mp == NULL)
7856 			break;
7857 
7858 		q = mp->b_queue;
7859 		func = (ipsq_func_t)mp->b_prev;
7860 		ipsq = mp_ipsq;
7861 		mp->b_next = mp->b_prev = NULL;
7862 		mp->b_queue = NULL;
7863 
7864 		/*
7865 		 * If 'q' is an conn queue, it is valid, since we did a
7866 		 * a refhold on the conn at the start of the ioctl.
7867 		 * If 'q' is an ill queue, it is valid, since close of an
7868 		 * ill will clean up its IPSQ.
7869 		 */
7870 		(*func)(ipsq, q, mp, NULL);
7871 	}
7872 }
7873 
7874 /*
7875  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7876  * and `ioccmd'.
7877  */
7878 void
7879 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7880 {
7881 	ill_t *ill = ipif->ipif_ill;
7882 	ipxop_t *ipx = ipsq->ipsq_xop;
7883 
7884 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7885 	ASSERT(ipx->ipx_current_ipif == NULL);
7886 	ASSERT(ipx->ipx_current_ioctl == 0);
7887 
7888 	ipx->ipx_current_done = B_FALSE;
7889 	ipx->ipx_current_ioctl = ioccmd;
7890 	mutex_enter(&ipx->ipx_lock);
7891 	ipx->ipx_current_ipif = ipif;
7892 	mutex_exit(&ipx->ipx_lock);
7893 
7894 	/*
7895 	 * Set IPIF_CHANGING on one or more ipifs associated with the
7896 	 * current exclusive operation.  IPIF_CHANGING prevents any new
7897 	 * references to the ipif (so that the references will eventually
7898 	 * drop to zero) and also prevents any "get" operations (e.g.,
7899 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
7900 	 * operation has completed and the ipif is again in a stable state.
7901 	 *
7902 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
7903 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
7904 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
7905 	 * ipifs will be affected.
7906 	 *
7907 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
7908 	 * IPIF_CONDEMNED internally after identifying the right ipif to
7909 	 * operate on.
7910 	 */
7911 	switch (ioccmd) {
7912 	case SIOCLIFREMOVEIF:
7913 		break;
7914 	case 0:
7915 		mutex_enter(&ill->ill_lock);
7916 		ipif = ipif->ipif_ill->ill_ipif;
7917 		for (; ipif != NULL; ipif = ipif->ipif_next)
7918 			ipif->ipif_state_flags |= IPIF_CHANGING;
7919 		mutex_exit(&ill->ill_lock);
7920 		break;
7921 	default:
7922 		mutex_enter(&ill->ill_lock);
7923 		ipif->ipif_state_flags |= IPIF_CHANGING;
7924 		mutex_exit(&ill->ill_lock);
7925 	}
7926 }
7927 
7928 /*
7929  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
7930  * the next exclusive operation to begin once we ipsq_exit().  However, if
7931  * pending DLPI operations remain, then we will wait for the queue to drain
7932  * before allowing the next exclusive operation to begin.  This ensures that
7933  * DLPI operations from one exclusive operation are never improperly processed
7934  * as part of a subsequent exclusive operation.
7935  */
7936 void
7937 ipsq_current_finish(ipsq_t *ipsq)
7938 {
7939 	ipxop_t	*ipx = ipsq->ipsq_xop;
7940 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
7941 	ipif_t	*ipif = ipx->ipx_current_ipif;
7942 
7943 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7944 
7945 	/*
7946 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
7947 	 * (but in that case, IPIF_CHANGING will already be clear and no
7948 	 * pending DLPI messages can remain).
7949 	 */
7950 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
7951 		ill_t *ill = ipif->ipif_ill;
7952 
7953 		mutex_enter(&ill->ill_lock);
7954 		dlpi_pending = ill->ill_dlpi_pending;
7955 		if (ipx->ipx_current_ioctl == 0) {
7956 			ipif = ill->ill_ipif;
7957 			for (; ipif != NULL; ipif = ipif->ipif_next)
7958 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
7959 		} else {
7960 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
7961 		}
7962 		mutex_exit(&ill->ill_lock);
7963 	}
7964 
7965 	ASSERT(!ipx->ipx_current_done);
7966 	ipx->ipx_current_done = B_TRUE;
7967 	ipx->ipx_current_ioctl = 0;
7968 	if (dlpi_pending == DL_PRIM_INVAL) {
7969 		mutex_enter(&ipx->ipx_lock);
7970 		ipx->ipx_current_ipif = NULL;
7971 		mutex_exit(&ipx->ipx_lock);
7972 	}
7973 }
7974 
7975 /*
7976  * The ill is closing. Flush all messages on the ipsq that originated
7977  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7978  * for this ill since ipsq_enter could not have entered until then.
7979  * New messages can't be queued since the CONDEMNED flag is set.
7980  */
7981 static void
7982 ipsq_flush(ill_t *ill)
7983 {
7984 	queue_t	*q;
7985 	mblk_t	*prev;
7986 	mblk_t	*mp;
7987 	mblk_t	*mp_next;
7988 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7989 
7990 	ASSERT(IAM_WRITER_ILL(ill));
7991 
7992 	/*
7993 	 * Flush any messages sent up by the driver.
7994 	 */
7995 	mutex_enter(&ipx->ipx_lock);
7996 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7997 		mp_next = mp->b_next;
7998 		q = mp->b_queue;
7999 		if (q == ill->ill_rq || q == ill->ill_wq) {
8000 			/* dequeue mp */
8001 			if (prev == NULL)
8002 				ipx->ipx_mphead = mp->b_next;
8003 			else
8004 				prev->b_next = mp->b_next;
8005 			if (ipx->ipx_mptail == mp) {
8006 				ASSERT(mp_next == NULL);
8007 				ipx->ipx_mptail = prev;
8008 			}
8009 			inet_freemsg(mp);
8010 		} else {
8011 			prev = mp;
8012 		}
8013 	}
8014 	mutex_exit(&ipx->ipx_lock);
8015 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8016 	ipsq_xopq_mp_cleanup(ill, NULL);
8017 	ill_pending_mp_cleanup(ill);
8018 }
8019 
8020 /*
8021  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8022  * refhold and return the associated ipif
8023  */
8024 /* ARGSUSED */
8025 int
8026 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8027     cmd_info_t *ci, ipsq_func_t func)
8028 {
8029 	boolean_t exists;
8030 	struct iftun_req *ta;
8031 	ipif_t  *ipif;
8032 	ill_t   *ill;
8033 	boolean_t isv6;
8034 	mblk_t  *mp1;
8035 	int error;
8036 	conn_t  *connp;
8037 	ip_stack_t  *ipst;
8038 
8039 	/* Existence verified in ip_wput_nondata */
8040 	mp1 = mp->b_cont->b_cont;
8041 	ta = (struct iftun_req *)mp1->b_rptr;
8042 	/*
8043 	 * Null terminate the string to protect against buffer
8044 	 * overrun. String was generated by user code and may not
8045 	 * be trusted.
8046 	 */
8047 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8048 
8049 	connp = Q_TO_CONN(q);
8050 	isv6 = connp->conn_af_isv6;
8051 	ipst = connp->conn_netstack->netstack_ip;
8052 
8053 	/* Disallows implicit create */
8054 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8055 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8056 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8057 	if (ipif == NULL)
8058 		return (error);
8059 
8060 	if (ipif->ipif_id != 0) {
8061 		/*
8062 		 * We really don't want to set/get tunnel parameters
8063 		 * on virtual tunnel interfaces.  Only allow the
8064 		 * base tunnel to do these.
8065 		 */
8066 		ipif_refrele(ipif);
8067 		return (EINVAL);
8068 	}
8069 
8070 	/*
8071 	 * Send down to tunnel mod for ioctl processing.
8072 	 * Will finish ioctl in ip_rput_other().
8073 	 */
8074 	ill = ipif->ipif_ill;
8075 	if (ill->ill_net_type == IRE_LOOPBACK) {
8076 		ipif_refrele(ipif);
8077 		return (EOPNOTSUPP);
8078 	}
8079 
8080 	if (ill->ill_wq == NULL) {
8081 		ipif_refrele(ipif);
8082 		return (ENXIO);
8083 	}
8084 	/*
8085 	 * Mark the ioctl as coming from an IPv6 interface for
8086 	 * tun's convenience.
8087 	 */
8088 	if (ill->ill_isv6)
8089 		ta->ifta_flags |= 0x80000000;
8090 	ci->ci_ipif = ipif;
8091 	return (0);
8092 }
8093 
8094 /*
8095  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8096  * and return the associated ipif.
8097  * Return value:
8098  *	Non zero: An error has occurred. ci may not be filled out.
8099  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8100  *	a held ipif in ci.ci_ipif.
8101  */
8102 int
8103 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8104     cmd_info_t *ci, ipsq_func_t func)
8105 {
8106 	char		*name;
8107 	struct ifreq    *ifr;
8108 	struct lifreq    *lifr;
8109 	ipif_t		*ipif = NULL;
8110 	ill_t		*ill;
8111 	conn_t		*connp;
8112 	boolean_t	isv6;
8113 	boolean_t	exists;
8114 	int		err;
8115 	mblk_t		*mp1;
8116 	zoneid_t	zoneid;
8117 	ip_stack_t	*ipst;
8118 
8119 	if (q->q_next != NULL) {
8120 		ill = (ill_t *)q->q_ptr;
8121 		isv6 = ill->ill_isv6;
8122 		connp = NULL;
8123 		zoneid = ALL_ZONES;
8124 		ipst = ill->ill_ipst;
8125 	} else {
8126 		ill = NULL;
8127 		connp = Q_TO_CONN(q);
8128 		isv6 = connp->conn_af_isv6;
8129 		zoneid = connp->conn_zoneid;
8130 		if (zoneid == GLOBAL_ZONEID) {
8131 			/* global zone can access ipifs in all zones */
8132 			zoneid = ALL_ZONES;
8133 		}
8134 		ipst = connp->conn_netstack->netstack_ip;
8135 	}
8136 
8137 	/* Has been checked in ip_wput_nondata */
8138 	mp1 = mp->b_cont->b_cont;
8139 
8140 	if (ipip->ipi_cmd_type == IF_CMD) {
8141 		/* This a old style SIOC[GS]IF* command */
8142 		ifr = (struct ifreq *)mp1->b_rptr;
8143 		/*
8144 		 * Null terminate the string to protect against buffer
8145 		 * overrun. String was generated by user code and may not
8146 		 * be trusted.
8147 		 */
8148 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8149 		name = ifr->ifr_name;
8150 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8151 		ci->ci_sin6 = NULL;
8152 		ci->ci_lifr = (struct lifreq *)ifr;
8153 	} else {
8154 		/* This a new style SIOC[GS]LIF* command */
8155 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8156 		lifr = (struct lifreq *)mp1->b_rptr;
8157 		/*
8158 		 * Null terminate the string to protect against buffer
8159 		 * overrun. String was generated by user code and may not
8160 		 * be trusted.
8161 		 */
8162 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8163 		name = lifr->lifr_name;
8164 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8165 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8166 		ci->ci_lifr = lifr;
8167 	}
8168 
8169 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8170 		/*
8171 		 * The ioctl will be failed if the ioctl comes down
8172 		 * an conn stream
8173 		 */
8174 		if (ill == NULL) {
8175 			/*
8176 			 * Not an ill queue, return EINVAL same as the
8177 			 * old error code.
8178 			 */
8179 			return (ENXIO);
8180 		}
8181 		ipif = ill->ill_ipif;
8182 		ipif_refhold(ipif);
8183 	} else {
8184 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8185 		    &exists, isv6, zoneid,
8186 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8187 		    ipst);
8188 		if (ipif == NULL) {
8189 			if (err == EINPROGRESS)
8190 				return (err);
8191 			err = 0;	/* Ensure we don't use it below */
8192 		}
8193 	}
8194 
8195 	/*
8196 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8197 	 */
8198 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8199 		ipif_refrele(ipif);
8200 		return (ENXIO);
8201 	}
8202 
8203 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8204 	    name[0] == '\0') {
8205 		/*
8206 		 * Handle a or a SIOC?IF* with a null name
8207 		 * during plumb (on the ill queue before the I_PLINK).
8208 		 */
8209 		ipif = ill->ill_ipif;
8210 		ipif_refhold(ipif);
8211 	}
8212 
8213 	if (ipif == NULL)
8214 		return (ENXIO);
8215 
8216 	ci->ci_ipif = ipif;
8217 	return (0);
8218 }
8219 
8220 /*
8221  * Return the total number of ipifs.
8222  */
8223 static uint_t
8224 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8225 {
8226 	uint_t numifs = 0;
8227 	ill_t	*ill;
8228 	ill_walk_context_t	ctx;
8229 	ipif_t	*ipif;
8230 
8231 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8232 	ill = ILL_START_WALK_V4(&ctx, ipst);
8233 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8234 		if (IS_UNDER_IPMP(ill))
8235 			continue;
8236 		for (ipif = ill->ill_ipif; ipif != NULL;
8237 		    ipif = ipif->ipif_next) {
8238 			if (ipif->ipif_zoneid == zoneid ||
8239 			    ipif->ipif_zoneid == ALL_ZONES)
8240 				numifs++;
8241 		}
8242 	}
8243 	rw_exit(&ipst->ips_ill_g_lock);
8244 	return (numifs);
8245 }
8246 
8247 /*
8248  * Return the total number of ipifs.
8249  */
8250 static uint_t
8251 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8252 {
8253 	uint_t numifs = 0;
8254 	ill_t	*ill;
8255 	ipif_t	*ipif;
8256 	ill_walk_context_t	ctx;
8257 
8258 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8259 
8260 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8261 	if (family == AF_INET)
8262 		ill = ILL_START_WALK_V4(&ctx, ipst);
8263 	else if (family == AF_INET6)
8264 		ill = ILL_START_WALK_V6(&ctx, ipst);
8265 	else
8266 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8267 
8268 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8269 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8270 			continue;
8271 
8272 		for (ipif = ill->ill_ipif; ipif != NULL;
8273 		    ipif = ipif->ipif_next) {
8274 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8275 			    !(lifn_flags & LIFC_NOXMIT))
8276 				continue;
8277 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8278 			    !(lifn_flags & LIFC_TEMPORARY))
8279 				continue;
8280 			if (((ipif->ipif_flags &
8281 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8282 			    IPIF_DEPRECATED)) ||
8283 			    IS_LOOPBACK(ill) ||
8284 			    !(ipif->ipif_flags & IPIF_UP)) &&
8285 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8286 				continue;
8287 
8288 			if (zoneid != ipif->ipif_zoneid &&
8289 			    ipif->ipif_zoneid != ALL_ZONES &&
8290 			    (zoneid != GLOBAL_ZONEID ||
8291 			    !(lifn_flags & LIFC_ALLZONES)))
8292 				continue;
8293 
8294 			numifs++;
8295 		}
8296 	}
8297 	rw_exit(&ipst->ips_ill_g_lock);
8298 	return (numifs);
8299 }
8300 
8301 uint_t
8302 ip_get_lifsrcofnum(ill_t *ill)
8303 {
8304 	uint_t numifs = 0;
8305 	ill_t	*ill_head = ill;
8306 	ip_stack_t	*ipst = ill->ill_ipst;
8307 
8308 	/*
8309 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8310 	 * other thread may be trying to relink the ILLs in this usesrc group
8311 	 * and adjusting the ill_usesrc_grp_next pointers
8312 	 */
8313 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8314 	if ((ill->ill_usesrc_ifindex == 0) &&
8315 	    (ill->ill_usesrc_grp_next != NULL)) {
8316 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8317 		    ill = ill->ill_usesrc_grp_next)
8318 			numifs++;
8319 	}
8320 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8321 
8322 	return (numifs);
8323 }
8324 
8325 /* Null values are passed in for ipif, sin, and ifreq */
8326 /* ARGSUSED */
8327 int
8328 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8329     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8330 {
8331 	int *nump;
8332 	conn_t *connp = Q_TO_CONN(q);
8333 
8334 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8335 
8336 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8337 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8338 
8339 	*nump = ip_get_numifs(connp->conn_zoneid,
8340 	    connp->conn_netstack->netstack_ip);
8341 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8342 	return (0);
8343 }
8344 
8345 /* Null values are passed in for ipif, sin, and ifreq */
8346 /* ARGSUSED */
8347 int
8348 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8349     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8350 {
8351 	struct lifnum *lifn;
8352 	mblk_t	*mp1;
8353 	conn_t *connp = Q_TO_CONN(q);
8354 
8355 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8356 
8357 	/* Existence checked in ip_wput_nondata */
8358 	mp1 = mp->b_cont->b_cont;
8359 
8360 	lifn = (struct lifnum *)mp1->b_rptr;
8361 	switch (lifn->lifn_family) {
8362 	case AF_UNSPEC:
8363 	case AF_INET:
8364 	case AF_INET6:
8365 		break;
8366 	default:
8367 		return (EAFNOSUPPORT);
8368 	}
8369 
8370 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8371 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8372 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8373 	return (0);
8374 }
8375 
8376 /* ARGSUSED */
8377 int
8378 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8379     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8380 {
8381 	STRUCT_HANDLE(ifconf, ifc);
8382 	mblk_t *mp1;
8383 	struct iocblk *iocp;
8384 	struct ifreq *ifr;
8385 	ill_walk_context_t	ctx;
8386 	ill_t	*ill;
8387 	ipif_t	*ipif;
8388 	struct sockaddr_in *sin;
8389 	int32_t	ifclen;
8390 	zoneid_t zoneid;
8391 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8392 
8393 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8394 
8395 	ip1dbg(("ip_sioctl_get_ifconf"));
8396 	/* Existence verified in ip_wput_nondata */
8397 	mp1 = mp->b_cont->b_cont;
8398 	iocp = (struct iocblk *)mp->b_rptr;
8399 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8400 
8401 	/*
8402 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8403 	 * the user buffer address and length into which the list of struct
8404 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8405 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8406 	 * the SIOCGIFCONF operation was redefined to simply provide
8407 	 * a large output buffer into which we are supposed to jam the ifreq
8408 	 * array.  The same ioctl command code was used, despite the fact that
8409 	 * both the applications and the kernel code had to change, thus making
8410 	 * it impossible to support both interfaces.
8411 	 *
8412 	 * For reasons not good enough to try to explain, the following
8413 	 * algorithm is used for deciding what to do with one of these:
8414 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8415 	 * form with the output buffer coming down as the continuation message.
8416 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8417 	 * and we have to copy in the ifconf structure to find out how big the
8418 	 * output buffer is and where to copy out to.  Sure no problem...
8419 	 *
8420 	 */
8421 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8422 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8423 		int numifs = 0;
8424 		size_t ifc_bufsize;
8425 
8426 		/*
8427 		 * Must be (better be!) continuation of a TRANSPARENT
8428 		 * IOCTL.  We just copied in the ifconf structure.
8429 		 */
8430 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8431 		    (struct ifconf *)mp1->b_rptr);
8432 
8433 		/*
8434 		 * Allocate a buffer to hold requested information.
8435 		 *
8436 		 * If ifc_len is larger than what is needed, we only
8437 		 * allocate what we will use.
8438 		 *
8439 		 * If ifc_len is smaller than what is needed, return
8440 		 * EINVAL.
8441 		 *
8442 		 * XXX: the ill_t structure can hava 2 counters, for
8443 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8444 		 * number of interfaces for a device, so we don't need
8445 		 * to count them here...
8446 		 */
8447 		numifs = ip_get_numifs(zoneid, ipst);
8448 
8449 		ifclen = STRUCT_FGET(ifc, ifc_len);
8450 		ifc_bufsize = numifs * sizeof (struct ifreq);
8451 		if (ifc_bufsize > ifclen) {
8452 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8453 				/* old behaviour */
8454 				return (EINVAL);
8455 			} else {
8456 				ifc_bufsize = ifclen;
8457 			}
8458 		}
8459 
8460 		mp1 = mi_copyout_alloc(q, mp,
8461 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8462 		if (mp1 == NULL)
8463 			return (ENOMEM);
8464 
8465 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8466 	}
8467 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8468 	/*
8469 	 * the SIOCGIFCONF ioctl only knows about
8470 	 * IPv4 addresses, so don't try to tell
8471 	 * it about interfaces with IPv6-only
8472 	 * addresses. (Last parm 'isv6' is B_FALSE)
8473 	 */
8474 
8475 	ifr = (struct ifreq *)mp1->b_rptr;
8476 
8477 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8478 	ill = ILL_START_WALK_V4(&ctx, ipst);
8479 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8480 		if (IS_UNDER_IPMP(ill))
8481 			continue;
8482 		for (ipif = ill->ill_ipif; ipif != NULL;
8483 		    ipif = ipif->ipif_next) {
8484 			if (zoneid != ipif->ipif_zoneid &&
8485 			    ipif->ipif_zoneid != ALL_ZONES)
8486 				continue;
8487 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8488 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8489 					/* old behaviour */
8490 					rw_exit(&ipst->ips_ill_g_lock);
8491 					return (EINVAL);
8492 				} else {
8493 					goto if_copydone;
8494 				}
8495 			}
8496 			ipif_get_name(ipif, ifr->ifr_name,
8497 			    sizeof (ifr->ifr_name));
8498 			sin = (sin_t *)&ifr->ifr_addr;
8499 			*sin = sin_null;
8500 			sin->sin_family = AF_INET;
8501 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8502 			ifr++;
8503 		}
8504 	}
8505 if_copydone:
8506 	rw_exit(&ipst->ips_ill_g_lock);
8507 	mp1->b_wptr = (uchar_t *)ifr;
8508 
8509 	if (STRUCT_BUF(ifc) != NULL) {
8510 		STRUCT_FSET(ifc, ifc_len,
8511 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8512 	}
8513 	return (0);
8514 }
8515 
8516 /*
8517  * Get the interfaces using the address hosted on the interface passed in,
8518  * as a source adddress
8519  */
8520 /* ARGSUSED */
8521 int
8522 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8523     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8524 {
8525 	mblk_t *mp1;
8526 	ill_t	*ill, *ill_head;
8527 	ipif_t	*ipif, *orig_ipif;
8528 	int	numlifs = 0;
8529 	size_t	lifs_bufsize, lifsmaxlen;
8530 	struct	lifreq *lifr;
8531 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8532 	uint_t	ifindex;
8533 	zoneid_t zoneid;
8534 	int err = 0;
8535 	boolean_t isv6 = B_FALSE;
8536 	struct	sockaddr_in	*sin;
8537 	struct	sockaddr_in6	*sin6;
8538 	STRUCT_HANDLE(lifsrcof, lifs);
8539 	ip_stack_t		*ipst;
8540 
8541 	ipst = CONNQ_TO_IPST(q);
8542 
8543 	ASSERT(q->q_next == NULL);
8544 
8545 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8546 
8547 	/* Existence verified in ip_wput_nondata */
8548 	mp1 = mp->b_cont->b_cont;
8549 
8550 	/*
8551 	 * Must be (better be!) continuation of a TRANSPARENT
8552 	 * IOCTL.  We just copied in the lifsrcof structure.
8553 	 */
8554 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8555 	    (struct lifsrcof *)mp1->b_rptr);
8556 
8557 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8558 		return (EINVAL);
8559 
8560 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8561 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8562 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8563 	    ip_process_ioctl, &err, ipst);
8564 	if (ipif == NULL) {
8565 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8566 		    ifindex));
8567 		return (err);
8568 	}
8569 
8570 	/* Allocate a buffer to hold requested information */
8571 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8572 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8573 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8574 	/* The actual size needed is always returned in lifs_len */
8575 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8576 
8577 	/* If the amount we need is more than what is passed in, abort */
8578 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8579 		ipif_refrele(ipif);
8580 		return (0);
8581 	}
8582 
8583 	mp1 = mi_copyout_alloc(q, mp,
8584 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8585 	if (mp1 == NULL) {
8586 		ipif_refrele(ipif);
8587 		return (ENOMEM);
8588 	}
8589 
8590 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8591 	bzero(mp1->b_rptr, lifs_bufsize);
8592 
8593 	lifr = (struct lifreq *)mp1->b_rptr;
8594 
8595 	ill = ill_head = ipif->ipif_ill;
8596 	orig_ipif = ipif;
8597 
8598 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8599 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8600 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8601 
8602 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8603 	for (; (ill != NULL) && (ill != ill_head);
8604 	    ill = ill->ill_usesrc_grp_next) {
8605 
8606 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8607 			break;
8608 
8609 		ipif = ill->ill_ipif;
8610 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8611 		if (ipif->ipif_isv6) {
8612 			sin6 = (sin6_t *)&lifr->lifr_addr;
8613 			*sin6 = sin6_null;
8614 			sin6->sin6_family = AF_INET6;
8615 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8616 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8617 			    &ipif->ipif_v6net_mask);
8618 		} else {
8619 			sin = (sin_t *)&lifr->lifr_addr;
8620 			*sin = sin_null;
8621 			sin->sin_family = AF_INET;
8622 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8623 			lifr->lifr_addrlen = ip_mask_to_plen(
8624 			    ipif->ipif_net_mask);
8625 		}
8626 		lifr++;
8627 	}
8628 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8629 	rw_exit(&ipst->ips_ill_g_lock);
8630 	ipif_refrele(orig_ipif);
8631 	mp1->b_wptr = (uchar_t *)lifr;
8632 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8633 
8634 	return (0);
8635 }
8636 
8637 /* ARGSUSED */
8638 int
8639 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8640     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8641 {
8642 	mblk_t *mp1;
8643 	int	list;
8644 	ill_t	*ill;
8645 	ipif_t	*ipif;
8646 	int	flags;
8647 	int	numlifs = 0;
8648 	size_t	lifc_bufsize;
8649 	struct	lifreq *lifr;
8650 	sa_family_t	family;
8651 	struct	sockaddr_in	*sin;
8652 	struct	sockaddr_in6	*sin6;
8653 	ill_walk_context_t	ctx;
8654 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8655 	int32_t	lifclen;
8656 	zoneid_t zoneid;
8657 	STRUCT_HANDLE(lifconf, lifc);
8658 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8659 
8660 	ip1dbg(("ip_sioctl_get_lifconf"));
8661 
8662 	ASSERT(q->q_next == NULL);
8663 
8664 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8665 
8666 	/* Existence verified in ip_wput_nondata */
8667 	mp1 = mp->b_cont->b_cont;
8668 
8669 	/*
8670 	 * An extended version of SIOCGIFCONF that takes an
8671 	 * additional address family and flags field.
8672 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8673 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8674 	 * interfaces are omitted.
8675 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8676 	 * unless LIFC_TEMPORARY is specified.
8677 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8678 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8679 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8680 	 * has priority over LIFC_NOXMIT.
8681 	 */
8682 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8683 
8684 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8685 		return (EINVAL);
8686 
8687 	/*
8688 	 * Must be (better be!) continuation of a TRANSPARENT
8689 	 * IOCTL.  We just copied in the lifconf structure.
8690 	 */
8691 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8692 
8693 	family = STRUCT_FGET(lifc, lifc_family);
8694 	flags = STRUCT_FGET(lifc, lifc_flags);
8695 
8696 	switch (family) {
8697 	case AF_UNSPEC:
8698 		/*
8699 		 * walk all ILL's.
8700 		 */
8701 		list = MAX_G_HEADS;
8702 		break;
8703 	case AF_INET:
8704 		/*
8705 		 * walk only IPV4 ILL's.
8706 		 */
8707 		list = IP_V4_G_HEAD;
8708 		break;
8709 	case AF_INET6:
8710 		/*
8711 		 * walk only IPV6 ILL's.
8712 		 */
8713 		list = IP_V6_G_HEAD;
8714 		break;
8715 	default:
8716 		return (EAFNOSUPPORT);
8717 	}
8718 
8719 	/*
8720 	 * Allocate a buffer to hold requested information.
8721 	 *
8722 	 * If lifc_len is larger than what is needed, we only
8723 	 * allocate what we will use.
8724 	 *
8725 	 * If lifc_len is smaller than what is needed, return
8726 	 * EINVAL.
8727 	 */
8728 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8729 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8730 	lifclen = STRUCT_FGET(lifc, lifc_len);
8731 	if (lifc_bufsize > lifclen) {
8732 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8733 			return (EINVAL);
8734 		else
8735 			lifc_bufsize = lifclen;
8736 	}
8737 
8738 	mp1 = mi_copyout_alloc(q, mp,
8739 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8740 	if (mp1 == NULL)
8741 		return (ENOMEM);
8742 
8743 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8744 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8745 
8746 	lifr = (struct lifreq *)mp1->b_rptr;
8747 
8748 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8749 	ill = ill_first(list, list, &ctx, ipst);
8750 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8751 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8752 			continue;
8753 
8754 		for (ipif = ill->ill_ipif; ipif != NULL;
8755 		    ipif = ipif->ipif_next) {
8756 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8757 			    !(flags & LIFC_NOXMIT))
8758 				continue;
8759 
8760 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8761 			    !(flags & LIFC_TEMPORARY))
8762 				continue;
8763 
8764 			if (((ipif->ipif_flags &
8765 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8766 			    IPIF_DEPRECATED)) ||
8767 			    IS_LOOPBACK(ill) ||
8768 			    !(ipif->ipif_flags & IPIF_UP)) &&
8769 			    (flags & LIFC_EXTERNAL_SOURCE))
8770 				continue;
8771 
8772 			if (zoneid != ipif->ipif_zoneid &&
8773 			    ipif->ipif_zoneid != ALL_ZONES &&
8774 			    (zoneid != GLOBAL_ZONEID ||
8775 			    !(flags & LIFC_ALLZONES)))
8776 				continue;
8777 
8778 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8779 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8780 					rw_exit(&ipst->ips_ill_g_lock);
8781 					return (EINVAL);
8782 				} else {
8783 					goto lif_copydone;
8784 				}
8785 			}
8786 
8787 			ipif_get_name(ipif, lifr->lifr_name,
8788 			    sizeof (lifr->lifr_name));
8789 			lifr->lifr_type = ill->ill_type;
8790 			if (ipif->ipif_isv6) {
8791 				sin6 = (sin6_t *)&lifr->lifr_addr;
8792 				*sin6 = sin6_null;
8793 				sin6->sin6_family = AF_INET6;
8794 				sin6->sin6_addr =
8795 				    ipif->ipif_v6lcl_addr;
8796 				lifr->lifr_addrlen =
8797 				    ip_mask_to_plen_v6(
8798 				    &ipif->ipif_v6net_mask);
8799 			} else {
8800 				sin = (sin_t *)&lifr->lifr_addr;
8801 				*sin = sin_null;
8802 				sin->sin_family = AF_INET;
8803 				sin->sin_addr.s_addr =
8804 				    ipif->ipif_lcl_addr;
8805 				lifr->lifr_addrlen =
8806 				    ip_mask_to_plen(
8807 				    ipif->ipif_net_mask);
8808 			}
8809 			lifr++;
8810 		}
8811 	}
8812 lif_copydone:
8813 	rw_exit(&ipst->ips_ill_g_lock);
8814 
8815 	mp1->b_wptr = (uchar_t *)lifr;
8816 	if (STRUCT_BUF(lifc) != NULL) {
8817 		STRUCT_FSET(lifc, lifc_len,
8818 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8819 	}
8820 	return (0);
8821 }
8822 
8823 static void
8824 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8825 {
8826 	ip6_asp_t *table;
8827 	size_t table_size;
8828 	mblk_t *data_mp;
8829 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8830 	ip_stack_t	*ipst;
8831 
8832 	if (q->q_next == NULL)
8833 		ipst = CONNQ_TO_IPST(q);
8834 	else
8835 		ipst = ILLQ_TO_IPST(q);
8836 
8837 	/* These two ioctls are I_STR only */
8838 	if (iocp->ioc_count == TRANSPARENT) {
8839 		miocnak(q, mp, 0, EINVAL);
8840 		return;
8841 	}
8842 
8843 	data_mp = mp->b_cont;
8844 	if (data_mp == NULL) {
8845 		/* The user passed us a NULL argument */
8846 		table = NULL;
8847 		table_size = iocp->ioc_count;
8848 	} else {
8849 		/*
8850 		 * The user provided a table.  The stream head
8851 		 * may have copied in the user data in chunks,
8852 		 * so make sure everything is pulled up
8853 		 * properly.
8854 		 */
8855 		if (MBLKL(data_mp) < iocp->ioc_count) {
8856 			mblk_t *new_data_mp;
8857 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8858 			    NULL) {
8859 				miocnak(q, mp, 0, ENOMEM);
8860 				return;
8861 			}
8862 			freemsg(data_mp);
8863 			data_mp = new_data_mp;
8864 			mp->b_cont = data_mp;
8865 		}
8866 		table = (ip6_asp_t *)data_mp->b_rptr;
8867 		table_size = iocp->ioc_count;
8868 	}
8869 
8870 	switch (iocp->ioc_cmd) {
8871 	case SIOCGIP6ADDRPOLICY:
8872 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8873 		if (iocp->ioc_rval == -1)
8874 			iocp->ioc_error = EINVAL;
8875 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8876 		else if (table != NULL &&
8877 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8878 			ip6_asp_t *src = table;
8879 			ip6_asp32_t *dst = (void *)table;
8880 			int count = table_size / sizeof (ip6_asp_t);
8881 			int i;
8882 
8883 			/*
8884 			 * We need to do an in-place shrink of the array
8885 			 * to match the alignment attributes of the
8886 			 * 32-bit ABI looking at it.
8887 			 */
8888 			/* LINTED: logical expression always true: op "||" */
8889 			ASSERT(sizeof (*src) > sizeof (*dst));
8890 			for (i = 1; i < count; i++)
8891 				bcopy(src + i, dst + i, sizeof (*dst));
8892 		}
8893 #endif
8894 		break;
8895 
8896 	case SIOCSIP6ADDRPOLICY:
8897 		ASSERT(mp->b_prev == NULL);
8898 		mp->b_prev = (void *)q;
8899 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8900 		/*
8901 		 * We pass in the datamodel here so that the ip6_asp_replace()
8902 		 * routine can handle converting from 32-bit to native formats
8903 		 * where necessary.
8904 		 *
8905 		 * A better way to handle this might be to convert the inbound
8906 		 * data structure here, and hang it off a new 'mp'; thus the
8907 		 * ip6_asp_replace() logic would always be dealing with native
8908 		 * format data structures..
8909 		 *
8910 		 * (An even simpler way to handle these ioctls is to just
8911 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8912 		 * and just recompile everything that depends on it.)
8913 		 */
8914 #endif
8915 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
8916 		    iocp->ioc_flag & IOC_MODELS);
8917 		return;
8918 	}
8919 
8920 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8921 	qreply(q, mp);
8922 }
8923 
8924 static void
8925 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8926 {
8927 	mblk_t 		*data_mp;
8928 	struct dstinforeq	*dir;
8929 	uint8_t		*end, *cur;
8930 	in6_addr_t	*daddr, *saddr;
8931 	ipaddr_t	v4daddr;
8932 	ire_t		*ire;
8933 	char		*slabel, *dlabel;
8934 	boolean_t	isipv4;
8935 	int		match_ire;
8936 	ill_t		*dst_ill;
8937 	ipif_t		*src_ipif, *ire_ipif;
8938 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8939 	zoneid_t	zoneid;
8940 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
8941 
8942 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8943 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8944 
8945 	/*
8946 	 * This ioctl is I_STR only, and must have a
8947 	 * data mblk following the M_IOCTL mblk.
8948 	 */
8949 	data_mp = mp->b_cont;
8950 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8951 		miocnak(q, mp, 0, EINVAL);
8952 		return;
8953 	}
8954 
8955 	if (MBLKL(data_mp) < iocp->ioc_count) {
8956 		mblk_t *new_data_mp;
8957 
8958 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8959 			miocnak(q, mp, 0, ENOMEM);
8960 			return;
8961 		}
8962 		freemsg(data_mp);
8963 		data_mp = new_data_mp;
8964 		mp->b_cont = data_mp;
8965 	}
8966 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8967 
8968 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8969 	    end - cur >= sizeof (struct dstinforeq);
8970 	    cur += sizeof (struct dstinforeq)) {
8971 		dir = (struct dstinforeq *)cur;
8972 		daddr = &dir->dir_daddr;
8973 		saddr = &dir->dir_saddr;
8974 
8975 		/*
8976 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8977 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8978 		 * and ipif_select_source[_v6]() do not.
8979 		 */
8980 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8981 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
8982 
8983 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8984 		if (isipv4) {
8985 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8986 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8987 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
8988 		} else {
8989 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8990 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
8991 		}
8992 		if (ire == NULL) {
8993 			dir->dir_dreachable = 0;
8994 
8995 			/* move on to next dst addr */
8996 			continue;
8997 		}
8998 		dir->dir_dreachable = 1;
8999 
9000 		ire_ipif = ire->ire_ipif;
9001 		if (ire_ipif == NULL)
9002 			goto next_dst;
9003 
9004 		/*
9005 		 * We expect to get back an interface ire or a
9006 		 * gateway ire cache entry.  For both types, the
9007 		 * output interface is ire_ipif->ipif_ill.
9008 		 */
9009 		dst_ill = ire_ipif->ipif_ill;
9010 		dir->dir_dmactype = dst_ill->ill_mactype;
9011 
9012 		if (isipv4) {
9013 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9014 		} else {
9015 			src_ipif = ipif_select_source_v6(dst_ill,
9016 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9017 		}
9018 		if (src_ipif == NULL)
9019 			goto next_dst;
9020 
9021 		*saddr = src_ipif->ipif_v6lcl_addr;
9022 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9023 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9024 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9025 		dir->dir_sdeprecated =
9026 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9027 		ipif_refrele(src_ipif);
9028 next_dst:
9029 		ire_refrele(ire);
9030 	}
9031 	miocack(q, mp, iocp->ioc_count, 0);
9032 }
9033 
9034 /*
9035  * Check if this is an address assigned to this machine.
9036  * Skips interfaces that are down by using ire checks.
9037  * Translates mapped addresses to v4 addresses and then
9038  * treats them as such, returning true if the v4 address
9039  * associated with this mapped address is configured.
9040  * Note: Applications will have to be careful what they do
9041  * with the response; use of mapped addresses limits
9042  * what can be done with the socket, especially with
9043  * respect to socket options and ioctls - neither IPv4
9044  * options nor IPv6 sticky options/ancillary data options
9045  * may be used.
9046  */
9047 /* ARGSUSED */
9048 int
9049 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9050     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9051 {
9052 	struct sioc_addrreq *sia;
9053 	sin_t *sin;
9054 	ire_t *ire;
9055 	mblk_t *mp1;
9056 	zoneid_t zoneid;
9057 	ip_stack_t	*ipst;
9058 
9059 	ip1dbg(("ip_sioctl_tmyaddr"));
9060 
9061 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9062 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9063 	ipst = CONNQ_TO_IPST(q);
9064 
9065 	/* Existence verified in ip_wput_nondata */
9066 	mp1 = mp->b_cont->b_cont;
9067 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9068 	sin = (sin_t *)&sia->sa_addr;
9069 	switch (sin->sin_family) {
9070 	case AF_INET6: {
9071 		sin6_t *sin6 = (sin6_t *)sin;
9072 
9073 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9074 			ipaddr_t v4_addr;
9075 
9076 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9077 			    v4_addr);
9078 			ire = ire_ctable_lookup(v4_addr, 0,
9079 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9080 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9081 		} else {
9082 			in6_addr_t v6addr;
9083 
9084 			v6addr = sin6->sin6_addr;
9085 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9086 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9087 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9088 		}
9089 		break;
9090 	}
9091 	case AF_INET: {
9092 		ipaddr_t v4addr;
9093 
9094 		v4addr = sin->sin_addr.s_addr;
9095 		ire = ire_ctable_lookup(v4addr, 0,
9096 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9097 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9098 		break;
9099 	}
9100 	default:
9101 		return (EAFNOSUPPORT);
9102 	}
9103 	if (ire != NULL) {
9104 		sia->sa_res = 1;
9105 		ire_refrele(ire);
9106 	} else {
9107 		sia->sa_res = 0;
9108 	}
9109 	return (0);
9110 }
9111 
9112 /*
9113  * Check if this is an address assigned on-link i.e. neighbor,
9114  * and makes sure it's reachable from the current zone.
9115  * Returns true for my addresses as well.
9116  * Translates mapped addresses to v4 addresses and then
9117  * treats them as such, returning true if the v4 address
9118  * associated with this mapped address is configured.
9119  * Note: Applications will have to be careful what they do
9120  * with the response; use of mapped addresses limits
9121  * what can be done with the socket, especially with
9122  * respect to socket options and ioctls - neither IPv4
9123  * options nor IPv6 sticky options/ancillary data options
9124  * may be used.
9125  */
9126 /* ARGSUSED */
9127 int
9128 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9129     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9130 {
9131 	struct sioc_addrreq *sia;
9132 	sin_t *sin;
9133 	mblk_t	*mp1;
9134 	ire_t *ire = NULL;
9135 	zoneid_t zoneid;
9136 	ip_stack_t	*ipst;
9137 
9138 	ip1dbg(("ip_sioctl_tonlink"));
9139 
9140 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9141 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9142 	ipst = CONNQ_TO_IPST(q);
9143 
9144 	/* Existence verified in ip_wput_nondata */
9145 	mp1 = mp->b_cont->b_cont;
9146 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9147 	sin = (sin_t *)&sia->sa_addr;
9148 
9149 	/*
9150 	 * Match addresses with a zero gateway field to avoid
9151 	 * routes going through a router.
9152 	 * Exclude broadcast and multicast addresses.
9153 	 */
9154 	switch (sin->sin_family) {
9155 	case AF_INET6: {
9156 		sin6_t *sin6 = (sin6_t *)sin;
9157 
9158 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9159 			ipaddr_t v4_addr;
9160 
9161 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9162 			    v4_addr);
9163 			if (!CLASSD(v4_addr)) {
9164 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9165 				    NULL, NULL, zoneid, NULL,
9166 				    MATCH_IRE_GW, ipst);
9167 			}
9168 		} else {
9169 			in6_addr_t v6addr;
9170 			in6_addr_t v6gw;
9171 
9172 			v6addr = sin6->sin6_addr;
9173 			v6gw = ipv6_all_zeros;
9174 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9175 				ire = ire_route_lookup_v6(&v6addr, 0,
9176 				    &v6gw, 0, NULL, NULL, zoneid,
9177 				    NULL, MATCH_IRE_GW, ipst);
9178 			}
9179 		}
9180 		break;
9181 	}
9182 	case AF_INET: {
9183 		ipaddr_t v4addr;
9184 
9185 		v4addr = sin->sin_addr.s_addr;
9186 		if (!CLASSD(v4addr)) {
9187 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9188 			    NULL, NULL, zoneid, NULL,
9189 			    MATCH_IRE_GW, ipst);
9190 		}
9191 		break;
9192 	}
9193 	default:
9194 		return (EAFNOSUPPORT);
9195 	}
9196 	sia->sa_res = 0;
9197 	if (ire != NULL) {
9198 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9199 		    IRE_LOCAL|IRE_LOOPBACK)) {
9200 			sia->sa_res = 1;
9201 		}
9202 		ire_refrele(ire);
9203 	}
9204 	return (0);
9205 }
9206 
9207 /*
9208  * TBD: implement when kernel maintaines a list of site prefixes.
9209  */
9210 /* ARGSUSED */
9211 int
9212 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9213     ip_ioctl_cmd_t *ipip, void *ifreq)
9214 {
9215 	return (ENXIO);
9216 }
9217 
9218 /* ARGSUSED */
9219 int
9220 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9221     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9222 {
9223 	ill_t		*ill;
9224 	mblk_t		*mp1;
9225 	conn_t		*connp;
9226 	boolean_t	success;
9227 
9228 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9229 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9230 	/* ioctl comes down on an conn */
9231 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9232 	connp = Q_TO_CONN(q);
9233 
9234 	mp->b_datap->db_type = M_IOCTL;
9235 
9236 	/*
9237 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9238 	 * The original mp contains contaminated b_next values due to 'mi',
9239 	 * which is needed to do the mi_copy_done. Unfortunately if we
9240 	 * send down the original mblk itself and if we are popped due to an
9241 	 * an unplumb before the response comes back from tunnel,
9242 	 * the streamhead (which does a freemsg) will see this contaminated
9243 	 * message and the assertion in freemsg about non-null b_next/b_prev
9244 	 * will panic a DEBUG kernel.
9245 	 */
9246 	mp1 = copymsg(mp);
9247 	if (mp1 == NULL)
9248 		return (ENOMEM);
9249 
9250 	ill = ipif->ipif_ill;
9251 	mutex_enter(&connp->conn_lock);
9252 	mutex_enter(&ill->ill_lock);
9253 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9254 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9255 		    mp, 0);
9256 	} else {
9257 		success = ill_pending_mp_add(ill, connp, mp);
9258 	}
9259 	mutex_exit(&ill->ill_lock);
9260 	mutex_exit(&connp->conn_lock);
9261 
9262 	if (success) {
9263 		ip1dbg(("sending down tunparam request "));
9264 		putnext(ill->ill_wq, mp1);
9265 		return (EINPROGRESS);
9266 	} else {
9267 		/* The conn has started closing */
9268 		freemsg(mp1);
9269 		return (EINTR);
9270 	}
9271 }
9272 
9273 /*
9274  * ARP IOCTLs.
9275  * How does IP get in the business of fronting ARP configuration/queries?
9276  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9277  * are by tradition passed in through a datagram socket.  That lands in IP.
9278  * As it happens, this is just as well since the interface is quite crude in
9279  * that it passes in no information about protocol or hardware types, or
9280  * interface association.  After making the protocol assumption, IP is in
9281  * the position to look up the name of the ILL, which ARP will need, and
9282  * format a request that can be handled by ARP.  The request is passed up
9283  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9284  * back a response.  ARP supports its own set of more general IOCTLs, in
9285  * case anyone is interested.
9286  */
9287 /* ARGSUSED */
9288 int
9289 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9290     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9291 {
9292 	mblk_t *mp1;
9293 	mblk_t *mp2;
9294 	mblk_t *pending_mp;
9295 	ipaddr_t ipaddr;
9296 	area_t *area;
9297 	struct iocblk *iocp;
9298 	conn_t *connp;
9299 	struct arpreq *ar;
9300 	struct xarpreq *xar;
9301 	int flags, alength;
9302 	uchar_t *lladdr;
9303 	ire_t *ire;
9304 	ip_stack_t *ipst;
9305 	ill_t *ill = ipif->ipif_ill;
9306 	ill_t *proxy_ill = NULL;
9307 	ipmp_arpent_t *entp = NULL;
9308 	boolean_t if_arp_ioctl = B_FALSE;
9309 	boolean_t proxyarp = B_FALSE;
9310 
9311 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9312 	connp = Q_TO_CONN(q);
9313 	ipst = connp->conn_netstack->netstack_ip;
9314 
9315 	if (ipip->ipi_cmd_type == XARP_CMD) {
9316 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9317 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9318 		ar = NULL;
9319 
9320 		flags = xar->xarp_flags;
9321 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9322 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9323 		/*
9324 		 * Validate against user's link layer address length
9325 		 * input and name and addr length limits.
9326 		 */
9327 		alength = ill->ill_phys_addr_length;
9328 		if (ipip->ipi_cmd == SIOCSXARP) {
9329 			if (alength != xar->xarp_ha.sdl_alen ||
9330 			    (alength + xar->xarp_ha.sdl_nlen >
9331 			    sizeof (xar->xarp_ha.sdl_data)))
9332 				return (EINVAL);
9333 		}
9334 	} else {
9335 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9336 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9337 		xar = NULL;
9338 
9339 		flags = ar->arp_flags;
9340 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9341 		/*
9342 		 * Theoretically, the sa_family could tell us what link
9343 		 * layer type this operation is trying to deal with. By
9344 		 * common usage AF_UNSPEC means ethernet. We'll assume
9345 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9346 		 * for now. Our new SIOC*XARP ioctls can be used more
9347 		 * generally.
9348 		 *
9349 		 * If the underlying media happens to have a non 6 byte
9350 		 * address, arp module will fail set/get, but the del
9351 		 * operation will succeed.
9352 		 */
9353 		alength = 6;
9354 		if ((ipip->ipi_cmd != SIOCDARP) &&
9355 		    (alength != ill->ill_phys_addr_length)) {
9356 			return (EINVAL);
9357 		}
9358 	}
9359 
9360 	ipaddr = sin->sin_addr.s_addr;
9361 
9362 	/*
9363 	 * IPMP ARP special handling:
9364 	 *
9365 	 * 1. Since ARP mappings must appear consistent across the group,
9366 	 *    prohibit changing ARP mappings on the underlying interfaces.
9367 	 *
9368 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9369 	 *    IP itself, prohibit changing them.
9370 	 *
9371 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9372 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9373 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9374 	 */
9375 	if (IS_UNDER_IPMP(ill)) {
9376 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9377 			return (EPERM);
9378 	}
9379 	if (IS_IPMP(ill)) {
9380 		ipmp_illgrp_t *illg = ill->ill_grp;
9381 
9382 		switch (ipip->ipi_cmd) {
9383 		case SIOCSARP:
9384 		case SIOCSXARP:
9385 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9386 			if (proxy_ill != NULL) {
9387 				proxyarp = B_TRUE;
9388 				if (!ipmp_ill_is_active(proxy_ill))
9389 					proxy_ill = ipmp_illgrp_next_ill(illg);
9390 				if (proxy_ill != NULL)
9391 					lladdr = proxy_ill->ill_phys_addr;
9392 			}
9393 			/* FALLTHRU */
9394 		case SIOCDARP:
9395 		case SIOCDXARP:
9396 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9397 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9398 			if (ire != NULL) {
9399 				ire_refrele(ire);
9400 				return (EPERM);
9401 			}
9402 		}
9403 	}
9404 
9405 	/*
9406 	 * We are going to pass up to ARP a packet chain that looks
9407 	 * like:
9408 	 *
9409 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9410 	 *
9411 	 * Get a copy of the original IOCTL mblk to head the chain,
9412 	 * to be sent up (in mp1). Also get another copy to store
9413 	 * in the ill_pending_mp list, for matching the response
9414 	 * when it comes back from ARP.
9415 	 */
9416 	mp1 = copyb(mp);
9417 	pending_mp = copymsg(mp);
9418 	if (mp1 == NULL || pending_mp == NULL) {
9419 		if (mp1 != NULL)
9420 			freeb(mp1);
9421 		if (pending_mp != NULL)
9422 			inet_freemsg(pending_mp);
9423 		return (ENOMEM);
9424 	}
9425 
9426 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9427 	    (caddr_t)&ipaddr);
9428 	if (mp2 == NULL) {
9429 		freeb(mp1);
9430 		inet_freemsg(pending_mp);
9431 		return (ENOMEM);
9432 	}
9433 	/* Put together the chain. */
9434 	mp1->b_cont = mp2;
9435 	mp1->b_datap->db_type = M_IOCTL;
9436 	mp2->b_cont = mp;
9437 	mp2->b_datap->db_type = M_DATA;
9438 
9439 	iocp = (struct iocblk *)mp1->b_rptr;
9440 
9441 	/*
9442 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9443 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9444 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9445 	 * ioc_count field; set ioc_count to be correct.
9446 	 */
9447 	iocp->ioc_count = MBLKL(mp1->b_cont);
9448 
9449 	/*
9450 	 * Set the proper command in the ARP message.
9451 	 * Convert the SIOC{G|S|D}ARP calls into our
9452 	 * AR_ENTRY_xxx calls.
9453 	 */
9454 	area = (area_t *)mp2->b_rptr;
9455 	switch (iocp->ioc_cmd) {
9456 	case SIOCDARP:
9457 	case SIOCDXARP:
9458 		/*
9459 		 * We defer deleting the corresponding IRE until
9460 		 * we return from arp.
9461 		 */
9462 		area->area_cmd = AR_ENTRY_DELETE;
9463 		area->area_proto_mask_offset = 0;
9464 		break;
9465 	case SIOCGARP:
9466 	case SIOCGXARP:
9467 		area->area_cmd = AR_ENTRY_SQUERY;
9468 		area->area_proto_mask_offset = 0;
9469 		break;
9470 	case SIOCSARP:
9471 	case SIOCSXARP:
9472 		/*
9473 		 * Delete the corresponding ire to make sure IP will
9474 		 * pick up any change from arp.
9475 		 */
9476 		if (!if_arp_ioctl) {
9477 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9478 		} else {
9479 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9480 			if (ipif != NULL) {
9481 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9482 				    ipst);
9483 				ipif_refrele(ipif);
9484 			}
9485 		}
9486 		break;
9487 	}
9488 	iocp->ioc_cmd = area->area_cmd;
9489 
9490 	/*
9491 	 * Fill in the rest of the ARP operation fields.
9492 	 */
9493 	area->area_hw_addr_length = alength;
9494 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9495 
9496 	/* Translate the flags. */
9497 	if (flags & ATF_PERM)
9498 		area->area_flags |= ACE_F_PERMANENT;
9499 	if (flags & ATF_PUBL)
9500 		area->area_flags |= ACE_F_PUBLISH;
9501 	if (flags & ATF_AUTHORITY)
9502 		area->area_flags |= ACE_F_AUTHORITY;
9503 
9504 	/*
9505 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9506 	 * so that IP can update ARP as the active ills in the group change.
9507 	 */
9508 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9509 	    (area->area_flags & ACE_F_PERMANENT)) {
9510 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9511 
9512 		/*
9513 		 * The second part of the conditional below handles a corner
9514 		 * case: if this is proxy ARP and the IPMP group has no active
9515 		 * interfaces, we can't send the request to ARP now since it
9516 		 * won't be able to build an ACE.  So we return success and
9517 		 * notify ARP about the proxy ARP entry once an interface
9518 		 * becomes active.
9519 		 */
9520 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9521 			mp2->b_cont = NULL;
9522 			inet_freemsg(mp1);
9523 			inet_freemsg(pending_mp);
9524 			return (entp == NULL ? ENOMEM : 0);
9525 		}
9526 	}
9527 
9528 	/*
9529 	 * Before sending 'mp' to ARP, we have to clear the b_next
9530 	 * and b_prev. Otherwise if STREAMS encounters such a message
9531 	 * in freemsg(), (because ARP can close any time) it can cause
9532 	 * a panic. But mi code needs the b_next and b_prev values of
9533 	 * mp->b_cont, to complete the ioctl. So we store it here
9534 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9535 	 * when the response comes down from ARP.
9536 	 */
9537 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9538 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9539 	mp->b_cont->b_next = NULL;
9540 	mp->b_cont->b_prev = NULL;
9541 
9542 	mutex_enter(&connp->conn_lock);
9543 	mutex_enter(&ill->ill_lock);
9544 	/* conn has not yet started closing, hence this can't fail */
9545 	if (ipip->ipi_flags & IPI_WR) {
9546 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9547 		    pending_mp, 0) != 0);
9548 	} else {
9549 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9550 	}
9551 	mutex_exit(&ill->ill_lock);
9552 	mutex_exit(&connp->conn_lock);
9553 
9554 	/*
9555 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9556 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9557 	 */
9558 	putnext(ill->ill_rq, mp1);
9559 
9560 	/*
9561 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9562 	 */
9563 	if (entp != NULL)
9564 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9565 
9566 	return (EINPROGRESS);
9567 }
9568 
9569 /*
9570  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9571  * the associated sin and refhold and return the associated ipif via `ci'.
9572  */
9573 int
9574 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9575     cmd_info_t *ci, ipsq_func_t func)
9576 {
9577 	mblk_t	*mp1;
9578 	int	err;
9579 	sin_t	*sin;
9580 	conn_t	*connp;
9581 	ipif_t	*ipif;
9582 	ire_t	*ire = NULL;
9583 	ill_t	*ill = NULL;
9584 	boolean_t exists;
9585 	ip_stack_t *ipst;
9586 	struct arpreq *ar;
9587 	struct xarpreq *xar;
9588 	struct sockaddr_dl *sdl;
9589 
9590 	/* ioctl comes down on a conn */
9591 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9592 	connp = Q_TO_CONN(q);
9593 	if (connp->conn_af_isv6)
9594 		return (ENXIO);
9595 
9596 	ipst = connp->conn_netstack->netstack_ip;
9597 
9598 	/* Verified in ip_wput_nondata */
9599 	mp1 = mp->b_cont->b_cont;
9600 
9601 	if (ipip->ipi_cmd_type == XARP_CMD) {
9602 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9603 		xar = (struct xarpreq *)mp1->b_rptr;
9604 		sin = (sin_t *)&xar->xarp_pa;
9605 		sdl = &xar->xarp_ha;
9606 
9607 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9608 			return (ENXIO);
9609 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9610 			return (EINVAL);
9611 	} else {
9612 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9613 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9614 		ar = (struct arpreq *)mp1->b_rptr;
9615 		sin = (sin_t *)&ar->arp_pa;
9616 	}
9617 
9618 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9619 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9620 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9621 		    mp, func, &err, ipst);
9622 		if (ipif == NULL)
9623 			return (err);
9624 		if (ipif->ipif_id != 0) {
9625 			ipif_refrele(ipif);
9626 			return (ENXIO);
9627 		}
9628 	} else {
9629 		/*
9630 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9631 		 * of 0: use the IP address to find the ipif.  If the IP
9632 		 * address is an IPMP test address, ire_ftable_lookup() will
9633 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9634 		 */
9635 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9636 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9637 		if (ipif == NULL) {
9638 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9639 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9640 			    MATCH_IRE_TYPE, ipst);
9641 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9642 				if (ire != NULL)
9643 					ire_refrele(ire);
9644 				return (ENXIO);
9645 			}
9646 			ipif = ill->ill_ipif;
9647 			ipif_refhold(ipif);
9648 			ire_refrele(ire);
9649 		}
9650 	}
9651 
9652 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9653 		ipif_refrele(ipif);
9654 		return (ENXIO);
9655 	}
9656 
9657 	ci->ci_sin = sin;
9658 	ci->ci_ipif = ipif;
9659 	return (0);
9660 }
9661 
9662 /*
9663  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9664  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9665  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9666  * up and thus an ill can join that illgrp.
9667  *
9668  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9669  * open()/close() primarily because close() is not allowed to fail or block
9670  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9671  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9672  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9673  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9674  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9675  * state if I_UNLINK didn't occur.
9676  *
9677  * Note that for each plumb/unplumb operation, we may end up here more than
9678  * once because of the way ifconfig works.  However, it's OK to link the same
9679  * illgrp more than once, or unlink an illgrp that's already unlinked.
9680  */
9681 static int
9682 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9683 {
9684 	int err;
9685 	ip_stack_t *ipst = ill->ill_ipst;
9686 
9687 	ASSERT(IS_IPMP(ill));
9688 	ASSERT(IAM_WRITER_ILL(ill));
9689 
9690 	switch (ioccmd) {
9691 	case I_LINK:
9692 		return (ENOTSUP);
9693 
9694 	case I_PLINK:
9695 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9696 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9697 		rw_exit(&ipst->ips_ipmp_lock);
9698 		break;
9699 
9700 	case I_PUNLINK:
9701 		/*
9702 		 * Require all UP ipifs be brought down prior to unlinking the
9703 		 * illgrp so any associated IREs (and other state) is torched.
9704 		 */
9705 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9706 			return (EBUSY);
9707 
9708 		/*
9709 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9710 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9711 		 * join this group.  Specifically: ills trying to join grab
9712 		 * ipmp_lock and bump a "pending join" counter checked by
9713 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9714 		 * joins can occur (since we have ipmp_lock).  Once we drop
9715 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9716 		 * find the illgrp (since we unlinked it) and will return
9717 		 * EAFNOSUPPORT.  This will then take them back through the
9718 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9719 		 * back through I_PLINK above.
9720 		 */
9721 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9722 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9723 		rw_exit(&ipst->ips_ipmp_lock);
9724 		return (err);
9725 	default:
9726 		break;
9727 	}
9728 	return (0);
9729 }
9730 
9731 /*
9732  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9733  * atomically set/clear the muxids. Also complete the ioctl by acking or
9734  * naking it.  Note that the code is structured such that the link type,
9735  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9736  * its clones use the persistent link, while pppd(1M) and perhaps many
9737  * other daemons may use non-persistent link.  When combined with some
9738  * ill_t states, linking and unlinking lower streams may be used as
9739  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9740  */
9741 /* ARGSUSED */
9742 void
9743 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9744 {
9745 	mblk_t		*mp1, *mp2;
9746 	struct linkblk	*li;
9747 	struct ipmx_s	*ipmxp;
9748 	ill_t		*ill;
9749 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9750 	int		err = 0;
9751 	boolean_t	entered_ipsq = B_FALSE;
9752 	boolean_t	islink;
9753 	ip_stack_t	*ipst;
9754 
9755 	if (CONN_Q(q))
9756 		ipst = CONNQ_TO_IPST(q);
9757 	else
9758 		ipst = ILLQ_TO_IPST(q);
9759 
9760 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9761 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9762 
9763 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9764 
9765 	mp1 = mp->b_cont;	/* This is the linkblk info */
9766 	li = (struct linkblk *)mp1->b_rptr;
9767 
9768 	/*
9769 	 * ARP has added this special mblk, and the utility is asking us
9770 	 * to perform consistency checks, and also atomically set the
9771 	 * muxid. Ifconfig is an example.  It achieves this by using
9772 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9773 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9774 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9775 	 * and other comments in this routine for more details.
9776 	 */
9777 	mp2 = mp1->b_cont;	/* This is added by ARP */
9778 
9779 	/*
9780 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9781 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9782 	 * get the special mblk above.  For backward compatibility, we
9783 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9784 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9785 	 * not atomic, and can leave the streams unplumbable if the utility
9786 	 * is interrupted before it does the SIOCSLIFMUXID.
9787 	 */
9788 	if (mp2 == NULL) {
9789 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9790 		if (err == EINPROGRESS)
9791 			return;
9792 		goto done;
9793 	}
9794 
9795 	/*
9796 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9797 	 * ARP has appended this last mblk to tell us whether the lower stream
9798 	 * is an arp-dev stream or an IP module stream.
9799 	 */
9800 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9801 	if (ipmxp->ipmx_arpdev_stream) {
9802 		/*
9803 		 * The lower stream is the arp-dev stream.
9804 		 */
9805 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9806 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9807 		if (ill == NULL) {
9808 			if (err == EINPROGRESS)
9809 				return;
9810 			err = EINVAL;
9811 			goto done;
9812 		}
9813 
9814 		if (ipsq == NULL) {
9815 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9816 			    NEW_OP, B_FALSE);
9817 			if (ipsq == NULL) {
9818 				ill_refrele(ill);
9819 				return;
9820 			}
9821 			entered_ipsq = B_TRUE;
9822 		}
9823 		ASSERT(IAM_WRITER_ILL(ill));
9824 		ill_refrele(ill);
9825 
9826 		/*
9827 		 * To ensure consistency between IP and ARP, the following
9828 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9829 		 * This is because the muxid's are stored in the IP stream on
9830 		 * the ill.
9831 		 *
9832 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9833 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9834 		 * not yet plinked, and it also checks that the corresponding
9835 		 * IP stream is already plinked.
9836 		 *
9837 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9838 		 * punlinking the IP stream. IP does not allow punlink of the
9839 		 * IP stream unless the arp stream has been punlinked.
9840 		 */
9841 		if ((islink &&
9842 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9843 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9844 			err = EINVAL;
9845 			goto done;
9846 		}
9847 
9848 		if (IS_IPMP(ill) &&
9849 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9850 			goto done;
9851 
9852 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9853 	} else {
9854 		/*
9855 		 * The lower stream is probably an IP module stream.  Do
9856 		 * consistency checking.
9857 		 */
9858 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9859 		if (err == EINPROGRESS)
9860 			return;
9861 	}
9862 done:
9863 	if (err == 0)
9864 		miocack(q, mp, 0, 0);
9865 	else
9866 		miocnak(q, mp, 0, err);
9867 
9868 	/* Conn was refheld in ip_sioctl_copyin_setup */
9869 	if (CONN_Q(q))
9870 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9871 	if (entered_ipsq)
9872 		ipsq_exit(ipsq);
9873 }
9874 
9875 /*
9876  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9877  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9878  * module stream).  If `doconsist' is set, then do the extended consistency
9879  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9880  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9881  * an error code on failure.
9882  */
9883 static int
9884 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9885     struct linkblk *li, boolean_t doconsist)
9886 {
9887 	int		err = 0;
9888 	ill_t  		*ill;
9889 	queue_t		*ipwq, *dwq;
9890 	const char	*name;
9891 	struct qinit	*qinfo;
9892 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9893 	boolean_t	entered_ipsq = B_FALSE;
9894 
9895 	/*
9896 	 * Walk the lower stream to verify it's the IP module stream.
9897 	 * The IP module is identified by its name, wput function,
9898 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9899 	 * (li->l_qbot) will not vanish until this ioctl completes.
9900 	 */
9901 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9902 		qinfo = ipwq->q_qinfo;
9903 		name = qinfo->qi_minfo->mi_idname;
9904 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9905 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9906 			break;
9907 		}
9908 	}
9909 
9910 	/*
9911 	 * If this isn't an IP module stream, bail.
9912 	 */
9913 	if (ipwq == NULL)
9914 		return (0);
9915 
9916 	ill = ipwq->q_ptr;
9917 	ASSERT(ill != NULL);
9918 
9919 	if (ipsq == NULL) {
9920 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9921 		    NEW_OP, B_FALSE);
9922 		if (ipsq == NULL)
9923 			return (EINPROGRESS);
9924 		entered_ipsq = B_TRUE;
9925 	}
9926 	ASSERT(IAM_WRITER_ILL(ill));
9927 
9928 	if (doconsist) {
9929 		/*
9930 		 * Consistency checking requires that I_{P}LINK occurs
9931 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9932 		 * occurs prior to clearing ill_arp_muxid.
9933 		 */
9934 		if ((islink && ill->ill_ip_muxid != 0) ||
9935 		    (!islink && ill->ill_arp_muxid != 0)) {
9936 			err = EINVAL;
9937 			goto done;
9938 		}
9939 	}
9940 
9941 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9942 		goto done;
9943 
9944 	/*
9945 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9946 	 * the read queue of the module immediately below IP in the ill.
9947 	 * These are used during the capability negotiation below.
9948 	 */
9949 	ill->ill_lmod_rq = NULL;
9950 	ill->ill_lmod_cnt = 0;
9951 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9952 		ill->ill_lmod_rq = RD(dwq);
9953 		for (; dwq != NULL; dwq = dwq->q_next)
9954 			ill->ill_lmod_cnt++;
9955 	}
9956 
9957 	if (doconsist)
9958 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9959 
9960 	/*
9961 	 * Mark the ipsq busy until the capability operations initiated below
9962 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
9963 	 * returns, but the capability operation may complete asynchronously
9964 	 * much later.
9965 	 */
9966 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
9967 	/*
9968 	 * If there's at least one up ipif on this ill, then we're bound to
9969 	 * the underlying driver via DLPI.  In that case, renegotiate
9970 	 * capabilities to account for any possible change in modules
9971 	 * interposed between IP and the driver.
9972 	 */
9973 	if (ill->ill_ipif_up_count > 0) {
9974 		if (islink)
9975 			ill_capability_probe(ill);
9976 		else
9977 			ill_capability_reset(ill, B_FALSE);
9978 	}
9979 	ipsq_current_finish(ipsq);
9980 done:
9981 	if (entered_ipsq)
9982 		ipsq_exit(ipsq);
9983 
9984 	return (err);
9985 }
9986 
9987 /*
9988  * Search the ioctl command in the ioctl tables and return a pointer
9989  * to the ioctl command information. The ioctl command tables are
9990  * static and fully populated at compile time.
9991  */
9992 ip_ioctl_cmd_t *
9993 ip_sioctl_lookup(int ioc_cmd)
9994 {
9995 	int index;
9996 	ip_ioctl_cmd_t *ipip;
9997 	ip_ioctl_cmd_t *ipip_end;
9998 
9999 	if (ioc_cmd == IPI_DONTCARE)
10000 		return (NULL);
10001 
10002 	/*
10003 	 * Do a 2 step search. First search the indexed table
10004 	 * based on the least significant byte of the ioctl cmd.
10005 	 * If we don't find a match, then search the misc table
10006 	 * serially.
10007 	 */
10008 	index = ioc_cmd & 0xFF;
10009 	if (index < ip_ndx_ioctl_count) {
10010 		ipip = &ip_ndx_ioctl_table[index];
10011 		if (ipip->ipi_cmd == ioc_cmd) {
10012 			/* Found a match in the ndx table */
10013 			return (ipip);
10014 		}
10015 	}
10016 
10017 	/* Search the misc table */
10018 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10019 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10020 		if (ipip->ipi_cmd == ioc_cmd)
10021 			/* Found a match in the misc table */
10022 			return (ipip);
10023 	}
10024 
10025 	return (NULL);
10026 }
10027 
10028 /*
10029  * Wrapper function for resuming deferred ioctl processing
10030  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10031  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10032  */
10033 /* ARGSUSED */
10034 void
10035 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10036     void *dummy_arg)
10037 {
10038 	ip_sioctl_copyin_setup(q, mp);
10039 }
10040 
10041 /*
10042  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10043  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10044  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10045  * We establish here the size of the block to be copied in.  mi_copyin
10046  * arranges for this to happen, an processing continues in ip_wput with
10047  * an M_IOCDATA message.
10048  */
10049 void
10050 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10051 {
10052 	int	copyin_size;
10053 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10054 	ip_ioctl_cmd_t *ipip;
10055 	cred_t *cr;
10056 	ip_stack_t	*ipst;
10057 
10058 	if (CONN_Q(q))
10059 		ipst = CONNQ_TO_IPST(q);
10060 	else
10061 		ipst = ILLQ_TO_IPST(q);
10062 
10063 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10064 	if (ipip == NULL) {
10065 		/*
10066 		 * The ioctl is not one we understand or own.
10067 		 * Pass it along to be processed down stream,
10068 		 * if this is a module instance of IP, else nak
10069 		 * the ioctl.
10070 		 */
10071 		if (q->q_next == NULL) {
10072 			goto nak;
10073 		} else {
10074 			putnext(q, mp);
10075 			return;
10076 		}
10077 	}
10078 
10079 	/*
10080 	 * If this is deferred, then we will do all the checks when we
10081 	 * come back.
10082 	 */
10083 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10084 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10085 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10086 		return;
10087 	}
10088 
10089 	/*
10090 	 * Only allow a very small subset of IP ioctls on this stream if
10091 	 * IP is a module and not a driver. Allowing ioctls to be processed
10092 	 * in this case may cause assert failures or data corruption.
10093 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10094 	 * ioctls allowed on an IP module stream, after which this stream
10095 	 * normally becomes a multiplexor (at which time the stream head
10096 	 * will fail all ioctls).
10097 	 */
10098 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10099 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10100 			/*
10101 			 * Pass common Streams ioctls which the IP
10102 			 * module does not own or consume along to
10103 			 * be processed down stream.
10104 			 */
10105 			putnext(q, mp);
10106 			return;
10107 		} else {
10108 			goto nak;
10109 		}
10110 	}
10111 
10112 	/* Make sure we have ioctl data to process. */
10113 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10114 		goto nak;
10115 
10116 	/*
10117 	 * Prefer dblk credential over ioctl credential; some synthesized
10118 	 * ioctls have kcred set because there's no way to crhold()
10119 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10120 	 * the framework; the caller of ioctl needs to hold the reference
10121 	 * for the duration of the call).
10122 	 */
10123 	cr = msg_getcred(mp, NULL);
10124 	if (cr == NULL)
10125 		cr = iocp->ioc_cr;
10126 
10127 	/* Make sure normal users don't send down privileged ioctls */
10128 	if ((ipip->ipi_flags & IPI_PRIV) &&
10129 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10130 		/* We checked the privilege earlier but log it here */
10131 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10132 		return;
10133 	}
10134 
10135 	/*
10136 	 * The ioctl command tables can only encode fixed length
10137 	 * ioctl data. If the length is variable, the table will
10138 	 * encode the length as zero. Such special cases are handled
10139 	 * below in the switch.
10140 	 */
10141 	if (ipip->ipi_copyin_size != 0) {
10142 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10143 		return;
10144 	}
10145 
10146 	switch (iocp->ioc_cmd) {
10147 	case O_SIOCGIFCONF:
10148 	case SIOCGIFCONF:
10149 		/*
10150 		 * This IOCTL is hilarious.  See comments in
10151 		 * ip_sioctl_get_ifconf for the story.
10152 		 */
10153 		if (iocp->ioc_count == TRANSPARENT)
10154 			copyin_size = SIZEOF_STRUCT(ifconf,
10155 			    iocp->ioc_flag);
10156 		else
10157 			copyin_size = iocp->ioc_count;
10158 		mi_copyin(q, mp, NULL, copyin_size);
10159 		return;
10160 
10161 	case O_SIOCGLIFCONF:
10162 	case SIOCGLIFCONF:
10163 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10164 		mi_copyin(q, mp, NULL, copyin_size);
10165 		return;
10166 
10167 	case SIOCGLIFSRCOF:
10168 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10169 		mi_copyin(q, mp, NULL, copyin_size);
10170 		return;
10171 	case SIOCGIP6ADDRPOLICY:
10172 		ip_sioctl_ip6addrpolicy(q, mp);
10173 		ip6_asp_table_refrele(ipst);
10174 		return;
10175 
10176 	case SIOCSIP6ADDRPOLICY:
10177 		ip_sioctl_ip6addrpolicy(q, mp);
10178 		return;
10179 
10180 	case SIOCGDSTINFO:
10181 		ip_sioctl_dstinfo(q, mp);
10182 		ip6_asp_table_refrele(ipst);
10183 		return;
10184 
10185 	case I_PLINK:
10186 	case I_PUNLINK:
10187 	case I_LINK:
10188 	case I_UNLINK:
10189 		/*
10190 		 * We treat non-persistent link similarly as the persistent
10191 		 * link case, in terms of plumbing/unplumbing, as well as
10192 		 * dynamic re-plumbing events indicator.  See comments
10193 		 * in ip_sioctl_plink() for more.
10194 		 *
10195 		 * Request can be enqueued in the 'ipsq' while waiting
10196 		 * to become exclusive. So bump up the conn ref.
10197 		 */
10198 		if (CONN_Q(q))
10199 			CONN_INC_REF(Q_TO_CONN(q));
10200 		ip_sioctl_plink(NULL, q, mp, NULL);
10201 		return;
10202 
10203 	case ND_GET:
10204 	case ND_SET:
10205 		/*
10206 		 * Use of the nd table requires holding the reader lock.
10207 		 * Modifying the nd table thru nd_load/nd_unload requires
10208 		 * the writer lock.
10209 		 */
10210 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10211 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10212 			rw_exit(&ipst->ips_ip_g_nd_lock);
10213 
10214 			if (iocp->ioc_error)
10215 				iocp->ioc_count = 0;
10216 			mp->b_datap->db_type = M_IOCACK;
10217 			qreply(q, mp);
10218 			return;
10219 		}
10220 		rw_exit(&ipst->ips_ip_g_nd_lock);
10221 		/*
10222 		 * We don't understand this subioctl of ND_GET / ND_SET.
10223 		 * Maybe intended for some driver / module below us
10224 		 */
10225 		if (q->q_next) {
10226 			putnext(q, mp);
10227 		} else {
10228 			iocp->ioc_error = ENOENT;
10229 			mp->b_datap->db_type = M_IOCNAK;
10230 			iocp->ioc_count = 0;
10231 			qreply(q, mp);
10232 		}
10233 		return;
10234 
10235 	case IP_IOCTL:
10236 		ip_wput_ioctl(q, mp);
10237 		return;
10238 	default:
10239 		cmn_err(CE_PANIC, "should not happen ");
10240 	}
10241 nak:
10242 	if (mp->b_cont != NULL) {
10243 		freemsg(mp->b_cont);
10244 		mp->b_cont = NULL;
10245 	}
10246 	iocp->ioc_error = EINVAL;
10247 	mp->b_datap->db_type = M_IOCNAK;
10248 	iocp->ioc_count = 0;
10249 	qreply(q, mp);
10250 }
10251 
10252 /* ip_wput hands off ARP IOCTL responses to us */
10253 /* ARGSUSED3 */
10254 void
10255 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10256 {
10257 	struct arpreq *ar;
10258 	struct xarpreq *xar;
10259 	area_t	*area;
10260 	mblk_t	*area_mp;
10261 	struct iocblk *iocp;
10262 	mblk_t	*orig_ioc_mp, *tmp;
10263 	struct iocblk	*orig_iocp;
10264 	ill_t *ill;
10265 	conn_t *connp = NULL;
10266 	mblk_t *pending_mp;
10267 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10268 	int *flagsp;
10269 	char *storage = NULL;
10270 	sin_t *sin;
10271 	ipaddr_t addr;
10272 	int err;
10273 	ip_stack_t *ipst;
10274 
10275 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10276 	ill = q->q_ptr;
10277 	ASSERT(ill != NULL);
10278 	ipst = ill->ill_ipst;
10279 
10280 	/*
10281 	 * We should get back from ARP a packet chain that looks like:
10282 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10283 	 */
10284 	if (!(area_mp = mp->b_cont) ||
10285 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10286 	    !(orig_ioc_mp = area_mp->b_cont) ||
10287 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10288 		freemsg(mp);
10289 		return;
10290 	}
10291 
10292 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10293 
10294 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10295 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10296 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10297 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10298 		x_arp_ioctl = B_TRUE;
10299 		xar = (struct xarpreq *)tmp->b_rptr;
10300 		sin = (sin_t *)&xar->xarp_pa;
10301 		flagsp = &xar->xarp_flags;
10302 		storage = xar->xarp_ha.sdl_data;
10303 		if (xar->xarp_ha.sdl_nlen != 0)
10304 			ifx_arp_ioctl = B_TRUE;
10305 	} else {
10306 		ar = (struct arpreq *)tmp->b_rptr;
10307 		sin = (sin_t *)&ar->arp_pa;
10308 		flagsp = &ar->arp_flags;
10309 		storage = ar->arp_ha.sa_data;
10310 	}
10311 
10312 	iocp = (struct iocblk *)mp->b_rptr;
10313 
10314 	/*
10315 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10316 	 * Otherwise, we can find it from our ioc_id.
10317 	 */
10318 	if (ipsq != NULL)
10319 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10320 	else
10321 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10322 
10323 	if (pending_mp == NULL) {
10324 		ASSERT(connp == NULL);
10325 		inet_freemsg(mp);
10326 		return;
10327 	}
10328 	ASSERT(connp != NULL);
10329 	q = CONNP_TO_WQ(connp);
10330 
10331 	/* Uncouple the internally generated IOCTL from the original one */
10332 	area = (area_t *)area_mp->b_rptr;
10333 	area_mp->b_cont = NULL;
10334 
10335 	/*
10336 	 * Restore the b_next and b_prev used by mi code. This is needed
10337 	 * to complete the ioctl using mi* functions. We stored them in
10338 	 * the pending mp prior to sending the request to ARP.
10339 	 */
10340 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10341 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10342 	inet_freemsg(pending_mp);
10343 
10344 	/*
10345 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10346 	 * Catch the case where there is an IRE_CACHE by no entry in the
10347 	 * arp table.
10348 	 */
10349 	addr = sin->sin_addr.s_addr;
10350 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10351 		ire_t			*ire;
10352 		dl_unitdata_req_t	*dlup;
10353 		mblk_t			*llmp;
10354 		int			addr_len;
10355 		ill_t			*ipsqill = NULL;
10356 
10357 		if (ifx_arp_ioctl) {
10358 			/*
10359 			 * There's no need to lookup the ill, since
10360 			 * we've already done that when we started
10361 			 * processing the ioctl and sent the message
10362 			 * to ARP on that ill.  So use the ill that
10363 			 * is stored in q->q_ptr.
10364 			 */
10365 			ipsqill = ill;
10366 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10367 			    ipsqill->ill_ipif, ALL_ZONES,
10368 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10369 		} else {
10370 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10371 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10372 			if (ire != NULL)
10373 				ipsqill = ire_to_ill(ire);
10374 		}
10375 
10376 		if ((x_arp_ioctl) && (ipsqill != NULL))
10377 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10378 
10379 		if (ire != NULL) {
10380 			/*
10381 			 * Since the ire obtained from cachetable is used for
10382 			 * mac addr copying below, treat an incomplete ire as if
10383 			 * as if we never found it.
10384 			 */
10385 			if (ire->ire_nce != NULL &&
10386 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10387 				ire_refrele(ire);
10388 				ire = NULL;
10389 				ipsqill = NULL;
10390 				goto errack;
10391 			}
10392 			*flagsp = ATF_INUSE;
10393 			llmp = (ire->ire_nce != NULL ?
10394 			    ire->ire_nce->nce_res_mp : NULL);
10395 			if (llmp != NULL && ipsqill != NULL) {
10396 				uchar_t *macaddr;
10397 
10398 				addr_len = ipsqill->ill_phys_addr_length;
10399 				if (x_arp_ioctl && ((addr_len +
10400 				    ipsqill->ill_name_length) >
10401 				    sizeof (xar->xarp_ha.sdl_data))) {
10402 					ire_refrele(ire);
10403 					freemsg(mp);
10404 					ip_ioctl_finish(q, orig_ioc_mp,
10405 					    EINVAL, NO_COPYOUT, ipsq);
10406 					return;
10407 				}
10408 				*flagsp |= ATF_COM;
10409 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10410 				if (ipsqill->ill_sap_length < 0)
10411 					macaddr = llmp->b_rptr +
10412 					    dlup->dl_dest_addr_offset;
10413 				else
10414 					macaddr = llmp->b_rptr +
10415 					    dlup->dl_dest_addr_offset +
10416 					    ipsqill->ill_sap_length;
10417 				/*
10418 				 * For SIOCGARP, MAC address length
10419 				 * validation has already been done
10420 				 * before the ioctl was issued to ARP to
10421 				 * allow it to progress only on 6 byte
10422 				 * addressable (ethernet like) media. Thus
10423 				 * the mac address copying can not overwrite
10424 				 * the sa_data area below.
10425 				 */
10426 				bcopy(macaddr, storage, addr_len);
10427 			}
10428 			/* Ditch the internal IOCTL. */
10429 			freemsg(mp);
10430 			ire_refrele(ire);
10431 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10432 			return;
10433 		}
10434 	}
10435 
10436 	/*
10437 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10438 	 * on the IPMP meta-interface, ensure any ARP entries added in
10439 	 * ip_sioctl_arp() are deleted.
10440 	 */
10441 	if (IS_IPMP(ill) &&
10442 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10443 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10444 		ipmp_illgrp_t *illg = ill->ill_grp;
10445 		ipmp_arpent_t *entp;
10446 
10447 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10448 			ipmp_illgrp_destroy_arpent(illg, entp);
10449 	}
10450 
10451 	/*
10452 	 * Delete the coresponding IRE_CACHE if any.
10453 	 * Reset the error if there was one (in case there was no entry
10454 	 * in arp.)
10455 	 */
10456 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10457 		ipif_t *ipintf = NULL;
10458 
10459 		if (ifx_arp_ioctl) {
10460 			/*
10461 			 * There's no need to lookup the ill, since
10462 			 * we've already done that when we started
10463 			 * processing the ioctl and sent the message
10464 			 * to ARP on that ill.  So use the ill that
10465 			 * is stored in q->q_ptr.
10466 			 */
10467 			ipintf = ill->ill_ipif;
10468 		}
10469 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10470 			/*
10471 			 * The address in "addr" may be an entry for a
10472 			 * router. If that's true, then any off-net
10473 			 * IRE_CACHE entries that go through the router
10474 			 * with address "addr" must be clobbered. Use
10475 			 * ire_walk to achieve this goal.
10476 			 */
10477 			if (ifx_arp_ioctl)
10478 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10479 				    ire_delete_cache_gw, (char *)&addr, ill);
10480 			else
10481 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10482 				    ALL_ZONES, ipst);
10483 			iocp->ioc_error = 0;
10484 		}
10485 	}
10486 errack:
10487 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10488 		err = iocp->ioc_error;
10489 		freemsg(mp);
10490 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10491 		return;
10492 	}
10493 
10494 	/*
10495 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10496 	 * the area_t into the struct {x}arpreq.
10497 	 */
10498 	if (x_arp_ioctl) {
10499 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10500 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10501 		    sizeof (xar->xarp_ha.sdl_data)) {
10502 			freemsg(mp);
10503 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10504 			    ipsq);
10505 			return;
10506 		}
10507 	}
10508 	*flagsp = ATF_INUSE;
10509 	if (area->area_flags & ACE_F_PERMANENT)
10510 		*flagsp |= ATF_PERM;
10511 	if (area->area_flags & ACE_F_PUBLISH)
10512 		*flagsp |= ATF_PUBL;
10513 	if (area->area_flags & ACE_F_AUTHORITY)
10514 		*flagsp |= ATF_AUTHORITY;
10515 	if (area->area_hw_addr_length != 0) {
10516 		*flagsp |= ATF_COM;
10517 		/*
10518 		 * For SIOCGARP, MAC address length validation has
10519 		 * already been done before the ioctl was issued to ARP
10520 		 * to allow it to progress only on 6 byte addressable
10521 		 * (ethernet like) media. Thus the mac address copying
10522 		 * can not overwrite the sa_data area below.
10523 		 */
10524 		bcopy((char *)area + area->area_hw_addr_offset,
10525 		    storage, area->area_hw_addr_length);
10526 	}
10527 
10528 	/* Ditch the internal IOCTL. */
10529 	freemsg(mp);
10530 	/* Complete the original. */
10531 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10532 }
10533 
10534 /*
10535  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10536  * interface) create the next available logical interface for this
10537  * physical interface.
10538  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10539  * ipif with the specified name.
10540  *
10541  * If the address family is not AF_UNSPEC then set the address as well.
10542  *
10543  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10544  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10545  *
10546  * Executed as a writer on the ill.
10547  * So no lock is needed to traverse the ipif chain, or examine the
10548  * phyint flags.
10549  */
10550 /* ARGSUSED */
10551 int
10552 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10553     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10554 {
10555 	mblk_t	*mp1;
10556 	struct lifreq *lifr;
10557 	boolean_t	isv6;
10558 	boolean_t	exists;
10559 	char 	*name;
10560 	char	*endp;
10561 	char	*cp;
10562 	int	namelen;
10563 	ipif_t	*ipif;
10564 	long	id;
10565 	ipsq_t	*ipsq;
10566 	ill_t	*ill;
10567 	sin_t	*sin;
10568 	int	err = 0;
10569 	boolean_t found_sep = B_FALSE;
10570 	conn_t	*connp;
10571 	zoneid_t zoneid;
10572 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10573 
10574 	ASSERT(q->q_next == NULL);
10575 	ip1dbg(("ip_sioctl_addif\n"));
10576 	/* Existence of mp1 has been checked in ip_wput_nondata */
10577 	mp1 = mp->b_cont->b_cont;
10578 	/*
10579 	 * Null terminate the string to protect against buffer
10580 	 * overrun. String was generated by user code and may not
10581 	 * be trusted.
10582 	 */
10583 	lifr = (struct lifreq *)mp1->b_rptr;
10584 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10585 	name = lifr->lifr_name;
10586 	ASSERT(CONN_Q(q));
10587 	connp = Q_TO_CONN(q);
10588 	isv6 = connp->conn_af_isv6;
10589 	zoneid = connp->conn_zoneid;
10590 	namelen = mi_strlen(name);
10591 	if (namelen == 0)
10592 		return (EINVAL);
10593 
10594 	exists = B_FALSE;
10595 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10596 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10597 		/*
10598 		 * Allow creating lo0 using SIOCLIFADDIF.
10599 		 * can't be any other writer thread. So can pass null below
10600 		 * for the last 4 args to ipif_lookup_name.
10601 		 */
10602 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10603 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10604 		/* Prevent any further action */
10605 		if (ipif == NULL) {
10606 			return (ENOBUFS);
10607 		} else if (!exists) {
10608 			/* We created the ipif now and as writer */
10609 			ipif_refrele(ipif);
10610 			return (0);
10611 		} else {
10612 			ill = ipif->ipif_ill;
10613 			ill_refhold(ill);
10614 			ipif_refrele(ipif);
10615 		}
10616 	} else {
10617 		/* Look for a colon in the name. */
10618 		endp = &name[namelen];
10619 		for (cp = endp; --cp > name; ) {
10620 			if (*cp == IPIF_SEPARATOR_CHAR) {
10621 				found_sep = B_TRUE;
10622 				/*
10623 				 * Reject any non-decimal aliases for plumbing
10624 				 * of logical interfaces. Aliases with leading
10625 				 * zeroes are also rejected as they introduce
10626 				 * ambiguity in the naming of the interfaces.
10627 				 * Comparing with "0" takes care of all such
10628 				 * cases.
10629 				 */
10630 				if ((strncmp("0", cp+1, 1)) == 0)
10631 					return (EINVAL);
10632 
10633 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10634 				    id <= 0 || *endp != '\0') {
10635 					return (EINVAL);
10636 				}
10637 				*cp = '\0';
10638 				break;
10639 			}
10640 		}
10641 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10642 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10643 		if (found_sep)
10644 			*cp = IPIF_SEPARATOR_CHAR;
10645 		if (ill == NULL)
10646 			return (err);
10647 	}
10648 
10649 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10650 	    B_TRUE);
10651 
10652 	/*
10653 	 * Release the refhold due to the lookup, now that we are excl
10654 	 * or we are just returning
10655 	 */
10656 	ill_refrele(ill);
10657 
10658 	if (ipsq == NULL)
10659 		return (EINPROGRESS);
10660 
10661 	/* We are now exclusive on the IPSQ */
10662 	ASSERT(IAM_WRITER_ILL(ill));
10663 
10664 	if (found_sep) {
10665 		/* Now see if there is an IPIF with this unit number. */
10666 		for (ipif = ill->ill_ipif; ipif != NULL;
10667 		    ipif = ipif->ipif_next) {
10668 			if (ipif->ipif_id == id) {
10669 				err = EEXIST;
10670 				goto done;
10671 			}
10672 		}
10673 	}
10674 
10675 	/*
10676 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10677 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10678 	 * instead.
10679 	 */
10680 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10681 	    B_TRUE, B_TRUE)) == NULL) {
10682 		err = ENOBUFS;
10683 		goto done;
10684 	}
10685 
10686 	/* Return created name with ioctl */
10687 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10688 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10689 	ip1dbg(("created %s\n", lifr->lifr_name));
10690 
10691 	/* Set address */
10692 	sin = (sin_t *)&lifr->lifr_addr;
10693 	if (sin->sin_family != AF_UNSPEC) {
10694 		err = ip_sioctl_addr(ipif, sin, q, mp,
10695 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10696 	}
10697 
10698 done:
10699 	ipsq_exit(ipsq);
10700 	return (err);
10701 }
10702 
10703 /*
10704  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10705  * interface) delete it based on the IP address (on this physical interface).
10706  * Otherwise delete it based on the ipif_id.
10707  * Also, special handling to allow a removeif of lo0.
10708  */
10709 /* ARGSUSED */
10710 int
10711 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10712     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10713 {
10714 	conn_t		*connp;
10715 	ill_t		*ill = ipif->ipif_ill;
10716 	boolean_t	 success;
10717 	ip_stack_t	*ipst;
10718 
10719 	ipst = CONNQ_TO_IPST(q);
10720 
10721 	ASSERT(q->q_next == NULL);
10722 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10723 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10724 	ASSERT(IAM_WRITER_IPIF(ipif));
10725 
10726 	connp = Q_TO_CONN(q);
10727 	/*
10728 	 * Special case for unplumbing lo0 (the loopback physical interface).
10729 	 * If unplumbing lo0, the incoming address structure has been
10730 	 * initialized to all zeros. When unplumbing lo0, all its logical
10731 	 * interfaces must be removed too.
10732 	 *
10733 	 * Note that this interface may be called to remove a specific
10734 	 * loopback logical interface (eg, lo0:1). But in that case
10735 	 * ipif->ipif_id != 0 so that the code path for that case is the
10736 	 * same as any other interface (meaning it skips the code directly
10737 	 * below).
10738 	 */
10739 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10740 		if (sin->sin_family == AF_UNSPEC &&
10741 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10742 			/*
10743 			 * Mark it condemned. No new ref. will be made to ill.
10744 			 */
10745 			mutex_enter(&ill->ill_lock);
10746 			ill->ill_state_flags |= ILL_CONDEMNED;
10747 			for (ipif = ill->ill_ipif; ipif != NULL;
10748 			    ipif = ipif->ipif_next) {
10749 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10750 			}
10751 			mutex_exit(&ill->ill_lock);
10752 
10753 			ipif = ill->ill_ipif;
10754 			/* unplumb the loopback interface */
10755 			ill_delete(ill);
10756 			mutex_enter(&connp->conn_lock);
10757 			mutex_enter(&ill->ill_lock);
10758 
10759 			/* Are any references to this ill active */
10760 			if (ill_is_freeable(ill)) {
10761 				mutex_exit(&ill->ill_lock);
10762 				mutex_exit(&connp->conn_lock);
10763 				ill_delete_tail(ill);
10764 				mi_free(ill);
10765 				return (0);
10766 			}
10767 			success = ipsq_pending_mp_add(connp, ipif,
10768 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10769 			mutex_exit(&connp->conn_lock);
10770 			mutex_exit(&ill->ill_lock);
10771 			if (success)
10772 				return (EINPROGRESS);
10773 			else
10774 				return (EINTR);
10775 		}
10776 	}
10777 
10778 	if (ipif->ipif_id == 0) {
10779 		ipsq_t *ipsq;
10780 
10781 		/* Find based on address */
10782 		if (ipif->ipif_isv6) {
10783 			sin6_t *sin6;
10784 
10785 			if (sin->sin_family != AF_INET6)
10786 				return (EAFNOSUPPORT);
10787 
10788 			sin6 = (sin6_t *)sin;
10789 			/* We are a writer, so we should be able to lookup */
10790 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10791 			    ipst);
10792 		} else {
10793 			if (sin->sin_family != AF_INET)
10794 				return (EAFNOSUPPORT);
10795 
10796 			/* We are a writer, so we should be able to lookup */
10797 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10798 			    ipst);
10799 		}
10800 		if (ipif == NULL) {
10801 			return (EADDRNOTAVAIL);
10802 		}
10803 
10804 		/*
10805 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10806 		 * lifr_name of the physical interface but with an ip address
10807 		 * lifr_addr of a logical interface plumbed over it.
10808 		 * So update ipx_current_ipif now that ipif points to the
10809 		 * correct one.
10810 		 */
10811 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10812 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10813 
10814 		/* This is a writer */
10815 		ipif_refrele(ipif);
10816 	}
10817 
10818 	/*
10819 	 * Can not delete instance zero since it is tied to the ill.
10820 	 */
10821 	if (ipif->ipif_id == 0)
10822 		return (EBUSY);
10823 
10824 	mutex_enter(&ill->ill_lock);
10825 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10826 	mutex_exit(&ill->ill_lock);
10827 
10828 	ipif_free(ipif);
10829 
10830 	mutex_enter(&connp->conn_lock);
10831 	mutex_enter(&ill->ill_lock);
10832 
10833 	/* Are any references to this ipif active */
10834 	if (ipif_is_freeable(ipif)) {
10835 		mutex_exit(&ill->ill_lock);
10836 		mutex_exit(&connp->conn_lock);
10837 		ipif_non_duplicate(ipif);
10838 		ipif_down_tail(ipif);
10839 		ipif_free_tail(ipif); /* frees ipif */
10840 		return (0);
10841 	}
10842 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10843 	    IPIF_FREE);
10844 	mutex_exit(&ill->ill_lock);
10845 	mutex_exit(&connp->conn_lock);
10846 	if (success)
10847 		return (EINPROGRESS);
10848 	else
10849 		return (EINTR);
10850 }
10851 
10852 /*
10853  * Restart the removeif ioctl. The refcnt has gone down to 0.
10854  * The ipif is already condemned. So can't find it thru lookups.
10855  */
10856 /* ARGSUSED */
10857 int
10858 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10859     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10860 {
10861 	ill_t *ill = ipif->ipif_ill;
10862 
10863 	ASSERT(IAM_WRITER_IPIF(ipif));
10864 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10865 
10866 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10867 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10868 
10869 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10870 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10871 		ill_delete_tail(ill);
10872 		mi_free(ill);
10873 		return (0);
10874 	}
10875 
10876 	ipif_non_duplicate(ipif);
10877 	ipif_down_tail(ipif);
10878 	ipif_free_tail(ipif);
10879 
10880 	ILL_UNMARK_CHANGING(ill);
10881 	return (0);
10882 }
10883 
10884 /*
10885  * Set the local interface address.
10886  * Allow an address of all zero when the interface is down.
10887  */
10888 /* ARGSUSED */
10889 int
10890 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10891     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10892 {
10893 	int err = 0;
10894 	in6_addr_t v6addr;
10895 	boolean_t need_up = B_FALSE;
10896 
10897 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10898 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10899 
10900 	ASSERT(IAM_WRITER_IPIF(ipif));
10901 
10902 	if (ipif->ipif_isv6) {
10903 		sin6_t *sin6;
10904 		ill_t *ill;
10905 		phyint_t *phyi;
10906 
10907 		if (sin->sin_family != AF_INET6)
10908 			return (EAFNOSUPPORT);
10909 
10910 		sin6 = (sin6_t *)sin;
10911 		v6addr = sin6->sin6_addr;
10912 		ill = ipif->ipif_ill;
10913 		phyi = ill->ill_phyint;
10914 
10915 		/*
10916 		 * Enforce that true multicast interfaces have a link-local
10917 		 * address for logical unit 0.
10918 		 */
10919 		if (ipif->ipif_id == 0 &&
10920 		    (ill->ill_flags & ILLF_MULTICAST) &&
10921 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10922 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10923 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10924 			return (EADDRNOTAVAIL);
10925 		}
10926 
10927 		/*
10928 		 * up interfaces shouldn't have the unspecified address
10929 		 * unless they also have the IPIF_NOLOCAL flags set and
10930 		 * have a subnet assigned.
10931 		 */
10932 		if ((ipif->ipif_flags & IPIF_UP) &&
10933 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10934 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10935 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10936 			return (EADDRNOTAVAIL);
10937 		}
10938 
10939 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10940 			return (EADDRNOTAVAIL);
10941 	} else {
10942 		ipaddr_t addr;
10943 
10944 		if (sin->sin_family != AF_INET)
10945 			return (EAFNOSUPPORT);
10946 
10947 		addr = sin->sin_addr.s_addr;
10948 
10949 		/* Allow 0 as the local address. */
10950 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10951 			return (EADDRNOTAVAIL);
10952 
10953 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10954 	}
10955 
10956 	/*
10957 	 * Even if there is no change we redo things just to rerun
10958 	 * ipif_set_default.
10959 	 */
10960 	if (ipif->ipif_flags & IPIF_UP) {
10961 		/*
10962 		 * Setting a new local address, make sure
10963 		 * we have net and subnet bcast ire's for
10964 		 * the old address if we need them.
10965 		 */
10966 		if (!ipif->ipif_isv6)
10967 			ipif_check_bcast_ires(ipif);
10968 		/*
10969 		 * If the interface is already marked up,
10970 		 * we call ipif_down which will take care
10971 		 * of ditching any IREs that have been set
10972 		 * up based on the old interface address.
10973 		 */
10974 		err = ipif_logical_down(ipif, q, mp);
10975 		if (err == EINPROGRESS)
10976 			return (err);
10977 		ipif_down_tail(ipif);
10978 		need_up = 1;
10979 	}
10980 
10981 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10982 	return (err);
10983 }
10984 
10985 int
10986 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10987     boolean_t need_up)
10988 {
10989 	in6_addr_t v6addr;
10990 	in6_addr_t ov6addr;
10991 	ipaddr_t addr;
10992 	sin6_t	*sin6;
10993 	int	sinlen;
10994 	int	err = 0;
10995 	ill_t	*ill = ipif->ipif_ill;
10996 	boolean_t need_dl_down;
10997 	boolean_t need_arp_down;
10998 	struct iocblk *iocp;
10999 
11000 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11001 
11002 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11003 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11004 	ASSERT(IAM_WRITER_IPIF(ipif));
11005 
11006 	/* Must cancel any pending timer before taking the ill_lock */
11007 	if (ipif->ipif_recovery_id != 0)
11008 		(void) untimeout(ipif->ipif_recovery_id);
11009 	ipif->ipif_recovery_id = 0;
11010 
11011 	if (ipif->ipif_isv6) {
11012 		sin6 = (sin6_t *)sin;
11013 		v6addr = sin6->sin6_addr;
11014 		sinlen = sizeof (struct sockaddr_in6);
11015 	} else {
11016 		addr = sin->sin_addr.s_addr;
11017 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11018 		sinlen = sizeof (struct sockaddr_in);
11019 	}
11020 	mutex_enter(&ill->ill_lock);
11021 	ov6addr = ipif->ipif_v6lcl_addr;
11022 	ipif->ipif_v6lcl_addr = v6addr;
11023 	sctp_update_ipif_addr(ipif, ov6addr);
11024 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11025 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11026 	} else {
11027 		ipif->ipif_v6src_addr = v6addr;
11028 	}
11029 	ipif->ipif_addr_ready = 0;
11030 
11031 	/*
11032 	 * If the interface was previously marked as a duplicate, then since
11033 	 * we've now got a "new" address, it should no longer be considered a
11034 	 * duplicate -- even if the "new" address is the same as the old one.
11035 	 * Note that if all ipifs are down, we may have a pending ARP down
11036 	 * event to handle.  This is because we want to recover from duplicates
11037 	 * and thus delay tearing down ARP until the duplicates have been
11038 	 * removed or disabled.
11039 	 */
11040 	need_dl_down = need_arp_down = B_FALSE;
11041 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11042 		need_arp_down = !need_up;
11043 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11044 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11045 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11046 			need_dl_down = B_TRUE;
11047 		}
11048 	}
11049 
11050 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11051 	    !ill->ill_is_6to4tun) {
11052 		queue_t *wqp = ill->ill_wq;
11053 
11054 		/*
11055 		 * The local address of this interface is a 6to4 address,
11056 		 * check if this interface is in fact a 6to4 tunnel or just
11057 		 * an interface configured with a 6to4 address.  We are only
11058 		 * interested in the former.
11059 		 */
11060 		if (wqp != NULL) {
11061 			while ((wqp->q_next != NULL) &&
11062 			    (wqp->q_next->q_qinfo != NULL) &&
11063 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11064 
11065 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11066 				    == TUN6TO4_MODID) {
11067 					/* set for use in IP */
11068 					ill->ill_is_6to4tun = 1;
11069 					break;
11070 				}
11071 				wqp = wqp->q_next;
11072 			}
11073 		}
11074 	}
11075 
11076 	ipif_set_default(ipif);
11077 
11078 	/*
11079 	 * When publishing an interface address change event, we only notify
11080 	 * the event listeners of the new address.  It is assumed that if they
11081 	 * actively care about the addresses assigned that they will have
11082 	 * already discovered the previous address assigned (if there was one.)
11083 	 *
11084 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11085 	 */
11086 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11087 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11088 		    NE_ADDRESS_CHANGE, sin, sinlen);
11089 	}
11090 
11091 	mutex_exit(&ill->ill_lock);
11092 
11093 	if (need_up) {
11094 		/*
11095 		 * Now bring the interface back up.  If this
11096 		 * is the only IPIF for the ILL, ipif_up
11097 		 * will have to re-bind to the device, so
11098 		 * we may get back EINPROGRESS, in which
11099 		 * case, this IOCTL will get completed in
11100 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11101 		 */
11102 		err = ipif_up(ipif, q, mp);
11103 	}
11104 
11105 	if (need_dl_down)
11106 		ill_dl_down(ill);
11107 	if (need_arp_down)
11108 		ipif_resolver_down(ipif);
11109 
11110 	return (err);
11111 }
11112 
11113 /*
11114  * Restart entry point to restart the address set operation after the
11115  * refcounts have dropped to zero.
11116  */
11117 /* ARGSUSED */
11118 int
11119 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11120     ip_ioctl_cmd_t *ipip, void *ifreq)
11121 {
11122 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11123 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11124 	ASSERT(IAM_WRITER_IPIF(ipif));
11125 	ipif_down_tail(ipif);
11126 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11127 }
11128 
11129 /* ARGSUSED */
11130 int
11131 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11132     ip_ioctl_cmd_t *ipip, void *if_req)
11133 {
11134 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11135 	struct lifreq *lifr = (struct lifreq *)if_req;
11136 
11137 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11138 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11139 	/*
11140 	 * The net mask and address can't change since we have a
11141 	 * reference to the ipif. So no lock is necessary.
11142 	 */
11143 	if (ipif->ipif_isv6) {
11144 		*sin6 = sin6_null;
11145 		sin6->sin6_family = AF_INET6;
11146 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11147 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11148 		lifr->lifr_addrlen =
11149 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11150 	} else {
11151 		*sin = sin_null;
11152 		sin->sin_family = AF_INET;
11153 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11154 		if (ipip->ipi_cmd_type == LIF_CMD) {
11155 			lifr->lifr_addrlen =
11156 			    ip_mask_to_plen(ipif->ipif_net_mask);
11157 		}
11158 	}
11159 	return (0);
11160 }
11161 
11162 /*
11163  * Set the destination address for a pt-pt interface.
11164  */
11165 /* ARGSUSED */
11166 int
11167 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11168     ip_ioctl_cmd_t *ipip, void *if_req)
11169 {
11170 	int err = 0;
11171 	in6_addr_t v6addr;
11172 	boolean_t need_up = B_FALSE;
11173 
11174 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11175 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11176 	ASSERT(IAM_WRITER_IPIF(ipif));
11177 
11178 	if (ipif->ipif_isv6) {
11179 		sin6_t *sin6;
11180 
11181 		if (sin->sin_family != AF_INET6)
11182 			return (EAFNOSUPPORT);
11183 
11184 		sin6 = (sin6_t *)sin;
11185 		v6addr = sin6->sin6_addr;
11186 
11187 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11188 			return (EADDRNOTAVAIL);
11189 	} else {
11190 		ipaddr_t addr;
11191 
11192 		if (sin->sin_family != AF_INET)
11193 			return (EAFNOSUPPORT);
11194 
11195 		addr = sin->sin_addr.s_addr;
11196 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11197 			return (EADDRNOTAVAIL);
11198 
11199 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11200 	}
11201 
11202 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11203 		return (0);	/* No change */
11204 
11205 	if (ipif->ipif_flags & IPIF_UP) {
11206 		/*
11207 		 * If the interface is already marked up,
11208 		 * we call ipif_down which will take care
11209 		 * of ditching any IREs that have been set
11210 		 * up based on the old pp dst address.
11211 		 */
11212 		err = ipif_logical_down(ipif, q, mp);
11213 		if (err == EINPROGRESS)
11214 			return (err);
11215 		ipif_down_tail(ipif);
11216 		need_up = B_TRUE;
11217 	}
11218 	/*
11219 	 * could return EINPROGRESS. If so ioctl will complete in
11220 	 * ip_rput_dlpi_writer
11221 	 */
11222 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11223 	return (err);
11224 }
11225 
11226 static int
11227 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11228     boolean_t need_up)
11229 {
11230 	in6_addr_t v6addr;
11231 	ill_t	*ill = ipif->ipif_ill;
11232 	int	err = 0;
11233 	boolean_t need_dl_down;
11234 	boolean_t need_arp_down;
11235 
11236 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11237 	    ipif->ipif_id, (void *)ipif));
11238 
11239 	/* Must cancel any pending timer before taking the ill_lock */
11240 	if (ipif->ipif_recovery_id != 0)
11241 		(void) untimeout(ipif->ipif_recovery_id);
11242 	ipif->ipif_recovery_id = 0;
11243 
11244 	if (ipif->ipif_isv6) {
11245 		sin6_t *sin6;
11246 
11247 		sin6 = (sin6_t *)sin;
11248 		v6addr = sin6->sin6_addr;
11249 	} else {
11250 		ipaddr_t addr;
11251 
11252 		addr = sin->sin_addr.s_addr;
11253 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11254 	}
11255 	mutex_enter(&ill->ill_lock);
11256 	/* Set point to point destination address. */
11257 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11258 		/*
11259 		 * Allow this as a means of creating logical
11260 		 * pt-pt interfaces on top of e.g. an Ethernet.
11261 		 * XXX Undocumented HACK for testing.
11262 		 * pt-pt interfaces are created with NUD disabled.
11263 		 */
11264 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11265 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11266 		if (ipif->ipif_isv6)
11267 			ill->ill_flags |= ILLF_NONUD;
11268 	}
11269 
11270 	/*
11271 	 * If the interface was previously marked as a duplicate, then since
11272 	 * we've now got a "new" address, it should no longer be considered a
11273 	 * duplicate -- even if the "new" address is the same as the old one.
11274 	 * Note that if all ipifs are down, we may have a pending ARP down
11275 	 * event to handle.
11276 	 */
11277 	need_dl_down = need_arp_down = B_FALSE;
11278 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11279 		need_arp_down = !need_up;
11280 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11281 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11282 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11283 			need_dl_down = B_TRUE;
11284 		}
11285 	}
11286 
11287 	/* Set the new address. */
11288 	ipif->ipif_v6pp_dst_addr = v6addr;
11289 	/* Make sure subnet tracks pp_dst */
11290 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11291 	mutex_exit(&ill->ill_lock);
11292 
11293 	if (need_up) {
11294 		/*
11295 		 * Now bring the interface back up.  If this
11296 		 * is the only IPIF for the ILL, ipif_up
11297 		 * will have to re-bind to the device, so
11298 		 * we may get back EINPROGRESS, in which
11299 		 * case, this IOCTL will get completed in
11300 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11301 		 */
11302 		err = ipif_up(ipif, q, mp);
11303 	}
11304 
11305 	if (need_dl_down)
11306 		ill_dl_down(ill);
11307 	if (need_arp_down)
11308 		ipif_resolver_down(ipif);
11309 
11310 	return (err);
11311 }
11312 
11313 /*
11314  * Restart entry point to restart the dstaddress set operation after the
11315  * refcounts have dropped to zero.
11316  */
11317 /* ARGSUSED */
11318 int
11319 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11320     ip_ioctl_cmd_t *ipip, void *ifreq)
11321 {
11322 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11323 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11324 	ipif_down_tail(ipif);
11325 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11326 }
11327 
11328 /* ARGSUSED */
11329 int
11330 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11331     ip_ioctl_cmd_t *ipip, void *if_req)
11332 {
11333 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11334 
11335 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11336 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11337 	/*
11338 	 * Get point to point destination address. The addresses can't
11339 	 * change since we hold a reference to the ipif.
11340 	 */
11341 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11342 		return (EADDRNOTAVAIL);
11343 
11344 	if (ipif->ipif_isv6) {
11345 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11346 		*sin6 = sin6_null;
11347 		sin6->sin6_family = AF_INET6;
11348 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11349 	} else {
11350 		*sin = sin_null;
11351 		sin->sin_family = AF_INET;
11352 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11353 	}
11354 	return (0);
11355 }
11356 
11357 /*
11358  * Set interface flags.  Many flags require special handling (e.g.,
11359  * bringing the interface down); see below for details.
11360  *
11361  * NOTE : We really don't enforce that ipif_id zero should be used
11362  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11363  *	  is because applications generally does SICGLIFFLAGS and
11364  *	  ORs in the new flags (that affects the logical) and does a
11365  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11366  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11367  *	  flags that will be turned on is correct with respect to
11368  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11369  */
11370 /* ARGSUSED */
11371 int
11372 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11373     ip_ioctl_cmd_t *ipip, void *if_req)
11374 {
11375 	uint64_t turn_on;
11376 	uint64_t turn_off;
11377 	int	err = 0;
11378 	phyint_t *phyi;
11379 	ill_t *ill;
11380 	uint64_t intf_flags, cantchange_flags;
11381 	boolean_t phyint_flags_modified = B_FALSE;
11382 	uint64_t flags;
11383 	struct ifreq *ifr;
11384 	struct lifreq *lifr;
11385 	boolean_t set_linklocal = B_FALSE;
11386 	boolean_t zero_source = B_FALSE;
11387 
11388 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11389 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11390 
11391 	ASSERT(IAM_WRITER_IPIF(ipif));
11392 
11393 	ill = ipif->ipif_ill;
11394 	phyi = ill->ill_phyint;
11395 
11396 	if (ipip->ipi_cmd_type == IF_CMD) {
11397 		ifr = (struct ifreq *)if_req;
11398 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11399 	} else {
11400 		lifr = (struct lifreq *)if_req;
11401 		flags = lifr->lifr_flags;
11402 	}
11403 
11404 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11405 
11406 	/*
11407 	 * Have the flags been set correctly until now?
11408 	 */
11409 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11410 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11411 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11412 	/*
11413 	 * Compare the new flags to the old, and partition
11414 	 * into those coming on and those going off.
11415 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11416 	 */
11417 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11418 		flags |= intf_flags & ~0xFFFF;
11419 
11420 	/*
11421 	 * Explicitly fail attempts to change flags that are always invalid on
11422 	 * an IPMP meta-interface.
11423 	 */
11424 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11425 		return (EINVAL);
11426 
11427 	/*
11428 	 * Check which flags will change; silently ignore flags which userland
11429 	 * is not allowed to control.  (Because these flags may change between
11430 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11431 	 * control, we need to silently ignore them rather than fail.)
11432 	 */
11433 	cantchange_flags = IFF_CANTCHANGE;
11434 	if (IS_IPMP(ill))
11435 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11436 
11437 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11438 	if (turn_on == 0)
11439 		return (0);	/* No change */
11440 
11441 	turn_off = intf_flags & turn_on;
11442 	turn_on ^= turn_off;
11443 
11444 	/*
11445 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11446 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11447 	 * allow it to be turned off.
11448 	 */
11449 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11450 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11451 		return (EINVAL);
11452 
11453 	if (turn_on & IFF_NOFAILOVER) {
11454 		turn_on |= IFF_DEPRECATED;
11455 		flags |= IFF_DEPRECATED;
11456 	}
11457 
11458 	/*
11459 	 * On underlying interfaces, only allow applications to manage test
11460 	 * addresses -- otherwise, they may get confused when the address
11461 	 * moves as part of being brought up.  Likewise, prevent an
11462 	 * application-managed test address from being converted to a data
11463 	 * address.  To prevent migration of administratively up addresses in
11464 	 * the kernel, we don't allow them to be converted either.
11465 	 */
11466 	if (IS_UNDER_IPMP(ill)) {
11467 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11468 
11469 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11470 			return (EINVAL);
11471 
11472 		if ((turn_off & IFF_NOFAILOVER) &&
11473 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11474 			return (EINVAL);
11475 	}
11476 
11477 	/*
11478 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11479 	 * IPv6 interfaces.
11480 	 */
11481 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11482 		return (EINVAL);
11483 
11484 	/*
11485 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11486 	 */
11487 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11488 		return (EINVAL);
11489 
11490 	/*
11491 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11492 	 * interfaces.  It makes no sense in that context.
11493 	 */
11494 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11495 		return (EINVAL);
11496 
11497 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11498 		zero_source = B_TRUE;
11499 
11500 	/*
11501 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11502 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11503 	 * If the link local address isn't set, and can be set, it will get
11504 	 * set later on in this function.
11505 	 */
11506 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11507 	    (flags & IFF_UP) && !zero_source &&
11508 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11509 		if (ipif_cant_setlinklocal(ipif))
11510 			return (EINVAL);
11511 		set_linklocal = B_TRUE;
11512 	}
11513 
11514 	/*
11515 	 * If we modify physical interface flags, we'll potentially need to
11516 	 * send up two routing socket messages for the changes (one for the
11517 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11518 	 */
11519 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11520 		phyint_flags_modified = B_TRUE;
11521 
11522 	/*
11523 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11524 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11525 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11526 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11527 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11528 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11529 	 * will not be honored.
11530 	 */
11531 	if (turn_on & PHYI_STANDBY) {
11532 		/*
11533 		 * No need to grab ill_g_usesrc_lock here; see the
11534 		 * synchronization notes in ip.c.
11535 		 */
11536 		if (ill->ill_usesrc_grp_next != NULL ||
11537 		    intf_flags & PHYI_INACTIVE)
11538 			return (EINVAL);
11539 		if (!(flags & PHYI_FAILED)) {
11540 			flags |= PHYI_INACTIVE;
11541 			turn_on |= PHYI_INACTIVE;
11542 		}
11543 	}
11544 
11545 	if (turn_off & PHYI_STANDBY) {
11546 		flags &= ~PHYI_INACTIVE;
11547 		turn_off |= PHYI_INACTIVE;
11548 	}
11549 
11550 	/*
11551 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11552 	 * would end up on.
11553 	 */
11554 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11555 	    (PHYI_FAILED | PHYI_INACTIVE))
11556 		return (EINVAL);
11557 
11558 	/*
11559 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11560 	 * status of the interface.
11561 	 */
11562 	if ((turn_on | turn_off) & ILLF_ROUTER)
11563 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11564 
11565 	/*
11566 	 * If the interface is not UP and we are not going to
11567 	 * bring it UP, record the flags and return. When the
11568 	 * interface comes UP later, the right actions will be
11569 	 * taken.
11570 	 */
11571 	if (!(ipif->ipif_flags & IPIF_UP) &&
11572 	    !(turn_on & IPIF_UP)) {
11573 		/* Record new flags in their respective places. */
11574 		mutex_enter(&ill->ill_lock);
11575 		mutex_enter(&ill->ill_phyint->phyint_lock);
11576 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11577 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11578 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11579 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11580 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11581 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11582 		mutex_exit(&ill->ill_lock);
11583 		mutex_exit(&ill->ill_phyint->phyint_lock);
11584 
11585 		/*
11586 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11587 		 * same to the kernel: if any of them has been set by
11588 		 * userland, the interface cannot be used for data traffic.
11589 		 */
11590 		if ((turn_on|turn_off) &
11591 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11592 			ASSERT(!IS_IPMP(ill));
11593 			/*
11594 			 * It's possible the ill is part of an "anonymous"
11595 			 * IPMP group rather than a real group.  In that case,
11596 			 * there are no other interfaces in the group and thus
11597 			 * no need to call ipmp_phyint_refresh_active().
11598 			 */
11599 			if (IS_UNDER_IPMP(ill))
11600 				ipmp_phyint_refresh_active(phyi);
11601 		}
11602 
11603 		if (phyint_flags_modified) {
11604 			if (phyi->phyint_illv4 != NULL) {
11605 				ip_rts_ifmsg(phyi->phyint_illv4->
11606 				    ill_ipif, RTSQ_DEFAULT);
11607 			}
11608 			if (phyi->phyint_illv6 != NULL) {
11609 				ip_rts_ifmsg(phyi->phyint_illv6->
11610 				    ill_ipif, RTSQ_DEFAULT);
11611 			}
11612 		}
11613 		return (0);
11614 	} else if (set_linklocal || zero_source) {
11615 		mutex_enter(&ill->ill_lock);
11616 		if (set_linklocal)
11617 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11618 		if (zero_source)
11619 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11620 		mutex_exit(&ill->ill_lock);
11621 	}
11622 
11623 	/*
11624 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11625 	 * or point-to-point interfaces with an unspecified destination. We do
11626 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11627 	 * have a subnet assigned, which is how in.ndpd currently manages its
11628 	 * onlink prefix list when no addresses are configured with those
11629 	 * prefixes.
11630 	 */
11631 	if (ipif->ipif_isv6 &&
11632 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11633 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11634 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11635 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11636 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11637 		return (EINVAL);
11638 	}
11639 
11640 	/*
11641 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11642 	 * from being brought up.
11643 	 */
11644 	if (!ipif->ipif_isv6 &&
11645 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11646 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11647 		return (EINVAL);
11648 	}
11649 
11650 	/*
11651 	 * The only flag changes that we currently take specific action on are
11652 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11653 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11654 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11655 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11656 	 * of bringing it back up will trigger the address to be moved.
11657 	 */
11658 	if ((turn_on|turn_off) &
11659 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11660 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11661 	    IPIF_NOFAILOVER)) {
11662 		/*
11663 		 * Taking this ipif down, make sure we have
11664 		 * valid net and subnet bcast ire's for other
11665 		 * logical interfaces, if we need them.
11666 		 */
11667 		if (!ipif->ipif_isv6)
11668 			ipif_check_bcast_ires(ipif);
11669 
11670 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11671 		    !(turn_off & IPIF_UP)) {
11672 			if (ipif->ipif_flags & IPIF_UP)
11673 				ill->ill_logical_down = 1;
11674 			turn_on &= ~IPIF_UP;
11675 		}
11676 		err = ipif_down(ipif, q, mp);
11677 		ip1dbg(("ipif_down returns %d err ", err));
11678 		if (err == EINPROGRESS)
11679 			return (err);
11680 		ipif_down_tail(ipif);
11681 	}
11682 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11683 }
11684 
11685 static int
11686 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11687 {
11688 	ill_t	*ill;
11689 	phyint_t *phyi;
11690 	uint64_t turn_on, turn_off;
11691 	uint64_t intf_flags, cantchange_flags;
11692 	boolean_t phyint_flags_modified = B_FALSE;
11693 	int	err = 0;
11694 	boolean_t set_linklocal = B_FALSE;
11695 	boolean_t zero_source = B_FALSE;
11696 
11697 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11698 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11699 
11700 	ASSERT(IAM_WRITER_IPIF(ipif));
11701 
11702 	ill = ipif->ipif_ill;
11703 	phyi = ill->ill_phyint;
11704 
11705 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11706 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11707 	if (IS_IPMP(ill))
11708 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11709 
11710 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11711 	turn_off = intf_flags & turn_on;
11712 	turn_on ^= turn_off;
11713 
11714 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11715 		phyint_flags_modified = B_TRUE;
11716 
11717 	/*
11718 	 * Now we change the flags. Track current value of
11719 	 * other flags in their respective places.
11720 	 */
11721 	mutex_enter(&ill->ill_lock);
11722 	mutex_enter(&phyi->phyint_lock);
11723 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11724 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11725 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11726 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11727 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11728 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11729 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11730 		set_linklocal = B_TRUE;
11731 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11732 	}
11733 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11734 		zero_source = B_TRUE;
11735 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11736 	}
11737 	mutex_exit(&ill->ill_lock);
11738 	mutex_exit(&phyi->phyint_lock);
11739 
11740 	if (set_linklocal)
11741 		(void) ipif_setlinklocal(ipif);
11742 
11743 	if (zero_source)
11744 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11745 	else
11746 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11747 
11748 	/*
11749 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11750 	 * the kernel: if any of them has been set by userland, the interface
11751 	 * cannot be used for data traffic.
11752 	 */
11753 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11754 		ASSERT(!IS_IPMP(ill));
11755 		/*
11756 		 * It's possible the ill is part of an "anonymous" IPMP group
11757 		 * rather than a real group.  In that case, there are no other
11758 		 * interfaces in the group and thus no need for us to call
11759 		 * ipmp_phyint_refresh_active().
11760 		 */
11761 		if (IS_UNDER_IPMP(ill))
11762 			ipmp_phyint_refresh_active(phyi);
11763 	}
11764 
11765 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11766 		/*
11767 		 * XXX ipif_up really does not know whether a phyint flags
11768 		 * was modified or not. So, it sends up information on
11769 		 * only one routing sockets message. As we don't bring up
11770 		 * the interface and also set PHYI_ flags simultaneously
11771 		 * it should be okay.
11772 		 */
11773 		err = ipif_up(ipif, q, mp);
11774 	} else {
11775 		/*
11776 		 * Make sure routing socket sees all changes to the flags.
11777 		 * ipif_up_done* handles this when we use ipif_up.
11778 		 */
11779 		if (phyint_flags_modified) {
11780 			if (phyi->phyint_illv4 != NULL) {
11781 				ip_rts_ifmsg(phyi->phyint_illv4->
11782 				    ill_ipif, RTSQ_DEFAULT);
11783 			}
11784 			if (phyi->phyint_illv6 != NULL) {
11785 				ip_rts_ifmsg(phyi->phyint_illv6->
11786 				    ill_ipif, RTSQ_DEFAULT);
11787 			}
11788 		} else {
11789 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11790 		}
11791 		/*
11792 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11793 		 * this in need_up case.
11794 		 */
11795 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11796 	}
11797 	return (err);
11798 }
11799 
11800 /*
11801  * Restart the flags operation now that the refcounts have dropped to zero.
11802  */
11803 /* ARGSUSED */
11804 int
11805 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11806     ip_ioctl_cmd_t *ipip, void *if_req)
11807 {
11808 	uint64_t flags;
11809 	struct ifreq *ifr = if_req;
11810 	struct lifreq *lifr = if_req;
11811 
11812 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11813 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11814 
11815 	ipif_down_tail(ipif);
11816 	if (ipip->ipi_cmd_type == IF_CMD) {
11817 		/* cast to uint16_t prevents unwanted sign extension */
11818 		flags = (uint16_t)ifr->ifr_flags;
11819 	} else {
11820 		flags = lifr->lifr_flags;
11821 	}
11822 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11823 }
11824 
11825 /*
11826  * Can operate on either a module or a driver queue.
11827  */
11828 /* ARGSUSED */
11829 int
11830 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11831     ip_ioctl_cmd_t *ipip, void *if_req)
11832 {
11833 	/*
11834 	 * Has the flags been set correctly till now ?
11835 	 */
11836 	ill_t *ill = ipif->ipif_ill;
11837 	phyint_t *phyi = ill->ill_phyint;
11838 
11839 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11840 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11841 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11842 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11843 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11844 
11845 	/*
11846 	 * Need a lock since some flags can be set even when there are
11847 	 * references to the ipif.
11848 	 */
11849 	mutex_enter(&ill->ill_lock);
11850 	if (ipip->ipi_cmd_type == IF_CMD) {
11851 		struct ifreq *ifr = (struct ifreq *)if_req;
11852 
11853 		/* Get interface flags (low 16 only). */
11854 		ifr->ifr_flags = ((ipif->ipif_flags |
11855 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11856 	} else {
11857 		struct lifreq *lifr = (struct lifreq *)if_req;
11858 
11859 		/* Get interface flags. */
11860 		lifr->lifr_flags = ipif->ipif_flags |
11861 		    ill->ill_flags | phyi->phyint_flags;
11862 	}
11863 	mutex_exit(&ill->ill_lock);
11864 	return (0);
11865 }
11866 
11867 /* ARGSUSED */
11868 int
11869 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11870     ip_ioctl_cmd_t *ipip, void *if_req)
11871 {
11872 	int mtu;
11873 	int ip_min_mtu;
11874 	struct ifreq	*ifr;
11875 	struct lifreq *lifr;
11876 	ire_t	*ire;
11877 	ip_stack_t *ipst;
11878 
11879 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11880 	    ipif->ipif_id, (void *)ipif));
11881 	if (ipip->ipi_cmd_type == IF_CMD) {
11882 		ifr = (struct ifreq *)if_req;
11883 		mtu = ifr->ifr_metric;
11884 	} else {
11885 		lifr = (struct lifreq *)if_req;
11886 		mtu = lifr->lifr_mtu;
11887 	}
11888 
11889 	if (ipif->ipif_isv6)
11890 		ip_min_mtu = IPV6_MIN_MTU;
11891 	else
11892 		ip_min_mtu = IP_MIN_MTU;
11893 
11894 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11895 		return (EINVAL);
11896 
11897 	/*
11898 	 * Change the MTU size in all relevant ire's.
11899 	 * Mtu change Vs. new ire creation - protocol below.
11900 	 * First change ipif_mtu and the ire_max_frag of the
11901 	 * interface ire. Then do an ire walk and change the
11902 	 * ire_max_frag of all affected ires. During ire_add
11903 	 * under the bucket lock, set the ire_max_frag of the
11904 	 * new ire being created from the ipif/ire from which
11905 	 * it is being derived. If an mtu change happens after
11906 	 * the ire is added, the new ire will be cleaned up.
11907 	 * Conversely if the mtu change happens before the ire
11908 	 * is added, ire_add will see the new value of the mtu.
11909 	 */
11910 	ipif->ipif_mtu = mtu;
11911 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11912 
11913 	if (ipif->ipif_isv6)
11914 		ire = ipif_to_ire_v6(ipif);
11915 	else
11916 		ire = ipif_to_ire(ipif);
11917 	if (ire != NULL) {
11918 		ire->ire_max_frag = ipif->ipif_mtu;
11919 		ire_refrele(ire);
11920 	}
11921 	ipst = ipif->ipif_ill->ill_ipst;
11922 	if (ipif->ipif_flags & IPIF_UP) {
11923 		if (ipif->ipif_isv6)
11924 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11925 			    ipst);
11926 		else
11927 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11928 			    ipst);
11929 	}
11930 	/* Update the MTU in SCTP's list */
11931 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11932 	return (0);
11933 }
11934 
11935 /* Get interface MTU. */
11936 /* ARGSUSED */
11937 int
11938 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11939 	ip_ioctl_cmd_t *ipip, void *if_req)
11940 {
11941 	struct ifreq	*ifr;
11942 	struct lifreq	*lifr;
11943 
11944 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11945 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11946 	if (ipip->ipi_cmd_type == IF_CMD) {
11947 		ifr = (struct ifreq *)if_req;
11948 		ifr->ifr_metric = ipif->ipif_mtu;
11949 	} else {
11950 		lifr = (struct lifreq *)if_req;
11951 		lifr->lifr_mtu = ipif->ipif_mtu;
11952 	}
11953 	return (0);
11954 }
11955 
11956 /* Set interface broadcast address. */
11957 /* ARGSUSED2 */
11958 int
11959 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11960 	ip_ioctl_cmd_t *ipip, void *if_req)
11961 {
11962 	ipaddr_t addr;
11963 	ire_t	*ire;
11964 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11965 
11966 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11967 	    ipif->ipif_id));
11968 
11969 	ASSERT(IAM_WRITER_IPIF(ipif));
11970 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11971 		return (EADDRNOTAVAIL);
11972 
11973 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11974 
11975 	if (sin->sin_family != AF_INET)
11976 		return (EAFNOSUPPORT);
11977 
11978 	addr = sin->sin_addr.s_addr;
11979 	if (ipif->ipif_flags & IPIF_UP) {
11980 		/*
11981 		 * If we are already up, make sure the new
11982 		 * broadcast address makes sense.  If it does,
11983 		 * there should be an IRE for it already.
11984 		 * Don't match on ipif, only on the ill
11985 		 * since we are sharing these now.
11986 		 */
11987 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11988 		    ipif, ALL_ZONES, NULL,
11989 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
11990 		if (ire == NULL) {
11991 			return (EINVAL);
11992 		} else {
11993 			ire_refrele(ire);
11994 		}
11995 	}
11996 	/*
11997 	 * Changing the broadcast addr for this ipif.
11998 	 * Make sure we have valid net and subnet bcast
11999 	 * ire's for other logical interfaces, if needed.
12000 	 */
12001 	if (addr != ipif->ipif_brd_addr)
12002 		ipif_check_bcast_ires(ipif);
12003 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12004 	return (0);
12005 }
12006 
12007 /* Get interface broadcast address. */
12008 /* ARGSUSED */
12009 int
12010 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12011     ip_ioctl_cmd_t *ipip, void *if_req)
12012 {
12013 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12014 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12015 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12016 		return (EADDRNOTAVAIL);
12017 
12018 	/* IPIF_BROADCAST not possible with IPv6 */
12019 	ASSERT(!ipif->ipif_isv6);
12020 	*sin = sin_null;
12021 	sin->sin_family = AF_INET;
12022 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12023 	return (0);
12024 }
12025 
12026 /*
12027  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12028  */
12029 /* ARGSUSED */
12030 int
12031 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12032     ip_ioctl_cmd_t *ipip, void *if_req)
12033 {
12034 	int err = 0;
12035 	in6_addr_t v6mask;
12036 
12037 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12039 
12040 	ASSERT(IAM_WRITER_IPIF(ipif));
12041 
12042 	if (ipif->ipif_isv6) {
12043 		sin6_t *sin6;
12044 
12045 		if (sin->sin_family != AF_INET6)
12046 			return (EAFNOSUPPORT);
12047 
12048 		sin6 = (sin6_t *)sin;
12049 		v6mask = sin6->sin6_addr;
12050 	} else {
12051 		ipaddr_t mask;
12052 
12053 		if (sin->sin_family != AF_INET)
12054 			return (EAFNOSUPPORT);
12055 
12056 		mask = sin->sin_addr.s_addr;
12057 		V4MASK_TO_V6(mask, v6mask);
12058 	}
12059 
12060 	/*
12061 	 * No big deal if the interface isn't already up, or the mask
12062 	 * isn't really changing, or this is pt-pt.
12063 	 */
12064 	if (!(ipif->ipif_flags & IPIF_UP) ||
12065 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12066 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12067 		ipif->ipif_v6net_mask = v6mask;
12068 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12069 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12070 			    ipif->ipif_v6net_mask,
12071 			    ipif->ipif_v6subnet);
12072 		}
12073 		return (0);
12074 	}
12075 	/*
12076 	 * Make sure we have valid net and subnet broadcast ire's
12077 	 * for the old netmask, if needed by other logical interfaces.
12078 	 */
12079 	if (!ipif->ipif_isv6)
12080 		ipif_check_bcast_ires(ipif);
12081 
12082 	err = ipif_logical_down(ipif, q, mp);
12083 	if (err == EINPROGRESS)
12084 		return (err);
12085 	ipif_down_tail(ipif);
12086 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12087 	return (err);
12088 }
12089 
12090 static int
12091 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12092 {
12093 	in6_addr_t v6mask;
12094 	int err = 0;
12095 
12096 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12097 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12098 
12099 	if (ipif->ipif_isv6) {
12100 		sin6_t *sin6;
12101 
12102 		sin6 = (sin6_t *)sin;
12103 		v6mask = sin6->sin6_addr;
12104 	} else {
12105 		ipaddr_t mask;
12106 
12107 		mask = sin->sin_addr.s_addr;
12108 		V4MASK_TO_V6(mask, v6mask);
12109 	}
12110 
12111 	ipif->ipif_v6net_mask = v6mask;
12112 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12113 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12114 		    ipif->ipif_v6subnet);
12115 	}
12116 	err = ipif_up(ipif, q, mp);
12117 
12118 	if (err == 0 || err == EINPROGRESS) {
12119 		/*
12120 		 * The interface must be DL_BOUND if this packet has to
12121 		 * go out on the wire. Since we only go through a logical
12122 		 * down and are bound with the driver during an internal
12123 		 * down/up that is satisfied.
12124 		 */
12125 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12126 			/* Potentially broadcast an address mask reply. */
12127 			ipif_mask_reply(ipif);
12128 		}
12129 	}
12130 	return (err);
12131 }
12132 
12133 /* ARGSUSED */
12134 int
12135 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12136     ip_ioctl_cmd_t *ipip, void *if_req)
12137 {
12138 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12139 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12140 	ipif_down_tail(ipif);
12141 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12142 }
12143 
12144 /* Get interface net mask. */
12145 /* ARGSUSED */
12146 int
12147 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12148     ip_ioctl_cmd_t *ipip, void *if_req)
12149 {
12150 	struct lifreq *lifr = (struct lifreq *)if_req;
12151 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12152 
12153 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12154 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12155 
12156 	/*
12157 	 * net mask can't change since we have a reference to the ipif.
12158 	 */
12159 	if (ipif->ipif_isv6) {
12160 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12161 		*sin6 = sin6_null;
12162 		sin6->sin6_family = AF_INET6;
12163 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12164 		lifr->lifr_addrlen =
12165 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12166 	} else {
12167 		*sin = sin_null;
12168 		sin->sin_family = AF_INET;
12169 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12170 		if (ipip->ipi_cmd_type == LIF_CMD) {
12171 			lifr->lifr_addrlen =
12172 			    ip_mask_to_plen(ipif->ipif_net_mask);
12173 		}
12174 	}
12175 	return (0);
12176 }
12177 
12178 /* ARGSUSED */
12179 int
12180 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12181     ip_ioctl_cmd_t *ipip, void *if_req)
12182 {
12183 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12184 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12185 
12186 	/*
12187 	 * Since no applications should ever be setting metrics on underlying
12188 	 * interfaces, we explicitly fail to smoke 'em out.
12189 	 */
12190 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12191 		return (EINVAL);
12192 
12193 	/*
12194 	 * Set interface metric.  We don't use this for
12195 	 * anything but we keep track of it in case it is
12196 	 * important to routing applications or such.
12197 	 */
12198 	if (ipip->ipi_cmd_type == IF_CMD) {
12199 		struct ifreq    *ifr;
12200 
12201 		ifr = (struct ifreq *)if_req;
12202 		ipif->ipif_metric = ifr->ifr_metric;
12203 	} else {
12204 		struct lifreq   *lifr;
12205 
12206 		lifr = (struct lifreq *)if_req;
12207 		ipif->ipif_metric = lifr->lifr_metric;
12208 	}
12209 	return (0);
12210 }
12211 
12212 /* ARGSUSED */
12213 int
12214 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12215     ip_ioctl_cmd_t *ipip, void *if_req)
12216 {
12217 	/* Get interface metric. */
12218 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12219 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12220 
12221 	if (ipip->ipi_cmd_type == IF_CMD) {
12222 		struct ifreq    *ifr;
12223 
12224 		ifr = (struct ifreq *)if_req;
12225 		ifr->ifr_metric = ipif->ipif_metric;
12226 	} else {
12227 		struct lifreq   *lifr;
12228 
12229 		lifr = (struct lifreq *)if_req;
12230 		lifr->lifr_metric = ipif->ipif_metric;
12231 	}
12232 
12233 	return (0);
12234 }
12235 
12236 /* ARGSUSED */
12237 int
12238 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12239     ip_ioctl_cmd_t *ipip, void *if_req)
12240 {
12241 
12242 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12243 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12244 	/*
12245 	 * Set the muxid returned from I_PLINK.
12246 	 */
12247 	if (ipip->ipi_cmd_type == IF_CMD) {
12248 		struct ifreq *ifr = (struct ifreq *)if_req;
12249 
12250 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12251 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12252 	} else {
12253 		struct lifreq *lifr = (struct lifreq *)if_req;
12254 
12255 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12256 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12257 	}
12258 	return (0);
12259 }
12260 
12261 /* ARGSUSED */
12262 int
12263 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12264     ip_ioctl_cmd_t *ipip, void *if_req)
12265 {
12266 
12267 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12268 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12269 	/*
12270 	 * Get the muxid saved in ill for I_PUNLINK.
12271 	 */
12272 	if (ipip->ipi_cmd_type == IF_CMD) {
12273 		struct ifreq *ifr = (struct ifreq *)if_req;
12274 
12275 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12276 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12277 	} else {
12278 		struct lifreq *lifr = (struct lifreq *)if_req;
12279 
12280 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12281 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12282 	}
12283 	return (0);
12284 }
12285 
12286 /*
12287  * Set the subnet prefix. Does not modify the broadcast address.
12288  */
12289 /* ARGSUSED */
12290 int
12291 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12292     ip_ioctl_cmd_t *ipip, void *if_req)
12293 {
12294 	int err = 0;
12295 	in6_addr_t v6addr;
12296 	in6_addr_t v6mask;
12297 	boolean_t need_up = B_FALSE;
12298 	int addrlen;
12299 
12300 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12301 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12302 
12303 	ASSERT(IAM_WRITER_IPIF(ipif));
12304 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12305 
12306 	if (ipif->ipif_isv6) {
12307 		sin6_t *sin6;
12308 
12309 		if (sin->sin_family != AF_INET6)
12310 			return (EAFNOSUPPORT);
12311 
12312 		sin6 = (sin6_t *)sin;
12313 		v6addr = sin6->sin6_addr;
12314 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12315 			return (EADDRNOTAVAIL);
12316 	} else {
12317 		ipaddr_t addr;
12318 
12319 		if (sin->sin_family != AF_INET)
12320 			return (EAFNOSUPPORT);
12321 
12322 		addr = sin->sin_addr.s_addr;
12323 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12324 			return (EADDRNOTAVAIL);
12325 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12326 		/* Add 96 bits */
12327 		addrlen += IPV6_ABITS - IP_ABITS;
12328 	}
12329 
12330 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12331 		return (EINVAL);
12332 
12333 	/* Check if bits in the address is set past the mask */
12334 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12335 		return (EINVAL);
12336 
12337 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12338 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12339 		return (0);	/* No change */
12340 
12341 	if (ipif->ipif_flags & IPIF_UP) {
12342 		/*
12343 		 * If the interface is already marked up,
12344 		 * we call ipif_down which will take care
12345 		 * of ditching any IREs that have been set
12346 		 * up based on the old interface address.
12347 		 */
12348 		err = ipif_logical_down(ipif, q, mp);
12349 		if (err == EINPROGRESS)
12350 			return (err);
12351 		ipif_down_tail(ipif);
12352 		need_up = B_TRUE;
12353 	}
12354 
12355 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12356 	return (err);
12357 }
12358 
12359 static int
12360 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12361     queue_t *q, mblk_t *mp, boolean_t need_up)
12362 {
12363 	ill_t	*ill = ipif->ipif_ill;
12364 	int	err = 0;
12365 
12366 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12367 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12368 
12369 	/* Set the new address. */
12370 	mutex_enter(&ill->ill_lock);
12371 	ipif->ipif_v6net_mask = v6mask;
12372 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12373 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12374 		    ipif->ipif_v6subnet);
12375 	}
12376 	mutex_exit(&ill->ill_lock);
12377 
12378 	if (need_up) {
12379 		/*
12380 		 * Now bring the interface back up.  If this
12381 		 * is the only IPIF for the ILL, ipif_up
12382 		 * will have to re-bind to the device, so
12383 		 * we may get back EINPROGRESS, in which
12384 		 * case, this IOCTL will get completed in
12385 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12386 		 */
12387 		err = ipif_up(ipif, q, mp);
12388 		if (err == EINPROGRESS)
12389 			return (err);
12390 	}
12391 	return (err);
12392 }
12393 
12394 /* ARGSUSED */
12395 int
12396 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12397     ip_ioctl_cmd_t *ipip, void *if_req)
12398 {
12399 	int	addrlen;
12400 	in6_addr_t v6addr;
12401 	in6_addr_t v6mask;
12402 	struct lifreq *lifr = (struct lifreq *)if_req;
12403 
12404 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12405 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12406 	ipif_down_tail(ipif);
12407 
12408 	addrlen = lifr->lifr_addrlen;
12409 	if (ipif->ipif_isv6) {
12410 		sin6_t *sin6;
12411 
12412 		sin6 = (sin6_t *)sin;
12413 		v6addr = sin6->sin6_addr;
12414 	} else {
12415 		ipaddr_t addr;
12416 
12417 		addr = sin->sin_addr.s_addr;
12418 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12419 		addrlen += IPV6_ABITS - IP_ABITS;
12420 	}
12421 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12422 
12423 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12424 }
12425 
12426 /* ARGSUSED */
12427 int
12428 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12429     ip_ioctl_cmd_t *ipip, void *if_req)
12430 {
12431 	struct lifreq *lifr = (struct lifreq *)if_req;
12432 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12433 
12434 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12435 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12436 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12437 
12438 	if (ipif->ipif_isv6) {
12439 		*sin6 = sin6_null;
12440 		sin6->sin6_family = AF_INET6;
12441 		sin6->sin6_addr = ipif->ipif_v6subnet;
12442 		lifr->lifr_addrlen =
12443 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12444 	} else {
12445 		*sin = sin_null;
12446 		sin->sin_family = AF_INET;
12447 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12448 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12449 	}
12450 	return (0);
12451 }
12452 
12453 /*
12454  * Set the IPv6 address token.
12455  */
12456 /* ARGSUSED */
12457 int
12458 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12459     ip_ioctl_cmd_t *ipi, void *if_req)
12460 {
12461 	ill_t *ill = ipif->ipif_ill;
12462 	int err;
12463 	in6_addr_t v6addr;
12464 	in6_addr_t v6mask;
12465 	boolean_t need_up = B_FALSE;
12466 	int i;
12467 	sin6_t *sin6 = (sin6_t *)sin;
12468 	struct lifreq *lifr = (struct lifreq *)if_req;
12469 	int addrlen;
12470 
12471 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12472 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12473 	ASSERT(IAM_WRITER_IPIF(ipif));
12474 
12475 	addrlen = lifr->lifr_addrlen;
12476 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12477 	if (ipif->ipif_id != 0)
12478 		return (EINVAL);
12479 
12480 	if (!ipif->ipif_isv6)
12481 		return (EINVAL);
12482 
12483 	if (addrlen > IPV6_ABITS)
12484 		return (EINVAL);
12485 
12486 	v6addr = sin6->sin6_addr;
12487 
12488 	/*
12489 	 * The length of the token is the length from the end.  To get
12490 	 * the proper mask for this, compute the mask of the bits not
12491 	 * in the token; ie. the prefix, and then xor to get the mask.
12492 	 */
12493 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12494 		return (EINVAL);
12495 	for (i = 0; i < 4; i++) {
12496 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12497 	}
12498 
12499 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12500 	    ill->ill_token_length == addrlen)
12501 		return (0);	/* No change */
12502 
12503 	if (ipif->ipif_flags & IPIF_UP) {
12504 		err = ipif_logical_down(ipif, q, mp);
12505 		if (err == EINPROGRESS)
12506 			return (err);
12507 		ipif_down_tail(ipif);
12508 		need_up = B_TRUE;
12509 	}
12510 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12511 	return (err);
12512 }
12513 
12514 static int
12515 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12516     mblk_t *mp, boolean_t need_up)
12517 {
12518 	in6_addr_t v6addr;
12519 	in6_addr_t v6mask;
12520 	ill_t	*ill = ipif->ipif_ill;
12521 	int	i;
12522 	int	err = 0;
12523 
12524 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12525 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12526 	v6addr = sin6->sin6_addr;
12527 	/*
12528 	 * The length of the token is the length from the end.  To get
12529 	 * the proper mask for this, compute the mask of the bits not
12530 	 * in the token; ie. the prefix, and then xor to get the mask.
12531 	 */
12532 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12533 	for (i = 0; i < 4; i++)
12534 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12535 
12536 	mutex_enter(&ill->ill_lock);
12537 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12538 	ill->ill_token_length = addrlen;
12539 	mutex_exit(&ill->ill_lock);
12540 
12541 	if (need_up) {
12542 		/*
12543 		 * Now bring the interface back up.  If this
12544 		 * is the only IPIF for the ILL, ipif_up
12545 		 * will have to re-bind to the device, so
12546 		 * we may get back EINPROGRESS, in which
12547 		 * case, this IOCTL will get completed in
12548 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12549 		 */
12550 		err = ipif_up(ipif, q, mp);
12551 		if (err == EINPROGRESS)
12552 			return (err);
12553 	}
12554 	return (err);
12555 }
12556 
12557 /* ARGSUSED */
12558 int
12559 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12560     ip_ioctl_cmd_t *ipi, void *if_req)
12561 {
12562 	ill_t *ill;
12563 	sin6_t *sin6 = (sin6_t *)sin;
12564 	struct lifreq *lifr = (struct lifreq *)if_req;
12565 
12566 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12567 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12568 	if (ipif->ipif_id != 0)
12569 		return (EINVAL);
12570 
12571 	ill = ipif->ipif_ill;
12572 	if (!ill->ill_isv6)
12573 		return (ENXIO);
12574 
12575 	*sin6 = sin6_null;
12576 	sin6->sin6_family = AF_INET6;
12577 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12578 	sin6->sin6_addr = ill->ill_token;
12579 	lifr->lifr_addrlen = ill->ill_token_length;
12580 	return (0);
12581 }
12582 
12583 /*
12584  * Set (hardware) link specific information that might override
12585  * what was acquired through the DL_INFO_ACK.
12586  * The logic is as follows.
12587  *
12588  * become exclusive
12589  * set CHANGING flag
12590  * change mtu on affected IREs
12591  * clear CHANGING flag
12592  *
12593  * An ire add that occurs before the CHANGING flag is set will have its mtu
12594  * changed by the ip_sioctl_lnkinfo.
12595  *
12596  * During the time the CHANGING flag is set, no new ires will be added to the
12597  * bucket, and ire add will fail (due the CHANGING flag).
12598  *
12599  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12600  * before it is added to the bucket.
12601  *
12602  * Obviously only 1 thread can set the CHANGING flag and we need to become
12603  * exclusive to set the flag.
12604  */
12605 /* ARGSUSED */
12606 int
12607 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12608     ip_ioctl_cmd_t *ipi, void *if_req)
12609 {
12610 	ill_t		*ill = ipif->ipif_ill;
12611 	ipif_t		*nipif;
12612 	int		ip_min_mtu;
12613 	boolean_t	mtu_walk = B_FALSE;
12614 	struct lifreq	*lifr = (struct lifreq *)if_req;
12615 	lif_ifinfo_req_t *lir;
12616 	ire_t		*ire;
12617 
12618 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12619 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12620 	lir = &lifr->lifr_ifinfo;
12621 	ASSERT(IAM_WRITER_IPIF(ipif));
12622 
12623 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12624 	if (ipif->ipif_id != 0)
12625 		return (EINVAL);
12626 
12627 	/* Set interface MTU. */
12628 	if (ipif->ipif_isv6)
12629 		ip_min_mtu = IPV6_MIN_MTU;
12630 	else
12631 		ip_min_mtu = IP_MIN_MTU;
12632 
12633 	/*
12634 	 * Verify values before we set anything. Allow zero to
12635 	 * mean unspecified.
12636 	 */
12637 	if (lir->lir_maxmtu != 0 &&
12638 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12639 	    lir->lir_maxmtu < ip_min_mtu))
12640 		return (EINVAL);
12641 	if (lir->lir_reachtime != 0 &&
12642 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12643 		return (EINVAL);
12644 	if (lir->lir_reachretrans != 0 &&
12645 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12646 		return (EINVAL);
12647 
12648 	mutex_enter(&ill->ill_lock);
12649 	ill->ill_state_flags |= ILL_CHANGING;
12650 	for (nipif = ill->ill_ipif; nipif != NULL;
12651 	    nipif = nipif->ipif_next) {
12652 		nipif->ipif_state_flags |= IPIF_CHANGING;
12653 	}
12654 
12655 	if (lir->lir_maxmtu != 0) {
12656 		ill->ill_max_mtu = lir->lir_maxmtu;
12657 		ill->ill_user_mtu = lir->lir_maxmtu;
12658 		mtu_walk = B_TRUE;
12659 	}
12660 	mutex_exit(&ill->ill_lock);
12661 
12662 	if (lir->lir_reachtime != 0)
12663 		ill->ill_reachable_time = lir->lir_reachtime;
12664 
12665 	if (lir->lir_reachretrans != 0)
12666 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12667 
12668 	ill->ill_max_hops = lir->lir_maxhops;
12669 
12670 	ill->ill_max_buf = ND_MAX_Q;
12671 
12672 	if (mtu_walk) {
12673 		/*
12674 		 * Set the MTU on all ipifs associated with this ill except
12675 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12676 		 */
12677 		for (nipif = ill->ill_ipif; nipif != NULL;
12678 		    nipif = nipif->ipif_next) {
12679 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12680 				continue;
12681 
12682 			nipif->ipif_mtu = ill->ill_max_mtu;
12683 
12684 			if (!(nipif->ipif_flags & IPIF_UP))
12685 				continue;
12686 
12687 			if (nipif->ipif_isv6)
12688 				ire = ipif_to_ire_v6(nipif);
12689 			else
12690 				ire = ipif_to_ire(nipif);
12691 			if (ire != NULL) {
12692 				ire->ire_max_frag = ipif->ipif_mtu;
12693 				ire_refrele(ire);
12694 			}
12695 
12696 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12697 			    nipif, ill);
12698 		}
12699 	}
12700 
12701 	mutex_enter(&ill->ill_lock);
12702 	for (nipif = ill->ill_ipif; nipif != NULL;
12703 	    nipif = nipif->ipif_next) {
12704 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12705 	}
12706 	ILL_UNMARK_CHANGING(ill);
12707 	mutex_exit(&ill->ill_lock);
12708 
12709 	/*
12710 	 * Refresh IPMP meta-interface MTU if necessary.
12711 	 */
12712 	if (IS_UNDER_IPMP(ill))
12713 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12714 
12715 	return (0);
12716 }
12717 
12718 /* ARGSUSED */
12719 int
12720 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12721     ip_ioctl_cmd_t *ipi, void *if_req)
12722 {
12723 	struct lif_ifinfo_req *lir;
12724 	ill_t *ill = ipif->ipif_ill;
12725 
12726 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12727 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12728 	if (ipif->ipif_id != 0)
12729 		return (EINVAL);
12730 
12731 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12732 	lir->lir_maxhops = ill->ill_max_hops;
12733 	lir->lir_reachtime = ill->ill_reachable_time;
12734 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12735 	lir->lir_maxmtu = ill->ill_max_mtu;
12736 
12737 	return (0);
12738 }
12739 
12740 /*
12741  * Return best guess as to the subnet mask for the specified address.
12742  * Based on the subnet masks for all the configured interfaces.
12743  *
12744  * We end up returning a zero mask in the case of default, multicast or
12745  * experimental.
12746  */
12747 static ipaddr_t
12748 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12749 {
12750 	ipaddr_t net_mask;
12751 	ill_t	*ill;
12752 	ipif_t	*ipif;
12753 	ill_walk_context_t ctx;
12754 	ipif_t	*fallback_ipif = NULL;
12755 
12756 	net_mask = ip_net_mask(addr);
12757 	if (net_mask == 0) {
12758 		*ipifp = NULL;
12759 		return (0);
12760 	}
12761 
12762 	/* Let's check to see if this is maybe a local subnet route. */
12763 	/* this function only applies to IPv4 interfaces */
12764 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12765 	ill = ILL_START_WALK_V4(&ctx, ipst);
12766 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12767 		mutex_enter(&ill->ill_lock);
12768 		for (ipif = ill->ill_ipif; ipif != NULL;
12769 		    ipif = ipif->ipif_next) {
12770 			if (!IPIF_CAN_LOOKUP(ipif))
12771 				continue;
12772 			if (!(ipif->ipif_flags & IPIF_UP))
12773 				continue;
12774 			if ((ipif->ipif_subnet & net_mask) ==
12775 			    (addr & net_mask)) {
12776 				/*
12777 				 * Don't trust pt-pt interfaces if there are
12778 				 * other interfaces.
12779 				 */
12780 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12781 					if (fallback_ipif == NULL) {
12782 						ipif_refhold_locked(ipif);
12783 						fallback_ipif = ipif;
12784 					}
12785 					continue;
12786 				}
12787 
12788 				/*
12789 				 * Fine. Just assume the same net mask as the
12790 				 * directly attached subnet interface is using.
12791 				 */
12792 				ipif_refhold_locked(ipif);
12793 				mutex_exit(&ill->ill_lock);
12794 				rw_exit(&ipst->ips_ill_g_lock);
12795 				if (fallback_ipif != NULL)
12796 					ipif_refrele(fallback_ipif);
12797 				*ipifp = ipif;
12798 				return (ipif->ipif_net_mask);
12799 			}
12800 		}
12801 		mutex_exit(&ill->ill_lock);
12802 	}
12803 	rw_exit(&ipst->ips_ill_g_lock);
12804 
12805 	*ipifp = fallback_ipif;
12806 	return ((fallback_ipif != NULL) ?
12807 	    fallback_ipif->ipif_net_mask : net_mask);
12808 }
12809 
12810 /*
12811  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12812  */
12813 static void
12814 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12815 {
12816 	IOCP	iocp;
12817 	ipft_t	*ipft;
12818 	ipllc_t	*ipllc;
12819 	mblk_t	*mp1;
12820 	cred_t	*cr;
12821 	int	error = 0;
12822 	conn_t	*connp;
12823 
12824 	ip1dbg(("ip_wput_ioctl"));
12825 	iocp = (IOCP)mp->b_rptr;
12826 	mp1 = mp->b_cont;
12827 	if (mp1 == NULL) {
12828 		iocp->ioc_error = EINVAL;
12829 		mp->b_datap->db_type = M_IOCNAK;
12830 		iocp->ioc_count = 0;
12831 		qreply(q, mp);
12832 		return;
12833 	}
12834 
12835 	/*
12836 	 * These IOCTLs provide various control capabilities to
12837 	 * upstream agents such as ULPs and processes.	There
12838 	 * are currently two such IOCTLs implemented.  They
12839 	 * are used by TCP to provide update information for
12840 	 * existing IREs and to forcibly delete an IRE for a
12841 	 * host that is not responding, thereby forcing an
12842 	 * attempt at a new route.
12843 	 */
12844 	iocp->ioc_error = EINVAL;
12845 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12846 		goto done;
12847 
12848 	ipllc = (ipllc_t *)mp1->b_rptr;
12849 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12850 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12851 			break;
12852 	}
12853 	/*
12854 	 * prefer credential from mblk over ioctl;
12855 	 * see ip_sioctl_copyin_setup
12856 	 */
12857 	cr = msg_getcred(mp, NULL);
12858 	if (cr == NULL)
12859 		cr = iocp->ioc_cr;
12860 
12861 	/*
12862 	 * Refhold the conn in case the request gets queued up in some lookup
12863 	 */
12864 	ASSERT(CONN_Q(q));
12865 	connp = Q_TO_CONN(q);
12866 	CONN_INC_REF(connp);
12867 	if (ipft->ipft_pfi &&
12868 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12869 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12870 		error = (*ipft->ipft_pfi)(q,
12871 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12872 	}
12873 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12874 		/*
12875 		 * CONN_OPER_PENDING_DONE happens in the function called
12876 		 * through ipft_pfi above.
12877 		 */
12878 		return;
12879 	}
12880 
12881 	CONN_OPER_PENDING_DONE(connp);
12882 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12883 		freemsg(mp);
12884 		return;
12885 	}
12886 	iocp->ioc_error = error;
12887 
12888 done:
12889 	mp->b_datap->db_type = M_IOCACK;
12890 	if (iocp->ioc_error)
12891 		iocp->ioc_count = 0;
12892 	qreply(q, mp);
12893 }
12894 
12895 /*
12896  * Lookup an ipif using the sequence id (ipif_seqid)
12897  */
12898 ipif_t *
12899 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12900 {
12901 	ipif_t *ipif;
12902 
12903 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12904 
12905 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12906 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12907 			return (ipif);
12908 	}
12909 	return (NULL);
12910 }
12911 
12912 /*
12913  * Assign a unique id for the ipif. This is used later when we send
12914  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12915  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12916  * IRE is added, we verify that ipif has not disappeared.
12917  */
12918 
12919 static void
12920 ipif_assign_seqid(ipif_t *ipif)
12921 {
12922 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12923 
12924 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
12925 }
12926 
12927 /*
12928  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
12929  * administratively down (i.e., no DAD), of the same type, and locked.  Note
12930  * that the clone is complete -- including the seqid -- and the expectation is
12931  * that the caller will either free or overwrite `sipif' before it's unlocked.
12932  */
12933 static void
12934 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
12935 {
12936 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
12937 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
12938 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12939 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12940 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
12941 	ASSERT(sipif->ipif_arp_del_mp == NULL);
12942 	ASSERT(dipif->ipif_arp_del_mp == NULL);
12943 	ASSERT(sipif->ipif_igmp_rpt == NULL);
12944 	ASSERT(dipif->ipif_igmp_rpt == NULL);
12945 	ASSERT(sipif->ipif_multicast_up == 0);
12946 	ASSERT(dipif->ipif_multicast_up == 0);
12947 	ASSERT(sipif->ipif_joined_allhosts == 0);
12948 	ASSERT(dipif->ipif_joined_allhosts == 0);
12949 
12950 	dipif->ipif_mtu = sipif->ipif_mtu;
12951 	dipif->ipif_flags = sipif->ipif_flags;
12952 	dipif->ipif_metric = sipif->ipif_metric;
12953 	dipif->ipif_zoneid = sipif->ipif_zoneid;
12954 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
12955 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
12956 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
12957 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
12958 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
12959 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
12960 
12961 	/*
12962 	 * While dipif is down right now, it might've been up before.  Since
12963 	 * it's changing identity, its packet counters need to be reset.
12964 	 */
12965 	dipif->ipif_ib_pkt_count = 0;
12966 	dipif->ipif_ob_pkt_count = 0;
12967 	dipif->ipif_fo_pkt_count = 0;
12968 
12969 	/*
12970 	 * As per the comment atop the function, we assume that these sipif
12971 	 * fields will be changed before sipif is unlocked.
12972 	 */
12973 	dipif->ipif_seqid = sipif->ipif_seqid;
12974 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
12975 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
12976 	dipif->ipif_state_flags = sipif->ipif_state_flags;
12977 }
12978 
12979 /*
12980  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
12981  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
12982  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
12983  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
12984  * down (i.e., no DAD), of the same type, and unlocked.
12985  */
12986 static void
12987 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
12988 {
12989 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
12990 	ipxop_t *ipx = ipsq->ipsq_xop;
12991 
12992 	ASSERT(sipif != dipif);
12993 	ASSERT(sipif != virgipif);
12994 
12995 	/*
12996 	 * Grab all of the locks that protect the ipif in a defined order.
12997 	 */
12998 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
12999 	if (sipif > dipif) {
13000 		mutex_enter(&sipif->ipif_saved_ire_lock);
13001 		mutex_enter(&dipif->ipif_saved_ire_lock);
13002 	} else {
13003 		mutex_enter(&dipif->ipif_saved_ire_lock);
13004 		mutex_enter(&sipif->ipif_saved_ire_lock);
13005 	}
13006 
13007 	ipif_clone(sipif, dipif);
13008 	if (virgipif != NULL) {
13009 		ipif_clone(virgipif, sipif);
13010 		mi_free(virgipif);
13011 	}
13012 
13013 	mutex_exit(&sipif->ipif_saved_ire_lock);
13014 	mutex_exit(&dipif->ipif_saved_ire_lock);
13015 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13016 
13017 	/*
13018 	 * Transfer ownership of the current xop, if necessary.
13019 	 */
13020 	if (ipx->ipx_current_ipif == sipif) {
13021 		ASSERT(ipx->ipx_pending_ipif == NULL);
13022 		mutex_enter(&ipx->ipx_lock);
13023 		ipx->ipx_current_ipif = dipif;
13024 		mutex_exit(&ipx->ipx_lock);
13025 	}
13026 
13027 	if (virgipif == NULL)
13028 		mi_free(sipif);
13029 }
13030 
13031 /*
13032  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13033  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13034  * be inserted into the first space available in the list. The value of
13035  * ipif_id will then be set to the appropriate value for its position.
13036  */
13037 static int
13038 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13039 {
13040 	ill_t *ill;
13041 	ipif_t *tipif;
13042 	ipif_t **tipifp;
13043 	int id;
13044 	ip_stack_t	*ipst;
13045 
13046 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13047 	    IAM_WRITER_IPIF(ipif));
13048 
13049 	ill = ipif->ipif_ill;
13050 	ASSERT(ill != NULL);
13051 	ipst = ill->ill_ipst;
13052 
13053 	/*
13054 	 * In the case of lo0:0 we already hold the ill_g_lock.
13055 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13056 	 * ipif_insert.
13057 	 */
13058 	if (acquire_g_lock)
13059 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13060 	mutex_enter(&ill->ill_lock);
13061 	id = ipif->ipif_id;
13062 	tipifp = &(ill->ill_ipif);
13063 	if (id == -1) {	/* need to find a real id */
13064 		id = 0;
13065 		while ((tipif = *tipifp) != NULL) {
13066 			ASSERT(tipif->ipif_id >= id);
13067 			if (tipif->ipif_id != id)
13068 				break; /* non-consecutive id */
13069 			id++;
13070 			tipifp = &(tipif->ipif_next);
13071 		}
13072 		/* limit number of logical interfaces */
13073 		if (id >= ipst->ips_ip_addrs_per_if) {
13074 			mutex_exit(&ill->ill_lock);
13075 			if (acquire_g_lock)
13076 				rw_exit(&ipst->ips_ill_g_lock);
13077 			return (-1);
13078 		}
13079 		ipif->ipif_id = id; /* assign new id */
13080 	} else if (id < ipst->ips_ip_addrs_per_if) {
13081 		/* we have a real id; insert ipif in the right place */
13082 		while ((tipif = *tipifp) != NULL) {
13083 			ASSERT(tipif->ipif_id != id);
13084 			if (tipif->ipif_id > id)
13085 				break; /* found correct location */
13086 			tipifp = &(tipif->ipif_next);
13087 		}
13088 	} else {
13089 		mutex_exit(&ill->ill_lock);
13090 		if (acquire_g_lock)
13091 			rw_exit(&ipst->ips_ill_g_lock);
13092 		return (-1);
13093 	}
13094 
13095 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13096 
13097 	ipif->ipif_next = tipif;
13098 	*tipifp = ipif;
13099 	mutex_exit(&ill->ill_lock);
13100 	if (acquire_g_lock)
13101 		rw_exit(&ipst->ips_ill_g_lock);
13102 
13103 	return (0);
13104 }
13105 
13106 static void
13107 ipif_remove(ipif_t *ipif)
13108 {
13109 	ipif_t	**ipifp;
13110 	ill_t	*ill = ipif->ipif_ill;
13111 
13112 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13113 
13114 	mutex_enter(&ill->ill_lock);
13115 	ipifp = &ill->ill_ipif;
13116 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13117 		if (*ipifp == ipif) {
13118 			*ipifp = ipif->ipif_next;
13119 			break;
13120 		}
13121 	}
13122 	mutex_exit(&ill->ill_lock);
13123 }
13124 
13125 /*
13126  * Allocate and initialize a new interface control structure.  (Always
13127  * called as writer.)
13128  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13129  * is not part of the global linked list of ills. ipif_seqid is unique
13130  * in the system and to preserve the uniqueness, it is assigned only
13131  * when ill becomes part of the global list. At that point ill will
13132  * have a name. If it doesn't get assigned here, it will get assigned
13133  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13134  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13135  * the interface flags or any other information from the DL_INFO_ACK for
13136  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13137  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13138  * second DL_INFO_ACK comes in from the driver.
13139  */
13140 static ipif_t *
13141 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13142     boolean_t insert)
13143 {
13144 	ipif_t	*ipif;
13145 	ip_stack_t *ipst = ill->ill_ipst;
13146 
13147 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13148 	    ill->ill_name, id, (void *)ill));
13149 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13150 
13151 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13152 		return (NULL);
13153 	*ipif = ipif_zero;	/* start clean */
13154 
13155 	ipif->ipif_ill = ill;
13156 	ipif->ipif_id = id;	/* could be -1 */
13157 	/*
13158 	 * Inherit the zoneid from the ill; for the shared stack instance
13159 	 * this is always the global zone
13160 	 */
13161 	ipif->ipif_zoneid = ill->ill_zoneid;
13162 
13163 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13164 
13165 	ipif->ipif_refcnt = 0;
13166 	ipif->ipif_saved_ire_cnt = 0;
13167 
13168 	if (insert) {
13169 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13170 			mi_free(ipif);
13171 			return (NULL);
13172 		}
13173 		/* -1 id should have been replaced by real id */
13174 		id = ipif->ipif_id;
13175 		ASSERT(id >= 0);
13176 	}
13177 
13178 	if (ill->ill_name[0] != '\0')
13179 		ipif_assign_seqid(ipif);
13180 
13181 	/*
13182 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
13183 	 * (which must not exist yet because the zeroth ipif is created once
13184 	 * per ill).  However, do not not link it to the ipmp_grp_t until
13185 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
13186 	 */
13187 	if (id == 0 && IS_IPMP(ill)) {
13188 		if (ipmp_illgrp_create(ill) == NULL) {
13189 			if (insert) {
13190 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13191 				ipif_remove(ipif);
13192 				rw_exit(&ipst->ips_ill_g_lock);
13193 			}
13194 			mi_free(ipif);
13195 			return (NULL);
13196 		}
13197 	}
13198 
13199 	/*
13200 	 * We grab ill_lock to protect the flag changes.  The ipif is still
13201 	 * not up and can't be looked up until the ioctl completes and the
13202 	 * IPIF_CHANGING flag is cleared.
13203 	 */
13204 	mutex_enter(&ill->ill_lock);
13205 
13206 	ipif->ipif_ire_type = ire_type;
13207 
13208 	if (ipif->ipif_isv6) {
13209 		ill->ill_flags |= ILLF_IPV6;
13210 	} else {
13211 		ipaddr_t inaddr_any = INADDR_ANY;
13212 
13213 		ill->ill_flags |= ILLF_IPV4;
13214 
13215 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13216 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13217 		    &ipif->ipif_v6lcl_addr);
13218 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13219 		    &ipif->ipif_v6src_addr);
13220 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13221 		    &ipif->ipif_v6subnet);
13222 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13223 		    &ipif->ipif_v6net_mask);
13224 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13225 		    &ipif->ipif_v6brd_addr);
13226 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13227 		    &ipif->ipif_v6pp_dst_addr);
13228 	}
13229 
13230 	/*
13231 	 * Don't set the interface flags etc. now, will do it in
13232 	 * ip_ll_subnet_defaults.
13233 	 */
13234 	if (!initialize)
13235 		goto out;
13236 
13237 	ipif->ipif_mtu = ill->ill_max_mtu;
13238 
13239 	/*
13240 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13241 	 * with no underlying interfaces (and thus an unknown broadcast
13242 	 * address length), but all interfaces that can be placed into an IPMP
13243 	 * group are required to be broadcast-capable.
13244 	 */
13245 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13246 		/*
13247 		 * Later detect lack of DLPI driver multicast capability by
13248 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
13249 		 */
13250 		ill->ill_flags |= ILLF_MULTICAST;
13251 		if (!ipif->ipif_isv6)
13252 			ipif->ipif_flags |= IPIF_BROADCAST;
13253 	} else {
13254 		if (ill->ill_net_type != IRE_LOOPBACK) {
13255 			if (ipif->ipif_isv6)
13256 				/*
13257 				 * Note: xresolv interfaces will eventually need
13258 				 * NOARP set here as well, but that will require
13259 				 * those external resolvers to have some
13260 				 * knowledge of that flag and act appropriately.
13261 				 * Not to be changed at present.
13262 				 */
13263 				ill->ill_flags |= ILLF_NONUD;
13264 			else
13265 				ill->ill_flags |= ILLF_NOARP;
13266 		}
13267 		if (ill->ill_phys_addr_length == 0) {
13268 			if (IS_VNI(ill)) {
13269 				ipif->ipif_flags |= IPIF_NOXMIT;
13270 			} else {
13271 				/* pt-pt supports multicast. */
13272 				ill->ill_flags |= ILLF_MULTICAST;
13273 				if (ill->ill_net_type != IRE_LOOPBACK)
13274 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13275 			}
13276 		}
13277 	}
13278 out:
13279 	mutex_exit(&ill->ill_lock);
13280 	return (ipif);
13281 }
13282 
13283 /*
13284  * If appropriate, send a message up to the resolver delete the entry
13285  * for the address of this interface which is going out of business.
13286  * (Always called as writer).
13287  *
13288  * NOTE : We need to check for NULL mps as some of the fields are
13289  *	  initialized only for some interface types. See ipif_resolver_up()
13290  *	  for details.
13291  */
13292 void
13293 ipif_resolver_down(ipif_t *ipif)
13294 {
13295 	mblk_t	*mp;
13296 	ill_t	*ill = ipif->ipif_ill;
13297 
13298 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13299 	ASSERT(IAM_WRITER_IPIF(ipif));
13300 
13301 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13302 		return;
13303 
13304 	/* Delete the mapping for the local address */
13305 	mp = ipif->ipif_arp_del_mp;
13306 	if (mp != NULL) {
13307 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13308 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13309 		putnext(ill->ill_rq, mp);
13310 		ipif->ipif_arp_del_mp = NULL;
13311 	}
13312 
13313 	/*
13314 	 * Make IPMP aware of the deleted data address.
13315 	 */
13316 	if (IS_IPMP(ill))
13317 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13318 
13319 	/*
13320 	 * If this is the last ipif that is going down and there are no
13321 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13322 	 * clean up ARP completely.
13323 	 */
13324 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13325 		/*
13326 		 * If this was the last ipif on an IPMP interface, purge any
13327 		 * IPMP ARP entries associated with it.
13328 		 */
13329 		if (IS_IPMP(ill))
13330 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13331 
13332 		/* Send up AR_INTERFACE_DOWN message */
13333 		mp = ill->ill_arp_down_mp;
13334 		if (mp != NULL) {
13335 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13336 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13337 			    ipif->ipif_id));
13338 			putnext(ill->ill_rq, mp);
13339 			ill->ill_arp_down_mp = NULL;
13340 		}
13341 
13342 		/* Tell ARP to delete the multicast mappings */
13343 		mp = ill->ill_arp_del_mapping_mp;
13344 		if (mp != NULL) {
13345 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13346 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13347 			    ipif->ipif_id));
13348 			putnext(ill->ill_rq, mp);
13349 			ill->ill_arp_del_mapping_mp = NULL;
13350 		}
13351 	}
13352 }
13353 
13354 /*
13355  * Set up the multicast mappings for `ipif' in ARP.  If `arp_add_mapping_mp'
13356  * is non-NULL, then upon success it will contain an mblk that can be passed
13357  * to ARP to create the mapping.  Otherwise, if it's NULL, upon success ARP
13358  * will have already been notified to create the mapping.  Returns zero on
13359  * success, -1 upon failure.
13360  */
13361 int
13362 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13363 {
13364 	mblk_t	*del_mp = NULL;
13365 	mblk_t *add_mp = NULL;
13366 	mblk_t *mp;
13367 	ill_t	*ill = ipif->ipif_ill;
13368 	phyint_t *phyi = ill->ill_phyint;
13369 	ipaddr_t addr, mask, extract_mask = 0;
13370 	arma_t	*arma;
13371 	uint8_t *maddr, *bphys_addr;
13372 	uint32_t hw_start;
13373 	dl_unitdata_req_t *dlur;
13374 
13375 	ASSERT(IAM_WRITER_IPIF(ipif));
13376 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13377 		return (0);
13378 
13379 	/*
13380 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13381 	 * and instead use the ones on the underlying interfaces.
13382 	 */
13383 	if (IS_IPMP(ill))
13384 		return (0);
13385 
13386 	/*
13387 	 * Delete the existing mapping from ARP.  Normally, ipif_down() ->
13388 	 * ipif_resolver_down() will send this up to ARP, but it may be that
13389 	 * we are enabling PHYI_MULTI_BCAST via ip_rput_dlpi_writer().
13390 	 */
13391 	mp = ill->ill_arp_del_mapping_mp;
13392 	if (mp != NULL) {
13393 		ip1dbg(("ipif_arp_setup_multicast: arp cmd %x for %s:%u\n",
13394 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13395 		putnext(ill->ill_rq, mp);
13396 		ill->ill_arp_del_mapping_mp = NULL;
13397 	}
13398 
13399 	if (arp_add_mapping_mp != NULL)
13400 		*arp_add_mapping_mp = NULL;
13401 
13402 	/*
13403 	 * Check that the address is not to long for the constant
13404 	 * length reserved in the template arma_t.
13405 	 */
13406 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13407 		return (-1);
13408 
13409 	/* Add mapping mblk */
13410 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13411 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13412 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13413 	    (caddr_t)&addr);
13414 	if (add_mp == NULL)
13415 		return (-1);
13416 	arma = (arma_t *)add_mp->b_rptr;
13417 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13418 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13419 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13420 
13421 	/*
13422 	 * Determine the broadcast address.
13423 	 */
13424 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13425 	if (ill->ill_sap_length < 0)
13426 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13427 	else
13428 		bphys_addr = (uchar_t *)dlur +
13429 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13430 	/*
13431 	 * Check PHYI_MULTI_BCAST and length of physical
13432 	 * address to determine if we use the mapping or the
13433 	 * broadcast address.
13434 	 */
13435 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13436 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13437 		    bphys_addr, maddr, &hw_start, &extract_mask))
13438 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13439 
13440 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13441 	    (ill->ill_flags & ILLF_MULTICAST)) {
13442 		/* Make sure this will not match the "exact" entry. */
13443 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13444 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13445 		    (caddr_t)&addr);
13446 		if (del_mp == NULL) {
13447 			freemsg(add_mp);
13448 			return (-1);
13449 		}
13450 		bcopy(&extract_mask, (char *)arma +
13451 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13452 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13453 			/* Use link-layer broadcast address for MULTI_BCAST */
13454 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13455 			ip2dbg(("ipif_arp_setup_multicast: adding"
13456 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13457 		} else {
13458 			arma->arma_hw_mapping_start = hw_start;
13459 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13460 			    " ARP setup for %s\n", ill->ill_name));
13461 		}
13462 	} else {
13463 		freemsg(add_mp);
13464 		ASSERT(del_mp == NULL);
13465 		/* It is neither MULTICAST nor MULTI_BCAST */
13466 		return (0);
13467 	}
13468 	ASSERT(add_mp != NULL && del_mp != NULL);
13469 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13470 	ill->ill_arp_del_mapping_mp = del_mp;
13471 	if (arp_add_mapping_mp != NULL) {
13472 		/* The caller just wants the mblks allocated */
13473 		*arp_add_mapping_mp = add_mp;
13474 	} else {
13475 		/* The caller wants us to send it to arp */
13476 		putnext(ill->ill_rq, add_mp);
13477 	}
13478 	return (0);
13479 }
13480 
13481 /*
13482  * Get the resolver set up for a new IP address.  (Always called as writer.)
13483  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13484  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13485  *
13486  * The enumerated value res_act tunes the behavior:
13487  * 	* Res_act_initial: set up all the resolver structures for a new
13488  *	  IP address.
13489  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13490  *	  ARP message in defense of the address.
13491  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13492  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13493  *
13494  * Returns zero on success, or an errno upon failure.
13495  */
13496 int
13497 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13498 {
13499 	mblk_t	*arp_up_mp = NULL;
13500 	mblk_t	*arp_down_mp = NULL;
13501 	mblk_t	*arp_add_mp = NULL;
13502 	mblk_t	*arp_del_mp = NULL;
13503 	mblk_t	*arp_add_mapping_mp = NULL;
13504 	mblk_t	*arp_del_mapping_mp = NULL;
13505 	ill_t	*ill = ipif->ipif_ill;
13506 	int	err = ENOMEM;
13507 	boolean_t added_ipif = B_FALSE;
13508 	boolean_t publish;
13509 	boolean_t was_dup;
13510 
13511 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13512 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13513 	ASSERT(IAM_WRITER_IPIF(ipif));
13514 
13515 	was_dup = B_FALSE;
13516 	if (res_act == Res_act_initial) {
13517 		ipif->ipif_addr_ready = 0;
13518 		/*
13519 		 * We're bringing an interface up here.  There's no way that we
13520 		 * should need to shut down ARP now.
13521 		 */
13522 		mutex_enter(&ill->ill_lock);
13523 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13524 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13525 			ill->ill_ipif_dup_count--;
13526 			was_dup = B_TRUE;
13527 		}
13528 		mutex_exit(&ill->ill_lock);
13529 	}
13530 	if (ipif->ipif_recovery_id != 0)
13531 		(void) untimeout(ipif->ipif_recovery_id);
13532 	ipif->ipif_recovery_id = 0;
13533 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13534 		ipif->ipif_addr_ready = 1;
13535 		return (0);
13536 	}
13537 	/* NDP will set the ipif_addr_ready flag when it's ready */
13538 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13539 		return (0);
13540 
13541 	if (ill->ill_isv6) {
13542 		/*
13543 		 * External resolver for IPv6
13544 		 */
13545 		ASSERT(res_act == Res_act_initial);
13546 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13547 	} else {
13548 		/*
13549 		 * IPv4 arp case. If the ARP stream has already started
13550 		 * closing, fail this request for ARP bringup. Else
13551 		 * record the fact that an ARP bringup is pending.
13552 		 */
13553 		mutex_enter(&ill->ill_lock);
13554 		if (ill->ill_arp_closing) {
13555 			mutex_exit(&ill->ill_lock);
13556 			err = EINVAL;
13557 			goto failed;
13558 		} else {
13559 			if (ill->ill_ipif_up_count == 0 &&
13560 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13561 				ill->ill_arp_bringup_pending = 1;
13562 			mutex_exit(&ill->ill_lock);
13563 		}
13564 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13565 	}
13566 
13567 	if (IS_IPMP(ill) && publish) {
13568 		/*
13569 		 * If we're here via ipif_up(), then the ipif won't be bound
13570 		 * yet -- add it to the group, which will bind it if possible.
13571 		 * (We would add it in ipif_up(), but deleting on failure
13572 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13573 		 * then the ipif has already been added to the group and we
13574 		 * just need to use the binding.
13575 		 */
13576 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13577 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13578 				/*
13579 				 * We couldn't bind the ipif to an ill yet,
13580 				 * so we have nothing to publish.
13581 				 */
13582 				publish = B_FALSE;
13583 			}
13584 			added_ipif = B_TRUE;
13585 		}
13586 	}
13587 
13588 	/*
13589 	 * Add an entry for the local address in ARP only if it
13590 	 * is not UNNUMBERED and it is suitable for publishing.
13591 	 */
13592 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13593 		if (res_act == Res_act_defend) {
13594 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13595 			if (arp_add_mp == NULL)
13596 				goto failed;
13597 			/*
13598 			 * If we're just defending our address now, then
13599 			 * there's no need to set up ARP multicast mappings.
13600 			 * The publish command is enough.
13601 			 */
13602 			goto done;
13603 		}
13604 
13605 		/*
13606 		 * Allocate an ARP add message and an ARP delete message (the
13607 		 * latter is saved for use when the address goes down).
13608 		 */
13609 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13610 			goto failed;
13611 
13612 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13613 			goto failed;
13614 
13615 		if (res_act != Res_act_initial)
13616 			goto arp_setup_multicast;
13617 	} else {
13618 		if (res_act != Res_act_initial)
13619 			goto done;
13620 	}
13621 	/*
13622 	 * Need to bring up ARP or setup multicast mapping only
13623 	 * when the first interface is coming UP.
13624 	 */
13625 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13626 		goto done;
13627 
13628 	/*
13629 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13630 	 */
13631 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13632 	if (arp_down_mp == NULL)
13633 		goto failed;
13634 
13635 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13636 	if (arp_up_mp == NULL)
13637 		goto failed;
13638 
13639 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13640 		goto done;
13641 
13642 arp_setup_multicast:
13643 	/*
13644 	 * Setup the multicast mappings. This function initializes
13645 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13646 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13647 	 */
13648 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13649 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13650 		if (err != 0)
13651 			goto failed;
13652 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13653 		ASSERT(arp_add_mapping_mp != NULL);
13654 	}
13655 done:
13656 	if (arp_up_mp != NULL) {
13657 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13658 		    ill->ill_name, ipif->ipif_id));
13659 		putnext(ill->ill_rq, arp_up_mp);
13660 		arp_up_mp = NULL;
13661 	}
13662 	if (arp_add_mp != NULL) {
13663 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13664 		    ill->ill_name, ipif->ipif_id));
13665 		/*
13666 		 * If it's an extended ARP implementation, then we'll wait to
13667 		 * hear that DAD has finished before using the interface.
13668 		 */
13669 		if (!ill->ill_arp_extend)
13670 			ipif->ipif_addr_ready = 1;
13671 		putnext(ill->ill_rq, arp_add_mp);
13672 		arp_add_mp = NULL;
13673 	} else {
13674 		ipif->ipif_addr_ready = 1;
13675 	}
13676 	if (arp_add_mapping_mp != NULL) {
13677 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13678 		    ill->ill_name, ipif->ipif_id));
13679 		putnext(ill->ill_rq, arp_add_mapping_mp);
13680 		arp_add_mapping_mp = NULL;
13681 	}
13682 
13683 	if (res_act == Res_act_initial) {
13684 		if (ill->ill_flags & ILLF_NOARP)
13685 			err = ill_arp_off(ill);
13686 		else
13687 			err = ill_arp_on(ill);
13688 		if (err != 0) {
13689 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13690 			    err));
13691 			goto failed;
13692 		}
13693 	}
13694 
13695 	if (arp_del_mp != NULL) {
13696 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13697 		ipif->ipif_arp_del_mp = arp_del_mp;
13698 	}
13699 	if (arp_down_mp != NULL) {
13700 		ASSERT(ill->ill_arp_down_mp == NULL);
13701 		ill->ill_arp_down_mp = arp_down_mp;
13702 	}
13703 	if (arp_del_mapping_mp != NULL) {
13704 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13705 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13706 	}
13707 
13708 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13709 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13710 failed:
13711 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13712 	if (added_ipif)
13713 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13714 	freemsg(arp_add_mp);
13715 	freemsg(arp_del_mp);
13716 	freemsg(arp_add_mapping_mp);
13717 	freemsg(arp_up_mp);
13718 	freemsg(arp_down_mp);
13719 	ill->ill_arp_bringup_pending = 0;
13720 	return (err);
13721 }
13722 
13723 /*
13724  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13725  * just gone back up.
13726  */
13727 static void
13728 ipif_arp_start_dad(ipif_t *ipif)
13729 {
13730 	ill_t *ill = ipif->ipif_ill;
13731 	mblk_t *arp_add_mp;
13732 
13733 	/* ACE_F_UNVERIFIED restarts DAD */
13734 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13735 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13736 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13737 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13738 		/*
13739 		 * If we can't contact ARP for some reason, that's not really a
13740 		 * problem.  Just send out the routing socket notification that
13741 		 * DAD completion would have done, and continue.
13742 		 */
13743 		ipif_mask_reply(ipif);
13744 		ipif_up_notify(ipif);
13745 		ipif->ipif_addr_ready = 1;
13746 		return;
13747 	}
13748 
13749 	putnext(ill->ill_rq, arp_add_mp);
13750 }
13751 
13752 static void
13753 ipif_ndp_start_dad(ipif_t *ipif)
13754 {
13755 	nce_t *nce;
13756 
13757 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13758 	    B_FALSE);
13759 	if (nce == NULL)
13760 		return;
13761 
13762 	if (!ndp_restart_dad(nce)) {
13763 		/*
13764 		 * If we can't restart DAD for some reason, that's not really a
13765 		 * problem.  Just send out the routing socket notification that
13766 		 * DAD completion would have done, and continue.
13767 		 */
13768 		ipif_up_notify(ipif);
13769 		ipif->ipif_addr_ready = 1;
13770 	}
13771 	NCE_REFRELE(nce);
13772 }
13773 
13774 /*
13775  * Restart duplicate address detection on all interfaces on the given ill.
13776  *
13777  * This is called when an interface transitions from down to up
13778  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13779  *
13780  * Note that since the underlying physical link has transitioned, we must cause
13781  * at least one routing socket message to be sent here, either via DAD
13782  * completion or just by default on the first ipif.  (If we don't do this, then
13783  * in.mpathd will see long delays when doing link-based failure recovery.)
13784  */
13785 void
13786 ill_restart_dad(ill_t *ill, boolean_t went_up)
13787 {
13788 	ipif_t *ipif;
13789 
13790 	if (ill == NULL)
13791 		return;
13792 
13793 	/*
13794 	 * If layer two doesn't support duplicate address detection, then just
13795 	 * send the routing socket message now and be done with it.
13796 	 */
13797 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13798 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13799 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13800 		return;
13801 	}
13802 
13803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13804 		if (went_up) {
13805 			if (ipif->ipif_flags & IPIF_UP) {
13806 				if (ill->ill_isv6)
13807 					ipif_ndp_start_dad(ipif);
13808 				else
13809 					ipif_arp_start_dad(ipif);
13810 			} else if (ill->ill_isv6 &&
13811 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13812 				/*
13813 				 * For IPv4, the ARP module itself will
13814 				 * automatically start the DAD process when it
13815 				 * sees DL_NOTE_LINK_UP.  We respond to the
13816 				 * AR_CN_READY at the completion of that task.
13817 				 * For IPv6, we must kick off the bring-up
13818 				 * process now.
13819 				 */
13820 				ndp_do_recovery(ipif);
13821 			} else {
13822 				/*
13823 				 * Unfortunately, the first ipif is "special"
13824 				 * and represents the underlying ill in the
13825 				 * routing socket messages.  Thus, when this
13826 				 * one ipif is down, we must still notify so
13827 				 * that the user knows the IFF_RUNNING status
13828 				 * change.  (If the first ipif is up, then
13829 				 * we'll handle eventual routing socket
13830 				 * notification via DAD completion.)
13831 				 */
13832 				if (ipif == ill->ill_ipif) {
13833 					ip_rts_ifmsg(ill->ill_ipif,
13834 					    RTSQ_DEFAULT);
13835 				}
13836 			}
13837 		} else {
13838 			/*
13839 			 * After link down, we'll need to send a new routing
13840 			 * message when the link comes back, so clear
13841 			 * ipif_addr_ready.
13842 			 */
13843 			ipif->ipif_addr_ready = 0;
13844 		}
13845 	}
13846 
13847 	/*
13848 	 * If we've torn down links, then notify the user right away.
13849 	 */
13850 	if (!went_up)
13851 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13852 }
13853 
13854 static void
13855 ipsq_delete(ipsq_t *ipsq)
13856 {
13857 	ipxop_t *ipx = ipsq->ipsq_xop;
13858 
13859 	ipsq->ipsq_ipst = NULL;
13860 	ASSERT(ipsq->ipsq_phyint == NULL);
13861 	ASSERT(ipsq->ipsq_xop != NULL);
13862 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
13863 	ASSERT(ipx->ipx_pending_mp == NULL);
13864 	kmem_free(ipsq, sizeof (ipsq_t));
13865 }
13866 
13867 static int
13868 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
13869 {
13870 	int err;
13871 	ipif_t *ipif;
13872 
13873 	if (ill == NULL)
13874 		return (0);
13875 
13876 	ASSERT(IAM_WRITER_ILL(ill));
13877 	ill->ill_up_ipifs = B_TRUE;
13878 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13879 		if (ipif->ipif_was_up) {
13880 			if (!(ipif->ipif_flags & IPIF_UP))
13881 				err = ipif_up(ipif, q, mp);
13882 			ipif->ipif_was_up = B_FALSE;
13883 			if (err != 0) {
13884 				ASSERT(err == EINPROGRESS);
13885 				return (err);
13886 			}
13887 		}
13888 	}
13889 	mutex_enter(&ill->ill_lock);
13890 	ill->ill_state_flags &= ~ILL_CHANGING;
13891 	mutex_exit(&ill->ill_lock);
13892 	ill->ill_up_ipifs = B_FALSE;
13893 	return (0);
13894 }
13895 
13896 /*
13897  * This function is called to bring up all the ipifs that were up before
13898  * bringing the ill down via ill_down_ipifs().
13899  */
13900 int
13901 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13902 {
13903 	int err;
13904 
13905 	ASSERT(IAM_WRITER_ILL(ill));
13906 
13907 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
13908 	if (err != 0)
13909 		return (err);
13910 
13911 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
13912 }
13913 
13914 /*
13915  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
13916  * down the ipifs without sending DL_UNBIND_REQ to the driver.
13917  */
13918 static void
13919 ill_down_ipifs(ill_t *ill, boolean_t logical)
13920 {
13921 	ipif_t *ipif;
13922 
13923 	ASSERT(IAM_WRITER_ILL(ill));
13924 
13925 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13926 		/*
13927 		 * We go through the ipif_down logic even if the ipif
13928 		 * is already down, since routes can be added based
13929 		 * on down ipifs. Going through ipif_down once again
13930 		 * will delete any IREs created based on these routes.
13931 		 */
13932 		if (ipif->ipif_flags & IPIF_UP)
13933 			ipif->ipif_was_up = B_TRUE;
13934 
13935 		/*
13936 		 * Need to re-create net/subnet bcast ires if
13937 		 * they are dependent on ipif.
13938 		 */
13939 		if (!ipif->ipif_isv6)
13940 			ipif_check_bcast_ires(ipif);
13941 		if (logical) {
13942 			(void) ipif_logical_down(ipif, NULL, NULL);
13943 			ipif_non_duplicate(ipif);
13944 			ipif_down_tail(ipif);
13945 		} else {
13946 			(void) ipif_down(ipif, NULL, NULL);
13947 		}
13948 	}
13949 }
13950 
13951 /*
13952  * Redo source address selection.  This is called when a
13953  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
13954  */
13955 void
13956 ill_update_source_selection(ill_t *ill)
13957 {
13958 	ipif_t *ipif;
13959 
13960 	ASSERT(IAM_WRITER_ILL(ill));
13961 
13962 	/*
13963 	 * Underlying interfaces are only used for test traffic and thus
13964 	 * should always send with their (deprecated) source addresses.
13965 	 */
13966 	if (IS_UNDER_IPMP(ill))
13967 		return;
13968 
13969 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13970 		if (ill->ill_isv6)
13971 			ipif_recreate_interface_routes_v6(NULL, ipif);
13972 		else
13973 			ipif_recreate_interface_routes(NULL, ipif);
13974 	}
13975 }
13976 
13977 /*
13978  * Finish the group join started in ip_sioctl_groupname().
13979  */
13980 /* ARGSUSED */
13981 static void
13982 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
13983 {
13984 	ill_t		*ill = q->q_ptr;
13985 	phyint_t	*phyi = ill->ill_phyint;
13986 	ipmp_grp_t	*grp = phyi->phyint_grp;
13987 	ip_stack_t	*ipst = ill->ill_ipst;
13988 
13989 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
13990 	ASSERT(!IS_IPMP(ill) && grp != NULL);
13991 	ASSERT(IAM_WRITER_IPSQ(ipsq));
13992 
13993 	if (phyi->phyint_illv4 != NULL) {
13994 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
13995 		VERIFY(grp->gr_pendv4-- > 0);
13996 		rw_exit(&ipst->ips_ipmp_lock);
13997 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
13998 	}
13999 	if (phyi->phyint_illv6 != NULL) {
14000 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14001 		VERIFY(grp->gr_pendv6-- > 0);
14002 		rw_exit(&ipst->ips_ipmp_lock);
14003 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14004 	}
14005 	freemsg(mp);
14006 }
14007 
14008 /*
14009  * Process an SIOCSLIFGROUPNAME request.
14010  */
14011 /* ARGSUSED */
14012 int
14013 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14014     ip_ioctl_cmd_t *ipip, void *ifreq)
14015 {
14016 	struct lifreq	*lifr = ifreq;
14017 	ill_t		*ill = ipif->ipif_ill;
14018 	ip_stack_t	*ipst = ill->ill_ipst;
14019 	phyint_t	*phyi = ill->ill_phyint;
14020 	ipmp_grp_t	*grp = phyi->phyint_grp;
14021 	mblk_t		*ipsq_mp;
14022 	int		err = 0;
14023 
14024 	/*
14025 	 * Note that phyint_grp can only change here, where we're exclusive.
14026 	 */
14027 	ASSERT(IAM_WRITER_ILL(ill));
14028 
14029 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14030 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14031 		return (EINVAL);
14032 
14033 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14034 
14035 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14036 
14037 	/*
14038 	 * If the name hasn't changed, there's nothing to do.
14039 	 */
14040 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14041 		goto unlock;
14042 
14043 	/*
14044 	 * Handle requests to rename an IPMP meta-interface.
14045 	 *
14046 	 * Note that creation of the IPMP meta-interface is handled in
14047 	 * userland through the standard plumbing sequence.  As part of the
14048 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14049 	 * the name of the interface (see ipif_set_values_tail()).
14050 	 */
14051 	if (IS_IPMP(ill)) {
14052 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14053 		goto unlock;
14054 	}
14055 
14056 	/*
14057 	 * Handle requests to add or remove an IP interface from a group.
14058 	 */
14059 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14060 		/*
14061 		 * Moves are handled by first removing the interface from
14062 		 * its existing group, and then adding it to another group.
14063 		 * So, fail if it's already in a group.
14064 		 */
14065 		if (IS_UNDER_IPMP(ill)) {
14066 			err = EALREADY;
14067 			goto unlock;
14068 		}
14069 
14070 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14071 		if (grp == NULL) {
14072 			err = ENOENT;
14073 			goto unlock;
14074 		}
14075 
14076 		/*
14077 		 * Check if the phyint and its ills are suitable for
14078 		 * inclusion into the group.
14079 		 */
14080 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14081 			goto unlock;
14082 
14083 		/*
14084 		 * Checks pass; join the group, and enqueue the remaining
14085 		 * illgrp joins for when we've become part of the group xop
14086 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14087 		 * requires an mblk_t to scribble on, and since `mp' will be
14088 		 * freed as part of completing the ioctl, allocate another.
14089 		 */
14090 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14091 			err = ENOMEM;
14092 			goto unlock;
14093 		}
14094 
14095 		/*
14096 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14097 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14098 		 * before ip_join_illgrps() is called back.  See the comments
14099 		 * in ip_sioctl_plink_ipmp() for more.
14100 		 */
14101 		if (phyi->phyint_illv4 != NULL)
14102 			grp->gr_pendv4++;
14103 		if (phyi->phyint_illv6 != NULL)
14104 			grp->gr_pendv6++;
14105 
14106 		rw_exit(&ipst->ips_ipmp_lock);
14107 
14108 		ipmp_phyint_join_grp(phyi, grp);
14109 		ill_refhold(ill);
14110 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14111 		    SWITCH_OP, B_FALSE);
14112 		return (0);
14113 	} else {
14114 		/*
14115 		 * Request to remove the interface from a group.  If the
14116 		 * interface is not in a group, this trivially succeeds.
14117 		 */
14118 		rw_exit(&ipst->ips_ipmp_lock);
14119 		if (IS_UNDER_IPMP(ill))
14120 			ipmp_phyint_leave_grp(phyi);
14121 		return (0);
14122 	}
14123 unlock:
14124 	rw_exit(&ipst->ips_ipmp_lock);
14125 	return (err);
14126 }
14127 
14128 /*
14129  * Process an SIOCGLIFBINDING request.
14130  */
14131 /* ARGSUSED */
14132 int
14133 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14134     ip_ioctl_cmd_t *ipip, void *ifreq)
14135 {
14136 	ill_t		*ill;
14137 	struct lifreq	*lifr = ifreq;
14138 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14139 
14140 	if (!IS_IPMP(ipif->ipif_ill))
14141 		return (EINVAL);
14142 
14143 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14144 	if ((ill = ipif->ipif_bound_ill) == NULL)
14145 		lifr->lifr_binding[0] = '\0';
14146 	else
14147 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
14148 	rw_exit(&ipst->ips_ipmp_lock);
14149 	return (0);
14150 }
14151 
14152 /*
14153  * Process an SIOCGLIFGROUPNAME request.
14154  */
14155 /* ARGSUSED */
14156 int
14157 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14158     ip_ioctl_cmd_t *ipip, void *ifreq)
14159 {
14160 	ipmp_grp_t	*grp;
14161 	struct lifreq	*lifr = ifreq;
14162 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14163 
14164 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14165 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14166 		lifr->lifr_groupname[0] = '\0';
14167 	else
14168 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14169 	rw_exit(&ipst->ips_ipmp_lock);
14170 	return (0);
14171 }
14172 
14173 /*
14174  * Process an SIOCGLIFGROUPINFO request.
14175  */
14176 /* ARGSUSED */
14177 int
14178 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14179     ip_ioctl_cmd_t *ipip, void *dummy)
14180 {
14181 	ipmp_grp_t	*grp;
14182 	lifgroupinfo_t	*lifgr;
14183 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14184 
14185 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14186 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14187 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14188 
14189 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14190 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14191 		rw_exit(&ipst->ips_ipmp_lock);
14192 		return (ENOENT);
14193 	}
14194 	ipmp_grp_info(grp, lifgr);
14195 	rw_exit(&ipst->ips_ipmp_lock);
14196 	return (0);
14197 }
14198 
14199 static void
14200 ill_dl_down(ill_t *ill)
14201 {
14202 	/*
14203 	 * The ill is down; unbind but stay attached since we're still
14204 	 * associated with a PPA. If we have negotiated DLPI capabilites
14205 	 * with the data link service provider (IDS_OK) then reset them.
14206 	 * The interval between unbinding and rebinding is potentially
14207 	 * unbounded hence we cannot assume things will be the same.
14208 	 * The DLPI capabilities will be probed again when the data link
14209 	 * is brought up.
14210 	 */
14211 	mblk_t	*mp = ill->ill_unbind_mp;
14212 
14213 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14214 
14215 	ill->ill_unbind_mp = NULL;
14216 	if (mp != NULL) {
14217 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14218 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14219 		    ill->ill_name));
14220 		mutex_enter(&ill->ill_lock);
14221 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14222 		mutex_exit(&ill->ill_lock);
14223 		/*
14224 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14225 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14226 		 * ill_capability_dld_disable disable rightaway. If this is not
14227 		 * an unplumb operation then the disable happens on receipt of
14228 		 * the capab ack via ip_rput_dlpi_writer ->
14229 		 * ill_capability_ack_thr. In both cases the order of
14230 		 * the operations seen by DLD is capability disable followed
14231 		 * by DL_UNBIND. Also the DLD capability disable needs a
14232 		 * cv_wait'able context.
14233 		 */
14234 		if (ill->ill_state_flags & ILL_CONDEMNED)
14235 			ill_capability_dld_disable(ill);
14236 		ill_capability_reset(ill, B_FALSE);
14237 		ill_dlpi_send(ill, mp);
14238 	}
14239 
14240 	/*
14241 	 * Toss all of our multicast memberships.  We could keep them, but
14242 	 * then we'd have to do bookkeeping of any joins and leaves performed
14243 	 * by the application while the the interface is down (we can't just
14244 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14245 	 * on a downed interface).
14246 	 */
14247 	ill_leave_multicast(ill);
14248 
14249 	mutex_enter(&ill->ill_lock);
14250 	ill->ill_dl_up = 0;
14251 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14252 	mutex_exit(&ill->ill_lock);
14253 }
14254 
14255 static void
14256 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14257 {
14258 	union DL_primitives *dlp;
14259 	t_uscalar_t prim;
14260 	boolean_t waitack = B_FALSE;
14261 
14262 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14263 
14264 	dlp = (union DL_primitives *)mp->b_rptr;
14265 	prim = dlp->dl_primitive;
14266 
14267 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14268 	    dl_primstr(prim), prim, ill->ill_name));
14269 
14270 	switch (prim) {
14271 	case DL_PHYS_ADDR_REQ:
14272 	{
14273 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14274 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14275 		break;
14276 	}
14277 	case DL_BIND_REQ:
14278 		mutex_enter(&ill->ill_lock);
14279 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14280 		mutex_exit(&ill->ill_lock);
14281 		break;
14282 	}
14283 
14284 	/*
14285 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14286 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14287 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14288 	 */
14289 	mutex_enter(&ill->ill_lock);
14290 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
14291 	    (prim == DL_UNBIND_REQ)) {
14292 		ill->ill_dlpi_pending = prim;
14293 		waitack = B_TRUE;
14294 	}
14295 
14296 	mutex_exit(&ill->ill_lock);
14297 	putnext(ill->ill_wq, mp);
14298 
14299 	/*
14300 	 * There is no ack for DL_NOTIFY_CONF messages
14301 	 */
14302 	if (waitack && prim == DL_NOTIFY_CONF)
14303 		ill_dlpi_done(ill, prim);
14304 }
14305 
14306 /*
14307  * Helper function for ill_dlpi_send().
14308  */
14309 /* ARGSUSED */
14310 static void
14311 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14312 {
14313 	ill_dlpi_send(q->q_ptr, mp);
14314 }
14315 
14316 /*
14317  * Send a DLPI control message to the driver but make sure there
14318  * is only one outstanding message. Uses ill_dlpi_pending to tell
14319  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14320  * when an ACK or a NAK is received to process the next queued message.
14321  */
14322 void
14323 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14324 {
14325 	mblk_t **mpp;
14326 
14327 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14328 
14329 	/*
14330 	 * To ensure that any DLPI requests for current exclusive operation
14331 	 * are always completely sent before any DLPI messages for other
14332 	 * operations, require writer access before enqueuing.
14333 	 */
14334 	if (!IAM_WRITER_ILL(ill)) {
14335 		ill_refhold(ill);
14336 		/* qwriter_ip() does the ill_refrele() */
14337 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14338 		    NEW_OP, B_TRUE);
14339 		return;
14340 	}
14341 
14342 	mutex_enter(&ill->ill_lock);
14343 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14344 		/* Must queue message. Tail insertion */
14345 		mpp = &ill->ill_dlpi_deferred;
14346 		while (*mpp != NULL)
14347 			mpp = &((*mpp)->b_next);
14348 
14349 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14350 		    ill->ill_name));
14351 
14352 		*mpp = mp;
14353 		mutex_exit(&ill->ill_lock);
14354 		return;
14355 	}
14356 	mutex_exit(&ill->ill_lock);
14357 	ill_dlpi_dispatch(ill, mp);
14358 }
14359 
14360 static void
14361 ill_capability_send(ill_t *ill, mblk_t *mp)
14362 {
14363 	ill->ill_capab_pending_cnt++;
14364 	ill_dlpi_send(ill, mp);
14365 }
14366 
14367 void
14368 ill_capability_done(ill_t *ill)
14369 {
14370 	ASSERT(ill->ill_capab_pending_cnt != 0);
14371 
14372 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14373 
14374 	ill->ill_capab_pending_cnt--;
14375 	if (ill->ill_capab_pending_cnt == 0 &&
14376 	    ill->ill_dlpi_capab_state == IDCS_OK)
14377 		ill_capability_reset_alloc(ill);
14378 }
14379 
14380 /*
14381  * Send all deferred DLPI messages without waiting for their ACKs.
14382  */
14383 void
14384 ill_dlpi_send_deferred(ill_t *ill)
14385 {
14386 	mblk_t *mp, *nextmp;
14387 
14388 	/*
14389 	 * Clear ill_dlpi_pending so that the message is not queued in
14390 	 * ill_dlpi_send().
14391 	 */
14392 	mutex_enter(&ill->ill_lock);
14393 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14394 	mp = ill->ill_dlpi_deferred;
14395 	ill->ill_dlpi_deferred = NULL;
14396 	mutex_exit(&ill->ill_lock);
14397 
14398 	for (; mp != NULL; mp = nextmp) {
14399 		nextmp = mp->b_next;
14400 		mp->b_next = NULL;
14401 		ill_dlpi_send(ill, mp);
14402 	}
14403 }
14404 
14405 /*
14406  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14407  */
14408 boolean_t
14409 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14410 {
14411 	t_uscalar_t pending;
14412 
14413 	mutex_enter(&ill->ill_lock);
14414 	if (ill->ill_dlpi_pending == prim) {
14415 		mutex_exit(&ill->ill_lock);
14416 		return (B_TRUE);
14417 	}
14418 
14419 	/*
14420 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14421 	 * without waiting, so don't print any warnings in that case.
14422 	 */
14423 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14424 		mutex_exit(&ill->ill_lock);
14425 		return (B_FALSE);
14426 	}
14427 	pending = ill->ill_dlpi_pending;
14428 	mutex_exit(&ill->ill_lock);
14429 
14430 	if (pending == DL_PRIM_INVAL) {
14431 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14432 		    "received unsolicited ack for %s on %s\n",
14433 		    dl_primstr(prim), ill->ill_name);
14434 	} else {
14435 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14436 		    "received unexpected ack for %s on %s (expecting %s)\n",
14437 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14438 	}
14439 	return (B_FALSE);
14440 }
14441 
14442 /*
14443  * Complete the current DLPI operation associated with `prim' on `ill' and
14444  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14445  * operations and the ill's current exclusive IPSQ operation has finished
14446  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14447  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14448  * the comments above ipsq_current_finish() for details.
14449  */
14450 void
14451 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14452 {
14453 	mblk_t *mp;
14454 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14455 	ipxop_t *ipx = ipsq->ipsq_xop;
14456 
14457 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14458 	mutex_enter(&ill->ill_lock);
14459 
14460 	ASSERT(prim != DL_PRIM_INVAL);
14461 	ASSERT(ill->ill_dlpi_pending == prim);
14462 
14463 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14464 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14465 
14466 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14467 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14468 		if (ipx->ipx_current_done) {
14469 			mutex_enter(&ipx->ipx_lock);
14470 			ipx->ipx_current_ipif = NULL;
14471 			mutex_exit(&ipx->ipx_lock);
14472 		}
14473 		cv_signal(&ill->ill_cv);
14474 		mutex_exit(&ill->ill_lock);
14475 		return;
14476 	}
14477 
14478 	ill->ill_dlpi_deferred = mp->b_next;
14479 	mp->b_next = NULL;
14480 	mutex_exit(&ill->ill_lock);
14481 
14482 	ill_dlpi_dispatch(ill, mp);
14483 }
14484 
14485 void
14486 conn_delete_ire(conn_t *connp, caddr_t arg)
14487 {
14488 	ipif_t	*ipif = (ipif_t *)arg;
14489 	ire_t	*ire;
14490 
14491 	/*
14492 	 * Look at the cached ires on conns which has pointers to ipifs.
14493 	 * We just call ire_refrele which clears up the reference
14494 	 * to ire. Called when a conn closes. Also called from ipif_free
14495 	 * to cleanup indirect references to the stale ipif via the cached ire.
14496 	 */
14497 	mutex_enter(&connp->conn_lock);
14498 	ire = connp->conn_ire_cache;
14499 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14500 		connp->conn_ire_cache = NULL;
14501 		mutex_exit(&connp->conn_lock);
14502 		IRE_REFRELE_NOTR(ire);
14503 		return;
14504 	}
14505 	mutex_exit(&connp->conn_lock);
14506 
14507 }
14508 
14509 /*
14510  * Some operations (e.g., ipif_down()) conditionally delete a number
14511  * of IREs. Those IREs may have been previously cached in the conn structure.
14512  * This ipcl_walk() walker function releases all references to such IREs based
14513  * on the condemned flag.
14514  */
14515 /* ARGSUSED */
14516 void
14517 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14518 {
14519 	ire_t	*ire;
14520 
14521 	mutex_enter(&connp->conn_lock);
14522 	ire = connp->conn_ire_cache;
14523 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14524 		connp->conn_ire_cache = NULL;
14525 		mutex_exit(&connp->conn_lock);
14526 		IRE_REFRELE_NOTR(ire);
14527 		return;
14528 	}
14529 	mutex_exit(&connp->conn_lock);
14530 }
14531 
14532 /*
14533  * Take down a specific interface, but don't lose any information about it.
14534  * (Always called as writer.)
14535  * This function goes through the down sequence even if the interface is
14536  * already down. There are 2 reasons.
14537  * a. Currently we permit interface routes that depend on down interfaces
14538  *    to be added. This behaviour itself is questionable. However it appears
14539  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14540  *    time. We go thru the cleanup in order to remove these routes.
14541  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14542  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14543  *    down, but we need to cleanup i.e. do ill_dl_down and
14544  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14545  *
14546  * IP-MT notes:
14547  *
14548  * Model of reference to interfaces.
14549  *
14550  * The following members in ipif_t track references to the ipif.
14551  *	int     ipif_refcnt;    Active reference count
14552  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14553  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14554  *
14555  * The following members in ill_t track references to the ill.
14556  *	int             ill_refcnt;     active refcnt
14557  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14558  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14559  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14560  *
14561  * Reference to an ipif or ill can be obtained in any of the following ways.
14562  *
14563  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14564  * Pointers to ipif / ill from other data structures viz ire and conn.
14565  * Implicit reference to the ipif / ill by holding a reference to the ire.
14566  *
14567  * The ipif/ill lookup functions return a reference held ipif / ill.
14568  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14569  * This is a purely dynamic reference count associated with threads holding
14570  * references to the ipif / ill. Pointers from other structures do not
14571  * count towards this reference count.
14572  *
14573  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14574  * associated with the ipif/ill. This is incremented whenever a new
14575  * ire is created referencing the ipif/ill. This is done atomically inside
14576  * ire_add_v[46] where the ire is actually added to the ire hash table.
14577  * The count is decremented in ire_inactive where the ire is destroyed.
14578  *
14579  * nce's reference ill's thru nce_ill and the count of nce's associated with
14580  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14581  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14582  * table. Similarly it is decremented in ndp_inactive() where the nce
14583  * is destroyed.
14584  *
14585  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14586  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14587  * in ilm_walker_cleanup() or ilm_delete().
14588  *
14589  * Flow of ioctls involving interface down/up
14590  *
14591  * The following is the sequence of an attempt to set some critical flags on an
14592  * up interface.
14593  * ip_sioctl_flags
14594  * ipif_down
14595  * wait for ipif to be quiescent
14596  * ipif_down_tail
14597  * ip_sioctl_flags_tail
14598  *
14599  * All set ioctls that involve down/up sequence would have a skeleton similar
14600  * to the above. All the *tail functions are called after the refcounts have
14601  * dropped to the appropriate values.
14602  *
14603  * The mechanism to quiesce an ipif is as follows.
14604  *
14605  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14606  * on the ipif. Callers either pass a flag requesting wait or the lookup
14607  *  functions will return NULL.
14608  *
14609  * Delete all ires referencing this ipif
14610  *
14611  * Any thread attempting to do an ipif_refhold on an ipif that has been
14612  * obtained thru a cached pointer will first make sure that
14613  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14614  * increment the refcount.
14615  *
14616  * The above guarantees that the ipif refcount will eventually come down to
14617  * zero and the ipif will quiesce, once all threads that currently hold a
14618  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14619  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14620  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14621  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14622  * in ip.h
14623  *
14624  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14625  *
14626  * Threads trying to lookup an ipif or ill can pass a flag requesting
14627  * wait and restart if the ipif / ill cannot be looked up currently.
14628  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14629  * failure if the ipif is currently undergoing an exclusive operation, and
14630  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14631  * is restarted by ipsq_exit() when the current exclusive operation completes.
14632  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14633  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14634  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14635  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14636  * until we release the ipsq_lock, even though the the ill/ipif state flags
14637  * can change after we drop the ill_lock.
14638  *
14639  * An attempt to send out a packet using an ipif that is currently
14640  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14641  * operation and restart it later when the exclusive condition on the ipif ends.
14642  * This is an example of not passing the wait flag to the lookup functions. For
14643  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14644  * out a multicast packet on that ipif will fail while the ipif is
14645  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14646  * currently IPIF_CHANGING will also fail.
14647  */
14648 int
14649 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14650 {
14651 	ill_t		*ill = ipif->ipif_ill;
14652 	conn_t		*connp;
14653 	boolean_t	success;
14654 	boolean_t	ipif_was_up = B_FALSE;
14655 	ip_stack_t	*ipst = ill->ill_ipst;
14656 
14657 	ASSERT(IAM_WRITER_IPIF(ipif));
14658 
14659 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14660 
14661 	if (ipif->ipif_flags & IPIF_UP) {
14662 		mutex_enter(&ill->ill_lock);
14663 		ipif->ipif_flags &= ~IPIF_UP;
14664 		ASSERT(ill->ill_ipif_up_count > 0);
14665 		--ill->ill_ipif_up_count;
14666 		mutex_exit(&ill->ill_lock);
14667 		ipif_was_up = B_TRUE;
14668 		/* Update status in SCTP's list */
14669 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14670 		ill_nic_event_dispatch(ipif->ipif_ill,
14671 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14672 	}
14673 
14674 	/*
14675 	 * Blow away memberships we established in ipif_multicast_up().
14676 	 */
14677 	ipif_multicast_down(ipif);
14678 
14679 	/*
14680 	 * Remove from the mapping for __sin6_src_id. We insert only
14681 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14682 	 * stored as mapped addresses, we need to check for mapped
14683 	 * INADDR_ANY also.
14684 	 */
14685 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14686 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14687 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14688 		int err;
14689 
14690 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14691 		    ipif->ipif_zoneid, ipst);
14692 		if (err != 0) {
14693 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14694 		}
14695 	}
14696 
14697 	/*
14698 	 * Delete all IRE's pointing at this ipif or its source address.
14699 	 */
14700 	if (ipif->ipif_isv6) {
14701 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14702 		    ipst);
14703 	} else {
14704 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14705 		    ipst);
14706 	}
14707 
14708 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14709 		/*
14710 		 * Since the interface is now down, it may have just become
14711 		 * inactive.  Note that this needs to be done even for a
14712 		 * lll_logical_down(), or ARP entries will not get correctly
14713 		 * restored when the interface comes back up.
14714 		 */
14715 		if (IS_UNDER_IPMP(ill))
14716 			ipmp_ill_refresh_active(ill);
14717 	}
14718 
14719 	/*
14720 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14721 	 * ires have been deleted above. Otherwise a thread could end up
14722 	 * caching an ire in a conn after we have finished the cleanup of the
14723 	 * conn. The caching is done after making sure that the ire is not yet
14724 	 * condemned. Also documented in the block comment above ip_output
14725 	 */
14726 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14727 	/* Also, delete the ires cached in SCTP */
14728 	sctp_ire_cache_flush(ipif);
14729 
14730 	/*
14731 	 * Update any other ipifs which have used "our" local address as
14732 	 * a source address. This entails removing and recreating IRE_INTERFACE
14733 	 * entries for such ipifs.
14734 	 */
14735 	if (ipif->ipif_isv6)
14736 		ipif_update_other_ipifs_v6(ipif);
14737 	else
14738 		ipif_update_other_ipifs(ipif);
14739 
14740 	/*
14741 	 * neighbor-discovery or arp entries for this interface.
14742 	 */
14743 	ipif_ndp_down(ipif);
14744 
14745 	/*
14746 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14747 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14748 	 * and ill_delete -> ipif_free -> ipif_down
14749 	 */
14750 	if (mp == NULL) {
14751 		ASSERT(q == NULL);
14752 		return (0);
14753 	}
14754 
14755 	if (CONN_Q(q)) {
14756 		connp = Q_TO_CONN(q);
14757 		mutex_enter(&connp->conn_lock);
14758 	} else {
14759 		connp = NULL;
14760 	}
14761 	mutex_enter(&ill->ill_lock);
14762 	/*
14763 	 * Are there any ire's pointing to this ipif that are still active ?
14764 	 * If this is the last ipif going down, are there any ire's pointing
14765 	 * to this ill that are still active ?
14766 	 */
14767 	if (ipif_is_quiescent(ipif)) {
14768 		mutex_exit(&ill->ill_lock);
14769 		if (connp != NULL)
14770 			mutex_exit(&connp->conn_lock);
14771 		return (0);
14772 	}
14773 
14774 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14775 	    ill->ill_name, (void *)ill));
14776 	/*
14777 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14778 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14779 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14780 	 */
14781 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14782 	if (!success) {
14783 		/* The conn is closing. So just return */
14784 		ASSERT(connp != NULL);
14785 		mutex_exit(&ill->ill_lock);
14786 		mutex_exit(&connp->conn_lock);
14787 		return (EINTR);
14788 	}
14789 
14790 	mutex_exit(&ill->ill_lock);
14791 	if (connp != NULL)
14792 		mutex_exit(&connp->conn_lock);
14793 	return (EINPROGRESS);
14794 }
14795 
14796 void
14797 ipif_down_tail(ipif_t *ipif)
14798 {
14799 	ill_t	*ill = ipif->ipif_ill;
14800 
14801 	/*
14802 	 * Skip any loopback interface (null wq).
14803 	 * If this is the last logical interface on the ill
14804 	 * have ill_dl_down tell the driver we are gone (unbind)
14805 	 * Note that lun 0 can ipif_down even though
14806 	 * there are other logical units that are up.
14807 	 * This occurs e.g. when we change a "significant" IFF_ flag.
14808 	 */
14809 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
14810 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
14811 	    ill->ill_dl_up) {
14812 		ill_dl_down(ill);
14813 	}
14814 	ill->ill_logical_down = 0;
14815 
14816 	/*
14817 	 * Has to be after removing the routes in ipif_down_delete_ire.
14818 	 */
14819 	ipif_resolver_down(ipif);
14820 
14821 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
14822 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
14823 }
14824 
14825 /*
14826  * Bring interface logically down without bringing the physical interface
14827  * down e.g. when the netmask is changed. This avoids long lasting link
14828  * negotiations between an ethernet interface and a certain switches.
14829  */
14830 static int
14831 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14832 {
14833 	/*
14834 	 * The ill_logical_down flag is a transient flag. It is set here
14835 	 * and is cleared once the down has completed in ipif_down_tail.
14836 	 * This flag does not indicate whether the ill stream is in the
14837 	 * DL_BOUND state with the driver. Instead this flag is used by
14838 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
14839 	 * the driver. The state of the ill stream i.e. whether it is
14840 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
14841 	 */
14842 	ipif->ipif_ill->ill_logical_down = 1;
14843 	return (ipif_down(ipif, q, mp));
14844 }
14845 
14846 /*
14847  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
14848  * If the usesrc client ILL is already part of a usesrc group or not,
14849  * in either case a ire_stq with the matching usesrc client ILL will
14850  * locate the IRE's that need to be deleted. We want IREs to be created
14851  * with the new source address.
14852  */
14853 static void
14854 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
14855 {
14856 	ill_t	*ucill = (ill_t *)ill_arg;
14857 
14858 	ASSERT(IAM_WRITER_ILL(ucill));
14859 
14860 	if (ire->ire_stq == NULL)
14861 		return;
14862 
14863 	if ((ire->ire_type == IRE_CACHE) &&
14864 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
14865 		ire_delete(ire);
14866 }
14867 
14868 /*
14869  * ire_walk routine to delete every IRE dependent on the interface
14870  * address that is going down.	(Always called as writer.)
14871  * Works for both v4 and v6.
14872  * In addition for checking for ire_ipif matches it also checks for
14873  * IRE_CACHE entries which have the same source address as the
14874  * disappearing ipif since ipif_select_source might have picked
14875  * that source. Note that ipif_down/ipif_update_other_ipifs takes
14876  * care of any IRE_INTERFACE with the disappearing source address.
14877  */
14878 static void
14879 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
14880 {
14881 	ipif_t	*ipif = (ipif_t *)ipif_arg;
14882 
14883 	ASSERT(IAM_WRITER_IPIF(ipif));
14884 	if (ire->ire_ipif == NULL)
14885 		return;
14886 
14887 	if (ire->ire_ipif != ipif) {
14888 		/*
14889 		 * Look for a matching source address.
14890 		 */
14891 		if (ire->ire_type != IRE_CACHE)
14892 			return;
14893 		if (ipif->ipif_flags & IPIF_NOLOCAL)
14894 			return;
14895 
14896 		if (ire->ire_ipversion == IPV4_VERSION) {
14897 			if (ire->ire_src_addr != ipif->ipif_src_addr)
14898 				return;
14899 		} else {
14900 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
14901 			    &ipif->ipif_v6lcl_addr))
14902 				return;
14903 		}
14904 		ire_delete(ire);
14905 		return;
14906 	}
14907 	/*
14908 	 * ire_delete() will do an ire_flush_cache which will delete
14909 	 * all ire_ipif matches
14910 	 */
14911 	ire_delete(ire);
14912 }
14913 
14914 /*
14915  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
14916  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
14917  * 2) when an interface is brought up or down (on that ill).
14918  * This ensures that the IRE_CACHE entries don't retain stale source
14919  * address selection results.
14920  */
14921 void
14922 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
14923 {
14924 	ill_t	*ill = (ill_t *)ill_arg;
14925 
14926 	ASSERT(IAM_WRITER_ILL(ill));
14927 	ASSERT(ire->ire_type == IRE_CACHE);
14928 
14929 	/*
14930 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14931 	 * ill, but we only want to delete the IRE if ire_ipif matches.
14932 	 */
14933 	ASSERT(ire->ire_ipif != NULL);
14934 	if (ill == ire->ire_ipif->ipif_ill)
14935 		ire_delete(ire);
14936 }
14937 
14938 /*
14939  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
14940  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
14941  * the IPMP ill.
14942  */
14943 void
14944 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
14945 {
14946 	ill_t	*ill = (ill_t *)ill_arg;
14947 
14948 	ASSERT(IAM_WRITER_ILL(ill));
14949 	ASSERT(ire->ire_type == IRE_CACHE);
14950 
14951 	/*
14952 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14953 	 * ill, but we only want to delete the IRE if ire_stq matches.
14954 	 */
14955 	if (ire->ire_stq->q_ptr == ill_arg)
14956 		ire_delete(ire);
14957 }
14958 
14959 /*
14960  * Delete all the IREs whose ire_stq's reference any ill in the same IPMP
14961  * group as `ill_arg'.  Used by ipmp_ill_deactivate() to flush all IRE_CACHE
14962  * entries for the illgrp.
14963  */
14964 void
14965 ill_grp_cache_delete(ire_t *ire, char *ill_arg)
14966 {
14967 	ill_t	*ill = (ill_t *)ill_arg;
14968 
14969 	ASSERT(IAM_WRITER_ILL(ill));
14970 
14971 	if (ire->ire_type == IRE_CACHE &&
14972 	    IS_IN_SAME_ILLGRP((ill_t *)ire->ire_stq->q_ptr, ill)) {
14973 		ire_delete(ire);
14974 	}
14975 }
14976 
14977 /*
14978  * Delete all broadcast IREs with a source address on `ill_arg'.
14979  */
14980 static void
14981 ill_broadcast_delete(ire_t *ire, char *ill_arg)
14982 {
14983 	ill_t *ill = (ill_t *)ill_arg;
14984 
14985 	ASSERT(IAM_WRITER_ILL(ill));
14986 	ASSERT(ire->ire_type == IRE_BROADCAST);
14987 
14988 	if (ire->ire_ipif->ipif_ill == ill)
14989 		ire_delete(ire);
14990 }
14991 
14992 /*
14993  * Initiate deallocate of an IPIF. Always called as writer. Called by
14994  * ill_delete or ip_sioctl_removeif.
14995  */
14996 static void
14997 ipif_free(ipif_t *ipif)
14998 {
14999 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15000 
15001 	ASSERT(IAM_WRITER_IPIF(ipif));
15002 
15003 	if (ipif->ipif_recovery_id != 0)
15004 		(void) untimeout(ipif->ipif_recovery_id);
15005 	ipif->ipif_recovery_id = 0;
15006 
15007 	/* Remove conn references */
15008 	reset_conn_ipif(ipif);
15009 
15010 	/*
15011 	 * Make sure we have valid net and subnet broadcast ire's for the
15012 	 * other ipif's which share them with this ipif.
15013 	 */
15014 	if (!ipif->ipif_isv6)
15015 		ipif_check_bcast_ires(ipif);
15016 
15017 	/*
15018 	 * Take down the interface. We can be called either from ill_delete
15019 	 * or from ip_sioctl_removeif.
15020 	 */
15021 	(void) ipif_down(ipif, NULL, NULL);
15022 
15023 	/*
15024 	 * Now that the interface is down, there's no chance it can still
15025 	 * become a duplicate.  Cancel any timer that may have been set while
15026 	 * tearing down.
15027 	 */
15028 	if (ipif->ipif_recovery_id != 0)
15029 		(void) untimeout(ipif->ipif_recovery_id);
15030 	ipif->ipif_recovery_id = 0;
15031 
15032 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15033 	/* Remove pointers to this ill in the multicast routing tables */
15034 	reset_mrt_vif_ipif(ipif);
15035 	/* If necessary, clear the cached source ipif rotor. */
15036 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15037 		ipif->ipif_ill->ill_src_ipif = NULL;
15038 	rw_exit(&ipst->ips_ill_g_lock);
15039 }
15040 
15041 static void
15042 ipif_free_tail(ipif_t *ipif)
15043 {
15044 	mblk_t	*mp;
15045 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15046 
15047 	/*
15048 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15049 	 */
15050 	mutex_enter(&ipif->ipif_saved_ire_lock);
15051 	mp = ipif->ipif_saved_ire_mp;
15052 	ipif->ipif_saved_ire_mp = NULL;
15053 	mutex_exit(&ipif->ipif_saved_ire_lock);
15054 	freemsg(mp);
15055 
15056 	/*
15057 	 * Need to hold both ill_g_lock and ill_lock while
15058 	 * inserting or removing an ipif from the linked list
15059 	 * of ipifs hanging off the ill.
15060 	 */
15061 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15062 
15063 	ASSERT(ilm_walk_ipif(ipif) == 0);
15064 
15065 #ifdef DEBUG
15066 	ipif_trace_cleanup(ipif);
15067 #endif
15068 
15069 	/* Ask SCTP to take it out of it list */
15070 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15071 
15072 	/* Get it out of the ILL interface list. */
15073 	ipif_remove(ipif);
15074 	rw_exit(&ipst->ips_ill_g_lock);
15075 
15076 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15077 
15078 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15079 	ASSERT(ipif->ipif_recovery_id == 0);
15080 
15081 	/* Free the memory. */
15082 	mi_free(ipif);
15083 }
15084 
15085 /*
15086  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15087  * is zero.
15088  */
15089 void
15090 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15091 {
15092 	char	lbuf[LIFNAMSIZ];
15093 	char	*name;
15094 	size_t	name_len;
15095 
15096 	buf[0] = '\0';
15097 	name = ipif->ipif_ill->ill_name;
15098 	name_len = ipif->ipif_ill->ill_name_length;
15099 	if (ipif->ipif_id != 0) {
15100 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15101 		    ipif->ipif_id);
15102 		name = lbuf;
15103 		name_len = mi_strlen(name) + 1;
15104 	}
15105 	len -= 1;
15106 	buf[len] = '\0';
15107 	len = MIN(len, name_len);
15108 	bcopy(name, buf, len);
15109 }
15110 
15111 /*
15112  * Find an IPIF based on the name passed in.  Names can be of the
15113  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15114  * The <phys> string can have forms like <dev><#> (e.g., le0),
15115  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15116  * When there is no colon, the implied unit id is zero. <phys> must
15117  * correspond to the name of an ILL.  (May be called as writer.)
15118  */
15119 static ipif_t *
15120 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15121     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15122     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15123 {
15124 	char	*cp;
15125 	char	*endp;
15126 	long	id;
15127 	ill_t	*ill;
15128 	ipif_t	*ipif;
15129 	uint_t	ire_type;
15130 	boolean_t did_alloc = B_FALSE;
15131 	ipsq_t	*ipsq;
15132 
15133 	if (error != NULL)
15134 		*error = 0;
15135 
15136 	/*
15137 	 * If the caller wants to us to create the ipif, make sure we have a
15138 	 * valid zoneid
15139 	 */
15140 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15141 
15142 	if (namelen == 0) {
15143 		if (error != NULL)
15144 			*error = ENXIO;
15145 		return (NULL);
15146 	}
15147 
15148 	*exists = B_FALSE;
15149 	/* Look for a colon in the name. */
15150 	endp = &name[namelen];
15151 	for (cp = endp; --cp > name; ) {
15152 		if (*cp == IPIF_SEPARATOR_CHAR)
15153 			break;
15154 	}
15155 
15156 	if (*cp == IPIF_SEPARATOR_CHAR) {
15157 		/*
15158 		 * Reject any non-decimal aliases for logical
15159 		 * interfaces. Aliases with leading zeroes
15160 		 * are also rejected as they introduce ambiguity
15161 		 * in the naming of the interfaces.
15162 		 * In order to confirm with existing semantics,
15163 		 * and to not break any programs/script relying
15164 		 * on that behaviour, if<0>:0 is considered to be
15165 		 * a valid interface.
15166 		 *
15167 		 * If alias has two or more digits and the first
15168 		 * is zero, fail.
15169 		 */
15170 		if (&cp[2] < endp && cp[1] == '0') {
15171 			if (error != NULL)
15172 				*error = EINVAL;
15173 			return (NULL);
15174 		}
15175 	}
15176 
15177 	if (cp <= name) {
15178 		cp = endp;
15179 	} else {
15180 		*cp = '\0';
15181 	}
15182 
15183 	/*
15184 	 * Look up the ILL, based on the portion of the name
15185 	 * before the slash. ill_lookup_on_name returns a held ill.
15186 	 * Temporary to check whether ill exists already. If so
15187 	 * ill_lookup_on_name will clear it.
15188 	 */
15189 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15190 	    q, mp, func, error, &did_alloc, ipst);
15191 	if (cp != endp)
15192 		*cp = IPIF_SEPARATOR_CHAR;
15193 	if (ill == NULL)
15194 		return (NULL);
15195 
15196 	/* Establish the unit number in the name. */
15197 	id = 0;
15198 	if (cp < endp && *endp == '\0') {
15199 		/* If there was a colon, the unit number follows. */
15200 		cp++;
15201 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15202 			ill_refrele(ill);
15203 			if (error != NULL)
15204 				*error = ENXIO;
15205 			return (NULL);
15206 		}
15207 	}
15208 
15209 	GRAB_CONN_LOCK(q);
15210 	mutex_enter(&ill->ill_lock);
15211 	/* Now see if there is an IPIF with this unit number. */
15212 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15213 		if (ipif->ipif_id == id) {
15214 			if (zoneid != ALL_ZONES &&
15215 			    zoneid != ipif->ipif_zoneid &&
15216 			    ipif->ipif_zoneid != ALL_ZONES) {
15217 				mutex_exit(&ill->ill_lock);
15218 				RELEASE_CONN_LOCK(q);
15219 				ill_refrele(ill);
15220 				if (error != NULL)
15221 					*error = ENXIO;
15222 				return (NULL);
15223 			}
15224 			/*
15225 			 * The block comment at the start of ipif_down
15226 			 * explains the use of the macros used below
15227 			 */
15228 			if (IPIF_CAN_LOOKUP(ipif)) {
15229 				ipif_refhold_locked(ipif);
15230 				mutex_exit(&ill->ill_lock);
15231 				if (!did_alloc)
15232 					*exists = B_TRUE;
15233 				/*
15234 				 * Drop locks before calling ill_refrele
15235 				 * since it can potentially call into
15236 				 * ipif_ill_refrele_tail which can end up
15237 				 * in trying to acquire any lock.
15238 				 */
15239 				RELEASE_CONN_LOCK(q);
15240 				ill_refrele(ill);
15241 				return (ipif);
15242 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15243 				ipsq = ill->ill_phyint->phyint_ipsq;
15244 				mutex_enter(&ipsq->ipsq_lock);
15245 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15246 				mutex_exit(&ill->ill_lock);
15247 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15248 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15249 				mutex_exit(&ipsq->ipsq_lock);
15250 				RELEASE_CONN_LOCK(q);
15251 				ill_refrele(ill);
15252 				if (error != NULL)
15253 					*error = EINPROGRESS;
15254 				return (NULL);
15255 			}
15256 		}
15257 	}
15258 	RELEASE_CONN_LOCK(q);
15259 
15260 	if (!do_alloc) {
15261 		mutex_exit(&ill->ill_lock);
15262 		ill_refrele(ill);
15263 		if (error != NULL)
15264 			*error = ENXIO;
15265 		return (NULL);
15266 	}
15267 
15268 	/*
15269 	 * If none found, atomically allocate and return a new one.
15270 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15271 	 * to support "receive only" use of lo0:1 etc. as is still done
15272 	 * below as an initial guess.
15273 	 * However, this is now likely to be overriden later in ipif_up_done()
15274 	 * when we know for sure what address has been configured on the
15275 	 * interface, since we might have more than one loopback interface
15276 	 * with a loopback address, e.g. in the case of zones, and all the
15277 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15278 	 */
15279 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15280 		ire_type = IRE_LOOPBACK;
15281 	else
15282 		ire_type = IRE_LOCAL;
15283 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15284 	if (ipif != NULL)
15285 		ipif_refhold_locked(ipif);
15286 	else if (error != NULL)
15287 		*error = ENOMEM;
15288 	mutex_exit(&ill->ill_lock);
15289 	ill_refrele(ill);
15290 	return (ipif);
15291 }
15292 
15293 /*
15294  * This routine is called whenever a new address comes up on an ipif.  If
15295  * we are configured to respond to address mask requests, then we are supposed
15296  * to broadcast an address mask reply at this time.  This routine is also
15297  * called if we are already up, but a netmask change is made.  This is legal
15298  * but might not make the system manager very popular.	(May be called
15299  * as writer.)
15300  */
15301 void
15302 ipif_mask_reply(ipif_t *ipif)
15303 {
15304 	icmph_t	*icmph;
15305 	ipha_t	*ipha;
15306 	mblk_t	*mp;
15307 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15308 
15309 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15310 
15311 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15312 		return;
15313 
15314 	/* ICMP mask reply is IPv4 only */
15315 	ASSERT(!ipif->ipif_isv6);
15316 	/* ICMP mask reply is not for a loopback interface */
15317 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15318 
15319 	mp = allocb(REPLY_LEN, BPRI_HI);
15320 	if (mp == NULL)
15321 		return;
15322 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15323 
15324 	ipha = (ipha_t *)mp->b_rptr;
15325 	bzero(ipha, REPLY_LEN);
15326 	*ipha = icmp_ipha;
15327 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15328 	ipha->ipha_src = ipif->ipif_src_addr;
15329 	ipha->ipha_dst = ipif->ipif_brd_addr;
15330 	ipha->ipha_length = htons(REPLY_LEN);
15331 	ipha->ipha_ident = 0;
15332 
15333 	icmph = (icmph_t *)&ipha[1];
15334 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15335 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15336 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15337 
15338 	put(ipif->ipif_wq, mp);
15339 
15340 #undef	REPLY_LEN
15341 }
15342 
15343 /*
15344  * When the mtu in the ipif changes, we call this routine through ire_walk
15345  * to update all the relevant IREs.
15346  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15347  */
15348 static void
15349 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15350 {
15351 	ipif_t *ipif = (ipif_t *)ipif_arg;
15352 
15353 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15354 		return;
15355 
15356 	mutex_enter(&ire->ire_lock);
15357 	if (ire->ire_marks & IRE_MARK_PMTU) {
15358 		/* Avoid increasing the PMTU */
15359 		ire->ire_max_frag = MIN(ipif->ipif_mtu, ire->ire_max_frag);
15360 		if (ire->ire_max_frag == ipif->ipif_mtu)
15361 			ire->ire_marks &= ~IRE_MARK_PMTU;
15362 	} else {
15363 		ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15364 	}
15365 	mutex_exit(&ire->ire_lock);
15366 }
15367 
15368 /*
15369  * When the mtu in the ill changes, we call this routine through ire_walk
15370  * to update all the relevant IREs.
15371  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15372  */
15373 void
15374 ill_mtu_change(ire_t *ire, char *ill_arg)
15375 {
15376 	ill_t	*ill = (ill_t *)ill_arg;
15377 
15378 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15379 		return;
15380 
15381 	mutex_enter(&ire->ire_lock);
15382 	if (ire->ire_marks & IRE_MARK_PMTU) {
15383 		/* Avoid increasing the PMTU */
15384 		ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
15385 		    ire->ire_max_frag);
15386 		if (ire->ire_max_frag == ire->ire_ipif->ipif_mtu) {
15387 			ire->ire_marks &= ~IRE_MARK_PMTU;
15388 		}
15389 	} else {
15390 		ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu, IP_MAXPACKET);
15391 	}
15392 	mutex_exit(&ire->ire_lock);
15393 }
15394 
15395 /*
15396  * Join the ipif specific multicast groups.
15397  * Must be called after a mapping has been set up in the resolver.  (Always
15398  * called as writer.)
15399  */
15400 void
15401 ipif_multicast_up(ipif_t *ipif)
15402 {
15403 	int err;
15404 	ill_t *ill;
15405 
15406 	ASSERT(IAM_WRITER_IPIF(ipif));
15407 
15408 	ill = ipif->ipif_ill;
15409 
15410 	ip1dbg(("ipif_multicast_up\n"));
15411 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15412 		return;
15413 
15414 	if (ipif->ipif_isv6) {
15415 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15416 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15417 
15418 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15419 
15420 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15421 			return;
15422 
15423 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15424 
15425 		/*
15426 		 * Join the all hosts multicast address.  We skip this for
15427 		 * underlying IPMP interfaces since they should be invisible.
15428 		 */
15429 		if (!IS_UNDER_IPMP(ill)) {
15430 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15431 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15432 			if (err != 0) {
15433 				ip0dbg(("ipif_multicast_up: "
15434 				    "all_hosts_mcast failed %d\n", err));
15435 				return;
15436 			}
15437 			ipif->ipif_joined_allhosts = 1;
15438 		}
15439 
15440 		/*
15441 		 * Enable multicast for the solicited node multicast address
15442 		 */
15443 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15444 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15445 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15446 			if (err != 0) {
15447 				ip0dbg(("ipif_multicast_up: solicited MC"
15448 				    " failed %d\n", err));
15449 				if (ipif->ipif_joined_allhosts) {
15450 					(void) ip_delmulti_v6(&v6allmc, ill,
15451 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15452 					ipif->ipif_joined_allhosts = 0;
15453 				}
15454 				return;
15455 			}
15456 		}
15457 	} else {
15458 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15459 			return;
15460 
15461 		/* Join the all hosts multicast address */
15462 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15463 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15464 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15465 		if (err) {
15466 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15467 			return;
15468 		}
15469 	}
15470 	ipif->ipif_multicast_up = 1;
15471 }
15472 
15473 /*
15474  * Blow away any multicast groups that we joined in ipif_multicast_up().
15475  * (Explicit memberships are blown away in ill_leave_multicast() when the
15476  * ill is brought down.)
15477  */
15478 void
15479 ipif_multicast_down(ipif_t *ipif)
15480 {
15481 	int err;
15482 
15483 	ASSERT(IAM_WRITER_IPIF(ipif));
15484 
15485 	ip1dbg(("ipif_multicast_down\n"));
15486 	if (!ipif->ipif_multicast_up)
15487 		return;
15488 
15489 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15490 
15491 	if (!ipif->ipif_isv6) {
15492 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15493 		    B_TRUE);
15494 		if (err != 0)
15495 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15496 
15497 		ipif->ipif_multicast_up = 0;
15498 		return;
15499 	}
15500 
15501 	/*
15502 	 * Leave the all-hosts multicast address.
15503 	 */
15504 	if (ipif->ipif_joined_allhosts) {
15505 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15506 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15507 		if (err != 0) {
15508 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15509 			    "failed %d\n", err));
15510 		}
15511 		ipif->ipif_joined_allhosts = 0;
15512 	}
15513 
15514 	/*
15515 	 * Disable multicast for the solicited node multicast address
15516 	 */
15517 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15518 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15519 
15520 		ipv6_multi.s6_addr32[3] |=
15521 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15522 
15523 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15524 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15525 		if (err != 0) {
15526 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15527 			    err));
15528 		}
15529 	}
15530 
15531 	ipif->ipif_multicast_up = 0;
15532 }
15533 
15534 /*
15535  * Used when an interface comes up to recreate any extra routes on this
15536  * interface.
15537  */
15538 static ire_t **
15539 ipif_recover_ire(ipif_t *ipif)
15540 {
15541 	mblk_t	*mp;
15542 	ire_t	**ipif_saved_irep;
15543 	ire_t	**irep;
15544 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15545 
15546 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15547 	    ipif->ipif_id));
15548 
15549 	mutex_enter(&ipif->ipif_saved_ire_lock);
15550 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15551 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15552 	if (ipif_saved_irep == NULL) {
15553 		mutex_exit(&ipif->ipif_saved_ire_lock);
15554 		return (NULL);
15555 	}
15556 
15557 	irep = ipif_saved_irep;
15558 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15559 		ire_t		*ire;
15560 		queue_t		*rfq;
15561 		queue_t		*stq;
15562 		ifrt_t		*ifrt;
15563 		uchar_t		*src_addr;
15564 		uchar_t		*gateway_addr;
15565 		ushort_t	type;
15566 
15567 		/*
15568 		 * When the ire was initially created and then added in
15569 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15570 		 * in the case of a traditional interface route, or as one of
15571 		 * the IRE_OFFSUBNET types (with the exception of
15572 		 * IRE_HOST types ire which is created by icmp_redirect() and
15573 		 * which we don't need to save or recover).  In the case where
15574 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15575 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15576 		 * to satisfy software like GateD and Sun Cluster which creates
15577 		 * routes using the the loopback interface's address as a
15578 		 * gateway.
15579 		 *
15580 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15581 		 * ire_create() will be called in the same way here as
15582 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15583 		 * the route looks like a traditional interface route (where
15584 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15585 		 * the saved ifrt->ifrt_type.  This means that in the case where
15586 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15587 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15588 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15589 		 */
15590 		ifrt = (ifrt_t *)mp->b_rptr;
15591 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15592 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15593 			rfq = NULL;
15594 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15595 			    ? ipif->ipif_rq : ipif->ipif_wq;
15596 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15597 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15598 			    : (uint8_t *)&ipif->ipif_src_addr;
15599 			gateway_addr = NULL;
15600 			type = ipif->ipif_net_type;
15601 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15602 			/* Recover multiroute broadcast IRE. */
15603 			rfq = ipif->ipif_rq;
15604 			stq = ipif->ipif_wq;
15605 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15606 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15607 			    : (uint8_t *)&ipif->ipif_src_addr;
15608 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15609 			type = ifrt->ifrt_type;
15610 		} else {
15611 			rfq = NULL;
15612 			stq = NULL;
15613 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15614 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15615 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15616 			type = ifrt->ifrt_type;
15617 		}
15618 
15619 		/*
15620 		 * Create a copy of the IRE with the saved address and netmask.
15621 		 */
15622 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15623 		    "0x%x/0x%x\n",
15624 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15625 		    ntohl(ifrt->ifrt_addr),
15626 		    ntohl(ifrt->ifrt_mask)));
15627 		ire = ire_create(
15628 		    (uint8_t *)&ifrt->ifrt_addr,
15629 		    (uint8_t *)&ifrt->ifrt_mask,
15630 		    src_addr,
15631 		    gateway_addr,
15632 		    &ifrt->ifrt_max_frag,
15633 		    NULL,
15634 		    rfq,
15635 		    stq,
15636 		    type,
15637 		    ipif,
15638 		    0,
15639 		    0,
15640 		    0,
15641 		    ifrt->ifrt_flags,
15642 		    &ifrt->ifrt_iulp_info,
15643 		    NULL,
15644 		    NULL,
15645 		    ipst);
15646 
15647 		if (ire == NULL) {
15648 			mutex_exit(&ipif->ipif_saved_ire_lock);
15649 			kmem_free(ipif_saved_irep,
15650 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15651 			return (NULL);
15652 		}
15653 
15654 		/*
15655 		 * Some software (for example, GateD and Sun Cluster) attempts
15656 		 * to create (what amount to) IRE_PREFIX routes with the
15657 		 * loopback address as the gateway.  This is primarily done to
15658 		 * set up prefixes with the RTF_REJECT flag set (for example,
15659 		 * when generating aggregate routes.)
15660 		 *
15661 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15662 		 * IRE_LOOPBACK, then we map the request into a
15663 		 * IRE_IF_NORESOLVER.
15664 		 */
15665 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15666 			ire->ire_type = IRE_IF_NORESOLVER;
15667 		/*
15668 		 * ire held by ire_add, will be refreled' towards the
15669 		 * the end of ipif_up_done
15670 		 */
15671 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15672 		*irep = ire;
15673 		irep++;
15674 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15675 	}
15676 	mutex_exit(&ipif->ipif_saved_ire_lock);
15677 	return (ipif_saved_irep);
15678 }
15679 
15680 /*
15681  * Used to set the netmask and broadcast address to default values when the
15682  * interface is brought up.  (Always called as writer.)
15683  */
15684 static void
15685 ipif_set_default(ipif_t *ipif)
15686 {
15687 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15688 
15689 	if (!ipif->ipif_isv6) {
15690 		/*
15691 		 * Interface holds an IPv4 address. Default
15692 		 * mask is the natural netmask.
15693 		 */
15694 		if (!ipif->ipif_net_mask) {
15695 			ipaddr_t	v4mask;
15696 
15697 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15698 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15699 		}
15700 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15701 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15702 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15703 		} else {
15704 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15705 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15706 		}
15707 		/*
15708 		 * NOTE: SunOS 4.X does this even if the broadcast address
15709 		 * has been already set thus we do the same here.
15710 		 */
15711 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15712 			ipaddr_t	v4addr;
15713 
15714 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15715 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15716 		}
15717 	} else {
15718 		/*
15719 		 * Interface holds an IPv6-only address.  Default
15720 		 * mask is all-ones.
15721 		 */
15722 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15723 			ipif->ipif_v6net_mask = ipv6_all_ones;
15724 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15725 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15726 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15727 		} else {
15728 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15729 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15730 		}
15731 	}
15732 }
15733 
15734 /*
15735  * Return 0 if this address can be used as local address without causing
15736  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15737  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15738  * Note that the same IPv6 link-local address is allowed as long as the ills
15739  * are not on the same link.
15740  */
15741 int
15742 ip_addr_availability_check(ipif_t *new_ipif)
15743 {
15744 	in6_addr_t our_v6addr;
15745 	ill_t *ill;
15746 	ipif_t *ipif;
15747 	ill_walk_context_t ctx;
15748 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15749 
15750 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15751 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15752 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15753 
15754 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15755 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15756 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15757 		return (0);
15758 
15759 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15760 
15761 	if (new_ipif->ipif_isv6)
15762 		ill = ILL_START_WALK_V6(&ctx, ipst);
15763 	else
15764 		ill = ILL_START_WALK_V4(&ctx, ipst);
15765 
15766 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15767 		for (ipif = ill->ill_ipif; ipif != NULL;
15768 		    ipif = ipif->ipif_next) {
15769 			if ((ipif == new_ipif) ||
15770 			    !(ipif->ipif_flags & IPIF_UP) ||
15771 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15772 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15773 			    &our_v6addr))
15774 				continue;
15775 
15776 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15777 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15778 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15779 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15780 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15781 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15782 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15783 				continue;
15784 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15785 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15786 				continue;
15787 			else if (new_ipif->ipif_ill == ill)
15788 				return (EADDRINUSE);
15789 			else
15790 				return (EADDRNOTAVAIL);
15791 		}
15792 	}
15793 
15794 	return (0);
15795 }
15796 
15797 /*
15798  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15799  * IREs for the ipif.
15800  * When the routine returns EINPROGRESS then mp has been consumed and
15801  * the ioctl will be acked from ip_rput_dlpi.
15802  */
15803 int
15804 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15805 {
15806 	ill_t		*ill = ipif->ipif_ill;
15807 	boolean_t 	isv6 = ipif->ipif_isv6;
15808 	int		err = 0;
15809 	boolean_t	success;
15810 	uint_t		ipif_orig_id;
15811 	ip_stack_t	*ipst = ill->ill_ipst;
15812 
15813 	ASSERT(IAM_WRITER_IPIF(ipif));
15814 
15815 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15816 
15817 	/* Shouldn't get here if it is already up. */
15818 	if (ipif->ipif_flags & IPIF_UP)
15819 		return (EALREADY);
15820 
15821 	/*
15822 	 * If this is a request to bring up a data address on an interface
15823 	 * under IPMP, then move the address to its IPMP meta-interface and
15824 	 * try to bring it up.  One complication is that the zeroth ipif for
15825 	 * an ill is special, in that every ill always has one, and that code
15826 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15827 	 */
15828 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15829 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15830 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15831 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15832 
15833 		/*
15834 		 * The ipif being brought up should be quiesced.  If it's not,
15835 		 * something has gone amiss and we need to bail out.  (If it's
15836 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15837 		 */
15838 		mutex_enter(&ill->ill_lock);
15839 		if (!ipif_is_quiescent(ipif)) {
15840 			mutex_exit(&ill->ill_lock);
15841 			return (EINVAL);
15842 		}
15843 		mutex_exit(&ill->ill_lock);
15844 
15845 		/*
15846 		 * If we're going to need to allocate ipifs, do it prior
15847 		 * to starting the move (and grabbing locks).
15848 		 */
15849 		if (ipif->ipif_id == 0) {
15850 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15851 			    B_FALSE);
15852 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15853 			    B_FALSE);
15854 			if (moveipif == NULL || stubipif == NULL) {
15855 				mi_free(moveipif);
15856 				mi_free(stubipif);
15857 				return (ENOMEM);
15858 			}
15859 		}
15860 
15861 		/*
15862 		 * Grab or transfer the ipif to move.  During the move, keep
15863 		 * ill_g_lock held to prevent any ill walker threads from
15864 		 * seeing things in an inconsistent state.
15865 		 */
15866 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15867 		if (ipif->ipif_id != 0) {
15868 			ipif_remove(ipif);
15869 		} else {
15870 			ipif_transfer(ipif, moveipif, stubipif);
15871 			ipif = moveipif;
15872 		}
15873 
15874 		/*
15875 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
15876 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
15877 		 * replace that one.  Otherwise, pick the next available slot.
15878 		 */
15879 		ipif->ipif_ill = ipmp_ill;
15880 		ipif_orig_id = ipif->ipif_id;
15881 
15882 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
15883 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
15884 			ipif = ipmp_ill->ill_ipif;
15885 		} else {
15886 			ipif->ipif_id = -1;
15887 			if (ipif_insert(ipif, B_FALSE) != 0) {
15888 				/*
15889 				 * No more available ipif_id's -- put it back
15890 				 * on the original ill and fail the operation.
15891 				 * Since we're writer on the ill, we can be
15892 				 * sure our old slot is still available.
15893 				 */
15894 				ipif->ipif_id = ipif_orig_id;
15895 				ipif->ipif_ill = ill;
15896 				if (ipif_orig_id == 0) {
15897 					ipif_transfer(ipif, ill->ill_ipif,
15898 					    NULL);
15899 				} else {
15900 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
15901 				}
15902 				rw_exit(&ipst->ips_ill_g_lock);
15903 				return (ENOMEM);
15904 			}
15905 		}
15906 		rw_exit(&ipst->ips_ill_g_lock);
15907 
15908 		/*
15909 		 * Tell SCTP that the ipif has moved.  Note that even if we
15910 		 * had to allocate a new ipif, the original sequence id was
15911 		 * preserved and therefore SCTP won't know.
15912 		 */
15913 		sctp_move_ipif(ipif, ill, ipmp_ill);
15914 
15915 		/*
15916 		 * If the ipif being brought up was on slot zero, then we
15917 		 * first need to bring up the placeholder we stuck there.  In
15918 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
15919 		 * to ipif_up() itself, if we successfully bring up the
15920 		 * placeholder, we'll check ill_move_ipif and bring it up too.
15921 		 */
15922 		if (ipif_orig_id == 0) {
15923 			ASSERT(ill->ill_move_ipif == NULL);
15924 			ill->ill_move_ipif = ipif;
15925 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
15926 				ASSERT(ill->ill_move_ipif == NULL);
15927 			if (err != EINPROGRESS)
15928 				ill->ill_move_ipif = NULL;
15929 			return (err);
15930 		}
15931 
15932 		/*
15933 		 * Bring it up on the IPMP ill.
15934 		 */
15935 		return (ipif_up(ipif, q, mp));
15936 	}
15937 
15938 	/* Skip arp/ndp for any loopback interface. */
15939 	if (ill->ill_wq != NULL) {
15940 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
15941 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
15942 
15943 		if (!ill->ill_dl_up) {
15944 			/*
15945 			 * ill_dl_up is not yet set. i.e. we are yet to
15946 			 * DL_BIND with the driver and this is the first
15947 			 * logical interface on the ill to become "up".
15948 			 * Tell the driver to get going (via DL_BIND_REQ).
15949 			 * Note that changing "significant" IFF_ flags
15950 			 * address/netmask etc cause a down/up dance, but
15951 			 * does not cause an unbind (DL_UNBIND) with the driver
15952 			 */
15953 			return (ill_dl_up(ill, ipif, mp, q));
15954 		}
15955 
15956 		/*
15957 		 * ipif_resolver_up may end up sending an
15958 		 * AR_INTERFACE_UP message to ARP, which would, in
15959 		 * turn send a DLPI message to the driver. ioctls are
15960 		 * serialized and so we cannot send more than one
15961 		 * interface up message at a time. If ipif_resolver_up
15962 		 * does send an interface up message to ARP, we get
15963 		 * EINPROGRESS and we will complete in ip_arp_done.
15964 		 */
15965 
15966 		ASSERT(connp != NULL || !CONN_Q(q));
15967 		if (connp != NULL)
15968 			mutex_enter(&connp->conn_lock);
15969 		mutex_enter(&ill->ill_lock);
15970 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
15971 		mutex_exit(&ill->ill_lock);
15972 		if (connp != NULL)
15973 			mutex_exit(&connp->conn_lock);
15974 		if (!success)
15975 			return (EINTR);
15976 
15977 		/*
15978 		 * Crank up the resolver.  For IPv6, this cranks up the
15979 		 * external resolver if one is configured, but even if an
15980 		 * external resolver isn't configured, it must be called to
15981 		 * reset DAD state.  For IPv6, if an external resolver is not
15982 		 * being used, ipif_resolver_up() will never return
15983 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
15984 		 * Note that if an external resolver is being used, there's no
15985 		 * need to call ipif_ndp_up() since it will do nothing.
15986 		 */
15987 		err = ipif_resolver_up(ipif, Res_act_initial);
15988 		if (err == EINPROGRESS) {
15989 			/* We will complete it in ip_arp_done() */
15990 			return (err);
15991 		}
15992 
15993 		if (isv6 && err == 0)
15994 			err = ipif_ndp_up(ipif, B_TRUE);
15995 
15996 		ASSERT(err != EINPROGRESS);
15997 		mp = ipsq_pending_mp_get(ipsq, &connp);
15998 		ASSERT(mp != NULL);
15999 		if (err != 0)
16000 			return (err);
16001 	} else {
16002 		/*
16003 		 * Interfaces without underlying hardware don't do duplicate
16004 		 * address detection.
16005 		 */
16006 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16007 		ipif->ipif_addr_ready = 1;
16008 	}
16009 
16010 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16011 	if (err == 0 && ill->ill_move_ipif != NULL) {
16012 		ipif = ill->ill_move_ipif;
16013 		ill->ill_move_ipif = NULL;
16014 		return (ipif_up(ipif, q, mp));
16015 	}
16016 	return (err);
16017 }
16018 
16019 /*
16020  * Perform a bind for the physical device.
16021  * When the routine returns EINPROGRESS then mp has been consumed and
16022  * the ioctl will be acked from ip_rput_dlpi.
16023  * Allocate an unbind message and save it until ipif_down.
16024  */
16025 static int
16026 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16027 {
16028 	areq_t	*areq;
16029 	mblk_t	*areq_mp = NULL;
16030 	mblk_t	*bind_mp = NULL;
16031 	mblk_t	*unbind_mp = NULL;
16032 	conn_t	*connp;
16033 	boolean_t success;
16034 	uint16_t sap_addr;
16035 
16036 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16037 	ASSERT(IAM_WRITER_ILL(ill));
16038 	ASSERT(mp != NULL);
16039 
16040 	/* Create a resolver cookie for ARP */
16041 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16042 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16043 		if (areq_mp == NULL)
16044 			return (ENOMEM);
16045 
16046 		freemsg(ill->ill_resolver_mp);
16047 		ill->ill_resolver_mp = areq_mp;
16048 		areq = (areq_t *)areq_mp->b_rptr;
16049 		sap_addr = ill->ill_sap;
16050 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16051 	}
16052 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16053 	    DL_BIND_REQ);
16054 	if (bind_mp == NULL)
16055 		goto bad;
16056 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16057 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16058 
16059 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16060 	if (unbind_mp == NULL)
16061 		goto bad;
16062 
16063 	/*
16064 	 * Record state needed to complete this operation when the
16065 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16066 	 */
16067 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16068 	ASSERT(connp != NULL || !CONN_Q(q));
16069 	GRAB_CONN_LOCK(q);
16070 	mutex_enter(&ipif->ipif_ill->ill_lock);
16071 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16072 	mutex_exit(&ipif->ipif_ill->ill_lock);
16073 	RELEASE_CONN_LOCK(q);
16074 	if (!success)
16075 		goto bad;
16076 
16077 	/*
16078 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16079 	 * the interface goes down.
16080 	 */
16081 	ASSERT(ill->ill_unbind_mp == NULL);
16082 	ill->ill_unbind_mp = unbind_mp;
16083 
16084 	ill_dlpi_send(ill, bind_mp);
16085 	/* Send down link-layer capabilities probe if not already done. */
16086 	ill_capability_probe(ill);
16087 
16088 	/*
16089 	 * Sysid used to rely on the fact that netboots set domainname
16090 	 * and the like. Now that miniroot boots aren't strictly netboots
16091 	 * and miniroot network configuration is driven from userland
16092 	 * these things still need to be set. This situation can be detected
16093 	 * by comparing the interface being configured here to the one
16094 	 * dhcifname was set to reference by the boot loader. Once sysid is
16095 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16096 	 */
16097 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16098 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16099 	    (strlen(srpc_domain) == 0)) {
16100 		if (dhcpinit() != 0)
16101 			cmn_err(CE_WARN, "no cached dhcp response");
16102 	}
16103 
16104 	/*
16105 	 * This operation will complete in ip_rput_dlpi with either
16106 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16107 	 */
16108 	return (EINPROGRESS);
16109 bad:
16110 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16111 
16112 	freemsg(bind_mp);
16113 	freemsg(unbind_mp);
16114 	return (ENOMEM);
16115 }
16116 
16117 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16118 
16119 /*
16120  * DLPI and ARP is up.
16121  * Create all the IREs associated with an interface bring up multicast.
16122  * Set the interface flag and finish other initialization
16123  * that potentially had to be differed to after DL_BIND_ACK.
16124  */
16125 int
16126 ipif_up_done(ipif_t *ipif)
16127 {
16128 	ire_t	*ire_array[20];
16129 	ire_t	**irep = ire_array;
16130 	ire_t	**irep1;
16131 	ipaddr_t net_mask = 0;
16132 	ipaddr_t subnet_mask, route_mask;
16133 	ill_t	*ill = ipif->ipif_ill;
16134 	queue_t	*stq;
16135 	ipif_t	 *src_ipif;
16136 	ipif_t   *tmp_ipif;
16137 	boolean_t	flush_ire_cache = B_TRUE;
16138 	int	err = 0;
16139 	ire_t	**ipif_saved_irep = NULL;
16140 	int ipif_saved_ire_cnt;
16141 	int	cnt;
16142 	boolean_t	src_ipif_held = B_FALSE;
16143 	boolean_t	loopback = B_FALSE;
16144 	ip_stack_t	*ipst = ill->ill_ipst;
16145 
16146 	ip1dbg(("ipif_up_done(%s:%u)\n",
16147 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16148 	/* Check if this is a loopback interface */
16149 	if (ipif->ipif_ill->ill_wq == NULL)
16150 		loopback = B_TRUE;
16151 
16152 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16153 	/*
16154 	 * If all other interfaces for this ill are down or DEPRECATED,
16155 	 * or otherwise unsuitable for source address selection, remove
16156 	 * any IRE_CACHE entries for this ill to make sure source
16157 	 * address selection gets to take this new ipif into account.
16158 	 * No need to hold ill_lock while traversing the ipif list since
16159 	 * we are writer
16160 	 */
16161 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16162 	    tmp_ipif = tmp_ipif->ipif_next) {
16163 		if (((tmp_ipif->ipif_flags &
16164 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16165 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16166 		    (tmp_ipif == ipif))
16167 			continue;
16168 		/* first useable pre-existing interface */
16169 		flush_ire_cache = B_FALSE;
16170 		break;
16171 	}
16172 	if (flush_ire_cache)
16173 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16174 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16175 
16176 	/*
16177 	 * Figure out which way the send-to queue should go.  Only
16178 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16179 	 * should show up here.
16180 	 */
16181 	switch (ill->ill_net_type) {
16182 	case IRE_IF_RESOLVER:
16183 		stq = ill->ill_rq;
16184 		break;
16185 	case IRE_IF_NORESOLVER:
16186 	case IRE_LOOPBACK:
16187 		stq = ill->ill_wq;
16188 		break;
16189 	default:
16190 		return (EINVAL);
16191 	}
16192 
16193 	if (IS_LOOPBACK(ill)) {
16194 		/*
16195 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16196 		 * ipif_lookup_on_name(), but in the case of zones we can have
16197 		 * several loopback addresses on lo0. So all the interfaces with
16198 		 * loopback addresses need to be marked IRE_LOOPBACK.
16199 		 */
16200 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16201 		    htonl(INADDR_LOOPBACK))
16202 			ipif->ipif_ire_type = IRE_LOOPBACK;
16203 		else
16204 			ipif->ipif_ire_type = IRE_LOCAL;
16205 	}
16206 
16207 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16208 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16209 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16210 		/*
16211 		 * Can't use our source address. Select a different
16212 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16213 		 */
16214 		src_ipif = ipif_select_source(ipif->ipif_ill,
16215 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16216 		if (src_ipif == NULL)
16217 			src_ipif = ipif;	/* Last resort */
16218 		else
16219 			src_ipif_held = B_TRUE;
16220 	} else {
16221 		src_ipif = ipif;
16222 	}
16223 
16224 	/* Create all the IREs associated with this interface */
16225 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16226 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16227 
16228 		/*
16229 		 * If we're on a labeled system then make sure that zone-
16230 		 * private addresses have proper remote host database entries.
16231 		 */
16232 		if (is_system_labeled() &&
16233 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16234 		    !tsol_check_interface_address(ipif))
16235 			return (EINVAL);
16236 
16237 		/* Register the source address for __sin6_src_id */
16238 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16239 		    ipif->ipif_zoneid, ipst);
16240 		if (err != 0) {
16241 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16242 			return (err);
16243 		}
16244 
16245 		/* If the interface address is set, create the local IRE. */
16246 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16247 		    (void *)ipif,
16248 		    ipif->ipif_ire_type,
16249 		    ntohl(ipif->ipif_lcl_addr)));
16250 		*irep++ = ire_create(
16251 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16252 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16253 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16254 		    NULL,				/* no gateway */
16255 		    &ip_loopback_mtuplus,		/* max frag size */
16256 		    NULL,
16257 		    ipif->ipif_rq,			/* recv-from queue */
16258 		    NULL,				/* no send-to queue */
16259 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16260 		    ipif,
16261 		    0,
16262 		    0,
16263 		    0,
16264 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16265 		    RTF_PRIVATE : 0,
16266 		    &ire_uinfo_null,
16267 		    NULL,
16268 		    NULL,
16269 		    ipst);
16270 	} else {
16271 		ip1dbg((
16272 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16273 		    ipif->ipif_ire_type,
16274 		    ntohl(ipif->ipif_lcl_addr),
16275 		    (uint_t)ipif->ipif_flags));
16276 	}
16277 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16278 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16279 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16280 	} else {
16281 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16282 	}
16283 
16284 	subnet_mask = ipif->ipif_net_mask;
16285 
16286 	/*
16287 	 * If mask was not specified, use natural netmask of
16288 	 * interface address. Also, store this mask back into the
16289 	 * ipif struct.
16290 	 */
16291 	if (subnet_mask == 0) {
16292 		subnet_mask = net_mask;
16293 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16294 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16295 		    ipif->ipif_v6subnet);
16296 	}
16297 
16298 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16299 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16300 	    ipif->ipif_subnet != INADDR_ANY) {
16301 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16302 
16303 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16304 			route_mask = IP_HOST_MASK;
16305 		} else {
16306 			route_mask = subnet_mask;
16307 		}
16308 
16309 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16310 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16311 		    (void *)ipif, (void *)ill,
16312 		    ill->ill_net_type,
16313 		    ntohl(ipif->ipif_subnet)));
16314 		*irep++ = ire_create(
16315 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16316 		    (uchar_t *)&route_mask,		/* mask */
16317 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16318 		    NULL,				/* no gateway */
16319 		    &ipif->ipif_mtu,			/* max frag */
16320 		    NULL,
16321 		    NULL,				/* no recv queue */
16322 		    stq,				/* send-to queue */
16323 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16324 		    ipif,
16325 		    0,
16326 		    0,
16327 		    0,
16328 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16329 		    &ire_uinfo_null,
16330 		    NULL,
16331 		    NULL,
16332 		    ipst);
16333 	}
16334 
16335 	/*
16336 	 * Create any necessary broadcast IREs.
16337 	 */
16338 	if (ipif->ipif_flags & IPIF_BROADCAST)
16339 		irep = ipif_create_bcast_ires(ipif, irep);
16340 
16341 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16342 
16343 	/* If an earlier ire_create failed, get out now */
16344 	for (irep1 = irep; irep1 > ire_array; ) {
16345 		irep1--;
16346 		if (*irep1 == NULL) {
16347 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16348 			err = ENOMEM;
16349 			goto bad;
16350 		}
16351 	}
16352 
16353 	/*
16354 	 * Need to atomically check for IP address availability under
16355 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16356 	 * ills or new ipifs can be added while we are checking availability.
16357 	 */
16358 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16359 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16360 	/* Mark it up, and increment counters. */
16361 	ipif->ipif_flags |= IPIF_UP;
16362 	ill->ill_ipif_up_count++;
16363 	err = ip_addr_availability_check(ipif);
16364 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16365 	rw_exit(&ipst->ips_ill_g_lock);
16366 
16367 	if (err != 0) {
16368 		/*
16369 		 * Our address may already be up on the same ill. In this case,
16370 		 * the ARP entry for our ipif replaced the one for the other
16371 		 * ipif. So we don't want to delete it (otherwise the other ipif
16372 		 * would be unable to send packets).
16373 		 * ip_addr_availability_check() identifies this case for us and
16374 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16375 		 * which is the expected error code.
16376 		 */
16377 		if (err == EADDRINUSE) {
16378 			freemsg(ipif->ipif_arp_del_mp);
16379 			ipif->ipif_arp_del_mp = NULL;
16380 			err = EADDRNOTAVAIL;
16381 		}
16382 		ill->ill_ipif_up_count--;
16383 		ipif->ipif_flags &= ~IPIF_UP;
16384 		goto bad;
16385 	}
16386 
16387 	/*
16388 	 * Add in all newly created IREs.  ire_create_bcast() has
16389 	 * already checked for duplicates of the IRE_BROADCAST type.
16390 	 */
16391 	for (irep1 = irep; irep1 > ire_array; ) {
16392 		irep1--;
16393 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16394 		/*
16395 		 * refheld by ire_add. refele towards the end of the func
16396 		 */
16397 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16398 	}
16399 
16400 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16401 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16402 	ipif_saved_irep = ipif_recover_ire(ipif);
16403 
16404 	if (!loopback) {
16405 		/*
16406 		 * If the broadcast address has been set, make sure it makes
16407 		 * sense based on the interface address.
16408 		 * Only match on ill since we are sharing broadcast addresses.
16409 		 */
16410 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16411 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16412 			ire_t	*ire;
16413 
16414 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16415 			    IRE_BROADCAST, ipif, ALL_ZONES,
16416 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16417 
16418 			if (ire == NULL) {
16419 				/*
16420 				 * If there isn't a matching broadcast IRE,
16421 				 * revert to the default for this netmask.
16422 				 */
16423 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16424 				mutex_enter(&ipif->ipif_ill->ill_lock);
16425 				ipif_set_default(ipif);
16426 				mutex_exit(&ipif->ipif_ill->ill_lock);
16427 			} else {
16428 				ire_refrele(ire);
16429 			}
16430 		}
16431 
16432 	}
16433 
16434 	if (ill->ill_need_recover_multicast) {
16435 		/*
16436 		 * Need to recover all multicast memberships in the driver.
16437 		 * This had to be deferred until we had attached.  The same
16438 		 * code exists in ipif_up_done_v6() to recover IPv6
16439 		 * memberships.
16440 		 *
16441 		 * Note that it would be preferable to unconditionally do the
16442 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16443 		 * that since ill_join_allmulti() depends on ill_dl_up being
16444 		 * set, and it is not set until we receive a DL_BIND_ACK after
16445 		 * having called ill_dl_up().
16446 		 */
16447 		ill_recover_multicast(ill);
16448 	}
16449 
16450 	if (ill->ill_ipif_up_count == 1) {
16451 		/*
16452 		 * Since the interface is now up, it may now be active.
16453 		 */
16454 		if (IS_UNDER_IPMP(ill))
16455 			ipmp_ill_refresh_active(ill);
16456 
16457 		/*
16458 		 * If this is an IPMP interface, we may now be able to
16459 		 * establish ARP entries.
16460 		 */
16461 		if (IS_IPMP(ill))
16462 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16463 	}
16464 
16465 	/* Join the allhosts multicast address */
16466 	ipif_multicast_up(ipif);
16467 
16468 	/*
16469 	 * See if anybody else would benefit from our new ipif.
16470 	 */
16471 	if (!loopback &&
16472 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16473 		ill_update_source_selection(ill);
16474 	}
16475 
16476 	for (irep1 = irep; irep1 > ire_array; ) {
16477 		irep1--;
16478 		if (*irep1 != NULL) {
16479 			/* was held in ire_add */
16480 			ire_refrele(*irep1);
16481 		}
16482 	}
16483 
16484 	cnt = ipif_saved_ire_cnt;
16485 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16486 		if (*irep1 != NULL) {
16487 			/* was held in ire_add */
16488 			ire_refrele(*irep1);
16489 		}
16490 	}
16491 
16492 	if (!loopback && ipif->ipif_addr_ready) {
16493 		/* Broadcast an address mask reply. */
16494 		ipif_mask_reply(ipif);
16495 	}
16496 	if (ipif_saved_irep != NULL) {
16497 		kmem_free(ipif_saved_irep,
16498 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16499 	}
16500 	if (src_ipif_held)
16501 		ipif_refrele(src_ipif);
16502 
16503 	/*
16504 	 * This had to be deferred until we had bound.  Tell routing sockets and
16505 	 * others that this interface is up if it looks like the address has
16506 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16507 	 * duplicate address detection to do its thing.
16508 	 */
16509 	if (ipif->ipif_addr_ready)
16510 		ipif_up_notify(ipif);
16511 	return (0);
16512 
16513 bad:
16514 	ip1dbg(("ipif_up_done: FAILED \n"));
16515 
16516 	while (irep > ire_array) {
16517 		irep--;
16518 		if (*irep != NULL)
16519 			ire_delete(*irep);
16520 	}
16521 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16522 
16523 	if (ipif_saved_irep != NULL) {
16524 		kmem_free(ipif_saved_irep,
16525 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16526 	}
16527 	if (src_ipif_held)
16528 		ipif_refrele(src_ipif);
16529 
16530 	ipif_resolver_down(ipif);
16531 	return (err);
16532 }
16533 
16534 /*
16535  * Turn off the ARP with the ILLF_NOARP flag.
16536  */
16537 static int
16538 ill_arp_off(ill_t *ill)
16539 {
16540 	mblk_t	*arp_off_mp = NULL;
16541 	mblk_t	*arp_on_mp = NULL;
16542 
16543 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16544 
16545 	ASSERT(IAM_WRITER_ILL(ill));
16546 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16547 
16548 	/*
16549 	 * If the on message is still around we've already done
16550 	 * an arp_off without doing an arp_on thus there is no
16551 	 * work needed.
16552 	 */
16553 	if (ill->ill_arp_on_mp != NULL)
16554 		return (0);
16555 
16556 	/*
16557 	 * Allocate an ARP on message (to be saved) and an ARP off message
16558 	 */
16559 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16560 	if (!arp_off_mp)
16561 		return (ENOMEM);
16562 
16563 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16564 	if (!arp_on_mp)
16565 		goto failed;
16566 
16567 	ASSERT(ill->ill_arp_on_mp == NULL);
16568 	ill->ill_arp_on_mp = arp_on_mp;
16569 
16570 	/* Send an AR_INTERFACE_OFF request */
16571 	putnext(ill->ill_rq, arp_off_mp);
16572 	return (0);
16573 failed:
16574 
16575 	if (arp_off_mp)
16576 		freemsg(arp_off_mp);
16577 	return (ENOMEM);
16578 }
16579 
16580 /*
16581  * Turn on ARP by turning off the ILLF_NOARP flag.
16582  */
16583 static int
16584 ill_arp_on(ill_t *ill)
16585 {
16586 	mblk_t	*mp;
16587 
16588 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16589 
16590 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16591 
16592 	ASSERT(IAM_WRITER_ILL(ill));
16593 	/*
16594 	 * Send an AR_INTERFACE_ON request if we have already done
16595 	 * an arp_off (which allocated the message).
16596 	 */
16597 	if (ill->ill_arp_on_mp != NULL) {
16598 		mp = ill->ill_arp_on_mp;
16599 		ill->ill_arp_on_mp = NULL;
16600 		putnext(ill->ill_rq, mp);
16601 	}
16602 	return (0);
16603 }
16604 
16605 /*
16606  * Checks for availbility of a usable source address (if there is one) when the
16607  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16608  * this selection is done regardless of the destination.
16609  */
16610 boolean_t
16611 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16612 {
16613 	uint_t	ifindex;
16614 	ipif_t	*ipif = NULL;
16615 	ill_t	*uill;
16616 	boolean_t isv6;
16617 	ip_stack_t	*ipst = ill->ill_ipst;
16618 
16619 	ASSERT(ill != NULL);
16620 
16621 	isv6 = ill->ill_isv6;
16622 	ifindex = ill->ill_usesrc_ifindex;
16623 	if (ifindex != 0) {
16624 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16625 		    NULL, ipst);
16626 		if (uill == NULL)
16627 			return (NULL);
16628 		mutex_enter(&uill->ill_lock);
16629 		for (ipif = uill->ill_ipif; ipif != NULL;
16630 		    ipif = ipif->ipif_next) {
16631 			if (!IPIF_CAN_LOOKUP(ipif))
16632 				continue;
16633 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16634 				continue;
16635 			if (!(ipif->ipif_flags & IPIF_UP))
16636 				continue;
16637 			if (ipif->ipif_zoneid != zoneid)
16638 				continue;
16639 			if ((isv6 &&
16640 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16641 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16642 				continue;
16643 			mutex_exit(&uill->ill_lock);
16644 			ill_refrele(uill);
16645 			return (B_TRUE);
16646 		}
16647 		mutex_exit(&uill->ill_lock);
16648 		ill_refrele(uill);
16649 	}
16650 	return (B_FALSE);
16651 }
16652 
16653 /*
16654  * IP source address type, sorted from worst to best.  For a given type,
16655  * always prefer IP addresses on the same subnet.  All-zones addresses are
16656  * suboptimal because they pose problems with unlabeled destinations.
16657  */
16658 typedef enum {
16659 	IPIF_NONE,
16660 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16661 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16662 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16663 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16664 	IPIF_DIFFNET,			/* normal and different subnet */
16665 	IPIF_SAMENET			/* normal and same subnet */
16666 } ipif_type_t;
16667 
16668 /*
16669  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16670  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16671  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16672  * the first one, unless IPMP is used in which case we round-robin among them;
16673  * see below for more.
16674  *
16675  * Returns NULL if there is no suitable source address for the ill.
16676  * This only occurs when there is no valid source address for the ill.
16677  */
16678 ipif_t *
16679 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16680 {
16681 	ill_t	*usill = NULL;
16682 	ill_t	*ipmp_ill = NULL;
16683 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16684 	ipif_type_t type, best_type;
16685 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16686 	ip_stack_t *ipst = ill->ill_ipst;
16687 	boolean_t samenet;
16688 
16689 	if (ill->ill_usesrc_ifindex != 0) {
16690 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16691 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16692 		if (usill != NULL)
16693 			ill = usill;	/* Select source from usesrc ILL */
16694 		else
16695 			return (NULL);
16696 	}
16697 
16698 	/*
16699 	 * Test addresses should never be used for source address selection,
16700 	 * so if we were passed one, switch to the IPMP meta-interface.
16701 	 */
16702 	if (IS_UNDER_IPMP(ill)) {
16703 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16704 			ill = ipmp_ill;	/* Select source from IPMP ill */
16705 		else
16706 			return (NULL);
16707 	}
16708 
16709 	/*
16710 	 * If we're dealing with an unlabeled destination on a labeled system,
16711 	 * make sure that we ignore source addresses that are incompatible with
16712 	 * the destination's default label.  That destination's default label
16713 	 * must dominate the minimum label on the source address.
16714 	 */
16715 	dst_rhtp = NULL;
16716 	if (is_system_labeled()) {
16717 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16718 		if (dst_rhtp == NULL)
16719 			return (NULL);
16720 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16721 			TPC_RELE(dst_rhtp);
16722 			dst_rhtp = NULL;
16723 		}
16724 	}
16725 
16726 	/*
16727 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16728 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16729 	 * After selecting the right ipif, under ill_lock make sure ipif is
16730 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16731 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16732 	 * but not under a lock.
16733 	 */
16734 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16735 retry:
16736 	/*
16737 	 * For source address selection, we treat the ipif list as circular
16738 	 * and continue until we get back to where we started.  This allows
16739 	 * IPMP to vary source address selection (which improves inbound load
16740 	 * spreading) by caching its last ending point and starting from
16741 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16742 	 * ills since that can't happen on the IPMP ill.
16743 	 */
16744 	start_ipif = ill->ill_ipif;
16745 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16746 		start_ipif = ill->ill_src_ipif;
16747 
16748 	ipif = start_ipif;
16749 	best_ipif = NULL;
16750 	best_type = IPIF_NONE;
16751 	do {
16752 		if ((next_ipif = ipif->ipif_next) == NULL)
16753 			next_ipif = ill->ill_ipif;
16754 
16755 		if (!IPIF_CAN_LOOKUP(ipif))
16756 			continue;
16757 		/* Always skip NOLOCAL and ANYCAST interfaces */
16758 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16759 			continue;
16760 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16761 			continue;
16762 		if (ipif->ipif_zoneid != zoneid &&
16763 		    ipif->ipif_zoneid != ALL_ZONES)
16764 			continue;
16765 
16766 		/*
16767 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16768 		 * are not valid as source addresses.
16769 		 */
16770 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16771 			continue;
16772 
16773 		/*
16774 		 * Check compatibility of local address for destination's
16775 		 * default label if we're on a labeled system.	Incompatible
16776 		 * addresses can't be used at all.
16777 		 */
16778 		if (dst_rhtp != NULL) {
16779 			boolean_t incompat;
16780 
16781 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16782 			    IPV4_VERSION, B_FALSE);
16783 			if (src_rhtp == NULL)
16784 				continue;
16785 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16786 			    src_rhtp->tpc_tp.tp_doi !=
16787 			    dst_rhtp->tpc_tp.tp_doi ||
16788 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16789 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16790 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16791 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16792 			TPC_RELE(src_rhtp);
16793 			if (incompat)
16794 				continue;
16795 		}
16796 
16797 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16798 
16799 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16800 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16801 			    IPIF_DIFFNET_DEPRECATED;
16802 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16803 			type = samenet ? IPIF_SAMENET_ALLZONES :
16804 			    IPIF_DIFFNET_ALLZONES;
16805 		} else {
16806 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16807 		}
16808 
16809 		if (type > best_type) {
16810 			best_type = type;
16811 			best_ipif = ipif;
16812 			if (best_type == IPIF_SAMENET)
16813 				break; /* can't get better */
16814 		}
16815 	} while ((ipif = next_ipif) != start_ipif);
16816 
16817 	if ((ipif = best_ipif) != NULL) {
16818 		mutex_enter(&ipif->ipif_ill->ill_lock);
16819 		if (!IPIF_CAN_LOOKUP(ipif)) {
16820 			mutex_exit(&ipif->ipif_ill->ill_lock);
16821 			goto retry;
16822 		}
16823 		ipif_refhold_locked(ipif);
16824 
16825 		/*
16826 		 * For IPMP, update the source ipif rotor to the next ipif,
16827 		 * provided we can look it up.  (We must not use it if it's
16828 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16829 		 * ipif_free() checked ill_src_ipif.)
16830 		 */
16831 		if (IS_IPMP(ill) && ipif != NULL) {
16832 			next_ipif = ipif->ipif_next;
16833 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16834 				ill->ill_src_ipif = next_ipif;
16835 			else
16836 				ill->ill_src_ipif = NULL;
16837 		}
16838 		mutex_exit(&ipif->ipif_ill->ill_lock);
16839 	}
16840 
16841 	rw_exit(&ipst->ips_ill_g_lock);
16842 	if (usill != NULL)
16843 		ill_refrele(usill);
16844 	if (ipmp_ill != NULL)
16845 		ill_refrele(ipmp_ill);
16846 	if (dst_rhtp != NULL)
16847 		TPC_RELE(dst_rhtp);
16848 
16849 #ifdef DEBUG
16850 	if (ipif == NULL) {
16851 		char buf1[INET6_ADDRSTRLEN];
16852 
16853 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
16854 		    ill->ill_name,
16855 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
16856 	} else {
16857 		char buf1[INET6_ADDRSTRLEN];
16858 		char buf2[INET6_ADDRSTRLEN];
16859 
16860 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
16861 		    ipif->ipif_ill->ill_name,
16862 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
16863 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
16864 		    buf2, sizeof (buf2))));
16865 	}
16866 #endif /* DEBUG */
16867 	return (ipif);
16868 }
16869 
16870 /*
16871  * If old_ipif is not NULL, see if ipif was derived from old
16872  * ipif and if so, recreate the interface route by re-doing
16873  * source address selection. This happens when ipif_down ->
16874  * ipif_update_other_ipifs calls us.
16875  *
16876  * If old_ipif is NULL, just redo the source address selection
16877  * if needed. This happens when ipif_up_done calls us.
16878  */
16879 static void
16880 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
16881 {
16882 	ire_t *ire;
16883 	ire_t *ipif_ire;
16884 	queue_t *stq;
16885 	ipif_t *nipif;
16886 	ill_t *ill;
16887 	boolean_t need_rele = B_FALSE;
16888 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16889 
16890 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
16891 	ASSERT(IAM_WRITER_IPIF(ipif));
16892 
16893 	ill = ipif->ipif_ill;
16894 	if (!(ipif->ipif_flags &
16895 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16896 		/*
16897 		 * Can't possibly have borrowed the source
16898 		 * from old_ipif.
16899 		 */
16900 		return;
16901 	}
16902 
16903 	/*
16904 	 * Is there any work to be done? No work if the address
16905 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
16906 	 * ipif_select_source() does not borrow addresses from
16907 	 * NOLOCAL and ANYCAST interfaces).
16908 	 */
16909 	if ((old_ipif != NULL) &&
16910 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
16911 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
16912 	    (old_ipif->ipif_flags &
16913 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
16914 		return;
16915 	}
16916 
16917 	/*
16918 	 * Perform the same checks as when creating the
16919 	 * IRE_INTERFACE in ipif_up_done.
16920 	 */
16921 	if (!(ipif->ipif_flags & IPIF_UP))
16922 		return;
16923 
16924 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
16925 	    (ipif->ipif_subnet == INADDR_ANY))
16926 		return;
16927 
16928 	ipif_ire = ipif_to_ire(ipif);
16929 	if (ipif_ire == NULL)
16930 		return;
16931 
16932 	/*
16933 	 * We know that ipif uses some other source for its
16934 	 * IRE_INTERFACE. Is it using the source of this
16935 	 * old_ipif?
16936 	 */
16937 	if (old_ipif != NULL &&
16938 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
16939 		ire_refrele(ipif_ire);
16940 		return;
16941 	}
16942 	if (ip_debug > 2) {
16943 		/* ip1dbg */
16944 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
16945 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
16946 	}
16947 
16948 	stq = ipif_ire->ire_stq;
16949 
16950 	/*
16951 	 * Can't use our source address. Select a different
16952 	 * source address for the IRE_INTERFACE.
16953 	 */
16954 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
16955 	if (nipif == NULL) {
16956 		/* Last resort - all ipif's have IPIF_NOLOCAL */
16957 		nipif = ipif;
16958 	} else {
16959 		need_rele = B_TRUE;
16960 	}
16961 
16962 	ire = ire_create(
16963 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
16964 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
16965 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
16966 	    NULL,				/* no gateway */
16967 	    &ipif->ipif_mtu,			/* max frag */
16968 	    NULL,				/* no src nce */
16969 	    NULL,				/* no recv from queue */
16970 	    stq,				/* send-to queue */
16971 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16972 	    ipif,
16973 	    0,
16974 	    0,
16975 	    0,
16976 	    0,
16977 	    &ire_uinfo_null,
16978 	    NULL,
16979 	    NULL,
16980 	    ipst);
16981 
16982 	if (ire != NULL) {
16983 		ire_t *ret_ire;
16984 		int error;
16985 
16986 		/*
16987 		 * We don't need ipif_ire anymore. We need to delete
16988 		 * before we add so that ire_add does not detect
16989 		 * duplicates.
16990 		 */
16991 		ire_delete(ipif_ire);
16992 		ret_ire = ire;
16993 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
16994 		ASSERT(error == 0);
16995 		ASSERT(ire == ret_ire);
16996 		/* Held in ire_add */
16997 		ire_refrele(ret_ire);
16998 	}
16999 	/*
17000 	 * Either we are falling through from above or could not
17001 	 * allocate a replacement.
17002 	 */
17003 	ire_refrele(ipif_ire);
17004 	if (need_rele)
17005 		ipif_refrele(nipif);
17006 }
17007 
17008 /*
17009  * This old_ipif is going away.
17010  *
17011  * Determine if any other ipif's are using our address as
17012  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17013  * IPIF_DEPRECATED).
17014  * Find the IRE_INTERFACE for such ipifs and recreate them
17015  * to use an different source address following the rules in
17016  * ipif_up_done.
17017  */
17018 static void
17019 ipif_update_other_ipifs(ipif_t *old_ipif)
17020 {
17021 	ipif_t	*ipif;
17022 	ill_t	*ill;
17023 	char	buf[INET6_ADDRSTRLEN];
17024 
17025 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17026 
17027 	ill = old_ipif->ipif_ill;
17028 
17029 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17030 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17031 
17032 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17033 		if (ipif == old_ipif)
17034 			continue;
17035 		ipif_recreate_interface_routes(old_ipif, ipif);
17036 	}
17037 }
17038 
17039 /* ARGSUSED */
17040 int
17041 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17042 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17043 {
17044 	/*
17045 	 * ill_phyint_reinit merged the v4 and v6 into a single
17046 	 * ipsq.  We might not have been able to complete the
17047 	 * operation in ipif_set_values, if we could not become
17048 	 * exclusive.  If so restart it here.
17049 	 */
17050 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17051 }
17052 
17053 /*
17054  * Can operate on either a module or a driver queue.
17055  * Returns an error if not a module queue.
17056  */
17057 /* ARGSUSED */
17058 int
17059 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17060     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17061 {
17062 	queue_t		*q1 = q;
17063 	char 		*cp;
17064 	char		interf_name[LIFNAMSIZ];
17065 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17066 
17067 	if (q->q_next == NULL) {
17068 		ip1dbg((
17069 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17070 		return (EINVAL);
17071 	}
17072 
17073 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17074 		return (EALREADY);
17075 
17076 	do {
17077 		q1 = q1->q_next;
17078 	} while (q1->q_next);
17079 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17080 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17081 
17082 	/*
17083 	 * Here we are not going to delay the ioack until after
17084 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17085 	 * original ioctl message before sending the requests.
17086 	 */
17087 	return (ipif_set_values(q, mp, interf_name, &ppa));
17088 }
17089 
17090 /* ARGSUSED */
17091 int
17092 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17093     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17094 {
17095 	return (ENXIO);
17096 }
17097 
17098 /*
17099  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17100  * minimum (but complete) set exist.  This is necessary when adding or
17101  * removing an interface to/from an IPMP group, since interfaces in an
17102  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17103  * its test address subnets overlap with IPMP data addresses).	It's also
17104  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17105  * interface when the nominated broadcast interface changes.
17106  */
17107 void
17108 ill_refresh_bcast(ill_t *ill)
17109 {
17110 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17111 	ire_t **irep;
17112 	ipif_t *ipif;
17113 
17114 	ASSERT(!ill->ill_isv6);
17115 	ASSERT(IAM_WRITER_ILL(ill));
17116 
17117 	/*
17118 	 * Remove any old broadcast IREs.
17119 	 */
17120 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17121 	    ill_broadcast_delete, ill, ill);
17122 
17123 	/*
17124 	 * Create new ones for any ipifs that are up and broadcast-capable.
17125 	 */
17126 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17127 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17128 		    (IPIF_UP|IPIF_BROADCAST))
17129 			continue;
17130 
17131 		irep = ipif_create_bcast_ires(ipif, ire_array);
17132 		while (irep-- > ire_array) {
17133 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17134 			if (*irep != NULL)
17135 				ire_refrele(*irep);
17136 		}
17137 	}
17138 }
17139 
17140 /*
17141  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17142  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17143  * ire_check_and_create_bcast()).
17144  */
17145 static ire_t **
17146 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17147 {
17148 	ipaddr_t addr;
17149 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17150 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17151 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17152 
17153 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17154 
17155 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17156 
17157 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17158 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17159 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17160 
17161 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17162 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17163 
17164 	/*
17165 	 * For backward compatibility, we create net broadcast IREs based on
17166 	 * the old "IP address class system", since some old machines only
17167 	 * respond to these class derived net broadcast.  However, we must not
17168 	 * create these net broadcast IREs if the subnetmask is shorter than
17169 	 * the IP address class based derived netmask.  Otherwise, we may
17170 	 * create a net broadcast address which is the same as an IP address
17171 	 * on the subnet -- and then TCP will refuse to talk to that address.
17172 	 */
17173 	if (netmask < subnetmask) {
17174 		addr = netmask & ipif->ipif_subnet;
17175 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17176 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17177 		    flags);
17178 	}
17179 
17180 	/*
17181 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17182 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17183 	 * created.  Creating these broadcast IREs will only create confusion
17184 	 * as `addr' will be the same as the IP address.
17185 	 */
17186 	if (subnetmask != 0xFFFFFFFF) {
17187 		addr = ipif->ipif_subnet;
17188 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17189 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17190 		    irep, flags);
17191 	}
17192 
17193 	return (irep);
17194 }
17195 
17196 /*
17197  * Broadcast IRE info structure used in the functions below.  Since we
17198  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17199  */
17200 typedef struct bcast_ireinfo {
17201 	uchar_t		bi_type;	/* BCAST_* value from below */
17202 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17203 			bi_needrep:1,	/* do we need to replace it? */
17204 			bi_haverep:1,	/* have we replaced it? */
17205 			bi_pad:5;
17206 	ipaddr_t	bi_addr;	/* IRE address */
17207 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17208 } bcast_ireinfo_t;
17209 
17210 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17211 
17212 /*
17213  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17214  * return B_TRUE if it should immediately be used to recreate the IRE.
17215  */
17216 static boolean_t
17217 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17218 {
17219 	ipaddr_t addr;
17220 
17221 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17222 
17223 	switch (bireinfop->bi_type) {
17224 	case BCAST_NET:
17225 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17226 		if (addr != bireinfop->bi_addr)
17227 			return (B_FALSE);
17228 		break;
17229 	case BCAST_SUBNET:
17230 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17231 			return (B_FALSE);
17232 		break;
17233 	}
17234 
17235 	bireinfop->bi_needrep = 1;
17236 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17237 		if (bireinfop->bi_backup == NULL)
17238 			bireinfop->bi_backup = ipif;
17239 		return (B_FALSE);
17240 	}
17241 	return (B_TRUE);
17242 }
17243 
17244 /*
17245  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17246  * them ala ire_check_and_create_bcast().
17247  */
17248 static ire_t **
17249 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17250 {
17251 	ipaddr_t mask, addr;
17252 
17253 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17254 
17255 	addr = bireinfop->bi_addr;
17256 	irep = ire_create_bcast(ipif, addr, irep);
17257 
17258 	switch (bireinfop->bi_type) {
17259 	case BCAST_NET:
17260 		mask = ip_net_mask(ipif->ipif_subnet);
17261 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17262 		break;
17263 	case BCAST_SUBNET:
17264 		mask = ipif->ipif_net_mask;
17265 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17266 		break;
17267 	}
17268 
17269 	bireinfop->bi_haverep = 1;
17270 	return (irep);
17271 }
17272 
17273 /*
17274  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17275  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17276  * that are going away are still needed.  If so, have ipif_create_bcast()
17277  * recreate them (except for the deprecated case, as explained below).
17278  */
17279 static ire_t **
17280 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17281     ire_t **irep)
17282 {
17283 	int i;
17284 	ipif_t *ipif;
17285 
17286 	ASSERT(!ill->ill_isv6);
17287 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17288 		/*
17289 		 * Skip this ipif if it's (a) the one being taken down, (b)
17290 		 * not in the same zone, or (c) has no valid local address.
17291 		 */
17292 		if (ipif == test_ipif ||
17293 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17294 		    ipif->ipif_subnet == 0 ||
17295 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17296 		    (IPIF_UP|IPIF_BROADCAST))
17297 			continue;
17298 
17299 		/*
17300 		 * For each dying IRE that hasn't yet been replaced, see if
17301 		 * `ipif' needs it and whether the IRE should be recreated on
17302 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17303 		 * will return B_FALSE even if `ipif' needs the IRE on the
17304 		 * hopes that we'll later find a needy non-deprecated ipif.
17305 		 * However, the ipif is recorded in bi_backup for possible
17306 		 * subsequent use by ipif_check_bcast_ires().
17307 		 */
17308 		for (i = 0; i < BCAST_COUNT; i++) {
17309 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17310 				continue;
17311 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17312 				continue;
17313 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17314 		}
17315 
17316 		/*
17317 		 * If we've replaced all of the broadcast IREs that are going
17318 		 * to be taken down, we know we're done.
17319 		 */
17320 		for (i = 0; i < BCAST_COUNT; i++) {
17321 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17322 				break;
17323 		}
17324 		if (i == BCAST_COUNT)
17325 			break;
17326 	}
17327 	return (irep);
17328 }
17329 
17330 /*
17331  * Check if `test_ipif' (which is going away) is associated with any existing
17332  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17333  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17334  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17335  *
17336  * This is necessary because broadcast IREs are shared.  In particular, a
17337  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17338  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17339  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17340  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17341  * same zone, they will share the same set of broadcast IREs.
17342  *
17343  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17344  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17345  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17346  */
17347 static void
17348 ipif_check_bcast_ires(ipif_t *test_ipif)
17349 {
17350 	ill_t		*ill = test_ipif->ipif_ill;
17351 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17352 	ire_t		**irep1, **irep = &ire_array[0];
17353 	uint_t 		i, willdie;
17354 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17355 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17356 
17357 	ASSERT(!test_ipif->ipif_isv6);
17358 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17359 
17360 	/*
17361 	 * No broadcast IREs for the LOOPBACK interface
17362 	 * or others such as point to point and IPIF_NOXMIT.
17363 	 */
17364 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17365 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17366 		return;
17367 
17368 	bzero(bireinfo, sizeof (bireinfo));
17369 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17370 	bireinfo[0].bi_addr = 0;
17371 
17372 	bireinfo[1].bi_type = BCAST_ALLONES;
17373 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17374 
17375 	bireinfo[2].bi_type = BCAST_NET;
17376 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17377 
17378 	if (test_ipif->ipif_net_mask != 0)
17379 		mask = test_ipif->ipif_net_mask;
17380 	bireinfo[3].bi_type = BCAST_SUBNET;
17381 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17382 
17383 	/*
17384 	 * Figure out what (if any) broadcast IREs will die as a result of
17385 	 * `test_ipif' going away.  If none will die, we're done.
17386 	 */
17387 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17388 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17389 		    test_ipif, ALL_ZONES, NULL,
17390 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17391 		if (ire != NULL) {
17392 			willdie++;
17393 			bireinfo[i].bi_willdie = 1;
17394 			ire_refrele(ire);
17395 		}
17396 	}
17397 
17398 	if (willdie == 0)
17399 		return;
17400 
17401 	/*
17402 	 * Walk through all the ipifs that will be affected by the dying IREs,
17403 	 * and recreate the IREs as necessary. Note that all interfaces in an
17404 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17405 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17406 	 * that broadcast IREs end up on it whenever possible).
17407 	 */
17408 	if (IS_UNDER_IPMP(ill))
17409 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17410 
17411 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17412 
17413 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17414 		ipmp_illgrp_t *illg = ill->ill_grp;
17415 
17416 		ill = list_head(&illg->ig_if);
17417 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17418 			for (i = 0; i < BCAST_COUNT; i++) {
17419 				if (bireinfo[i].bi_willdie &&
17420 				    !bireinfo[i].bi_haverep)
17421 					break;
17422 			}
17423 			if (i == BCAST_COUNT)
17424 				break;
17425 
17426 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17427 		}
17428 	}
17429 
17430 	/*
17431 	 * Scan through the set of broadcast IREs and see if there are any
17432 	 * that we need to replace that have not yet been replaced.  If so,
17433 	 * replace them using the appropriate backup ipif.
17434 	 */
17435 	for (i = 0; i < BCAST_COUNT; i++) {
17436 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17437 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17438 			    &bireinfo[i], irep);
17439 	}
17440 
17441 	/*
17442 	 * If we can't create all of them, don't add any of them.  (Code in
17443 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17444 	 * non-loopback copy and loopback copy for a given address.)
17445 	 */
17446 	for (irep1 = irep; irep1 > ire_array; ) {
17447 		irep1--;
17448 		if (*irep1 == NULL) {
17449 			ip0dbg(("ipif_check_bcast_ires: can't create "
17450 			    "IRE_BROADCAST, memory allocation failure\n"));
17451 			while (irep > ire_array) {
17452 				irep--;
17453 				if (*irep != NULL)
17454 					ire_delete(*irep);
17455 			}
17456 			return;
17457 		}
17458 	}
17459 
17460 	for (irep1 = irep; irep1 > ire_array; ) {
17461 		irep1--;
17462 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17463 			ire_refrele(*irep1);		/* Held in ire_add */
17464 	}
17465 }
17466 
17467 /*
17468  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17469  * from lifr_flags and the name from lifr_name.
17470  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17471  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17472  * Returns EINPROGRESS when mp has been consumed by queueing it on
17473  * ill_pending_mp and the ioctl will complete in ip_rput.
17474  *
17475  * Can operate on either a module or a driver queue.
17476  * Returns an error if not a module queue.
17477  */
17478 /* ARGSUSED */
17479 int
17480 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17481     ip_ioctl_cmd_t *ipip, void *if_req)
17482 {
17483 	ill_t	*ill = q->q_ptr;
17484 	phyint_t *phyi;
17485 	ip_stack_t *ipst;
17486 	struct lifreq *lifr = if_req;
17487 	uint64_t new_flags;
17488 
17489 	ASSERT(ipif != NULL);
17490 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17491 
17492 	if (q->q_next == NULL) {
17493 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17494 		return (EINVAL);
17495 	}
17496 
17497 	/*
17498 	 * If we are not writer on 'q' then this interface exists already
17499 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17500 	 * so return EALREADY.
17501 	 */
17502 	if (ill != ipif->ipif_ill)
17503 		return (EALREADY);
17504 
17505 	if (ill->ill_name[0] != '\0')
17506 		return (EALREADY);
17507 
17508 	/*
17509 	 * If there's another ill already with the requested name, ensure
17510 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17511 	 * fuse together two unrelated ills, which will cause chaos.
17512 	 */
17513 	ipst = ill->ill_ipst;
17514 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17515 	    lifr->lifr_name, NULL);
17516 	if (phyi != NULL) {
17517 		ill_t *ill_mate = phyi->phyint_illv4;
17518 
17519 		if (ill_mate == NULL)
17520 			ill_mate = phyi->phyint_illv6;
17521 		ASSERT(ill_mate != NULL);
17522 
17523 		if (ill_mate->ill_media->ip_m_mac_type !=
17524 		    ill->ill_media->ip_m_mac_type) {
17525 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17526 			    "use the same ill name on differing media\n"));
17527 			return (EINVAL);
17528 		}
17529 	}
17530 
17531 	/*
17532 	 * We start off as IFF_IPV4 in ipif_allocate and become
17533 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
17534 	 * The only flags that we read from user space are IFF_IPV4,
17535 	 * IFF_IPV6, IFF_XRESOLV and IFF_BROADCAST.
17536 	 *
17537 	 * This ill has not been inserted into the global list.
17538 	 * So we are still single threaded and don't need any lock
17539 	 *
17540 	 * Saniy check the flags.
17541 	 */
17542 
17543 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17544 	    ((lifr->lifr_flags & IFF_IPV6) ||
17545 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17546 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
17547 		    "or IPv6 i.e., no broadcast \n"));
17548 		return (EINVAL);
17549 	}
17550 
17551 	new_flags =
17552 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_XRESOLV|IFF_BROADCAST);
17553 
17554 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
17555 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
17556 		    "IFF_IPV4 or IFF_IPV6\n"));
17557 		return (EINVAL);
17558 	}
17559 	/*
17560 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17561 	 */
17562 	if ((new_flags & IFF_XRESOLV) && !(new_flags & IFF_IPV6) &&
17563 	    !(ipif->ipif_isv6)) {
17564 		ip1dbg(("ip_sioctl_slifname: XRESOLV only allowed on "
17565 		    "IPv6 interface\n"));
17566 		return (EINVAL);
17567 	}
17568 
17569 	/*
17570 	 * We always start off as IPv4, so only need to check for IPv6.
17571 	 */
17572 	if ((new_flags & IFF_IPV6) != 0) {
17573 		ill->ill_flags |= ILLF_IPV6;
17574 		ill->ill_flags &= ~ILLF_IPV4;
17575 	}
17576 
17577 	if ((new_flags & IFF_BROADCAST) != 0)
17578 		ipif->ipif_flags |= IPIF_BROADCAST;
17579 	else
17580 		ipif->ipif_flags &= ~IPIF_BROADCAST;
17581 
17582 	if ((new_flags & IFF_XRESOLV) != 0)
17583 		ill->ill_flags |= ILLF_XRESOLV;
17584 	else
17585 		ill->ill_flags &= ~ILLF_XRESOLV;
17586 
17587 	/* We started off as V4. */
17588 	if (ill->ill_flags & ILLF_IPV6) {
17589 		ill->ill_phyint->phyint_illv6 = ill;
17590 		ill->ill_phyint->phyint_illv4 = NULL;
17591 	}
17592 
17593 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17594 }
17595 
17596 /* ARGSUSED */
17597 int
17598 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17599     ip_ioctl_cmd_t *ipip, void *if_req)
17600 {
17601 	/*
17602 	 * ill_phyint_reinit merged the v4 and v6 into a single
17603 	 * ipsq.  We might not have been able to complete the
17604 	 * slifname in ipif_set_values, if we could not become
17605 	 * exclusive.  If so restart it here
17606 	 */
17607 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17608 }
17609 
17610 /*
17611  * Return a pointer to the ipif which matches the index, IP version type and
17612  * zoneid.
17613  */
17614 ipif_t *
17615 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17616     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17617 {
17618 	ill_t	*ill;
17619 	ipif_t	*ipif = NULL;
17620 
17621 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17622 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17623 
17624 	if (err != NULL)
17625 		*err = 0;
17626 
17627 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17628 	if (ill != NULL) {
17629 		mutex_enter(&ill->ill_lock);
17630 		for (ipif = ill->ill_ipif; ipif != NULL;
17631 		    ipif = ipif->ipif_next) {
17632 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17633 			    zoneid == ipif->ipif_zoneid ||
17634 			    ipif->ipif_zoneid == ALL_ZONES)) {
17635 				ipif_refhold_locked(ipif);
17636 				break;
17637 			}
17638 		}
17639 		mutex_exit(&ill->ill_lock);
17640 		ill_refrele(ill);
17641 		if (ipif == NULL && err != NULL)
17642 			*err = ENXIO;
17643 	}
17644 	return (ipif);
17645 }
17646 
17647 /*
17648  * Change an existing physical interface's index. If the new index
17649  * is acceptable we update the index and the phyint_list_avl_by_index tree.
17650  * Finally, we update other systems which may have a dependence on the
17651  * index value.
17652  */
17653 /* ARGSUSED */
17654 int
17655 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17656     ip_ioctl_cmd_t *ipip, void *ifreq)
17657 {
17658 	ill_t		*ill;
17659 	phyint_t	*phyi;
17660 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17661 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17662 	uint_t	old_index, index;
17663 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17664 	avl_index_t	where;
17665 
17666 	if (ipip->ipi_cmd_type == IF_CMD)
17667 		index = ifr->ifr_index;
17668 	else
17669 		index = lifr->lifr_index;
17670 
17671 	/*
17672 	 * Only allow on physical interface. Also, index zero is illegal.
17673 	 */
17674 	ill = ipif->ipif_ill;
17675 	phyi = ill->ill_phyint;
17676 	if (ipif->ipif_id != 0 || index == 0) {
17677 		return (EINVAL);
17678 	}
17679 
17680 	/* If the index is not changing, no work to do */
17681 	if (phyi->phyint_ifindex == index)
17682 		return (0);
17683 
17684 	/*
17685 	 * Use phyint_exists() to determine if the new interface index
17686 	 * is already in use. If the index is unused then we need to
17687 	 * change the phyint's position in the phyint_list_avl_by_index
17688 	 * tree. If we do not do this, subsequent lookups (using the new
17689 	 * index value) will not find the phyint.
17690 	 */
17691 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17692 	if (phyint_exists(index, ipst)) {
17693 		rw_exit(&ipst->ips_ill_g_lock);
17694 		return (EEXIST);
17695 	}
17696 
17697 	/* The new index is unused. Set it in the phyint. */
17698 	old_index = phyi->phyint_ifindex;
17699 	phyi->phyint_ifindex = index;
17700 
17701 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
17702 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17703 	    &index, &where);
17704 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17705 	    phyi, where);
17706 	rw_exit(&ipst->ips_ill_g_lock);
17707 
17708 	/* Update SCTP's ILL list */
17709 	sctp_ill_reindex(ill, old_index);
17710 
17711 	/* Send the routing sockets message */
17712 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17713 	if (ILL_OTHER(ill))
17714 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17715 
17716 	return (0);
17717 }
17718 
17719 /* ARGSUSED */
17720 int
17721 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17722     ip_ioctl_cmd_t *ipip, void *ifreq)
17723 {
17724 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17725 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17726 
17727 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17728 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17729 	/* Get the interface index */
17730 	if (ipip->ipi_cmd_type == IF_CMD) {
17731 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17732 	} else {
17733 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17734 	}
17735 	return (0);
17736 }
17737 
17738 /* ARGSUSED */
17739 int
17740 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17741     ip_ioctl_cmd_t *ipip, void *ifreq)
17742 {
17743 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17744 
17745 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17746 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17747 	/* Get the interface zone */
17748 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17749 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17750 	return (0);
17751 }
17752 
17753 /*
17754  * Set the zoneid of an interface.
17755  */
17756 /* ARGSUSED */
17757 int
17758 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17759     ip_ioctl_cmd_t *ipip, void *ifreq)
17760 {
17761 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17762 	int err = 0;
17763 	boolean_t need_up = B_FALSE;
17764 	zone_t *zptr;
17765 	zone_status_t status;
17766 	zoneid_t zoneid;
17767 
17768 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17769 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17770 		if (!is_system_labeled())
17771 			return (ENOTSUP);
17772 		zoneid = GLOBAL_ZONEID;
17773 	}
17774 
17775 	/* cannot assign instance zero to a non-global zone */
17776 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17777 		return (ENOTSUP);
17778 
17779 	/*
17780 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17781 	 * the event of a race with the zone shutdown processing, since IP
17782 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17783 	 * interface will be cleaned up even if the zone is shut down
17784 	 * immediately after the status check. If the interface can't be brought
17785 	 * down right away, and the zone is shut down before the restart
17786 	 * function is called, we resolve the possible races by rechecking the
17787 	 * zone status in the restart function.
17788 	 */
17789 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17790 		return (EINVAL);
17791 	status = zone_status_get(zptr);
17792 	zone_rele(zptr);
17793 
17794 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17795 		return (EINVAL);
17796 
17797 	if (ipif->ipif_flags & IPIF_UP) {
17798 		/*
17799 		 * If the interface is already marked up,
17800 		 * we call ipif_down which will take care
17801 		 * of ditching any IREs that have been set
17802 		 * up based on the old interface address.
17803 		 */
17804 		err = ipif_logical_down(ipif, q, mp);
17805 		if (err == EINPROGRESS)
17806 			return (err);
17807 		ipif_down_tail(ipif);
17808 		need_up = B_TRUE;
17809 	}
17810 
17811 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17812 	return (err);
17813 }
17814 
17815 static int
17816 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17817     queue_t *q, mblk_t *mp, boolean_t need_up)
17818 {
17819 	int	err = 0;
17820 	ip_stack_t	*ipst;
17821 
17822 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17823 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17824 
17825 	if (CONN_Q(q))
17826 		ipst = CONNQ_TO_IPST(q);
17827 	else
17828 		ipst = ILLQ_TO_IPST(q);
17829 
17830 	/*
17831 	 * For exclusive stacks we don't allow a different zoneid than
17832 	 * global.
17833 	 */
17834 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17835 	    zoneid != GLOBAL_ZONEID)
17836 		return (EINVAL);
17837 
17838 	/* Set the new zone id. */
17839 	ipif->ipif_zoneid = zoneid;
17840 
17841 	/* Update sctp list */
17842 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
17843 
17844 	if (need_up) {
17845 		/*
17846 		 * Now bring the interface back up.  If this
17847 		 * is the only IPIF for the ILL, ipif_up
17848 		 * will have to re-bind to the device, so
17849 		 * we may get back EINPROGRESS, in which
17850 		 * case, this IOCTL will get completed in
17851 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
17852 		 */
17853 		err = ipif_up(ipif, q, mp);
17854 	}
17855 	return (err);
17856 }
17857 
17858 /* ARGSUSED */
17859 int
17860 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17861     ip_ioctl_cmd_t *ipip, void *if_req)
17862 {
17863 	struct lifreq *lifr = (struct lifreq *)if_req;
17864 	zoneid_t zoneid;
17865 	zone_t *zptr;
17866 	zone_status_t status;
17867 
17868 	ASSERT(ipif->ipif_id != 0);
17869 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17870 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
17871 		zoneid = GLOBAL_ZONEID;
17872 
17873 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
17874 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17875 
17876 	/*
17877 	 * We recheck the zone status to resolve the following race condition:
17878 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
17879 	 * 2) hme0:1 is up and can't be brought down right away;
17880 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
17881 	 * 3) zone "myzone" is halted; the zone status switches to
17882 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
17883 	 * the interfaces to remove - hme0:1 is not returned because it's not
17884 	 * yet in "myzone", so it won't be removed;
17885 	 * 4) the restart function for SIOCSLIFZONE is called; without the
17886 	 * status check here, we would have hme0:1 in "myzone" after it's been
17887 	 * destroyed.
17888 	 * Note that if the status check fails, we need to bring the interface
17889 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
17890 	 * ipif_up_done[_v6]().
17891 	 */
17892 	status = ZONE_IS_UNINITIALIZED;
17893 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
17894 		status = zone_status_get(zptr);
17895 		zone_rele(zptr);
17896 	}
17897 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
17898 		if (ipif->ipif_isv6) {
17899 			(void) ipif_up_done_v6(ipif);
17900 		} else {
17901 			(void) ipif_up_done(ipif);
17902 		}
17903 		return (EINVAL);
17904 	}
17905 
17906 	ipif_down_tail(ipif);
17907 
17908 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
17909 	    B_TRUE));
17910 }
17911 
17912 /*
17913  * Return the number of addresses on `ill' with one or more of the values
17914  * in `set' set and all of the values in `clear' clear.
17915  */
17916 static uint_t
17917 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
17918 {
17919 	ipif_t	*ipif;
17920 	uint_t	cnt = 0;
17921 
17922 	ASSERT(IAM_WRITER_ILL(ill));
17923 
17924 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
17925 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
17926 			cnt++;
17927 
17928 	return (cnt);
17929 }
17930 
17931 /*
17932  * Return the number of migratable addresses on `ill' that are under
17933  * application control.
17934  */
17935 uint_t
17936 ill_appaddr_cnt(const ill_t *ill)
17937 {
17938 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
17939 	    IPIF_NOFAILOVER));
17940 }
17941 
17942 /*
17943  * Return the number of point-to-point addresses on `ill'.
17944  */
17945 uint_t
17946 ill_ptpaddr_cnt(const ill_t *ill)
17947 {
17948 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
17949 }
17950 
17951 /* ARGSUSED */
17952 int
17953 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17954 	ip_ioctl_cmd_t *ipip, void *ifreq)
17955 {
17956 	struct lifreq	*lifr = ifreq;
17957 
17958 	ASSERT(q->q_next == NULL);
17959 	ASSERT(CONN_Q(q));
17960 
17961 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
17962 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17963 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
17964 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
17965 
17966 	return (0);
17967 }
17968 
17969 /* Find the previous ILL in this usesrc group */
17970 static ill_t *
17971 ill_prev_usesrc(ill_t *uill)
17972 {
17973 	ill_t *ill;
17974 
17975 	for (ill = uill->ill_usesrc_grp_next;
17976 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
17977 	    ill = ill->ill_usesrc_grp_next)
17978 		/* do nothing */;
17979 	return (ill);
17980 }
17981 
17982 /*
17983  * Release all members of the usesrc group. This routine is called
17984  * from ill_delete when the interface being unplumbed is the
17985  * group head.
17986  */
17987 static void
17988 ill_disband_usesrc_group(ill_t *uill)
17989 {
17990 	ill_t *next_ill, *tmp_ill;
17991 	ip_stack_t	*ipst = uill->ill_ipst;
17992 
17993 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
17994 	next_ill = uill->ill_usesrc_grp_next;
17995 
17996 	do {
17997 		ASSERT(next_ill != NULL);
17998 		tmp_ill = next_ill->ill_usesrc_grp_next;
17999 		ASSERT(tmp_ill != NULL);
18000 		next_ill->ill_usesrc_grp_next = NULL;
18001 		next_ill->ill_usesrc_ifindex = 0;
18002 		next_ill = tmp_ill;
18003 	} while (next_ill->ill_usesrc_ifindex != 0);
18004 	uill->ill_usesrc_grp_next = NULL;
18005 }
18006 
18007 /*
18008  * Remove the client usesrc ILL from the list and relink to a new list
18009  */
18010 int
18011 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18012 {
18013 	ill_t *ill, *tmp_ill;
18014 	ip_stack_t	*ipst = ucill->ill_ipst;
18015 
18016 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18017 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18018 
18019 	/*
18020 	 * Check if the usesrc client ILL passed in is not already
18021 	 * in use as a usesrc ILL i.e one whose source address is
18022 	 * in use OR a usesrc ILL is not already in use as a usesrc
18023 	 * client ILL
18024 	 */
18025 	if ((ucill->ill_usesrc_ifindex == 0) ||
18026 	    (uill->ill_usesrc_ifindex != 0)) {
18027 		return (-1);
18028 	}
18029 
18030 	ill = ill_prev_usesrc(ucill);
18031 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18032 
18033 	/* Remove from the current list */
18034 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18035 		/* Only two elements in the list */
18036 		ASSERT(ill->ill_usesrc_ifindex == 0);
18037 		ill->ill_usesrc_grp_next = NULL;
18038 	} else {
18039 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18040 	}
18041 
18042 	if (ifindex == 0) {
18043 		ucill->ill_usesrc_ifindex = 0;
18044 		ucill->ill_usesrc_grp_next = NULL;
18045 		return (0);
18046 	}
18047 
18048 	ucill->ill_usesrc_ifindex = ifindex;
18049 	tmp_ill = uill->ill_usesrc_grp_next;
18050 	uill->ill_usesrc_grp_next = ucill;
18051 	ucill->ill_usesrc_grp_next =
18052 	    (tmp_ill != NULL) ? tmp_ill : uill;
18053 	return (0);
18054 }
18055 
18056 /*
18057  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18058  * ip.c for locking details.
18059  */
18060 /* ARGSUSED */
18061 int
18062 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18063     ip_ioctl_cmd_t *ipip, void *ifreq)
18064 {
18065 	struct lifreq *lifr = (struct lifreq *)ifreq;
18066 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18067 	    ill_flag_changed = B_FALSE;
18068 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18069 	int err = 0, ret;
18070 	uint_t ifindex;
18071 	ipsq_t *ipsq = NULL;
18072 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18073 
18074 	ASSERT(IAM_WRITER_IPIF(ipif));
18075 	ASSERT(q->q_next == NULL);
18076 	ASSERT(CONN_Q(q));
18077 
18078 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18079 
18080 	ifindex = lifr->lifr_index;
18081 	if (ifindex == 0) {
18082 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18083 			/* non usesrc group interface, nothing to reset */
18084 			return (0);
18085 		}
18086 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18087 		/* valid reset request */
18088 		reset_flg = B_TRUE;
18089 	}
18090 
18091 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18092 	    ip_process_ioctl, &err, ipst);
18093 	if (usesrc_ill == NULL) {
18094 		return (err);
18095 	}
18096 
18097 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18098 	    NEW_OP, B_TRUE);
18099 	if (ipsq == NULL) {
18100 		err = EINPROGRESS;
18101 		/* Operation enqueued on the ipsq of the usesrc ILL */
18102 		goto done;
18103 	}
18104 
18105 	/* USESRC isn't currently supported with IPMP */
18106 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18107 		err = ENOTSUP;
18108 		goto done;
18109 	}
18110 
18111 	/*
18112 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18113 	 * used by IPMP underlying interfaces, but someone might think it's
18114 	 * more general and try to use it independently with VNI.)
18115 	 */
18116 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18117 		err = ENOTSUP;
18118 		goto done;
18119 	}
18120 
18121 	/*
18122 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18123 	 * already a client then return EINVAL
18124 	 */
18125 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18126 		err = EINVAL;
18127 		goto done;
18128 	}
18129 
18130 	/*
18131 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18132 	 * be then this is a duplicate operation.
18133 	 */
18134 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18135 		err = 0;
18136 		goto done;
18137 	}
18138 
18139 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18140 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18141 	    usesrc_ill->ill_isv6));
18142 
18143 	/*
18144 	 * The next step ensures that no new ires will be created referencing
18145 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18146 	 * we go through an ire walk deleting all ire caches that reference
18147 	 * the client ill. New ires referencing the client ill that are added
18148 	 * to the ire table before the ILL_CHANGING flag is set, will be
18149 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18150 	 * the client ill while the ILL_CHANGING flag is set will be failed
18151 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18152 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18153 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18154 	 * belong to the same usesrc group.
18155 	 */
18156 	mutex_enter(&usesrc_cli_ill->ill_lock);
18157 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18158 	mutex_exit(&usesrc_cli_ill->ill_lock);
18159 	ill_flag_changed = B_TRUE;
18160 
18161 	if (ipif->ipif_isv6)
18162 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18163 		    ALL_ZONES, ipst);
18164 	else
18165 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18166 		    ALL_ZONES, ipst);
18167 
18168 	/*
18169 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18170 	 * and the ill_usesrc_ifindex fields
18171 	 */
18172 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18173 
18174 	if (reset_flg) {
18175 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18176 		if (ret != 0) {
18177 			err = EINVAL;
18178 		}
18179 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18180 		goto done;
18181 	}
18182 
18183 	/*
18184 	 * Four possibilities to consider:
18185 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18186 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18187 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18188 	 * 4. Both are part of their respective usesrc groups
18189 	 */
18190 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18191 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18192 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18193 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18194 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18195 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18196 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18197 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18198 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18199 		/* Insert at head of list */
18200 		usesrc_cli_ill->ill_usesrc_grp_next =
18201 		    usesrc_ill->ill_usesrc_grp_next;
18202 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18203 	} else {
18204 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18205 		    ifindex);
18206 		if (ret != 0)
18207 			err = EINVAL;
18208 	}
18209 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18210 
18211 done:
18212 	if (ill_flag_changed) {
18213 		mutex_enter(&usesrc_cli_ill->ill_lock);
18214 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18215 		mutex_exit(&usesrc_cli_ill->ill_lock);
18216 	}
18217 	if (ipsq != NULL)
18218 		ipsq_exit(ipsq);
18219 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18220 	ill_refrele(usesrc_ill);
18221 	return (err);
18222 }
18223 
18224 /*
18225  * comparison function used by avl.
18226  */
18227 static int
18228 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18229 {
18230 
18231 	uint_t index;
18232 
18233 	ASSERT(phyip != NULL && index_ptr != NULL);
18234 
18235 	index = *((uint_t *)index_ptr);
18236 	/*
18237 	 * let the phyint with the lowest index be on top.
18238 	 */
18239 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18240 		return (1);
18241 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18242 		return (-1);
18243 	return (0);
18244 }
18245 
18246 /*
18247  * comparison function used by avl.
18248  */
18249 static int
18250 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18251 {
18252 	ill_t *ill;
18253 	int res = 0;
18254 
18255 	ASSERT(phyip != NULL && name_ptr != NULL);
18256 
18257 	if (((phyint_t *)phyip)->phyint_illv4)
18258 		ill = ((phyint_t *)phyip)->phyint_illv4;
18259 	else
18260 		ill = ((phyint_t *)phyip)->phyint_illv6;
18261 	ASSERT(ill != NULL);
18262 
18263 	res = strcmp(ill->ill_name, (char *)name_ptr);
18264 	if (res > 0)
18265 		return (1);
18266 	else if (res < 0)
18267 		return (-1);
18268 	return (0);
18269 }
18270 
18271 /*
18272  * This function is called on the unplumb path via ill_glist_delete() when
18273  * there are no ills left on the phyint and thus the phyint can be freed.
18274  */
18275 static void
18276 phyint_free(phyint_t *phyi)
18277 {
18278 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18279 
18280 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18281 
18282 	/*
18283 	 * If this phyint was an IPMP meta-interface, blow away the group.
18284 	 * This is safe to do because all of the illgrps have already been
18285 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18286 	 * If we're cleaning up as a result of failed initialization,
18287 	 * phyint_grp may be NULL.
18288 	 */
18289 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18290 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18291 		ipmp_grp_destroy(phyi->phyint_grp);
18292 		phyi->phyint_grp = NULL;
18293 		rw_exit(&ipst->ips_ipmp_lock);
18294 	}
18295 
18296 	/*
18297 	 * If this interface was under IPMP, take it out of the group.
18298 	 */
18299 	if (phyi->phyint_grp != NULL)
18300 		ipmp_phyint_leave_grp(phyi);
18301 
18302 	/*
18303 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18304 	 * will be freed in ipsq_exit().
18305 	 */
18306 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18307 	phyi->phyint_name[0] = '\0';
18308 
18309 	mi_free(phyi);
18310 }
18311 
18312 /*
18313  * Attach the ill to the phyint structure which can be shared by both
18314  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18315  * function is called from ipif_set_values and ill_lookup_on_name (for
18316  * loopback) where we know the name of the ill. We lookup the ill and if
18317  * there is one present already with the name use that phyint. Otherwise
18318  * reuse the one allocated by ill_init.
18319  */
18320 static void
18321 ill_phyint_reinit(ill_t *ill)
18322 {
18323 	boolean_t isv6 = ill->ill_isv6;
18324 	phyint_t *phyi_old;
18325 	phyint_t *phyi;
18326 	avl_index_t where = 0;
18327 	ill_t	*ill_other = NULL;
18328 	ip_stack_t	*ipst = ill->ill_ipst;
18329 
18330 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18331 
18332 	phyi_old = ill->ill_phyint;
18333 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18334 	    phyi_old->phyint_illv6 == NULL));
18335 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18336 	    phyi_old->phyint_illv4 == NULL));
18337 	ASSERT(phyi_old->phyint_ifindex == 0);
18338 
18339 	/*
18340 	 * Now that our ill has a name, set it in the phyint.
18341 	 */
18342 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18343 
18344 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18345 	    ill->ill_name, &where);
18346 
18347 	/*
18348 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18349 	 *    the global list of ills. So no other thread could have located
18350 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18351 	 * 2. Now locate the other protocol instance of this ill.
18352 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18353 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18354 	 *    of neither ill can change.
18355 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18356 	 *    other ill.
18357 	 * 5. Release all locks.
18358 	 */
18359 
18360 	/*
18361 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18362 	 * we are initializing IPv4.
18363 	 */
18364 	if (phyi != NULL) {
18365 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18366 		ASSERT(ill_other->ill_phyint != NULL);
18367 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18368 		    (!isv6 && ill_other->ill_isv6));
18369 		GRAB_ILL_LOCKS(ill, ill_other);
18370 		/*
18371 		 * We are potentially throwing away phyint_flags which
18372 		 * could be different from the one that we obtain from
18373 		 * ill_other->ill_phyint. But it is okay as we are assuming
18374 		 * that the state maintained within IP is correct.
18375 		 */
18376 		mutex_enter(&phyi->phyint_lock);
18377 		if (isv6) {
18378 			ASSERT(phyi->phyint_illv6 == NULL);
18379 			phyi->phyint_illv6 = ill;
18380 		} else {
18381 			ASSERT(phyi->phyint_illv4 == NULL);
18382 			phyi->phyint_illv4 = ill;
18383 		}
18384 
18385 		/*
18386 		 * Delete the old phyint and make its ipsq eligible
18387 		 * to be freed in ipsq_exit().
18388 		 */
18389 		phyi_old->phyint_illv4 = NULL;
18390 		phyi_old->phyint_illv6 = NULL;
18391 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18392 		phyi_old->phyint_name[0] = '\0';
18393 		mi_free(phyi_old);
18394 	} else {
18395 		mutex_enter(&ill->ill_lock);
18396 		/*
18397 		 * We don't need to acquire any lock, since
18398 		 * the ill is not yet visible globally  and we
18399 		 * have not yet released the ill_g_lock.
18400 		 */
18401 		phyi = phyi_old;
18402 		mutex_enter(&phyi->phyint_lock);
18403 		/* XXX We need a recovery strategy here. */
18404 		if (!phyint_assign_ifindex(phyi, ipst))
18405 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18406 
18407 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18408 		    (void *)phyi, where);
18409 
18410 		(void) avl_find(&ipst->ips_phyint_g_list->
18411 		    phyint_list_avl_by_index,
18412 		    &phyi->phyint_ifindex, &where);
18413 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18414 		    (void *)phyi, where);
18415 	}
18416 
18417 	/*
18418 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18419 	 * pending mp is not affected because that is per ill basis.
18420 	 */
18421 	ill->ill_phyint = phyi;
18422 
18423 	/*
18424 	 * Now that the phyint's ifindex has been assigned, complete the
18425 	 * remaining
18426 	 */
18427 
18428 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18429 	if (ill->ill_isv6) {
18430 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18431 		    ill->ill_phyint->phyint_ifindex;
18432 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18433 	} else {
18434 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18435 	}
18436 
18437 	/*
18438 	 * Generate an event within the hooks framework to indicate that
18439 	 * a new interface has just been added to IP.  For this event to
18440 	 * be generated, the network interface must, at least, have an
18441 	 * ifindex assigned to it.  (We don't generate the event for
18442 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
18443 	 *
18444 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18445 	 * that the ordering of delivered events to listeners matches the
18446 	 * order of them in the kernel.
18447 	 */
18448 	if (!IS_LOOPBACK(ill)) {
18449 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18450 		    ill->ill_name_length);
18451 	}
18452 	RELEASE_ILL_LOCKS(ill, ill_other);
18453 	mutex_exit(&phyi->phyint_lock);
18454 }
18455 
18456 /*
18457  * Notify any downstream modules of the name of this interface.
18458  * An M_IOCTL is used even though we don't expect a successful reply.
18459  * Any reply message from the driver (presumably an M_IOCNAK) will
18460  * eventually get discarded somewhere upstream.  The message format is
18461  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18462  * to IP.
18463  */
18464 static void
18465 ip_ifname_notify(ill_t *ill, queue_t *q)
18466 {
18467 	mblk_t *mp1, *mp2;
18468 	struct iocblk *iocp;
18469 	struct lifreq *lifr;
18470 
18471 	mp1 = mkiocb(SIOCSLIFNAME);
18472 	if (mp1 == NULL)
18473 		return;
18474 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18475 	if (mp2 == NULL) {
18476 		freeb(mp1);
18477 		return;
18478 	}
18479 
18480 	mp1->b_cont = mp2;
18481 	iocp = (struct iocblk *)mp1->b_rptr;
18482 	iocp->ioc_count = sizeof (struct lifreq);
18483 
18484 	lifr = (struct lifreq *)mp2->b_rptr;
18485 	mp2->b_wptr += sizeof (struct lifreq);
18486 	bzero(lifr, sizeof (struct lifreq));
18487 
18488 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18489 	lifr->lifr_ppa = ill->ill_ppa;
18490 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18491 
18492 	putnext(q, mp1);
18493 }
18494 
18495 static int
18496 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18497 {
18498 	int		err;
18499 	ip_stack_t	*ipst = ill->ill_ipst;
18500 	phyint_t	*phyi = ill->ill_phyint;
18501 
18502 	/* Set the obsolete NDD per-interface forwarding name. */
18503 	err = ill_set_ndd_name(ill);
18504 	if (err != 0) {
18505 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18506 		    err);
18507 	}
18508 
18509 	/*
18510 	 * Now that ill_name is set, the configuration for the IPMP
18511 	 * meta-interface can be performed.
18512 	 */
18513 	if (IS_IPMP(ill)) {
18514 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18515 		/*
18516 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18517 		 * meta-interface and we need to create the IPMP group.
18518 		 */
18519 		if (phyi->phyint_grp == NULL) {
18520 			/*
18521 			 * If someone has renamed another IPMP group to have
18522 			 * the same name as our interface, bail.
18523 			 */
18524 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18525 				rw_exit(&ipst->ips_ipmp_lock);
18526 				return (EEXIST);
18527 			}
18528 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18529 			if (phyi->phyint_grp == NULL) {
18530 				rw_exit(&ipst->ips_ipmp_lock);
18531 				return (ENOMEM);
18532 			}
18533 		}
18534 		rw_exit(&ipst->ips_ipmp_lock);
18535 	}
18536 
18537 	/* Tell downstream modules where they are. */
18538 	ip_ifname_notify(ill, q);
18539 
18540 	/*
18541 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18542 	 * Error cases are ENOMEM ...
18543 	 */
18544 	err = ill_dl_phys(ill, ipif, mp, q);
18545 
18546 	/*
18547 	 * If there is no IRE expiration timer running, get one started.
18548 	 * igmp and mld timers will be triggered by the first multicast
18549 	 */
18550 	if (ipst->ips_ip_ire_expire_id == 0) {
18551 		/*
18552 		 * acquire the lock and check again.
18553 		 */
18554 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18555 		if (ipst->ips_ip_ire_expire_id == 0) {
18556 			ipst->ips_ip_ire_expire_id = timeout(
18557 			    ip_trash_timer_expire, ipst,
18558 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18559 		}
18560 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18561 	}
18562 
18563 	if (ill->ill_isv6) {
18564 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18565 		if (ipst->ips_mld_slowtimeout_id == 0) {
18566 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18567 			    (void *)ipst,
18568 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18569 		}
18570 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18571 	} else {
18572 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18573 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18574 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18575 			    (void *)ipst,
18576 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18577 		}
18578 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18579 	}
18580 
18581 	return (err);
18582 }
18583 
18584 /*
18585  * Common routine for ppa and ifname setting. Should be called exclusive.
18586  *
18587  * Returns EINPROGRESS when mp has been consumed by queueing it on
18588  * ill_pending_mp and the ioctl will complete in ip_rput.
18589  *
18590  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18591  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18592  * For SLIFNAME, we pass these values back to the userland.
18593  */
18594 static int
18595 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18596 {
18597 	ill_t	*ill;
18598 	ipif_t	*ipif;
18599 	ipsq_t	*ipsq;
18600 	char	*ppa_ptr;
18601 	char	*old_ptr;
18602 	char	old_char;
18603 	int	error;
18604 	ip_stack_t	*ipst;
18605 
18606 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18607 	ASSERT(q->q_next != NULL);
18608 	ASSERT(interf_name != NULL);
18609 
18610 	ill = (ill_t *)q->q_ptr;
18611 	ipst = ill->ill_ipst;
18612 
18613 	ASSERT(ill->ill_ipst != NULL);
18614 	ASSERT(ill->ill_name[0] == '\0');
18615 	ASSERT(IAM_WRITER_ILL(ill));
18616 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18617 	ASSERT(ill->ill_ppa == UINT_MAX);
18618 
18619 	/* The ppa is sent down by ifconfig or is chosen */
18620 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18621 		return (EINVAL);
18622 	}
18623 
18624 	/*
18625 	 * make sure ppa passed in is same as ppa in the name.
18626 	 * This check is not made when ppa == UINT_MAX in that case ppa
18627 	 * in the name could be anything. System will choose a ppa and
18628 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18629 	 */
18630 	if (*new_ppa_ptr != UINT_MAX) {
18631 		/* stoi changes the pointer */
18632 		old_ptr = ppa_ptr;
18633 		/*
18634 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18635 		 * (they don't have an externally visible ppa).  We assign one
18636 		 * here so that we can manage the interface.  Note that in
18637 		 * the past this value was always 0 for DLPI 1 drivers.
18638 		 */
18639 		if (*new_ppa_ptr == 0)
18640 			*new_ppa_ptr = stoi(&old_ptr);
18641 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18642 			return (EINVAL);
18643 	}
18644 	/*
18645 	 * terminate string before ppa
18646 	 * save char at that location.
18647 	 */
18648 	old_char = ppa_ptr[0];
18649 	ppa_ptr[0] = '\0';
18650 
18651 	ill->ill_ppa = *new_ppa_ptr;
18652 	/*
18653 	 * Finish as much work now as possible before calling ill_glist_insert
18654 	 * which makes the ill globally visible and also merges it with the
18655 	 * other protocol instance of this phyint. The remaining work is
18656 	 * done after entering the ipsq which may happen sometime later.
18657 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18658 	 */
18659 	ipif = ill->ill_ipif;
18660 
18661 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18662 	ipif_assign_seqid(ipif);
18663 
18664 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18665 		ill->ill_flags |= ILLF_IPV4;
18666 
18667 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18668 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18669 
18670 	if (ill->ill_flags & ILLF_IPV6) {
18671 
18672 		ill->ill_isv6 = B_TRUE;
18673 		if (ill->ill_rq != NULL) {
18674 			ill->ill_rq->q_qinfo = &iprinitv6;
18675 			ill->ill_wq->q_qinfo = &ipwinitv6;
18676 		}
18677 
18678 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18679 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18680 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18681 		ipif->ipif_v6subnet = ipv6_all_zeros;
18682 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18683 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18684 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18685 		/*
18686 		 * point-to-point or Non-mulicast capable
18687 		 * interfaces won't do NUD unless explicitly
18688 		 * configured to do so.
18689 		 */
18690 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18691 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18692 			ill->ill_flags |= ILLF_NONUD;
18693 		}
18694 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18695 		if (ill->ill_flags & ILLF_NOARP) {
18696 			/*
18697 			 * Note: xresolv interfaces will eventually need
18698 			 * NOARP set here as well, but that will require
18699 			 * those external resolvers to have some
18700 			 * knowledge of that flag and act appropriately.
18701 			 * Not to be changed at present.
18702 			 */
18703 			ill->ill_flags &= ~ILLF_NOARP;
18704 		}
18705 		/*
18706 		 * Set the ILLF_ROUTER flag according to the global
18707 		 * IPv6 forwarding policy.
18708 		 */
18709 		if (ipst->ips_ipv6_forward != 0)
18710 			ill->ill_flags |= ILLF_ROUTER;
18711 	} else if (ill->ill_flags & ILLF_IPV4) {
18712 		ill->ill_isv6 = B_FALSE;
18713 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18714 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18715 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18716 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18717 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18718 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18719 		/*
18720 		 * Set the ILLF_ROUTER flag according to the global
18721 		 * IPv4 forwarding policy.
18722 		 */
18723 		if (ipst->ips_ip_g_forward != 0)
18724 			ill->ill_flags |= ILLF_ROUTER;
18725 	}
18726 
18727 	ASSERT(ill->ill_phyint != NULL);
18728 
18729 	/*
18730 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18731 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18732 	 */
18733 	if (!ill_allocate_mibs(ill))
18734 		return (ENOMEM);
18735 
18736 	/*
18737 	 * Pick a default sap until we get the DL_INFO_ACK back from
18738 	 * the driver.
18739 	 */
18740 	if (ill->ill_sap == 0) {
18741 		if (ill->ill_isv6)
18742 			ill->ill_sap = IP6_DL_SAP;
18743 		else
18744 			ill->ill_sap = IP_DL_SAP;
18745 	}
18746 
18747 	ill->ill_ifname_pending = 1;
18748 	ill->ill_ifname_pending_err = 0;
18749 
18750 	/*
18751 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18752 	 * that were joined while this ill was not bound to the DLPI link need
18753 	 * to be recovered by ill_recover_multicast().
18754 	 */
18755 	ill->ill_need_recover_multicast = 1;
18756 
18757 	ill_refhold(ill);
18758 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18759 	if ((error = ill_glist_insert(ill, interf_name,
18760 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18761 		ill->ill_ppa = UINT_MAX;
18762 		ill->ill_name[0] = '\0';
18763 		/*
18764 		 * undo null termination done above.
18765 		 */
18766 		ppa_ptr[0] = old_char;
18767 		rw_exit(&ipst->ips_ill_g_lock);
18768 		ill_refrele(ill);
18769 		return (error);
18770 	}
18771 
18772 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18773 
18774 	/*
18775 	 * When we return the buffer pointed to by interf_name should contain
18776 	 * the same name as in ill_name.
18777 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18778 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18779 	 * so copy full name and update the ppa ptr.
18780 	 * When ppa passed in != UINT_MAX all values are correct just undo
18781 	 * null termination, this saves a bcopy.
18782 	 */
18783 	if (*new_ppa_ptr == UINT_MAX) {
18784 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18785 		*new_ppa_ptr = ill->ill_ppa;
18786 	} else {
18787 		/*
18788 		 * undo null termination done above.
18789 		 */
18790 		ppa_ptr[0] = old_char;
18791 	}
18792 
18793 	/* Let SCTP know about this ILL */
18794 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18795 
18796 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18797 	    B_TRUE);
18798 
18799 	rw_exit(&ipst->ips_ill_g_lock);
18800 	ill_refrele(ill);
18801 	if (ipsq == NULL)
18802 		return (EINPROGRESS);
18803 
18804 	/*
18805 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18806 	 */
18807 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18808 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18809 	else
18810 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18811 
18812 	error = ipif_set_values_tail(ill, ipif, mp, q);
18813 	ipsq_exit(ipsq);
18814 	if (error != 0 && error != EINPROGRESS) {
18815 		/*
18816 		 * restore previous values
18817 		 */
18818 		ill->ill_isv6 = B_FALSE;
18819 	}
18820 	return (error);
18821 }
18822 
18823 void
18824 ipif_init(ip_stack_t *ipst)
18825 {
18826 	int i;
18827 
18828 	for (i = 0; i < MAX_G_HEADS; i++) {
18829 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18830 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18831 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
18832 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18833 	}
18834 
18835 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18836 	    ill_phyint_compare_index,
18837 	    sizeof (phyint_t),
18838 	    offsetof(struct phyint, phyint_avl_by_index));
18839 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18840 	    ill_phyint_compare_name,
18841 	    sizeof (phyint_t),
18842 	    offsetof(struct phyint, phyint_avl_by_name));
18843 }
18844 
18845 /*
18846  * Lookup the ipif corresponding to the onlink destination address. For
18847  * point-to-point interfaces, it matches with remote endpoint destination
18848  * address. For point-to-multipoint interfaces it only tries to match the
18849  * destination with the interface's subnet address. The longest, most specific
18850  * match is found to take care of such rare network configurations like -
18851  * le0: 129.146.1.1/16
18852  * le1: 129.146.2.2/24
18853  *
18854  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
18855  * supported on underlying interfaces in an IPMP group, underlying interfaces
18856  * are ignored when looking up a match.  (If we didn't ignore them, we'd
18857  * risk using a test address as a source for outgoing traffic.)
18858  */
18859 ipif_t *
18860 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
18861 {
18862 	ipif_t	*ipif, *best_ipif;
18863 	ill_t	*ill;
18864 	ill_walk_context_t ctx;
18865 
18866 	ASSERT(zoneid != ALL_ZONES);
18867 	best_ipif = NULL;
18868 
18869 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18870 	ill = ILL_START_WALK_V4(&ctx, ipst);
18871 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18872 		if (IS_UNDER_IPMP(ill))
18873 			continue;
18874 		mutex_enter(&ill->ill_lock);
18875 		for (ipif = ill->ill_ipif; ipif != NULL;
18876 		    ipif = ipif->ipif_next) {
18877 			if (!IPIF_CAN_LOOKUP(ipif))
18878 				continue;
18879 			if (ipif->ipif_zoneid != zoneid &&
18880 			    ipif->ipif_zoneid != ALL_ZONES)
18881 				continue;
18882 			/*
18883 			 * Point-to-point case. Look for exact match with
18884 			 * destination address.
18885 			 */
18886 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18887 				if (ipif->ipif_pp_dst_addr == addr) {
18888 					ipif_refhold_locked(ipif);
18889 					mutex_exit(&ill->ill_lock);
18890 					rw_exit(&ipst->ips_ill_g_lock);
18891 					if (best_ipif != NULL)
18892 						ipif_refrele(best_ipif);
18893 					return (ipif);
18894 				}
18895 			} else if (ipif->ipif_subnet == (addr &
18896 			    ipif->ipif_net_mask)) {
18897 				/*
18898 				 * Point-to-multipoint case. Looping through to
18899 				 * find the most specific match. If there are
18900 				 * multiple best match ipif's then prefer ipif's
18901 				 * that are UP. If there is only one best match
18902 				 * ipif and it is DOWN we must still return it.
18903 				 */
18904 				if ((best_ipif == NULL) ||
18905 				    (ipif->ipif_net_mask >
18906 				    best_ipif->ipif_net_mask) ||
18907 				    ((ipif->ipif_net_mask ==
18908 				    best_ipif->ipif_net_mask) &&
18909 				    ((ipif->ipif_flags & IPIF_UP) &&
18910 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
18911 					ipif_refhold_locked(ipif);
18912 					mutex_exit(&ill->ill_lock);
18913 					rw_exit(&ipst->ips_ill_g_lock);
18914 					if (best_ipif != NULL)
18915 						ipif_refrele(best_ipif);
18916 					best_ipif = ipif;
18917 					rw_enter(&ipst->ips_ill_g_lock,
18918 					    RW_READER);
18919 					mutex_enter(&ill->ill_lock);
18920 				}
18921 			}
18922 		}
18923 		mutex_exit(&ill->ill_lock);
18924 	}
18925 	rw_exit(&ipst->ips_ill_g_lock);
18926 	return (best_ipif);
18927 }
18928 
18929 /*
18930  * Save enough information so that we can recreate the IRE if
18931  * the interface goes down and then up.
18932  */
18933 static void
18934 ipif_save_ire(ipif_t *ipif, ire_t *ire)
18935 {
18936 	mblk_t	*save_mp;
18937 
18938 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
18939 	if (save_mp != NULL) {
18940 		ifrt_t	*ifrt;
18941 
18942 		save_mp->b_wptr += sizeof (ifrt_t);
18943 		ifrt = (ifrt_t *)save_mp->b_rptr;
18944 		bzero(ifrt, sizeof (ifrt_t));
18945 		ifrt->ifrt_type = ire->ire_type;
18946 		ifrt->ifrt_addr = ire->ire_addr;
18947 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
18948 		ifrt->ifrt_src_addr = ire->ire_src_addr;
18949 		ifrt->ifrt_mask = ire->ire_mask;
18950 		ifrt->ifrt_flags = ire->ire_flags;
18951 		ifrt->ifrt_max_frag = ire->ire_max_frag;
18952 		mutex_enter(&ipif->ipif_saved_ire_lock);
18953 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
18954 		ipif->ipif_saved_ire_mp = save_mp;
18955 		ipif->ipif_saved_ire_cnt++;
18956 		mutex_exit(&ipif->ipif_saved_ire_lock);
18957 	}
18958 }
18959 
18960 static void
18961 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
18962 {
18963 	mblk_t	**mpp;
18964 	mblk_t	*mp;
18965 	ifrt_t	*ifrt;
18966 
18967 	/* Remove from ipif_saved_ire_mp list if it is there */
18968 	mutex_enter(&ipif->ipif_saved_ire_lock);
18969 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
18970 	    mpp = &(*mpp)->b_cont) {
18971 		/*
18972 		 * On a given ipif, the triple of address, gateway and
18973 		 * mask is unique for each saved IRE (in the case of
18974 		 * ordinary interface routes, the gateway address is
18975 		 * all-zeroes).
18976 		 */
18977 		mp = *mpp;
18978 		ifrt = (ifrt_t *)mp->b_rptr;
18979 		if (ifrt->ifrt_addr == ire->ire_addr &&
18980 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
18981 		    ifrt->ifrt_mask == ire->ire_mask) {
18982 			*mpp = mp->b_cont;
18983 			ipif->ipif_saved_ire_cnt--;
18984 			freeb(mp);
18985 			break;
18986 		}
18987 	}
18988 	mutex_exit(&ipif->ipif_saved_ire_lock);
18989 }
18990 
18991 /*
18992  * IP multirouting broadcast routes handling
18993  * Append CGTP broadcast IREs to regular ones created
18994  * at ifconfig time.
18995  */
18996 static void
18997 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
18998 {
18999 	ire_t *ire_prim;
19000 
19001 	ASSERT(ire != NULL);
19002 	ASSERT(ire_dst != NULL);
19003 
19004 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19005 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19006 	if (ire_prim != NULL) {
19007 		/*
19008 		 * We are in the special case of broadcasts for
19009 		 * CGTP. We add an IRE_BROADCAST that holds
19010 		 * the RTF_MULTIRT flag, the destination
19011 		 * address of ire_dst and the low level
19012 		 * info of ire_prim. In other words, CGTP
19013 		 * broadcast is added to the redundant ipif.
19014 		 */
19015 		ipif_t *ipif_prim;
19016 		ire_t  *bcast_ire;
19017 
19018 		ipif_prim = ire_prim->ire_ipif;
19019 
19020 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19021 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19022 		    (void *)ire_dst, (void *)ire_prim,
19023 		    (void *)ipif_prim));
19024 
19025 		bcast_ire = ire_create(
19026 		    (uchar_t *)&ire->ire_addr,
19027 		    (uchar_t *)&ip_g_all_ones,
19028 		    (uchar_t *)&ire_dst->ire_src_addr,
19029 		    (uchar_t *)&ire->ire_gateway_addr,
19030 		    &ipif_prim->ipif_mtu,
19031 		    NULL,
19032 		    ipif_prim->ipif_rq,
19033 		    ipif_prim->ipif_wq,
19034 		    IRE_BROADCAST,
19035 		    ipif_prim,
19036 		    0,
19037 		    0,
19038 		    0,
19039 		    ire->ire_flags,
19040 		    &ire_uinfo_null,
19041 		    NULL,
19042 		    NULL,
19043 		    ipst);
19044 
19045 		if (bcast_ire != NULL) {
19046 
19047 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19048 			    B_FALSE) == 0) {
19049 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19050 				    "added bcast_ire %p\n",
19051 				    (void *)bcast_ire));
19052 
19053 				ipif_save_ire(bcast_ire->ire_ipif,
19054 				    bcast_ire);
19055 				ire_refrele(bcast_ire);
19056 			}
19057 		}
19058 		ire_refrele(ire_prim);
19059 	}
19060 }
19061 
19062 /*
19063  * IP multirouting broadcast routes handling
19064  * Remove the broadcast ire
19065  */
19066 static void
19067 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19068 {
19069 	ire_t *ire_dst;
19070 
19071 	ASSERT(ire != NULL);
19072 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19073 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19074 	if (ire_dst != NULL) {
19075 		ire_t *ire_prim;
19076 
19077 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19078 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19079 		if (ire_prim != NULL) {
19080 			ipif_t *ipif_prim;
19081 			ire_t  *bcast_ire;
19082 
19083 			ipif_prim = ire_prim->ire_ipif;
19084 
19085 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19086 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19087 			    (void *)ire_dst, (void *)ire_prim,
19088 			    (void *)ipif_prim));
19089 
19090 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19091 			    ire->ire_gateway_addr,
19092 			    IRE_BROADCAST,
19093 			    ipif_prim, ALL_ZONES,
19094 			    NULL,
19095 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19096 			    MATCH_IRE_MASK, ipst);
19097 
19098 			if (bcast_ire != NULL) {
19099 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19100 				    "looked up bcast_ire %p\n",
19101 				    (void *)bcast_ire));
19102 				ipif_remove_ire(bcast_ire->ire_ipif,
19103 				    bcast_ire);
19104 				ire_delete(bcast_ire);
19105 				ire_refrele(bcast_ire);
19106 			}
19107 			ire_refrele(ire_prim);
19108 		}
19109 		ire_refrele(ire_dst);
19110 	}
19111 }
19112 
19113 /*
19114  * IPsec hardware acceleration capabilities related functions.
19115  */
19116 
19117 /*
19118  * Free a per-ill IPsec capabilities structure.
19119  */
19120 static void
19121 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19122 {
19123 	if (capab->auth_hw_algs != NULL)
19124 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19125 	if (capab->encr_hw_algs != NULL)
19126 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19127 	if (capab->encr_algparm != NULL)
19128 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19129 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19130 }
19131 
19132 /*
19133  * Allocate a new per-ill IPsec capabilities structure. This structure
19134  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19135  * an array which specifies, for each algorithm, whether this algorithm
19136  * is supported by the ill or not.
19137  */
19138 static ill_ipsec_capab_t *
19139 ill_ipsec_capab_alloc(void)
19140 {
19141 	ill_ipsec_capab_t *capab;
19142 	uint_t nelems;
19143 
19144 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19145 	if (capab == NULL)
19146 		return (NULL);
19147 
19148 	/* we need one bit per algorithm */
19149 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19150 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19151 
19152 	/* allocate memory to store algorithm flags */
19153 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19154 	if (capab->encr_hw_algs == NULL)
19155 		goto nomem;
19156 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19157 	if (capab->auth_hw_algs == NULL)
19158 		goto nomem;
19159 	/*
19160 	 * Leave encr_algparm NULL for now since we won't need it half
19161 	 * the time
19162 	 */
19163 	return (capab);
19164 
19165 nomem:
19166 	ill_ipsec_capab_free(capab);
19167 	return (NULL);
19168 }
19169 
19170 /*
19171  * Resize capability array.  Since we're exclusive, this is OK.
19172  */
19173 static boolean_t
19174 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19175 {
19176 	ipsec_capab_algparm_t *nalp, *oalp;
19177 	uint32_t olen, nlen;
19178 
19179 	oalp = capab->encr_algparm;
19180 	olen = capab->encr_algparm_size;
19181 
19182 	if (oalp != NULL) {
19183 		if (algid < capab->encr_algparm_end)
19184 			return (B_TRUE);
19185 	}
19186 
19187 	nlen = (algid + 1) * sizeof (*nalp);
19188 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19189 	if (nalp == NULL)
19190 		return (B_FALSE);
19191 
19192 	if (oalp != NULL) {
19193 		bcopy(oalp, nalp, olen);
19194 		kmem_free(oalp, olen);
19195 	}
19196 	capab->encr_algparm = nalp;
19197 	capab->encr_algparm_size = nlen;
19198 	capab->encr_algparm_end = algid + 1;
19199 
19200 	return (B_TRUE);
19201 }
19202 
19203 /*
19204  * Compare the capabilities of the specified ill with the protocol
19205  * and algorithms specified by the SA passed as argument.
19206  * If they match, returns B_TRUE, B_FALSE if they do not match.
19207  *
19208  * The ill can be passed as a pointer to it, or by specifying its index
19209  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19210  *
19211  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19212  * packet is eligible for hardware acceleration, and by
19213  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19214  * to a particular ill.
19215  */
19216 boolean_t
19217 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19218     ipsa_t *sa, netstack_t *ns)
19219 {
19220 	boolean_t sa_isv6;
19221 	uint_t algid;
19222 	struct ill_ipsec_capab_s *cpp;
19223 	boolean_t need_refrele = B_FALSE;
19224 	ip_stack_t	*ipst = ns->netstack_ip;
19225 
19226 	if (ill == NULL) {
19227 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19228 		    NULL, NULL, NULL, ipst);
19229 		if (ill == NULL) {
19230 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19231 			return (B_FALSE);
19232 		}
19233 		need_refrele = B_TRUE;
19234 	}
19235 
19236 	/*
19237 	 * Use the address length specified by the SA to determine
19238 	 * if it corresponds to a IPv6 address, and fail the matching
19239 	 * if the isv6 flag passed as argument does not match.
19240 	 * Note: this check is used for SADB capability checking before
19241 	 * sending SA information to an ill.
19242 	 */
19243 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19244 	if (sa_isv6 != ill_isv6)
19245 		/* protocol mismatch */
19246 		goto done;
19247 
19248 	/*
19249 	 * Check if the ill supports the protocol, algorithm(s) and
19250 	 * key size(s) specified by the SA, and get the pointers to
19251 	 * the algorithms supported by the ill.
19252 	 */
19253 	switch (sa->ipsa_type) {
19254 
19255 	case SADB_SATYPE_ESP:
19256 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19257 			/* ill does not support ESP acceleration */
19258 			goto done;
19259 		cpp = ill->ill_ipsec_capab_esp;
19260 		algid = sa->ipsa_auth_alg;
19261 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19262 			goto done;
19263 		algid = sa->ipsa_encr_alg;
19264 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19265 			goto done;
19266 		if (algid < cpp->encr_algparm_end) {
19267 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19268 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19269 				goto done;
19270 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19271 				goto done;
19272 		}
19273 		break;
19274 
19275 	case SADB_SATYPE_AH:
19276 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19277 			/* ill does not support AH acceleration */
19278 			goto done;
19279 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19280 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19281 			goto done;
19282 		break;
19283 	}
19284 
19285 	if (need_refrele)
19286 		ill_refrele(ill);
19287 	return (B_TRUE);
19288 done:
19289 	if (need_refrele)
19290 		ill_refrele(ill);
19291 	return (B_FALSE);
19292 }
19293 
19294 /*
19295  * Add a new ill to the list of IPsec capable ills.
19296  * Called from ill_capability_ipsec_ack() when an ACK was received
19297  * indicating that IPsec hardware processing was enabled for an ill.
19298  *
19299  * ill must point to the ill for which acceleration was enabled.
19300  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19301  */
19302 static void
19303 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19304 {
19305 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19306 	uint_t sa_type;
19307 	uint_t ipproto;
19308 	ip_stack_t	*ipst = ill->ill_ipst;
19309 
19310 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19311 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19312 
19313 	switch (dl_cap) {
19314 	case DL_CAPAB_IPSEC_AH:
19315 		sa_type = SADB_SATYPE_AH;
19316 		ills = &ipst->ips_ipsec_capab_ills_ah;
19317 		ipproto = IPPROTO_AH;
19318 		break;
19319 	case DL_CAPAB_IPSEC_ESP:
19320 		sa_type = SADB_SATYPE_ESP;
19321 		ills = &ipst->ips_ipsec_capab_ills_esp;
19322 		ipproto = IPPROTO_ESP;
19323 		break;
19324 	}
19325 
19326 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19327 
19328 	/*
19329 	 * Add ill index to list of hardware accelerators. If
19330 	 * already in list, do nothing.
19331 	 */
19332 	for (cur_ill = *ills; cur_ill != NULL &&
19333 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19334 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19335 		;
19336 
19337 	if (cur_ill == NULL) {
19338 		/* if this is a new entry for this ill */
19339 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19340 		if (new_ill == NULL) {
19341 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19342 			return;
19343 		}
19344 
19345 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19346 		new_ill->ill_isv6 = ill->ill_isv6;
19347 		new_ill->next = *ills;
19348 		*ills = new_ill;
19349 	} else if (!sadb_resync) {
19350 		/* not resync'ing SADB and an entry exists for this ill */
19351 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19352 		return;
19353 	}
19354 
19355 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19356 
19357 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19358 		/*
19359 		 * IPsec module for protocol loaded, initiate dump
19360 		 * of the SADB to this ill.
19361 		 */
19362 		sadb_ill_download(ill, sa_type);
19363 }
19364 
19365 /*
19366  * Remove an ill from the list of IPsec capable ills.
19367  */
19368 static void
19369 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19370 {
19371 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19372 	ip_stack_t	*ipst = ill->ill_ipst;
19373 
19374 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19375 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19376 
19377 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19378 	    &ipst->ips_ipsec_capab_ills_esp;
19379 
19380 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19381 
19382 	prev_ill = NULL;
19383 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19384 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19385 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19386 		;
19387 	if (cur_ill == NULL) {
19388 		/* entry not found */
19389 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19390 		return;
19391 	}
19392 	if (prev_ill == NULL) {
19393 		/* entry at front of list */
19394 		*ills = NULL;
19395 	} else {
19396 		prev_ill->next = cur_ill->next;
19397 	}
19398 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19399 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19400 }
19401 
19402 /*
19403  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19404  * supporting the specified IPsec protocol acceleration.
19405  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19406  * We free the mblk and, if sa is non-null, release the held referece.
19407  */
19408 void
19409 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19410     netstack_t *ns)
19411 {
19412 	ipsec_capab_ill_t *ici, *cur_ici;
19413 	ill_t *ill;
19414 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19415 	ip_stack_t	*ipst = ns->netstack_ip;
19416 
19417 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19418 	    ipst->ips_ipsec_capab_ills_esp;
19419 
19420 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19421 
19422 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19423 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19424 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19425 
19426 		/*
19427 		 * Handle the case where the ill goes away while the SADB is
19428 		 * attempting to send messages.  If it's going away, it's
19429 		 * nuking its shadow SADB, so we don't care..
19430 		 */
19431 
19432 		if (ill == NULL)
19433 			continue;
19434 
19435 		if (sa != NULL) {
19436 			/*
19437 			 * Make sure capabilities match before
19438 			 * sending SA to ill.
19439 			 */
19440 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19441 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19442 				ill_refrele(ill);
19443 				continue;
19444 			}
19445 
19446 			mutex_enter(&sa->ipsa_lock);
19447 			sa->ipsa_flags |= IPSA_F_HW;
19448 			mutex_exit(&sa->ipsa_lock);
19449 		}
19450 
19451 		/*
19452 		 * Copy template message, and add it to the front
19453 		 * of the mblk ship list. We want to avoid holding
19454 		 * the ipsec_capab_ills_lock while sending the
19455 		 * message to the ills.
19456 		 *
19457 		 * The b_next and b_prev are temporarily used
19458 		 * to build a list of mblks to be sent down, and to
19459 		 * save the ill to which they must be sent.
19460 		 */
19461 		nmp = copymsg(mp);
19462 		if (nmp == NULL) {
19463 			ill_refrele(ill);
19464 			continue;
19465 		}
19466 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19467 		nmp->b_next = mp_ship_list;
19468 		mp_ship_list = nmp;
19469 		nmp->b_prev = (mblk_t *)ill;
19470 	}
19471 
19472 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19473 
19474 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19475 		/* restore the mblk to a sane state */
19476 		next_mp = nmp->b_next;
19477 		nmp->b_next = NULL;
19478 		ill = (ill_t *)nmp->b_prev;
19479 		nmp->b_prev = NULL;
19480 
19481 		ill_dlpi_send(ill, nmp);
19482 		ill_refrele(ill);
19483 	}
19484 
19485 	if (sa != NULL)
19486 		IPSA_REFRELE(sa);
19487 	freemsg(mp);
19488 }
19489 
19490 /*
19491  * Derive an interface id from the link layer address.
19492  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19493  */
19494 static boolean_t
19495 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19496 {
19497 	char		*addr;
19498 
19499 	if (ill->ill_phys_addr_length != ETHERADDRL)
19500 		return (B_FALSE);
19501 
19502 	/* Form EUI-64 like address */
19503 	addr = (char *)&v6addr->s6_addr32[2];
19504 	bcopy(ill->ill_phys_addr, addr, 3);
19505 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19506 	addr[3] = (char)0xff;
19507 	addr[4] = (char)0xfe;
19508 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19509 	return (B_TRUE);
19510 }
19511 
19512 /* ARGSUSED */
19513 static boolean_t
19514 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19515 {
19516 	return (B_FALSE);
19517 }
19518 
19519 typedef struct ipmp_ifcookie {
19520 	uint32_t	ic_hostid;
19521 	char		ic_ifname[LIFNAMSIZ];
19522 	char		ic_zonename[ZONENAME_MAX];
19523 } ipmp_ifcookie_t;
19524 
19525 /*
19526  * Construct a pseudo-random interface ID for the IPMP interface that's both
19527  * predictable and (almost) guaranteed to be unique.
19528  */
19529 static boolean_t
19530 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19531 {
19532 	zone_t		*zp;
19533 	uint8_t		*addr;
19534 	uchar_t		hash[16];
19535 	ulong_t 	hostid;
19536 	MD5_CTX		ctx;
19537 	ipmp_ifcookie_t	ic = { 0 };
19538 
19539 	ASSERT(IS_IPMP(ill));
19540 
19541 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19542 	ic.ic_hostid = htonl((uint32_t)hostid);
19543 
19544 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19545 
19546 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19547 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19548 		zone_rele(zp);
19549 	}
19550 
19551 	MD5Init(&ctx);
19552 	MD5Update(&ctx, &ic, sizeof (ic));
19553 	MD5Final(hash, &ctx);
19554 
19555 	/*
19556 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19557 	 */
19558 	addr = &v6addr->s6_addr8[8];
19559 	bcopy(hash + 8, addr, sizeof (uint64_t));
19560 	addr[0] &= ~0x2;				/* set local bit */
19561 
19562 	return (B_TRUE);
19563 }
19564 
19565 /* ARGSUSED */
19566 static boolean_t
19567 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19568     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19569 {
19570 	/*
19571 	 * Multicast address mappings used over Ethernet/802.X.
19572 	 * This address is used as a base for mappings.
19573 	 */
19574 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19575 	    0x00, 0x00, 0x00};
19576 
19577 	/*
19578 	 * Extract low order 32 bits from IPv6 multicast address.
19579 	 * Or that into the link layer address, starting from the
19580 	 * second byte.
19581 	 */
19582 	*hw_start = 2;
19583 	v6_extract_mask->s6_addr32[0] = 0;
19584 	v6_extract_mask->s6_addr32[1] = 0;
19585 	v6_extract_mask->s6_addr32[2] = 0;
19586 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19587 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19588 	return (B_TRUE);
19589 }
19590 
19591 /*
19592  * Indicate by return value whether multicast is supported. If not,
19593  * this code should not touch/change any parameters.
19594  */
19595 /* ARGSUSED */
19596 static boolean_t
19597 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19598     uint32_t *hw_start, ipaddr_t *extract_mask)
19599 {
19600 	/*
19601 	 * Multicast address mappings used over Ethernet/802.X.
19602 	 * This address is used as a base for mappings.
19603 	 */
19604 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19605 	    0x00, 0x00, 0x00 };
19606 
19607 	if (phys_length != ETHERADDRL)
19608 		return (B_FALSE);
19609 
19610 	*extract_mask = htonl(0x007fffff);
19611 	*hw_start = 2;
19612 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19613 	return (B_TRUE);
19614 }
19615 
19616 /*
19617  * Derive IPoIB interface id from the link layer address.
19618  */
19619 static boolean_t
19620 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19621 {
19622 	char		*addr;
19623 
19624 	if (ill->ill_phys_addr_length != 20)
19625 		return (B_FALSE);
19626 	addr = (char *)&v6addr->s6_addr32[2];
19627 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19628 	/*
19629 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19630 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19631 	 * rules. In these cases, the IBA considers these GUIDs to be in
19632 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19633 	 * required; vendors are required not to assign global EUI-64's
19634 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19635 	 * of the interface identifier. Whether the GUID is in modified
19636 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19637 	 * bit set to 1.
19638 	 */
19639 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19640 	return (B_TRUE);
19641 }
19642 
19643 /*
19644  * Note on mapping from multicast IP addresses to IPoIB multicast link
19645  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19646  * The format of an IPoIB multicast address is:
19647  *
19648  *  4 byte QPN      Scope Sign.  Pkey
19649  * +--------------------------------------------+
19650  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19651  * +--------------------------------------------+
19652  *
19653  * The Scope and Pkey components are properties of the IBA port and
19654  * network interface. They can be ascertained from the broadcast address.
19655  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19656  */
19657 
19658 static boolean_t
19659 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19660     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19661 {
19662 	/*
19663 	 * Base IPoIB IPv6 multicast address used for mappings.
19664 	 * Does not contain the IBA scope/Pkey values.
19665 	 */
19666 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19667 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19668 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19669 
19670 	/*
19671 	 * Extract low order 80 bits from IPv6 multicast address.
19672 	 * Or that into the link layer address, starting from the
19673 	 * sixth byte.
19674 	 */
19675 	*hw_start = 6;
19676 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19677 
19678 	/*
19679 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19680 	 */
19681 	*(maddr + 5) = *(bphys_addr + 5);
19682 	*(maddr + 8) = *(bphys_addr + 8);
19683 	*(maddr + 9) = *(bphys_addr + 9);
19684 
19685 	v6_extract_mask->s6_addr32[0] = 0;
19686 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19687 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19688 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19689 	return (B_TRUE);
19690 }
19691 
19692 static boolean_t
19693 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19694     uint32_t *hw_start, ipaddr_t *extract_mask)
19695 {
19696 	/*
19697 	 * Base IPoIB IPv4 multicast address used for mappings.
19698 	 * Does not contain the IBA scope/Pkey values.
19699 	 */
19700 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19701 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19702 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19703 
19704 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19705 		return (B_FALSE);
19706 
19707 	/*
19708 	 * Extract low order 28 bits from IPv4 multicast address.
19709 	 * Or that into the link layer address, starting from the
19710 	 * sixteenth byte.
19711 	 */
19712 	*extract_mask = htonl(0x0fffffff);
19713 	*hw_start = 16;
19714 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19715 
19716 	/*
19717 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19718 	 */
19719 	*(maddr + 5) = *(bphys_addr + 5);
19720 	*(maddr + 8) = *(bphys_addr + 8);
19721 	*(maddr + 9) = *(bphys_addr + 9);
19722 	return (B_TRUE);
19723 }
19724 
19725 /*
19726  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19727  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19728  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19729  * the link-local address is preferred.
19730  */
19731 boolean_t
19732 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19733 {
19734 	ipif_t	*ipif;
19735 	ipif_t	*maybe_ipif = NULL;
19736 
19737 	mutex_enter(&ill->ill_lock);
19738 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19739 		mutex_exit(&ill->ill_lock);
19740 		if (ipifp != NULL)
19741 			*ipifp = NULL;
19742 		return (B_FALSE);
19743 	}
19744 
19745 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19746 		if (!IPIF_CAN_LOOKUP(ipif))
19747 			continue;
19748 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19749 		    ipif->ipif_zoneid != ALL_ZONES)
19750 			continue;
19751 		if ((ipif->ipif_flags & flags) != flags)
19752 			continue;
19753 
19754 		if (ipifp == NULL) {
19755 			mutex_exit(&ill->ill_lock);
19756 			ASSERT(maybe_ipif == NULL);
19757 			return (B_TRUE);
19758 		}
19759 		if (!ill->ill_isv6 ||
19760 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19761 			ipif_refhold_locked(ipif);
19762 			mutex_exit(&ill->ill_lock);
19763 			*ipifp = ipif;
19764 			return (B_TRUE);
19765 		}
19766 		if (maybe_ipif == NULL)
19767 			maybe_ipif = ipif;
19768 	}
19769 	if (ipifp != NULL) {
19770 		if (maybe_ipif != NULL)
19771 			ipif_refhold_locked(maybe_ipif);
19772 		*ipifp = maybe_ipif;
19773 	}
19774 	mutex_exit(&ill->ill_lock);
19775 	return (maybe_ipif != NULL);
19776 }
19777 
19778 /*
19779  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19780  * If a pointer to an ipif_t is returned then the caller will need to do
19781  * an ill_refrele().
19782  */
19783 ipif_t *
19784 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19785     ip_stack_t *ipst)
19786 {
19787 	ipif_t *ipif;
19788 	ill_t *ill;
19789 
19790 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19791 	    ipst);
19792 	if (ill == NULL)
19793 		return (NULL);
19794 
19795 	mutex_enter(&ill->ill_lock);
19796 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19797 		mutex_exit(&ill->ill_lock);
19798 		ill_refrele(ill);
19799 		return (NULL);
19800 	}
19801 
19802 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19803 		if (!IPIF_CAN_LOOKUP(ipif))
19804 			continue;
19805 		if (lifidx == ipif->ipif_id) {
19806 			ipif_refhold_locked(ipif);
19807 			break;
19808 		}
19809 	}
19810 
19811 	mutex_exit(&ill->ill_lock);
19812 	ill_refrele(ill);
19813 	return (ipif);
19814 }
19815 
19816 /*
19817  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19818  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19819  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19820  * for details.
19821  */
19822 void
19823 ill_fastpath_flush(ill_t *ill)
19824 {
19825 	ip_stack_t *ipst = ill->ill_ipst;
19826 
19827 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19828 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19829 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19830 }
19831 
19832 /*
19833  * Set the physical address information for `ill' to the contents of the
19834  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
19835  * asynchronous if `ill' cannot immediately be quiesced -- in which case
19836  * EINPROGRESS will be returned.
19837  */
19838 int
19839 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
19840 {
19841 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19842 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
19843 
19844 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19845 
19846 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
19847 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
19848 		/* Changing DL_IPV6_TOKEN is not yet supported */
19849 		return (0);
19850 	}
19851 
19852 	/*
19853 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
19854 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
19855 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
19856 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
19857 	 */
19858 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
19859 		freemsg(mp);
19860 		return (ENOMEM);
19861 	}
19862 
19863 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19864 
19865 	/*
19866 	 * If we can quiesce the ill, then set the address.  If not, then
19867 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
19868 	 */
19869 	ill_down_ipifs(ill, B_TRUE);
19870 	mutex_enter(&ill->ill_lock);
19871 	if (!ill_is_quiescent(ill)) {
19872 		/* call cannot fail since `conn_t *' argument is NULL */
19873 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19874 		    mp, ILL_DOWN);
19875 		mutex_exit(&ill->ill_lock);
19876 		return (EINPROGRESS);
19877 	}
19878 	mutex_exit(&ill->ill_lock);
19879 
19880 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
19881 	return (0);
19882 }
19883 
19884 /*
19885  * Once the ill associated with `q' has quiesced, set its physical address
19886  * information to the values in `addrmp'.  Note that two copies of `addrmp'
19887  * are passed (linked by b_cont), since we sometimes need to save two distinct
19888  * copies in the ill_t, and our context doesn't permit sleeping or allocation
19889  * failure (we'll free the other copy if it's not needed).  Since the ill_t
19890  * is quiesced, we know any stale IREs with the old address information have
19891  * already been removed, so we don't need to call ill_fastpath_flush().
19892  */
19893 /* ARGSUSED */
19894 static void
19895 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
19896 {
19897 	ill_t		*ill = q->q_ptr;
19898 	mblk_t		*addrmp2 = unlinkb(addrmp);
19899 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
19900 	uint_t		addrlen, addroff;
19901 
19902 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19903 
19904 	addroff	= dlindp->dl_addr_offset;
19905 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
19906 
19907 	switch (dlindp->dl_data) {
19908 	case DL_IPV6_LINK_LAYER_ADDR:
19909 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
19910 		freemsg(addrmp2);
19911 		break;
19912 
19913 	case DL_CURR_PHYS_ADDR:
19914 		freemsg(ill->ill_phys_addr_mp);
19915 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
19916 		ill->ill_phys_addr_mp = addrmp;
19917 		ill->ill_phys_addr_length = addrlen;
19918 
19919 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
19920 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
19921 		else
19922 			freemsg(addrmp2);
19923 		break;
19924 	default:
19925 		ASSERT(0);
19926 	}
19927 
19928 	/*
19929 	 * If there are ipifs to bring up, ill_up_ipifs() will return
19930 	 * EINPROGRESS, and ipsq_current_finish() will be called by
19931 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
19932 	 * brought up.
19933 	 */
19934 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
19935 		ipsq_current_finish(ipsq);
19936 }
19937 
19938 /*
19939  * Helper routine for setting the ill_nd_lla fields.
19940  */
19941 void
19942 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
19943 {
19944 	freemsg(ill->ill_nd_lla_mp);
19945 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
19946 	ill->ill_nd_lla_mp = ndmp;
19947 	ill->ill_nd_lla_len = addrlen;
19948 }
19949 
19950 /*
19951  * Replumb the ill.
19952  */
19953 int
19954 ill_replumb(ill_t *ill, mblk_t *mp)
19955 {
19956 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19957 
19958 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19959 
19960 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19961 
19962 	/*
19963 	 * If we can quiesce the ill, then continue.  If not, then
19964 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
19965 	 */
19966 	ill_down_ipifs(ill, B_FALSE);
19967 
19968 	mutex_enter(&ill->ill_lock);
19969 	if (!ill_is_quiescent(ill)) {
19970 		/* call cannot fail since `conn_t *' argument is NULL */
19971 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19972 		    mp, ILL_DOWN);
19973 		mutex_exit(&ill->ill_lock);
19974 		return (EINPROGRESS);
19975 	}
19976 	mutex_exit(&ill->ill_lock);
19977 
19978 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
19979 	return (0);
19980 }
19981 
19982 /* ARGSUSED */
19983 static void
19984 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
19985 {
19986 	ill_t *ill = q->q_ptr;
19987 
19988 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19989 
19990 	ill_down_ipifs_tail(ill);
19991 
19992 	freemsg(ill->ill_replumb_mp);
19993 	ill->ill_replumb_mp = copyb(mp);
19994 
19995 	/*
19996 	 * Successfully quiesced and brought down the interface, now we send
19997 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
19998 	 * DL_NOTE_REPLUMB message.
19999 	 */
20000 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
20001 	    DL_NOTIFY_CONF);
20002 	ASSERT(mp != NULL);
20003 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
20004 	    DL_NOTE_REPLUMB_DONE;
20005 	ill_dlpi_send(ill, mp);
20006 
20007 	/*
20008 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20009 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20010 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20011 	 * brought up.
20012 	 */
20013 	if (ill->ill_replumb_mp == NULL ||
20014 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) != EINPROGRESS) {
20015 		ipsq_current_finish(ipsq);
20016 	}
20017 }
20018 
20019 major_t IP_MAJ;
20020 #define	IP	"ip"
20021 
20022 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20023 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20024 
20025 /*
20026  * Issue REMOVEIF ioctls to have the loopback interfaces
20027  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20028  * the former going away when the user-level processes in the zone
20029  * are killed  * and the latter are cleaned up by the stream head
20030  * str_stack_shutdown callback that undoes all I_PLINKs.
20031  */
20032 void
20033 ip_loopback_cleanup(ip_stack_t *ipst)
20034 {
20035 	int error;
20036 	ldi_handle_t	lh = NULL;
20037 	ldi_ident_t	li = NULL;
20038 	int		rval;
20039 	cred_t		*cr;
20040 	struct strioctl iocb;
20041 	struct lifreq	lifreq;
20042 
20043 	IP_MAJ = ddi_name_to_major(IP);
20044 
20045 #ifdef NS_DEBUG
20046 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20047 	    ipst->ips_netstack->netstack_stackid);
20048 #endif
20049 
20050 	bzero(&lifreq, sizeof (lifreq));
20051 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20052 
20053 	error = ldi_ident_from_major(IP_MAJ, &li);
20054 	if (error) {
20055 #ifdef DEBUG
20056 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20057 		    error);
20058 #endif
20059 		return;
20060 	}
20061 
20062 	cr = zone_get_kcred(netstackid_to_zoneid(
20063 	    ipst->ips_netstack->netstack_stackid));
20064 	ASSERT(cr != NULL);
20065 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20066 	if (error) {
20067 #ifdef DEBUG
20068 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20069 		    error);
20070 #endif
20071 		goto out;
20072 	}
20073 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20074 	iocb.ic_timout = 15;
20075 	iocb.ic_len = sizeof (lifreq);
20076 	iocb.ic_dp = (char *)&lifreq;
20077 
20078 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20079 	/* LINTED - statement has no consequent */
20080 	if (error) {
20081 #ifdef NS_DEBUG
20082 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20083 		    "UDP6 error %d\n", error);
20084 #endif
20085 	}
20086 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20087 	lh = NULL;
20088 
20089 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20090 	if (error) {
20091 #ifdef NS_DEBUG
20092 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20093 		    error);
20094 #endif
20095 		goto out;
20096 	}
20097 
20098 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20099 	iocb.ic_timout = 15;
20100 	iocb.ic_len = sizeof (lifreq);
20101 	iocb.ic_dp = (char *)&lifreq;
20102 
20103 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20104 	/* LINTED - statement has no consequent */
20105 	if (error) {
20106 #ifdef NS_DEBUG
20107 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20108 		    "UDP error %d\n", error);
20109 #endif
20110 	}
20111 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20112 	lh = NULL;
20113 
20114 out:
20115 	/* Close layered handles */
20116 	if (lh)
20117 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20118 	if (li)
20119 		ldi_ident_release(li);
20120 
20121 	crfree(cr);
20122 }
20123 
20124 /*
20125  * This needs to be in-sync with nic_event_t definition
20126  */
20127 static const char *
20128 ill_hook_event2str(nic_event_t event)
20129 {
20130 	switch (event) {
20131 	case NE_PLUMB:
20132 		return ("PLUMB");
20133 	case NE_UNPLUMB:
20134 		return ("UNPLUMB");
20135 	case NE_UP:
20136 		return ("UP");
20137 	case NE_DOWN:
20138 		return ("DOWN");
20139 	case NE_ADDRESS_CHANGE:
20140 		return ("ADDRESS_CHANGE");
20141 	case NE_LIF_UP:
20142 		return ("LIF_UP");
20143 	case NE_LIF_DOWN:
20144 		return ("LIF_DOWN");
20145 	default:
20146 		return ("UNKNOWN");
20147 	}
20148 }
20149 
20150 void
20151 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20152     nic_event_data_t data, size_t datalen)
20153 {
20154 	ip_stack_t		*ipst = ill->ill_ipst;
20155 	hook_nic_event_int_t	*info;
20156 	const char		*str = NULL;
20157 
20158 	/* create a new nic event info */
20159 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20160 		goto fail;
20161 
20162 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20163 	info->hnei_event.hne_lif = lif;
20164 	info->hnei_event.hne_event = event;
20165 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20166 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20167 	info->hnei_event.hne_data = NULL;
20168 	info->hnei_event.hne_datalen = 0;
20169 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20170 
20171 	if (data != NULL && datalen != 0) {
20172 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20173 		if (info->hnei_event.hne_data == NULL)
20174 			goto fail;
20175 		bcopy(data, info->hnei_event.hne_data, datalen);
20176 		info->hnei_event.hne_datalen = datalen;
20177 	}
20178 
20179 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20180 	    DDI_NOSLEEP) == DDI_SUCCESS)
20181 		return;
20182 
20183 fail:
20184 	if (info != NULL) {
20185 		if (info->hnei_event.hne_data != NULL) {
20186 			kmem_free(info->hnei_event.hne_data,
20187 			    info->hnei_event.hne_datalen);
20188 		}
20189 		kmem_free(info, sizeof (hook_nic_event_t));
20190 	}
20191 	str = ill_hook_event2str(event);
20192 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20193 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20194 }
20195 
20196 void
20197 ipif_up_notify(ipif_t *ipif)
20198 {
20199 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20200 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20201 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20202 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20203 	    NE_LIF_UP, NULL, 0);
20204 }
20205