xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision ef2504f26d1ea5859db9838255bb63f488f1b050)
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  * Send a DL_NOTIFY_REQ to the specified ill to enable
2074  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2075  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2076  * acceleration.
2077  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2078  */
2079 static boolean_t
2080 ill_enable_promisc_notify(ill_t *ill)
2081 {
2082 	mblk_t *mp;
2083 	dl_notify_req_t *req;
2084 
2085 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2086 
2087 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2088 	if (mp == NULL)
2089 		return (B_FALSE);
2090 
2091 	req = (dl_notify_req_t *)mp->b_rptr;
2092 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2093 	    DL_NOTE_PROMISC_OFF_PHYS;
2094 
2095 	ill_dlpi_send(ill, mp);
2096 
2097 	return (B_TRUE);
2098 }
2099 
2100 /*
2101  * Allocate an IPsec capability request which will be filled by our
2102  * caller to turn on support for one or more algorithms.
2103  */
2104 static mblk_t *
2105 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2106 {
2107 	mblk_t *nmp;
2108 	dl_capability_req_t	*ocap;
2109 	dl_capab_ipsec_t	*ocip;
2110 	dl_capab_ipsec_t	*icip;
2111 	uint8_t			*ptr;
2112 	icip = (dl_capab_ipsec_t *)(isub + 1);
2113 
2114 	/*
2115 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2116 	 * PROMISC_ON/OFF notification from the provider. We need to
2117 	 * do this before enabling the algorithms to avoid leakage of
2118 	 * cleartext packets.
2119 	 */
2120 
2121 	if (!ill_enable_promisc_notify(ill))
2122 		return (NULL);
2123 
2124 	/*
2125 	 * Allocate new mblk which will contain a new capability
2126 	 * request to enable the capabilities.
2127 	 */
2128 
2129 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2130 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2131 	if (nmp == NULL)
2132 		return (NULL);
2133 
2134 	ptr = nmp->b_rptr;
2135 
2136 	/* initialize dl_capability_req_t */
2137 	ocap = (dl_capability_req_t *)ptr;
2138 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2139 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2140 	ptr += sizeof (dl_capability_req_t);
2141 
2142 	/* initialize dl_capability_sub_t */
2143 	bcopy(isub, ptr, sizeof (*isub));
2144 	ptr += sizeof (*isub);
2145 
2146 	/* initialize dl_capab_ipsec_t */
2147 	ocip = (dl_capab_ipsec_t *)ptr;
2148 	bcopy(icip, ocip, sizeof (*icip));
2149 
2150 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2151 	return (nmp);
2152 }
2153 
2154 /*
2155  * Process an IPsec capability negotiation ack received from a DLS Provider.
2156  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2157  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2158  */
2159 static void
2160 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2161 {
2162 	dl_capab_ipsec_t	*icip;
2163 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2164 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2165 	uint_t cipher, nciphers;
2166 	mblk_t *nmp;
2167 	uint_t alg_len;
2168 	boolean_t need_sadb_dump;
2169 	uint_t sub_dl_cap = isub->dl_cap;
2170 	ill_ipsec_capab_t **ill_capab;
2171 	uint64_t ill_capab_flag;
2172 	uint8_t *capend, *ciphend;
2173 	boolean_t sadb_resync;
2174 
2175 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2176 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2177 
2178 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2179 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2180 		ill_capab_flag = ILL_CAPAB_AH;
2181 	} else {
2182 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2183 		ill_capab_flag = ILL_CAPAB_ESP;
2184 	}
2185 
2186 	/*
2187 	 * If the ill capability structure exists, then this incoming
2188 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2189 	 * If this is so, then we'd need to resynchronize the SADB
2190 	 * after re-enabling the offloaded ciphers.
2191 	 */
2192 	sadb_resync = (*ill_capab != NULL);
2193 
2194 	/*
2195 	 * Note: range checks here are not absolutely sufficient to
2196 	 * make us robust against malformed messages sent by drivers;
2197 	 * this is in keeping with the rest of IP's dlpi handling.
2198 	 * (Remember, it's coming from something else in the kernel
2199 	 * address space)
2200 	 */
2201 
2202 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2203 	if (capend > mp->b_wptr) {
2204 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2205 		    "malformed sub-capability too long for mblk");
2206 		return;
2207 	}
2208 
2209 	/*
2210 	 * There are two types of acks we process here:
2211 	 * 1. acks in reply to a (first form) generic capability req
2212 	 *    (no ENABLE flag set)
2213 	 * 2. acks in reply to a ENABLE capability req.
2214 	 *    (ENABLE flag set)
2215 	 *
2216 	 * We process the subcapability passed as argument as follows:
2217 	 * 1 do initializations
2218 	 *   1.1 initialize nmp = NULL
2219 	 *   1.2 set need_sadb_dump to B_FALSE
2220 	 * 2 for each cipher in subcapability:
2221 	 *   2.1 if ENABLE flag is set:
2222 	 *	2.1.1 update per-ill ipsec capabilities info
2223 	 *	2.1.2 set need_sadb_dump to B_TRUE
2224 	 *   2.2 if ENABLE flag is not set:
2225 	 *	2.2.1 if nmp is NULL:
2226 	 *		2.2.1.1 allocate and initialize nmp
2227 	 *		2.2.1.2 init current pos in nmp
2228 	 *	2.2.2 copy current cipher to current pos in nmp
2229 	 *	2.2.3 set ENABLE flag in nmp
2230 	 *	2.2.4 update current pos
2231 	 * 3 if nmp is not equal to NULL, send enable request
2232 	 *   3.1 send capability request
2233 	 * 4 if need_sadb_dump is B_TRUE
2234 	 *   4.1 enable promiscuous on/off notifications
2235 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2236 	 *	AH or ESP SA's to interface.
2237 	 */
2238 
2239 	nmp = NULL;
2240 	oalg = NULL;
2241 	need_sadb_dump = B_FALSE;
2242 	icip = (dl_capab_ipsec_t *)(isub + 1);
2243 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2244 
2245 	nciphers = icip->cip_nciphers;
2246 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2247 
2248 	if (ciphend > capend) {
2249 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2250 		    "too many ciphers for sub-capability len");
2251 		return;
2252 	}
2253 
2254 	for (cipher = 0; cipher < nciphers; cipher++) {
2255 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2256 
2257 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2258 			/*
2259 			 * TBD: when we provide a way to disable capabilities
2260 			 * from above, need to manage the request-pending state
2261 			 * and fail if we were not expecting this ACK.
2262 			 */
2263 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2264 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2265 
2266 			/*
2267 			 * Update IPsec capabilities for this ill
2268 			 */
2269 
2270 			if (*ill_capab == NULL) {
2271 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2272 				    ("ill_capability_ipsec_ack: "
2273 				    "allocating ipsec_capab for ill\n"));
2274 				*ill_capab = ill_ipsec_capab_alloc();
2275 
2276 				if (*ill_capab == NULL) {
2277 					cmn_err(CE_WARN,
2278 					    "ill_capability_ipsec_ack: "
2279 					    "could not enable IPsec Hardware "
2280 					    "acceleration for %s (ENOMEM)\n",
2281 					    ill->ill_name);
2282 					return;
2283 				}
2284 			}
2285 
2286 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2287 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2288 
2289 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2290 				cmn_err(CE_WARN,
2291 				    "ill_capability_ipsec_ack: "
2292 				    "malformed IPsec algorithm id %d",
2293 				    ialg->alg_prim);
2294 				continue;
2295 			}
2296 
2297 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2298 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2299 				    ialg->alg_prim);
2300 			} else {
2301 				ipsec_capab_algparm_t *alp;
2302 
2303 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2304 				    ialg->alg_prim);
2305 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2306 				    ialg->alg_prim)) {
2307 					cmn_err(CE_WARN,
2308 					    "ill_capability_ipsec_ack: "
2309 					    "no space for IPsec alg id %d",
2310 					    ialg->alg_prim);
2311 					continue;
2312 				}
2313 				alp = &((*ill_capab)->encr_algparm[
2314 				    ialg->alg_prim]);
2315 				alp->minkeylen = ialg->alg_minbits;
2316 				alp->maxkeylen = ialg->alg_maxbits;
2317 			}
2318 			ill->ill_capabilities |= ill_capab_flag;
2319 			/*
2320 			 * indicate that a capability was enabled, which
2321 			 * will be used below to kick off a SADB dump
2322 			 * to the ill.
2323 			 */
2324 			need_sadb_dump = B_TRUE;
2325 		} else {
2326 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2327 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2328 			    ialg->alg_prim));
2329 
2330 			if (nmp == NULL) {
2331 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2332 				if (nmp == NULL) {
2333 					/*
2334 					 * Sending the PROMISC_ON/OFF
2335 					 * notification request failed.
2336 					 * We cannot enable the algorithms
2337 					 * since the Provider will not
2338 					 * notify IP of promiscous mode
2339 					 * changes, which could lead
2340 					 * to leakage of packets.
2341 					 */
2342 					cmn_err(CE_WARN,
2343 					    "ill_capability_ipsec_ack: "
2344 					    "could not enable IPsec Hardware "
2345 					    "acceleration for %s (ENOMEM)\n",
2346 					    ill->ill_name);
2347 					return;
2348 				}
2349 				/* ptr to current output alg specifier */
2350 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2351 			}
2352 
2353 			/*
2354 			 * Copy current alg specifier, set ENABLE
2355 			 * flag, and advance to next output alg.
2356 			 * For now we enable all IPsec capabilities.
2357 			 */
2358 			ASSERT(oalg != NULL);
2359 			bcopy(ialg, oalg, alg_len);
2360 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2361 			nmp->b_wptr += alg_len;
2362 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2363 		}
2364 
2365 		/* move to next input algorithm specifier */
2366 		ialg = (dl_capab_ipsec_alg_t *)
2367 		    ((char *)ialg + alg_len);
2368 	}
2369 
2370 	if (nmp != NULL)
2371 		/*
2372 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2373 		 * IPsec hardware acceleration.
2374 		 */
2375 		ill_capability_send(ill, nmp);
2376 
2377 	if (need_sadb_dump)
2378 		/*
2379 		 * An acknowledgement corresponding to a request to
2380 		 * enable acceleration was received, notify SADB.
2381 		 */
2382 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2383 }
2384 
2385 /*
2386  * Given an mblk with enough space in it, create sub-capability entries for
2387  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2388  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2389  * in preparation for the reset the DL_CAPABILITY_REQ message.
2390  */
2391 static void
2392 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2393     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2394 {
2395 	dl_capab_ipsec_t *oipsec;
2396 	dl_capab_ipsec_alg_t *oalg;
2397 	dl_capability_sub_t *dl_subcap;
2398 	int i, k;
2399 
2400 	ASSERT(nciphers > 0);
2401 	ASSERT(ill_cap != NULL);
2402 	ASSERT(mp != NULL);
2403 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2404 
2405 	/* dl_capability_sub_t for "stype" */
2406 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2407 	dl_subcap->dl_cap = stype;
2408 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2409 	mp->b_wptr += sizeof (dl_capability_sub_t);
2410 
2411 	/* dl_capab_ipsec_t for "stype" */
2412 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2413 	oipsec->cip_version = 1;
2414 	oipsec->cip_nciphers = nciphers;
2415 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2416 
2417 	/* create entries for "stype" AUTH ciphers */
2418 	for (i = 0; i < ill_cap->algs_size; i++) {
2419 		for (k = 0; k < BITSPERBYTE; k++) {
2420 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2421 				continue;
2422 
2423 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2424 			bzero((void *)oalg, sizeof (*oalg));
2425 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2426 			oalg->alg_prim = k + (BITSPERBYTE * i);
2427 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2428 		}
2429 	}
2430 	/* create entries for "stype" ENCR ciphers */
2431 	for (i = 0; i < ill_cap->algs_size; i++) {
2432 		for (k = 0; k < BITSPERBYTE; k++) {
2433 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2434 				continue;
2435 
2436 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2437 			bzero((void *)oalg, sizeof (*oalg));
2438 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2439 			oalg->alg_prim = k + (BITSPERBYTE * i);
2440 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2441 		}
2442 	}
2443 }
2444 
2445 /*
2446  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2447  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2448  * POPC instruction, but our macro is more flexible for an arbitrary length
2449  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2450  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2451  * stays that way, we can reduce the number of iterations required.
2452  */
2453 #define	COUNT_1S(val, sum) {					\
2454 	uint8_t x = val & 0xff;					\
2455 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2456 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2457 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2458 }
2459 
2460 /* ARGSUSED */
2461 static int
2462 ill_capability_ipsec_reset_size(ill_t *ill, int *ah_cntp, int *ah_lenp,
2463     int *esp_cntp, int *esp_lenp)
2464 {
2465 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2466 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2467 	uint64_t ill_capabilities = ill->ill_capabilities;
2468 	int ah_cnt = 0, esp_cnt = 0;
2469 	int ah_len = 0, esp_len = 0;
2470 	int i, size = 0;
2471 
2472 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2473 		return (0);
2474 
2475 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2476 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2477 
2478 	/* Find out the number of ciphers for AH */
2479 	if (cap_ah != NULL) {
2480 		for (i = 0; i < cap_ah->algs_size; i++) {
2481 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2482 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2483 		}
2484 		if (ah_cnt > 0) {
2485 			size += sizeof (dl_capability_sub_t) +
2486 			    sizeof (dl_capab_ipsec_t);
2487 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2488 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2489 			size += ah_len;
2490 		}
2491 	}
2492 
2493 	/* Find out the number of ciphers for ESP */
2494 	if (cap_esp != NULL) {
2495 		for (i = 0; i < cap_esp->algs_size; i++) {
2496 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2497 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2498 		}
2499 		if (esp_cnt > 0) {
2500 			size += sizeof (dl_capability_sub_t) +
2501 			    sizeof (dl_capab_ipsec_t);
2502 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2503 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2504 			size += esp_len;
2505 		}
2506 	}
2507 
2508 	if (ah_cntp != NULL)
2509 		*ah_cntp = ah_cnt;
2510 	if (ah_lenp != NULL)
2511 		*ah_lenp = ah_len;
2512 	if (esp_cntp != NULL)
2513 		*esp_cntp = esp_cnt;
2514 	if (esp_lenp != NULL)
2515 		*esp_lenp = esp_len;
2516 
2517 	return (size);
2518 }
2519 
2520 /* ARGSUSED */
2521 static void
2522 ill_capability_ipsec_reset_fill(ill_t *ill, mblk_t *mp)
2523 {
2524 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2525 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2526 	int ah_cnt = 0, esp_cnt = 0;
2527 	int ah_len = 0, esp_len = 0;
2528 	int size;
2529 
2530 	size = ill_capability_ipsec_reset_size(ill, &ah_cnt, &ah_len,
2531 	    &esp_cnt, &esp_len);
2532 	if (size == 0)
2533 		return;
2534 
2535 	/*
2536 	 * Clear the capability flags for IPsec HA but retain the ill
2537 	 * capability structures since it's possible that another thread
2538 	 * is still referring to them.  The structures only get deallocated
2539 	 * when we destroy the ill.
2540 	 *
2541 	 * Various places check the flags to see if the ill is capable of
2542 	 * hardware acceleration, and by clearing them we ensure that new
2543 	 * outbound IPsec packets are sent down encrypted.
2544 	 */
2545 
2546 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2547 	if (ah_cnt > 0) {
2548 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2549 		    cap_ah, mp);
2550 	}
2551 
2552 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2553 	if (esp_cnt > 0) {
2554 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2555 		    cap_esp, mp);
2556 	}
2557 
2558 	/*
2559 	 * At this point we've composed a bunch of sub-capabilities to be
2560 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2561 	 * by the caller.  Upon receiving this reset message, the driver
2562 	 * must stop inbound decryption (by destroying all inbound SAs)
2563 	 * and let the corresponding packets come in encrypted.
2564 	 */
2565 }
2566 
2567 static void
2568 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2569     boolean_t encapsulated)
2570 {
2571 	boolean_t legacy = B_FALSE;
2572 
2573 	/*
2574 	 * Note that only the following two sub-capabilities may be
2575 	 * considered as "legacy", since their original definitions
2576 	 * do not incorporate the dl_mid_t module ID token, and hence
2577 	 * may require the use of the wrapper sub-capability.
2578 	 */
2579 	switch (subp->dl_cap) {
2580 	case DL_CAPAB_IPSEC_AH:
2581 	case DL_CAPAB_IPSEC_ESP:
2582 		legacy = B_TRUE;
2583 		break;
2584 	}
2585 
2586 	/*
2587 	 * For legacy sub-capabilities which don't incorporate a queue_t
2588 	 * pointer in their structures, discard them if we detect that
2589 	 * there are intermediate modules in between IP and the driver.
2590 	 */
2591 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2592 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2593 		    "%d discarded; %d module(s) present below IP\n",
2594 		    subp->dl_cap, ill->ill_lmod_cnt));
2595 		return;
2596 	}
2597 
2598 	switch (subp->dl_cap) {
2599 	case DL_CAPAB_IPSEC_AH:
2600 	case DL_CAPAB_IPSEC_ESP:
2601 		ill_capability_ipsec_ack(ill, mp, subp);
2602 		break;
2603 	case DL_CAPAB_MDT:
2604 		ill_capability_mdt_ack(ill, mp, subp);
2605 		break;
2606 	case DL_CAPAB_HCKSUM:
2607 		ill_capability_hcksum_ack(ill, mp, subp);
2608 		break;
2609 	case DL_CAPAB_ZEROCOPY:
2610 		ill_capability_zerocopy_ack(ill, mp, subp);
2611 		break;
2612 	case DL_CAPAB_DLD:
2613 		ill_capability_dld_ack(ill, mp, subp);
2614 		break;
2615 	default:
2616 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2617 		    subp->dl_cap));
2618 	}
2619 }
2620 
2621 /*
2622  * Process a hardware checksum offload capability negotiation ack received
2623  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2624  * of a DL_CAPABILITY_ACK message.
2625  */
2626 static void
2627 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2628 {
2629 	dl_capability_req_t	*ocap;
2630 	dl_capab_hcksum_t	*ihck, *ohck;
2631 	ill_hcksum_capab_t	**ill_hcksum;
2632 	mblk_t			*nmp = NULL;
2633 	uint_t			sub_dl_cap = isub->dl_cap;
2634 	uint8_t			*capend;
2635 
2636 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
2637 
2638 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
2639 
2640 	/*
2641 	 * Note: range checks here are not absolutely sufficient to
2642 	 * make us robust against malformed messages sent by drivers;
2643 	 * this is in keeping with the rest of IP's dlpi handling.
2644 	 * (Remember, it's coming from something else in the kernel
2645 	 * address space)
2646 	 */
2647 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2648 	if (capend > mp->b_wptr) {
2649 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2650 		    "malformed sub-capability too long for mblk");
2651 		return;
2652 	}
2653 
2654 	/*
2655 	 * There are two types of acks we process here:
2656 	 * 1. acks in reply to a (first form) generic capability req
2657 	 *    (no ENABLE flag set)
2658 	 * 2. acks in reply to a ENABLE capability req.
2659 	 *    (ENABLE flag set)
2660 	 */
2661 	ihck = (dl_capab_hcksum_t *)(isub + 1);
2662 
2663 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
2664 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
2665 		    "unsupported hardware checksum "
2666 		    "sub-capability (version %d, expected %d)",
2667 		    ihck->hcksum_version, HCKSUM_VERSION_1);
2668 		return;
2669 	}
2670 
2671 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
2672 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
2673 		    "checksum capability isn't as expected; pass-thru "
2674 		    "module(s) detected, discarding capability\n"));
2675 		return;
2676 	}
2677 
2678 #define	CURR_HCKSUM_CAPAB				\
2679 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
2680 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
2681 
2682 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
2683 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
2684 		/* do ENABLE processing */
2685 		if (*ill_hcksum == NULL) {
2686 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
2687 			    KM_NOSLEEP);
2688 
2689 			if (*ill_hcksum == NULL) {
2690 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2691 				    "could not enable hcksum version %d "
2692 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
2693 				    ill->ill_name);
2694 				return;
2695 			}
2696 		}
2697 
2698 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
2699 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
2700 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
2701 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
2702 		    "has enabled hardware checksumming\n ",
2703 		    ill->ill_name));
2704 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
2705 		/*
2706 		 * Enabling hardware checksum offload
2707 		 * Currently IP supports {TCP,UDP}/IPv4
2708 		 * partial and full cksum offload and
2709 		 * IPv4 header checksum offload.
2710 		 * Allocate new mblk which will
2711 		 * contain a new capability request
2712 		 * to enable hardware checksum offload.
2713 		 */
2714 		uint_t	size;
2715 		uchar_t	*rptr;
2716 
2717 		size = sizeof (dl_capability_req_t) +
2718 		    sizeof (dl_capability_sub_t) + isub->dl_length;
2719 
2720 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2721 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2722 			    "could not enable hardware cksum for %s (ENOMEM)\n",
2723 			    ill->ill_name);
2724 			return;
2725 		}
2726 
2727 		rptr = nmp->b_rptr;
2728 		/* initialize dl_capability_req_t */
2729 		ocap = (dl_capability_req_t *)nmp->b_rptr;
2730 		ocap->dl_sub_offset =
2731 		    sizeof (dl_capability_req_t);
2732 		ocap->dl_sub_length =
2733 		    sizeof (dl_capability_sub_t) +
2734 		    isub->dl_length;
2735 		nmp->b_rptr += sizeof (dl_capability_req_t);
2736 
2737 		/* initialize dl_capability_sub_t */
2738 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2739 		nmp->b_rptr += sizeof (*isub);
2740 
2741 		/* initialize dl_capab_hcksum_t */
2742 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
2743 		bcopy(ihck, ohck, sizeof (*ihck));
2744 
2745 		nmp->b_rptr = rptr;
2746 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2747 
2748 		/* Set ENABLE flag */
2749 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
2750 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
2751 
2752 		/*
2753 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2754 		 * hardware checksum acceleration.
2755 		 */
2756 		ill_capability_send(ill, nmp);
2757 	} else {
2758 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
2759 		    "advertised %x hardware checksum capability flags\n",
2760 		    ill->ill_name, ihck->hcksum_txflags));
2761 	}
2762 }
2763 
2764 static void
2765 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
2766 {
2767 	dl_capab_hcksum_t *hck_subcap;
2768 	dl_capability_sub_t *dl_subcap;
2769 
2770 	if (!ILL_HCKSUM_CAPABLE(ill))
2771 		return;
2772 
2773 	ASSERT(ill->ill_hcksum_capab != NULL);
2774 
2775 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2776 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
2777 	dl_subcap->dl_length = sizeof (*hck_subcap);
2778 
2779 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
2780 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
2781 	hck_subcap->hcksum_txflags = 0;
2782 
2783 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
2784 }
2785 
2786 static void
2787 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2788 {
2789 	mblk_t *nmp = NULL;
2790 	dl_capability_req_t *oc;
2791 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
2792 	ill_zerocopy_capab_t **ill_zerocopy_capab;
2793 	uint_t sub_dl_cap = isub->dl_cap;
2794 	uint8_t *capend;
2795 
2796 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
2797 
2798 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
2799 
2800 	/*
2801 	 * Note: range checks here are not absolutely sufficient to
2802 	 * make us robust against malformed messages sent by drivers;
2803 	 * this is in keeping with the rest of IP's dlpi handling.
2804 	 * (Remember, it's coming from something else in the kernel
2805 	 * address space)
2806 	 */
2807 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2808 	if (capend > mp->b_wptr) {
2809 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2810 		    "malformed sub-capability too long for mblk");
2811 		return;
2812 	}
2813 
2814 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
2815 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
2816 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
2817 		    "unsupported ZEROCOPY sub-capability (version %d, "
2818 		    "expected %d)", zc_ic->zerocopy_version,
2819 		    ZEROCOPY_VERSION_1);
2820 		return;
2821 	}
2822 
2823 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
2824 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
2825 		    "capability isn't as expected; pass-thru module(s) "
2826 		    "detected, discarding capability\n"));
2827 		return;
2828 	}
2829 
2830 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
2831 		if (*ill_zerocopy_capab == NULL) {
2832 			*ill_zerocopy_capab =
2833 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
2834 			    KM_NOSLEEP);
2835 
2836 			if (*ill_zerocopy_capab == NULL) {
2837 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2838 				    "could not enable Zero-copy version %d "
2839 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
2840 				    ill->ill_name);
2841 				return;
2842 			}
2843 		}
2844 
2845 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
2846 		    "supports Zero-copy version %d\n", ill->ill_name,
2847 		    ZEROCOPY_VERSION_1));
2848 
2849 		(*ill_zerocopy_capab)->ill_zerocopy_version =
2850 		    zc_ic->zerocopy_version;
2851 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
2852 		    zc_ic->zerocopy_flags;
2853 
2854 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
2855 	} else {
2856 		uint_t size;
2857 		uchar_t *rptr;
2858 
2859 		size = sizeof (dl_capability_req_t) +
2860 		    sizeof (dl_capability_sub_t) +
2861 		    sizeof (dl_capab_zerocopy_t);
2862 
2863 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2864 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2865 			    "could not enable zerocopy for %s (ENOMEM)\n",
2866 			    ill->ill_name);
2867 			return;
2868 		}
2869 
2870 		rptr = nmp->b_rptr;
2871 		/* initialize dl_capability_req_t */
2872 		oc = (dl_capability_req_t *)rptr;
2873 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2874 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2875 		    sizeof (dl_capab_zerocopy_t);
2876 		rptr += sizeof (dl_capability_req_t);
2877 
2878 		/* initialize dl_capability_sub_t */
2879 		bcopy(isub, rptr, sizeof (*isub));
2880 		rptr += sizeof (*isub);
2881 
2882 		/* initialize dl_capab_zerocopy_t */
2883 		zc_oc = (dl_capab_zerocopy_t *)rptr;
2884 		*zc_oc = *zc_ic;
2885 
2886 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
2887 		    "to enable zero-copy version %d\n", ill->ill_name,
2888 		    ZEROCOPY_VERSION_1));
2889 
2890 		/* set VMSAFE_MEM flag */
2891 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
2892 
2893 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
2894 		ill_capability_send(ill, nmp);
2895 	}
2896 }
2897 
2898 static void
2899 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
2900 {
2901 	dl_capab_zerocopy_t *zerocopy_subcap;
2902 	dl_capability_sub_t *dl_subcap;
2903 
2904 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
2905 		return;
2906 
2907 	ASSERT(ill->ill_zerocopy_capab != NULL);
2908 
2909 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2910 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
2911 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
2912 
2913 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
2914 	zerocopy_subcap->zerocopy_version =
2915 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
2916 	zerocopy_subcap->zerocopy_flags = 0;
2917 
2918 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
2919 }
2920 
2921 /*
2922  * DLD capability
2923  * Refer to dld.h for more information regarding the purpose and usage
2924  * of this capability.
2925  */
2926 static void
2927 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2928 {
2929 	dl_capab_dld_t		*dld_ic, dld;
2930 	uint_t			sub_dl_cap = isub->dl_cap;
2931 	uint8_t			*capend;
2932 	ill_dld_capab_t		*idc;
2933 
2934 	ASSERT(IAM_WRITER_ILL(ill));
2935 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
2936 
2937 	/*
2938 	 * Note: range checks here are not absolutely sufficient to
2939 	 * make us robust against malformed messages sent by drivers;
2940 	 * this is in keeping with the rest of IP's dlpi handling.
2941 	 * (Remember, it's coming from something else in the kernel
2942 	 * address space)
2943 	 */
2944 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2945 	if (capend > mp->b_wptr) {
2946 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
2947 		    "malformed sub-capability too long for mblk");
2948 		return;
2949 	}
2950 	dld_ic = (dl_capab_dld_t *)(isub + 1);
2951 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
2952 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
2953 		    "unsupported DLD sub-capability (version %d, "
2954 		    "expected %d)", dld_ic->dld_version,
2955 		    DLD_CURRENT_VERSION);
2956 		return;
2957 	}
2958 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
2959 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
2960 		    "capability isn't as expected; pass-thru module(s) "
2961 		    "detected, discarding capability\n"));
2962 		return;
2963 	}
2964 
2965 	/*
2966 	 * Copy locally to ensure alignment.
2967 	 */
2968 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
2969 
2970 	if ((idc = ill->ill_dld_capab) == NULL) {
2971 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
2972 		if (idc == NULL) {
2973 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
2974 			    "could not enable DLD version %d "
2975 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
2976 			    ill->ill_name);
2977 			return;
2978 		}
2979 		ill->ill_dld_capab = idc;
2980 	}
2981 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
2982 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
2983 	ip1dbg(("ill_capability_dld_ack: interface %s "
2984 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
2985 
2986 	ill_capability_dld_enable(ill);
2987 }
2988 
2989 /*
2990  * Typically capability negotiation between IP and the driver happens via
2991  * DLPI message exchange. However GLD also offers a direct function call
2992  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
2993  * But arbitrary function calls into IP or GLD are not permitted, since both
2994  * of them are protected by their own perimeter mechanism. The perimeter can
2995  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
2996  * these perimeters is IP -> MAC. Thus for example to enable the squeue
2997  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
2998  * to enter the mac perimeter and then do the direct function calls into
2999  * GLD to enable squeue polling. The ring related callbacks from the mac into
3000  * the stack to add, bind, quiesce, restart or cleanup a ring are all
3001  * protected by the mac perimeter.
3002  */
3003 static void
3004 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
3005 {
3006 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3007 	int			err;
3008 
3009 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
3010 	    DLD_ENABLE);
3011 	ASSERT(err == 0);
3012 }
3013 
3014 static void
3015 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
3016 {
3017 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3018 	int			err;
3019 
3020 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
3021 	    DLD_DISABLE);
3022 	ASSERT(err == 0);
3023 }
3024 
3025 boolean_t
3026 ill_mac_perim_held(ill_t *ill)
3027 {
3028 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3029 
3030 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
3031 	    DLD_QUERY));
3032 }
3033 
3034 static void
3035 ill_capability_direct_enable(ill_t *ill)
3036 {
3037 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3038 	ill_dld_direct_t	*idd = &idc->idc_direct;
3039 	dld_capab_direct_t	direct;
3040 	int			rc;
3041 
3042 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3043 
3044 	bzero(&direct, sizeof (direct));
3045 	direct.di_rx_cf = (uintptr_t)ip_input;
3046 	direct.di_rx_ch = ill;
3047 
3048 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
3049 	    DLD_ENABLE);
3050 	if (rc == 0) {
3051 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
3052 		idd->idd_tx_dh = direct.di_tx_dh;
3053 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
3054 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
3055 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
3056 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
3057 		/*
3058 		 * One time registration of flow enable callback function
3059 		 */
3060 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
3061 		    ill_flow_enable, ill);
3062 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
3063 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
3064 	} else {
3065 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
3066 		    "capability, rc = %d\n", rc);
3067 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
3068 	}
3069 }
3070 
3071 static void
3072 ill_capability_poll_enable(ill_t *ill)
3073 {
3074 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3075 	dld_capab_poll_t	poll;
3076 	int			rc;
3077 
3078 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3079 
3080 	bzero(&poll, sizeof (poll));
3081 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
3082 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
3083 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
3084 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
3085 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
3086 	poll.poll_ring_ch = ill;
3087 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
3088 	    DLD_ENABLE);
3089 	if (rc == 0) {
3090 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
3091 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
3092 	} else {
3093 		ip1dbg(("warning: could not enable POLL "
3094 		    "capability, rc = %d\n", rc));
3095 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
3096 	}
3097 }
3098 
3099 /*
3100  * Enable the LSO capability.
3101  */
3102 static void
3103 ill_capability_lso_enable(ill_t *ill)
3104 {
3105 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
3106 	dld_capab_lso_t	lso;
3107 	int rc;
3108 
3109 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3110 
3111 	if (ill->ill_lso_capab == NULL) {
3112 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3113 		    KM_NOSLEEP);
3114 		if (ill->ill_lso_capab == NULL) {
3115 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
3116 			    "could not enable LSO for %s (ENOMEM)\n",
3117 			    ill->ill_name);
3118 			return;
3119 		}
3120 	}
3121 
3122 	bzero(&lso, sizeof (lso));
3123 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
3124 	    DLD_ENABLE)) == 0) {
3125 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
3126 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
3127 		ill->ill_capabilities |= ILL_CAPAB_DLD_LSO;
3128 		ip1dbg(("ill_capability_lso_enable: interface %s "
3129 		    "has enabled LSO\n ", ill->ill_name));
3130 	} else {
3131 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
3132 		ill->ill_lso_capab = NULL;
3133 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
3134 	}
3135 }
3136 
3137 static void
3138 ill_capability_dld_enable(ill_t *ill)
3139 {
3140 	mac_perim_handle_t mph;
3141 
3142 	ASSERT(IAM_WRITER_ILL(ill));
3143 
3144 	if (ill->ill_isv6)
3145 		return;
3146 
3147 	ill_mac_perim_enter(ill, &mph);
3148 	if (!ill->ill_isv6) {
3149 		ill_capability_direct_enable(ill);
3150 		ill_capability_poll_enable(ill);
3151 		ill_capability_lso_enable(ill);
3152 	}
3153 	ill->ill_capabilities |= ILL_CAPAB_DLD;
3154 	ill_mac_perim_exit(ill, mph);
3155 }
3156 
3157 static void
3158 ill_capability_dld_disable(ill_t *ill)
3159 {
3160 	ill_dld_capab_t	*idc;
3161 	ill_dld_direct_t *idd;
3162 	mac_perim_handle_t	mph;
3163 
3164 	ASSERT(IAM_WRITER_ILL(ill));
3165 
3166 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
3167 		return;
3168 
3169 	ill_mac_perim_enter(ill, &mph);
3170 
3171 	idc = ill->ill_dld_capab;
3172 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
3173 		/*
3174 		 * For performance we avoid locks in the transmit data path
3175 		 * and don't maintain a count of the number of threads using
3176 		 * direct calls. Thus some threads could be using direct
3177 		 * transmit calls to GLD, even after the capability mechanism
3178 		 * turns it off. This is still safe since the handles used in
3179 		 * the direct calls continue to be valid until the unplumb is
3180 		 * completed. Remove the callback that was added (1-time) at
3181 		 * capab enable time.
3182 		 */
3183 		mutex_enter(&ill->ill_lock);
3184 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
3185 		mutex_exit(&ill->ill_lock);
3186 		if (ill->ill_flownotify_mh != NULL) {
3187 			idd = &idc->idc_direct;
3188 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
3189 			    ill->ill_flownotify_mh);
3190 			ill->ill_flownotify_mh = NULL;
3191 		}
3192 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
3193 		    NULL, DLD_DISABLE);
3194 	}
3195 
3196 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
3197 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
3198 		ip_squeue_clean_all(ill);
3199 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
3200 		    NULL, DLD_DISABLE);
3201 	}
3202 
3203 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_LSO) != 0) {
3204 		ASSERT(ill->ill_lso_capab != NULL);
3205 		/*
3206 		 * Clear the capability flag for LSO but retain the
3207 		 * ill_lso_capab structure since it's possible that another
3208 		 * thread is still referring to it.  The structure only gets
3209 		 * deallocated when we destroy the ill.
3210 		 */
3211 
3212 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_LSO;
3213 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
3214 		    NULL, DLD_DISABLE);
3215 	}
3216 
3217 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
3218 	ill_mac_perim_exit(ill, mph);
3219 }
3220 
3221 /*
3222  * Capability Negotiation protocol
3223  *
3224  * We don't wait for DLPI capability operations to finish during interface
3225  * bringup or teardown. Doing so would introduce more asynchrony and the
3226  * interface up/down operations will need multiple return and restarts.
3227  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
3228  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
3229  * exclusive operation won't start until the DLPI operations of the previous
3230  * exclusive operation complete.
3231  *
3232  * The capability state machine is shown below.
3233  *
3234  * state		next state		event, action
3235  *
3236  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
3237  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
3238  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
3239  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
3240  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
3241  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
3242  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
3243  *						    ill_capability_probe.
3244  */
3245 
3246 /*
3247  * Dedicated thread started from ip_stack_init that handles capability
3248  * disable. This thread ensures the taskq dispatch does not fail by waiting
3249  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
3250  * that direct calls to DLD are done in a cv_waitable context.
3251  */
3252 void
3253 ill_taskq_dispatch(ip_stack_t *ipst)
3254 {
3255 	callb_cpr_t cprinfo;
3256 	char 	name[64];
3257 	mblk_t	*mp;
3258 
3259 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
3260 	    ipst->ips_netstack->netstack_stackid);
3261 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
3262 	    name);
3263 	mutex_enter(&ipst->ips_capab_taskq_lock);
3264 
3265 	for (;;) {
3266 		mp = list_head(&ipst->ips_capab_taskq_list);
3267 		while (mp != NULL) {
3268 			list_remove(&ipst->ips_capab_taskq_list, mp);
3269 			mutex_exit(&ipst->ips_capab_taskq_lock);
3270 			VERIFY(taskq_dispatch(system_taskq,
3271 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
3272 			mutex_enter(&ipst->ips_capab_taskq_lock);
3273 			mp = list_head(&ipst->ips_capab_taskq_list);
3274 		}
3275 
3276 		if (ipst->ips_capab_taskq_quit)
3277 			break;
3278 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3279 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
3280 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
3281 	}
3282 	VERIFY(list_head(&ipst->ips_capab_taskq_list) == NULL);
3283 	CALLB_CPR_EXIT(&cprinfo);
3284 	thread_exit();
3285 }
3286 
3287 /*
3288  * Consume a new-style hardware capabilities negotiation ack.
3289  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
3290  */
3291 static void
3292 ill_capability_ack_thr(void *arg)
3293 {
3294 	mblk_t	*mp = arg;
3295 	dl_capability_ack_t *capp;
3296 	dl_capability_sub_t *subp, *endp;
3297 	ill_t	*ill;
3298 	boolean_t reneg;
3299 
3300 	ill = (ill_t *)mp->b_prev;
3301 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
3302 
3303 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
3304 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
3305 		/*
3306 		 * We have received the ack for our DL_CAPAB reset request.
3307 		 * There isnt' anything in the message that needs processing.
3308 		 * All message based capabilities have been disabled, now
3309 		 * do the function call based capability disable.
3310 		 */
3311 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
3312 		ill_capability_dld_disable(ill);
3313 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
3314 		if (reneg)
3315 			ill_capability_probe(ill);
3316 		goto done;
3317 	}
3318 
3319 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
3320 		ill->ill_dlpi_capab_state = IDCS_OK;
3321 
3322 	capp = (dl_capability_ack_t *)mp->b_rptr;
3323 
3324 	if (capp->dl_sub_length == 0) {
3325 		/* no new-style capabilities */
3326 		goto done;
3327 	}
3328 
3329 	/* make sure the driver supplied correct dl_sub_length */
3330 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3331 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3332 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3333 		goto done;
3334 	}
3335 
3336 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3337 	/*
3338 	 * There are sub-capabilities. Process the ones we know about.
3339 	 * Loop until we don't have room for another sub-cap header..
3340 	 */
3341 	for (subp = SC(capp, capp->dl_sub_offset),
3342 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3343 	    subp <= endp;
3344 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3345 
3346 		switch (subp->dl_cap) {
3347 		case DL_CAPAB_ID_WRAPPER:
3348 			ill_capability_id_ack(ill, mp, subp);
3349 			break;
3350 		default:
3351 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3352 			break;
3353 		}
3354 	}
3355 #undef SC
3356 done:
3357 	inet_freemsg(mp);
3358 	ill_capability_done(ill);
3359 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3360 }
3361 
3362 /*
3363  * This needs to be started in a taskq thread to provide a cv_waitable
3364  * context.
3365  */
3366 void
3367 ill_capability_ack(ill_t *ill, mblk_t *mp)
3368 {
3369 	ip_stack_t	*ipst = ill->ill_ipst;
3370 
3371 	mp->b_prev = (mblk_t *)ill;
3372 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3373 	    TQ_NOSLEEP) != 0)
3374 		return;
3375 
3376 	/*
3377 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3378 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3379 	 */
3380 	mutex_enter(&ipst->ips_capab_taskq_lock);
3381 	list_insert_tail(&ipst->ips_capab_taskq_list, mp);
3382 	cv_signal(&ipst->ips_capab_taskq_cv);
3383 	mutex_exit(&ipst->ips_capab_taskq_lock);
3384 }
3385 
3386 /*
3387  * This routine is called to scan the fragmentation reassembly table for
3388  * the specified ILL for any packets that are starting to smell.
3389  * dead_interval is the maximum time in seconds that will be tolerated.  It
3390  * will either be the value specified in ip_g_frag_timeout, or zero if the
3391  * ILL is shutting down and it is time to blow everything off.
3392  *
3393  * It returns the number of seconds (as a time_t) that the next frag timer
3394  * should be scheduled for, 0 meaning that the timer doesn't need to be
3395  * re-started.  Note that the method of calculating next_timeout isn't
3396  * entirely accurate since time will flow between the time we grab
3397  * current_time and the time we schedule the next timeout.  This isn't a
3398  * big problem since this is the timer for sending an ICMP reassembly time
3399  * exceeded messages, and it doesn't have to be exactly accurate.
3400  *
3401  * This function is
3402  * sometimes called as writer, although this is not required.
3403  */
3404 time_t
3405 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3406 {
3407 	ipfb_t	*ipfb;
3408 	ipfb_t	*endp;
3409 	ipf_t	*ipf;
3410 	ipf_t	*ipfnext;
3411 	mblk_t	*mp;
3412 	time_t	current_time = gethrestime_sec();
3413 	time_t	next_timeout = 0;
3414 	uint32_t	hdr_length;
3415 	mblk_t	*send_icmp_head;
3416 	mblk_t	*send_icmp_head_v6;
3417 	zoneid_t zoneid;
3418 	ip_stack_t *ipst = ill->ill_ipst;
3419 
3420 	ipfb = ill->ill_frag_hash_tbl;
3421 	if (ipfb == NULL)
3422 		return (B_FALSE);
3423 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3424 	/* Walk the frag hash table. */
3425 	for (; ipfb < endp; ipfb++) {
3426 		send_icmp_head = NULL;
3427 		send_icmp_head_v6 = NULL;
3428 		mutex_enter(&ipfb->ipfb_lock);
3429 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3430 			time_t frag_time = current_time - ipf->ipf_timestamp;
3431 			time_t frag_timeout;
3432 
3433 			if (frag_time < dead_interval) {
3434 				/*
3435 				 * There are some outstanding fragments
3436 				 * that will timeout later.  Make note of
3437 				 * the time so that we can reschedule the
3438 				 * next timeout appropriately.
3439 				 */
3440 				frag_timeout = dead_interval - frag_time;
3441 				if (next_timeout == 0 ||
3442 				    frag_timeout < next_timeout) {
3443 					next_timeout = frag_timeout;
3444 				}
3445 				break;
3446 			}
3447 			/* Time's up.  Get it out of here. */
3448 			hdr_length = ipf->ipf_nf_hdr_len;
3449 			ipfnext = ipf->ipf_hash_next;
3450 			if (ipfnext)
3451 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3452 			*ipf->ipf_ptphn = ipfnext;
3453 			mp = ipf->ipf_mp->b_cont;
3454 			for (; mp; mp = mp->b_cont) {
3455 				/* Extra points for neatness. */
3456 				IP_REASS_SET_START(mp, 0);
3457 				IP_REASS_SET_END(mp, 0);
3458 			}
3459 			mp = ipf->ipf_mp->b_cont;
3460 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3461 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3462 			ipfb->ipfb_count -= ipf->ipf_count;
3463 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3464 			ipfb->ipfb_frag_pkts--;
3465 			/*
3466 			 * We do not send any icmp message from here because
3467 			 * we currently are holding the ipfb_lock for this
3468 			 * hash chain. If we try and send any icmp messages
3469 			 * from here we may end up via a put back into ip
3470 			 * trying to get the same lock, causing a recursive
3471 			 * mutex panic. Instead we build a list and send all
3472 			 * the icmp messages after we have dropped the lock.
3473 			 */
3474 			if (ill->ill_isv6) {
3475 				if (hdr_length != 0) {
3476 					mp->b_next = send_icmp_head_v6;
3477 					send_icmp_head_v6 = mp;
3478 				} else {
3479 					freemsg(mp);
3480 				}
3481 			} else {
3482 				if (hdr_length != 0) {
3483 					mp->b_next = send_icmp_head;
3484 					send_icmp_head = mp;
3485 				} else {
3486 					freemsg(mp);
3487 				}
3488 			}
3489 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3490 			freeb(ipf->ipf_mp);
3491 		}
3492 		mutex_exit(&ipfb->ipfb_lock);
3493 		/*
3494 		 * Now need to send any icmp messages that we delayed from
3495 		 * above.
3496 		 */
3497 		while (send_icmp_head_v6 != NULL) {
3498 			ip6_t *ip6h;
3499 
3500 			mp = send_icmp_head_v6;
3501 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3502 			mp->b_next = NULL;
3503 			if (mp->b_datap->db_type == M_CTL)
3504 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3505 			else
3506 				ip6h = (ip6_t *)mp->b_rptr;
3507 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3508 			    ill, ipst);
3509 			if (zoneid == ALL_ZONES) {
3510 				freemsg(mp);
3511 			} else {
3512 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3513 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3514 				    B_FALSE, zoneid, ipst);
3515 			}
3516 		}
3517 		while (send_icmp_head != NULL) {
3518 			ipaddr_t dst;
3519 
3520 			mp = send_icmp_head;
3521 			send_icmp_head = send_icmp_head->b_next;
3522 			mp->b_next = NULL;
3523 
3524 			if (mp->b_datap->db_type == M_CTL)
3525 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3526 			else
3527 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3528 
3529 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3530 			if (zoneid == ALL_ZONES) {
3531 				freemsg(mp);
3532 			} else {
3533 				icmp_time_exceeded(ill->ill_wq, mp,
3534 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3535 				    ipst);
3536 			}
3537 		}
3538 	}
3539 	/*
3540 	 * A non-dying ILL will use the return value to decide whether to
3541 	 * restart the frag timer, and for how long.
3542 	 */
3543 	return (next_timeout);
3544 }
3545 
3546 /*
3547  * This routine is called when the approximate count of mblk memory used
3548  * for the specified ILL has exceeded max_count.
3549  */
3550 void
3551 ill_frag_prune(ill_t *ill, uint_t max_count)
3552 {
3553 	ipfb_t	*ipfb;
3554 	ipf_t	*ipf;
3555 	size_t	count;
3556 
3557 	/*
3558 	 * If we are here within ip_min_frag_prune_time msecs remove
3559 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3560 	 * ill_frag_free_num_pkts.
3561 	 */
3562 	mutex_enter(&ill->ill_lock);
3563 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3564 	    (ip_min_frag_prune_time != 0 ?
3565 	    ip_min_frag_prune_time : msec_per_tick)) {
3566 
3567 		ill->ill_frag_free_num_pkts++;
3568 
3569 	} else {
3570 		ill->ill_frag_free_num_pkts = 0;
3571 	}
3572 	ill->ill_last_frag_clean_time = lbolt;
3573 	mutex_exit(&ill->ill_lock);
3574 
3575 	/*
3576 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3577 	 */
3578 	if (ill->ill_frag_free_num_pkts != 0) {
3579 		int ix;
3580 
3581 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3582 			ipfb = &ill->ill_frag_hash_tbl[ix];
3583 			mutex_enter(&ipfb->ipfb_lock);
3584 			if (ipfb->ipfb_ipf != NULL) {
3585 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3586 				    ill->ill_frag_free_num_pkts);
3587 			}
3588 			mutex_exit(&ipfb->ipfb_lock);
3589 		}
3590 	}
3591 	/*
3592 	 * While the reassembly list for this ILL is too big, prune a fragment
3593 	 * queue by age, oldest first.
3594 	 */
3595 	while (ill->ill_frag_count > max_count) {
3596 		int	ix;
3597 		ipfb_t	*oipfb = NULL;
3598 		uint_t	oldest = UINT_MAX;
3599 
3600 		count = 0;
3601 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3602 			ipfb = &ill->ill_frag_hash_tbl[ix];
3603 			mutex_enter(&ipfb->ipfb_lock);
3604 			ipf = ipfb->ipfb_ipf;
3605 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3606 				oldest = ipf->ipf_gen;
3607 				oipfb = ipfb;
3608 			}
3609 			count += ipfb->ipfb_count;
3610 			mutex_exit(&ipfb->ipfb_lock);
3611 		}
3612 		if (oipfb == NULL)
3613 			break;
3614 
3615 		if (count <= max_count)
3616 			return;	/* Somebody beat us to it, nothing to do */
3617 		mutex_enter(&oipfb->ipfb_lock);
3618 		ipf = oipfb->ipfb_ipf;
3619 		if (ipf != NULL) {
3620 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3621 		}
3622 		mutex_exit(&oipfb->ipfb_lock);
3623 	}
3624 }
3625 
3626 /*
3627  * free 'free_cnt' fragmented packets starting at ipf.
3628  */
3629 void
3630 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3631 {
3632 	size_t	count;
3633 	mblk_t	*mp;
3634 	mblk_t	*tmp;
3635 	ipf_t **ipfp = ipf->ipf_ptphn;
3636 
3637 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3638 	ASSERT(ipfp != NULL);
3639 	ASSERT(ipf != NULL);
3640 
3641 	while (ipf != NULL && free_cnt-- > 0) {
3642 		count = ipf->ipf_count;
3643 		mp = ipf->ipf_mp;
3644 		ipf = ipf->ipf_hash_next;
3645 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3646 			IP_REASS_SET_START(tmp, 0);
3647 			IP_REASS_SET_END(tmp, 0);
3648 		}
3649 		atomic_add_32(&ill->ill_frag_count, -count);
3650 		ASSERT(ipfb->ipfb_count >= count);
3651 		ipfb->ipfb_count -= count;
3652 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3653 		ipfb->ipfb_frag_pkts--;
3654 		freemsg(mp);
3655 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3656 	}
3657 
3658 	if (ipf)
3659 		ipf->ipf_ptphn = ipfp;
3660 	ipfp[0] = ipf;
3661 }
3662 
3663 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3664 	"obsolete and may be removed in a future release of Solaris.  Use " \
3665 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3666 
3667 /*
3668  * For obsolete per-interface forwarding configuration;
3669  * called in response to ND_GET.
3670  */
3671 /* ARGSUSED */
3672 static int
3673 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3674 {
3675 	ill_t *ill = (ill_t *)cp;
3676 
3677 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3678 
3679 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3680 	return (0);
3681 }
3682 
3683 /*
3684  * For obsolete per-interface forwarding configuration;
3685  * called in response to ND_SET.
3686  */
3687 /* ARGSUSED */
3688 static int
3689 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3690     cred_t *ioc_cr)
3691 {
3692 	long value;
3693 	int retval;
3694 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3695 
3696 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3697 
3698 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3699 	    value < 0 || value > 1) {
3700 		return (EINVAL);
3701 	}
3702 
3703 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3704 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3705 	rw_exit(&ipst->ips_ill_g_lock);
3706 	return (retval);
3707 }
3708 
3709 /*
3710  * Helper function for ill_forward_set().
3711  */
3712 static void
3713 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
3714 {
3715 	ip_stack_t	*ipst = ill->ill_ipst;
3716 
3717 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3718 
3719 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3720 	    (enable ? "Enabling" : "Disabling"),
3721 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3722 	mutex_enter(&ill->ill_lock);
3723 	if (enable)
3724 		ill->ill_flags |= ILLF_ROUTER;
3725 	else
3726 		ill->ill_flags &= ~ILLF_ROUTER;
3727 	mutex_exit(&ill->ill_lock);
3728 	if (ill->ill_isv6)
3729 		ill_set_nce_router_flags(ill, enable);
3730 	/* Notify routing socket listeners of this change. */
3731 	if (ill->ill_ipif != NULL)
3732 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
3733 }
3734 
3735 /*
3736  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
3737  * socket messages for each interface whose flags we change.
3738  */
3739 int
3740 ill_forward_set(ill_t *ill, boolean_t enable)
3741 {
3742 	ipmp_illgrp_t *illg;
3743 	ip_stack_t *ipst = ill->ill_ipst;
3744 
3745 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3746 
3747 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3748 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3749 		return (0);
3750 
3751 	if (IS_LOOPBACK(ill))
3752 		return (EINVAL);
3753 
3754 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
3755 		/*
3756 		 * Update all of the interfaces in the group.
3757 		 */
3758 		illg = ill->ill_grp;
3759 		ill = list_head(&illg->ig_if);
3760 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
3761 			ill_forward_set_on_ill(ill, enable);
3762 
3763 		/*
3764 		 * Update the IPMP meta-interface.
3765 		 */
3766 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
3767 		return (0);
3768 	}
3769 
3770 	ill_forward_set_on_ill(ill, enable);
3771 	return (0);
3772 }
3773 
3774 /*
3775  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3776  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3777  * set or clear.
3778  */
3779 static void
3780 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3781 {
3782 	ipif_t *ipif;
3783 	nce_t *nce;
3784 
3785 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3786 		/*
3787 		 * NOTE: we match across the illgrp because nce's for
3788 		 * addresses on IPMP interfaces have an nce_ill that points to
3789 		 * the bound underlying ill.
3790 		 */
3791 		nce = ndp_lookup_v6(ill, B_TRUE, &ipif->ipif_v6lcl_addr,
3792 		    B_FALSE);
3793 		if (nce != NULL) {
3794 			mutex_enter(&nce->nce_lock);
3795 			if (enable)
3796 				nce->nce_flags |= NCE_F_ISROUTER;
3797 			else
3798 				nce->nce_flags &= ~NCE_F_ISROUTER;
3799 			mutex_exit(&nce->nce_lock);
3800 			NCE_REFRELE(nce);
3801 		}
3802 	}
3803 }
3804 
3805 /*
3806  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3807  * for this ill.  Make sure the v6/v4 question has been answered about this
3808  * ill.  The creation of this ndd variable is only for backwards compatibility.
3809  * The preferred way to control per-interface IP forwarding is through the
3810  * ILLF_ROUTER interface flag.
3811  */
3812 static int
3813 ill_set_ndd_name(ill_t *ill)
3814 {
3815 	char *suffix;
3816 	ip_stack_t	*ipst = ill->ill_ipst;
3817 
3818 	ASSERT(IAM_WRITER_ILL(ill));
3819 
3820 	if (ill->ill_isv6)
3821 		suffix = ipv6_forward_suffix;
3822 	else
3823 		suffix = ipv4_forward_suffix;
3824 
3825 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3826 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3827 	/*
3828 	 * Copies over the '\0'.
3829 	 * Note that strlen(suffix) is always bounded.
3830 	 */
3831 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3832 	    strlen(suffix) + 1);
3833 
3834 	/*
3835 	 * Use of the nd table requires holding the reader lock.
3836 	 * Modifying the nd table thru nd_load/nd_unload requires
3837 	 * the writer lock.
3838 	 */
3839 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3840 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3841 	    nd_ill_forward_set, (caddr_t)ill)) {
3842 		/*
3843 		 * If the nd_load failed, it only meant that it could not
3844 		 * allocate a new bunch of room for further NDD expansion.
3845 		 * Because of that, the ill_ndd_name will be set to 0, and
3846 		 * this interface is at the mercy of the global ip_forwarding
3847 		 * variable.
3848 		 */
3849 		rw_exit(&ipst->ips_ip_g_nd_lock);
3850 		ill->ill_ndd_name = NULL;
3851 		return (ENOMEM);
3852 	}
3853 	rw_exit(&ipst->ips_ip_g_nd_lock);
3854 	return (0);
3855 }
3856 
3857 /*
3858  * Intializes the context structure and returns the first ill in the list
3859  * cuurently start_list and end_list can have values:
3860  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3861  * IP_V4_G_HEAD		Traverse IPV4 list only.
3862  * IP_V6_G_HEAD		Traverse IPV6 list only.
3863  */
3864 
3865 /*
3866  * We don't check for CONDEMNED ills here. Caller must do that if
3867  * necessary under the ill lock.
3868  */
3869 ill_t *
3870 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3871     ip_stack_t *ipst)
3872 {
3873 	ill_if_t *ifp;
3874 	ill_t *ill;
3875 	avl_tree_t *avl_tree;
3876 
3877 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3878 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3879 
3880 	/*
3881 	 * setup the lists to search
3882 	 */
3883 	if (end_list != MAX_G_HEADS) {
3884 		ctx->ctx_current_list = start_list;
3885 		ctx->ctx_last_list = end_list;
3886 	} else {
3887 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3888 		ctx->ctx_current_list = 0;
3889 	}
3890 
3891 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3892 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3893 		if (ifp != (ill_if_t *)
3894 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3895 			avl_tree = &ifp->illif_avl_by_ppa;
3896 			ill = avl_first(avl_tree);
3897 			/*
3898 			 * ill is guaranteed to be non NULL or ifp should have
3899 			 * not existed.
3900 			 */
3901 			ASSERT(ill != NULL);
3902 			return (ill);
3903 		}
3904 		ctx->ctx_current_list++;
3905 	}
3906 
3907 	return (NULL);
3908 }
3909 
3910 /*
3911  * returns the next ill in the list. ill_first() must have been called
3912  * before calling ill_next() or bad things will happen.
3913  */
3914 
3915 /*
3916  * We don't check for CONDEMNED ills here. Caller must do that if
3917  * necessary under the ill lock.
3918  */
3919 ill_t *
3920 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3921 {
3922 	ill_if_t *ifp;
3923 	ill_t *ill;
3924 	ip_stack_t	*ipst = lastill->ill_ipst;
3925 
3926 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3927 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3928 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3929 	    AVL_AFTER)) != NULL) {
3930 		return (ill);
3931 	}
3932 
3933 	/* goto next ill_ifp in the list. */
3934 	ifp = lastill->ill_ifptr->illif_next;
3935 
3936 	/* make sure not at end of circular list */
3937 	while (ifp ==
3938 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3939 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3940 			return (NULL);
3941 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3942 	}
3943 
3944 	return (avl_first(&ifp->illif_avl_by_ppa));
3945 }
3946 
3947 /*
3948  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
3949  * The final number (PPA) must not have any leading zeros.  Upon success, a
3950  * pointer to the start of the PPA is returned; otherwise NULL is returned.
3951  */
3952 static char *
3953 ill_get_ppa_ptr(char *name)
3954 {
3955 	int namelen = strlen(name);
3956 	int end_ndx = namelen - 1;
3957 	int ppa_ndx, i;
3958 
3959 	/*
3960 	 * Check that the first character is [a-zA-Z], and that the last
3961 	 * character is [0-9].
3962 	 */
3963 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
3964 		return (NULL);
3965 
3966 	/*
3967 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
3968 	 */
3969 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
3970 		if (!isdigit(name[ppa_ndx - 1]))
3971 			break;
3972 
3973 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
3974 		return (NULL);
3975 
3976 	/*
3977 	 * Check that the intermediate characters are [a-z0-9.]
3978 	 */
3979 	for (i = 1; i < ppa_ndx; i++) {
3980 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
3981 		    name[i] != '.' && name[i] != '_') {
3982 			return (NULL);
3983 		}
3984 	}
3985 
3986 	return (name + ppa_ndx);
3987 }
3988 
3989 /*
3990  * use avl tree to locate the ill.
3991  */
3992 static ill_t *
3993 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3994     ipsq_func_t func, int *error, ip_stack_t *ipst)
3995 {
3996 	char *ppa_ptr = NULL;
3997 	int len;
3998 	uint_t ppa;
3999 	ill_t *ill = NULL;
4000 	ill_if_t *ifp;
4001 	int list;
4002 	ipsq_t *ipsq;
4003 
4004 	if (error != NULL)
4005 		*error = 0;
4006 
4007 	/*
4008 	 * get ppa ptr
4009 	 */
4010 	if (isv6)
4011 		list = IP_V6_G_HEAD;
4012 	else
4013 		list = IP_V4_G_HEAD;
4014 
4015 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4016 		if (error != NULL)
4017 			*error = ENXIO;
4018 		return (NULL);
4019 	}
4020 
4021 	len = ppa_ptr - name + 1;
4022 
4023 	ppa = stoi(&ppa_ptr);
4024 
4025 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4026 
4027 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4028 		/*
4029 		 * match is done on len - 1 as the name is not null
4030 		 * terminated it contains ppa in addition to the interface
4031 		 * name.
4032 		 */
4033 		if ((ifp->illif_name_len == len) &&
4034 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4035 			break;
4036 		} else {
4037 			ifp = ifp->illif_next;
4038 		}
4039 	}
4040 
4041 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4042 		/*
4043 		 * Even the interface type does not exist.
4044 		 */
4045 		if (error != NULL)
4046 			*error = ENXIO;
4047 		return (NULL);
4048 	}
4049 
4050 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4051 	if (ill != NULL) {
4052 		/*
4053 		 * The block comment at the start of ipif_down
4054 		 * explains the use of the macros used below
4055 		 */
4056 		GRAB_CONN_LOCK(q);
4057 		mutex_enter(&ill->ill_lock);
4058 		if (ILL_CAN_LOOKUP(ill)) {
4059 			ill_refhold_locked(ill);
4060 			mutex_exit(&ill->ill_lock);
4061 			RELEASE_CONN_LOCK(q);
4062 			return (ill);
4063 		} else if (ILL_CAN_WAIT(ill, q)) {
4064 			ipsq = ill->ill_phyint->phyint_ipsq;
4065 			mutex_enter(&ipsq->ipsq_lock);
4066 			mutex_enter(&ipsq->ipsq_xop->ipx_lock);
4067 			mutex_exit(&ill->ill_lock);
4068 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4069 			mutex_exit(&ipsq->ipsq_xop->ipx_lock);
4070 			mutex_exit(&ipsq->ipsq_lock);
4071 			RELEASE_CONN_LOCK(q);
4072 			if (error != NULL)
4073 				*error = EINPROGRESS;
4074 			return (NULL);
4075 		}
4076 		mutex_exit(&ill->ill_lock);
4077 		RELEASE_CONN_LOCK(q);
4078 	}
4079 	if (error != NULL)
4080 		*error = ENXIO;
4081 	return (NULL);
4082 }
4083 
4084 /*
4085  * comparison function for use with avl.
4086  */
4087 static int
4088 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4089 {
4090 	uint_t ppa;
4091 	uint_t ill_ppa;
4092 
4093 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4094 
4095 	ppa = *((uint_t *)ppa_ptr);
4096 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4097 	/*
4098 	 * We want the ill with the lowest ppa to be on the
4099 	 * top.
4100 	 */
4101 	if (ill_ppa < ppa)
4102 		return (1);
4103 	if (ill_ppa > ppa)
4104 		return (-1);
4105 	return (0);
4106 }
4107 
4108 /*
4109  * remove an interface type from the global list.
4110  */
4111 static void
4112 ill_delete_interface_type(ill_if_t *interface)
4113 {
4114 	ASSERT(interface != NULL);
4115 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4116 
4117 	avl_destroy(&interface->illif_avl_by_ppa);
4118 	if (interface->illif_ppa_arena != NULL)
4119 		vmem_destroy(interface->illif_ppa_arena);
4120 
4121 	remque(interface);
4122 
4123 	mi_free(interface);
4124 }
4125 
4126 /*
4127  * remove ill from the global list.
4128  */
4129 static void
4130 ill_glist_delete(ill_t *ill)
4131 {
4132 	ip_stack_t	*ipst;
4133 	phyint_t	*phyi;
4134 
4135 	if (ill == NULL)
4136 		return;
4137 	ipst = ill->ill_ipst;
4138 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4139 
4140 	/*
4141 	 * If the ill was never inserted into the AVL tree
4142 	 * we skip the if branch.
4143 	 */
4144 	if (ill->ill_ifptr != NULL) {
4145 		/*
4146 		 * remove from AVL tree and free ppa number
4147 		 */
4148 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4149 
4150 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4151 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4152 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4153 		}
4154 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4155 			ill_delete_interface_type(ill->ill_ifptr);
4156 		}
4157 
4158 		/*
4159 		 * Indicate ill is no longer in the list.
4160 		 */
4161 		ill->ill_ifptr = NULL;
4162 		ill->ill_name_length = 0;
4163 		ill->ill_name[0] = '\0';
4164 		ill->ill_ppa = UINT_MAX;
4165 	}
4166 
4167 	/* Generate one last event for this ill. */
4168 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4169 	    ill->ill_name_length);
4170 
4171 	ASSERT(ill->ill_phyint != NULL);
4172 	phyi = ill->ill_phyint;
4173 	ill->ill_phyint = NULL;
4174 
4175 	/*
4176 	 * ill_init allocates a phyint always to store the copy
4177 	 * of flags relevant to phyint. At that point in time, we could
4178 	 * not assign the name and hence phyint_illv4/v6 could not be
4179 	 * initialized. Later in ipif_set_values, we assign the name to
4180 	 * the ill, at which point in time we assign phyint_illv4/v6.
4181 	 * Thus we don't rely on phyint_illv6 to be initialized always.
4182 	 */
4183 	if (ill->ill_flags & ILLF_IPV6)
4184 		phyi->phyint_illv6 = NULL;
4185 	else
4186 		phyi->phyint_illv4 = NULL;
4187 
4188 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
4189 		rw_exit(&ipst->ips_ill_g_lock);
4190 		return;
4191 	}
4192 
4193 	/*
4194 	 * There are no ills left on this phyint; pull it out of the phyint
4195 	 * avl trees, and free it.
4196 	 */
4197 	if (phyi->phyint_ifindex > 0) {
4198 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4199 		    phyi);
4200 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4201 		    phyi);
4202 	}
4203 	rw_exit(&ipst->ips_ill_g_lock);
4204 
4205 	phyint_free(phyi);
4206 }
4207 
4208 /*
4209  * allocate a ppa, if the number of plumbed interfaces of this type are
4210  * less than ill_no_arena do a linear search to find a unused ppa.
4211  * When the number goes beyond ill_no_arena switch to using an arena.
4212  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4213  * is the return value for an error condition, so allocation starts at one
4214  * and is decremented by one.
4215  */
4216 static int
4217 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4218 {
4219 	ill_t *tmp_ill;
4220 	uint_t start, end;
4221 	int ppa;
4222 
4223 	if (ifp->illif_ppa_arena == NULL &&
4224 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4225 		/*
4226 		 * Create an arena.
4227 		 */
4228 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4229 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4230 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4231 			/* allocate what has already been assigned */
4232 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4233 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4234 		    tmp_ill, AVL_AFTER)) {
4235 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4236 			    1,		/* size */
4237 			    1,		/* align/quantum */
4238 			    0,		/* phase */
4239 			    0,		/* nocross */
4240 			    /* minaddr */
4241 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4242 			    /* maxaddr */
4243 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4244 			    VM_NOSLEEP|VM_FIRSTFIT);
4245 			if (ppa == 0) {
4246 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4247 				    " failed while switching"));
4248 				vmem_destroy(ifp->illif_ppa_arena);
4249 				ifp->illif_ppa_arena = NULL;
4250 				break;
4251 			}
4252 		}
4253 	}
4254 
4255 	if (ifp->illif_ppa_arena != NULL) {
4256 		if (ill->ill_ppa == UINT_MAX) {
4257 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4258 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4259 			if (ppa == 0)
4260 				return (EAGAIN);
4261 			ill->ill_ppa = --ppa;
4262 		} else {
4263 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4264 			    1, 		/* size */
4265 			    1, 		/* align/quantum */
4266 			    0, 		/* phase */
4267 			    0, 		/* nocross */
4268 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4269 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4270 			    VM_NOSLEEP|VM_FIRSTFIT);
4271 			/*
4272 			 * Most likely the allocation failed because
4273 			 * the requested ppa was in use.
4274 			 */
4275 			if (ppa == 0)
4276 				return (EEXIST);
4277 		}
4278 		return (0);
4279 	}
4280 
4281 	/*
4282 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4283 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4284 	 */
4285 	if (ill->ill_ppa == UINT_MAX) {
4286 		end = UINT_MAX - 1;
4287 		start = 0;
4288 	} else {
4289 		end = start = ill->ill_ppa;
4290 	}
4291 
4292 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4293 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4294 		if (start++ >= end) {
4295 			if (ill->ill_ppa == UINT_MAX)
4296 				return (EAGAIN);
4297 			else
4298 				return (EEXIST);
4299 		}
4300 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4301 	}
4302 	ill->ill_ppa = start;
4303 	return (0);
4304 }
4305 
4306 /*
4307  * Insert ill into the list of configured ill's. Once this function completes,
4308  * the ill is globally visible and is available through lookups. More precisely
4309  * this happens after the caller drops the ill_g_lock.
4310  */
4311 static int
4312 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4313 {
4314 	ill_if_t *ill_interface;
4315 	avl_index_t where = 0;
4316 	int error;
4317 	int name_length;
4318 	int index;
4319 	boolean_t check_length = B_FALSE;
4320 	ip_stack_t	*ipst = ill->ill_ipst;
4321 
4322 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4323 
4324 	name_length = mi_strlen(name) + 1;
4325 
4326 	if (isv6)
4327 		index = IP_V6_G_HEAD;
4328 	else
4329 		index = IP_V4_G_HEAD;
4330 
4331 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4332 	/*
4333 	 * Search for interface type based on name
4334 	 */
4335 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4336 		if ((ill_interface->illif_name_len == name_length) &&
4337 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4338 			break;
4339 		}
4340 		ill_interface = ill_interface->illif_next;
4341 	}
4342 
4343 	/*
4344 	 * Interface type not found, create one.
4345 	 */
4346 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4347 		ill_g_head_t ghead;
4348 
4349 		/*
4350 		 * allocate ill_if_t structure
4351 		 */
4352 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4353 		if (ill_interface == NULL) {
4354 			return (ENOMEM);
4355 		}
4356 
4357 		(void) strcpy(ill_interface->illif_name, name);
4358 		ill_interface->illif_name_len = name_length;
4359 
4360 		avl_create(&ill_interface->illif_avl_by_ppa,
4361 		    ill_compare_ppa, sizeof (ill_t),
4362 		    offsetof(struct ill_s, ill_avl_byppa));
4363 
4364 		/*
4365 		 * link the structure in the back to maintain order
4366 		 * of configuration for ifconfig output.
4367 		 */
4368 		ghead = ipst->ips_ill_g_heads[index];
4369 		insque(ill_interface, ghead.ill_g_list_tail);
4370 	}
4371 
4372 	if (ill->ill_ppa == UINT_MAX)
4373 		check_length = B_TRUE;
4374 
4375 	error = ill_alloc_ppa(ill_interface, ill);
4376 	if (error != 0) {
4377 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4378 			ill_delete_interface_type(ill->ill_ifptr);
4379 		return (error);
4380 	}
4381 
4382 	/*
4383 	 * When the ppa is choosen by the system, check that there is
4384 	 * enough space to insert ppa. if a specific ppa was passed in this
4385 	 * check is not required as the interface name passed in will have
4386 	 * the right ppa in it.
4387 	 */
4388 	if (check_length) {
4389 		/*
4390 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4391 		 */
4392 		char buf[sizeof (uint_t) * 3];
4393 
4394 		/*
4395 		 * convert ppa to string to calculate the amount of space
4396 		 * required for it in the name.
4397 		 */
4398 		numtos(ill->ill_ppa, buf);
4399 
4400 		/* Do we have enough space to insert ppa ? */
4401 
4402 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4403 			/* Free ppa and interface type struct */
4404 			if (ill_interface->illif_ppa_arena != NULL) {
4405 				vmem_free(ill_interface->illif_ppa_arena,
4406 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4407 			}
4408 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4409 				ill_delete_interface_type(ill->ill_ifptr);
4410 
4411 			return (EINVAL);
4412 		}
4413 	}
4414 
4415 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4416 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4417 
4418 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4419 	    &where);
4420 	ill->ill_ifptr = ill_interface;
4421 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4422 
4423 	ill_phyint_reinit(ill);
4424 	return (0);
4425 }
4426 
4427 /* Initialize the per phyint ipsq used for serialization */
4428 static boolean_t
4429 ipsq_init(ill_t *ill, boolean_t enter)
4430 {
4431 	ipsq_t  *ipsq;
4432 	ipxop_t	*ipx;
4433 
4434 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
4435 		return (B_FALSE);
4436 
4437 	ill->ill_phyint->phyint_ipsq = ipsq;
4438 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
4439 	ipx->ipx_ipsq = ipsq;
4440 	ipsq->ipsq_next = ipsq;
4441 	ipsq->ipsq_phyint = ill->ill_phyint;
4442 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4443 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
4444 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4445 	if (enter) {
4446 		ipx->ipx_writer = curthread;
4447 		ipx->ipx_forced = B_FALSE;
4448 		ipx->ipx_reentry_cnt = 1;
4449 #ifdef DEBUG
4450 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
4451 #endif
4452 	}
4453 	return (B_TRUE);
4454 }
4455 
4456 /*
4457  * ill_init is called by ip_open when a device control stream is opened.
4458  * It does a few initializations, and shoots a DL_INFO_REQ message down
4459  * to the driver.  The response is later picked up in ip_rput_dlpi and
4460  * used to set up default mechanisms for talking to the driver.  (Always
4461  * called as writer.)
4462  *
4463  * If this function returns error, ip_open will call ip_close which in
4464  * turn will call ill_delete to clean up any memory allocated here that
4465  * is not yet freed.
4466  */
4467 int
4468 ill_init(queue_t *q, ill_t *ill)
4469 {
4470 	int	count;
4471 	dl_info_req_t	*dlir;
4472 	mblk_t	*info_mp;
4473 	uchar_t *frag_ptr;
4474 
4475 	/*
4476 	 * The ill is initialized to zero by mi_alloc*(). In addition
4477 	 * some fields already contain valid values, initialized in
4478 	 * ip_open(), before we reach here.
4479 	 */
4480 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4481 
4482 	ill->ill_rq = q;
4483 	ill->ill_wq = WR(q);
4484 
4485 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4486 	    BPRI_HI);
4487 	if (info_mp == NULL)
4488 		return (ENOMEM);
4489 
4490 	/*
4491 	 * Allocate sufficient space to contain our fragment hash table and
4492 	 * the device name.
4493 	 */
4494 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4495 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4496 	if (frag_ptr == NULL) {
4497 		freemsg(info_mp);
4498 		return (ENOMEM);
4499 	}
4500 	ill->ill_frag_ptr = frag_ptr;
4501 	ill->ill_frag_free_num_pkts = 0;
4502 	ill->ill_last_frag_clean_time = 0;
4503 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4504 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4505 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4506 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4507 		    NULL, MUTEX_DEFAULT, NULL);
4508 	}
4509 
4510 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4511 	if (ill->ill_phyint == NULL) {
4512 		freemsg(info_mp);
4513 		mi_free(frag_ptr);
4514 		return (ENOMEM);
4515 	}
4516 
4517 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4518 	/*
4519 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4520 	 * at this point because of the following reason. If we can't
4521 	 * enter the ipsq at some point and cv_wait, the writer that
4522 	 * wakes us up tries to locate us using the list of all phyints
4523 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4524 	 * If we don't set it now, we risk a missed wakeup.
4525 	 */
4526 	ill->ill_phyint->phyint_illv4 = ill;
4527 	ill->ill_ppa = UINT_MAX;
4528 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4529 
4530 	if (!ipsq_init(ill, B_TRUE)) {
4531 		freemsg(info_mp);
4532 		mi_free(frag_ptr);
4533 		mi_free(ill->ill_phyint);
4534 		return (ENOMEM);
4535 	}
4536 
4537 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4538 
4539 	/* Frag queue limit stuff */
4540 	ill->ill_frag_count = 0;
4541 	ill->ill_ipf_gen = 0;
4542 
4543 	ill->ill_global_timer = INFINITY;
4544 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4545 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4546 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4547 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4548 
4549 	/*
4550 	 * Initialize IPv6 configuration variables.  The IP module is always
4551 	 * opened as an IPv4 module.  Instead tracking down the cases where
4552 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4553 	 * here for convenience, this has no effect until the ill is set to do
4554 	 * IPv6.
4555 	 */
4556 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4557 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4558 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4559 	ill->ill_max_buf = ND_MAX_Q;
4560 	ill->ill_refcnt = 0;
4561 
4562 	/* Send down the Info Request to the driver. */
4563 	info_mp->b_datap->db_type = M_PCPROTO;
4564 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4565 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4566 	dlir->dl_primitive = DL_INFO_REQ;
4567 
4568 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4569 
4570 	qprocson(q);
4571 	ill_dlpi_send(ill, info_mp);
4572 
4573 	return (0);
4574 }
4575 
4576 /*
4577  * ill_dls_info
4578  * creates datalink socket info from the device.
4579  */
4580 int
4581 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4582 {
4583 	size_t	len;
4584 	ill_t	*ill = ipif->ipif_ill;
4585 
4586 	sdl->sdl_family = AF_LINK;
4587 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4588 	sdl->sdl_type = ill->ill_type;
4589 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4590 	len = strlen(sdl->sdl_data);
4591 	ASSERT(len < 256);
4592 	sdl->sdl_nlen = (uchar_t)len;
4593 	sdl->sdl_alen = ill->ill_phys_addr_length;
4594 	sdl->sdl_slen = 0;
4595 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4596 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4597 
4598 	return (sizeof (struct sockaddr_dl));
4599 }
4600 
4601 /*
4602  * ill_xarp_info
4603  * creates xarp info from the device.
4604  */
4605 static int
4606 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4607 {
4608 	sdl->sdl_family = AF_LINK;
4609 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4610 	sdl->sdl_type = ill->ill_type;
4611 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4612 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4613 	sdl->sdl_alen = ill->ill_phys_addr_length;
4614 	sdl->sdl_slen = 0;
4615 	return (sdl->sdl_nlen);
4616 }
4617 
4618 static int
4619 loopback_kstat_update(kstat_t *ksp, int rw)
4620 {
4621 	kstat_named_t *kn;
4622 	netstackid_t	stackid;
4623 	netstack_t	*ns;
4624 	ip_stack_t	*ipst;
4625 
4626 	if (ksp == NULL || ksp->ks_data == NULL)
4627 		return (EIO);
4628 
4629 	if (rw == KSTAT_WRITE)
4630 		return (EACCES);
4631 
4632 	kn = KSTAT_NAMED_PTR(ksp);
4633 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4634 
4635 	ns = netstack_find_by_stackid(stackid);
4636 	if (ns == NULL)
4637 		return (-1);
4638 
4639 	ipst = ns->netstack_ip;
4640 	if (ipst == NULL) {
4641 		netstack_rele(ns);
4642 		return (-1);
4643 	}
4644 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4645 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4646 	netstack_rele(ns);
4647 	return (0);
4648 }
4649 
4650 /*
4651  * Has ifindex been plumbed already?
4652  */
4653 boolean_t
4654 phyint_exists(uint_t index, ip_stack_t *ipst)
4655 {
4656 	ASSERT(index != 0);
4657 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4658 
4659 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4660 	    &index, NULL) != NULL);
4661 }
4662 
4663 /* Pick a unique ifindex */
4664 boolean_t
4665 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4666 {
4667 	uint_t starting_index;
4668 
4669 	if (!ipst->ips_ill_index_wrap) {
4670 		*indexp = ipst->ips_ill_index++;
4671 		if (ipst->ips_ill_index == 0) {
4672 			/* Reached the uint_t limit Next time wrap  */
4673 			ipst->ips_ill_index_wrap = B_TRUE;
4674 		}
4675 		return (B_TRUE);
4676 	}
4677 
4678 	/*
4679 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4680 	 * at this point and don't want to call any function that attempts
4681 	 * to get the lock again.
4682 	 */
4683 	starting_index = ipst->ips_ill_index++;
4684 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4685 		if (ipst->ips_ill_index != 0 &&
4686 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4687 			/* found unused index - use it */
4688 			*indexp = ipst->ips_ill_index;
4689 			return (B_TRUE);
4690 		}
4691 	}
4692 
4693 	/*
4694 	 * all interface indicies are inuse.
4695 	 */
4696 	return (B_FALSE);
4697 }
4698 
4699 /*
4700  * Assign a unique interface index for the phyint.
4701  */
4702 static boolean_t
4703 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4704 {
4705 	ASSERT(phyi->phyint_ifindex == 0);
4706 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4707 }
4708 
4709 /*
4710  * Initialize the flags on `phyi' as per the provided mactype.
4711  */
4712 static void
4713 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
4714 {
4715 	uint64_t flags = 0;
4716 
4717 	/*
4718 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
4719 	 * we always presume the underlying hardware is working and set
4720 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
4721 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
4722 	 * there are no active interfaces in the group so we set PHYI_FAILED.
4723 	 */
4724 	if (mactype == SUNW_DL_IPMP)
4725 		flags |= PHYI_FAILED;
4726 	else
4727 		flags |= PHYI_RUNNING;
4728 
4729 	switch (mactype) {
4730 	case SUNW_DL_VNI:
4731 		flags |= PHYI_VIRTUAL;
4732 		break;
4733 	case SUNW_DL_IPMP:
4734 		flags |= PHYI_IPMP;
4735 		break;
4736 	case DL_LOOP:
4737 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
4738 		break;
4739 	}
4740 
4741 	mutex_enter(&phyi->phyint_lock);
4742 	phyi->phyint_flags |= flags;
4743 	mutex_exit(&phyi->phyint_lock);
4744 }
4745 
4746 /*
4747  * Return a pointer to the ill which matches the supplied name.  Note that
4748  * the ill name length includes the null termination character.  (May be
4749  * called as writer.)
4750  * If do_alloc and the interface is "lo0" it will be automatically created.
4751  * Cannot bump up reference on condemned ills. So dup detect can't be done
4752  * using this func.
4753  */
4754 ill_t *
4755 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4756     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4757     ip_stack_t *ipst)
4758 {
4759 	ill_t	*ill;
4760 	ipif_t	*ipif;
4761 	ipsq_t	*ipsq;
4762 	kstat_named_t	*kn;
4763 	boolean_t isloopback;
4764 	in6_addr_t ov6addr;
4765 
4766 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4767 
4768 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4769 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4770 	rw_exit(&ipst->ips_ill_g_lock);
4771 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4772 		return (ill);
4773 
4774 	/*
4775 	 * Couldn't find it.  Does this happen to be a lookup for the
4776 	 * loopback device and are we allowed to allocate it?
4777 	 */
4778 	if (!isloopback || !do_alloc)
4779 		return (NULL);
4780 
4781 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4782 
4783 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4784 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4785 		rw_exit(&ipst->ips_ill_g_lock);
4786 		return (ill);
4787 	}
4788 
4789 	/* Create the loopback device on demand */
4790 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4791 	    sizeof (ipif_loopback_name), BPRI_MED));
4792 	if (ill == NULL)
4793 		goto done;
4794 
4795 	*ill = ill_null;
4796 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4797 	ill->ill_ipst = ipst;
4798 	netstack_hold(ipst->ips_netstack);
4799 	/*
4800 	 * For exclusive stacks we set the zoneid to zero
4801 	 * to make IP operate as if in the global zone.
4802 	 */
4803 	ill->ill_zoneid = GLOBAL_ZONEID;
4804 
4805 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4806 	if (ill->ill_phyint == NULL)
4807 		goto done;
4808 
4809 	if (isv6)
4810 		ill->ill_phyint->phyint_illv6 = ill;
4811 	else
4812 		ill->ill_phyint->phyint_illv4 = ill;
4813 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4814 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
4815 
4816 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4817 	/* Add room for tcp+ip headers */
4818 	if (isv6) {
4819 		ill->ill_isv6 = B_TRUE;
4820 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4821 	} else {
4822 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4823 	}
4824 	if (!ill_allocate_mibs(ill))
4825 		goto done;
4826 	ill->ill_max_mtu = ill->ill_max_frag;
4827 	/*
4828 	 * ipif_loopback_name can't be pointed at directly because its used
4829 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4830 	 * from the glist, ill_glist_delete() sets the first character of
4831 	 * ill_name to '\0'.
4832 	 */
4833 	ill->ill_name = (char *)ill + sizeof (*ill);
4834 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4835 	ill->ill_name_length = sizeof (ipif_loopback_name);
4836 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4837 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4838 
4839 	ill->ill_global_timer = INFINITY;
4840 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4841 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4842 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4843 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4844 
4845 	/* No resolver here. */
4846 	ill->ill_net_type = IRE_LOOPBACK;
4847 
4848 	/* Initialize the ipsq */
4849 	if (!ipsq_init(ill, B_FALSE))
4850 		goto done;
4851 
4852 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4853 	if (ipif == NULL)
4854 		goto done;
4855 
4856 	ill->ill_flags = ILLF_MULTICAST;
4857 
4858 	ov6addr = ipif->ipif_v6lcl_addr;
4859 	/* Set up default loopback address and mask. */
4860 	if (!isv6) {
4861 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4862 
4863 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4864 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4865 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4866 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4867 		    ipif->ipif_v6subnet);
4868 		ill->ill_flags |= ILLF_IPV4;
4869 	} else {
4870 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4871 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4872 		ipif->ipif_v6net_mask = ipv6_all_ones;
4873 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4874 		    ipif->ipif_v6subnet);
4875 		ill->ill_flags |= ILLF_IPV6;
4876 	}
4877 
4878 	/*
4879 	 * Chain us in at the end of the ill list. hold the ill
4880 	 * before we make it globally visible. 1 for the lookup.
4881 	 */
4882 	ill->ill_refcnt = 0;
4883 	ill_refhold(ill);
4884 
4885 	ill->ill_frag_count = 0;
4886 	ill->ill_frag_free_num_pkts = 0;
4887 	ill->ill_last_frag_clean_time = 0;
4888 
4889 	ipsq = ill->ill_phyint->phyint_ipsq;
4890 
4891 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4892 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4893 
4894 	/* Let SCTP know so that it can add this to its list */
4895 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4896 
4897 	/*
4898 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4899 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4900 	 * requires to be after ill_glist_insert() since we need the
4901 	 * ill_index set. Pass on ipv6_loopback as the old address.
4902 	 */
4903 	sctp_update_ipif_addr(ipif, ov6addr);
4904 
4905 	/*
4906 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4907 	 * If so, free our original one.
4908 	 */
4909 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4910 		ipsq_delete(ipsq);
4911 
4912 	if (ipst->ips_loopback_ksp == NULL) {
4913 		/* Export loopback interface statistics */
4914 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4915 		    ipif_loopback_name, "net",
4916 		    KSTAT_TYPE_NAMED, 2, 0,
4917 		    ipst->ips_netstack->netstack_stackid);
4918 		if (ipst->ips_loopback_ksp != NULL) {
4919 			ipst->ips_loopback_ksp->ks_update =
4920 			    loopback_kstat_update;
4921 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4922 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4923 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4924 			ipst->ips_loopback_ksp->ks_private =
4925 			    (void *)(uintptr_t)ipst->ips_netstack->
4926 			    netstack_stackid;
4927 			kstat_install(ipst->ips_loopback_ksp);
4928 		}
4929 	}
4930 
4931 	if (error != NULL)
4932 		*error = 0;
4933 	*did_alloc = B_TRUE;
4934 	rw_exit(&ipst->ips_ill_g_lock);
4935 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4936 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4937 	return (ill);
4938 done:
4939 	if (ill != NULL) {
4940 		if (ill->ill_phyint != NULL) {
4941 			ipsq = ill->ill_phyint->phyint_ipsq;
4942 			if (ipsq != NULL) {
4943 				ipsq->ipsq_phyint = NULL;
4944 				ipsq_delete(ipsq);
4945 			}
4946 			mi_free(ill->ill_phyint);
4947 		}
4948 		ill_free_mib(ill);
4949 		if (ill->ill_ipst != NULL)
4950 			netstack_rele(ill->ill_ipst->ips_netstack);
4951 		mi_free(ill);
4952 	}
4953 	rw_exit(&ipst->ips_ill_g_lock);
4954 	if (error != NULL)
4955 		*error = ENOMEM;
4956 	return (NULL);
4957 }
4958 
4959 /*
4960  * For IPP calls - use the ip_stack_t for global stack.
4961  */
4962 ill_t *
4963 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4964     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4965 {
4966 	ip_stack_t	*ipst;
4967 	ill_t		*ill;
4968 
4969 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4970 	if (ipst == NULL) {
4971 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4972 		return (NULL);
4973 	}
4974 
4975 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4976 	netstack_rele(ipst->ips_netstack);
4977 	return (ill);
4978 }
4979 
4980 /*
4981  * Return a pointer to the ill which matches the index and IP version type.
4982  */
4983 ill_t *
4984 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4985     ipsq_func_t func, int *err, ip_stack_t *ipst)
4986 {
4987 	ill_t	*ill;
4988 	ipsq_t  *ipsq;
4989 	phyint_t *phyi;
4990 
4991 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4992 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4993 
4994 	if (err != NULL)
4995 		*err = 0;
4996 
4997 	/*
4998 	 * Indexes are stored in the phyint - a common structure
4999 	 * to both IPv4 and IPv6.
5000 	 */
5001 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5002 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5003 	    (void *) &index, NULL);
5004 	if (phyi != NULL) {
5005 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5006 		if (ill != NULL) {
5007 			/*
5008 			 * The block comment at the start of ipif_down
5009 			 * explains the use of the macros used below
5010 			 */
5011 			GRAB_CONN_LOCK(q);
5012 			mutex_enter(&ill->ill_lock);
5013 			if (ILL_CAN_LOOKUP(ill)) {
5014 				ill_refhold_locked(ill);
5015 				mutex_exit(&ill->ill_lock);
5016 				RELEASE_CONN_LOCK(q);
5017 				rw_exit(&ipst->ips_ill_g_lock);
5018 				return (ill);
5019 			} else if (ILL_CAN_WAIT(ill, q)) {
5020 				ipsq = ill->ill_phyint->phyint_ipsq;
5021 				mutex_enter(&ipsq->ipsq_lock);
5022 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5023 				rw_exit(&ipst->ips_ill_g_lock);
5024 				mutex_exit(&ill->ill_lock);
5025 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5026 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5027 				mutex_exit(&ipsq->ipsq_lock);
5028 				RELEASE_CONN_LOCK(q);
5029 				if (err != NULL)
5030 					*err = EINPROGRESS;
5031 				return (NULL);
5032 			}
5033 			RELEASE_CONN_LOCK(q);
5034 			mutex_exit(&ill->ill_lock);
5035 		}
5036 	}
5037 	rw_exit(&ipst->ips_ill_g_lock);
5038 	if (err != NULL)
5039 		*err = ENXIO;
5040 	return (NULL);
5041 }
5042 
5043 /*
5044  * Return the ifindex next in sequence after the passed in ifindex.
5045  * If there is no next ifindex for the given protocol, return 0.
5046  */
5047 uint_t
5048 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5049 {
5050 	phyint_t *phyi;
5051 	phyint_t *phyi_initial;
5052 	uint_t   ifindex;
5053 
5054 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5055 
5056 	if (index == 0) {
5057 		phyi = avl_first(
5058 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5059 	} else {
5060 		phyi = phyi_initial = avl_find(
5061 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5062 		    (void *) &index, NULL);
5063 	}
5064 
5065 	for (; phyi != NULL;
5066 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5067 	    phyi, AVL_AFTER)) {
5068 		/*
5069 		 * If we're not returning the first interface in the tree
5070 		 * and we still haven't moved past the phyint_t that
5071 		 * corresponds to index, avl_walk needs to be called again
5072 		 */
5073 		if (!((index != 0) && (phyi == phyi_initial))) {
5074 			if (isv6) {
5075 				if ((phyi->phyint_illv6) &&
5076 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5077 				    (phyi->phyint_illv6->ill_isv6 == 1))
5078 					break;
5079 			} else {
5080 				if ((phyi->phyint_illv4) &&
5081 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5082 				    (phyi->phyint_illv4->ill_isv6 == 0))
5083 					break;
5084 			}
5085 		}
5086 	}
5087 
5088 	rw_exit(&ipst->ips_ill_g_lock);
5089 
5090 	if (phyi != NULL)
5091 		ifindex = phyi->phyint_ifindex;
5092 	else
5093 		ifindex = 0;
5094 
5095 	return (ifindex);
5096 }
5097 
5098 /*
5099  * Return the ifindex for the named interface.
5100  * If there is no next ifindex for the interface, return 0.
5101  */
5102 uint_t
5103 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5104 {
5105 	phyint_t	*phyi;
5106 	avl_index_t	where = 0;
5107 	uint_t		ifindex;
5108 
5109 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5110 
5111 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5112 	    name, &where)) == NULL) {
5113 		rw_exit(&ipst->ips_ill_g_lock);
5114 		return (0);
5115 	}
5116 
5117 	ifindex = phyi->phyint_ifindex;
5118 
5119 	rw_exit(&ipst->ips_ill_g_lock);
5120 
5121 	return (ifindex);
5122 }
5123 
5124 /*
5125  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5126  * that gives a running thread a reference to the ill. This reference must be
5127  * released by the thread when it is done accessing the ill and related
5128  * objects. ill_refcnt can not be used to account for static references
5129  * such as other structures pointing to an ill. Callers must generally
5130  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5131  * or be sure that the ill is not being deleted or changing state before
5132  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5133  * ill won't change any of its critical state such as address, netmask etc.
5134  */
5135 void
5136 ill_refhold(ill_t *ill)
5137 {
5138 	mutex_enter(&ill->ill_lock);
5139 	ill->ill_refcnt++;
5140 	ILL_TRACE_REF(ill);
5141 	mutex_exit(&ill->ill_lock);
5142 }
5143 
5144 void
5145 ill_refhold_locked(ill_t *ill)
5146 {
5147 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5148 	ill->ill_refcnt++;
5149 	ILL_TRACE_REF(ill);
5150 }
5151 
5152 int
5153 ill_check_and_refhold(ill_t *ill)
5154 {
5155 	mutex_enter(&ill->ill_lock);
5156 	if (ILL_CAN_LOOKUP(ill)) {
5157 		ill_refhold_locked(ill);
5158 		mutex_exit(&ill->ill_lock);
5159 		return (0);
5160 	}
5161 	mutex_exit(&ill->ill_lock);
5162 	return (ILL_LOOKUP_FAILED);
5163 }
5164 
5165 /*
5166  * Must not be called while holding any locks. Otherwise if this is
5167  * the last reference to be released, there is a chance of recursive mutex
5168  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5169  * to restart an ioctl.
5170  */
5171 void
5172 ill_refrele(ill_t *ill)
5173 {
5174 	mutex_enter(&ill->ill_lock);
5175 	ASSERT(ill->ill_refcnt != 0);
5176 	ill->ill_refcnt--;
5177 	ILL_UNTRACE_REF(ill);
5178 	if (ill->ill_refcnt != 0) {
5179 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5180 		mutex_exit(&ill->ill_lock);
5181 		return;
5182 	}
5183 
5184 	/* Drops the ill_lock */
5185 	ipif_ill_refrele_tail(ill);
5186 }
5187 
5188 /*
5189  * Obtain a weak reference count on the ill. This reference ensures the
5190  * ill won't be freed, but the ill may change any of its critical state
5191  * such as netmask, address etc. Returns an error if the ill has started
5192  * closing.
5193  */
5194 boolean_t
5195 ill_waiter_inc(ill_t *ill)
5196 {
5197 	mutex_enter(&ill->ill_lock);
5198 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5199 		mutex_exit(&ill->ill_lock);
5200 		return (B_FALSE);
5201 	}
5202 	ill->ill_waiters++;
5203 	mutex_exit(&ill->ill_lock);
5204 	return (B_TRUE);
5205 }
5206 
5207 void
5208 ill_waiter_dcr(ill_t *ill)
5209 {
5210 	mutex_enter(&ill->ill_lock);
5211 	ill->ill_waiters--;
5212 	if (ill->ill_waiters == 0)
5213 		cv_broadcast(&ill->ill_cv);
5214 	mutex_exit(&ill->ill_lock);
5215 }
5216 
5217 /*
5218  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5219  * driver.  We construct best guess defaults for lower level information that
5220  * we need.  If an interface is brought up without injection of any overriding
5221  * information from outside, we have to be ready to go with these defaults.
5222  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5223  * we primarely want the dl_provider_style.
5224  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5225  * at which point we assume the other part of the information is valid.
5226  */
5227 void
5228 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5229 {
5230 	uchar_t		*brdcst_addr;
5231 	uint_t		brdcst_addr_length, phys_addr_length;
5232 	t_scalar_t	sap_length;
5233 	dl_info_ack_t	*dlia;
5234 	ip_m_t		*ipm;
5235 	dl_qos_cl_sel1_t *sel1;
5236 	int		min_mtu;
5237 
5238 	ASSERT(IAM_WRITER_ILL(ill));
5239 
5240 	/*
5241 	 * Till the ill is fully up ILL_CHANGING will be set and
5242 	 * the ill is not globally visible. So no need for a lock.
5243 	 */
5244 	dlia = (dl_info_ack_t *)mp->b_rptr;
5245 	ill->ill_mactype = dlia->dl_mac_type;
5246 
5247 	ipm = ip_m_lookup(dlia->dl_mac_type);
5248 	if (ipm == NULL) {
5249 		ipm = ip_m_lookup(DL_OTHER);
5250 		ASSERT(ipm != NULL);
5251 	}
5252 	ill->ill_media = ipm;
5253 
5254 	/*
5255 	 * When the new DLPI stuff is ready we'll pull lengths
5256 	 * from dlia.
5257 	 */
5258 	if (dlia->dl_version == DL_VERSION_2) {
5259 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5260 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5261 		    brdcst_addr_length);
5262 		if (brdcst_addr == NULL) {
5263 			brdcst_addr_length = 0;
5264 		}
5265 		sap_length = dlia->dl_sap_length;
5266 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5267 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5268 		    brdcst_addr_length, sap_length, phys_addr_length));
5269 	} else {
5270 		brdcst_addr_length = 6;
5271 		brdcst_addr = ip_six_byte_all_ones;
5272 		sap_length = -2;
5273 		phys_addr_length = brdcst_addr_length;
5274 	}
5275 
5276 	ill->ill_bcast_addr_length = brdcst_addr_length;
5277 	ill->ill_phys_addr_length = phys_addr_length;
5278 	ill->ill_sap_length = sap_length;
5279 
5280 	/*
5281 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5282 	 * but we must ensure a minimum IP MTU is used since other bits of
5283 	 * IP will fly apart otherwise.
5284 	 */
5285 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5286 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5287 	ill->ill_max_mtu = ill->ill_max_frag;
5288 
5289 	ill->ill_type = ipm->ip_m_type;
5290 
5291 	if (!ill->ill_dlpi_style_set) {
5292 		if (dlia->dl_provider_style == DL_STYLE2)
5293 			ill->ill_needs_attach = 1;
5294 
5295 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
5296 
5297 		/*
5298 		 * Allocate the first ipif on this ill.  We don't delay it
5299 		 * further as ioctl handling assumes at least one ipif exists.
5300 		 *
5301 		 * At this point we don't know whether the ill is v4 or v6.
5302 		 * We will know this whan the SIOCSLIFNAME happens and
5303 		 * the correct value for ill_isv6 will be assigned in
5304 		 * ipif_set_values(). We need to hold the ill lock and
5305 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5306 		 * the wakeup.
5307 		 */
5308 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5309 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5310 		mutex_enter(&ill->ill_lock);
5311 		ASSERT(ill->ill_dlpi_style_set == 0);
5312 		ill->ill_dlpi_style_set = 1;
5313 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5314 		cv_broadcast(&ill->ill_cv);
5315 		mutex_exit(&ill->ill_lock);
5316 		freemsg(mp);
5317 		return;
5318 	}
5319 	ASSERT(ill->ill_ipif != NULL);
5320 	/*
5321 	 * We know whether it is IPv4 or IPv6 now, as this is the
5322 	 * second DL_INFO_ACK we are recieving in response to the
5323 	 * DL_INFO_REQ sent in ipif_set_values.
5324 	 */
5325 	if (ill->ill_isv6)
5326 		ill->ill_sap = IP6_DL_SAP;
5327 	else
5328 		ill->ill_sap = IP_DL_SAP;
5329 	/*
5330 	 * Set ipif_mtu which is used to set the IRE's
5331 	 * ire_max_frag value. The driver could have sent
5332 	 * a different mtu from what it sent last time. No
5333 	 * need to call ipif_mtu_change because IREs have
5334 	 * not yet been created.
5335 	 */
5336 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5337 	/*
5338 	 * Clear all the flags that were set based on ill_bcast_addr_length
5339 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5340 	 * changed now and we need to re-evaluate.
5341 	 */
5342 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5343 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5344 
5345 	/*
5346 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5347 	 * changed now.
5348 	 *
5349 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5350 	 * with no underlying interfaces (and thus an unknown broadcast
5351 	 * address length), but we enforce that an interface is broadcast-
5352 	 * capable as part of allowing it to join a group.
5353 	 */
5354 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5355 		if (ill->ill_resolver_mp != NULL)
5356 			freemsg(ill->ill_resolver_mp);
5357 		if (ill->ill_bcast_mp != NULL)
5358 			freemsg(ill->ill_bcast_mp);
5359 		if (ill->ill_flags & ILLF_XRESOLV)
5360 			ill->ill_net_type = IRE_IF_RESOLVER;
5361 		else
5362 			ill->ill_net_type = IRE_IF_NORESOLVER;
5363 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5364 		    ill->ill_phys_addr_length,
5365 		    ill->ill_sap,
5366 		    ill->ill_sap_length);
5367 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5368 
5369 		if (ill->ill_isv6)
5370 			/*
5371 			 * Note: xresolv interfaces will eventually need NOARP
5372 			 * set here as well, but that will require those
5373 			 * external resolvers to have some knowledge of
5374 			 * that flag and act appropriately. Not to be changed
5375 			 * at present.
5376 			 */
5377 			ill->ill_flags |= ILLF_NONUD;
5378 		else
5379 			ill->ill_flags |= ILLF_NOARP;
5380 
5381 		if (ill->ill_phys_addr_length == 0) {
5382 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5383 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5384 			} else {
5385 				/* pt-pt supports multicast. */
5386 				ill->ill_flags |= ILLF_MULTICAST;
5387 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5388 			}
5389 		}
5390 	} else {
5391 		ill->ill_net_type = IRE_IF_RESOLVER;
5392 		if (ill->ill_bcast_mp != NULL)
5393 			freemsg(ill->ill_bcast_mp);
5394 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5395 		    ill->ill_bcast_addr_length, ill->ill_sap,
5396 		    ill->ill_sap_length);
5397 		/*
5398 		 * Later detect lack of DLPI driver multicast
5399 		 * capability by catching DL_ENABMULTI errors in
5400 		 * ip_rput_dlpi.
5401 		 */
5402 		ill->ill_flags |= ILLF_MULTICAST;
5403 		if (!ill->ill_isv6)
5404 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5405 	}
5406 
5407 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
5408 	if (ill->ill_mactype == SUNW_DL_IPMP)
5409 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5410 
5411 	/* By default an interface does not support any CoS marking */
5412 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5413 
5414 	/*
5415 	 * If we get QoS information in DL_INFO_ACK, the device supports
5416 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5417 	 */
5418 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5419 	    dlia->dl_qos_length);
5420 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5421 		ill->ill_flags |= ILLF_COS_ENABLED;
5422 	}
5423 
5424 	/* Clear any previous error indication. */
5425 	ill->ill_error = 0;
5426 	freemsg(mp);
5427 }
5428 
5429 /*
5430  * Perform various checks to verify that an address would make sense as a
5431  * local, remote, or subnet interface address.
5432  */
5433 static boolean_t
5434 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5435 {
5436 	ipaddr_t	net_mask;
5437 
5438 	/*
5439 	 * Don't allow all zeroes, or all ones, but allow
5440 	 * all ones netmask.
5441 	 */
5442 	if ((net_mask = ip_net_mask(addr)) == 0)
5443 		return (B_FALSE);
5444 	/* A given netmask overrides the "guess" netmask */
5445 	if (subnet_mask != 0)
5446 		net_mask = subnet_mask;
5447 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5448 	    (addr == (addr | ~net_mask)))) {
5449 		return (B_FALSE);
5450 	}
5451 
5452 	/*
5453 	 * Even if the netmask is all ones, we do not allow address to be
5454 	 * 255.255.255.255
5455 	 */
5456 	if (addr == INADDR_BROADCAST)
5457 		return (B_FALSE);
5458 
5459 	if (CLASSD(addr))
5460 		return (B_FALSE);
5461 
5462 	return (B_TRUE);
5463 }
5464 
5465 #define	V6_IPIF_LINKLOCAL(p)	\
5466 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5467 
5468 /*
5469  * Compare two given ipifs and check if the second one is better than
5470  * the first one using the order of preference (not taking deprecated
5471  * into acount) specified in ipif_lookup_multicast().
5472  */
5473 static boolean_t
5474 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5475 {
5476 	/* Check the least preferred first. */
5477 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5478 		/* If both ipifs are the same, use the first one. */
5479 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5480 			return (B_FALSE);
5481 		else
5482 			return (B_TRUE);
5483 	}
5484 
5485 	/* For IPv6, check for link local address. */
5486 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5487 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5488 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5489 			/* The second one is equal or less preferred. */
5490 			return (B_FALSE);
5491 		} else {
5492 			return (B_TRUE);
5493 		}
5494 	}
5495 
5496 	/* Then check for point to point interface. */
5497 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5498 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5499 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5500 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5501 			return (B_FALSE);
5502 		} else {
5503 			return (B_TRUE);
5504 		}
5505 	}
5506 
5507 	/* old_ipif is a normal interface, so no need to use the new one. */
5508 	return (B_FALSE);
5509 }
5510 
5511 /*
5512  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5513  * The ipif must be up, and its ill must multicast-capable, not
5514  * condemned, not an underlying interface in an IPMP group, and
5515  * not a VNI interface.  Order of preference:
5516  *
5517  * 	1a. normal
5518  * 	1b. normal, but deprecated
5519  * 	2a. point to point
5520  * 	2b. point to point, but deprecated
5521  * 	3a. link local
5522  * 	3b. link local, but deprecated
5523  * 	4. loopback.
5524  */
5525 ipif_t *
5526 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5527 {
5528 	ill_t			*ill;
5529 	ill_walk_context_t	ctx;
5530 	ipif_t			*ipif;
5531 	ipif_t			*saved_ipif = NULL;
5532 	ipif_t			*dep_ipif = NULL;
5533 
5534 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5535 	if (isv6)
5536 		ill = ILL_START_WALK_V6(&ctx, ipst);
5537 	else
5538 		ill = ILL_START_WALK_V4(&ctx, ipst);
5539 
5540 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5541 		mutex_enter(&ill->ill_lock);
5542 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5543 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5544 			mutex_exit(&ill->ill_lock);
5545 			continue;
5546 		}
5547 		for (ipif = ill->ill_ipif; ipif != NULL;
5548 		    ipif = ipif->ipif_next) {
5549 			if (zoneid != ipif->ipif_zoneid &&
5550 			    zoneid != ALL_ZONES &&
5551 			    ipif->ipif_zoneid != ALL_ZONES) {
5552 				continue;
5553 			}
5554 			if (!(ipif->ipif_flags & IPIF_UP) ||
5555 			    !IPIF_CAN_LOOKUP(ipif)) {
5556 				continue;
5557 			}
5558 
5559 			/*
5560 			 * Found one candidate.  If it is deprecated,
5561 			 * remember it in dep_ipif.  If it is not deprecated,
5562 			 * remember it in saved_ipif.
5563 			 */
5564 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5565 				if (dep_ipif == NULL) {
5566 					dep_ipif = ipif;
5567 				} else if (ipif_comp_multi(dep_ipif, ipif,
5568 				    isv6)) {
5569 					/*
5570 					 * If the previous dep_ipif does not
5571 					 * belong to the same ill, we've done
5572 					 * a ipif_refhold() on it.  So we need
5573 					 * to release it.
5574 					 */
5575 					if (dep_ipif->ipif_ill != ill)
5576 						ipif_refrele(dep_ipif);
5577 					dep_ipif = ipif;
5578 				}
5579 				continue;
5580 			}
5581 			if (saved_ipif == NULL) {
5582 				saved_ipif = ipif;
5583 			} else {
5584 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5585 					if (saved_ipif->ipif_ill != ill)
5586 						ipif_refrele(saved_ipif);
5587 					saved_ipif = ipif;
5588 				}
5589 			}
5590 		}
5591 		/*
5592 		 * Before going to the next ill, do a ipif_refhold() on the
5593 		 * saved ones.
5594 		 */
5595 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5596 			ipif_refhold_locked(saved_ipif);
5597 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5598 			ipif_refhold_locked(dep_ipif);
5599 		mutex_exit(&ill->ill_lock);
5600 	}
5601 	rw_exit(&ipst->ips_ill_g_lock);
5602 
5603 	/*
5604 	 * If we have only the saved_ipif, return it.  But if we have both
5605 	 * saved_ipif and dep_ipif, check to see which one is better.
5606 	 */
5607 	if (saved_ipif != NULL) {
5608 		if (dep_ipif != NULL) {
5609 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5610 				ipif_refrele(saved_ipif);
5611 				return (dep_ipif);
5612 			} else {
5613 				ipif_refrele(dep_ipif);
5614 				return (saved_ipif);
5615 			}
5616 		}
5617 		return (saved_ipif);
5618 	} else {
5619 		return (dep_ipif);
5620 	}
5621 }
5622 
5623 /*
5624  * This function is called when an application does not specify an interface
5625  * to be used for multicast traffic (joining a group/sending data).  It
5626  * calls ire_lookup_multi() to look for an interface route for the
5627  * specified multicast group.  Doing this allows the administrator to add
5628  * prefix routes for multicast to indicate which interface to be used for
5629  * multicast traffic in the above scenario.  The route could be for all
5630  * multicast (224.0/4), for a single multicast group (a /32 route) or
5631  * anything in between.  If there is no such multicast route, we just find
5632  * any multicast capable interface and return it.  The returned ipif
5633  * is refhold'ed.
5634  */
5635 ipif_t *
5636 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5637 {
5638 	ire_t			*ire;
5639 	ipif_t			*ipif;
5640 
5641 	ire = ire_lookup_multi(group, zoneid, ipst);
5642 	if (ire != NULL) {
5643 		ipif = ire->ire_ipif;
5644 		ipif_refhold(ipif);
5645 		ire_refrele(ire);
5646 		return (ipif);
5647 	}
5648 
5649 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5650 }
5651 
5652 /*
5653  * Look for an ipif with the specified interface address and destination.
5654  * The destination address is used only for matching point-to-point interfaces.
5655  */
5656 ipif_t *
5657 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5658     ipsq_func_t func, int *error, ip_stack_t *ipst)
5659 {
5660 	ipif_t	*ipif;
5661 	ill_t	*ill;
5662 	ill_walk_context_t ctx;
5663 	ipsq_t	*ipsq;
5664 
5665 	if (error != NULL)
5666 		*error = 0;
5667 
5668 	/*
5669 	 * First match all the point-to-point interfaces
5670 	 * before looking at non-point-to-point interfaces.
5671 	 * This is done to avoid returning non-point-to-point
5672 	 * ipif instead of unnumbered point-to-point ipif.
5673 	 */
5674 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5675 	ill = ILL_START_WALK_V4(&ctx, ipst);
5676 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5677 		GRAB_CONN_LOCK(q);
5678 		mutex_enter(&ill->ill_lock);
5679 		for (ipif = ill->ill_ipif; ipif != NULL;
5680 		    ipif = ipif->ipif_next) {
5681 			/* Allow the ipif to be down */
5682 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5683 			    (ipif->ipif_lcl_addr == if_addr) &&
5684 			    (ipif->ipif_pp_dst_addr == dst)) {
5685 				/*
5686 				 * The block comment at the start of ipif_down
5687 				 * explains the use of the macros used below
5688 				 */
5689 				if (IPIF_CAN_LOOKUP(ipif)) {
5690 					ipif_refhold_locked(ipif);
5691 					mutex_exit(&ill->ill_lock);
5692 					RELEASE_CONN_LOCK(q);
5693 					rw_exit(&ipst->ips_ill_g_lock);
5694 					return (ipif);
5695 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5696 					ipsq = ill->ill_phyint->phyint_ipsq;
5697 					mutex_enter(&ipsq->ipsq_lock);
5698 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5699 					mutex_exit(&ill->ill_lock);
5700 					rw_exit(&ipst->ips_ill_g_lock);
5701 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5702 					    ill);
5703 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5704 					mutex_exit(&ipsq->ipsq_lock);
5705 					RELEASE_CONN_LOCK(q);
5706 					if (error != NULL)
5707 						*error = EINPROGRESS;
5708 					return (NULL);
5709 				}
5710 			}
5711 		}
5712 		mutex_exit(&ill->ill_lock);
5713 		RELEASE_CONN_LOCK(q);
5714 	}
5715 	rw_exit(&ipst->ips_ill_g_lock);
5716 
5717 	/* lookup the ipif based on interface address */
5718 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5719 	    ipst);
5720 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5721 	return (ipif);
5722 }
5723 
5724 /*
5725  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5726  */
5727 static ipif_t *
5728 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5729     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5730     ip_stack_t *ipst)
5731 {
5732 	ipif_t  *ipif;
5733 	ill_t   *ill;
5734 	boolean_t ptp = B_FALSE;
5735 	ipsq_t	*ipsq;
5736 	ill_walk_context_t	ctx;
5737 
5738 	if (error != NULL)
5739 		*error = 0;
5740 
5741 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5742 	/*
5743 	 * Repeat twice, first based on local addresses and
5744 	 * next time for pointopoint.
5745 	 */
5746 repeat:
5747 	ill = ILL_START_WALK_V4(&ctx, ipst);
5748 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5749 		if (match_ill != NULL && ill != match_ill &&
5750 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5751 			continue;
5752 		}
5753 		GRAB_CONN_LOCK(q);
5754 		mutex_enter(&ill->ill_lock);
5755 		for (ipif = ill->ill_ipif; ipif != NULL;
5756 		    ipif = ipif->ipif_next) {
5757 			if (zoneid != ALL_ZONES &&
5758 			    zoneid != ipif->ipif_zoneid &&
5759 			    ipif->ipif_zoneid != ALL_ZONES)
5760 				continue;
5761 			/* Allow the ipif to be down */
5762 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5763 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5764 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5765 			    (ipif->ipif_pp_dst_addr == addr))) {
5766 				/*
5767 				 * The block comment at the start of ipif_down
5768 				 * explains the use of the macros used below
5769 				 */
5770 				if (IPIF_CAN_LOOKUP(ipif)) {
5771 					ipif_refhold_locked(ipif);
5772 					mutex_exit(&ill->ill_lock);
5773 					RELEASE_CONN_LOCK(q);
5774 					rw_exit(&ipst->ips_ill_g_lock);
5775 					return (ipif);
5776 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5777 					ipsq = ill->ill_phyint->phyint_ipsq;
5778 					mutex_enter(&ipsq->ipsq_lock);
5779 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5780 					mutex_exit(&ill->ill_lock);
5781 					rw_exit(&ipst->ips_ill_g_lock);
5782 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5783 					    ill);
5784 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5785 					mutex_exit(&ipsq->ipsq_lock);
5786 					RELEASE_CONN_LOCK(q);
5787 					if (error != NULL)
5788 						*error = EINPROGRESS;
5789 					return (NULL);
5790 				}
5791 			}
5792 		}
5793 		mutex_exit(&ill->ill_lock);
5794 		RELEASE_CONN_LOCK(q);
5795 	}
5796 
5797 	/* If we already did the ptp case, then we are done */
5798 	if (ptp) {
5799 		rw_exit(&ipst->ips_ill_g_lock);
5800 		if (error != NULL)
5801 			*error = ENXIO;
5802 		return (NULL);
5803 	}
5804 	ptp = B_TRUE;
5805 	goto repeat;
5806 }
5807 
5808 /*
5809  * Check if the address exists in the system.
5810  * We don't hold the conn_lock as we will not perform defered ipsqueue
5811  * operation.
5812  */
5813 boolean_t
5814 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5815 {
5816 	ipif_t  *ipif;
5817 	ill_t   *ill;
5818 	ill_walk_context_t	ctx;
5819 
5820 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5821 
5822 	ill = ILL_START_WALK_V4(&ctx, ipst);
5823 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5824 		mutex_enter(&ill->ill_lock);
5825 		for (ipif = ill->ill_ipif; ipif != NULL;
5826 		    ipif = ipif->ipif_next) {
5827 			if (zoneid != ALL_ZONES &&
5828 			    zoneid != ipif->ipif_zoneid &&
5829 			    ipif->ipif_zoneid != ALL_ZONES)
5830 				continue;
5831 			/* Allow the ipif to be down */
5832 			/*
5833 			 * XXX Different from ipif_lookup_addr(), we don't do
5834 			 * twice lookups. As from bind()'s point of view, we
5835 			 * may return once we find a match.
5836 			 */
5837 			if (((ipif->ipif_lcl_addr == addr) &&
5838 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5839 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5840 			    (ipif->ipif_pp_dst_addr == addr))) {
5841 				/*
5842 				 * Allow bind() to be successful even if the
5843 				 * ipif is with IPIF_CHANGING bit set.
5844 				 */
5845 				mutex_exit(&ill->ill_lock);
5846 				rw_exit(&ipst->ips_ill_g_lock);
5847 				return (B_TRUE);
5848 			}
5849 		}
5850 		mutex_exit(&ill->ill_lock);
5851 	}
5852 
5853 	rw_exit(&ipst->ips_ill_g_lock);
5854 	return (B_FALSE);
5855 }
5856 
5857 /*
5858  * Lookup an ipif with the specified address.  For point-to-point links we
5859  * look for matches on either the destination address or the local address,
5860  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5861  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5862  * (or illgrp if `match_ill' is in an IPMP group).
5863  */
5864 ipif_t *
5865 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5866     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5867 {
5868 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
5869 	    func, error, ipst));
5870 }
5871 
5872 /*
5873  * Special abbreviated version of ipif_lookup_addr() that doesn't match
5874  * `match_ill' across the IPMP group.  This function is only needed in some
5875  * corner-cases; almost everything should use ipif_lookup_addr().
5876  */
5877 static ipif_t *
5878 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5879 {
5880 	ASSERT(match_ill != NULL);
5881 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
5882 	    NULL, NULL, NULL, NULL, ipst));
5883 }
5884 
5885 /*
5886  * Look for an ipif with the specified address. For point-point links
5887  * we look for matches on either the destination address and the local
5888  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5889  * is set.
5890  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
5891  * ill (or illgrp if `match_ill' is in an IPMP group).
5892  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5893  */
5894 zoneid_t
5895 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5896 {
5897 	zoneid_t zoneid;
5898 	ipif_t  *ipif;
5899 	ill_t   *ill;
5900 	boolean_t ptp = B_FALSE;
5901 	ill_walk_context_t	ctx;
5902 
5903 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5904 	/*
5905 	 * Repeat twice, first based on local addresses and
5906 	 * next time for pointopoint.
5907 	 */
5908 repeat:
5909 	ill = ILL_START_WALK_V4(&ctx, ipst);
5910 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5911 		if (match_ill != NULL && ill != match_ill &&
5912 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
5913 			continue;
5914 		}
5915 		mutex_enter(&ill->ill_lock);
5916 		for (ipif = ill->ill_ipif; ipif != NULL;
5917 		    ipif = ipif->ipif_next) {
5918 			/* Allow the ipif to be down */
5919 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5920 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5921 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5922 			    (ipif->ipif_pp_dst_addr == addr)) &&
5923 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5924 				zoneid = ipif->ipif_zoneid;
5925 				mutex_exit(&ill->ill_lock);
5926 				rw_exit(&ipst->ips_ill_g_lock);
5927 				/*
5928 				 * If ipif_zoneid was ALL_ZONES then we have
5929 				 * a trusted extensions shared IP address.
5930 				 * In that case GLOBAL_ZONEID works to send.
5931 				 */
5932 				if (zoneid == ALL_ZONES)
5933 					zoneid = GLOBAL_ZONEID;
5934 				return (zoneid);
5935 			}
5936 		}
5937 		mutex_exit(&ill->ill_lock);
5938 	}
5939 
5940 	/* If we already did the ptp case, then we are done */
5941 	if (ptp) {
5942 		rw_exit(&ipst->ips_ill_g_lock);
5943 		return (ALL_ZONES);
5944 	}
5945 	ptp = B_TRUE;
5946 	goto repeat;
5947 }
5948 
5949 /*
5950  * Look for an ipif that matches the specified remote address i.e. the
5951  * ipif that would receive the specified packet.
5952  * First look for directly connected interfaces and then do a recursive
5953  * IRE lookup and pick the first ipif corresponding to the source address in the
5954  * ire.
5955  * Returns: held ipif
5956  */
5957 ipif_t *
5958 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5959 {
5960 	ipif_t	*ipif;
5961 	ire_t	*ire;
5962 	ip_stack_t	*ipst = ill->ill_ipst;
5963 
5964 	ASSERT(!ill->ill_isv6);
5965 
5966 	/*
5967 	 * Someone could be changing this ipif currently or change it
5968 	 * after we return this. Thus  a few packets could use the old
5969 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5970 	 * will atomically be updated or cleaned up with the new value
5971 	 * Thus we don't need a lock to check the flags or other attrs below.
5972 	 */
5973 	mutex_enter(&ill->ill_lock);
5974 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5975 		if (!IPIF_CAN_LOOKUP(ipif))
5976 			continue;
5977 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5978 		    ipif->ipif_zoneid != ALL_ZONES)
5979 			continue;
5980 		/* Allow the ipif to be down */
5981 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5982 			if ((ipif->ipif_pp_dst_addr == addr) ||
5983 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5984 			    ipif->ipif_lcl_addr == addr)) {
5985 				ipif_refhold_locked(ipif);
5986 				mutex_exit(&ill->ill_lock);
5987 				return (ipif);
5988 			}
5989 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5990 			ipif_refhold_locked(ipif);
5991 			mutex_exit(&ill->ill_lock);
5992 			return (ipif);
5993 		}
5994 	}
5995 	mutex_exit(&ill->ill_lock);
5996 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5997 	    NULL, MATCH_IRE_RECURSIVE, ipst);
5998 	if (ire != NULL) {
5999 		/*
6000 		 * The callers of this function wants to know the
6001 		 * interface on which they have to send the replies
6002 		 * back. For IREs that have ire_stq and ire_ipif
6003 		 * derived from different ills, we really don't care
6004 		 * what we return here.
6005 		 */
6006 		ipif = ire->ire_ipif;
6007 		if (ipif != NULL) {
6008 			ipif_refhold(ipif);
6009 			ire_refrele(ire);
6010 			return (ipif);
6011 		}
6012 		ire_refrele(ire);
6013 	}
6014 	/* Pick the first interface */
6015 	ipif = ipif_get_next_ipif(NULL, ill);
6016 	return (ipif);
6017 }
6018 
6019 /*
6020  * This func does not prevent refcnt from increasing. But if
6021  * the caller has taken steps to that effect, then this func
6022  * can be used to determine whether the ill has become quiescent
6023  */
6024 static boolean_t
6025 ill_is_quiescent(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_DOWN_OK(ipif)) {
6033 			return (B_FALSE);
6034 		}
6035 	}
6036 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6037 		return (B_FALSE);
6038 	}
6039 	return (B_TRUE);
6040 }
6041 
6042 boolean_t
6043 ill_is_freeable(ill_t *ill)
6044 {
6045 	ipif_t	*ipif;
6046 
6047 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6048 
6049 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6050 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6051 			return (B_FALSE);
6052 		}
6053 	}
6054 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6055 		return (B_FALSE);
6056 	}
6057 	return (B_TRUE);
6058 }
6059 
6060 /*
6061  * This func does not prevent refcnt from increasing. But if
6062  * the caller has taken steps to that effect, then this func
6063  * can be used to determine whether the ipif has become quiescent
6064  */
6065 static boolean_t
6066 ipif_is_quiescent(ipif_t *ipif)
6067 {
6068 	ill_t *ill;
6069 
6070 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6071 
6072 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6073 		return (B_FALSE);
6074 	}
6075 
6076 	ill = ipif->ipif_ill;
6077 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6078 	    ill->ill_logical_down) {
6079 		return (B_TRUE);
6080 	}
6081 
6082 	/* This is the last ipif going down or being deleted on this ill */
6083 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6084 		return (B_FALSE);
6085 	}
6086 
6087 	return (B_TRUE);
6088 }
6089 
6090 /*
6091  * return true if the ipif can be destroyed: the ipif has to be quiescent
6092  * with zero references from ire/nce/ilm to it.
6093  */
6094 static boolean_t
6095 ipif_is_freeable(ipif_t *ipif)
6096 {
6097 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6098 	ASSERT(ipif->ipif_id != 0);
6099 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6100 }
6101 
6102 /*
6103  * The ipif/ill/ire has been refreled. Do the tail processing.
6104  * Determine if the ipif or ill in question has become quiescent and if so
6105  * wakeup close and/or restart any queued pending ioctl that is waiting
6106  * for the ipif_down (or ill_down)
6107  */
6108 void
6109 ipif_ill_refrele_tail(ill_t *ill)
6110 {
6111 	mblk_t	*mp;
6112 	conn_t	*connp;
6113 	ipsq_t	*ipsq;
6114 	ipxop_t	*ipx;
6115 	ipif_t	*ipif;
6116 	dl_notify_ind_t *dlindp;
6117 
6118 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6119 
6120 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6121 		/* ip_modclose() may be waiting */
6122 		cv_broadcast(&ill->ill_cv);
6123 	}
6124 
6125 	ipsq = ill->ill_phyint->phyint_ipsq;
6126 	mutex_enter(&ipsq->ipsq_lock);
6127 	ipx = ipsq->ipsq_xop;
6128 	mutex_enter(&ipx->ipx_lock);
6129 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6130 		goto unlock;
6131 
6132 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6133 
6134 	ipif = ipx->ipx_pending_ipif;
6135 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6136 		goto unlock;
6137 
6138 	switch (ipx->ipx_waitfor) {
6139 	case IPIF_DOWN:
6140 		if (!ipif_is_quiescent(ipif))
6141 			goto unlock;
6142 		break;
6143 	case IPIF_FREE:
6144 		if (!ipif_is_freeable(ipif))
6145 			goto unlock;
6146 		break;
6147 	case ILL_DOWN:
6148 		if (!ill_is_quiescent(ill))
6149 			goto unlock;
6150 		break;
6151 	case ILL_FREE:
6152 		/*
6153 		 * ILL_FREE is only for loopback; normal ill teardown waits
6154 		 * synchronously in ip_modclose() without using ipx_waitfor,
6155 		 * handled by the cv_broadcast() at the top of this function.
6156 		 */
6157 		if (!ill_is_freeable(ill))
6158 			goto unlock;
6159 		break;
6160 	default:
6161 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6162 		    (void *)ipsq, ipx->ipx_waitfor);
6163 	}
6164 
6165 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6166 	mutex_exit(&ipx->ipx_lock);
6167 	mp = ipsq_pending_mp_get(ipsq, &connp);
6168 	mutex_exit(&ipsq->ipsq_lock);
6169 	mutex_exit(&ill->ill_lock);
6170 
6171 	ASSERT(mp != NULL);
6172 	/*
6173 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6174 	 * we can only get here when the current operation decides it
6175 	 * it needs to quiesce via ipsq_pending_mp_add().
6176 	 */
6177 	switch (mp->b_datap->db_type) {
6178 	case M_PCPROTO:
6179 	case M_PROTO:
6180 		/*
6181 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6182 		 */
6183 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6184 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6185 
6186 		switch (dlindp->dl_notification) {
6187 		case DL_NOTE_PHYS_ADDR:
6188 			qwriter_ip(ill, ill->ill_rq, mp,
6189 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6190 			return;
6191 		case DL_NOTE_REPLUMB:
6192 			qwriter_ip(ill, ill->ill_rq, mp,
6193 			    ill_replumb_tail, CUR_OP, B_TRUE);
6194 			return;
6195 		default:
6196 			ASSERT(0);
6197 			ill_refrele(ill);
6198 		}
6199 		break;
6200 
6201 	case M_ERROR:
6202 	case M_HANGUP:
6203 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6204 		    B_TRUE);
6205 		return;
6206 
6207 	case M_IOCTL:
6208 	case M_IOCDATA:
6209 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6210 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6211 		return;
6212 
6213 	default:
6214 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6215 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6216 	}
6217 	return;
6218 unlock:
6219 	mutex_exit(&ipsq->ipsq_lock);
6220 	mutex_exit(&ipx->ipx_lock);
6221 	mutex_exit(&ill->ill_lock);
6222 }
6223 
6224 #ifdef DEBUG
6225 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6226 static void
6227 th_trace_rrecord(th_trace_t *th_trace)
6228 {
6229 	tr_buf_t *tr_buf;
6230 	uint_t lastref;
6231 
6232 	lastref = th_trace->th_trace_lastref;
6233 	lastref++;
6234 	if (lastref == TR_BUF_MAX)
6235 		lastref = 0;
6236 	th_trace->th_trace_lastref = lastref;
6237 	tr_buf = &th_trace->th_trbuf[lastref];
6238 	tr_buf->tr_time = lbolt;
6239 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6240 }
6241 
6242 static void
6243 th_trace_free(void *value)
6244 {
6245 	th_trace_t *th_trace = value;
6246 
6247 	ASSERT(th_trace->th_refcnt == 0);
6248 	kmem_free(th_trace, sizeof (*th_trace));
6249 }
6250 
6251 /*
6252  * Find or create the per-thread hash table used to track object references.
6253  * The ipst argument is NULL if we shouldn't allocate.
6254  *
6255  * Accesses per-thread data, so there's no need to lock here.
6256  */
6257 static mod_hash_t *
6258 th_trace_gethash(ip_stack_t *ipst)
6259 {
6260 	th_hash_t *thh;
6261 
6262 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6263 		mod_hash_t *mh;
6264 		char name[256];
6265 		size_t objsize, rshift;
6266 		int retv;
6267 
6268 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6269 			return (NULL);
6270 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6271 		    (void *)curthread);
6272 
6273 		/*
6274 		 * We use mod_hash_create_extended here rather than the more
6275 		 * obvious mod_hash_create_ptrhash because the latter has a
6276 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6277 		 * block.
6278 		 */
6279 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6280 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6281 		rshift = highbit(objsize);
6282 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6283 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6284 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6285 		if (mh == NULL) {
6286 			kmem_free(thh, sizeof (*thh));
6287 			return (NULL);
6288 		}
6289 		thh->thh_hash = mh;
6290 		thh->thh_ipst = ipst;
6291 		/*
6292 		 * We trace ills, ipifs, ires, and nces.  All of these are
6293 		 * per-IP-stack, so the lock on the thread list is as well.
6294 		 */
6295 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6296 		list_insert_tail(&ip_thread_list, thh);
6297 		rw_exit(&ip_thread_rwlock);
6298 		retv = tsd_set(ip_thread_data, thh);
6299 		ASSERT(retv == 0);
6300 	}
6301 	return (thh != NULL ? thh->thh_hash : NULL);
6302 }
6303 
6304 boolean_t
6305 th_trace_ref(const void *obj, ip_stack_t *ipst)
6306 {
6307 	th_trace_t *th_trace;
6308 	mod_hash_t *mh;
6309 	mod_hash_val_t val;
6310 
6311 	if ((mh = th_trace_gethash(ipst)) == NULL)
6312 		return (B_FALSE);
6313 
6314 	/*
6315 	 * Attempt to locate the trace buffer for this obj and thread.
6316 	 * If it does not exist, then allocate a new trace buffer and
6317 	 * insert into the hash.
6318 	 */
6319 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6320 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6321 		if (th_trace == NULL)
6322 			return (B_FALSE);
6323 
6324 		th_trace->th_id = curthread;
6325 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6326 		    (mod_hash_val_t)th_trace) != 0) {
6327 			kmem_free(th_trace, sizeof (th_trace_t));
6328 			return (B_FALSE);
6329 		}
6330 	} else {
6331 		th_trace = (th_trace_t *)val;
6332 	}
6333 
6334 	ASSERT(th_trace->th_refcnt >= 0 &&
6335 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6336 
6337 	th_trace->th_refcnt++;
6338 	th_trace_rrecord(th_trace);
6339 	return (B_TRUE);
6340 }
6341 
6342 /*
6343  * For the purpose of tracing a reference release, we assume that global
6344  * tracing is always on and that the same thread initiated the reference hold
6345  * is releasing.
6346  */
6347 void
6348 th_trace_unref(const void *obj)
6349 {
6350 	int retv;
6351 	mod_hash_t *mh;
6352 	th_trace_t *th_trace;
6353 	mod_hash_val_t val;
6354 
6355 	mh = th_trace_gethash(NULL);
6356 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6357 	ASSERT(retv == 0);
6358 	th_trace = (th_trace_t *)val;
6359 
6360 	ASSERT(th_trace->th_refcnt > 0);
6361 	th_trace->th_refcnt--;
6362 	th_trace_rrecord(th_trace);
6363 }
6364 
6365 /*
6366  * If tracing has been disabled, then we assume that the reference counts are
6367  * now useless, and we clear them out before destroying the entries.
6368  */
6369 void
6370 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6371 {
6372 	th_hash_t	*thh;
6373 	mod_hash_t	*mh;
6374 	mod_hash_val_t	val;
6375 	th_trace_t	*th_trace;
6376 	int		retv;
6377 
6378 	rw_enter(&ip_thread_rwlock, RW_READER);
6379 	for (thh = list_head(&ip_thread_list); thh != NULL;
6380 	    thh = list_next(&ip_thread_list, thh)) {
6381 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6382 		    &val) == 0) {
6383 			th_trace = (th_trace_t *)val;
6384 			if (trace_disable)
6385 				th_trace->th_refcnt = 0;
6386 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6387 			ASSERT(retv == 0);
6388 		}
6389 	}
6390 	rw_exit(&ip_thread_rwlock);
6391 }
6392 
6393 void
6394 ipif_trace_ref(ipif_t *ipif)
6395 {
6396 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6397 
6398 	if (ipif->ipif_trace_disable)
6399 		return;
6400 
6401 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6402 		ipif->ipif_trace_disable = B_TRUE;
6403 		ipif_trace_cleanup(ipif);
6404 	}
6405 }
6406 
6407 void
6408 ipif_untrace_ref(ipif_t *ipif)
6409 {
6410 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6411 
6412 	if (!ipif->ipif_trace_disable)
6413 		th_trace_unref(ipif);
6414 }
6415 
6416 void
6417 ill_trace_ref(ill_t *ill)
6418 {
6419 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6420 
6421 	if (ill->ill_trace_disable)
6422 		return;
6423 
6424 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6425 		ill->ill_trace_disable = B_TRUE;
6426 		ill_trace_cleanup(ill);
6427 	}
6428 }
6429 
6430 void
6431 ill_untrace_ref(ill_t *ill)
6432 {
6433 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6434 
6435 	if (!ill->ill_trace_disable)
6436 		th_trace_unref(ill);
6437 }
6438 
6439 /*
6440  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6441  * failure, ipif_trace_disable is set.
6442  */
6443 static void
6444 ipif_trace_cleanup(const ipif_t *ipif)
6445 {
6446 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6447 }
6448 
6449 /*
6450  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6451  * failure, ill_trace_disable is set.
6452  */
6453 static void
6454 ill_trace_cleanup(const ill_t *ill)
6455 {
6456 	th_trace_cleanup(ill, ill->ill_trace_disable);
6457 }
6458 #endif /* DEBUG */
6459 
6460 void
6461 ipif_refhold_locked(ipif_t *ipif)
6462 {
6463 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6464 	ipif->ipif_refcnt++;
6465 	IPIF_TRACE_REF(ipif);
6466 }
6467 
6468 void
6469 ipif_refhold(ipif_t *ipif)
6470 {
6471 	ill_t	*ill;
6472 
6473 	ill = ipif->ipif_ill;
6474 	mutex_enter(&ill->ill_lock);
6475 	ipif->ipif_refcnt++;
6476 	IPIF_TRACE_REF(ipif);
6477 	mutex_exit(&ill->ill_lock);
6478 }
6479 
6480 /*
6481  * Must not be called while holding any locks. Otherwise if this is
6482  * the last reference to be released there is a chance of recursive mutex
6483  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6484  * to restart an ioctl.
6485  */
6486 void
6487 ipif_refrele(ipif_t *ipif)
6488 {
6489 	ill_t	*ill;
6490 
6491 	ill = ipif->ipif_ill;
6492 
6493 	mutex_enter(&ill->ill_lock);
6494 	ASSERT(ipif->ipif_refcnt != 0);
6495 	ipif->ipif_refcnt--;
6496 	IPIF_UNTRACE_REF(ipif);
6497 	if (ipif->ipif_refcnt != 0) {
6498 		mutex_exit(&ill->ill_lock);
6499 		return;
6500 	}
6501 
6502 	/* Drops the ill_lock */
6503 	ipif_ill_refrele_tail(ill);
6504 }
6505 
6506 ipif_t *
6507 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6508 {
6509 	ipif_t	*ipif;
6510 
6511 	mutex_enter(&ill->ill_lock);
6512 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6513 	    ipif != NULL; ipif = ipif->ipif_next) {
6514 		if (!IPIF_CAN_LOOKUP(ipif))
6515 			continue;
6516 		ipif_refhold_locked(ipif);
6517 		mutex_exit(&ill->ill_lock);
6518 		return (ipif);
6519 	}
6520 	mutex_exit(&ill->ill_lock);
6521 	return (NULL);
6522 }
6523 
6524 /*
6525  * TODO: make this table extendible at run time
6526  * Return a pointer to the mac type info for 'mac_type'
6527  */
6528 static ip_m_t *
6529 ip_m_lookup(t_uscalar_t mac_type)
6530 {
6531 	ip_m_t	*ipm;
6532 
6533 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6534 		if (ipm->ip_m_mac_type == mac_type)
6535 			return (ipm);
6536 	return (NULL);
6537 }
6538 
6539 /*
6540  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6541  * ipif_arg is passed in to associate it with the correct interface.
6542  * We may need to restart this operation if the ipif cannot be looked up
6543  * due to an exclusive operation that is currently in progress. The restart
6544  * entry point is specified by 'func'
6545  */
6546 int
6547 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6548     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6549     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6550     struct rtsa_s *sp, ip_stack_t *ipst)
6551 {
6552 	ire_t	*ire;
6553 	ire_t	*gw_ire = NULL;
6554 	ipif_t	*ipif = NULL;
6555 	boolean_t ipif_refheld = B_FALSE;
6556 	uint_t	type;
6557 	int	match_flags = MATCH_IRE_TYPE;
6558 	int	error;
6559 	tsol_gc_t *gc = NULL;
6560 	tsol_gcgrp_t *gcgrp = NULL;
6561 	boolean_t gcgrp_xtraref = B_FALSE;
6562 
6563 	ip1dbg(("ip_rt_add:"));
6564 
6565 	if (ire_arg != NULL)
6566 		*ire_arg = NULL;
6567 
6568 	/*
6569 	 * If this is the case of RTF_HOST being set, then we set the netmask
6570 	 * to all ones (regardless if one was supplied).
6571 	 */
6572 	if (flags & RTF_HOST)
6573 		mask = IP_HOST_MASK;
6574 
6575 	/*
6576 	 * Prevent routes with a zero gateway from being created (since
6577 	 * interfaces can currently be plumbed and brought up no assigned
6578 	 * address).
6579 	 */
6580 	if (gw_addr == 0)
6581 		return (ENETUNREACH);
6582 	/*
6583 	 * Get the ipif, if any, corresponding to the gw_addr
6584 	 */
6585 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6586 	    ipst);
6587 	if (ipif != NULL) {
6588 		if (IS_VNI(ipif->ipif_ill)) {
6589 			ipif_refrele(ipif);
6590 			return (EINVAL);
6591 		}
6592 		ipif_refheld = B_TRUE;
6593 	} else if (error == EINPROGRESS) {
6594 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6595 		return (EINPROGRESS);
6596 	} else {
6597 		error = 0;
6598 	}
6599 
6600 	if (ipif != NULL) {
6601 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6602 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6603 	} else {
6604 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6605 	}
6606 
6607 	/*
6608 	 * GateD will attempt to create routes with a loopback interface
6609 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6610 	 * these routes to be added, but create them as interface routes
6611 	 * since the gateway is an interface address.
6612 	 */
6613 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6614 		flags &= ~RTF_GATEWAY;
6615 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6616 		    mask == IP_HOST_MASK) {
6617 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6618 			    ALL_ZONES, NULL, match_flags, ipst);
6619 			if (ire != NULL) {
6620 				ire_refrele(ire);
6621 				if (ipif_refheld)
6622 					ipif_refrele(ipif);
6623 				return (EEXIST);
6624 			}
6625 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
6626 			    "for 0x%x\n", (void *)ipif,
6627 			    ipif->ipif_ire_type,
6628 			    ntohl(ipif->ipif_lcl_addr)));
6629 			ire = ire_create(
6630 			    (uchar_t *)&dst_addr,	/* dest address */
6631 			    (uchar_t *)&mask,		/* mask */
6632 			    (uchar_t *)&ipif->ipif_src_addr,
6633 			    NULL,			/* no gateway */
6634 			    &ipif->ipif_mtu,
6635 			    NULL,
6636 			    ipif->ipif_rq,		/* recv-from queue */
6637 			    NULL,			/* no send-to queue */
6638 			    ipif->ipif_ire_type,	/* LOOPBACK */
6639 			    ipif,
6640 			    0,
6641 			    0,
6642 			    0,
6643 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6644 			    RTF_PRIVATE : 0,
6645 			    &ire_uinfo_null,
6646 			    NULL,
6647 			    NULL,
6648 			    ipst);
6649 
6650 			if (ire == NULL) {
6651 				if (ipif_refheld)
6652 					ipif_refrele(ipif);
6653 				return (ENOMEM);
6654 			}
6655 			error = ire_add(&ire, q, mp, func, B_FALSE);
6656 			if (error == 0)
6657 				goto save_ire;
6658 			if (ipif_refheld)
6659 				ipif_refrele(ipif);
6660 			return (error);
6661 
6662 		}
6663 	}
6664 
6665 	/*
6666 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6667 	 * and the gateway address provided is one of the system's interface
6668 	 * addresses.  By using the routing socket interface and supplying an
6669 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6670 	 * specifying an interface route to be created is available which uses
6671 	 * the interface index that specifies the outgoing interface rather than
6672 	 * the address of an outgoing interface (which may not be able to
6673 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6674 	 * flag, routes can be specified which not only specify the next-hop to
6675 	 * be used when routing to a certain prefix, but also which outgoing
6676 	 * interface should be used.
6677 	 *
6678 	 * Previously, interfaces would have unique addresses assigned to them
6679 	 * and so the address assigned to a particular interface could be used
6680 	 * to identify a particular interface.  One exception to this was the
6681 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6682 	 *
6683 	 * With the advent of IPv6 and its link-local addresses, this
6684 	 * restriction was relaxed and interfaces could share addresses between
6685 	 * themselves.  In fact, typically all of the link-local interfaces on
6686 	 * an IPv6 node or router will have the same link-local address.  In
6687 	 * order to differentiate between these interfaces, the use of an
6688 	 * interface index is necessary and this index can be carried inside a
6689 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6690 	 * of using the interface index, however, is that all of the ipif's that
6691 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6692 	 * cannot be used to differentiate between ipif's (or logical
6693 	 * interfaces) that belong to the same ill (physical interface).
6694 	 *
6695 	 * For example, in the following case involving IPv4 interfaces and
6696 	 * logical interfaces
6697 	 *
6698 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6699 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6700 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6701 	 *
6702 	 * the ipif's corresponding to each of these interface routes can be
6703 	 * uniquely identified by the "gateway" (actually interface address).
6704 	 *
6705 	 * In this case involving multiple IPv6 default routes to a particular
6706 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6707 	 * default route is of interest:
6708 	 *
6709 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6710 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6711 	 */
6712 
6713 	/* RTF_GATEWAY not set */
6714 	if (!(flags & RTF_GATEWAY)) {
6715 		queue_t	*stq;
6716 
6717 		if (sp != NULL) {
6718 			ip2dbg(("ip_rt_add: gateway security attributes "
6719 			    "cannot be set with interface route\n"));
6720 			if (ipif_refheld)
6721 				ipif_refrele(ipif);
6722 			return (EINVAL);
6723 		}
6724 
6725 		/*
6726 		 * As the interface index specified with the RTA_IFP sockaddr is
6727 		 * the same for all ipif's off of an ill, the matching logic
6728 		 * below uses MATCH_IRE_ILL if such an index was specified.
6729 		 * This means that routes sharing the same prefix when added
6730 		 * using a RTA_IFP sockaddr must have distinct interface
6731 		 * indices (namely, they must be on distinct ill's).
6732 		 *
6733 		 * On the other hand, since the gateway address will usually be
6734 		 * different for each ipif on the system, the matching logic
6735 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6736 		 * route.  This means that interface routes for the same prefix
6737 		 * can be created if they belong to distinct ipif's and if a
6738 		 * RTA_IFP sockaddr is not present.
6739 		 */
6740 		if (ipif_arg != NULL) {
6741 			if (ipif_refheld)  {
6742 				ipif_refrele(ipif);
6743 				ipif_refheld = B_FALSE;
6744 			}
6745 			ipif = ipif_arg;
6746 			match_flags |= MATCH_IRE_ILL;
6747 		} else {
6748 			/*
6749 			 * Check the ipif corresponding to the gw_addr
6750 			 */
6751 			if (ipif == NULL)
6752 				return (ENETUNREACH);
6753 			match_flags |= MATCH_IRE_IPIF;
6754 		}
6755 		ASSERT(ipif != NULL);
6756 
6757 		/*
6758 		 * We check for an existing entry at this point.
6759 		 *
6760 		 * Since a netmask isn't passed in via the ioctl interface
6761 		 * (SIOCADDRT), we don't check for a matching netmask in that
6762 		 * case.
6763 		 */
6764 		if (!ioctl_msg)
6765 			match_flags |= MATCH_IRE_MASK;
6766 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6767 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6768 		if (ire != NULL) {
6769 			ire_refrele(ire);
6770 			if (ipif_refheld)
6771 				ipif_refrele(ipif);
6772 			return (EEXIST);
6773 		}
6774 
6775 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6776 		    ? ipif->ipif_rq : ipif->ipif_wq;
6777 
6778 		/*
6779 		 * Create a copy of the IRE_LOOPBACK,
6780 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6781 		 * the modified address and netmask.
6782 		 */
6783 		ire = ire_create(
6784 		    (uchar_t *)&dst_addr,
6785 		    (uint8_t *)&mask,
6786 		    (uint8_t *)&ipif->ipif_src_addr,
6787 		    NULL,
6788 		    &ipif->ipif_mtu,
6789 		    NULL,
6790 		    NULL,
6791 		    stq,
6792 		    ipif->ipif_net_type,
6793 		    ipif,
6794 		    0,
6795 		    0,
6796 		    0,
6797 		    flags,
6798 		    &ire_uinfo_null,
6799 		    NULL,
6800 		    NULL,
6801 		    ipst);
6802 		if (ire == NULL) {
6803 			if (ipif_refheld)
6804 				ipif_refrele(ipif);
6805 			return (ENOMEM);
6806 		}
6807 
6808 		/*
6809 		 * Some software (for example, GateD and Sun Cluster) attempts
6810 		 * to create (what amount to) IRE_PREFIX routes with the
6811 		 * loopback address as the gateway.  This is primarily done to
6812 		 * set up prefixes with the RTF_REJECT flag set (for example,
6813 		 * when generating aggregate routes.)
6814 		 *
6815 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6816 		 * IRE_LOOPBACK, then we map the request into a
6817 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6818 		 * these interface routes, by definition, can only be that.
6819 		 *
6820 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6821 		 * routine, but rather using ire_create() directly.
6822 		 *
6823 		 */
6824 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6825 			ire->ire_type = IRE_IF_NORESOLVER;
6826 			ire->ire_flags |= RTF_BLACKHOLE;
6827 		}
6828 
6829 		error = ire_add(&ire, q, mp, func, B_FALSE);
6830 		if (error == 0)
6831 			goto save_ire;
6832 
6833 		/*
6834 		 * In the result of failure, ire_add() will have already
6835 		 * deleted the ire in question, so there is no need to
6836 		 * do that here.
6837 		 */
6838 		if (ipif_refheld)
6839 			ipif_refrele(ipif);
6840 		return (error);
6841 	}
6842 	if (ipif_refheld) {
6843 		ipif_refrele(ipif);
6844 		ipif_refheld = B_FALSE;
6845 	}
6846 
6847 	/*
6848 	 * Get an interface IRE for the specified gateway.
6849 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6850 	 * gateway, it is currently unreachable and we fail the request
6851 	 * accordingly.
6852 	 */
6853 	ipif = ipif_arg;
6854 	if (ipif_arg != NULL)
6855 		match_flags |= MATCH_IRE_ILL;
6856 again:
6857 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6858 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6859 	if (gw_ire == NULL) {
6860 		/*
6861 		 * With IPMP, we allow host routes to influence in.mpathd's
6862 		 * target selection.  However, if the test addresses are on
6863 		 * their own network, the above lookup will fail since the
6864 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6865 		 * hidden test IREs to be found and try again.
6866 		 */
6867 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6868 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6869 			goto again;
6870 		}
6871 		return (ENETUNREACH);
6872 	}
6873 
6874 	/*
6875 	 * We create one of three types of IREs as a result of this request
6876 	 * based on the netmask.  A netmask of all ones (which is automatically
6877 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6878 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6879 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6880 	 * destination prefix.
6881 	 */
6882 	if (mask == IP_HOST_MASK)
6883 		type = IRE_HOST;
6884 	else if (mask == 0)
6885 		type = IRE_DEFAULT;
6886 	else
6887 		type = IRE_PREFIX;
6888 
6889 	/* check for a duplicate entry */
6890 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6891 	    NULL, ALL_ZONES, 0, NULL,
6892 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
6893 	if (ire != NULL) {
6894 		ire_refrele(gw_ire);
6895 		ire_refrele(ire);
6896 		return (EEXIST);
6897 	}
6898 
6899 	/* Security attribute exists */
6900 	if (sp != NULL) {
6901 		tsol_gcgrp_addr_t ga;
6902 
6903 		/* find or create the gateway credentials group */
6904 		ga.ga_af = AF_INET;
6905 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6906 
6907 		/* we hold reference to it upon success */
6908 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6909 		if (gcgrp == NULL) {
6910 			ire_refrele(gw_ire);
6911 			return (ENOMEM);
6912 		}
6913 
6914 		/*
6915 		 * Create and add the security attribute to the group; a
6916 		 * reference to the group is made upon allocating a new
6917 		 * entry successfully.  If it finds an already-existing
6918 		 * entry for the security attribute in the group, it simply
6919 		 * returns it and no new reference is made to the group.
6920 		 */
6921 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6922 		if (gc == NULL) {
6923 			/* release reference held by gcgrp_lookup */
6924 			GCGRP_REFRELE(gcgrp);
6925 			ire_refrele(gw_ire);
6926 			return (ENOMEM);
6927 		}
6928 	}
6929 
6930 	/* Create the IRE. */
6931 	ire = ire_create(
6932 	    (uchar_t *)&dst_addr,		/* dest address */
6933 	    (uchar_t *)&mask,			/* mask */
6934 	    /* src address assigned by the caller? */
6935 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6936 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
6937 	    (uchar_t *)&gw_addr,		/* gateway address */
6938 	    &gw_ire->ire_max_frag,
6939 	    NULL,				/* no src nce */
6940 	    NULL,				/* no recv-from queue */
6941 	    NULL,				/* no send-to queue */
6942 	    (ushort_t)type,			/* IRE type */
6943 	    ipif_arg,
6944 	    0,
6945 	    0,
6946 	    0,
6947 	    flags,
6948 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6949 	    gc,					/* security attribute */
6950 	    NULL,
6951 	    ipst);
6952 
6953 	/*
6954 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6955 	 * reference to the 'gcgrp'. We can now release the extra reference
6956 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6957 	 */
6958 	if (gcgrp_xtraref)
6959 		GCGRP_REFRELE(gcgrp);
6960 	if (ire == NULL) {
6961 		if (gc != NULL)
6962 			GC_REFRELE(gc);
6963 		ire_refrele(gw_ire);
6964 		return (ENOMEM);
6965 	}
6966 
6967 	/*
6968 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6969 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6970 	 */
6971 
6972 	/* Add the new IRE. */
6973 	error = ire_add(&ire, q, mp, func, B_FALSE);
6974 	if (error != 0) {
6975 		/*
6976 		 * In the result of failure, ire_add() will have already
6977 		 * deleted the ire in question, so there is no need to
6978 		 * do that here.
6979 		 */
6980 		ire_refrele(gw_ire);
6981 		return (error);
6982 	}
6983 
6984 	if (flags & RTF_MULTIRT) {
6985 		/*
6986 		 * Invoke the CGTP (multirouting) filtering module
6987 		 * to add the dst address in the filtering database.
6988 		 * Replicated inbound packets coming from that address
6989 		 * will be filtered to discard the duplicates.
6990 		 * It is not necessary to call the CGTP filter hook
6991 		 * when the dst address is a broadcast or multicast,
6992 		 * because an IP source address cannot be a broadcast
6993 		 * or a multicast.
6994 		 */
6995 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6996 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
6997 		if (ire_dst != NULL) {
6998 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
6999 			ire_refrele(ire_dst);
7000 			goto save_ire;
7001 		}
7002 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7003 		    !CLASSD(ire->ire_addr)) {
7004 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7005 			    ipst->ips_netstack->netstack_stackid,
7006 			    ire->ire_addr,
7007 			    ire->ire_gateway_addr,
7008 			    ire->ire_src_addr,
7009 			    gw_ire->ire_src_addr);
7010 			if (res != 0) {
7011 				ire_refrele(gw_ire);
7012 				ire_delete(ire);
7013 				return (res);
7014 			}
7015 		}
7016 	}
7017 
7018 	/*
7019 	 * Now that the prefix IRE entry has been created, delete any
7020 	 * existing gateway IRE cache entries as well as any IRE caches
7021 	 * using the gateway, and force them to be created through
7022 	 * ip_newroute.
7023 	 */
7024 	if (gc != NULL) {
7025 		ASSERT(gcgrp != NULL);
7026 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7027 	}
7028 
7029 save_ire:
7030 	if (gw_ire != NULL) {
7031 		ire_refrele(gw_ire);
7032 	}
7033 	if (ipif != NULL) {
7034 		/*
7035 		 * Save enough information so that we can recreate the IRE if
7036 		 * the interface goes down and then up.  The metrics associated
7037 		 * with the route will be saved as well when rts_setmetrics() is
7038 		 * called after the IRE has been created.  In the case where
7039 		 * memory cannot be allocated, none of this information will be
7040 		 * saved.
7041 		 */
7042 		ipif_save_ire(ipif, ire);
7043 	}
7044 	if (ioctl_msg)
7045 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7046 	if (ire_arg != NULL) {
7047 		/*
7048 		 * Store the ire that was successfully added into where ire_arg
7049 		 * points to so that callers don't have to look it up
7050 		 * themselves (but they are responsible for ire_refrele()ing
7051 		 * the ire when they are finished with it).
7052 		 */
7053 		*ire_arg = ire;
7054 	} else {
7055 		ire_refrele(ire);		/* Held in ire_add */
7056 	}
7057 	if (ipif_refheld)
7058 		ipif_refrele(ipif);
7059 	return (0);
7060 }
7061 
7062 /*
7063  * ip_rt_delete is called to delete an IPv4 route.
7064  * ipif_arg is passed in to associate it with the correct interface.
7065  * We may need to restart this operation if the ipif cannot be looked up
7066  * due to an exclusive operation that is currently in progress. The restart
7067  * entry point is specified by 'func'
7068  */
7069 /* ARGSUSED4 */
7070 int
7071 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7072     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7073     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7074 {
7075 	ire_t	*ire = NULL;
7076 	ipif_t	*ipif;
7077 	boolean_t ipif_refheld = B_FALSE;
7078 	uint_t	type;
7079 	uint_t	match_flags = MATCH_IRE_TYPE;
7080 	int	err = 0;
7081 
7082 	ip1dbg(("ip_rt_delete:"));
7083 	/*
7084 	 * If this is the case of RTF_HOST being set, then we set the netmask
7085 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7086 	 */
7087 	if (flags & RTF_HOST) {
7088 		mask = IP_HOST_MASK;
7089 		match_flags |= MATCH_IRE_MASK;
7090 	} else if (rtm_addrs & RTA_NETMASK) {
7091 		match_flags |= MATCH_IRE_MASK;
7092 	}
7093 
7094 	/*
7095 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7096 	 * we check if the gateway address is one of our interfaces first,
7097 	 * and fall back on RTF_GATEWAY routes.
7098 	 *
7099 	 * This makes it possible to delete an original
7100 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7101 	 *
7102 	 * As the interface index specified with the RTA_IFP sockaddr is the
7103 	 * same for all ipif's off of an ill, the matching logic below uses
7104 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7105 	 * sharing the same prefix and interface index as the the route
7106 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7107 	 * is specified in the request.
7108 	 *
7109 	 * On the other hand, since the gateway address will usually be
7110 	 * different for each ipif on the system, the matching logic
7111 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7112 	 * route.  This means that interface routes for the same prefix can be
7113 	 * uniquely identified if they belong to distinct ipif's and if a
7114 	 * RTA_IFP sockaddr is not present.
7115 	 *
7116 	 * For more detail on specifying routes by gateway address and by
7117 	 * interface index, see the comments in ip_rt_add().
7118 	 */
7119 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7120 	    ipst);
7121 	if (ipif != NULL)
7122 		ipif_refheld = B_TRUE;
7123 	else if (err == EINPROGRESS)
7124 		return (err);
7125 	else
7126 		err = 0;
7127 	if (ipif != NULL) {
7128 		if (ipif_arg != NULL) {
7129 			if (ipif_refheld) {
7130 				ipif_refrele(ipif);
7131 				ipif_refheld = B_FALSE;
7132 			}
7133 			ipif = ipif_arg;
7134 			match_flags |= MATCH_IRE_ILL;
7135 		} else {
7136 			match_flags |= MATCH_IRE_IPIF;
7137 		}
7138 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7139 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7140 			    ALL_ZONES, NULL, match_flags, ipst);
7141 		}
7142 		if (ire == NULL) {
7143 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7144 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7145 			    match_flags, ipst);
7146 		}
7147 	}
7148 
7149 	if (ire == NULL) {
7150 		/*
7151 		 * At this point, the gateway address is not one of our own
7152 		 * addresses or a matching interface route was not found.  We
7153 		 * set the IRE type to lookup based on whether
7154 		 * this is a host route, a default route or just a prefix.
7155 		 *
7156 		 * If an ipif_arg was passed in, then the lookup is based on an
7157 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7158 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7159 		 * set as the route being looked up is not a traditional
7160 		 * interface route.
7161 		 */
7162 		match_flags &= ~MATCH_IRE_IPIF;
7163 		match_flags |= MATCH_IRE_GW;
7164 		if (ipif_arg != NULL)
7165 			match_flags |= MATCH_IRE_ILL;
7166 		if (mask == IP_HOST_MASK)
7167 			type = IRE_HOST;
7168 		else if (mask == 0)
7169 			type = IRE_DEFAULT;
7170 		else
7171 			type = IRE_PREFIX;
7172 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7173 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7174 	}
7175 
7176 	if (ipif_refheld)
7177 		ipif_refrele(ipif);
7178 
7179 	/* ipif is not refheld anymore */
7180 	if (ire == NULL)
7181 		return (ESRCH);
7182 
7183 	if (ire->ire_flags & RTF_MULTIRT) {
7184 		/*
7185 		 * Invoke the CGTP (multirouting) filtering module
7186 		 * to remove the dst address from the filtering database.
7187 		 * Packets coming from that address will no longer be
7188 		 * filtered to remove duplicates.
7189 		 */
7190 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7191 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7192 			    ipst->ips_netstack->netstack_stackid,
7193 			    ire->ire_addr, ire->ire_gateway_addr);
7194 		}
7195 		ip_cgtp_bcast_delete(ire, ipst);
7196 	}
7197 
7198 	ipif = ire->ire_ipif;
7199 	if (ipif != NULL)
7200 		ipif_remove_ire(ipif, ire);
7201 	if (ioctl_msg)
7202 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7203 	ire_delete(ire);
7204 	ire_refrele(ire);
7205 	return (err);
7206 }
7207 
7208 /*
7209  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7210  */
7211 /* ARGSUSED */
7212 int
7213 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7214     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7215 {
7216 	ipaddr_t dst_addr;
7217 	ipaddr_t gw_addr;
7218 	ipaddr_t mask;
7219 	int error = 0;
7220 	mblk_t *mp1;
7221 	struct rtentry *rt;
7222 	ipif_t *ipif = NULL;
7223 	ip_stack_t	*ipst;
7224 
7225 	ASSERT(q->q_next == NULL);
7226 	ipst = CONNQ_TO_IPST(q);
7227 
7228 	ip1dbg(("ip_siocaddrt:"));
7229 	/* Existence of mp1 verified in ip_wput_nondata */
7230 	mp1 = mp->b_cont->b_cont;
7231 	rt = (struct rtentry *)mp1->b_rptr;
7232 
7233 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7234 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7235 
7236 	/*
7237 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7238 	 * to a particular host address.  In this case, we set the netmask to
7239 	 * all ones for the particular destination address.  Otherwise,
7240 	 * determine the netmask to be used based on dst_addr and the interfaces
7241 	 * in use.
7242 	 */
7243 	if (rt->rt_flags & RTF_HOST) {
7244 		mask = IP_HOST_MASK;
7245 	} else {
7246 		/*
7247 		 * Note that ip_subnet_mask returns a zero mask in the case of
7248 		 * default (an all-zeroes address).
7249 		 */
7250 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7251 	}
7252 
7253 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7254 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7255 	if (ipif != NULL)
7256 		ipif_refrele(ipif);
7257 	return (error);
7258 }
7259 
7260 /*
7261  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7262  */
7263 /* ARGSUSED */
7264 int
7265 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7266     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7267 {
7268 	ipaddr_t dst_addr;
7269 	ipaddr_t gw_addr;
7270 	ipaddr_t mask;
7271 	int error;
7272 	mblk_t *mp1;
7273 	struct rtentry *rt;
7274 	ipif_t *ipif = NULL;
7275 	ip_stack_t	*ipst;
7276 
7277 	ASSERT(q->q_next == NULL);
7278 	ipst = CONNQ_TO_IPST(q);
7279 
7280 	ip1dbg(("ip_siocdelrt:"));
7281 	/* Existence of mp1 verified in ip_wput_nondata */
7282 	mp1 = mp->b_cont->b_cont;
7283 	rt = (struct rtentry *)mp1->b_rptr;
7284 
7285 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7286 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7287 
7288 	/*
7289 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7290 	 * to a particular host address.  In this case, we set the netmask to
7291 	 * all ones for the particular destination address.  Otherwise,
7292 	 * determine the netmask to be used based on dst_addr and the interfaces
7293 	 * in use.
7294 	 */
7295 	if (rt->rt_flags & RTF_HOST) {
7296 		mask = IP_HOST_MASK;
7297 	} else {
7298 		/*
7299 		 * Note that ip_subnet_mask returns a zero mask in the case of
7300 		 * default (an all-zeroes address).
7301 		 */
7302 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7303 	}
7304 
7305 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7306 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7307 	    mp, ip_process_ioctl, ipst);
7308 	if (ipif != NULL)
7309 		ipif_refrele(ipif);
7310 	return (error);
7311 }
7312 
7313 /*
7314  * Enqueue the mp onto the ipsq, chained by b_next.
7315  * b_prev stores the function to be executed later, and b_queue the queue
7316  * where this mp originated.
7317  */
7318 void
7319 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7320     ill_t *pending_ill)
7321 {
7322 	conn_t	*connp;
7323 	ipxop_t *ipx = ipsq->ipsq_xop;
7324 
7325 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7326 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7327 	ASSERT(func != NULL);
7328 
7329 	mp->b_queue = q;
7330 	mp->b_prev = (void *)func;
7331 	mp->b_next = NULL;
7332 
7333 	switch (type) {
7334 	case CUR_OP:
7335 		if (ipx->ipx_mptail != NULL) {
7336 			ASSERT(ipx->ipx_mphead != NULL);
7337 			ipx->ipx_mptail->b_next = mp;
7338 		} else {
7339 			ASSERT(ipx->ipx_mphead == NULL);
7340 			ipx->ipx_mphead = mp;
7341 		}
7342 		ipx->ipx_mptail = mp;
7343 		break;
7344 
7345 	case NEW_OP:
7346 		if (ipsq->ipsq_xopq_mptail != NULL) {
7347 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7348 			ipsq->ipsq_xopq_mptail->b_next = mp;
7349 		} else {
7350 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7351 			ipsq->ipsq_xopq_mphead = mp;
7352 		}
7353 		ipsq->ipsq_xopq_mptail = mp;
7354 		ipx->ipx_ipsq_queued = B_TRUE;
7355 		break;
7356 
7357 	case SWITCH_OP:
7358 		ASSERT(ipsq->ipsq_swxop != NULL);
7359 		/* only one switch operation is currently allowed */
7360 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7361 		ipsq->ipsq_switch_mp = mp;
7362 		ipx->ipx_ipsq_queued = B_TRUE;
7363 		break;
7364 	default:
7365 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7366 	}
7367 
7368 	if (CONN_Q(q) && pending_ill != NULL) {
7369 		connp = Q_TO_CONN(q);
7370 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7371 		connp->conn_oper_pending_ill = pending_ill;
7372 	}
7373 }
7374 
7375 /*
7376  * Dequeue the next message that requested exclusive access to this IPSQ's
7377  * xop.  Specifically:
7378  *
7379  *  1. If we're still processing the current operation on `ipsq', then
7380  *     dequeue the next message for the operation (from ipx_mphead), or
7381  *     return NULL if there are no queued messages for the operation.
7382  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7383  *
7384  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7385  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7386  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7387  *     leaving IPMP groups and require a careful dance -- see the comments
7388  *     in-line below for details.  If we're leaving a group xop or if we're
7389  *     joining a group xop and become writer on it, then we proceed to (3).
7390  *     Otherwise, we return NULL and exit the xop.
7391  *
7392  *  3. For each IPSQ in the xop, return any switch operation stored on
7393  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7394  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7395  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7396  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7397  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7398  *     each phyint in the group, including the IPMP meta-interface phyint.
7399  */
7400 static mblk_t *
7401 ipsq_dq(ipsq_t *ipsq)
7402 {
7403 	ill_t	*illv4, *illv6;
7404 	mblk_t	*mp;
7405 	ipsq_t	*xopipsq;
7406 	ipsq_t	*leftipsq = NULL;
7407 	ipxop_t *ipx;
7408 	phyint_t *phyi = ipsq->ipsq_phyint;
7409 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7410 	boolean_t emptied = B_FALSE;
7411 
7412 	/*
7413 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7414 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7415 	 */
7416 	rw_enter(&ipst->ips_ill_g_lock,
7417 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7418 	mutex_enter(&ipsq->ipsq_lock);
7419 	ipx = ipsq->ipsq_xop;
7420 	mutex_enter(&ipx->ipx_lock);
7421 
7422 	/*
7423 	 * Dequeue the next message associated with the current exclusive
7424 	 * operation, if any.
7425 	 */
7426 	if ((mp = ipx->ipx_mphead) != NULL) {
7427 		ipx->ipx_mphead = mp->b_next;
7428 		if (ipx->ipx_mphead == NULL)
7429 			ipx->ipx_mptail = NULL;
7430 		mp->b_next = (void *)ipsq;
7431 		goto out;
7432 	}
7433 
7434 	if (ipx->ipx_current_ipif != NULL)
7435 		goto empty;
7436 
7437 	if (ipsq->ipsq_swxop != NULL) {
7438 		/*
7439 		 * The exclusive operation that is now being completed has
7440 		 * requested a switch to a different xop.  This happens
7441 		 * when an interface joins or leaves an IPMP group.  Joins
7442 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7443 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7444 		 * (phyint_free()), or interface plumb for an ill type
7445 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7446 		 *
7447 		 * Xop switches are not allowed on the IPMP meta-interface.
7448 		 */
7449 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7450 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7451 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7452 
7453 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7454 			/*
7455 			 * We're switching back to our own xop, so we have two
7456 			 * xop's to drain/exit: our own, and the group xop
7457 			 * that we are leaving.
7458 			 *
7459 			 * First, pull ourselves out of the group ipsq list.
7460 			 * This is safe since we're writer on ill_g_lock.
7461 			 */
7462 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7463 
7464 			xopipsq = ipx->ipx_ipsq;
7465 			while (xopipsq->ipsq_next != ipsq)
7466 				xopipsq = xopipsq->ipsq_next;
7467 
7468 			xopipsq->ipsq_next = ipsq->ipsq_next;
7469 			ipsq->ipsq_next = ipsq;
7470 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7471 			ipsq->ipsq_swxop = NULL;
7472 
7473 			/*
7474 			 * Second, prepare to exit the group xop.  The actual
7475 			 * ipsq_exit() is done at the end of this function
7476 			 * since we cannot hold any locks across ipsq_exit().
7477 			 * Note that although we drop the group's ipx_lock, no
7478 			 * threads can proceed since we're still ipx_writer.
7479 			 */
7480 			leftipsq = xopipsq;
7481 			mutex_exit(&ipx->ipx_lock);
7482 
7483 			/*
7484 			 * Third, set ipx to point to our own xop (which was
7485 			 * inactive and therefore can be entered).
7486 			 */
7487 			ipx = ipsq->ipsq_xop;
7488 			mutex_enter(&ipx->ipx_lock);
7489 			ASSERT(ipx->ipx_writer == NULL);
7490 			ASSERT(ipx->ipx_current_ipif == NULL);
7491 		} else {
7492 			/*
7493 			 * We're switching from our own xop to a group xop.
7494 			 * The requestor of the switch must ensure that the
7495 			 * group xop cannot go away (e.g. by ensuring the
7496 			 * phyint associated with the xop cannot go away).
7497 			 *
7498 			 * If we can become writer on our new xop, then we'll
7499 			 * do the drain.  Otherwise, the current writer of our
7500 			 * new xop will do the drain when it exits.
7501 			 *
7502 			 * First, splice ourselves into the group IPSQ list.
7503 			 * This is safe since we're writer on ill_g_lock.
7504 			 */
7505 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7506 
7507 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7508 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7509 				xopipsq = xopipsq->ipsq_next;
7510 
7511 			xopipsq->ipsq_next = ipsq;
7512 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7513 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7514 			ipsq->ipsq_swxop = NULL;
7515 
7516 			/*
7517 			 * Second, exit our own xop, since it's now unused.
7518 			 * This is safe since we've got the only reference.
7519 			 */
7520 			ASSERT(ipx->ipx_writer == curthread);
7521 			ipx->ipx_writer = NULL;
7522 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7523 			ipx->ipx_ipsq_queued = B_FALSE;
7524 			mutex_exit(&ipx->ipx_lock);
7525 
7526 			/*
7527 			 * Third, set ipx to point to our new xop, and check
7528 			 * if we can become writer on it.  If we cannot, then
7529 			 * the current writer will drain the IPSQ group when
7530 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7531 			 * because we're still holding ipsq_lock.
7532 			 */
7533 			ipx = ipsq->ipsq_xop;
7534 			mutex_enter(&ipx->ipx_lock);
7535 			if (ipx->ipx_writer != NULL ||
7536 			    ipx->ipx_current_ipif != NULL) {
7537 				goto out;
7538 			}
7539 		}
7540 
7541 		/*
7542 		 * Fourth, become writer on our new ipx before we continue
7543 		 * with the drain.  Note that we never dropped ipsq_lock
7544 		 * above, so no other thread could've raced with us to
7545 		 * become writer first.  Also, we're holding ipx_lock, so
7546 		 * no other thread can examine the ipx right now.
7547 		 */
7548 		ASSERT(ipx->ipx_current_ipif == NULL);
7549 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7550 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7551 		ipx->ipx_writer = curthread;
7552 		ipx->ipx_forced = B_FALSE;
7553 #ifdef DEBUG
7554 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7555 #endif
7556 	}
7557 
7558 	xopipsq = ipsq;
7559 	do {
7560 		/*
7561 		 * So that other operations operate on a consistent and
7562 		 * complete phyint, a switch message on an IPSQ must be
7563 		 * handled prior to any other operations on that IPSQ.
7564 		 */
7565 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7566 			xopipsq->ipsq_switch_mp = NULL;
7567 			ASSERT(mp->b_next == NULL);
7568 			mp->b_next = (void *)xopipsq;
7569 			goto out;
7570 		}
7571 
7572 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7573 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7574 			if (xopipsq->ipsq_xopq_mphead == NULL)
7575 				xopipsq->ipsq_xopq_mptail = NULL;
7576 			mp->b_next = (void *)xopipsq;
7577 			goto out;
7578 		}
7579 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7580 empty:
7581 	/*
7582 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7583 	 * new messages can end up on any IPSQ in the xop.
7584 	 */
7585 	ipx->ipx_writer = NULL;
7586 	ipx->ipx_forced = B_FALSE;
7587 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7588 	ipx->ipx_ipsq_queued = B_FALSE;
7589 	emptied = B_TRUE;
7590 #ifdef	DEBUG
7591 	ipx->ipx_depth = 0;
7592 #endif
7593 out:
7594 	mutex_exit(&ipx->ipx_lock);
7595 	mutex_exit(&ipsq->ipsq_lock);
7596 
7597 	/*
7598 	 * If we completely emptied the xop, then wake up any threads waiting
7599 	 * to enter any of the IPSQ's associated with it.
7600 	 */
7601 	if (emptied) {
7602 		xopipsq = ipsq;
7603 		do {
7604 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7605 				continue;
7606 
7607 			illv4 = phyi->phyint_illv4;
7608 			illv6 = phyi->phyint_illv6;
7609 
7610 			GRAB_ILL_LOCKS(illv4, illv6);
7611 			if (illv4 != NULL)
7612 				cv_broadcast(&illv4->ill_cv);
7613 			if (illv6 != NULL)
7614 				cv_broadcast(&illv6->ill_cv);
7615 			RELEASE_ILL_LOCKS(illv4, illv6);
7616 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7617 	}
7618 	rw_exit(&ipst->ips_ill_g_lock);
7619 
7620 	/*
7621 	 * Now that all locks are dropped, exit the IPSQ we left.
7622 	 */
7623 	if (leftipsq != NULL)
7624 		ipsq_exit(leftipsq);
7625 
7626 	return (mp);
7627 }
7628 
7629 /*
7630  * Enter the ipsq corresponding to ill, by waiting synchronously till
7631  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7632  * will have to drain completely before ipsq_enter returns success.
7633  * ipx_current_ipif will be set if some exclusive op is in progress,
7634  * and the ipsq_exit logic will start the next enqueued op after
7635  * completion of the current op. If 'force' is used, we don't wait
7636  * for the enqueued ops. This is needed when a conn_close wants to
7637  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7638  * of an ill can also use this option. But we dont' use it currently.
7639  */
7640 #define	ENTER_SQ_WAIT_TICKS 100
7641 boolean_t
7642 ipsq_enter(ill_t *ill, boolean_t force, int type)
7643 {
7644 	ipsq_t	*ipsq;
7645 	ipxop_t *ipx;
7646 	boolean_t waited_enough = B_FALSE;
7647 
7648 	/*
7649 	 * Note that the relationship between ill and ipsq is fixed as long as
7650 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7651 	 * relationship between the IPSQ and xop cannot change.  However,
7652 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7653 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7654 	 * waking up all ills in the xop when it becomes available.
7655 	 */
7656 	mutex_enter(&ill->ill_lock);
7657 	for (;;) {
7658 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7659 			mutex_exit(&ill->ill_lock);
7660 			return (B_FALSE);
7661 		}
7662 
7663 		ipsq = ill->ill_phyint->phyint_ipsq;
7664 		mutex_enter(&ipsq->ipsq_lock);
7665 		ipx = ipsq->ipsq_xop;
7666 		mutex_enter(&ipx->ipx_lock);
7667 
7668 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7669 		    ipx->ipx_current_ipif == NULL || waited_enough))
7670 			break;
7671 
7672 		if (!force || ipx->ipx_writer != NULL) {
7673 			mutex_exit(&ipx->ipx_lock);
7674 			mutex_exit(&ipsq->ipsq_lock);
7675 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7676 		} else {
7677 			mutex_exit(&ipx->ipx_lock);
7678 			mutex_exit(&ipsq->ipsq_lock);
7679 			(void) cv_timedwait(&ill->ill_cv,
7680 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7681 			waited_enough = B_TRUE;
7682 		}
7683 	}
7684 
7685 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7686 	ASSERT(ipx->ipx_reentry_cnt == 0);
7687 	ipx->ipx_writer = curthread;
7688 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7689 	ipx->ipx_reentry_cnt++;
7690 #ifdef DEBUG
7691 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7692 #endif
7693 	mutex_exit(&ipx->ipx_lock);
7694 	mutex_exit(&ipsq->ipsq_lock);
7695 	mutex_exit(&ill->ill_lock);
7696 	return (B_TRUE);
7697 }
7698 
7699 boolean_t
7700 ill_perim_enter(ill_t *ill)
7701 {
7702 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7703 }
7704 
7705 void
7706 ill_perim_exit(ill_t *ill)
7707 {
7708 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7709 }
7710 
7711 /*
7712  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7713  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7714  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7715  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7716  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7717  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7718  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7719  * up the interface) and are enqueued in ipx_mphead.
7720  *
7721  * If a thread does not want to reenter the ipsq when it is already writer,
7722  * it must make sure that the specified reentry point to be called later
7723  * when the ipsq is empty, nor any code path starting from the specified reentry
7724  * point must never ever try to enter the ipsq again. Otherwise it can lead
7725  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7726  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7727  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7728  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7729  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7730  * ioctl if the current ioctl has completed. If the current ioctl is still
7731  * in progress it simply returns. The current ioctl could be waiting for
7732  * a response from another module (arp or the driver or could be waiting for
7733  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7734  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7735  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7736  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7737  * all associated DLPI operations have completed.
7738  */
7739 
7740 /*
7741  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7742  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7743  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7744  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7745  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7746  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7747  */
7748 ipsq_t *
7749 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7750     ipsq_func_t func, int type, boolean_t reentry_ok)
7751 {
7752 	ipsq_t	*ipsq;
7753 	ipxop_t	*ipx;
7754 
7755 	/* Only 1 of ipif or ill can be specified */
7756 	ASSERT((ipif != NULL) ^ (ill != NULL));
7757 	if (ipif != NULL)
7758 		ill = ipif->ipif_ill;
7759 
7760 	/*
7761 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7762 	 * ipx of an ipsq can't change when ipsq_lock is held.
7763 	 */
7764 	GRAB_CONN_LOCK(q);
7765 	mutex_enter(&ill->ill_lock);
7766 	ipsq = ill->ill_phyint->phyint_ipsq;
7767 	mutex_enter(&ipsq->ipsq_lock);
7768 	ipx = ipsq->ipsq_xop;
7769 	mutex_enter(&ipx->ipx_lock);
7770 
7771 	/*
7772 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7773 	 *    (Note: If the caller does not specify reentry_ok then neither
7774 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7775 	 *    again. Otherwise it can lead to an infinite loop
7776 	 * 2. Enter the ipsq if there is no current writer and this attempted
7777 	 *    entry is part of the current operation
7778 	 * 3. Enter the ipsq if there is no current writer and this is a new
7779 	 *    operation and the operation queue is empty and there is no
7780 	 *    operation currently in progress
7781 	 */
7782 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7783 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7784 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7785 		/* Success. */
7786 		ipx->ipx_reentry_cnt++;
7787 		ipx->ipx_writer = curthread;
7788 		ipx->ipx_forced = B_FALSE;
7789 		mutex_exit(&ipx->ipx_lock);
7790 		mutex_exit(&ipsq->ipsq_lock);
7791 		mutex_exit(&ill->ill_lock);
7792 		RELEASE_CONN_LOCK(q);
7793 #ifdef DEBUG
7794 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7795 #endif
7796 		return (ipsq);
7797 	}
7798 
7799 	if (func != NULL)
7800 		ipsq_enq(ipsq, q, mp, func, type, ill);
7801 
7802 	mutex_exit(&ipx->ipx_lock);
7803 	mutex_exit(&ipsq->ipsq_lock);
7804 	mutex_exit(&ill->ill_lock);
7805 	RELEASE_CONN_LOCK(q);
7806 	return (NULL);
7807 }
7808 
7809 /*
7810  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7811  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7812  * cannot be entered, the mp is queued for completion.
7813  */
7814 void
7815 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7816     boolean_t reentry_ok)
7817 {
7818 	ipsq_t	*ipsq;
7819 
7820 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7821 
7822 	/*
7823 	 * Drop the caller's refhold on the ill.  This is safe since we either
7824 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7825 	 * IPSQ, in which case we return without accessing ill anymore.  This
7826 	 * is needed because func needs to see the correct refcount.
7827 	 * e.g. removeif can work only then.
7828 	 */
7829 	ill_refrele(ill);
7830 	if (ipsq != NULL) {
7831 		(*func)(ipsq, q, mp, NULL);
7832 		ipsq_exit(ipsq);
7833 	}
7834 }
7835 
7836 /*
7837  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7838  * prior to exiting.  Caller must be writer on the specified IPSQ.
7839  */
7840 void
7841 ipsq_exit(ipsq_t *ipsq)
7842 {
7843 	mblk_t *mp;
7844 	ipsq_t *mp_ipsq;
7845 	queue_t	*q;
7846 	phyint_t *phyi;
7847 	ipsq_func_t func;
7848 
7849 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7850 
7851 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7852 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7853 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7854 		return;
7855 	}
7856 
7857 	for (;;) {
7858 		phyi = ipsq->ipsq_phyint;
7859 		mp = ipsq_dq(ipsq);
7860 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7861 
7862 		/*
7863 		 * If we've changed to a new IPSQ, and the phyint associated
7864 		 * with the old one has gone away, free the old IPSQ.  Note
7865 		 * that this cannot happen while the IPSQ is in a group.
7866 		 */
7867 		if (mp_ipsq != ipsq && phyi == NULL) {
7868 			ASSERT(ipsq->ipsq_next == ipsq);
7869 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7870 			ipsq_delete(ipsq);
7871 		}
7872 
7873 		if (mp == NULL)
7874 			break;
7875 
7876 		q = mp->b_queue;
7877 		func = (ipsq_func_t)mp->b_prev;
7878 		ipsq = mp_ipsq;
7879 		mp->b_next = mp->b_prev = NULL;
7880 		mp->b_queue = NULL;
7881 
7882 		/*
7883 		 * If 'q' is an conn queue, it is valid, since we did a
7884 		 * a refhold on the conn at the start of the ioctl.
7885 		 * If 'q' is an ill queue, it is valid, since close of an
7886 		 * ill will clean up its IPSQ.
7887 		 */
7888 		(*func)(ipsq, q, mp, NULL);
7889 	}
7890 }
7891 
7892 /*
7893  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7894  * and `ioccmd'.
7895  */
7896 void
7897 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7898 {
7899 	ill_t *ill = ipif->ipif_ill;
7900 	ipxop_t *ipx = ipsq->ipsq_xop;
7901 
7902 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7903 	ASSERT(ipx->ipx_current_ipif == NULL);
7904 	ASSERT(ipx->ipx_current_ioctl == 0);
7905 
7906 	ipx->ipx_current_done = B_FALSE;
7907 	ipx->ipx_current_ioctl = ioccmd;
7908 	mutex_enter(&ipx->ipx_lock);
7909 	ipx->ipx_current_ipif = ipif;
7910 	mutex_exit(&ipx->ipx_lock);
7911 
7912 	/*
7913 	 * Set IPIF_CHANGING on one or more ipifs associated with the
7914 	 * current exclusive operation.  IPIF_CHANGING prevents any new
7915 	 * references to the ipif (so that the references will eventually
7916 	 * drop to zero) and also prevents any "get" operations (e.g.,
7917 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
7918 	 * operation has completed and the ipif is again in a stable state.
7919 	 *
7920 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
7921 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
7922 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
7923 	 * ipifs will be affected.
7924 	 *
7925 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
7926 	 * IPIF_CONDEMNED internally after identifying the right ipif to
7927 	 * operate on.
7928 	 */
7929 	switch (ioccmd) {
7930 	case SIOCLIFREMOVEIF:
7931 		break;
7932 	case 0:
7933 		mutex_enter(&ill->ill_lock);
7934 		ipif = ipif->ipif_ill->ill_ipif;
7935 		for (; ipif != NULL; ipif = ipif->ipif_next)
7936 			ipif->ipif_state_flags |= IPIF_CHANGING;
7937 		mutex_exit(&ill->ill_lock);
7938 		break;
7939 	default:
7940 		mutex_enter(&ill->ill_lock);
7941 		ipif->ipif_state_flags |= IPIF_CHANGING;
7942 		mutex_exit(&ill->ill_lock);
7943 	}
7944 }
7945 
7946 /*
7947  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
7948  * the next exclusive operation to begin once we ipsq_exit().  However, if
7949  * pending DLPI operations remain, then we will wait for the queue to drain
7950  * before allowing the next exclusive operation to begin.  This ensures that
7951  * DLPI operations from one exclusive operation are never improperly processed
7952  * as part of a subsequent exclusive operation.
7953  */
7954 void
7955 ipsq_current_finish(ipsq_t *ipsq)
7956 {
7957 	ipxop_t	*ipx = ipsq->ipsq_xop;
7958 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
7959 	ipif_t	*ipif = ipx->ipx_current_ipif;
7960 
7961 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7962 
7963 	/*
7964 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
7965 	 * (but in that case, IPIF_CHANGING will already be clear and no
7966 	 * pending DLPI messages can remain).
7967 	 */
7968 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
7969 		ill_t *ill = ipif->ipif_ill;
7970 
7971 		mutex_enter(&ill->ill_lock);
7972 		dlpi_pending = ill->ill_dlpi_pending;
7973 		if (ipx->ipx_current_ioctl == 0) {
7974 			ipif = ill->ill_ipif;
7975 			for (; ipif != NULL; ipif = ipif->ipif_next)
7976 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
7977 		} else {
7978 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
7979 		}
7980 		mutex_exit(&ill->ill_lock);
7981 	}
7982 
7983 	ASSERT(!ipx->ipx_current_done);
7984 	ipx->ipx_current_done = B_TRUE;
7985 	ipx->ipx_current_ioctl = 0;
7986 	if (dlpi_pending == DL_PRIM_INVAL) {
7987 		mutex_enter(&ipx->ipx_lock);
7988 		ipx->ipx_current_ipif = NULL;
7989 		mutex_exit(&ipx->ipx_lock);
7990 	}
7991 }
7992 
7993 /*
7994  * The ill is closing. Flush all messages on the ipsq that originated
7995  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7996  * for this ill since ipsq_enter could not have entered until then.
7997  * New messages can't be queued since the CONDEMNED flag is set.
7998  */
7999 static void
8000 ipsq_flush(ill_t *ill)
8001 {
8002 	queue_t	*q;
8003 	mblk_t	*prev;
8004 	mblk_t	*mp;
8005 	mblk_t	*mp_next;
8006 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
8007 
8008 	ASSERT(IAM_WRITER_ILL(ill));
8009 
8010 	/*
8011 	 * Flush any messages sent up by the driver.
8012 	 */
8013 	mutex_enter(&ipx->ipx_lock);
8014 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
8015 		mp_next = mp->b_next;
8016 		q = mp->b_queue;
8017 		if (q == ill->ill_rq || q == ill->ill_wq) {
8018 			/* dequeue mp */
8019 			if (prev == NULL)
8020 				ipx->ipx_mphead = mp->b_next;
8021 			else
8022 				prev->b_next = mp->b_next;
8023 			if (ipx->ipx_mptail == mp) {
8024 				ASSERT(mp_next == NULL);
8025 				ipx->ipx_mptail = prev;
8026 			}
8027 			inet_freemsg(mp);
8028 		} else {
8029 			prev = mp;
8030 		}
8031 	}
8032 	mutex_exit(&ipx->ipx_lock);
8033 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8034 	ipsq_xopq_mp_cleanup(ill, NULL);
8035 	ill_pending_mp_cleanup(ill);
8036 }
8037 
8038 /*
8039  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8040  * refhold and return the associated ipif
8041  */
8042 /* ARGSUSED */
8043 int
8044 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8045     cmd_info_t *ci, ipsq_func_t func)
8046 {
8047 	boolean_t exists;
8048 	struct iftun_req *ta;
8049 	ipif_t  *ipif;
8050 	ill_t   *ill;
8051 	boolean_t isv6;
8052 	mblk_t  *mp1;
8053 	int error;
8054 	conn_t  *connp;
8055 	ip_stack_t  *ipst;
8056 
8057 	/* Existence verified in ip_wput_nondata */
8058 	mp1 = mp->b_cont->b_cont;
8059 	ta = (struct iftun_req *)mp1->b_rptr;
8060 	/*
8061 	 * Null terminate the string to protect against buffer
8062 	 * overrun. String was generated by user code and may not
8063 	 * be trusted.
8064 	 */
8065 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8066 
8067 	connp = Q_TO_CONN(q);
8068 	isv6 = connp->conn_af_isv6;
8069 	ipst = connp->conn_netstack->netstack_ip;
8070 
8071 	/* Disallows implicit create */
8072 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8073 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8074 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8075 	if (ipif == NULL)
8076 		return (error);
8077 
8078 	if (ipif->ipif_id != 0) {
8079 		/*
8080 		 * We really don't want to set/get tunnel parameters
8081 		 * on virtual tunnel interfaces.  Only allow the
8082 		 * base tunnel to do these.
8083 		 */
8084 		ipif_refrele(ipif);
8085 		return (EINVAL);
8086 	}
8087 
8088 	/*
8089 	 * Send down to tunnel mod for ioctl processing.
8090 	 * Will finish ioctl in ip_rput_other().
8091 	 */
8092 	ill = ipif->ipif_ill;
8093 	if (ill->ill_net_type == IRE_LOOPBACK) {
8094 		ipif_refrele(ipif);
8095 		return (EOPNOTSUPP);
8096 	}
8097 
8098 	if (ill->ill_wq == NULL) {
8099 		ipif_refrele(ipif);
8100 		return (ENXIO);
8101 	}
8102 	/*
8103 	 * Mark the ioctl as coming from an IPv6 interface for
8104 	 * tun's convenience.
8105 	 */
8106 	if (ill->ill_isv6)
8107 		ta->ifta_flags |= 0x80000000;
8108 	ci->ci_ipif = ipif;
8109 	return (0);
8110 }
8111 
8112 /*
8113  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8114  * and return the associated ipif.
8115  * Return value:
8116  *	Non zero: An error has occurred. ci may not be filled out.
8117  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8118  *	a held ipif in ci.ci_ipif.
8119  */
8120 int
8121 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8122     cmd_info_t *ci, ipsq_func_t func)
8123 {
8124 	char		*name;
8125 	struct ifreq    *ifr;
8126 	struct lifreq    *lifr;
8127 	ipif_t		*ipif = NULL;
8128 	ill_t		*ill;
8129 	conn_t		*connp;
8130 	boolean_t	isv6;
8131 	boolean_t	exists;
8132 	int		err;
8133 	mblk_t		*mp1;
8134 	zoneid_t	zoneid;
8135 	ip_stack_t	*ipst;
8136 
8137 	if (q->q_next != NULL) {
8138 		ill = (ill_t *)q->q_ptr;
8139 		isv6 = ill->ill_isv6;
8140 		connp = NULL;
8141 		zoneid = ALL_ZONES;
8142 		ipst = ill->ill_ipst;
8143 	} else {
8144 		ill = NULL;
8145 		connp = Q_TO_CONN(q);
8146 		isv6 = connp->conn_af_isv6;
8147 		zoneid = connp->conn_zoneid;
8148 		if (zoneid == GLOBAL_ZONEID) {
8149 			/* global zone can access ipifs in all zones */
8150 			zoneid = ALL_ZONES;
8151 		}
8152 		ipst = connp->conn_netstack->netstack_ip;
8153 	}
8154 
8155 	/* Has been checked in ip_wput_nondata */
8156 	mp1 = mp->b_cont->b_cont;
8157 
8158 	if (ipip->ipi_cmd_type == IF_CMD) {
8159 		/* This a old style SIOC[GS]IF* command */
8160 		ifr = (struct ifreq *)mp1->b_rptr;
8161 		/*
8162 		 * Null terminate the string to protect against buffer
8163 		 * overrun. String was generated by user code and may not
8164 		 * be trusted.
8165 		 */
8166 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8167 		name = ifr->ifr_name;
8168 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8169 		ci->ci_sin6 = NULL;
8170 		ci->ci_lifr = (struct lifreq *)ifr;
8171 	} else {
8172 		/* This a new style SIOC[GS]LIF* command */
8173 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8174 		lifr = (struct lifreq *)mp1->b_rptr;
8175 		/*
8176 		 * Null terminate the string to protect against buffer
8177 		 * overrun. String was generated by user code and may not
8178 		 * be trusted.
8179 		 */
8180 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8181 		name = lifr->lifr_name;
8182 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8183 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8184 		ci->ci_lifr = lifr;
8185 	}
8186 
8187 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8188 		/*
8189 		 * The ioctl will be failed if the ioctl comes down
8190 		 * an conn stream
8191 		 */
8192 		if (ill == NULL) {
8193 			/*
8194 			 * Not an ill queue, return EINVAL same as the
8195 			 * old error code.
8196 			 */
8197 			return (ENXIO);
8198 		}
8199 		ipif = ill->ill_ipif;
8200 		ipif_refhold(ipif);
8201 	} else {
8202 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8203 		    &exists, isv6, zoneid,
8204 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8205 		    ipst);
8206 		if (ipif == NULL) {
8207 			if (err == EINPROGRESS)
8208 				return (err);
8209 			err = 0;	/* Ensure we don't use it below */
8210 		}
8211 	}
8212 
8213 	/*
8214 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8215 	 */
8216 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8217 		ipif_refrele(ipif);
8218 		return (ENXIO);
8219 	}
8220 
8221 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8222 	    name[0] == '\0') {
8223 		/*
8224 		 * Handle a or a SIOC?IF* with a null name
8225 		 * during plumb (on the ill queue before the I_PLINK).
8226 		 */
8227 		ipif = ill->ill_ipif;
8228 		ipif_refhold(ipif);
8229 	}
8230 
8231 	if (ipif == NULL)
8232 		return (ENXIO);
8233 
8234 	ci->ci_ipif = ipif;
8235 	return (0);
8236 }
8237 
8238 /*
8239  * Return the total number of ipifs.
8240  */
8241 static uint_t
8242 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8243 {
8244 	uint_t numifs = 0;
8245 	ill_t	*ill;
8246 	ill_walk_context_t	ctx;
8247 	ipif_t	*ipif;
8248 
8249 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8250 	ill = ILL_START_WALK_V4(&ctx, ipst);
8251 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8252 		if (IS_UNDER_IPMP(ill))
8253 			continue;
8254 		for (ipif = ill->ill_ipif; ipif != NULL;
8255 		    ipif = ipif->ipif_next) {
8256 			if (ipif->ipif_zoneid == zoneid ||
8257 			    ipif->ipif_zoneid == ALL_ZONES)
8258 				numifs++;
8259 		}
8260 	}
8261 	rw_exit(&ipst->ips_ill_g_lock);
8262 	return (numifs);
8263 }
8264 
8265 /*
8266  * Return the total number of ipifs.
8267  */
8268 static uint_t
8269 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8270 {
8271 	uint_t numifs = 0;
8272 	ill_t	*ill;
8273 	ipif_t	*ipif;
8274 	ill_walk_context_t	ctx;
8275 
8276 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8277 
8278 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8279 	if (family == AF_INET)
8280 		ill = ILL_START_WALK_V4(&ctx, ipst);
8281 	else if (family == AF_INET6)
8282 		ill = ILL_START_WALK_V6(&ctx, ipst);
8283 	else
8284 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8285 
8286 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8287 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8288 			continue;
8289 
8290 		for (ipif = ill->ill_ipif; ipif != NULL;
8291 		    ipif = ipif->ipif_next) {
8292 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8293 			    !(lifn_flags & LIFC_NOXMIT))
8294 				continue;
8295 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8296 			    !(lifn_flags & LIFC_TEMPORARY))
8297 				continue;
8298 			if (((ipif->ipif_flags &
8299 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8300 			    IPIF_DEPRECATED)) ||
8301 			    IS_LOOPBACK(ill) ||
8302 			    !(ipif->ipif_flags & IPIF_UP)) &&
8303 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8304 				continue;
8305 
8306 			if (zoneid != ipif->ipif_zoneid &&
8307 			    ipif->ipif_zoneid != ALL_ZONES &&
8308 			    (zoneid != GLOBAL_ZONEID ||
8309 			    !(lifn_flags & LIFC_ALLZONES)))
8310 				continue;
8311 
8312 			numifs++;
8313 		}
8314 	}
8315 	rw_exit(&ipst->ips_ill_g_lock);
8316 	return (numifs);
8317 }
8318 
8319 uint_t
8320 ip_get_lifsrcofnum(ill_t *ill)
8321 {
8322 	uint_t numifs = 0;
8323 	ill_t	*ill_head = ill;
8324 	ip_stack_t	*ipst = ill->ill_ipst;
8325 
8326 	/*
8327 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8328 	 * other thread may be trying to relink the ILLs in this usesrc group
8329 	 * and adjusting the ill_usesrc_grp_next pointers
8330 	 */
8331 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8332 	if ((ill->ill_usesrc_ifindex == 0) &&
8333 	    (ill->ill_usesrc_grp_next != NULL)) {
8334 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8335 		    ill = ill->ill_usesrc_grp_next)
8336 			numifs++;
8337 	}
8338 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8339 
8340 	return (numifs);
8341 }
8342 
8343 /* Null values are passed in for ipif, sin, and ifreq */
8344 /* ARGSUSED */
8345 int
8346 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8347     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8348 {
8349 	int *nump;
8350 	conn_t *connp = Q_TO_CONN(q);
8351 
8352 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8353 
8354 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8355 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8356 
8357 	*nump = ip_get_numifs(connp->conn_zoneid,
8358 	    connp->conn_netstack->netstack_ip);
8359 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8360 	return (0);
8361 }
8362 
8363 /* Null values are passed in for ipif, sin, and ifreq */
8364 /* ARGSUSED */
8365 int
8366 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8367     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8368 {
8369 	struct lifnum *lifn;
8370 	mblk_t	*mp1;
8371 	conn_t *connp = Q_TO_CONN(q);
8372 
8373 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8374 
8375 	/* Existence checked in ip_wput_nondata */
8376 	mp1 = mp->b_cont->b_cont;
8377 
8378 	lifn = (struct lifnum *)mp1->b_rptr;
8379 	switch (lifn->lifn_family) {
8380 	case AF_UNSPEC:
8381 	case AF_INET:
8382 	case AF_INET6:
8383 		break;
8384 	default:
8385 		return (EAFNOSUPPORT);
8386 	}
8387 
8388 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8389 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8390 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8391 	return (0);
8392 }
8393 
8394 /* ARGSUSED */
8395 int
8396 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8397     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8398 {
8399 	STRUCT_HANDLE(ifconf, ifc);
8400 	mblk_t *mp1;
8401 	struct iocblk *iocp;
8402 	struct ifreq *ifr;
8403 	ill_walk_context_t	ctx;
8404 	ill_t	*ill;
8405 	ipif_t	*ipif;
8406 	struct sockaddr_in *sin;
8407 	int32_t	ifclen;
8408 	zoneid_t zoneid;
8409 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8410 
8411 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8412 
8413 	ip1dbg(("ip_sioctl_get_ifconf"));
8414 	/* Existence verified in ip_wput_nondata */
8415 	mp1 = mp->b_cont->b_cont;
8416 	iocp = (struct iocblk *)mp->b_rptr;
8417 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8418 
8419 	/*
8420 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8421 	 * the user buffer address and length into which the list of struct
8422 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8423 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8424 	 * the SIOCGIFCONF operation was redefined to simply provide
8425 	 * a large output buffer into which we are supposed to jam the ifreq
8426 	 * array.  The same ioctl command code was used, despite the fact that
8427 	 * both the applications and the kernel code had to change, thus making
8428 	 * it impossible to support both interfaces.
8429 	 *
8430 	 * For reasons not good enough to try to explain, the following
8431 	 * algorithm is used for deciding what to do with one of these:
8432 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8433 	 * form with the output buffer coming down as the continuation message.
8434 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8435 	 * and we have to copy in the ifconf structure to find out how big the
8436 	 * output buffer is and where to copy out to.  Sure no problem...
8437 	 *
8438 	 */
8439 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8440 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8441 		int numifs = 0;
8442 		size_t ifc_bufsize;
8443 
8444 		/*
8445 		 * Must be (better be!) continuation of a TRANSPARENT
8446 		 * IOCTL.  We just copied in the ifconf structure.
8447 		 */
8448 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8449 		    (struct ifconf *)mp1->b_rptr);
8450 
8451 		/*
8452 		 * Allocate a buffer to hold requested information.
8453 		 *
8454 		 * If ifc_len is larger than what is needed, we only
8455 		 * allocate what we will use.
8456 		 *
8457 		 * If ifc_len is smaller than what is needed, return
8458 		 * EINVAL.
8459 		 *
8460 		 * XXX: the ill_t structure can hava 2 counters, for
8461 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8462 		 * number of interfaces for a device, so we don't need
8463 		 * to count them here...
8464 		 */
8465 		numifs = ip_get_numifs(zoneid, ipst);
8466 
8467 		ifclen = STRUCT_FGET(ifc, ifc_len);
8468 		ifc_bufsize = numifs * sizeof (struct ifreq);
8469 		if (ifc_bufsize > ifclen) {
8470 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8471 				/* old behaviour */
8472 				return (EINVAL);
8473 			} else {
8474 				ifc_bufsize = ifclen;
8475 			}
8476 		}
8477 
8478 		mp1 = mi_copyout_alloc(q, mp,
8479 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8480 		if (mp1 == NULL)
8481 			return (ENOMEM);
8482 
8483 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8484 	}
8485 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8486 	/*
8487 	 * the SIOCGIFCONF ioctl only knows about
8488 	 * IPv4 addresses, so don't try to tell
8489 	 * it about interfaces with IPv6-only
8490 	 * addresses. (Last parm 'isv6' is B_FALSE)
8491 	 */
8492 
8493 	ifr = (struct ifreq *)mp1->b_rptr;
8494 
8495 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8496 	ill = ILL_START_WALK_V4(&ctx, ipst);
8497 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8498 		if (IS_UNDER_IPMP(ill))
8499 			continue;
8500 		for (ipif = ill->ill_ipif; ipif != NULL;
8501 		    ipif = ipif->ipif_next) {
8502 			if (zoneid != ipif->ipif_zoneid &&
8503 			    ipif->ipif_zoneid != ALL_ZONES)
8504 				continue;
8505 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8506 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8507 					/* old behaviour */
8508 					rw_exit(&ipst->ips_ill_g_lock);
8509 					return (EINVAL);
8510 				} else {
8511 					goto if_copydone;
8512 				}
8513 			}
8514 			ipif_get_name(ipif, ifr->ifr_name,
8515 			    sizeof (ifr->ifr_name));
8516 			sin = (sin_t *)&ifr->ifr_addr;
8517 			*sin = sin_null;
8518 			sin->sin_family = AF_INET;
8519 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8520 			ifr++;
8521 		}
8522 	}
8523 if_copydone:
8524 	rw_exit(&ipst->ips_ill_g_lock);
8525 	mp1->b_wptr = (uchar_t *)ifr;
8526 
8527 	if (STRUCT_BUF(ifc) != NULL) {
8528 		STRUCT_FSET(ifc, ifc_len,
8529 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8530 	}
8531 	return (0);
8532 }
8533 
8534 /*
8535  * Get the interfaces using the address hosted on the interface passed in,
8536  * as a source adddress
8537  */
8538 /* ARGSUSED */
8539 int
8540 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8541     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8542 {
8543 	mblk_t *mp1;
8544 	ill_t	*ill, *ill_head;
8545 	ipif_t	*ipif, *orig_ipif;
8546 	int	numlifs = 0;
8547 	size_t	lifs_bufsize, lifsmaxlen;
8548 	struct	lifreq *lifr;
8549 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8550 	uint_t	ifindex;
8551 	zoneid_t zoneid;
8552 	int err = 0;
8553 	boolean_t isv6 = B_FALSE;
8554 	struct	sockaddr_in	*sin;
8555 	struct	sockaddr_in6	*sin6;
8556 	STRUCT_HANDLE(lifsrcof, lifs);
8557 	ip_stack_t		*ipst;
8558 
8559 	ipst = CONNQ_TO_IPST(q);
8560 
8561 	ASSERT(q->q_next == NULL);
8562 
8563 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8564 
8565 	/* Existence verified in ip_wput_nondata */
8566 	mp1 = mp->b_cont->b_cont;
8567 
8568 	/*
8569 	 * Must be (better be!) continuation of a TRANSPARENT
8570 	 * IOCTL.  We just copied in the lifsrcof structure.
8571 	 */
8572 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8573 	    (struct lifsrcof *)mp1->b_rptr);
8574 
8575 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8576 		return (EINVAL);
8577 
8578 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8579 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8580 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8581 	    ip_process_ioctl, &err, ipst);
8582 	if (ipif == NULL) {
8583 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8584 		    ifindex));
8585 		return (err);
8586 	}
8587 
8588 	/* Allocate a buffer to hold requested information */
8589 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8590 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8591 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8592 	/* The actual size needed is always returned in lifs_len */
8593 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8594 
8595 	/* If the amount we need is more than what is passed in, abort */
8596 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8597 		ipif_refrele(ipif);
8598 		return (0);
8599 	}
8600 
8601 	mp1 = mi_copyout_alloc(q, mp,
8602 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8603 	if (mp1 == NULL) {
8604 		ipif_refrele(ipif);
8605 		return (ENOMEM);
8606 	}
8607 
8608 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8609 	bzero(mp1->b_rptr, lifs_bufsize);
8610 
8611 	lifr = (struct lifreq *)mp1->b_rptr;
8612 
8613 	ill = ill_head = ipif->ipif_ill;
8614 	orig_ipif = ipif;
8615 
8616 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8617 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8618 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8619 
8620 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8621 	for (; (ill != NULL) && (ill != ill_head);
8622 	    ill = ill->ill_usesrc_grp_next) {
8623 
8624 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8625 			break;
8626 
8627 		ipif = ill->ill_ipif;
8628 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8629 		if (ipif->ipif_isv6) {
8630 			sin6 = (sin6_t *)&lifr->lifr_addr;
8631 			*sin6 = sin6_null;
8632 			sin6->sin6_family = AF_INET6;
8633 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8634 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8635 			    &ipif->ipif_v6net_mask);
8636 		} else {
8637 			sin = (sin_t *)&lifr->lifr_addr;
8638 			*sin = sin_null;
8639 			sin->sin_family = AF_INET;
8640 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8641 			lifr->lifr_addrlen = ip_mask_to_plen(
8642 			    ipif->ipif_net_mask);
8643 		}
8644 		lifr++;
8645 	}
8646 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8647 	rw_exit(&ipst->ips_ill_g_lock);
8648 	ipif_refrele(orig_ipif);
8649 	mp1->b_wptr = (uchar_t *)lifr;
8650 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8651 
8652 	return (0);
8653 }
8654 
8655 /* ARGSUSED */
8656 int
8657 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8658     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8659 {
8660 	mblk_t *mp1;
8661 	int	list;
8662 	ill_t	*ill;
8663 	ipif_t	*ipif;
8664 	int	flags;
8665 	int	numlifs = 0;
8666 	size_t	lifc_bufsize;
8667 	struct	lifreq *lifr;
8668 	sa_family_t	family;
8669 	struct	sockaddr_in	*sin;
8670 	struct	sockaddr_in6	*sin6;
8671 	ill_walk_context_t	ctx;
8672 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8673 	int32_t	lifclen;
8674 	zoneid_t zoneid;
8675 	STRUCT_HANDLE(lifconf, lifc);
8676 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8677 
8678 	ip1dbg(("ip_sioctl_get_lifconf"));
8679 
8680 	ASSERT(q->q_next == NULL);
8681 
8682 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8683 
8684 	/* Existence verified in ip_wput_nondata */
8685 	mp1 = mp->b_cont->b_cont;
8686 
8687 	/*
8688 	 * An extended version of SIOCGIFCONF that takes an
8689 	 * additional address family and flags field.
8690 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8691 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8692 	 * interfaces are omitted.
8693 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8694 	 * unless LIFC_TEMPORARY is specified.
8695 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8696 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8697 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8698 	 * has priority over LIFC_NOXMIT.
8699 	 */
8700 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8701 
8702 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8703 		return (EINVAL);
8704 
8705 	/*
8706 	 * Must be (better be!) continuation of a TRANSPARENT
8707 	 * IOCTL.  We just copied in the lifconf structure.
8708 	 */
8709 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8710 
8711 	family = STRUCT_FGET(lifc, lifc_family);
8712 	flags = STRUCT_FGET(lifc, lifc_flags);
8713 
8714 	switch (family) {
8715 	case AF_UNSPEC:
8716 		/*
8717 		 * walk all ILL's.
8718 		 */
8719 		list = MAX_G_HEADS;
8720 		break;
8721 	case AF_INET:
8722 		/*
8723 		 * walk only IPV4 ILL's.
8724 		 */
8725 		list = IP_V4_G_HEAD;
8726 		break;
8727 	case AF_INET6:
8728 		/*
8729 		 * walk only IPV6 ILL's.
8730 		 */
8731 		list = IP_V6_G_HEAD;
8732 		break;
8733 	default:
8734 		return (EAFNOSUPPORT);
8735 	}
8736 
8737 	/*
8738 	 * Allocate a buffer to hold requested information.
8739 	 *
8740 	 * If lifc_len is larger than what is needed, we only
8741 	 * allocate what we will use.
8742 	 *
8743 	 * If lifc_len is smaller than what is needed, return
8744 	 * EINVAL.
8745 	 */
8746 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8747 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8748 	lifclen = STRUCT_FGET(lifc, lifc_len);
8749 	if (lifc_bufsize > lifclen) {
8750 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8751 			return (EINVAL);
8752 		else
8753 			lifc_bufsize = lifclen;
8754 	}
8755 
8756 	mp1 = mi_copyout_alloc(q, mp,
8757 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8758 	if (mp1 == NULL)
8759 		return (ENOMEM);
8760 
8761 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8762 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8763 
8764 	lifr = (struct lifreq *)mp1->b_rptr;
8765 
8766 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8767 	ill = ill_first(list, list, &ctx, ipst);
8768 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8769 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8770 			continue;
8771 
8772 		for (ipif = ill->ill_ipif; ipif != NULL;
8773 		    ipif = ipif->ipif_next) {
8774 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8775 			    !(flags & LIFC_NOXMIT))
8776 				continue;
8777 
8778 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8779 			    !(flags & LIFC_TEMPORARY))
8780 				continue;
8781 
8782 			if (((ipif->ipif_flags &
8783 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8784 			    IPIF_DEPRECATED)) ||
8785 			    IS_LOOPBACK(ill) ||
8786 			    !(ipif->ipif_flags & IPIF_UP)) &&
8787 			    (flags & LIFC_EXTERNAL_SOURCE))
8788 				continue;
8789 
8790 			if (zoneid != ipif->ipif_zoneid &&
8791 			    ipif->ipif_zoneid != ALL_ZONES &&
8792 			    (zoneid != GLOBAL_ZONEID ||
8793 			    !(flags & LIFC_ALLZONES)))
8794 				continue;
8795 
8796 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8797 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8798 					rw_exit(&ipst->ips_ill_g_lock);
8799 					return (EINVAL);
8800 				} else {
8801 					goto lif_copydone;
8802 				}
8803 			}
8804 
8805 			ipif_get_name(ipif, lifr->lifr_name,
8806 			    sizeof (lifr->lifr_name));
8807 			lifr->lifr_type = ill->ill_type;
8808 			if (ipif->ipif_isv6) {
8809 				sin6 = (sin6_t *)&lifr->lifr_addr;
8810 				*sin6 = sin6_null;
8811 				sin6->sin6_family = AF_INET6;
8812 				sin6->sin6_addr =
8813 				    ipif->ipif_v6lcl_addr;
8814 				lifr->lifr_addrlen =
8815 				    ip_mask_to_plen_v6(
8816 				    &ipif->ipif_v6net_mask);
8817 			} else {
8818 				sin = (sin_t *)&lifr->lifr_addr;
8819 				*sin = sin_null;
8820 				sin->sin_family = AF_INET;
8821 				sin->sin_addr.s_addr =
8822 				    ipif->ipif_lcl_addr;
8823 				lifr->lifr_addrlen =
8824 				    ip_mask_to_plen(
8825 				    ipif->ipif_net_mask);
8826 			}
8827 			lifr++;
8828 		}
8829 	}
8830 lif_copydone:
8831 	rw_exit(&ipst->ips_ill_g_lock);
8832 
8833 	mp1->b_wptr = (uchar_t *)lifr;
8834 	if (STRUCT_BUF(lifc) != NULL) {
8835 		STRUCT_FSET(lifc, lifc_len,
8836 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8837 	}
8838 	return (0);
8839 }
8840 
8841 static void
8842 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8843 {
8844 	ip6_asp_t *table;
8845 	size_t table_size;
8846 	mblk_t *data_mp;
8847 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8848 	ip_stack_t	*ipst;
8849 
8850 	if (q->q_next == NULL)
8851 		ipst = CONNQ_TO_IPST(q);
8852 	else
8853 		ipst = ILLQ_TO_IPST(q);
8854 
8855 	/* These two ioctls are I_STR only */
8856 	if (iocp->ioc_count == TRANSPARENT) {
8857 		miocnak(q, mp, 0, EINVAL);
8858 		return;
8859 	}
8860 
8861 	data_mp = mp->b_cont;
8862 	if (data_mp == NULL) {
8863 		/* The user passed us a NULL argument */
8864 		table = NULL;
8865 		table_size = iocp->ioc_count;
8866 	} else {
8867 		/*
8868 		 * The user provided a table.  The stream head
8869 		 * may have copied in the user data in chunks,
8870 		 * so make sure everything is pulled up
8871 		 * properly.
8872 		 */
8873 		if (MBLKL(data_mp) < iocp->ioc_count) {
8874 			mblk_t *new_data_mp;
8875 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8876 			    NULL) {
8877 				miocnak(q, mp, 0, ENOMEM);
8878 				return;
8879 			}
8880 			freemsg(data_mp);
8881 			data_mp = new_data_mp;
8882 			mp->b_cont = data_mp;
8883 		}
8884 		table = (ip6_asp_t *)data_mp->b_rptr;
8885 		table_size = iocp->ioc_count;
8886 	}
8887 
8888 	switch (iocp->ioc_cmd) {
8889 	case SIOCGIP6ADDRPOLICY:
8890 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8891 		if (iocp->ioc_rval == -1)
8892 			iocp->ioc_error = EINVAL;
8893 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8894 		else if (table != NULL &&
8895 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8896 			ip6_asp_t *src = table;
8897 			ip6_asp32_t *dst = (void *)table;
8898 			int count = table_size / sizeof (ip6_asp_t);
8899 			int i;
8900 
8901 			/*
8902 			 * We need to do an in-place shrink of the array
8903 			 * to match the alignment attributes of the
8904 			 * 32-bit ABI looking at it.
8905 			 */
8906 			/* LINTED: logical expression always true: op "||" */
8907 			ASSERT(sizeof (*src) > sizeof (*dst));
8908 			for (i = 1; i < count; i++)
8909 				bcopy(src + i, dst + i, sizeof (*dst));
8910 		}
8911 #endif
8912 		break;
8913 
8914 	case SIOCSIP6ADDRPOLICY:
8915 		ASSERT(mp->b_prev == NULL);
8916 		mp->b_prev = (void *)q;
8917 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8918 		/*
8919 		 * We pass in the datamodel here so that the ip6_asp_replace()
8920 		 * routine can handle converting from 32-bit to native formats
8921 		 * where necessary.
8922 		 *
8923 		 * A better way to handle this might be to convert the inbound
8924 		 * data structure here, and hang it off a new 'mp'; thus the
8925 		 * ip6_asp_replace() logic would always be dealing with native
8926 		 * format data structures..
8927 		 *
8928 		 * (An even simpler way to handle these ioctls is to just
8929 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8930 		 * and just recompile everything that depends on it.)
8931 		 */
8932 #endif
8933 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
8934 		    iocp->ioc_flag & IOC_MODELS);
8935 		return;
8936 	}
8937 
8938 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8939 	qreply(q, mp);
8940 }
8941 
8942 static void
8943 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8944 {
8945 	mblk_t 		*data_mp;
8946 	struct dstinforeq	*dir;
8947 	uint8_t		*end, *cur;
8948 	in6_addr_t	*daddr, *saddr;
8949 	ipaddr_t	v4daddr;
8950 	ire_t		*ire;
8951 	char		*slabel, *dlabel;
8952 	boolean_t	isipv4;
8953 	int		match_ire;
8954 	ill_t		*dst_ill;
8955 	ipif_t		*src_ipif, *ire_ipif;
8956 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8957 	zoneid_t	zoneid;
8958 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
8959 
8960 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8961 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8962 
8963 	/*
8964 	 * This ioctl is I_STR only, and must have a
8965 	 * data mblk following the M_IOCTL mblk.
8966 	 */
8967 	data_mp = mp->b_cont;
8968 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8969 		miocnak(q, mp, 0, EINVAL);
8970 		return;
8971 	}
8972 
8973 	if (MBLKL(data_mp) < iocp->ioc_count) {
8974 		mblk_t *new_data_mp;
8975 
8976 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8977 			miocnak(q, mp, 0, ENOMEM);
8978 			return;
8979 		}
8980 		freemsg(data_mp);
8981 		data_mp = new_data_mp;
8982 		mp->b_cont = data_mp;
8983 	}
8984 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8985 
8986 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8987 	    end - cur >= sizeof (struct dstinforeq);
8988 	    cur += sizeof (struct dstinforeq)) {
8989 		dir = (struct dstinforeq *)cur;
8990 		daddr = &dir->dir_daddr;
8991 		saddr = &dir->dir_saddr;
8992 
8993 		/*
8994 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8995 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8996 		 * and ipif_select_source[_v6]() do not.
8997 		 */
8998 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8999 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9000 
9001 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9002 		if (isipv4) {
9003 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9004 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9005 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9006 		} else {
9007 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9008 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9009 		}
9010 		if (ire == NULL) {
9011 			dir->dir_dreachable = 0;
9012 
9013 			/* move on to next dst addr */
9014 			continue;
9015 		}
9016 		dir->dir_dreachable = 1;
9017 
9018 		ire_ipif = ire->ire_ipif;
9019 		if (ire_ipif == NULL)
9020 			goto next_dst;
9021 
9022 		/*
9023 		 * We expect to get back an interface ire or a
9024 		 * gateway ire cache entry.  For both types, the
9025 		 * output interface is ire_ipif->ipif_ill.
9026 		 */
9027 		dst_ill = ire_ipif->ipif_ill;
9028 		dir->dir_dmactype = dst_ill->ill_mactype;
9029 
9030 		if (isipv4) {
9031 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9032 		} else {
9033 			src_ipif = ipif_select_source_v6(dst_ill,
9034 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9035 		}
9036 		if (src_ipif == NULL)
9037 			goto next_dst;
9038 
9039 		*saddr = src_ipif->ipif_v6lcl_addr;
9040 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9041 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9042 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9043 		dir->dir_sdeprecated =
9044 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9045 		ipif_refrele(src_ipif);
9046 next_dst:
9047 		ire_refrele(ire);
9048 	}
9049 	miocack(q, mp, iocp->ioc_count, 0);
9050 }
9051 
9052 /*
9053  * Check if this is an address assigned to this machine.
9054  * Skips interfaces that are down by using ire checks.
9055  * Translates mapped addresses to v4 addresses and then
9056  * treats them as such, returning true if the v4 address
9057  * associated with this mapped address is configured.
9058  * Note: Applications will have to be careful what they do
9059  * with the response; use of mapped addresses limits
9060  * what can be done with the socket, especially with
9061  * respect to socket options and ioctls - neither IPv4
9062  * options nor IPv6 sticky options/ancillary data options
9063  * may be used.
9064  */
9065 /* ARGSUSED */
9066 int
9067 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9068     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9069 {
9070 	struct sioc_addrreq *sia;
9071 	sin_t *sin;
9072 	ire_t *ire;
9073 	mblk_t *mp1;
9074 	zoneid_t zoneid;
9075 	ip_stack_t	*ipst;
9076 
9077 	ip1dbg(("ip_sioctl_tmyaddr"));
9078 
9079 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9080 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9081 	ipst = CONNQ_TO_IPST(q);
9082 
9083 	/* Existence verified in ip_wput_nondata */
9084 	mp1 = mp->b_cont->b_cont;
9085 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9086 	sin = (sin_t *)&sia->sa_addr;
9087 	switch (sin->sin_family) {
9088 	case AF_INET6: {
9089 		sin6_t *sin6 = (sin6_t *)sin;
9090 
9091 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9092 			ipaddr_t v4_addr;
9093 
9094 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9095 			    v4_addr);
9096 			ire = ire_ctable_lookup(v4_addr, 0,
9097 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9098 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9099 		} else {
9100 			in6_addr_t v6addr;
9101 
9102 			v6addr = sin6->sin6_addr;
9103 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9104 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9105 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9106 		}
9107 		break;
9108 	}
9109 	case AF_INET: {
9110 		ipaddr_t v4addr;
9111 
9112 		v4addr = sin->sin_addr.s_addr;
9113 		ire = ire_ctable_lookup(v4addr, 0,
9114 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9115 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9116 		break;
9117 	}
9118 	default:
9119 		return (EAFNOSUPPORT);
9120 	}
9121 	if (ire != NULL) {
9122 		sia->sa_res = 1;
9123 		ire_refrele(ire);
9124 	} else {
9125 		sia->sa_res = 0;
9126 	}
9127 	return (0);
9128 }
9129 
9130 /*
9131  * Check if this is an address assigned on-link i.e. neighbor,
9132  * and makes sure it's reachable from the current zone.
9133  * Returns true for my addresses as well.
9134  * Translates mapped addresses to v4 addresses and then
9135  * treats them as such, returning true if the v4 address
9136  * associated with this mapped address is configured.
9137  * Note: Applications will have to be careful what they do
9138  * with the response; use of mapped addresses limits
9139  * what can be done with the socket, especially with
9140  * respect to socket options and ioctls - neither IPv4
9141  * options nor IPv6 sticky options/ancillary data options
9142  * may be used.
9143  */
9144 /* ARGSUSED */
9145 int
9146 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9147     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9148 {
9149 	struct sioc_addrreq *sia;
9150 	sin_t *sin;
9151 	mblk_t	*mp1;
9152 	ire_t *ire = NULL;
9153 	zoneid_t zoneid;
9154 	ip_stack_t	*ipst;
9155 
9156 	ip1dbg(("ip_sioctl_tonlink"));
9157 
9158 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9159 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9160 	ipst = CONNQ_TO_IPST(q);
9161 
9162 	/* Existence verified in ip_wput_nondata */
9163 	mp1 = mp->b_cont->b_cont;
9164 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9165 	sin = (sin_t *)&sia->sa_addr;
9166 
9167 	/*
9168 	 * Match addresses with a zero gateway field to avoid
9169 	 * routes going through a router.
9170 	 * Exclude broadcast and multicast addresses.
9171 	 */
9172 	switch (sin->sin_family) {
9173 	case AF_INET6: {
9174 		sin6_t *sin6 = (sin6_t *)sin;
9175 
9176 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9177 			ipaddr_t v4_addr;
9178 
9179 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9180 			    v4_addr);
9181 			if (!CLASSD(v4_addr)) {
9182 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9183 				    NULL, NULL, zoneid, NULL,
9184 				    MATCH_IRE_GW, ipst);
9185 			}
9186 		} else {
9187 			in6_addr_t v6addr;
9188 			in6_addr_t v6gw;
9189 
9190 			v6addr = sin6->sin6_addr;
9191 			v6gw = ipv6_all_zeros;
9192 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9193 				ire = ire_route_lookup_v6(&v6addr, 0,
9194 				    &v6gw, 0, NULL, NULL, zoneid,
9195 				    NULL, MATCH_IRE_GW, ipst);
9196 			}
9197 		}
9198 		break;
9199 	}
9200 	case AF_INET: {
9201 		ipaddr_t v4addr;
9202 
9203 		v4addr = sin->sin_addr.s_addr;
9204 		if (!CLASSD(v4addr)) {
9205 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9206 			    NULL, NULL, zoneid, NULL,
9207 			    MATCH_IRE_GW, ipst);
9208 		}
9209 		break;
9210 	}
9211 	default:
9212 		return (EAFNOSUPPORT);
9213 	}
9214 	sia->sa_res = 0;
9215 	if (ire != NULL) {
9216 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9217 		    IRE_LOCAL|IRE_LOOPBACK)) {
9218 			sia->sa_res = 1;
9219 		}
9220 		ire_refrele(ire);
9221 	}
9222 	return (0);
9223 }
9224 
9225 /*
9226  * TBD: implement when kernel maintaines a list of site prefixes.
9227  */
9228 /* ARGSUSED */
9229 int
9230 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9231     ip_ioctl_cmd_t *ipip, void *ifreq)
9232 {
9233 	return (ENXIO);
9234 }
9235 
9236 /* ARGSUSED */
9237 int
9238 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9239     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9240 {
9241 	ill_t		*ill;
9242 	mblk_t		*mp1;
9243 	conn_t		*connp;
9244 	boolean_t	success;
9245 
9246 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9247 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9248 	/* ioctl comes down on an conn */
9249 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9250 	connp = Q_TO_CONN(q);
9251 
9252 	mp->b_datap->db_type = M_IOCTL;
9253 
9254 	/*
9255 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9256 	 * The original mp contains contaminated b_next values due to 'mi',
9257 	 * which is needed to do the mi_copy_done. Unfortunately if we
9258 	 * send down the original mblk itself and if we are popped due to an
9259 	 * an unplumb before the response comes back from tunnel,
9260 	 * the streamhead (which does a freemsg) will see this contaminated
9261 	 * message and the assertion in freemsg about non-null b_next/b_prev
9262 	 * will panic a DEBUG kernel.
9263 	 */
9264 	mp1 = copymsg(mp);
9265 	if (mp1 == NULL)
9266 		return (ENOMEM);
9267 
9268 	ill = ipif->ipif_ill;
9269 	mutex_enter(&connp->conn_lock);
9270 	mutex_enter(&ill->ill_lock);
9271 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9272 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9273 		    mp, 0);
9274 	} else {
9275 		success = ill_pending_mp_add(ill, connp, mp);
9276 	}
9277 	mutex_exit(&ill->ill_lock);
9278 	mutex_exit(&connp->conn_lock);
9279 
9280 	if (success) {
9281 		ip1dbg(("sending down tunparam request "));
9282 		putnext(ill->ill_wq, mp1);
9283 		return (EINPROGRESS);
9284 	} else {
9285 		/* The conn has started closing */
9286 		freemsg(mp1);
9287 		return (EINTR);
9288 	}
9289 }
9290 
9291 /*
9292  * ARP IOCTLs.
9293  * How does IP get in the business of fronting ARP configuration/queries?
9294  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9295  * are by tradition passed in through a datagram socket.  That lands in IP.
9296  * As it happens, this is just as well since the interface is quite crude in
9297  * that it passes in no information about protocol or hardware types, or
9298  * interface association.  After making the protocol assumption, IP is in
9299  * the position to look up the name of the ILL, which ARP will need, and
9300  * format a request that can be handled by ARP.  The request is passed up
9301  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9302  * back a response.  ARP supports its own set of more general IOCTLs, in
9303  * case anyone is interested.
9304  */
9305 /* ARGSUSED */
9306 int
9307 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9308     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9309 {
9310 	mblk_t *mp1;
9311 	mblk_t *mp2;
9312 	mblk_t *pending_mp;
9313 	ipaddr_t ipaddr;
9314 	area_t *area;
9315 	struct iocblk *iocp;
9316 	conn_t *connp;
9317 	struct arpreq *ar;
9318 	struct xarpreq *xar;
9319 	int flags, alength;
9320 	uchar_t *lladdr;
9321 	ire_t *ire;
9322 	ip_stack_t *ipst;
9323 	ill_t *ill = ipif->ipif_ill;
9324 	ill_t *proxy_ill = NULL;
9325 	ipmp_arpent_t *entp = NULL;
9326 	boolean_t if_arp_ioctl = B_FALSE;
9327 	boolean_t proxyarp = B_FALSE;
9328 
9329 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9330 	connp = Q_TO_CONN(q);
9331 	ipst = connp->conn_netstack->netstack_ip;
9332 
9333 	if (ipip->ipi_cmd_type == XARP_CMD) {
9334 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9335 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9336 		ar = NULL;
9337 
9338 		flags = xar->xarp_flags;
9339 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9340 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9341 		/*
9342 		 * Validate against user's link layer address length
9343 		 * input and name and addr length limits.
9344 		 */
9345 		alength = ill->ill_phys_addr_length;
9346 		if (ipip->ipi_cmd == SIOCSXARP) {
9347 			if (alength != xar->xarp_ha.sdl_alen ||
9348 			    (alength + xar->xarp_ha.sdl_nlen >
9349 			    sizeof (xar->xarp_ha.sdl_data)))
9350 				return (EINVAL);
9351 		}
9352 	} else {
9353 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9354 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9355 		xar = NULL;
9356 
9357 		flags = ar->arp_flags;
9358 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9359 		/*
9360 		 * Theoretically, the sa_family could tell us what link
9361 		 * layer type this operation is trying to deal with. By
9362 		 * common usage AF_UNSPEC means ethernet. We'll assume
9363 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9364 		 * for now. Our new SIOC*XARP ioctls can be used more
9365 		 * generally.
9366 		 *
9367 		 * If the underlying media happens to have a non 6 byte
9368 		 * address, arp module will fail set/get, but the del
9369 		 * operation will succeed.
9370 		 */
9371 		alength = 6;
9372 		if ((ipip->ipi_cmd != SIOCDARP) &&
9373 		    (alength != ill->ill_phys_addr_length)) {
9374 			return (EINVAL);
9375 		}
9376 	}
9377 
9378 	ipaddr = sin->sin_addr.s_addr;
9379 
9380 	/*
9381 	 * IPMP ARP special handling:
9382 	 *
9383 	 * 1. Since ARP mappings must appear consistent across the group,
9384 	 *    prohibit changing ARP mappings on the underlying interfaces.
9385 	 *
9386 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9387 	 *    IP itself, prohibit changing them.
9388 	 *
9389 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9390 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9391 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9392 	 */
9393 	if (IS_UNDER_IPMP(ill)) {
9394 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9395 			return (EPERM);
9396 	}
9397 	if (IS_IPMP(ill)) {
9398 		ipmp_illgrp_t *illg = ill->ill_grp;
9399 
9400 		switch (ipip->ipi_cmd) {
9401 		case SIOCSARP:
9402 		case SIOCSXARP:
9403 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9404 			if (proxy_ill != NULL) {
9405 				proxyarp = B_TRUE;
9406 				if (!ipmp_ill_is_active(proxy_ill))
9407 					proxy_ill = ipmp_illgrp_next_ill(illg);
9408 				if (proxy_ill != NULL)
9409 					lladdr = proxy_ill->ill_phys_addr;
9410 			}
9411 			/* FALLTHRU */
9412 		case SIOCDARP:
9413 		case SIOCDXARP:
9414 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9415 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9416 			if (ire != NULL) {
9417 				ire_refrele(ire);
9418 				return (EPERM);
9419 			}
9420 		}
9421 	}
9422 
9423 	/*
9424 	 * We are going to pass up to ARP a packet chain that looks
9425 	 * like:
9426 	 *
9427 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9428 	 *
9429 	 * Get a copy of the original IOCTL mblk to head the chain,
9430 	 * to be sent up (in mp1). Also get another copy to store
9431 	 * in the ill_pending_mp list, for matching the response
9432 	 * when it comes back from ARP.
9433 	 */
9434 	mp1 = copyb(mp);
9435 	pending_mp = copymsg(mp);
9436 	if (mp1 == NULL || pending_mp == NULL) {
9437 		if (mp1 != NULL)
9438 			freeb(mp1);
9439 		if (pending_mp != NULL)
9440 			inet_freemsg(pending_mp);
9441 		return (ENOMEM);
9442 	}
9443 
9444 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9445 	    (caddr_t)&ipaddr);
9446 	if (mp2 == NULL) {
9447 		freeb(mp1);
9448 		inet_freemsg(pending_mp);
9449 		return (ENOMEM);
9450 	}
9451 	/* Put together the chain. */
9452 	mp1->b_cont = mp2;
9453 	mp1->b_datap->db_type = M_IOCTL;
9454 	mp2->b_cont = mp;
9455 	mp2->b_datap->db_type = M_DATA;
9456 
9457 	iocp = (struct iocblk *)mp1->b_rptr;
9458 
9459 	/*
9460 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9461 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9462 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9463 	 * ioc_count field; set ioc_count to be correct.
9464 	 */
9465 	iocp->ioc_count = MBLKL(mp1->b_cont);
9466 
9467 	/*
9468 	 * Set the proper command in the ARP message.
9469 	 * Convert the SIOC{G|S|D}ARP calls into our
9470 	 * AR_ENTRY_xxx calls.
9471 	 */
9472 	area = (area_t *)mp2->b_rptr;
9473 	switch (iocp->ioc_cmd) {
9474 	case SIOCDARP:
9475 	case SIOCDXARP:
9476 		/*
9477 		 * We defer deleting the corresponding IRE until
9478 		 * we return from arp.
9479 		 */
9480 		area->area_cmd = AR_ENTRY_DELETE;
9481 		area->area_proto_mask_offset = 0;
9482 		break;
9483 	case SIOCGARP:
9484 	case SIOCGXARP:
9485 		area->area_cmd = AR_ENTRY_SQUERY;
9486 		area->area_proto_mask_offset = 0;
9487 		break;
9488 	case SIOCSARP:
9489 	case SIOCSXARP:
9490 		/*
9491 		 * Delete the corresponding ire to make sure IP will
9492 		 * pick up any change from arp.
9493 		 */
9494 		if (!if_arp_ioctl) {
9495 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9496 		} else {
9497 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9498 			if (ipif != NULL) {
9499 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9500 				    ipst);
9501 				ipif_refrele(ipif);
9502 			}
9503 		}
9504 		break;
9505 	}
9506 	iocp->ioc_cmd = area->area_cmd;
9507 
9508 	/*
9509 	 * Fill in the rest of the ARP operation fields.
9510 	 */
9511 	area->area_hw_addr_length = alength;
9512 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9513 
9514 	/* Translate the flags. */
9515 	if (flags & ATF_PERM)
9516 		area->area_flags |= ACE_F_PERMANENT;
9517 	if (flags & ATF_PUBL)
9518 		area->area_flags |= ACE_F_PUBLISH;
9519 	if (flags & ATF_AUTHORITY)
9520 		area->area_flags |= ACE_F_AUTHORITY;
9521 
9522 	/*
9523 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9524 	 * so that IP can update ARP as the active ills in the group change.
9525 	 */
9526 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9527 	    (area->area_flags & ACE_F_PERMANENT)) {
9528 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9529 
9530 		/*
9531 		 * The second part of the conditional below handles a corner
9532 		 * case: if this is proxy ARP and the IPMP group has no active
9533 		 * interfaces, we can't send the request to ARP now since it
9534 		 * won't be able to build an ACE.  So we return success and
9535 		 * notify ARP about the proxy ARP entry once an interface
9536 		 * becomes active.
9537 		 */
9538 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9539 			mp2->b_cont = NULL;
9540 			inet_freemsg(mp1);
9541 			inet_freemsg(pending_mp);
9542 			return (entp == NULL ? ENOMEM : 0);
9543 		}
9544 	}
9545 
9546 	/*
9547 	 * Before sending 'mp' to ARP, we have to clear the b_next
9548 	 * and b_prev. Otherwise if STREAMS encounters such a message
9549 	 * in freemsg(), (because ARP can close any time) it can cause
9550 	 * a panic. But mi code needs the b_next and b_prev values of
9551 	 * mp->b_cont, to complete the ioctl. So we store it here
9552 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9553 	 * when the response comes down from ARP.
9554 	 */
9555 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9556 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9557 	mp->b_cont->b_next = NULL;
9558 	mp->b_cont->b_prev = NULL;
9559 
9560 	mutex_enter(&connp->conn_lock);
9561 	mutex_enter(&ill->ill_lock);
9562 	/* conn has not yet started closing, hence this can't fail */
9563 	if (ipip->ipi_flags & IPI_WR) {
9564 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9565 		    pending_mp, 0) != 0);
9566 	} else {
9567 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9568 	}
9569 	mutex_exit(&ill->ill_lock);
9570 	mutex_exit(&connp->conn_lock);
9571 
9572 	/*
9573 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9574 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9575 	 */
9576 	putnext(ill->ill_rq, mp1);
9577 
9578 	/*
9579 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9580 	 */
9581 	if (entp != NULL)
9582 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9583 
9584 	return (EINPROGRESS);
9585 }
9586 
9587 /*
9588  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9589  * the associated sin and refhold and return the associated ipif via `ci'.
9590  */
9591 int
9592 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9593     cmd_info_t *ci, ipsq_func_t func)
9594 {
9595 	mblk_t	*mp1;
9596 	int	err;
9597 	sin_t	*sin;
9598 	conn_t	*connp;
9599 	ipif_t	*ipif;
9600 	ire_t	*ire = NULL;
9601 	ill_t	*ill = NULL;
9602 	boolean_t exists;
9603 	ip_stack_t *ipst;
9604 	struct arpreq *ar;
9605 	struct xarpreq *xar;
9606 	struct sockaddr_dl *sdl;
9607 
9608 	/* ioctl comes down on a conn */
9609 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9610 	connp = Q_TO_CONN(q);
9611 	if (connp->conn_af_isv6)
9612 		return (ENXIO);
9613 
9614 	ipst = connp->conn_netstack->netstack_ip;
9615 
9616 	/* Verified in ip_wput_nondata */
9617 	mp1 = mp->b_cont->b_cont;
9618 
9619 	if (ipip->ipi_cmd_type == XARP_CMD) {
9620 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9621 		xar = (struct xarpreq *)mp1->b_rptr;
9622 		sin = (sin_t *)&xar->xarp_pa;
9623 		sdl = &xar->xarp_ha;
9624 
9625 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9626 			return (ENXIO);
9627 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9628 			return (EINVAL);
9629 	} else {
9630 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9631 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9632 		ar = (struct arpreq *)mp1->b_rptr;
9633 		sin = (sin_t *)&ar->arp_pa;
9634 	}
9635 
9636 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9637 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9638 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9639 		    mp, func, &err, ipst);
9640 		if (ipif == NULL)
9641 			return (err);
9642 		if (ipif->ipif_id != 0) {
9643 			ipif_refrele(ipif);
9644 			return (ENXIO);
9645 		}
9646 	} else {
9647 		/*
9648 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9649 		 * of 0: use the IP address to find the ipif.  If the IP
9650 		 * address is an IPMP test address, ire_ftable_lookup() will
9651 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9652 		 */
9653 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9654 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9655 		if (ipif == NULL) {
9656 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9657 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9658 			    MATCH_IRE_TYPE, ipst);
9659 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9660 				if (ire != NULL)
9661 					ire_refrele(ire);
9662 				return (ENXIO);
9663 			}
9664 			ipif = ill->ill_ipif;
9665 			ipif_refhold(ipif);
9666 			ire_refrele(ire);
9667 		}
9668 	}
9669 
9670 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9671 		ipif_refrele(ipif);
9672 		return (ENXIO);
9673 	}
9674 
9675 	ci->ci_sin = sin;
9676 	ci->ci_ipif = ipif;
9677 	return (0);
9678 }
9679 
9680 /*
9681  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9682  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9683  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9684  * up and thus an ill can join that illgrp.
9685  *
9686  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9687  * open()/close() primarily because close() is not allowed to fail or block
9688  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9689  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9690  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9691  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9692  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9693  * state if I_UNLINK didn't occur.
9694  *
9695  * Note that for each plumb/unplumb operation, we may end up here more than
9696  * once because of the way ifconfig works.  However, it's OK to link the same
9697  * illgrp more than once, or unlink an illgrp that's already unlinked.
9698  */
9699 static int
9700 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9701 {
9702 	int err;
9703 	ip_stack_t *ipst = ill->ill_ipst;
9704 
9705 	ASSERT(IS_IPMP(ill));
9706 	ASSERT(IAM_WRITER_ILL(ill));
9707 
9708 	switch (ioccmd) {
9709 	case I_LINK:
9710 		return (ENOTSUP);
9711 
9712 	case I_PLINK:
9713 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9714 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9715 		rw_exit(&ipst->ips_ipmp_lock);
9716 		break;
9717 
9718 	case I_PUNLINK:
9719 		/*
9720 		 * Require all UP ipifs be brought down prior to unlinking the
9721 		 * illgrp so any associated IREs (and other state) is torched.
9722 		 */
9723 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9724 			return (EBUSY);
9725 
9726 		/*
9727 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9728 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9729 		 * join this group.  Specifically: ills trying to join grab
9730 		 * ipmp_lock and bump a "pending join" counter checked by
9731 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9732 		 * joins can occur (since we have ipmp_lock).  Once we drop
9733 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9734 		 * find the illgrp (since we unlinked it) and will return
9735 		 * EAFNOSUPPORT.  This will then take them back through the
9736 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9737 		 * back through I_PLINK above.
9738 		 */
9739 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9740 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9741 		rw_exit(&ipst->ips_ipmp_lock);
9742 		return (err);
9743 	default:
9744 		break;
9745 	}
9746 	return (0);
9747 }
9748 
9749 /*
9750  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9751  * atomically set/clear the muxids. Also complete the ioctl by acking or
9752  * naking it.  Note that the code is structured such that the link type,
9753  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9754  * its clones use the persistent link, while pppd(1M) and perhaps many
9755  * other daemons may use non-persistent link.  When combined with some
9756  * ill_t states, linking and unlinking lower streams may be used as
9757  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9758  */
9759 /* ARGSUSED */
9760 void
9761 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9762 {
9763 	mblk_t		*mp1, *mp2;
9764 	struct linkblk	*li;
9765 	struct ipmx_s	*ipmxp;
9766 	ill_t		*ill;
9767 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9768 	int		err = 0;
9769 	boolean_t	entered_ipsq = B_FALSE;
9770 	boolean_t	islink;
9771 	ip_stack_t	*ipst;
9772 
9773 	if (CONN_Q(q))
9774 		ipst = CONNQ_TO_IPST(q);
9775 	else
9776 		ipst = ILLQ_TO_IPST(q);
9777 
9778 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9779 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9780 
9781 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9782 
9783 	mp1 = mp->b_cont;	/* This is the linkblk info */
9784 	li = (struct linkblk *)mp1->b_rptr;
9785 
9786 	/*
9787 	 * ARP has added this special mblk, and the utility is asking us
9788 	 * to perform consistency checks, and also atomically set the
9789 	 * muxid. Ifconfig is an example.  It achieves this by using
9790 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9791 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9792 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9793 	 * and other comments in this routine for more details.
9794 	 */
9795 	mp2 = mp1->b_cont;	/* This is added by ARP */
9796 
9797 	/*
9798 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9799 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9800 	 * get the special mblk above.  For backward compatibility, we
9801 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9802 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9803 	 * not atomic, and can leave the streams unplumbable if the utility
9804 	 * is interrupted before it does the SIOCSLIFMUXID.
9805 	 */
9806 	if (mp2 == NULL) {
9807 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9808 		if (err == EINPROGRESS)
9809 			return;
9810 		goto done;
9811 	}
9812 
9813 	/*
9814 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9815 	 * ARP has appended this last mblk to tell us whether the lower stream
9816 	 * is an arp-dev stream or an IP module stream.
9817 	 */
9818 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9819 	if (ipmxp->ipmx_arpdev_stream) {
9820 		/*
9821 		 * The lower stream is the arp-dev stream.
9822 		 */
9823 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9824 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9825 		if (ill == NULL) {
9826 			if (err == EINPROGRESS)
9827 				return;
9828 			err = EINVAL;
9829 			goto done;
9830 		}
9831 
9832 		if (ipsq == NULL) {
9833 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9834 			    NEW_OP, B_FALSE);
9835 			if (ipsq == NULL) {
9836 				ill_refrele(ill);
9837 				return;
9838 			}
9839 			entered_ipsq = B_TRUE;
9840 		}
9841 		ASSERT(IAM_WRITER_ILL(ill));
9842 		ill_refrele(ill);
9843 
9844 		/*
9845 		 * To ensure consistency between IP and ARP, the following
9846 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9847 		 * This is because the muxid's are stored in the IP stream on
9848 		 * the ill.
9849 		 *
9850 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9851 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9852 		 * not yet plinked, and it also checks that the corresponding
9853 		 * IP stream is already plinked.
9854 		 *
9855 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9856 		 * punlinking the IP stream. IP does not allow punlink of the
9857 		 * IP stream unless the arp stream has been punlinked.
9858 		 */
9859 		if ((islink &&
9860 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9861 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9862 			err = EINVAL;
9863 			goto done;
9864 		}
9865 
9866 		if (IS_IPMP(ill) &&
9867 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9868 			goto done;
9869 
9870 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9871 	} else {
9872 		/*
9873 		 * The lower stream is probably an IP module stream.  Do
9874 		 * consistency checking.
9875 		 */
9876 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9877 		if (err == EINPROGRESS)
9878 			return;
9879 	}
9880 done:
9881 	if (err == 0)
9882 		miocack(q, mp, 0, 0);
9883 	else
9884 		miocnak(q, mp, 0, err);
9885 
9886 	/* Conn was refheld in ip_sioctl_copyin_setup */
9887 	if (CONN_Q(q))
9888 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9889 	if (entered_ipsq)
9890 		ipsq_exit(ipsq);
9891 }
9892 
9893 /*
9894  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9895  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9896  * module stream).  If `doconsist' is set, then do the extended consistency
9897  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9898  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9899  * an error code on failure.
9900  */
9901 static int
9902 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9903     struct linkblk *li, boolean_t doconsist)
9904 {
9905 	int		err = 0;
9906 	ill_t  		*ill;
9907 	queue_t		*ipwq, *dwq;
9908 	const char	*name;
9909 	struct qinit	*qinfo;
9910 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9911 	boolean_t	entered_ipsq = B_FALSE;
9912 
9913 	/*
9914 	 * Walk the lower stream to verify it's the IP module stream.
9915 	 * The IP module is identified by its name, wput function,
9916 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9917 	 * (li->l_qbot) will not vanish until this ioctl completes.
9918 	 */
9919 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9920 		qinfo = ipwq->q_qinfo;
9921 		name = qinfo->qi_minfo->mi_idname;
9922 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9923 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9924 			break;
9925 		}
9926 	}
9927 
9928 	/*
9929 	 * If this isn't an IP module stream, bail.
9930 	 */
9931 	if (ipwq == NULL)
9932 		return (0);
9933 
9934 	ill = ipwq->q_ptr;
9935 	ASSERT(ill != NULL);
9936 
9937 	if (ipsq == NULL) {
9938 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9939 		    NEW_OP, B_FALSE);
9940 		if (ipsq == NULL)
9941 			return (EINPROGRESS);
9942 		entered_ipsq = B_TRUE;
9943 	}
9944 	ASSERT(IAM_WRITER_ILL(ill));
9945 
9946 	if (doconsist) {
9947 		/*
9948 		 * Consistency checking requires that I_{P}LINK occurs
9949 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9950 		 * occurs prior to clearing ill_arp_muxid.
9951 		 */
9952 		if ((islink && ill->ill_ip_muxid != 0) ||
9953 		    (!islink && ill->ill_arp_muxid != 0)) {
9954 			err = EINVAL;
9955 			goto done;
9956 		}
9957 	}
9958 
9959 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9960 		goto done;
9961 
9962 	/*
9963 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9964 	 * the read queue of the module immediately below IP in the ill.
9965 	 * These are used during the capability negotiation below.
9966 	 */
9967 	ill->ill_lmod_rq = NULL;
9968 	ill->ill_lmod_cnt = 0;
9969 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9970 		ill->ill_lmod_rq = RD(dwq);
9971 		for (; dwq != NULL; dwq = dwq->q_next)
9972 			ill->ill_lmod_cnt++;
9973 	}
9974 
9975 	if (doconsist)
9976 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9977 
9978 	/*
9979 	 * Mark the ipsq busy until the capability operations initiated below
9980 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
9981 	 * returns, but the capability operation may complete asynchronously
9982 	 * much later.
9983 	 */
9984 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
9985 	/*
9986 	 * If there's at least one up ipif on this ill, then we're bound to
9987 	 * the underlying driver via DLPI.  In that case, renegotiate
9988 	 * capabilities to account for any possible change in modules
9989 	 * interposed between IP and the driver.
9990 	 */
9991 	if (ill->ill_ipif_up_count > 0) {
9992 		if (islink)
9993 			ill_capability_probe(ill);
9994 		else
9995 			ill_capability_reset(ill, B_FALSE);
9996 	}
9997 	ipsq_current_finish(ipsq);
9998 done:
9999 	if (entered_ipsq)
10000 		ipsq_exit(ipsq);
10001 
10002 	return (err);
10003 }
10004 
10005 /*
10006  * Search the ioctl command in the ioctl tables and return a pointer
10007  * to the ioctl command information. The ioctl command tables are
10008  * static and fully populated at compile time.
10009  */
10010 ip_ioctl_cmd_t *
10011 ip_sioctl_lookup(int ioc_cmd)
10012 {
10013 	int index;
10014 	ip_ioctl_cmd_t *ipip;
10015 	ip_ioctl_cmd_t *ipip_end;
10016 
10017 	if (ioc_cmd == IPI_DONTCARE)
10018 		return (NULL);
10019 
10020 	/*
10021 	 * Do a 2 step search. First search the indexed table
10022 	 * based on the least significant byte of the ioctl cmd.
10023 	 * If we don't find a match, then search the misc table
10024 	 * serially.
10025 	 */
10026 	index = ioc_cmd & 0xFF;
10027 	if (index < ip_ndx_ioctl_count) {
10028 		ipip = &ip_ndx_ioctl_table[index];
10029 		if (ipip->ipi_cmd == ioc_cmd) {
10030 			/* Found a match in the ndx table */
10031 			return (ipip);
10032 		}
10033 	}
10034 
10035 	/* Search the misc table */
10036 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10037 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10038 		if (ipip->ipi_cmd == ioc_cmd)
10039 			/* Found a match in the misc table */
10040 			return (ipip);
10041 	}
10042 
10043 	return (NULL);
10044 }
10045 
10046 /*
10047  * Wrapper function for resuming deferred ioctl processing
10048  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10049  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10050  */
10051 /* ARGSUSED */
10052 void
10053 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10054     void *dummy_arg)
10055 {
10056 	ip_sioctl_copyin_setup(q, mp);
10057 }
10058 
10059 /*
10060  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10061  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10062  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10063  * We establish here the size of the block to be copied in.  mi_copyin
10064  * arranges for this to happen, an processing continues in ip_wput with
10065  * an M_IOCDATA message.
10066  */
10067 void
10068 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10069 {
10070 	int	copyin_size;
10071 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10072 	ip_ioctl_cmd_t *ipip;
10073 	cred_t *cr;
10074 	ip_stack_t	*ipst;
10075 
10076 	if (CONN_Q(q))
10077 		ipst = CONNQ_TO_IPST(q);
10078 	else
10079 		ipst = ILLQ_TO_IPST(q);
10080 
10081 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10082 	if (ipip == NULL) {
10083 		/*
10084 		 * The ioctl is not one we understand or own.
10085 		 * Pass it along to be processed down stream,
10086 		 * if this is a module instance of IP, else nak
10087 		 * the ioctl.
10088 		 */
10089 		if (q->q_next == NULL) {
10090 			goto nak;
10091 		} else {
10092 			putnext(q, mp);
10093 			return;
10094 		}
10095 	}
10096 
10097 	/*
10098 	 * If this is deferred, then we will do all the checks when we
10099 	 * come back.
10100 	 */
10101 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10102 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10103 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10104 		return;
10105 	}
10106 
10107 	/*
10108 	 * Only allow a very small subset of IP ioctls on this stream if
10109 	 * IP is a module and not a driver. Allowing ioctls to be processed
10110 	 * in this case may cause assert failures or data corruption.
10111 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10112 	 * ioctls allowed on an IP module stream, after which this stream
10113 	 * normally becomes a multiplexor (at which time the stream head
10114 	 * will fail all ioctls).
10115 	 */
10116 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10117 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10118 			/*
10119 			 * Pass common Streams ioctls which the IP
10120 			 * module does not own or consume along to
10121 			 * be processed down stream.
10122 			 */
10123 			putnext(q, mp);
10124 			return;
10125 		} else {
10126 			goto nak;
10127 		}
10128 	}
10129 
10130 	/* Make sure we have ioctl data to process. */
10131 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10132 		goto nak;
10133 
10134 	/*
10135 	 * Prefer dblk credential over ioctl credential; some synthesized
10136 	 * ioctls have kcred set because there's no way to crhold()
10137 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10138 	 * the framework; the caller of ioctl needs to hold the reference
10139 	 * for the duration of the call).
10140 	 */
10141 	cr = msg_getcred(mp, NULL);
10142 	if (cr == NULL)
10143 		cr = iocp->ioc_cr;
10144 
10145 	/* Make sure normal users don't send down privileged ioctls */
10146 	if ((ipip->ipi_flags & IPI_PRIV) &&
10147 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10148 		/* We checked the privilege earlier but log it here */
10149 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10150 		return;
10151 	}
10152 
10153 	/*
10154 	 * The ioctl command tables can only encode fixed length
10155 	 * ioctl data. If the length is variable, the table will
10156 	 * encode the length as zero. Such special cases are handled
10157 	 * below in the switch.
10158 	 */
10159 	if (ipip->ipi_copyin_size != 0) {
10160 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10161 		return;
10162 	}
10163 
10164 	switch (iocp->ioc_cmd) {
10165 	case O_SIOCGIFCONF:
10166 	case SIOCGIFCONF:
10167 		/*
10168 		 * This IOCTL is hilarious.  See comments in
10169 		 * ip_sioctl_get_ifconf for the story.
10170 		 */
10171 		if (iocp->ioc_count == TRANSPARENT)
10172 			copyin_size = SIZEOF_STRUCT(ifconf,
10173 			    iocp->ioc_flag);
10174 		else
10175 			copyin_size = iocp->ioc_count;
10176 		mi_copyin(q, mp, NULL, copyin_size);
10177 		return;
10178 
10179 	case O_SIOCGLIFCONF:
10180 	case SIOCGLIFCONF:
10181 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10182 		mi_copyin(q, mp, NULL, copyin_size);
10183 		return;
10184 
10185 	case SIOCGLIFSRCOF:
10186 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10187 		mi_copyin(q, mp, NULL, copyin_size);
10188 		return;
10189 	case SIOCGIP6ADDRPOLICY:
10190 		ip_sioctl_ip6addrpolicy(q, mp);
10191 		ip6_asp_table_refrele(ipst);
10192 		return;
10193 
10194 	case SIOCSIP6ADDRPOLICY:
10195 		ip_sioctl_ip6addrpolicy(q, mp);
10196 		return;
10197 
10198 	case SIOCGDSTINFO:
10199 		ip_sioctl_dstinfo(q, mp);
10200 		ip6_asp_table_refrele(ipst);
10201 		return;
10202 
10203 	case I_PLINK:
10204 	case I_PUNLINK:
10205 	case I_LINK:
10206 	case I_UNLINK:
10207 		/*
10208 		 * We treat non-persistent link similarly as the persistent
10209 		 * link case, in terms of plumbing/unplumbing, as well as
10210 		 * dynamic re-plumbing events indicator.  See comments
10211 		 * in ip_sioctl_plink() for more.
10212 		 *
10213 		 * Request can be enqueued in the 'ipsq' while waiting
10214 		 * to become exclusive. So bump up the conn ref.
10215 		 */
10216 		if (CONN_Q(q))
10217 			CONN_INC_REF(Q_TO_CONN(q));
10218 		ip_sioctl_plink(NULL, q, mp, NULL);
10219 		return;
10220 
10221 	case ND_GET:
10222 	case ND_SET:
10223 		/*
10224 		 * Use of the nd table requires holding the reader lock.
10225 		 * Modifying the nd table thru nd_load/nd_unload requires
10226 		 * the writer lock.
10227 		 */
10228 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10229 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10230 			rw_exit(&ipst->ips_ip_g_nd_lock);
10231 
10232 			if (iocp->ioc_error)
10233 				iocp->ioc_count = 0;
10234 			mp->b_datap->db_type = M_IOCACK;
10235 			qreply(q, mp);
10236 			return;
10237 		}
10238 		rw_exit(&ipst->ips_ip_g_nd_lock);
10239 		/*
10240 		 * We don't understand this subioctl of ND_GET / ND_SET.
10241 		 * Maybe intended for some driver / module below us
10242 		 */
10243 		if (q->q_next) {
10244 			putnext(q, mp);
10245 		} else {
10246 			iocp->ioc_error = ENOENT;
10247 			mp->b_datap->db_type = M_IOCNAK;
10248 			iocp->ioc_count = 0;
10249 			qreply(q, mp);
10250 		}
10251 		return;
10252 
10253 	case IP_IOCTL:
10254 		ip_wput_ioctl(q, mp);
10255 		return;
10256 	default:
10257 		cmn_err(CE_PANIC, "should not happen ");
10258 	}
10259 nak:
10260 	if (mp->b_cont != NULL) {
10261 		freemsg(mp->b_cont);
10262 		mp->b_cont = NULL;
10263 	}
10264 	iocp->ioc_error = EINVAL;
10265 	mp->b_datap->db_type = M_IOCNAK;
10266 	iocp->ioc_count = 0;
10267 	qreply(q, mp);
10268 }
10269 
10270 /* ip_wput hands off ARP IOCTL responses to us */
10271 /* ARGSUSED3 */
10272 void
10273 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10274 {
10275 	struct arpreq *ar;
10276 	struct xarpreq *xar;
10277 	area_t	*area;
10278 	mblk_t	*area_mp;
10279 	struct iocblk *iocp;
10280 	mblk_t	*orig_ioc_mp, *tmp;
10281 	struct iocblk	*orig_iocp;
10282 	ill_t *ill;
10283 	conn_t *connp = NULL;
10284 	mblk_t *pending_mp;
10285 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10286 	int *flagsp;
10287 	char *storage = NULL;
10288 	sin_t *sin;
10289 	ipaddr_t addr;
10290 	int err;
10291 	ip_stack_t *ipst;
10292 
10293 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10294 	ill = q->q_ptr;
10295 	ASSERT(ill != NULL);
10296 	ipst = ill->ill_ipst;
10297 
10298 	/*
10299 	 * We should get back from ARP a packet chain that looks like:
10300 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10301 	 */
10302 	if (!(area_mp = mp->b_cont) ||
10303 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10304 	    !(orig_ioc_mp = area_mp->b_cont) ||
10305 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10306 		freemsg(mp);
10307 		return;
10308 	}
10309 
10310 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10311 
10312 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10313 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10314 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10315 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10316 		x_arp_ioctl = B_TRUE;
10317 		xar = (struct xarpreq *)tmp->b_rptr;
10318 		sin = (sin_t *)&xar->xarp_pa;
10319 		flagsp = &xar->xarp_flags;
10320 		storage = xar->xarp_ha.sdl_data;
10321 		if (xar->xarp_ha.sdl_nlen != 0)
10322 			ifx_arp_ioctl = B_TRUE;
10323 	} else {
10324 		ar = (struct arpreq *)tmp->b_rptr;
10325 		sin = (sin_t *)&ar->arp_pa;
10326 		flagsp = &ar->arp_flags;
10327 		storage = ar->arp_ha.sa_data;
10328 	}
10329 
10330 	iocp = (struct iocblk *)mp->b_rptr;
10331 
10332 	/*
10333 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10334 	 * Otherwise, we can find it from our ioc_id.
10335 	 */
10336 	if (ipsq != NULL)
10337 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10338 	else
10339 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10340 
10341 	if (pending_mp == NULL) {
10342 		ASSERT(connp == NULL);
10343 		inet_freemsg(mp);
10344 		return;
10345 	}
10346 	ASSERT(connp != NULL);
10347 	q = CONNP_TO_WQ(connp);
10348 
10349 	/* Uncouple the internally generated IOCTL from the original one */
10350 	area = (area_t *)area_mp->b_rptr;
10351 	area_mp->b_cont = NULL;
10352 
10353 	/*
10354 	 * Restore the b_next and b_prev used by mi code. This is needed
10355 	 * to complete the ioctl using mi* functions. We stored them in
10356 	 * the pending mp prior to sending the request to ARP.
10357 	 */
10358 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10359 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10360 	inet_freemsg(pending_mp);
10361 
10362 	/*
10363 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10364 	 * Catch the case where there is an IRE_CACHE by no entry in the
10365 	 * arp table.
10366 	 */
10367 	addr = sin->sin_addr.s_addr;
10368 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10369 		ire_t			*ire;
10370 		dl_unitdata_req_t	*dlup;
10371 		mblk_t			*llmp;
10372 		int			addr_len;
10373 		ill_t			*ipsqill = NULL;
10374 
10375 		if (ifx_arp_ioctl) {
10376 			/*
10377 			 * There's no need to lookup the ill, since
10378 			 * we've already done that when we started
10379 			 * processing the ioctl and sent the message
10380 			 * to ARP on that ill.  So use the ill that
10381 			 * is stored in q->q_ptr.
10382 			 */
10383 			ipsqill = ill;
10384 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10385 			    ipsqill->ill_ipif, ALL_ZONES,
10386 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10387 		} else {
10388 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10389 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10390 			if (ire != NULL)
10391 				ipsqill = ire_to_ill(ire);
10392 		}
10393 
10394 		if ((x_arp_ioctl) && (ipsqill != NULL))
10395 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10396 
10397 		if (ire != NULL) {
10398 			/*
10399 			 * Since the ire obtained from cachetable is used for
10400 			 * mac addr copying below, treat an incomplete ire as if
10401 			 * as if we never found it.
10402 			 */
10403 			if (ire->ire_nce != NULL &&
10404 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10405 				ire_refrele(ire);
10406 				ire = NULL;
10407 				ipsqill = NULL;
10408 				goto errack;
10409 			}
10410 			*flagsp = ATF_INUSE;
10411 			llmp = (ire->ire_nce != NULL ?
10412 			    ire->ire_nce->nce_res_mp : NULL);
10413 			if (llmp != NULL && ipsqill != NULL) {
10414 				uchar_t *macaddr;
10415 
10416 				addr_len = ipsqill->ill_phys_addr_length;
10417 				if (x_arp_ioctl && ((addr_len +
10418 				    ipsqill->ill_name_length) >
10419 				    sizeof (xar->xarp_ha.sdl_data))) {
10420 					ire_refrele(ire);
10421 					freemsg(mp);
10422 					ip_ioctl_finish(q, orig_ioc_mp,
10423 					    EINVAL, NO_COPYOUT, ipsq);
10424 					return;
10425 				}
10426 				*flagsp |= ATF_COM;
10427 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10428 				if (ipsqill->ill_sap_length < 0)
10429 					macaddr = llmp->b_rptr +
10430 					    dlup->dl_dest_addr_offset;
10431 				else
10432 					macaddr = llmp->b_rptr +
10433 					    dlup->dl_dest_addr_offset +
10434 					    ipsqill->ill_sap_length;
10435 				/*
10436 				 * For SIOCGARP, MAC address length
10437 				 * validation has already been done
10438 				 * before the ioctl was issued to ARP to
10439 				 * allow it to progress only on 6 byte
10440 				 * addressable (ethernet like) media. Thus
10441 				 * the mac address copying can not overwrite
10442 				 * the sa_data area below.
10443 				 */
10444 				bcopy(macaddr, storage, addr_len);
10445 			}
10446 			/* Ditch the internal IOCTL. */
10447 			freemsg(mp);
10448 			ire_refrele(ire);
10449 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10450 			return;
10451 		}
10452 	}
10453 
10454 	/*
10455 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10456 	 * on the IPMP meta-interface, ensure any ARP entries added in
10457 	 * ip_sioctl_arp() are deleted.
10458 	 */
10459 	if (IS_IPMP(ill) &&
10460 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10461 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10462 		ipmp_illgrp_t *illg = ill->ill_grp;
10463 		ipmp_arpent_t *entp;
10464 
10465 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10466 			ipmp_illgrp_destroy_arpent(illg, entp);
10467 	}
10468 
10469 	/*
10470 	 * Delete the coresponding IRE_CACHE if any.
10471 	 * Reset the error if there was one (in case there was no entry
10472 	 * in arp.)
10473 	 */
10474 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10475 		ipif_t *ipintf = NULL;
10476 
10477 		if (ifx_arp_ioctl) {
10478 			/*
10479 			 * There's no need to lookup the ill, since
10480 			 * we've already done that when we started
10481 			 * processing the ioctl and sent the message
10482 			 * to ARP on that ill.  So use the ill that
10483 			 * is stored in q->q_ptr.
10484 			 */
10485 			ipintf = ill->ill_ipif;
10486 		}
10487 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10488 			/*
10489 			 * The address in "addr" may be an entry for a
10490 			 * router. If that's true, then any off-net
10491 			 * IRE_CACHE entries that go through the router
10492 			 * with address "addr" must be clobbered. Use
10493 			 * ire_walk to achieve this goal.
10494 			 */
10495 			if (ifx_arp_ioctl)
10496 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10497 				    ire_delete_cache_gw, (char *)&addr, ill);
10498 			else
10499 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10500 				    ALL_ZONES, ipst);
10501 			iocp->ioc_error = 0;
10502 		}
10503 	}
10504 errack:
10505 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10506 		err = iocp->ioc_error;
10507 		freemsg(mp);
10508 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10509 		return;
10510 	}
10511 
10512 	/*
10513 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10514 	 * the area_t into the struct {x}arpreq.
10515 	 */
10516 	if (x_arp_ioctl) {
10517 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10518 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10519 		    sizeof (xar->xarp_ha.sdl_data)) {
10520 			freemsg(mp);
10521 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10522 			    ipsq);
10523 			return;
10524 		}
10525 	}
10526 	*flagsp = ATF_INUSE;
10527 	if (area->area_flags & ACE_F_PERMANENT)
10528 		*flagsp |= ATF_PERM;
10529 	if (area->area_flags & ACE_F_PUBLISH)
10530 		*flagsp |= ATF_PUBL;
10531 	if (area->area_flags & ACE_F_AUTHORITY)
10532 		*flagsp |= ATF_AUTHORITY;
10533 	if (area->area_hw_addr_length != 0) {
10534 		*flagsp |= ATF_COM;
10535 		/*
10536 		 * For SIOCGARP, MAC address length validation has
10537 		 * already been done before the ioctl was issued to ARP
10538 		 * to allow it to progress only on 6 byte addressable
10539 		 * (ethernet like) media. Thus the mac address copying
10540 		 * can not overwrite the sa_data area below.
10541 		 */
10542 		bcopy((char *)area + area->area_hw_addr_offset,
10543 		    storage, area->area_hw_addr_length);
10544 	}
10545 
10546 	/* Ditch the internal IOCTL. */
10547 	freemsg(mp);
10548 	/* Complete the original. */
10549 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10550 }
10551 
10552 /*
10553  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10554  * interface) create the next available logical interface for this
10555  * physical interface.
10556  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10557  * ipif with the specified name.
10558  *
10559  * If the address family is not AF_UNSPEC then set the address as well.
10560  *
10561  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10562  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10563  *
10564  * Executed as a writer on the ill.
10565  * So no lock is needed to traverse the ipif chain, or examine the
10566  * phyint flags.
10567  */
10568 /* ARGSUSED */
10569 int
10570 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10571     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10572 {
10573 	mblk_t	*mp1;
10574 	struct lifreq *lifr;
10575 	boolean_t	isv6;
10576 	boolean_t	exists;
10577 	char 	*name;
10578 	char	*endp;
10579 	char	*cp;
10580 	int	namelen;
10581 	ipif_t	*ipif;
10582 	long	id;
10583 	ipsq_t	*ipsq;
10584 	ill_t	*ill;
10585 	sin_t	*sin;
10586 	int	err = 0;
10587 	boolean_t found_sep = B_FALSE;
10588 	conn_t	*connp;
10589 	zoneid_t zoneid;
10590 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10591 
10592 	ASSERT(q->q_next == NULL);
10593 	ip1dbg(("ip_sioctl_addif\n"));
10594 	/* Existence of mp1 has been checked in ip_wput_nondata */
10595 	mp1 = mp->b_cont->b_cont;
10596 	/*
10597 	 * Null terminate the string to protect against buffer
10598 	 * overrun. String was generated by user code and may not
10599 	 * be trusted.
10600 	 */
10601 	lifr = (struct lifreq *)mp1->b_rptr;
10602 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10603 	name = lifr->lifr_name;
10604 	ASSERT(CONN_Q(q));
10605 	connp = Q_TO_CONN(q);
10606 	isv6 = connp->conn_af_isv6;
10607 	zoneid = connp->conn_zoneid;
10608 	namelen = mi_strlen(name);
10609 	if (namelen == 0)
10610 		return (EINVAL);
10611 
10612 	exists = B_FALSE;
10613 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10614 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10615 		/*
10616 		 * Allow creating lo0 using SIOCLIFADDIF.
10617 		 * can't be any other writer thread. So can pass null below
10618 		 * for the last 4 args to ipif_lookup_name.
10619 		 */
10620 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10621 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10622 		/* Prevent any further action */
10623 		if (ipif == NULL) {
10624 			return (ENOBUFS);
10625 		} else if (!exists) {
10626 			/* We created the ipif now and as writer */
10627 			ipif_refrele(ipif);
10628 			return (0);
10629 		} else {
10630 			ill = ipif->ipif_ill;
10631 			ill_refhold(ill);
10632 			ipif_refrele(ipif);
10633 		}
10634 	} else {
10635 		/* Look for a colon in the name. */
10636 		endp = &name[namelen];
10637 		for (cp = endp; --cp > name; ) {
10638 			if (*cp == IPIF_SEPARATOR_CHAR) {
10639 				found_sep = B_TRUE;
10640 				/*
10641 				 * Reject any non-decimal aliases for plumbing
10642 				 * of logical interfaces. Aliases with leading
10643 				 * zeroes are also rejected as they introduce
10644 				 * ambiguity in the naming of the interfaces.
10645 				 * Comparing with "0" takes care of all such
10646 				 * cases.
10647 				 */
10648 				if ((strncmp("0", cp+1, 1)) == 0)
10649 					return (EINVAL);
10650 
10651 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10652 				    id <= 0 || *endp != '\0') {
10653 					return (EINVAL);
10654 				}
10655 				*cp = '\0';
10656 				break;
10657 			}
10658 		}
10659 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10660 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10661 		if (found_sep)
10662 			*cp = IPIF_SEPARATOR_CHAR;
10663 		if (ill == NULL)
10664 			return (err);
10665 	}
10666 
10667 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10668 	    B_TRUE);
10669 
10670 	/*
10671 	 * Release the refhold due to the lookup, now that we are excl
10672 	 * or we are just returning
10673 	 */
10674 	ill_refrele(ill);
10675 
10676 	if (ipsq == NULL)
10677 		return (EINPROGRESS);
10678 
10679 	/* We are now exclusive on the IPSQ */
10680 	ASSERT(IAM_WRITER_ILL(ill));
10681 
10682 	if (found_sep) {
10683 		/* Now see if there is an IPIF with this unit number. */
10684 		for (ipif = ill->ill_ipif; ipif != NULL;
10685 		    ipif = ipif->ipif_next) {
10686 			if (ipif->ipif_id == id) {
10687 				err = EEXIST;
10688 				goto done;
10689 			}
10690 		}
10691 	}
10692 
10693 	/*
10694 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10695 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10696 	 * instead.
10697 	 */
10698 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10699 	    B_TRUE, B_TRUE)) == NULL) {
10700 		err = ENOBUFS;
10701 		goto done;
10702 	}
10703 
10704 	/* Return created name with ioctl */
10705 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10706 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10707 	ip1dbg(("created %s\n", lifr->lifr_name));
10708 
10709 	/* Set address */
10710 	sin = (sin_t *)&lifr->lifr_addr;
10711 	if (sin->sin_family != AF_UNSPEC) {
10712 		err = ip_sioctl_addr(ipif, sin, q, mp,
10713 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10714 	}
10715 
10716 done:
10717 	ipsq_exit(ipsq);
10718 	return (err);
10719 }
10720 
10721 /*
10722  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10723  * interface) delete it based on the IP address (on this physical interface).
10724  * Otherwise delete it based on the ipif_id.
10725  * Also, special handling to allow a removeif of lo0.
10726  */
10727 /* ARGSUSED */
10728 int
10729 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10730     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10731 {
10732 	conn_t		*connp;
10733 	ill_t		*ill = ipif->ipif_ill;
10734 	boolean_t	 success;
10735 	ip_stack_t	*ipst;
10736 
10737 	ipst = CONNQ_TO_IPST(q);
10738 
10739 	ASSERT(q->q_next == NULL);
10740 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10741 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10742 	ASSERT(IAM_WRITER_IPIF(ipif));
10743 
10744 	connp = Q_TO_CONN(q);
10745 	/*
10746 	 * Special case for unplumbing lo0 (the loopback physical interface).
10747 	 * If unplumbing lo0, the incoming address structure has been
10748 	 * initialized to all zeros. When unplumbing lo0, all its logical
10749 	 * interfaces must be removed too.
10750 	 *
10751 	 * Note that this interface may be called to remove a specific
10752 	 * loopback logical interface (eg, lo0:1). But in that case
10753 	 * ipif->ipif_id != 0 so that the code path for that case is the
10754 	 * same as any other interface (meaning it skips the code directly
10755 	 * below).
10756 	 */
10757 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10758 		if (sin->sin_family == AF_UNSPEC &&
10759 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10760 			/*
10761 			 * Mark it condemned. No new ref. will be made to ill.
10762 			 */
10763 			mutex_enter(&ill->ill_lock);
10764 			ill->ill_state_flags |= ILL_CONDEMNED;
10765 			for (ipif = ill->ill_ipif; ipif != NULL;
10766 			    ipif = ipif->ipif_next) {
10767 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10768 			}
10769 			mutex_exit(&ill->ill_lock);
10770 
10771 			ipif = ill->ill_ipif;
10772 			/* unplumb the loopback interface */
10773 			ill_delete(ill);
10774 			mutex_enter(&connp->conn_lock);
10775 			mutex_enter(&ill->ill_lock);
10776 
10777 			/* Are any references to this ill active */
10778 			if (ill_is_freeable(ill)) {
10779 				mutex_exit(&ill->ill_lock);
10780 				mutex_exit(&connp->conn_lock);
10781 				ill_delete_tail(ill);
10782 				mi_free(ill);
10783 				return (0);
10784 			}
10785 			success = ipsq_pending_mp_add(connp, ipif,
10786 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10787 			mutex_exit(&connp->conn_lock);
10788 			mutex_exit(&ill->ill_lock);
10789 			if (success)
10790 				return (EINPROGRESS);
10791 			else
10792 				return (EINTR);
10793 		}
10794 	}
10795 
10796 	if (ipif->ipif_id == 0) {
10797 		ipsq_t *ipsq;
10798 
10799 		/* Find based on address */
10800 		if (ipif->ipif_isv6) {
10801 			sin6_t *sin6;
10802 
10803 			if (sin->sin_family != AF_INET6)
10804 				return (EAFNOSUPPORT);
10805 
10806 			sin6 = (sin6_t *)sin;
10807 			/* We are a writer, so we should be able to lookup */
10808 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10809 			    ipst);
10810 		} else {
10811 			if (sin->sin_family != AF_INET)
10812 				return (EAFNOSUPPORT);
10813 
10814 			/* We are a writer, so we should be able to lookup */
10815 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10816 			    ipst);
10817 		}
10818 		if (ipif == NULL) {
10819 			return (EADDRNOTAVAIL);
10820 		}
10821 
10822 		/*
10823 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10824 		 * lifr_name of the physical interface but with an ip address
10825 		 * lifr_addr of a logical interface plumbed over it.
10826 		 * So update ipx_current_ipif now that ipif points to the
10827 		 * correct one.
10828 		 */
10829 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10830 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10831 
10832 		/* This is a writer */
10833 		ipif_refrele(ipif);
10834 	}
10835 
10836 	/*
10837 	 * Can not delete instance zero since it is tied to the ill.
10838 	 */
10839 	if (ipif->ipif_id == 0)
10840 		return (EBUSY);
10841 
10842 	mutex_enter(&ill->ill_lock);
10843 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10844 	mutex_exit(&ill->ill_lock);
10845 
10846 	ipif_free(ipif);
10847 
10848 	mutex_enter(&connp->conn_lock);
10849 	mutex_enter(&ill->ill_lock);
10850 
10851 	/* Are any references to this ipif active */
10852 	if (ipif_is_freeable(ipif)) {
10853 		mutex_exit(&ill->ill_lock);
10854 		mutex_exit(&connp->conn_lock);
10855 		ipif_non_duplicate(ipif);
10856 		ipif_down_tail(ipif);
10857 		ipif_free_tail(ipif); /* frees ipif */
10858 		return (0);
10859 	}
10860 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10861 	    IPIF_FREE);
10862 	mutex_exit(&ill->ill_lock);
10863 	mutex_exit(&connp->conn_lock);
10864 	if (success)
10865 		return (EINPROGRESS);
10866 	else
10867 		return (EINTR);
10868 }
10869 
10870 /*
10871  * Restart the removeif ioctl. The refcnt has gone down to 0.
10872  * The ipif is already condemned. So can't find it thru lookups.
10873  */
10874 /* ARGSUSED */
10875 int
10876 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10877     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10878 {
10879 	ill_t *ill = ipif->ipif_ill;
10880 
10881 	ASSERT(IAM_WRITER_IPIF(ipif));
10882 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10883 
10884 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10885 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10886 
10887 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10888 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10889 		ill_delete_tail(ill);
10890 		mi_free(ill);
10891 		return (0);
10892 	}
10893 
10894 	ipif_non_duplicate(ipif);
10895 	ipif_down_tail(ipif);
10896 	ipif_free_tail(ipif);
10897 
10898 	ILL_UNMARK_CHANGING(ill);
10899 	return (0);
10900 }
10901 
10902 /*
10903  * Set the local interface address.
10904  * Allow an address of all zero when the interface is down.
10905  */
10906 /* ARGSUSED */
10907 int
10908 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10909     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10910 {
10911 	int err = 0;
10912 	in6_addr_t v6addr;
10913 	boolean_t need_up = B_FALSE;
10914 
10915 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10916 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10917 
10918 	ASSERT(IAM_WRITER_IPIF(ipif));
10919 
10920 	if (ipif->ipif_isv6) {
10921 		sin6_t *sin6;
10922 		ill_t *ill;
10923 		phyint_t *phyi;
10924 
10925 		if (sin->sin_family != AF_INET6)
10926 			return (EAFNOSUPPORT);
10927 
10928 		sin6 = (sin6_t *)sin;
10929 		v6addr = sin6->sin6_addr;
10930 		ill = ipif->ipif_ill;
10931 		phyi = ill->ill_phyint;
10932 
10933 		/*
10934 		 * Enforce that true multicast interfaces have a link-local
10935 		 * address for logical unit 0.
10936 		 */
10937 		if (ipif->ipif_id == 0 &&
10938 		    (ill->ill_flags & ILLF_MULTICAST) &&
10939 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10940 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10941 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10942 			return (EADDRNOTAVAIL);
10943 		}
10944 
10945 		/*
10946 		 * up interfaces shouldn't have the unspecified address
10947 		 * unless they also have the IPIF_NOLOCAL flags set and
10948 		 * have a subnet assigned.
10949 		 */
10950 		if ((ipif->ipif_flags & IPIF_UP) &&
10951 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10952 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10953 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10954 			return (EADDRNOTAVAIL);
10955 		}
10956 
10957 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10958 			return (EADDRNOTAVAIL);
10959 	} else {
10960 		ipaddr_t addr;
10961 
10962 		if (sin->sin_family != AF_INET)
10963 			return (EAFNOSUPPORT);
10964 
10965 		addr = sin->sin_addr.s_addr;
10966 
10967 		/* Allow 0 as the local address. */
10968 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10969 			return (EADDRNOTAVAIL);
10970 
10971 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10972 	}
10973 
10974 	/*
10975 	 * Even if there is no change we redo things just to rerun
10976 	 * ipif_set_default.
10977 	 */
10978 	if (ipif->ipif_flags & IPIF_UP) {
10979 		/*
10980 		 * Setting a new local address, make sure
10981 		 * we have net and subnet bcast ire's for
10982 		 * the old address if we need them.
10983 		 */
10984 		if (!ipif->ipif_isv6)
10985 			ipif_check_bcast_ires(ipif);
10986 		/*
10987 		 * If the interface is already marked up,
10988 		 * we call ipif_down which will take care
10989 		 * of ditching any IREs that have been set
10990 		 * up based on the old interface address.
10991 		 */
10992 		err = ipif_logical_down(ipif, q, mp);
10993 		if (err == EINPROGRESS)
10994 			return (err);
10995 		ipif_down_tail(ipif);
10996 		need_up = 1;
10997 	}
10998 
10999 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11000 	return (err);
11001 }
11002 
11003 int
11004 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11005     boolean_t need_up)
11006 {
11007 	in6_addr_t v6addr;
11008 	in6_addr_t ov6addr;
11009 	ipaddr_t addr;
11010 	sin6_t	*sin6;
11011 	int	sinlen;
11012 	int	err = 0;
11013 	ill_t	*ill = ipif->ipif_ill;
11014 	boolean_t need_dl_down;
11015 	boolean_t need_arp_down;
11016 	struct iocblk *iocp;
11017 
11018 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11019 
11020 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11021 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11022 	ASSERT(IAM_WRITER_IPIF(ipif));
11023 
11024 	/* Must cancel any pending timer before taking the ill_lock */
11025 	if (ipif->ipif_recovery_id != 0)
11026 		(void) untimeout(ipif->ipif_recovery_id);
11027 	ipif->ipif_recovery_id = 0;
11028 
11029 	if (ipif->ipif_isv6) {
11030 		sin6 = (sin6_t *)sin;
11031 		v6addr = sin6->sin6_addr;
11032 		sinlen = sizeof (struct sockaddr_in6);
11033 	} else {
11034 		addr = sin->sin_addr.s_addr;
11035 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11036 		sinlen = sizeof (struct sockaddr_in);
11037 	}
11038 	mutex_enter(&ill->ill_lock);
11039 	ov6addr = ipif->ipif_v6lcl_addr;
11040 	ipif->ipif_v6lcl_addr = v6addr;
11041 	sctp_update_ipif_addr(ipif, ov6addr);
11042 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11043 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11044 	} else {
11045 		ipif->ipif_v6src_addr = v6addr;
11046 	}
11047 	ipif->ipif_addr_ready = 0;
11048 
11049 	/*
11050 	 * If the interface was previously marked as a duplicate, then since
11051 	 * we've now got a "new" address, it should no longer be considered a
11052 	 * duplicate -- even if the "new" address is the same as the old one.
11053 	 * Note that if all ipifs are down, we may have a pending ARP down
11054 	 * event to handle.  This is because we want to recover from duplicates
11055 	 * and thus delay tearing down ARP until the duplicates have been
11056 	 * removed or disabled.
11057 	 */
11058 	need_dl_down = need_arp_down = B_FALSE;
11059 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11060 		need_arp_down = !need_up;
11061 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11062 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11063 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11064 			need_dl_down = B_TRUE;
11065 		}
11066 	}
11067 
11068 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11069 	    !ill->ill_is_6to4tun) {
11070 		queue_t *wqp = ill->ill_wq;
11071 
11072 		/*
11073 		 * The local address of this interface is a 6to4 address,
11074 		 * check if this interface is in fact a 6to4 tunnel or just
11075 		 * an interface configured with a 6to4 address.  We are only
11076 		 * interested in the former.
11077 		 */
11078 		if (wqp != NULL) {
11079 			while ((wqp->q_next != NULL) &&
11080 			    (wqp->q_next->q_qinfo != NULL) &&
11081 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11082 
11083 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11084 				    == TUN6TO4_MODID) {
11085 					/* set for use in IP */
11086 					ill->ill_is_6to4tun = 1;
11087 					break;
11088 				}
11089 				wqp = wqp->q_next;
11090 			}
11091 		}
11092 	}
11093 
11094 	ipif_set_default(ipif);
11095 
11096 	/*
11097 	 * When publishing an interface address change event, we only notify
11098 	 * the event listeners of the new address.  It is assumed that if they
11099 	 * actively care about the addresses assigned that they will have
11100 	 * already discovered the previous address assigned (if there was one.)
11101 	 *
11102 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11103 	 */
11104 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11105 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11106 		    NE_ADDRESS_CHANGE, sin, sinlen);
11107 	}
11108 
11109 	mutex_exit(&ill->ill_lock);
11110 
11111 	if (need_up) {
11112 		/*
11113 		 * Now bring the interface back up.  If this
11114 		 * is the only IPIF for the ILL, ipif_up
11115 		 * will have to re-bind to the device, so
11116 		 * we may get back EINPROGRESS, in which
11117 		 * case, this IOCTL will get completed in
11118 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11119 		 */
11120 		err = ipif_up(ipif, q, mp);
11121 	}
11122 
11123 	if (need_dl_down)
11124 		ill_dl_down(ill);
11125 	if (need_arp_down)
11126 		ipif_resolver_down(ipif);
11127 
11128 	return (err);
11129 }
11130 
11131 /*
11132  * Restart entry point to restart the address set operation after the
11133  * refcounts have dropped to zero.
11134  */
11135 /* ARGSUSED */
11136 int
11137 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11138     ip_ioctl_cmd_t *ipip, void *ifreq)
11139 {
11140 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11141 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11142 	ASSERT(IAM_WRITER_IPIF(ipif));
11143 	ipif_down_tail(ipif);
11144 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11145 }
11146 
11147 /* ARGSUSED */
11148 int
11149 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11150     ip_ioctl_cmd_t *ipip, void *if_req)
11151 {
11152 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11153 	struct lifreq *lifr = (struct lifreq *)if_req;
11154 
11155 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11156 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11157 	/*
11158 	 * The net mask and address can't change since we have a
11159 	 * reference to the ipif. So no lock is necessary.
11160 	 */
11161 	if (ipif->ipif_isv6) {
11162 		*sin6 = sin6_null;
11163 		sin6->sin6_family = AF_INET6;
11164 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11165 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11166 		lifr->lifr_addrlen =
11167 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11168 	} else {
11169 		*sin = sin_null;
11170 		sin->sin_family = AF_INET;
11171 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11172 		if (ipip->ipi_cmd_type == LIF_CMD) {
11173 			lifr->lifr_addrlen =
11174 			    ip_mask_to_plen(ipif->ipif_net_mask);
11175 		}
11176 	}
11177 	return (0);
11178 }
11179 
11180 /*
11181  * Set the destination address for a pt-pt interface.
11182  */
11183 /* ARGSUSED */
11184 int
11185 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11186     ip_ioctl_cmd_t *ipip, void *if_req)
11187 {
11188 	int err = 0;
11189 	in6_addr_t v6addr;
11190 	boolean_t need_up = B_FALSE;
11191 
11192 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11193 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11194 	ASSERT(IAM_WRITER_IPIF(ipif));
11195 
11196 	if (ipif->ipif_isv6) {
11197 		sin6_t *sin6;
11198 
11199 		if (sin->sin_family != AF_INET6)
11200 			return (EAFNOSUPPORT);
11201 
11202 		sin6 = (sin6_t *)sin;
11203 		v6addr = sin6->sin6_addr;
11204 
11205 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11206 			return (EADDRNOTAVAIL);
11207 	} else {
11208 		ipaddr_t addr;
11209 
11210 		if (sin->sin_family != AF_INET)
11211 			return (EAFNOSUPPORT);
11212 
11213 		addr = sin->sin_addr.s_addr;
11214 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11215 			return (EADDRNOTAVAIL);
11216 
11217 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11218 	}
11219 
11220 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11221 		return (0);	/* No change */
11222 
11223 	if (ipif->ipif_flags & IPIF_UP) {
11224 		/*
11225 		 * If the interface is already marked up,
11226 		 * we call ipif_down which will take care
11227 		 * of ditching any IREs that have been set
11228 		 * up based on the old pp dst address.
11229 		 */
11230 		err = ipif_logical_down(ipif, q, mp);
11231 		if (err == EINPROGRESS)
11232 			return (err);
11233 		ipif_down_tail(ipif);
11234 		need_up = B_TRUE;
11235 	}
11236 	/*
11237 	 * could return EINPROGRESS. If so ioctl will complete in
11238 	 * ip_rput_dlpi_writer
11239 	 */
11240 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11241 	return (err);
11242 }
11243 
11244 static int
11245 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11246     boolean_t need_up)
11247 {
11248 	in6_addr_t v6addr;
11249 	ill_t	*ill = ipif->ipif_ill;
11250 	int	err = 0;
11251 	boolean_t need_dl_down;
11252 	boolean_t need_arp_down;
11253 
11254 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11255 	    ipif->ipif_id, (void *)ipif));
11256 
11257 	/* Must cancel any pending timer before taking the ill_lock */
11258 	if (ipif->ipif_recovery_id != 0)
11259 		(void) untimeout(ipif->ipif_recovery_id);
11260 	ipif->ipif_recovery_id = 0;
11261 
11262 	if (ipif->ipif_isv6) {
11263 		sin6_t *sin6;
11264 
11265 		sin6 = (sin6_t *)sin;
11266 		v6addr = sin6->sin6_addr;
11267 	} else {
11268 		ipaddr_t addr;
11269 
11270 		addr = sin->sin_addr.s_addr;
11271 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11272 	}
11273 	mutex_enter(&ill->ill_lock);
11274 	/* Set point to point destination address. */
11275 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11276 		/*
11277 		 * Allow this as a means of creating logical
11278 		 * pt-pt interfaces on top of e.g. an Ethernet.
11279 		 * XXX Undocumented HACK for testing.
11280 		 * pt-pt interfaces are created with NUD disabled.
11281 		 */
11282 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11283 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11284 		if (ipif->ipif_isv6)
11285 			ill->ill_flags |= ILLF_NONUD;
11286 	}
11287 
11288 	/*
11289 	 * If the interface was previously marked as a duplicate, then since
11290 	 * we've now got a "new" address, it should no longer be considered a
11291 	 * duplicate -- even if the "new" address is the same as the old one.
11292 	 * Note that if all ipifs are down, we may have a pending ARP down
11293 	 * event to handle.
11294 	 */
11295 	need_dl_down = need_arp_down = B_FALSE;
11296 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11297 		need_arp_down = !need_up;
11298 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11299 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11300 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11301 			need_dl_down = B_TRUE;
11302 		}
11303 	}
11304 
11305 	/* Set the new address. */
11306 	ipif->ipif_v6pp_dst_addr = v6addr;
11307 	/* Make sure subnet tracks pp_dst */
11308 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11309 	mutex_exit(&ill->ill_lock);
11310 
11311 	if (need_up) {
11312 		/*
11313 		 * Now bring the interface back up.  If this
11314 		 * is the only IPIF for the ILL, ipif_up
11315 		 * will have to re-bind to the device, so
11316 		 * we may get back EINPROGRESS, in which
11317 		 * case, this IOCTL will get completed in
11318 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11319 		 */
11320 		err = ipif_up(ipif, q, mp);
11321 	}
11322 
11323 	if (need_dl_down)
11324 		ill_dl_down(ill);
11325 	if (need_arp_down)
11326 		ipif_resolver_down(ipif);
11327 
11328 	return (err);
11329 }
11330 
11331 /*
11332  * Restart entry point to restart the dstaddress set operation after the
11333  * refcounts have dropped to zero.
11334  */
11335 /* ARGSUSED */
11336 int
11337 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11338     ip_ioctl_cmd_t *ipip, void *ifreq)
11339 {
11340 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11341 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11342 	ipif_down_tail(ipif);
11343 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11344 }
11345 
11346 /* ARGSUSED */
11347 int
11348 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11349     ip_ioctl_cmd_t *ipip, void *if_req)
11350 {
11351 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11352 
11353 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11354 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11355 	/*
11356 	 * Get point to point destination address. The addresses can't
11357 	 * change since we hold a reference to the ipif.
11358 	 */
11359 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11360 		return (EADDRNOTAVAIL);
11361 
11362 	if (ipif->ipif_isv6) {
11363 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11364 		*sin6 = sin6_null;
11365 		sin6->sin6_family = AF_INET6;
11366 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11367 	} else {
11368 		*sin = sin_null;
11369 		sin->sin_family = AF_INET;
11370 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11371 	}
11372 	return (0);
11373 }
11374 
11375 /*
11376  * Set interface flags.  Many flags require special handling (e.g.,
11377  * bringing the interface down); see below for details.
11378  *
11379  * NOTE : We really don't enforce that ipif_id zero should be used
11380  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11381  *	  is because applications generally does SICGLIFFLAGS and
11382  *	  ORs in the new flags (that affects the logical) and does a
11383  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11384  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11385  *	  flags that will be turned on is correct with respect to
11386  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11387  */
11388 /* ARGSUSED */
11389 int
11390 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11391     ip_ioctl_cmd_t *ipip, void *if_req)
11392 {
11393 	uint64_t turn_on;
11394 	uint64_t turn_off;
11395 	int	err = 0;
11396 	phyint_t *phyi;
11397 	ill_t *ill;
11398 	uint64_t intf_flags, cantchange_flags;
11399 	boolean_t phyint_flags_modified = B_FALSE;
11400 	uint64_t flags;
11401 	struct ifreq *ifr;
11402 	struct lifreq *lifr;
11403 	boolean_t set_linklocal = B_FALSE;
11404 	boolean_t zero_source = B_FALSE;
11405 
11406 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11407 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11408 
11409 	ASSERT(IAM_WRITER_IPIF(ipif));
11410 
11411 	ill = ipif->ipif_ill;
11412 	phyi = ill->ill_phyint;
11413 
11414 	if (ipip->ipi_cmd_type == IF_CMD) {
11415 		ifr = (struct ifreq *)if_req;
11416 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11417 	} else {
11418 		lifr = (struct lifreq *)if_req;
11419 		flags = lifr->lifr_flags;
11420 	}
11421 
11422 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11423 
11424 	/*
11425 	 * Have the flags been set correctly until now?
11426 	 */
11427 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11428 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11429 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11430 	/*
11431 	 * Compare the new flags to the old, and partition
11432 	 * into those coming on and those going off.
11433 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11434 	 */
11435 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11436 		flags |= intf_flags & ~0xFFFF;
11437 
11438 	/*
11439 	 * Explicitly fail attempts to change flags that are always invalid on
11440 	 * an IPMP meta-interface.
11441 	 */
11442 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11443 		return (EINVAL);
11444 
11445 	/*
11446 	 * Check which flags will change; silently ignore flags which userland
11447 	 * is not allowed to control.  (Because these flags may change between
11448 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11449 	 * control, we need to silently ignore them rather than fail.)
11450 	 */
11451 	cantchange_flags = IFF_CANTCHANGE;
11452 	if (IS_IPMP(ill))
11453 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11454 
11455 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11456 	if (turn_on == 0)
11457 		return (0);	/* No change */
11458 
11459 	turn_off = intf_flags & turn_on;
11460 	turn_on ^= turn_off;
11461 
11462 	/*
11463 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11464 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11465 	 * allow it to be turned off.
11466 	 */
11467 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11468 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11469 		return (EINVAL);
11470 
11471 	if (turn_on & IFF_NOFAILOVER) {
11472 		turn_on |= IFF_DEPRECATED;
11473 		flags |= IFF_DEPRECATED;
11474 	}
11475 
11476 	/*
11477 	 * On underlying interfaces, only allow applications to manage test
11478 	 * addresses -- otherwise, they may get confused when the address
11479 	 * moves as part of being brought up.  Likewise, prevent an
11480 	 * application-managed test address from being converted to a data
11481 	 * address.  To prevent migration of administratively up addresses in
11482 	 * the kernel, we don't allow them to be converted either.
11483 	 */
11484 	if (IS_UNDER_IPMP(ill)) {
11485 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11486 
11487 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11488 			return (EINVAL);
11489 
11490 		if ((turn_off & IFF_NOFAILOVER) &&
11491 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11492 			return (EINVAL);
11493 	}
11494 
11495 	/*
11496 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11497 	 * IPv6 interfaces.
11498 	 */
11499 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11500 		return (EINVAL);
11501 
11502 	/*
11503 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11504 	 */
11505 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11506 		return (EINVAL);
11507 
11508 	/*
11509 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11510 	 * interfaces.  It makes no sense in that context.
11511 	 */
11512 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11513 		return (EINVAL);
11514 
11515 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11516 		zero_source = B_TRUE;
11517 
11518 	/*
11519 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11520 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11521 	 * If the link local address isn't set, and can be set, it will get
11522 	 * set later on in this function.
11523 	 */
11524 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11525 	    (flags & IFF_UP) && !zero_source &&
11526 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11527 		if (ipif_cant_setlinklocal(ipif))
11528 			return (EINVAL);
11529 		set_linklocal = B_TRUE;
11530 	}
11531 
11532 	/*
11533 	 * If we modify physical interface flags, we'll potentially need to
11534 	 * send up two routing socket messages for the changes (one for the
11535 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11536 	 */
11537 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11538 		phyint_flags_modified = B_TRUE;
11539 
11540 	/*
11541 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11542 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11543 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11544 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11545 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11546 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11547 	 * will not be honored.
11548 	 */
11549 	if (turn_on & PHYI_STANDBY) {
11550 		/*
11551 		 * No need to grab ill_g_usesrc_lock here; see the
11552 		 * synchronization notes in ip.c.
11553 		 */
11554 		if (ill->ill_usesrc_grp_next != NULL ||
11555 		    intf_flags & PHYI_INACTIVE)
11556 			return (EINVAL);
11557 		if (!(flags & PHYI_FAILED)) {
11558 			flags |= PHYI_INACTIVE;
11559 			turn_on |= PHYI_INACTIVE;
11560 		}
11561 	}
11562 
11563 	if (turn_off & PHYI_STANDBY) {
11564 		flags &= ~PHYI_INACTIVE;
11565 		turn_off |= PHYI_INACTIVE;
11566 	}
11567 
11568 	/*
11569 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11570 	 * would end up on.
11571 	 */
11572 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11573 	    (PHYI_FAILED | PHYI_INACTIVE))
11574 		return (EINVAL);
11575 
11576 	/*
11577 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11578 	 * status of the interface.
11579 	 */
11580 	if ((turn_on | turn_off) & ILLF_ROUTER)
11581 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11582 
11583 	/*
11584 	 * If the interface is not UP and we are not going to
11585 	 * bring it UP, record the flags and return. When the
11586 	 * interface comes UP later, the right actions will be
11587 	 * taken.
11588 	 */
11589 	if (!(ipif->ipif_flags & IPIF_UP) &&
11590 	    !(turn_on & IPIF_UP)) {
11591 		/* Record new flags in their respective places. */
11592 		mutex_enter(&ill->ill_lock);
11593 		mutex_enter(&ill->ill_phyint->phyint_lock);
11594 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11595 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11596 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11597 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11598 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11599 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11600 		mutex_exit(&ill->ill_lock);
11601 		mutex_exit(&ill->ill_phyint->phyint_lock);
11602 
11603 		/*
11604 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11605 		 * same to the kernel: if any of them has been set by
11606 		 * userland, the interface cannot be used for data traffic.
11607 		 */
11608 		if ((turn_on|turn_off) &
11609 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11610 			ASSERT(!IS_IPMP(ill));
11611 			/*
11612 			 * It's possible the ill is part of an "anonymous"
11613 			 * IPMP group rather than a real group.  In that case,
11614 			 * there are no other interfaces in the group and thus
11615 			 * no need to call ipmp_phyint_refresh_active().
11616 			 */
11617 			if (IS_UNDER_IPMP(ill))
11618 				ipmp_phyint_refresh_active(phyi);
11619 		}
11620 
11621 		if (phyint_flags_modified) {
11622 			if (phyi->phyint_illv4 != NULL) {
11623 				ip_rts_ifmsg(phyi->phyint_illv4->
11624 				    ill_ipif, RTSQ_DEFAULT);
11625 			}
11626 			if (phyi->phyint_illv6 != NULL) {
11627 				ip_rts_ifmsg(phyi->phyint_illv6->
11628 				    ill_ipif, RTSQ_DEFAULT);
11629 			}
11630 		}
11631 		return (0);
11632 	} else if (set_linklocal || zero_source) {
11633 		mutex_enter(&ill->ill_lock);
11634 		if (set_linklocal)
11635 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11636 		if (zero_source)
11637 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11638 		mutex_exit(&ill->ill_lock);
11639 	}
11640 
11641 	/*
11642 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11643 	 * or point-to-point interfaces with an unspecified destination. We do
11644 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11645 	 * have a subnet assigned, which is how in.ndpd currently manages its
11646 	 * onlink prefix list when no addresses are configured with those
11647 	 * prefixes.
11648 	 */
11649 	if (ipif->ipif_isv6 &&
11650 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11651 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11652 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11653 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11654 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11655 		return (EINVAL);
11656 	}
11657 
11658 	/*
11659 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11660 	 * from being brought up.
11661 	 */
11662 	if (!ipif->ipif_isv6 &&
11663 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11664 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11665 		return (EINVAL);
11666 	}
11667 
11668 	/*
11669 	 * The only flag changes that we currently take specific action on are
11670 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11671 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11672 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11673 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11674 	 * of bringing it back up will trigger the address to be moved.
11675 	 */
11676 	if ((turn_on|turn_off) &
11677 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11678 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11679 	    IPIF_NOFAILOVER)) {
11680 		/*
11681 		 * Taking this ipif down, make sure we have
11682 		 * valid net and subnet bcast ire's for other
11683 		 * logical interfaces, if we need them.
11684 		 */
11685 		if (!ipif->ipif_isv6)
11686 			ipif_check_bcast_ires(ipif);
11687 
11688 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11689 		    !(turn_off & IPIF_UP)) {
11690 			if (ipif->ipif_flags & IPIF_UP)
11691 				ill->ill_logical_down = 1;
11692 			turn_on &= ~IPIF_UP;
11693 		}
11694 		err = ipif_down(ipif, q, mp);
11695 		ip1dbg(("ipif_down returns %d err ", err));
11696 		if (err == EINPROGRESS)
11697 			return (err);
11698 		ipif_down_tail(ipif);
11699 	}
11700 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11701 }
11702 
11703 static int
11704 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11705 {
11706 	ill_t	*ill;
11707 	phyint_t *phyi;
11708 	uint64_t turn_on, turn_off;
11709 	uint64_t intf_flags, cantchange_flags;
11710 	boolean_t phyint_flags_modified = B_FALSE;
11711 	int	err = 0;
11712 	boolean_t set_linklocal = B_FALSE;
11713 	boolean_t zero_source = B_FALSE;
11714 
11715 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11716 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11717 
11718 	ASSERT(IAM_WRITER_IPIF(ipif));
11719 
11720 	ill = ipif->ipif_ill;
11721 	phyi = ill->ill_phyint;
11722 
11723 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11724 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11725 	if (IS_IPMP(ill))
11726 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11727 
11728 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11729 	turn_off = intf_flags & turn_on;
11730 	turn_on ^= turn_off;
11731 
11732 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11733 		phyint_flags_modified = B_TRUE;
11734 
11735 	/*
11736 	 * Now we change the flags. Track current value of
11737 	 * other flags in their respective places.
11738 	 */
11739 	mutex_enter(&ill->ill_lock);
11740 	mutex_enter(&phyi->phyint_lock);
11741 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11742 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11743 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11744 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11745 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11746 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11747 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11748 		set_linklocal = B_TRUE;
11749 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11750 	}
11751 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11752 		zero_source = B_TRUE;
11753 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11754 	}
11755 	mutex_exit(&ill->ill_lock);
11756 	mutex_exit(&phyi->phyint_lock);
11757 
11758 	if (set_linklocal)
11759 		(void) ipif_setlinklocal(ipif);
11760 
11761 	if (zero_source)
11762 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11763 	else
11764 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11765 
11766 	/*
11767 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11768 	 * the kernel: if any of them has been set by userland, the interface
11769 	 * cannot be used for data traffic.
11770 	 */
11771 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11772 		ASSERT(!IS_IPMP(ill));
11773 		/*
11774 		 * It's possible the ill is part of an "anonymous" IPMP group
11775 		 * rather than a real group.  In that case, there are no other
11776 		 * interfaces in the group and thus no need for us to call
11777 		 * ipmp_phyint_refresh_active().
11778 		 */
11779 		if (IS_UNDER_IPMP(ill))
11780 			ipmp_phyint_refresh_active(phyi);
11781 	}
11782 
11783 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11784 		/*
11785 		 * XXX ipif_up really does not know whether a phyint flags
11786 		 * was modified or not. So, it sends up information on
11787 		 * only one routing sockets message. As we don't bring up
11788 		 * the interface and also set PHYI_ flags simultaneously
11789 		 * it should be okay.
11790 		 */
11791 		err = ipif_up(ipif, q, mp);
11792 	} else {
11793 		/*
11794 		 * Make sure routing socket sees all changes to the flags.
11795 		 * ipif_up_done* handles this when we use ipif_up.
11796 		 */
11797 		if (phyint_flags_modified) {
11798 			if (phyi->phyint_illv4 != NULL) {
11799 				ip_rts_ifmsg(phyi->phyint_illv4->
11800 				    ill_ipif, RTSQ_DEFAULT);
11801 			}
11802 			if (phyi->phyint_illv6 != NULL) {
11803 				ip_rts_ifmsg(phyi->phyint_illv6->
11804 				    ill_ipif, RTSQ_DEFAULT);
11805 			}
11806 		} else {
11807 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11808 		}
11809 		/*
11810 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11811 		 * this in need_up case.
11812 		 */
11813 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11814 	}
11815 	return (err);
11816 }
11817 
11818 /*
11819  * Restart the flags operation now that the refcounts have dropped to zero.
11820  */
11821 /* ARGSUSED */
11822 int
11823 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11824     ip_ioctl_cmd_t *ipip, void *if_req)
11825 {
11826 	uint64_t flags;
11827 	struct ifreq *ifr = if_req;
11828 	struct lifreq *lifr = if_req;
11829 
11830 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11831 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11832 
11833 	ipif_down_tail(ipif);
11834 	if (ipip->ipi_cmd_type == IF_CMD) {
11835 		/* cast to uint16_t prevents unwanted sign extension */
11836 		flags = (uint16_t)ifr->ifr_flags;
11837 	} else {
11838 		flags = lifr->lifr_flags;
11839 	}
11840 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11841 }
11842 
11843 /*
11844  * Can operate on either a module or a driver queue.
11845  */
11846 /* ARGSUSED */
11847 int
11848 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11849     ip_ioctl_cmd_t *ipip, void *if_req)
11850 {
11851 	/*
11852 	 * Has the flags been set correctly till now ?
11853 	 */
11854 	ill_t *ill = ipif->ipif_ill;
11855 	phyint_t *phyi = ill->ill_phyint;
11856 
11857 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11858 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11859 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11860 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11861 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11862 
11863 	/*
11864 	 * Need a lock since some flags can be set even when there are
11865 	 * references to the ipif.
11866 	 */
11867 	mutex_enter(&ill->ill_lock);
11868 	if (ipip->ipi_cmd_type == IF_CMD) {
11869 		struct ifreq *ifr = (struct ifreq *)if_req;
11870 
11871 		/* Get interface flags (low 16 only). */
11872 		ifr->ifr_flags = ((ipif->ipif_flags |
11873 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11874 	} else {
11875 		struct lifreq *lifr = (struct lifreq *)if_req;
11876 
11877 		/* Get interface flags. */
11878 		lifr->lifr_flags = ipif->ipif_flags |
11879 		    ill->ill_flags | phyi->phyint_flags;
11880 	}
11881 	mutex_exit(&ill->ill_lock);
11882 	return (0);
11883 }
11884 
11885 /* ARGSUSED */
11886 int
11887 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11888     ip_ioctl_cmd_t *ipip, void *if_req)
11889 {
11890 	int mtu;
11891 	int ip_min_mtu;
11892 	struct ifreq	*ifr;
11893 	struct lifreq *lifr;
11894 	ire_t	*ire;
11895 	ip_stack_t *ipst;
11896 
11897 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11898 	    ipif->ipif_id, (void *)ipif));
11899 	if (ipip->ipi_cmd_type == IF_CMD) {
11900 		ifr = (struct ifreq *)if_req;
11901 		mtu = ifr->ifr_metric;
11902 	} else {
11903 		lifr = (struct lifreq *)if_req;
11904 		mtu = lifr->lifr_mtu;
11905 	}
11906 
11907 	if (ipif->ipif_isv6)
11908 		ip_min_mtu = IPV6_MIN_MTU;
11909 	else
11910 		ip_min_mtu = IP_MIN_MTU;
11911 
11912 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11913 		return (EINVAL);
11914 
11915 	/*
11916 	 * Change the MTU size in all relevant ire's.
11917 	 * Mtu change Vs. new ire creation - protocol below.
11918 	 * First change ipif_mtu and the ire_max_frag of the
11919 	 * interface ire. Then do an ire walk and change the
11920 	 * ire_max_frag of all affected ires. During ire_add
11921 	 * under the bucket lock, set the ire_max_frag of the
11922 	 * new ire being created from the ipif/ire from which
11923 	 * it is being derived. If an mtu change happens after
11924 	 * the ire is added, the new ire will be cleaned up.
11925 	 * Conversely if the mtu change happens before the ire
11926 	 * is added, ire_add will see the new value of the mtu.
11927 	 */
11928 	ipif->ipif_mtu = mtu;
11929 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11930 
11931 	if (ipif->ipif_isv6)
11932 		ire = ipif_to_ire_v6(ipif);
11933 	else
11934 		ire = ipif_to_ire(ipif);
11935 	if (ire != NULL) {
11936 		ire->ire_max_frag = ipif->ipif_mtu;
11937 		ire_refrele(ire);
11938 	}
11939 	ipst = ipif->ipif_ill->ill_ipst;
11940 	if (ipif->ipif_flags & IPIF_UP) {
11941 		if (ipif->ipif_isv6)
11942 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11943 			    ipst);
11944 		else
11945 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11946 			    ipst);
11947 	}
11948 	/* Update the MTU in SCTP's list */
11949 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11950 	return (0);
11951 }
11952 
11953 /* Get interface MTU. */
11954 /* ARGSUSED */
11955 int
11956 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11957 	ip_ioctl_cmd_t *ipip, void *if_req)
11958 {
11959 	struct ifreq	*ifr;
11960 	struct lifreq	*lifr;
11961 
11962 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11963 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11964 	if (ipip->ipi_cmd_type == IF_CMD) {
11965 		ifr = (struct ifreq *)if_req;
11966 		ifr->ifr_metric = ipif->ipif_mtu;
11967 	} else {
11968 		lifr = (struct lifreq *)if_req;
11969 		lifr->lifr_mtu = ipif->ipif_mtu;
11970 	}
11971 	return (0);
11972 }
11973 
11974 /* Set interface broadcast address. */
11975 /* ARGSUSED2 */
11976 int
11977 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11978 	ip_ioctl_cmd_t *ipip, void *if_req)
11979 {
11980 	ipaddr_t addr;
11981 	ire_t	*ire;
11982 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11983 
11984 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11985 	    ipif->ipif_id));
11986 
11987 	ASSERT(IAM_WRITER_IPIF(ipif));
11988 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11989 		return (EADDRNOTAVAIL);
11990 
11991 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11992 
11993 	if (sin->sin_family != AF_INET)
11994 		return (EAFNOSUPPORT);
11995 
11996 	addr = sin->sin_addr.s_addr;
11997 	if (ipif->ipif_flags & IPIF_UP) {
11998 		/*
11999 		 * If we are already up, make sure the new
12000 		 * broadcast address makes sense.  If it does,
12001 		 * there should be an IRE for it already.
12002 		 * Don't match on ipif, only on the ill
12003 		 * since we are sharing these now.
12004 		 */
12005 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12006 		    ipif, ALL_ZONES, NULL,
12007 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12008 		if (ire == NULL) {
12009 			return (EINVAL);
12010 		} else {
12011 			ire_refrele(ire);
12012 		}
12013 	}
12014 	/*
12015 	 * Changing the broadcast addr for this ipif.
12016 	 * Make sure we have valid net and subnet bcast
12017 	 * ire's for other logical interfaces, if needed.
12018 	 */
12019 	if (addr != ipif->ipif_brd_addr)
12020 		ipif_check_bcast_ires(ipif);
12021 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12022 	return (0);
12023 }
12024 
12025 /* Get interface broadcast address. */
12026 /* ARGSUSED */
12027 int
12028 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12029     ip_ioctl_cmd_t *ipip, void *if_req)
12030 {
12031 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12032 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12033 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12034 		return (EADDRNOTAVAIL);
12035 
12036 	/* IPIF_BROADCAST not possible with IPv6 */
12037 	ASSERT(!ipif->ipif_isv6);
12038 	*sin = sin_null;
12039 	sin->sin_family = AF_INET;
12040 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12041 	return (0);
12042 }
12043 
12044 /*
12045  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12046  */
12047 /* ARGSUSED */
12048 int
12049 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12050     ip_ioctl_cmd_t *ipip, void *if_req)
12051 {
12052 	int err = 0;
12053 	in6_addr_t v6mask;
12054 
12055 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12056 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12057 
12058 	ASSERT(IAM_WRITER_IPIF(ipif));
12059 
12060 	if (ipif->ipif_isv6) {
12061 		sin6_t *sin6;
12062 
12063 		if (sin->sin_family != AF_INET6)
12064 			return (EAFNOSUPPORT);
12065 
12066 		sin6 = (sin6_t *)sin;
12067 		v6mask = sin6->sin6_addr;
12068 	} else {
12069 		ipaddr_t mask;
12070 
12071 		if (sin->sin_family != AF_INET)
12072 			return (EAFNOSUPPORT);
12073 
12074 		mask = sin->sin_addr.s_addr;
12075 		V4MASK_TO_V6(mask, v6mask);
12076 	}
12077 
12078 	/*
12079 	 * No big deal if the interface isn't already up, or the mask
12080 	 * isn't really changing, or this is pt-pt.
12081 	 */
12082 	if (!(ipif->ipif_flags & IPIF_UP) ||
12083 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12084 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12085 		ipif->ipif_v6net_mask = v6mask;
12086 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12087 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12088 			    ipif->ipif_v6net_mask,
12089 			    ipif->ipif_v6subnet);
12090 		}
12091 		return (0);
12092 	}
12093 	/*
12094 	 * Make sure we have valid net and subnet broadcast ire's
12095 	 * for the old netmask, if needed by other logical interfaces.
12096 	 */
12097 	if (!ipif->ipif_isv6)
12098 		ipif_check_bcast_ires(ipif);
12099 
12100 	err = ipif_logical_down(ipif, q, mp);
12101 	if (err == EINPROGRESS)
12102 		return (err);
12103 	ipif_down_tail(ipif);
12104 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12105 	return (err);
12106 }
12107 
12108 static int
12109 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12110 {
12111 	in6_addr_t v6mask;
12112 	int err = 0;
12113 
12114 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12115 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12116 
12117 	if (ipif->ipif_isv6) {
12118 		sin6_t *sin6;
12119 
12120 		sin6 = (sin6_t *)sin;
12121 		v6mask = sin6->sin6_addr;
12122 	} else {
12123 		ipaddr_t mask;
12124 
12125 		mask = sin->sin_addr.s_addr;
12126 		V4MASK_TO_V6(mask, v6mask);
12127 	}
12128 
12129 	ipif->ipif_v6net_mask = v6mask;
12130 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12131 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12132 		    ipif->ipif_v6subnet);
12133 	}
12134 	err = ipif_up(ipif, q, mp);
12135 
12136 	if (err == 0 || err == EINPROGRESS) {
12137 		/*
12138 		 * The interface must be DL_BOUND if this packet has to
12139 		 * go out on the wire. Since we only go through a logical
12140 		 * down and are bound with the driver during an internal
12141 		 * down/up that is satisfied.
12142 		 */
12143 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12144 			/* Potentially broadcast an address mask reply. */
12145 			ipif_mask_reply(ipif);
12146 		}
12147 	}
12148 	return (err);
12149 }
12150 
12151 /* ARGSUSED */
12152 int
12153 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12154     ip_ioctl_cmd_t *ipip, void *if_req)
12155 {
12156 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12157 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12158 	ipif_down_tail(ipif);
12159 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12160 }
12161 
12162 /* Get interface net mask. */
12163 /* ARGSUSED */
12164 int
12165 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12166     ip_ioctl_cmd_t *ipip, void *if_req)
12167 {
12168 	struct lifreq *lifr = (struct lifreq *)if_req;
12169 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12170 
12171 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12172 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12173 
12174 	/*
12175 	 * net mask can't change since we have a reference to the ipif.
12176 	 */
12177 	if (ipif->ipif_isv6) {
12178 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12179 		*sin6 = sin6_null;
12180 		sin6->sin6_family = AF_INET6;
12181 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12182 		lifr->lifr_addrlen =
12183 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12184 	} else {
12185 		*sin = sin_null;
12186 		sin->sin_family = AF_INET;
12187 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12188 		if (ipip->ipi_cmd_type == LIF_CMD) {
12189 			lifr->lifr_addrlen =
12190 			    ip_mask_to_plen(ipif->ipif_net_mask);
12191 		}
12192 	}
12193 	return (0);
12194 }
12195 
12196 /* ARGSUSED */
12197 int
12198 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12199     ip_ioctl_cmd_t *ipip, void *if_req)
12200 {
12201 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12202 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12203 
12204 	/*
12205 	 * Since no applications should ever be setting metrics on underlying
12206 	 * interfaces, we explicitly fail to smoke 'em out.
12207 	 */
12208 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12209 		return (EINVAL);
12210 
12211 	/*
12212 	 * Set interface metric.  We don't use this for
12213 	 * anything but we keep track of it in case it is
12214 	 * important to routing applications or such.
12215 	 */
12216 	if (ipip->ipi_cmd_type == IF_CMD) {
12217 		struct ifreq    *ifr;
12218 
12219 		ifr = (struct ifreq *)if_req;
12220 		ipif->ipif_metric = ifr->ifr_metric;
12221 	} else {
12222 		struct lifreq   *lifr;
12223 
12224 		lifr = (struct lifreq *)if_req;
12225 		ipif->ipif_metric = lifr->lifr_metric;
12226 	}
12227 	return (0);
12228 }
12229 
12230 /* ARGSUSED */
12231 int
12232 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12233     ip_ioctl_cmd_t *ipip, void *if_req)
12234 {
12235 	/* Get interface metric. */
12236 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12237 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12238 
12239 	if (ipip->ipi_cmd_type == IF_CMD) {
12240 		struct ifreq    *ifr;
12241 
12242 		ifr = (struct ifreq *)if_req;
12243 		ifr->ifr_metric = ipif->ipif_metric;
12244 	} else {
12245 		struct lifreq   *lifr;
12246 
12247 		lifr = (struct lifreq *)if_req;
12248 		lifr->lifr_metric = ipif->ipif_metric;
12249 	}
12250 
12251 	return (0);
12252 }
12253 
12254 /* ARGSUSED */
12255 int
12256 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12257     ip_ioctl_cmd_t *ipip, void *if_req)
12258 {
12259 
12260 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12261 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12262 	/*
12263 	 * Set the muxid returned from I_PLINK.
12264 	 */
12265 	if (ipip->ipi_cmd_type == IF_CMD) {
12266 		struct ifreq *ifr = (struct ifreq *)if_req;
12267 
12268 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12269 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12270 	} else {
12271 		struct lifreq *lifr = (struct lifreq *)if_req;
12272 
12273 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12274 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12275 	}
12276 	return (0);
12277 }
12278 
12279 /* ARGSUSED */
12280 int
12281 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12282     ip_ioctl_cmd_t *ipip, void *if_req)
12283 {
12284 
12285 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12286 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12287 	/*
12288 	 * Get the muxid saved in ill for I_PUNLINK.
12289 	 */
12290 	if (ipip->ipi_cmd_type == IF_CMD) {
12291 		struct ifreq *ifr = (struct ifreq *)if_req;
12292 
12293 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12294 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12295 	} else {
12296 		struct lifreq *lifr = (struct lifreq *)if_req;
12297 
12298 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12299 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12300 	}
12301 	return (0);
12302 }
12303 
12304 /*
12305  * Set the subnet prefix. Does not modify the broadcast address.
12306  */
12307 /* ARGSUSED */
12308 int
12309 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12310     ip_ioctl_cmd_t *ipip, void *if_req)
12311 {
12312 	int err = 0;
12313 	in6_addr_t v6addr;
12314 	in6_addr_t v6mask;
12315 	boolean_t need_up = B_FALSE;
12316 	int addrlen;
12317 
12318 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12319 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12320 
12321 	ASSERT(IAM_WRITER_IPIF(ipif));
12322 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12323 
12324 	if (ipif->ipif_isv6) {
12325 		sin6_t *sin6;
12326 
12327 		if (sin->sin_family != AF_INET6)
12328 			return (EAFNOSUPPORT);
12329 
12330 		sin6 = (sin6_t *)sin;
12331 		v6addr = sin6->sin6_addr;
12332 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12333 			return (EADDRNOTAVAIL);
12334 	} else {
12335 		ipaddr_t addr;
12336 
12337 		if (sin->sin_family != AF_INET)
12338 			return (EAFNOSUPPORT);
12339 
12340 		addr = sin->sin_addr.s_addr;
12341 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12342 			return (EADDRNOTAVAIL);
12343 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12344 		/* Add 96 bits */
12345 		addrlen += IPV6_ABITS - IP_ABITS;
12346 	}
12347 
12348 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12349 		return (EINVAL);
12350 
12351 	/* Check if bits in the address is set past the mask */
12352 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12353 		return (EINVAL);
12354 
12355 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12356 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12357 		return (0);	/* No change */
12358 
12359 	if (ipif->ipif_flags & IPIF_UP) {
12360 		/*
12361 		 * If the interface is already marked up,
12362 		 * we call ipif_down which will take care
12363 		 * of ditching any IREs that have been set
12364 		 * up based on the old interface address.
12365 		 */
12366 		err = ipif_logical_down(ipif, q, mp);
12367 		if (err == EINPROGRESS)
12368 			return (err);
12369 		ipif_down_tail(ipif);
12370 		need_up = B_TRUE;
12371 	}
12372 
12373 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12374 	return (err);
12375 }
12376 
12377 static int
12378 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12379     queue_t *q, mblk_t *mp, boolean_t need_up)
12380 {
12381 	ill_t	*ill = ipif->ipif_ill;
12382 	int	err = 0;
12383 
12384 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12385 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12386 
12387 	/* Set the new address. */
12388 	mutex_enter(&ill->ill_lock);
12389 	ipif->ipif_v6net_mask = v6mask;
12390 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12391 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12392 		    ipif->ipif_v6subnet);
12393 	}
12394 	mutex_exit(&ill->ill_lock);
12395 
12396 	if (need_up) {
12397 		/*
12398 		 * Now bring the interface back up.  If this
12399 		 * is the only IPIF for the ILL, ipif_up
12400 		 * will have to re-bind to the device, so
12401 		 * we may get back EINPROGRESS, in which
12402 		 * case, this IOCTL will get completed in
12403 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12404 		 */
12405 		err = ipif_up(ipif, q, mp);
12406 		if (err == EINPROGRESS)
12407 			return (err);
12408 	}
12409 	return (err);
12410 }
12411 
12412 /* ARGSUSED */
12413 int
12414 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12415     ip_ioctl_cmd_t *ipip, void *if_req)
12416 {
12417 	int	addrlen;
12418 	in6_addr_t v6addr;
12419 	in6_addr_t v6mask;
12420 	struct lifreq *lifr = (struct lifreq *)if_req;
12421 
12422 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12423 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12424 	ipif_down_tail(ipif);
12425 
12426 	addrlen = lifr->lifr_addrlen;
12427 	if (ipif->ipif_isv6) {
12428 		sin6_t *sin6;
12429 
12430 		sin6 = (sin6_t *)sin;
12431 		v6addr = sin6->sin6_addr;
12432 	} else {
12433 		ipaddr_t addr;
12434 
12435 		addr = sin->sin_addr.s_addr;
12436 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12437 		addrlen += IPV6_ABITS - IP_ABITS;
12438 	}
12439 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12440 
12441 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12442 }
12443 
12444 /* ARGSUSED */
12445 int
12446 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12447     ip_ioctl_cmd_t *ipip, void *if_req)
12448 {
12449 	struct lifreq *lifr = (struct lifreq *)if_req;
12450 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12451 
12452 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12453 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12454 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12455 
12456 	if (ipif->ipif_isv6) {
12457 		*sin6 = sin6_null;
12458 		sin6->sin6_family = AF_INET6;
12459 		sin6->sin6_addr = ipif->ipif_v6subnet;
12460 		lifr->lifr_addrlen =
12461 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12462 	} else {
12463 		*sin = sin_null;
12464 		sin->sin_family = AF_INET;
12465 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12466 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12467 	}
12468 	return (0);
12469 }
12470 
12471 /*
12472  * Set the IPv6 address token.
12473  */
12474 /* ARGSUSED */
12475 int
12476 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12477     ip_ioctl_cmd_t *ipi, void *if_req)
12478 {
12479 	ill_t *ill = ipif->ipif_ill;
12480 	int err;
12481 	in6_addr_t v6addr;
12482 	in6_addr_t v6mask;
12483 	boolean_t need_up = B_FALSE;
12484 	int i;
12485 	sin6_t *sin6 = (sin6_t *)sin;
12486 	struct lifreq *lifr = (struct lifreq *)if_req;
12487 	int addrlen;
12488 
12489 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12490 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12491 	ASSERT(IAM_WRITER_IPIF(ipif));
12492 
12493 	addrlen = lifr->lifr_addrlen;
12494 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12495 	if (ipif->ipif_id != 0)
12496 		return (EINVAL);
12497 
12498 	if (!ipif->ipif_isv6)
12499 		return (EINVAL);
12500 
12501 	if (addrlen > IPV6_ABITS)
12502 		return (EINVAL);
12503 
12504 	v6addr = sin6->sin6_addr;
12505 
12506 	/*
12507 	 * The length of the token is the length from the end.  To get
12508 	 * the proper mask for this, compute the mask of the bits not
12509 	 * in the token; ie. the prefix, and then xor to get the mask.
12510 	 */
12511 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12512 		return (EINVAL);
12513 	for (i = 0; i < 4; i++) {
12514 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12515 	}
12516 
12517 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12518 	    ill->ill_token_length == addrlen)
12519 		return (0);	/* No change */
12520 
12521 	if (ipif->ipif_flags & IPIF_UP) {
12522 		err = ipif_logical_down(ipif, q, mp);
12523 		if (err == EINPROGRESS)
12524 			return (err);
12525 		ipif_down_tail(ipif);
12526 		need_up = B_TRUE;
12527 	}
12528 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12529 	return (err);
12530 }
12531 
12532 static int
12533 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12534     mblk_t *mp, boolean_t need_up)
12535 {
12536 	in6_addr_t v6addr;
12537 	in6_addr_t v6mask;
12538 	ill_t	*ill = ipif->ipif_ill;
12539 	int	i;
12540 	int	err = 0;
12541 
12542 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12543 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12544 	v6addr = sin6->sin6_addr;
12545 	/*
12546 	 * The length of the token is the length from the end.  To get
12547 	 * the proper mask for this, compute the mask of the bits not
12548 	 * in the token; ie. the prefix, and then xor to get the mask.
12549 	 */
12550 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12551 	for (i = 0; i < 4; i++)
12552 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12553 
12554 	mutex_enter(&ill->ill_lock);
12555 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12556 	ill->ill_token_length = addrlen;
12557 	mutex_exit(&ill->ill_lock);
12558 
12559 	if (need_up) {
12560 		/*
12561 		 * Now bring the interface back up.  If this
12562 		 * is the only IPIF for the ILL, ipif_up
12563 		 * will have to re-bind to the device, so
12564 		 * we may get back EINPROGRESS, in which
12565 		 * case, this IOCTL will get completed in
12566 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12567 		 */
12568 		err = ipif_up(ipif, q, mp);
12569 		if (err == EINPROGRESS)
12570 			return (err);
12571 	}
12572 	return (err);
12573 }
12574 
12575 /* ARGSUSED */
12576 int
12577 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12578     ip_ioctl_cmd_t *ipi, void *if_req)
12579 {
12580 	ill_t *ill;
12581 	sin6_t *sin6 = (sin6_t *)sin;
12582 	struct lifreq *lifr = (struct lifreq *)if_req;
12583 
12584 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12585 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12586 	if (ipif->ipif_id != 0)
12587 		return (EINVAL);
12588 
12589 	ill = ipif->ipif_ill;
12590 	if (!ill->ill_isv6)
12591 		return (ENXIO);
12592 
12593 	*sin6 = sin6_null;
12594 	sin6->sin6_family = AF_INET6;
12595 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12596 	sin6->sin6_addr = ill->ill_token;
12597 	lifr->lifr_addrlen = ill->ill_token_length;
12598 	return (0);
12599 }
12600 
12601 /*
12602  * Set (hardware) link specific information that might override
12603  * what was acquired through the DL_INFO_ACK.
12604  * The logic is as follows.
12605  *
12606  * become exclusive
12607  * set CHANGING flag
12608  * change mtu on affected IREs
12609  * clear CHANGING flag
12610  *
12611  * An ire add that occurs before the CHANGING flag is set will have its mtu
12612  * changed by the ip_sioctl_lnkinfo.
12613  *
12614  * During the time the CHANGING flag is set, no new ires will be added to the
12615  * bucket, and ire add will fail (due the CHANGING flag).
12616  *
12617  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12618  * before it is added to the bucket.
12619  *
12620  * Obviously only 1 thread can set the CHANGING flag and we need to become
12621  * exclusive to set the flag.
12622  */
12623 /* ARGSUSED */
12624 int
12625 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12626     ip_ioctl_cmd_t *ipi, void *if_req)
12627 {
12628 	ill_t		*ill = ipif->ipif_ill;
12629 	ipif_t		*nipif;
12630 	int		ip_min_mtu;
12631 	boolean_t	mtu_walk = B_FALSE;
12632 	struct lifreq	*lifr = (struct lifreq *)if_req;
12633 	lif_ifinfo_req_t *lir;
12634 	ire_t		*ire;
12635 
12636 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12637 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12638 	lir = &lifr->lifr_ifinfo;
12639 	ASSERT(IAM_WRITER_IPIF(ipif));
12640 
12641 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12642 	if (ipif->ipif_id != 0)
12643 		return (EINVAL);
12644 
12645 	/* Set interface MTU. */
12646 	if (ipif->ipif_isv6)
12647 		ip_min_mtu = IPV6_MIN_MTU;
12648 	else
12649 		ip_min_mtu = IP_MIN_MTU;
12650 
12651 	/*
12652 	 * Verify values before we set anything. Allow zero to
12653 	 * mean unspecified.
12654 	 */
12655 	if (lir->lir_maxmtu != 0 &&
12656 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12657 	    lir->lir_maxmtu < ip_min_mtu))
12658 		return (EINVAL);
12659 	if (lir->lir_reachtime != 0 &&
12660 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12661 		return (EINVAL);
12662 	if (lir->lir_reachretrans != 0 &&
12663 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12664 		return (EINVAL);
12665 
12666 	mutex_enter(&ill->ill_lock);
12667 	ill->ill_state_flags |= ILL_CHANGING;
12668 	for (nipif = ill->ill_ipif; nipif != NULL;
12669 	    nipif = nipif->ipif_next) {
12670 		nipif->ipif_state_flags |= IPIF_CHANGING;
12671 	}
12672 
12673 	if (lir->lir_maxmtu != 0) {
12674 		ill->ill_max_mtu = lir->lir_maxmtu;
12675 		ill->ill_user_mtu = lir->lir_maxmtu;
12676 		mtu_walk = B_TRUE;
12677 	}
12678 	mutex_exit(&ill->ill_lock);
12679 
12680 	if (lir->lir_reachtime != 0)
12681 		ill->ill_reachable_time = lir->lir_reachtime;
12682 
12683 	if (lir->lir_reachretrans != 0)
12684 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12685 
12686 	ill->ill_max_hops = lir->lir_maxhops;
12687 
12688 	ill->ill_max_buf = ND_MAX_Q;
12689 
12690 	if (mtu_walk) {
12691 		/*
12692 		 * Set the MTU on all ipifs associated with this ill except
12693 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12694 		 */
12695 		for (nipif = ill->ill_ipif; nipif != NULL;
12696 		    nipif = nipif->ipif_next) {
12697 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12698 				continue;
12699 
12700 			nipif->ipif_mtu = ill->ill_max_mtu;
12701 
12702 			if (!(nipif->ipif_flags & IPIF_UP))
12703 				continue;
12704 
12705 			if (nipif->ipif_isv6)
12706 				ire = ipif_to_ire_v6(nipif);
12707 			else
12708 				ire = ipif_to_ire(nipif);
12709 			if (ire != NULL) {
12710 				ire->ire_max_frag = ipif->ipif_mtu;
12711 				ire_refrele(ire);
12712 			}
12713 
12714 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12715 			    nipif, ill);
12716 		}
12717 	}
12718 
12719 	mutex_enter(&ill->ill_lock);
12720 	for (nipif = ill->ill_ipif; nipif != NULL;
12721 	    nipif = nipif->ipif_next) {
12722 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12723 	}
12724 	ILL_UNMARK_CHANGING(ill);
12725 	mutex_exit(&ill->ill_lock);
12726 
12727 	/*
12728 	 * Refresh IPMP meta-interface MTU if necessary.
12729 	 */
12730 	if (IS_UNDER_IPMP(ill))
12731 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12732 
12733 	return (0);
12734 }
12735 
12736 /* ARGSUSED */
12737 int
12738 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12739     ip_ioctl_cmd_t *ipi, void *if_req)
12740 {
12741 	struct lif_ifinfo_req *lir;
12742 	ill_t *ill = ipif->ipif_ill;
12743 
12744 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12745 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12746 	if (ipif->ipif_id != 0)
12747 		return (EINVAL);
12748 
12749 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12750 	lir->lir_maxhops = ill->ill_max_hops;
12751 	lir->lir_reachtime = ill->ill_reachable_time;
12752 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12753 	lir->lir_maxmtu = ill->ill_max_mtu;
12754 
12755 	return (0);
12756 }
12757 
12758 /*
12759  * Return best guess as to the subnet mask for the specified address.
12760  * Based on the subnet masks for all the configured interfaces.
12761  *
12762  * We end up returning a zero mask in the case of default, multicast or
12763  * experimental.
12764  */
12765 static ipaddr_t
12766 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12767 {
12768 	ipaddr_t net_mask;
12769 	ill_t	*ill;
12770 	ipif_t	*ipif;
12771 	ill_walk_context_t ctx;
12772 	ipif_t	*fallback_ipif = NULL;
12773 
12774 	net_mask = ip_net_mask(addr);
12775 	if (net_mask == 0) {
12776 		*ipifp = NULL;
12777 		return (0);
12778 	}
12779 
12780 	/* Let's check to see if this is maybe a local subnet route. */
12781 	/* this function only applies to IPv4 interfaces */
12782 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12783 	ill = ILL_START_WALK_V4(&ctx, ipst);
12784 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12785 		mutex_enter(&ill->ill_lock);
12786 		for (ipif = ill->ill_ipif; ipif != NULL;
12787 		    ipif = ipif->ipif_next) {
12788 			if (!IPIF_CAN_LOOKUP(ipif))
12789 				continue;
12790 			if (!(ipif->ipif_flags & IPIF_UP))
12791 				continue;
12792 			if ((ipif->ipif_subnet & net_mask) ==
12793 			    (addr & net_mask)) {
12794 				/*
12795 				 * Don't trust pt-pt interfaces if there are
12796 				 * other interfaces.
12797 				 */
12798 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12799 					if (fallback_ipif == NULL) {
12800 						ipif_refhold_locked(ipif);
12801 						fallback_ipif = ipif;
12802 					}
12803 					continue;
12804 				}
12805 
12806 				/*
12807 				 * Fine. Just assume the same net mask as the
12808 				 * directly attached subnet interface is using.
12809 				 */
12810 				ipif_refhold_locked(ipif);
12811 				mutex_exit(&ill->ill_lock);
12812 				rw_exit(&ipst->ips_ill_g_lock);
12813 				if (fallback_ipif != NULL)
12814 					ipif_refrele(fallback_ipif);
12815 				*ipifp = ipif;
12816 				return (ipif->ipif_net_mask);
12817 			}
12818 		}
12819 		mutex_exit(&ill->ill_lock);
12820 	}
12821 	rw_exit(&ipst->ips_ill_g_lock);
12822 
12823 	*ipifp = fallback_ipif;
12824 	return ((fallback_ipif != NULL) ?
12825 	    fallback_ipif->ipif_net_mask : net_mask);
12826 }
12827 
12828 /*
12829  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12830  */
12831 static void
12832 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12833 {
12834 	IOCP	iocp;
12835 	ipft_t	*ipft;
12836 	ipllc_t	*ipllc;
12837 	mblk_t	*mp1;
12838 	cred_t	*cr;
12839 	int	error = 0;
12840 	conn_t	*connp;
12841 
12842 	ip1dbg(("ip_wput_ioctl"));
12843 	iocp = (IOCP)mp->b_rptr;
12844 	mp1 = mp->b_cont;
12845 	if (mp1 == NULL) {
12846 		iocp->ioc_error = EINVAL;
12847 		mp->b_datap->db_type = M_IOCNAK;
12848 		iocp->ioc_count = 0;
12849 		qreply(q, mp);
12850 		return;
12851 	}
12852 
12853 	/*
12854 	 * These IOCTLs provide various control capabilities to
12855 	 * upstream agents such as ULPs and processes.	There
12856 	 * are currently two such IOCTLs implemented.  They
12857 	 * are used by TCP to provide update information for
12858 	 * existing IREs and to forcibly delete an IRE for a
12859 	 * host that is not responding, thereby forcing an
12860 	 * attempt at a new route.
12861 	 */
12862 	iocp->ioc_error = EINVAL;
12863 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12864 		goto done;
12865 
12866 	ipllc = (ipllc_t *)mp1->b_rptr;
12867 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12868 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12869 			break;
12870 	}
12871 	/*
12872 	 * prefer credential from mblk over ioctl;
12873 	 * see ip_sioctl_copyin_setup
12874 	 */
12875 	cr = msg_getcred(mp, NULL);
12876 	if (cr == NULL)
12877 		cr = iocp->ioc_cr;
12878 
12879 	/*
12880 	 * Refhold the conn in case the request gets queued up in some lookup
12881 	 */
12882 	ASSERT(CONN_Q(q));
12883 	connp = Q_TO_CONN(q);
12884 	CONN_INC_REF(connp);
12885 	if (ipft->ipft_pfi &&
12886 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12887 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12888 		error = (*ipft->ipft_pfi)(q,
12889 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12890 	}
12891 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12892 		/*
12893 		 * CONN_OPER_PENDING_DONE happens in the function called
12894 		 * through ipft_pfi above.
12895 		 */
12896 		return;
12897 	}
12898 
12899 	CONN_OPER_PENDING_DONE(connp);
12900 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12901 		freemsg(mp);
12902 		return;
12903 	}
12904 	iocp->ioc_error = error;
12905 
12906 done:
12907 	mp->b_datap->db_type = M_IOCACK;
12908 	if (iocp->ioc_error)
12909 		iocp->ioc_count = 0;
12910 	qreply(q, mp);
12911 }
12912 
12913 /*
12914  * Lookup an ipif using the sequence id (ipif_seqid)
12915  */
12916 ipif_t *
12917 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12918 {
12919 	ipif_t *ipif;
12920 
12921 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12922 
12923 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12924 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12925 			return (ipif);
12926 	}
12927 	return (NULL);
12928 }
12929 
12930 /*
12931  * Assign a unique id for the ipif. This is used later when we send
12932  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12933  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12934  * IRE is added, we verify that ipif has not disappeared.
12935  */
12936 
12937 static void
12938 ipif_assign_seqid(ipif_t *ipif)
12939 {
12940 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12941 
12942 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
12943 }
12944 
12945 /*
12946  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
12947  * administratively down (i.e., no DAD), of the same type, and locked.  Note
12948  * that the clone is complete -- including the seqid -- and the expectation is
12949  * that the caller will either free or overwrite `sipif' before it's unlocked.
12950  */
12951 static void
12952 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
12953 {
12954 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
12955 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
12956 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12957 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12958 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
12959 	ASSERT(sipif->ipif_arp_del_mp == NULL);
12960 	ASSERT(dipif->ipif_arp_del_mp == NULL);
12961 	ASSERT(sipif->ipif_igmp_rpt == NULL);
12962 	ASSERT(dipif->ipif_igmp_rpt == NULL);
12963 	ASSERT(sipif->ipif_multicast_up == 0);
12964 	ASSERT(dipif->ipif_multicast_up == 0);
12965 	ASSERT(sipif->ipif_joined_allhosts == 0);
12966 	ASSERT(dipif->ipif_joined_allhosts == 0);
12967 
12968 	dipif->ipif_mtu = sipif->ipif_mtu;
12969 	dipif->ipif_flags = sipif->ipif_flags;
12970 	dipif->ipif_metric = sipif->ipif_metric;
12971 	dipif->ipif_zoneid = sipif->ipif_zoneid;
12972 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
12973 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
12974 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
12975 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
12976 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
12977 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
12978 
12979 	/*
12980 	 * While dipif is down right now, it might've been up before.  Since
12981 	 * it's changing identity, its packet counters need to be reset.
12982 	 */
12983 	dipif->ipif_ib_pkt_count = 0;
12984 	dipif->ipif_ob_pkt_count = 0;
12985 	dipif->ipif_fo_pkt_count = 0;
12986 
12987 	/*
12988 	 * As per the comment atop the function, we assume that these sipif
12989 	 * fields will be changed before sipif is unlocked.
12990 	 */
12991 	dipif->ipif_seqid = sipif->ipif_seqid;
12992 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
12993 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
12994 	dipif->ipif_state_flags = sipif->ipif_state_flags;
12995 }
12996 
12997 /*
12998  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
12999  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
13000  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
13001  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
13002  * down (i.e., no DAD), of the same type, and unlocked.
13003  */
13004 static void
13005 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
13006 {
13007 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
13008 	ipxop_t *ipx = ipsq->ipsq_xop;
13009 
13010 	ASSERT(sipif != dipif);
13011 	ASSERT(sipif != virgipif);
13012 
13013 	/*
13014 	 * Grab all of the locks that protect the ipif in a defined order.
13015 	 */
13016 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13017 	if (sipif > dipif) {
13018 		mutex_enter(&sipif->ipif_saved_ire_lock);
13019 		mutex_enter(&dipif->ipif_saved_ire_lock);
13020 	} else {
13021 		mutex_enter(&dipif->ipif_saved_ire_lock);
13022 		mutex_enter(&sipif->ipif_saved_ire_lock);
13023 	}
13024 
13025 	ipif_clone(sipif, dipif);
13026 	if (virgipif != NULL) {
13027 		ipif_clone(virgipif, sipif);
13028 		mi_free(virgipif);
13029 	}
13030 
13031 	mutex_exit(&sipif->ipif_saved_ire_lock);
13032 	mutex_exit(&dipif->ipif_saved_ire_lock);
13033 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13034 
13035 	/*
13036 	 * Transfer ownership of the current xop, if necessary.
13037 	 */
13038 	if (ipx->ipx_current_ipif == sipif) {
13039 		ASSERT(ipx->ipx_pending_ipif == NULL);
13040 		mutex_enter(&ipx->ipx_lock);
13041 		ipx->ipx_current_ipif = dipif;
13042 		mutex_exit(&ipx->ipx_lock);
13043 	}
13044 
13045 	if (virgipif == NULL)
13046 		mi_free(sipif);
13047 }
13048 
13049 /*
13050  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13051  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13052  * be inserted into the first space available in the list. The value of
13053  * ipif_id will then be set to the appropriate value for its position.
13054  */
13055 static int
13056 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13057 {
13058 	ill_t *ill;
13059 	ipif_t *tipif;
13060 	ipif_t **tipifp;
13061 	int id;
13062 	ip_stack_t	*ipst;
13063 
13064 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13065 	    IAM_WRITER_IPIF(ipif));
13066 
13067 	ill = ipif->ipif_ill;
13068 	ASSERT(ill != NULL);
13069 	ipst = ill->ill_ipst;
13070 
13071 	/*
13072 	 * In the case of lo0:0 we already hold the ill_g_lock.
13073 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13074 	 * ipif_insert.
13075 	 */
13076 	if (acquire_g_lock)
13077 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13078 	mutex_enter(&ill->ill_lock);
13079 	id = ipif->ipif_id;
13080 	tipifp = &(ill->ill_ipif);
13081 	if (id == -1) {	/* need to find a real id */
13082 		id = 0;
13083 		while ((tipif = *tipifp) != NULL) {
13084 			ASSERT(tipif->ipif_id >= id);
13085 			if (tipif->ipif_id != id)
13086 				break; /* non-consecutive id */
13087 			id++;
13088 			tipifp = &(tipif->ipif_next);
13089 		}
13090 		/* limit number of logical interfaces */
13091 		if (id >= ipst->ips_ip_addrs_per_if) {
13092 			mutex_exit(&ill->ill_lock);
13093 			if (acquire_g_lock)
13094 				rw_exit(&ipst->ips_ill_g_lock);
13095 			return (-1);
13096 		}
13097 		ipif->ipif_id = id; /* assign new id */
13098 	} else if (id < ipst->ips_ip_addrs_per_if) {
13099 		/* we have a real id; insert ipif in the right place */
13100 		while ((tipif = *tipifp) != NULL) {
13101 			ASSERT(tipif->ipif_id != id);
13102 			if (tipif->ipif_id > id)
13103 				break; /* found correct location */
13104 			tipifp = &(tipif->ipif_next);
13105 		}
13106 	} else {
13107 		mutex_exit(&ill->ill_lock);
13108 		if (acquire_g_lock)
13109 			rw_exit(&ipst->ips_ill_g_lock);
13110 		return (-1);
13111 	}
13112 
13113 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13114 
13115 	ipif->ipif_next = tipif;
13116 	*tipifp = ipif;
13117 	mutex_exit(&ill->ill_lock);
13118 	if (acquire_g_lock)
13119 		rw_exit(&ipst->ips_ill_g_lock);
13120 
13121 	return (0);
13122 }
13123 
13124 static void
13125 ipif_remove(ipif_t *ipif)
13126 {
13127 	ipif_t	**ipifp;
13128 	ill_t	*ill = ipif->ipif_ill;
13129 
13130 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13131 
13132 	mutex_enter(&ill->ill_lock);
13133 	ipifp = &ill->ill_ipif;
13134 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13135 		if (*ipifp == ipif) {
13136 			*ipifp = ipif->ipif_next;
13137 			break;
13138 		}
13139 	}
13140 	mutex_exit(&ill->ill_lock);
13141 }
13142 
13143 /*
13144  * Allocate and initialize a new interface control structure.  (Always
13145  * called as writer.)
13146  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13147  * is not part of the global linked list of ills. ipif_seqid is unique
13148  * in the system and to preserve the uniqueness, it is assigned only
13149  * when ill becomes part of the global list. At that point ill will
13150  * have a name. If it doesn't get assigned here, it will get assigned
13151  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13152  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13153  * the interface flags or any other information from the DL_INFO_ACK for
13154  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13155  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13156  * second DL_INFO_ACK comes in from the driver.
13157  */
13158 static ipif_t *
13159 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13160     boolean_t insert)
13161 {
13162 	ipif_t	*ipif;
13163 	ip_stack_t *ipst = ill->ill_ipst;
13164 
13165 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13166 	    ill->ill_name, id, (void *)ill));
13167 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13168 
13169 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13170 		return (NULL);
13171 	*ipif = ipif_zero;	/* start clean */
13172 
13173 	ipif->ipif_ill = ill;
13174 	ipif->ipif_id = id;	/* could be -1 */
13175 	/*
13176 	 * Inherit the zoneid from the ill; for the shared stack instance
13177 	 * this is always the global zone
13178 	 */
13179 	ipif->ipif_zoneid = ill->ill_zoneid;
13180 
13181 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13182 
13183 	ipif->ipif_refcnt = 0;
13184 	ipif->ipif_saved_ire_cnt = 0;
13185 
13186 	if (insert) {
13187 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13188 			mi_free(ipif);
13189 			return (NULL);
13190 		}
13191 		/* -1 id should have been replaced by real id */
13192 		id = ipif->ipif_id;
13193 		ASSERT(id >= 0);
13194 	}
13195 
13196 	if (ill->ill_name[0] != '\0')
13197 		ipif_assign_seqid(ipif);
13198 
13199 	/*
13200 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
13201 	 * (which must not exist yet because the zeroth ipif is created once
13202 	 * per ill).  However, do not not link it to the ipmp_grp_t until
13203 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
13204 	 */
13205 	if (id == 0 && IS_IPMP(ill)) {
13206 		if (ipmp_illgrp_create(ill) == NULL) {
13207 			if (insert) {
13208 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13209 				ipif_remove(ipif);
13210 				rw_exit(&ipst->ips_ill_g_lock);
13211 			}
13212 			mi_free(ipif);
13213 			return (NULL);
13214 		}
13215 	}
13216 
13217 	/*
13218 	 * We grab ill_lock to protect the flag changes.  The ipif is still
13219 	 * not up and can't be looked up until the ioctl completes and the
13220 	 * IPIF_CHANGING flag is cleared.
13221 	 */
13222 	mutex_enter(&ill->ill_lock);
13223 
13224 	ipif->ipif_ire_type = ire_type;
13225 
13226 	if (ipif->ipif_isv6) {
13227 		ill->ill_flags |= ILLF_IPV6;
13228 	} else {
13229 		ipaddr_t inaddr_any = INADDR_ANY;
13230 
13231 		ill->ill_flags |= ILLF_IPV4;
13232 
13233 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13234 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13235 		    &ipif->ipif_v6lcl_addr);
13236 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13237 		    &ipif->ipif_v6src_addr);
13238 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13239 		    &ipif->ipif_v6subnet);
13240 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13241 		    &ipif->ipif_v6net_mask);
13242 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13243 		    &ipif->ipif_v6brd_addr);
13244 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13245 		    &ipif->ipif_v6pp_dst_addr);
13246 	}
13247 
13248 	/*
13249 	 * Don't set the interface flags etc. now, will do it in
13250 	 * ip_ll_subnet_defaults.
13251 	 */
13252 	if (!initialize)
13253 		goto out;
13254 
13255 	ipif->ipif_mtu = ill->ill_max_mtu;
13256 
13257 	/*
13258 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13259 	 * with no underlying interfaces (and thus an unknown broadcast
13260 	 * address length), but all interfaces that can be placed into an IPMP
13261 	 * group are required to be broadcast-capable.
13262 	 */
13263 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13264 		/*
13265 		 * Later detect lack of DLPI driver multicast capability by
13266 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
13267 		 */
13268 		ill->ill_flags |= ILLF_MULTICAST;
13269 		if (!ipif->ipif_isv6)
13270 			ipif->ipif_flags |= IPIF_BROADCAST;
13271 	} else {
13272 		if (ill->ill_net_type != IRE_LOOPBACK) {
13273 			if (ipif->ipif_isv6)
13274 				/*
13275 				 * Note: xresolv interfaces will eventually need
13276 				 * NOARP set here as well, but that will require
13277 				 * those external resolvers to have some
13278 				 * knowledge of that flag and act appropriately.
13279 				 * Not to be changed at present.
13280 				 */
13281 				ill->ill_flags |= ILLF_NONUD;
13282 			else
13283 				ill->ill_flags |= ILLF_NOARP;
13284 		}
13285 		if (ill->ill_phys_addr_length == 0) {
13286 			if (IS_VNI(ill)) {
13287 				ipif->ipif_flags |= IPIF_NOXMIT;
13288 			} else {
13289 				/* pt-pt supports multicast. */
13290 				ill->ill_flags |= ILLF_MULTICAST;
13291 				if (ill->ill_net_type != IRE_LOOPBACK)
13292 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13293 			}
13294 		}
13295 	}
13296 out:
13297 	mutex_exit(&ill->ill_lock);
13298 	return (ipif);
13299 }
13300 
13301 /*
13302  * If appropriate, send a message up to the resolver delete the entry
13303  * for the address of this interface which is going out of business.
13304  * (Always called as writer).
13305  *
13306  * NOTE : We need to check for NULL mps as some of the fields are
13307  *	  initialized only for some interface types. See ipif_resolver_up()
13308  *	  for details.
13309  */
13310 void
13311 ipif_resolver_down(ipif_t *ipif)
13312 {
13313 	mblk_t	*mp;
13314 	ill_t	*ill = ipif->ipif_ill;
13315 
13316 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13317 	ASSERT(IAM_WRITER_IPIF(ipif));
13318 
13319 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13320 		return;
13321 
13322 	/* Delete the mapping for the local address */
13323 	mp = ipif->ipif_arp_del_mp;
13324 	if (mp != NULL) {
13325 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13326 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13327 		putnext(ill->ill_rq, mp);
13328 		ipif->ipif_arp_del_mp = NULL;
13329 	}
13330 
13331 	/*
13332 	 * Make IPMP aware of the deleted data address.
13333 	 */
13334 	if (IS_IPMP(ill))
13335 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13336 
13337 	/*
13338 	 * If this is the last ipif that is going down and there are no
13339 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13340 	 * clean up ARP completely.
13341 	 */
13342 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13343 		/*
13344 		 * If this was the last ipif on an IPMP interface, purge any
13345 		 * IPMP ARP entries associated with it.
13346 		 */
13347 		if (IS_IPMP(ill))
13348 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13349 
13350 		/* Send up AR_INTERFACE_DOWN message */
13351 		mp = ill->ill_arp_down_mp;
13352 		if (mp != NULL) {
13353 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13354 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13355 			    ipif->ipif_id));
13356 			putnext(ill->ill_rq, mp);
13357 			ill->ill_arp_down_mp = NULL;
13358 		}
13359 
13360 		/* Tell ARP to delete the multicast mappings */
13361 		mp = ill->ill_arp_del_mapping_mp;
13362 		if (mp != NULL) {
13363 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13364 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13365 			    ipif->ipif_id));
13366 			putnext(ill->ill_rq, mp);
13367 			ill->ill_arp_del_mapping_mp = NULL;
13368 		}
13369 	}
13370 }
13371 
13372 /*
13373  * Set up the multicast mappings for `ipif' in ARP.  If `arp_add_mapping_mp'
13374  * is non-NULL, then upon success it will contain an mblk that can be passed
13375  * to ARP to create the mapping.  Otherwise, if it's NULL, upon success ARP
13376  * will have already been notified to create the mapping.  Returns zero on
13377  * success, -1 upon failure.
13378  */
13379 int
13380 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13381 {
13382 	mblk_t	*del_mp = NULL;
13383 	mblk_t *add_mp = NULL;
13384 	mblk_t *mp;
13385 	ill_t	*ill = ipif->ipif_ill;
13386 	phyint_t *phyi = ill->ill_phyint;
13387 	ipaddr_t addr, mask, extract_mask = 0;
13388 	arma_t	*arma;
13389 	uint8_t *maddr, *bphys_addr;
13390 	uint32_t hw_start;
13391 	dl_unitdata_req_t *dlur;
13392 
13393 	ASSERT(IAM_WRITER_IPIF(ipif));
13394 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13395 		return (0);
13396 
13397 	/*
13398 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13399 	 * and instead use the ones on the underlying interfaces.
13400 	 */
13401 	if (IS_IPMP(ill))
13402 		return (0);
13403 
13404 	/*
13405 	 * Delete the existing mapping from ARP.  Normally, ipif_down() ->
13406 	 * ipif_resolver_down() will send this up to ARP, but it may be that
13407 	 * we are enabling PHYI_MULTI_BCAST via ip_rput_dlpi_writer().
13408 	 */
13409 	mp = ill->ill_arp_del_mapping_mp;
13410 	if (mp != NULL) {
13411 		ip1dbg(("ipif_arp_setup_multicast: arp cmd %x for %s:%u\n",
13412 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13413 		putnext(ill->ill_rq, mp);
13414 		ill->ill_arp_del_mapping_mp = NULL;
13415 	}
13416 
13417 	if (arp_add_mapping_mp != NULL)
13418 		*arp_add_mapping_mp = NULL;
13419 
13420 	/*
13421 	 * Check that the address is not to long for the constant
13422 	 * length reserved in the template arma_t.
13423 	 */
13424 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13425 		return (-1);
13426 
13427 	/* Add mapping mblk */
13428 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13429 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13430 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13431 	    (caddr_t)&addr);
13432 	if (add_mp == NULL)
13433 		return (-1);
13434 	arma = (arma_t *)add_mp->b_rptr;
13435 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13436 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13437 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13438 
13439 	/*
13440 	 * Determine the broadcast address.
13441 	 */
13442 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13443 	if (ill->ill_sap_length < 0)
13444 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13445 	else
13446 		bphys_addr = (uchar_t *)dlur +
13447 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13448 	/*
13449 	 * Check PHYI_MULTI_BCAST and length of physical
13450 	 * address to determine if we use the mapping or the
13451 	 * broadcast address.
13452 	 */
13453 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13454 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13455 		    bphys_addr, maddr, &hw_start, &extract_mask))
13456 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13457 
13458 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13459 	    (ill->ill_flags & ILLF_MULTICAST)) {
13460 		/* Make sure this will not match the "exact" entry. */
13461 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13462 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13463 		    (caddr_t)&addr);
13464 		if (del_mp == NULL) {
13465 			freemsg(add_mp);
13466 			return (-1);
13467 		}
13468 		bcopy(&extract_mask, (char *)arma +
13469 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13470 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13471 			/* Use link-layer broadcast address for MULTI_BCAST */
13472 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13473 			ip2dbg(("ipif_arp_setup_multicast: adding"
13474 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13475 		} else {
13476 			arma->arma_hw_mapping_start = hw_start;
13477 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13478 			    " ARP setup for %s\n", ill->ill_name));
13479 		}
13480 	} else {
13481 		freemsg(add_mp);
13482 		ASSERT(del_mp == NULL);
13483 		/* It is neither MULTICAST nor MULTI_BCAST */
13484 		return (0);
13485 	}
13486 	ASSERT(add_mp != NULL && del_mp != NULL);
13487 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13488 	ill->ill_arp_del_mapping_mp = del_mp;
13489 	if (arp_add_mapping_mp != NULL) {
13490 		/* The caller just wants the mblks allocated */
13491 		*arp_add_mapping_mp = add_mp;
13492 	} else {
13493 		/* The caller wants us to send it to arp */
13494 		putnext(ill->ill_rq, add_mp);
13495 	}
13496 	return (0);
13497 }
13498 
13499 /*
13500  * Get the resolver set up for a new IP address.  (Always called as writer.)
13501  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13502  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13503  *
13504  * The enumerated value res_act tunes the behavior:
13505  * 	* Res_act_initial: set up all the resolver structures for a new
13506  *	  IP address.
13507  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13508  *	  ARP message in defense of the address.
13509  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13510  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13511  *
13512  * Returns zero on success, or an errno upon failure.
13513  */
13514 int
13515 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13516 {
13517 	mblk_t	*arp_up_mp = NULL;
13518 	mblk_t	*arp_down_mp = NULL;
13519 	mblk_t	*arp_add_mp = NULL;
13520 	mblk_t	*arp_del_mp = NULL;
13521 	mblk_t	*arp_add_mapping_mp = NULL;
13522 	mblk_t	*arp_del_mapping_mp = NULL;
13523 	ill_t	*ill = ipif->ipif_ill;
13524 	int	err = ENOMEM;
13525 	boolean_t added_ipif = B_FALSE;
13526 	boolean_t publish;
13527 	boolean_t was_dup;
13528 
13529 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13530 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13531 	ASSERT(IAM_WRITER_IPIF(ipif));
13532 
13533 	was_dup = B_FALSE;
13534 	if (res_act == Res_act_initial) {
13535 		ipif->ipif_addr_ready = 0;
13536 		/*
13537 		 * We're bringing an interface up here.  There's no way that we
13538 		 * should need to shut down ARP now.
13539 		 */
13540 		mutex_enter(&ill->ill_lock);
13541 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13542 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13543 			ill->ill_ipif_dup_count--;
13544 			was_dup = B_TRUE;
13545 		}
13546 		mutex_exit(&ill->ill_lock);
13547 	}
13548 	if (ipif->ipif_recovery_id != 0)
13549 		(void) untimeout(ipif->ipif_recovery_id);
13550 	ipif->ipif_recovery_id = 0;
13551 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13552 		ipif->ipif_addr_ready = 1;
13553 		return (0);
13554 	}
13555 	/* NDP will set the ipif_addr_ready flag when it's ready */
13556 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13557 		return (0);
13558 
13559 	if (ill->ill_isv6) {
13560 		/*
13561 		 * External resolver for IPv6
13562 		 */
13563 		ASSERT(res_act == Res_act_initial);
13564 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13565 	} else {
13566 		/*
13567 		 * IPv4 arp case. If the ARP stream has already started
13568 		 * closing, fail this request for ARP bringup. Else
13569 		 * record the fact that an ARP bringup is pending.
13570 		 */
13571 		mutex_enter(&ill->ill_lock);
13572 		if (ill->ill_arp_closing) {
13573 			mutex_exit(&ill->ill_lock);
13574 			err = EINVAL;
13575 			goto failed;
13576 		} else {
13577 			if (ill->ill_ipif_up_count == 0 &&
13578 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13579 				ill->ill_arp_bringup_pending = 1;
13580 			mutex_exit(&ill->ill_lock);
13581 		}
13582 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13583 	}
13584 
13585 	if (IS_IPMP(ill) && publish) {
13586 		/*
13587 		 * If we're here via ipif_up(), then the ipif won't be bound
13588 		 * yet -- add it to the group, which will bind it if possible.
13589 		 * (We would add it in ipif_up(), but deleting on failure
13590 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13591 		 * then the ipif has already been added to the group and we
13592 		 * just need to use the binding.
13593 		 */
13594 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13595 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13596 				/*
13597 				 * We couldn't bind the ipif to an ill yet,
13598 				 * so we have nothing to publish.
13599 				 */
13600 				publish = B_FALSE;
13601 			}
13602 			added_ipif = B_TRUE;
13603 		}
13604 	}
13605 
13606 	/*
13607 	 * Add an entry for the local address in ARP only if it
13608 	 * is not UNNUMBERED and it is suitable for publishing.
13609 	 */
13610 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13611 		if (res_act == Res_act_defend) {
13612 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13613 			if (arp_add_mp == NULL)
13614 				goto failed;
13615 			/*
13616 			 * If we're just defending our address now, then
13617 			 * there's no need to set up ARP multicast mappings.
13618 			 * The publish command is enough.
13619 			 */
13620 			goto done;
13621 		}
13622 
13623 		/*
13624 		 * Allocate an ARP add message and an ARP delete message (the
13625 		 * latter is saved for use when the address goes down).
13626 		 */
13627 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13628 			goto failed;
13629 
13630 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13631 			goto failed;
13632 
13633 		if (res_act != Res_act_initial)
13634 			goto arp_setup_multicast;
13635 	} else {
13636 		if (res_act != Res_act_initial)
13637 			goto done;
13638 	}
13639 	/*
13640 	 * Need to bring up ARP or setup multicast mapping only
13641 	 * when the first interface is coming UP.
13642 	 */
13643 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13644 		goto done;
13645 
13646 	/*
13647 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13648 	 */
13649 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13650 	if (arp_down_mp == NULL)
13651 		goto failed;
13652 
13653 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13654 	if (arp_up_mp == NULL)
13655 		goto failed;
13656 
13657 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13658 		goto done;
13659 
13660 arp_setup_multicast:
13661 	/*
13662 	 * Setup the multicast mappings. This function initializes
13663 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13664 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13665 	 */
13666 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13667 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13668 		if (err != 0)
13669 			goto failed;
13670 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13671 		ASSERT(arp_add_mapping_mp != NULL);
13672 	}
13673 done:
13674 	if (arp_up_mp != NULL) {
13675 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13676 		    ill->ill_name, ipif->ipif_id));
13677 		putnext(ill->ill_rq, arp_up_mp);
13678 		arp_up_mp = NULL;
13679 	}
13680 	if (arp_add_mp != NULL) {
13681 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13682 		    ill->ill_name, ipif->ipif_id));
13683 		/*
13684 		 * If it's an extended ARP implementation, then we'll wait to
13685 		 * hear that DAD has finished before using the interface.
13686 		 */
13687 		if (!ill->ill_arp_extend)
13688 			ipif->ipif_addr_ready = 1;
13689 		putnext(ill->ill_rq, arp_add_mp);
13690 		arp_add_mp = NULL;
13691 	} else {
13692 		ipif->ipif_addr_ready = 1;
13693 	}
13694 	if (arp_add_mapping_mp != NULL) {
13695 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13696 		    ill->ill_name, ipif->ipif_id));
13697 		putnext(ill->ill_rq, arp_add_mapping_mp);
13698 		arp_add_mapping_mp = NULL;
13699 	}
13700 
13701 	if (res_act == Res_act_initial) {
13702 		if (ill->ill_flags & ILLF_NOARP)
13703 			err = ill_arp_off(ill);
13704 		else
13705 			err = ill_arp_on(ill);
13706 		if (err != 0) {
13707 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13708 			    err));
13709 			goto failed;
13710 		}
13711 	}
13712 
13713 	if (arp_del_mp != NULL) {
13714 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13715 		ipif->ipif_arp_del_mp = arp_del_mp;
13716 	}
13717 	if (arp_down_mp != NULL) {
13718 		ASSERT(ill->ill_arp_down_mp == NULL);
13719 		ill->ill_arp_down_mp = arp_down_mp;
13720 	}
13721 	if (arp_del_mapping_mp != NULL) {
13722 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13723 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13724 	}
13725 
13726 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13727 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13728 failed:
13729 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13730 	if (added_ipif)
13731 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13732 	freemsg(arp_add_mp);
13733 	freemsg(arp_del_mp);
13734 	freemsg(arp_add_mapping_mp);
13735 	freemsg(arp_up_mp);
13736 	freemsg(arp_down_mp);
13737 	ill->ill_arp_bringup_pending = 0;
13738 	return (err);
13739 }
13740 
13741 /*
13742  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13743  * just gone back up.
13744  */
13745 static void
13746 ipif_arp_start_dad(ipif_t *ipif)
13747 {
13748 	ill_t *ill = ipif->ipif_ill;
13749 	mblk_t *arp_add_mp;
13750 
13751 	/* ACE_F_UNVERIFIED restarts DAD */
13752 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13753 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13754 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13755 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13756 		/*
13757 		 * If we can't contact ARP for some reason, that's not really a
13758 		 * problem.  Just send out the routing socket notification that
13759 		 * DAD completion would have done, and continue.
13760 		 */
13761 		ipif_mask_reply(ipif);
13762 		ipif_up_notify(ipif);
13763 		ipif->ipif_addr_ready = 1;
13764 		return;
13765 	}
13766 
13767 	putnext(ill->ill_rq, arp_add_mp);
13768 }
13769 
13770 static void
13771 ipif_ndp_start_dad(ipif_t *ipif)
13772 {
13773 	nce_t *nce;
13774 
13775 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13776 	    B_FALSE);
13777 	if (nce == NULL)
13778 		return;
13779 
13780 	if (!ndp_restart_dad(nce)) {
13781 		/*
13782 		 * If we can't restart DAD for some reason, that's not really a
13783 		 * problem.  Just send out the routing socket notification that
13784 		 * DAD completion would have done, and continue.
13785 		 */
13786 		ipif_up_notify(ipif);
13787 		ipif->ipif_addr_ready = 1;
13788 	}
13789 	NCE_REFRELE(nce);
13790 }
13791 
13792 /*
13793  * Restart duplicate address detection on all interfaces on the given ill.
13794  *
13795  * This is called when an interface transitions from down to up
13796  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13797  *
13798  * Note that since the underlying physical link has transitioned, we must cause
13799  * at least one routing socket message to be sent here, either via DAD
13800  * completion or just by default on the first ipif.  (If we don't do this, then
13801  * in.mpathd will see long delays when doing link-based failure recovery.)
13802  */
13803 void
13804 ill_restart_dad(ill_t *ill, boolean_t went_up)
13805 {
13806 	ipif_t *ipif;
13807 
13808 	if (ill == NULL)
13809 		return;
13810 
13811 	/*
13812 	 * If layer two doesn't support duplicate address detection, then just
13813 	 * send the routing socket message now and be done with it.
13814 	 */
13815 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13816 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13817 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13818 		return;
13819 	}
13820 
13821 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13822 		if (went_up) {
13823 			if (ipif->ipif_flags & IPIF_UP) {
13824 				if (ill->ill_isv6)
13825 					ipif_ndp_start_dad(ipif);
13826 				else
13827 					ipif_arp_start_dad(ipif);
13828 			} else if (ill->ill_isv6 &&
13829 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13830 				/*
13831 				 * For IPv4, the ARP module itself will
13832 				 * automatically start the DAD process when it
13833 				 * sees DL_NOTE_LINK_UP.  We respond to the
13834 				 * AR_CN_READY at the completion of that task.
13835 				 * For IPv6, we must kick off the bring-up
13836 				 * process now.
13837 				 */
13838 				ndp_do_recovery(ipif);
13839 			} else {
13840 				/*
13841 				 * Unfortunately, the first ipif is "special"
13842 				 * and represents the underlying ill in the
13843 				 * routing socket messages.  Thus, when this
13844 				 * one ipif is down, we must still notify so
13845 				 * that the user knows the IFF_RUNNING status
13846 				 * change.  (If the first ipif is up, then
13847 				 * we'll handle eventual routing socket
13848 				 * notification via DAD completion.)
13849 				 */
13850 				if (ipif == ill->ill_ipif) {
13851 					ip_rts_ifmsg(ill->ill_ipif,
13852 					    RTSQ_DEFAULT);
13853 				}
13854 			}
13855 		} else {
13856 			/*
13857 			 * After link down, we'll need to send a new routing
13858 			 * message when the link comes back, so clear
13859 			 * ipif_addr_ready.
13860 			 */
13861 			ipif->ipif_addr_ready = 0;
13862 		}
13863 	}
13864 
13865 	/*
13866 	 * If we've torn down links, then notify the user right away.
13867 	 */
13868 	if (!went_up)
13869 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13870 }
13871 
13872 static void
13873 ipsq_delete(ipsq_t *ipsq)
13874 {
13875 	ipxop_t *ipx = ipsq->ipsq_xop;
13876 
13877 	ipsq->ipsq_ipst = NULL;
13878 	ASSERT(ipsq->ipsq_phyint == NULL);
13879 	ASSERT(ipsq->ipsq_xop != NULL);
13880 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
13881 	ASSERT(ipx->ipx_pending_mp == NULL);
13882 	kmem_free(ipsq, sizeof (ipsq_t));
13883 }
13884 
13885 static int
13886 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
13887 {
13888 	int err;
13889 	ipif_t *ipif;
13890 
13891 	if (ill == NULL)
13892 		return (0);
13893 
13894 	ASSERT(IAM_WRITER_ILL(ill));
13895 	ill->ill_up_ipifs = B_TRUE;
13896 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13897 		if (ipif->ipif_was_up) {
13898 			if (!(ipif->ipif_flags & IPIF_UP))
13899 				err = ipif_up(ipif, q, mp);
13900 			ipif->ipif_was_up = B_FALSE;
13901 			if (err != 0) {
13902 				ASSERT(err == EINPROGRESS);
13903 				return (err);
13904 			}
13905 		}
13906 	}
13907 	mutex_enter(&ill->ill_lock);
13908 	ill->ill_state_flags &= ~ILL_CHANGING;
13909 	mutex_exit(&ill->ill_lock);
13910 	ill->ill_up_ipifs = B_FALSE;
13911 	return (0);
13912 }
13913 
13914 /*
13915  * This function is called to bring up all the ipifs that were up before
13916  * bringing the ill down via ill_down_ipifs().
13917  */
13918 int
13919 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13920 {
13921 	int err;
13922 
13923 	ASSERT(IAM_WRITER_ILL(ill));
13924 
13925 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
13926 	if (err != 0)
13927 		return (err);
13928 
13929 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
13930 }
13931 
13932 /*
13933  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
13934  * down the ipifs without sending DL_UNBIND_REQ to the driver.
13935  */
13936 static void
13937 ill_down_ipifs(ill_t *ill, boolean_t logical)
13938 {
13939 	ipif_t *ipif;
13940 
13941 	ASSERT(IAM_WRITER_ILL(ill));
13942 
13943 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13944 		/*
13945 		 * We go through the ipif_down logic even if the ipif
13946 		 * is already down, since routes can be added based
13947 		 * on down ipifs. Going through ipif_down once again
13948 		 * will delete any IREs created based on these routes.
13949 		 */
13950 		if (ipif->ipif_flags & IPIF_UP)
13951 			ipif->ipif_was_up = B_TRUE;
13952 
13953 		/*
13954 		 * Need to re-create net/subnet bcast ires if
13955 		 * they are dependent on ipif.
13956 		 */
13957 		if (!ipif->ipif_isv6)
13958 			ipif_check_bcast_ires(ipif);
13959 		if (logical) {
13960 			(void) ipif_logical_down(ipif, NULL, NULL);
13961 			ipif_non_duplicate(ipif);
13962 			ipif_down_tail(ipif);
13963 		} else {
13964 			(void) ipif_down(ipif, NULL, NULL);
13965 		}
13966 	}
13967 }
13968 
13969 /*
13970  * Redo source address selection.  This is called when a
13971  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
13972  */
13973 void
13974 ill_update_source_selection(ill_t *ill)
13975 {
13976 	ipif_t *ipif;
13977 
13978 	ASSERT(IAM_WRITER_ILL(ill));
13979 
13980 	/*
13981 	 * Underlying interfaces are only used for test traffic and thus
13982 	 * should always send with their (deprecated) source addresses.
13983 	 */
13984 	if (IS_UNDER_IPMP(ill))
13985 		return;
13986 
13987 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13988 		if (ill->ill_isv6)
13989 			ipif_recreate_interface_routes_v6(NULL, ipif);
13990 		else
13991 			ipif_recreate_interface_routes(NULL, ipif);
13992 	}
13993 }
13994 
13995 /*
13996  * Finish the group join started in ip_sioctl_groupname().
13997  */
13998 /* ARGSUSED */
13999 static void
14000 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14001 {
14002 	ill_t		*ill = q->q_ptr;
14003 	phyint_t	*phyi = ill->ill_phyint;
14004 	ipmp_grp_t	*grp = phyi->phyint_grp;
14005 	ip_stack_t	*ipst = ill->ill_ipst;
14006 
14007 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14008 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14009 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14010 
14011 	if (phyi->phyint_illv4 != NULL) {
14012 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14013 		VERIFY(grp->gr_pendv4-- > 0);
14014 		rw_exit(&ipst->ips_ipmp_lock);
14015 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14016 	}
14017 	if (phyi->phyint_illv6 != NULL) {
14018 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14019 		VERIFY(grp->gr_pendv6-- > 0);
14020 		rw_exit(&ipst->ips_ipmp_lock);
14021 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14022 	}
14023 	freemsg(mp);
14024 }
14025 
14026 /*
14027  * Process an SIOCSLIFGROUPNAME request.
14028  */
14029 /* ARGSUSED */
14030 int
14031 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14032     ip_ioctl_cmd_t *ipip, void *ifreq)
14033 {
14034 	struct lifreq	*lifr = ifreq;
14035 	ill_t		*ill = ipif->ipif_ill;
14036 	ip_stack_t	*ipst = ill->ill_ipst;
14037 	phyint_t	*phyi = ill->ill_phyint;
14038 	ipmp_grp_t	*grp = phyi->phyint_grp;
14039 	mblk_t		*ipsq_mp;
14040 	int		err = 0;
14041 
14042 	/*
14043 	 * Note that phyint_grp can only change here, where we're exclusive.
14044 	 */
14045 	ASSERT(IAM_WRITER_ILL(ill));
14046 
14047 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14048 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14049 		return (EINVAL);
14050 
14051 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14052 
14053 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14054 
14055 	/*
14056 	 * If the name hasn't changed, there's nothing to do.
14057 	 */
14058 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14059 		goto unlock;
14060 
14061 	/*
14062 	 * Handle requests to rename an IPMP meta-interface.
14063 	 *
14064 	 * Note that creation of the IPMP meta-interface is handled in
14065 	 * userland through the standard plumbing sequence.  As part of the
14066 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14067 	 * the name of the interface (see ipif_set_values_tail()).
14068 	 */
14069 	if (IS_IPMP(ill)) {
14070 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14071 		goto unlock;
14072 	}
14073 
14074 	/*
14075 	 * Handle requests to add or remove an IP interface from a group.
14076 	 */
14077 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14078 		/*
14079 		 * Moves are handled by first removing the interface from
14080 		 * its existing group, and then adding it to another group.
14081 		 * So, fail if it's already in a group.
14082 		 */
14083 		if (IS_UNDER_IPMP(ill)) {
14084 			err = EALREADY;
14085 			goto unlock;
14086 		}
14087 
14088 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14089 		if (grp == NULL) {
14090 			err = ENOENT;
14091 			goto unlock;
14092 		}
14093 
14094 		/*
14095 		 * Check if the phyint and its ills are suitable for
14096 		 * inclusion into the group.
14097 		 */
14098 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14099 			goto unlock;
14100 
14101 		/*
14102 		 * Checks pass; join the group, and enqueue the remaining
14103 		 * illgrp joins for when we've become part of the group xop
14104 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14105 		 * requires an mblk_t to scribble on, and since `mp' will be
14106 		 * freed as part of completing the ioctl, allocate another.
14107 		 */
14108 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14109 			err = ENOMEM;
14110 			goto unlock;
14111 		}
14112 
14113 		/*
14114 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14115 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14116 		 * before ip_join_illgrps() is called back.  See the comments
14117 		 * in ip_sioctl_plink_ipmp() for more.
14118 		 */
14119 		if (phyi->phyint_illv4 != NULL)
14120 			grp->gr_pendv4++;
14121 		if (phyi->phyint_illv6 != NULL)
14122 			grp->gr_pendv6++;
14123 
14124 		rw_exit(&ipst->ips_ipmp_lock);
14125 
14126 		ipmp_phyint_join_grp(phyi, grp);
14127 		ill_refhold(ill);
14128 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14129 		    SWITCH_OP, B_FALSE);
14130 		return (0);
14131 	} else {
14132 		/*
14133 		 * Request to remove the interface from a group.  If the
14134 		 * interface is not in a group, this trivially succeeds.
14135 		 */
14136 		rw_exit(&ipst->ips_ipmp_lock);
14137 		if (IS_UNDER_IPMP(ill))
14138 			ipmp_phyint_leave_grp(phyi);
14139 		return (0);
14140 	}
14141 unlock:
14142 	rw_exit(&ipst->ips_ipmp_lock);
14143 	return (err);
14144 }
14145 
14146 /*
14147  * Process an SIOCGLIFBINDING request.
14148  */
14149 /* ARGSUSED */
14150 int
14151 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14152     ip_ioctl_cmd_t *ipip, void *ifreq)
14153 {
14154 	ill_t		*ill;
14155 	struct lifreq	*lifr = ifreq;
14156 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14157 
14158 	if (!IS_IPMP(ipif->ipif_ill))
14159 		return (EINVAL);
14160 
14161 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14162 	if ((ill = ipif->ipif_bound_ill) == NULL)
14163 		lifr->lifr_binding[0] = '\0';
14164 	else
14165 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
14166 	rw_exit(&ipst->ips_ipmp_lock);
14167 	return (0);
14168 }
14169 
14170 /*
14171  * Process an SIOCGLIFGROUPNAME request.
14172  */
14173 /* ARGSUSED */
14174 int
14175 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14176     ip_ioctl_cmd_t *ipip, void *ifreq)
14177 {
14178 	ipmp_grp_t	*grp;
14179 	struct lifreq	*lifr = ifreq;
14180 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14181 
14182 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14183 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14184 		lifr->lifr_groupname[0] = '\0';
14185 	else
14186 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14187 	rw_exit(&ipst->ips_ipmp_lock);
14188 	return (0);
14189 }
14190 
14191 /*
14192  * Process an SIOCGLIFGROUPINFO request.
14193  */
14194 /* ARGSUSED */
14195 int
14196 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14197     ip_ioctl_cmd_t *ipip, void *dummy)
14198 {
14199 	ipmp_grp_t	*grp;
14200 	lifgroupinfo_t	*lifgr;
14201 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14202 
14203 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14204 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14205 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14206 
14207 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14208 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14209 		rw_exit(&ipst->ips_ipmp_lock);
14210 		return (ENOENT);
14211 	}
14212 	ipmp_grp_info(grp, lifgr);
14213 	rw_exit(&ipst->ips_ipmp_lock);
14214 	return (0);
14215 }
14216 
14217 static void
14218 ill_dl_down(ill_t *ill)
14219 {
14220 	/*
14221 	 * The ill is down; unbind but stay attached since we're still
14222 	 * associated with a PPA. If we have negotiated DLPI capabilites
14223 	 * with the data link service provider (IDS_OK) then reset them.
14224 	 * The interval between unbinding and rebinding is potentially
14225 	 * unbounded hence we cannot assume things will be the same.
14226 	 * The DLPI capabilities will be probed again when the data link
14227 	 * is brought up.
14228 	 */
14229 	mblk_t	*mp = ill->ill_unbind_mp;
14230 
14231 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14232 
14233 	ill->ill_unbind_mp = NULL;
14234 	if (mp != NULL) {
14235 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14236 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14237 		    ill->ill_name));
14238 		mutex_enter(&ill->ill_lock);
14239 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14240 		mutex_exit(&ill->ill_lock);
14241 		/*
14242 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14243 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14244 		 * ill_capability_dld_disable disable rightaway. If this is not
14245 		 * an unplumb operation then the disable happens on receipt of
14246 		 * the capab ack via ip_rput_dlpi_writer ->
14247 		 * ill_capability_ack_thr. In both cases the order of
14248 		 * the operations seen by DLD is capability disable followed
14249 		 * by DL_UNBIND. Also the DLD capability disable needs a
14250 		 * cv_wait'able context.
14251 		 */
14252 		if (ill->ill_state_flags & ILL_CONDEMNED)
14253 			ill_capability_dld_disable(ill);
14254 		ill_capability_reset(ill, B_FALSE);
14255 		ill_dlpi_send(ill, mp);
14256 	}
14257 
14258 	/*
14259 	 * Toss all of our multicast memberships.  We could keep them, but
14260 	 * then we'd have to do bookkeeping of any joins and leaves performed
14261 	 * by the application while the the interface is down (we can't just
14262 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14263 	 * on a downed interface).
14264 	 */
14265 	ill_leave_multicast(ill);
14266 
14267 	mutex_enter(&ill->ill_lock);
14268 	ill->ill_dl_up = 0;
14269 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14270 	mutex_exit(&ill->ill_lock);
14271 }
14272 
14273 static void
14274 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14275 {
14276 	union DL_primitives *dlp;
14277 	t_uscalar_t prim;
14278 	boolean_t waitack = B_FALSE;
14279 
14280 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14281 
14282 	dlp = (union DL_primitives *)mp->b_rptr;
14283 	prim = dlp->dl_primitive;
14284 
14285 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14286 	    dl_primstr(prim), prim, ill->ill_name));
14287 
14288 	switch (prim) {
14289 	case DL_PHYS_ADDR_REQ:
14290 	{
14291 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14292 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14293 		break;
14294 	}
14295 	case DL_BIND_REQ:
14296 		mutex_enter(&ill->ill_lock);
14297 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14298 		mutex_exit(&ill->ill_lock);
14299 		break;
14300 	}
14301 
14302 	/*
14303 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14304 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14305 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14306 	 */
14307 	mutex_enter(&ill->ill_lock);
14308 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
14309 	    (prim == DL_UNBIND_REQ)) {
14310 		ill->ill_dlpi_pending = prim;
14311 		waitack = B_TRUE;
14312 	}
14313 
14314 	mutex_exit(&ill->ill_lock);
14315 	putnext(ill->ill_wq, mp);
14316 
14317 	/*
14318 	 * There is no ack for DL_NOTIFY_CONF messages
14319 	 */
14320 	if (waitack && prim == DL_NOTIFY_CONF)
14321 		ill_dlpi_done(ill, prim);
14322 }
14323 
14324 /*
14325  * Helper function for ill_dlpi_send().
14326  */
14327 /* ARGSUSED */
14328 static void
14329 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14330 {
14331 	ill_dlpi_send(q->q_ptr, mp);
14332 }
14333 
14334 /*
14335  * Send a DLPI control message to the driver but make sure there
14336  * is only one outstanding message. Uses ill_dlpi_pending to tell
14337  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14338  * when an ACK or a NAK is received to process the next queued message.
14339  */
14340 void
14341 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14342 {
14343 	mblk_t **mpp;
14344 
14345 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14346 
14347 	/*
14348 	 * To ensure that any DLPI requests for current exclusive operation
14349 	 * are always completely sent before any DLPI messages for other
14350 	 * operations, require writer access before enqueuing.
14351 	 */
14352 	if (!IAM_WRITER_ILL(ill)) {
14353 		ill_refhold(ill);
14354 		/* qwriter_ip() does the ill_refrele() */
14355 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14356 		    NEW_OP, B_TRUE);
14357 		return;
14358 	}
14359 
14360 	mutex_enter(&ill->ill_lock);
14361 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14362 		/* Must queue message. Tail insertion */
14363 		mpp = &ill->ill_dlpi_deferred;
14364 		while (*mpp != NULL)
14365 			mpp = &((*mpp)->b_next);
14366 
14367 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14368 		    ill->ill_name));
14369 
14370 		*mpp = mp;
14371 		mutex_exit(&ill->ill_lock);
14372 		return;
14373 	}
14374 	mutex_exit(&ill->ill_lock);
14375 	ill_dlpi_dispatch(ill, mp);
14376 }
14377 
14378 static void
14379 ill_capability_send(ill_t *ill, mblk_t *mp)
14380 {
14381 	ill->ill_capab_pending_cnt++;
14382 	ill_dlpi_send(ill, mp);
14383 }
14384 
14385 void
14386 ill_capability_done(ill_t *ill)
14387 {
14388 	ASSERT(ill->ill_capab_pending_cnt != 0);
14389 
14390 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14391 
14392 	ill->ill_capab_pending_cnt--;
14393 	if (ill->ill_capab_pending_cnt == 0 &&
14394 	    ill->ill_dlpi_capab_state == IDCS_OK)
14395 		ill_capability_reset_alloc(ill);
14396 }
14397 
14398 /*
14399  * Send all deferred DLPI messages without waiting for their ACKs.
14400  */
14401 void
14402 ill_dlpi_send_deferred(ill_t *ill)
14403 {
14404 	mblk_t *mp, *nextmp;
14405 
14406 	/*
14407 	 * Clear ill_dlpi_pending so that the message is not queued in
14408 	 * ill_dlpi_send().
14409 	 */
14410 	mutex_enter(&ill->ill_lock);
14411 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14412 	mp = ill->ill_dlpi_deferred;
14413 	ill->ill_dlpi_deferred = NULL;
14414 	mutex_exit(&ill->ill_lock);
14415 
14416 	for (; mp != NULL; mp = nextmp) {
14417 		nextmp = mp->b_next;
14418 		mp->b_next = NULL;
14419 		ill_dlpi_send(ill, mp);
14420 	}
14421 }
14422 
14423 /*
14424  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14425  */
14426 boolean_t
14427 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14428 {
14429 	t_uscalar_t pending;
14430 
14431 	mutex_enter(&ill->ill_lock);
14432 	if (ill->ill_dlpi_pending == prim) {
14433 		mutex_exit(&ill->ill_lock);
14434 		return (B_TRUE);
14435 	}
14436 
14437 	/*
14438 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14439 	 * without waiting, so don't print any warnings in that case.
14440 	 */
14441 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14442 		mutex_exit(&ill->ill_lock);
14443 		return (B_FALSE);
14444 	}
14445 	pending = ill->ill_dlpi_pending;
14446 	mutex_exit(&ill->ill_lock);
14447 
14448 	if (pending == DL_PRIM_INVAL) {
14449 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14450 		    "received unsolicited ack for %s on %s\n",
14451 		    dl_primstr(prim), ill->ill_name);
14452 	} else {
14453 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14454 		    "received unexpected ack for %s on %s (expecting %s)\n",
14455 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14456 	}
14457 	return (B_FALSE);
14458 }
14459 
14460 /*
14461  * Complete the current DLPI operation associated with `prim' on `ill' and
14462  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14463  * operations and the ill's current exclusive IPSQ operation has finished
14464  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14465  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14466  * the comments above ipsq_current_finish() for details.
14467  */
14468 void
14469 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14470 {
14471 	mblk_t *mp;
14472 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14473 	ipxop_t *ipx = ipsq->ipsq_xop;
14474 
14475 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14476 	mutex_enter(&ill->ill_lock);
14477 
14478 	ASSERT(prim != DL_PRIM_INVAL);
14479 	ASSERT(ill->ill_dlpi_pending == prim);
14480 
14481 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14482 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14483 
14484 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14485 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14486 		if (ipx->ipx_current_done) {
14487 			mutex_enter(&ipx->ipx_lock);
14488 			ipx->ipx_current_ipif = NULL;
14489 			mutex_exit(&ipx->ipx_lock);
14490 		}
14491 		cv_signal(&ill->ill_cv);
14492 		mutex_exit(&ill->ill_lock);
14493 		return;
14494 	}
14495 
14496 	ill->ill_dlpi_deferred = mp->b_next;
14497 	mp->b_next = NULL;
14498 	mutex_exit(&ill->ill_lock);
14499 
14500 	ill_dlpi_dispatch(ill, mp);
14501 }
14502 
14503 void
14504 conn_delete_ire(conn_t *connp, caddr_t arg)
14505 {
14506 	ipif_t	*ipif = (ipif_t *)arg;
14507 	ire_t	*ire;
14508 
14509 	/*
14510 	 * Look at the cached ires on conns which has pointers to ipifs.
14511 	 * We just call ire_refrele which clears up the reference
14512 	 * to ire. Called when a conn closes. Also called from ipif_free
14513 	 * to cleanup indirect references to the stale ipif via the cached ire.
14514 	 */
14515 	mutex_enter(&connp->conn_lock);
14516 	ire = connp->conn_ire_cache;
14517 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14518 		connp->conn_ire_cache = NULL;
14519 		mutex_exit(&connp->conn_lock);
14520 		IRE_REFRELE_NOTR(ire);
14521 		return;
14522 	}
14523 	mutex_exit(&connp->conn_lock);
14524 
14525 }
14526 
14527 /*
14528  * Some operations (e.g., ipif_down()) conditionally delete a number
14529  * of IREs. Those IREs may have been previously cached in the conn structure.
14530  * This ipcl_walk() walker function releases all references to such IREs based
14531  * on the condemned flag.
14532  */
14533 /* ARGSUSED */
14534 void
14535 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14536 {
14537 	ire_t	*ire;
14538 
14539 	mutex_enter(&connp->conn_lock);
14540 	ire = connp->conn_ire_cache;
14541 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14542 		connp->conn_ire_cache = NULL;
14543 		mutex_exit(&connp->conn_lock);
14544 		IRE_REFRELE_NOTR(ire);
14545 		return;
14546 	}
14547 	mutex_exit(&connp->conn_lock);
14548 }
14549 
14550 /*
14551  * Take down a specific interface, but don't lose any information about it.
14552  * (Always called as writer.)
14553  * This function goes through the down sequence even if the interface is
14554  * already down. There are 2 reasons.
14555  * a. Currently we permit interface routes that depend on down interfaces
14556  *    to be added. This behaviour itself is questionable. However it appears
14557  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14558  *    time. We go thru the cleanup in order to remove these routes.
14559  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14560  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14561  *    down, but we need to cleanup i.e. do ill_dl_down and
14562  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14563  *
14564  * IP-MT notes:
14565  *
14566  * Model of reference to interfaces.
14567  *
14568  * The following members in ipif_t track references to the ipif.
14569  *	int     ipif_refcnt;    Active reference count
14570  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14571  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14572  *
14573  * The following members in ill_t track references to the ill.
14574  *	int             ill_refcnt;     active refcnt
14575  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14576  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14577  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14578  *
14579  * Reference to an ipif or ill can be obtained in any of the following ways.
14580  *
14581  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14582  * Pointers to ipif / ill from other data structures viz ire and conn.
14583  * Implicit reference to the ipif / ill by holding a reference to the ire.
14584  *
14585  * The ipif/ill lookup functions return a reference held ipif / ill.
14586  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14587  * This is a purely dynamic reference count associated with threads holding
14588  * references to the ipif / ill. Pointers from other structures do not
14589  * count towards this reference count.
14590  *
14591  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14592  * associated with the ipif/ill. This is incremented whenever a new
14593  * ire is created referencing the ipif/ill. This is done atomically inside
14594  * ire_add_v[46] where the ire is actually added to the ire hash table.
14595  * The count is decremented in ire_inactive where the ire is destroyed.
14596  *
14597  * nce's reference ill's thru nce_ill and the count of nce's associated with
14598  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14599  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14600  * table. Similarly it is decremented in ndp_inactive() where the nce
14601  * is destroyed.
14602  *
14603  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14604  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14605  * in ilm_walker_cleanup() or ilm_delete().
14606  *
14607  * Flow of ioctls involving interface down/up
14608  *
14609  * The following is the sequence of an attempt to set some critical flags on an
14610  * up interface.
14611  * ip_sioctl_flags
14612  * ipif_down
14613  * wait for ipif to be quiescent
14614  * ipif_down_tail
14615  * ip_sioctl_flags_tail
14616  *
14617  * All set ioctls that involve down/up sequence would have a skeleton similar
14618  * to the above. All the *tail functions are called after the refcounts have
14619  * dropped to the appropriate values.
14620  *
14621  * The mechanism to quiesce an ipif is as follows.
14622  *
14623  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14624  * on the ipif. Callers either pass a flag requesting wait or the lookup
14625  *  functions will return NULL.
14626  *
14627  * Delete all ires referencing this ipif
14628  *
14629  * Any thread attempting to do an ipif_refhold on an ipif that has been
14630  * obtained thru a cached pointer will first make sure that
14631  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14632  * increment the refcount.
14633  *
14634  * The above guarantees that the ipif refcount will eventually come down to
14635  * zero and the ipif will quiesce, once all threads that currently hold a
14636  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14637  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14638  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14639  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14640  * in ip.h
14641  *
14642  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14643  *
14644  * Threads trying to lookup an ipif or ill can pass a flag requesting
14645  * wait and restart if the ipif / ill cannot be looked up currently.
14646  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14647  * failure if the ipif is currently undergoing an exclusive operation, and
14648  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14649  * is restarted by ipsq_exit() when the current exclusive operation completes.
14650  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14651  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14652  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14653  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14654  * until we release the ipsq_lock, even though the the ill/ipif state flags
14655  * can change after we drop the ill_lock.
14656  *
14657  * An attempt to send out a packet using an ipif that is currently
14658  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14659  * operation and restart it later when the exclusive condition on the ipif ends.
14660  * This is an example of not passing the wait flag to the lookup functions. For
14661  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14662  * out a multicast packet on that ipif will fail while the ipif is
14663  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14664  * currently IPIF_CHANGING will also fail.
14665  */
14666 int
14667 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14668 {
14669 	ill_t		*ill = ipif->ipif_ill;
14670 	conn_t		*connp;
14671 	boolean_t	success;
14672 	boolean_t	ipif_was_up = B_FALSE;
14673 	ip_stack_t	*ipst = ill->ill_ipst;
14674 
14675 	ASSERT(IAM_WRITER_IPIF(ipif));
14676 
14677 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14678 
14679 	if (ipif->ipif_flags & IPIF_UP) {
14680 		mutex_enter(&ill->ill_lock);
14681 		ipif->ipif_flags &= ~IPIF_UP;
14682 		ASSERT(ill->ill_ipif_up_count > 0);
14683 		--ill->ill_ipif_up_count;
14684 		mutex_exit(&ill->ill_lock);
14685 		ipif_was_up = B_TRUE;
14686 		/* Update status in SCTP's list */
14687 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14688 		ill_nic_event_dispatch(ipif->ipif_ill,
14689 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14690 	}
14691 
14692 	/*
14693 	 * Blow away memberships we established in ipif_multicast_up().
14694 	 */
14695 	ipif_multicast_down(ipif);
14696 
14697 	/*
14698 	 * Remove from the mapping for __sin6_src_id. We insert only
14699 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14700 	 * stored as mapped addresses, we need to check for mapped
14701 	 * INADDR_ANY also.
14702 	 */
14703 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14704 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14705 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14706 		int err;
14707 
14708 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14709 		    ipif->ipif_zoneid, ipst);
14710 		if (err != 0) {
14711 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14712 		}
14713 	}
14714 
14715 	/*
14716 	 * Delete all IRE's pointing at this ipif or its source address.
14717 	 */
14718 	if (ipif->ipif_isv6) {
14719 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14720 		    ipst);
14721 	} else {
14722 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14723 		    ipst);
14724 	}
14725 
14726 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14727 		/*
14728 		 * Since the interface is now down, it may have just become
14729 		 * inactive.  Note that this needs to be done even for a
14730 		 * lll_logical_down(), or ARP entries will not get correctly
14731 		 * restored when the interface comes back up.
14732 		 */
14733 		if (IS_UNDER_IPMP(ill))
14734 			ipmp_ill_refresh_active(ill);
14735 	}
14736 
14737 	/*
14738 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14739 	 * ires have been deleted above. Otherwise a thread could end up
14740 	 * caching an ire in a conn after we have finished the cleanup of the
14741 	 * conn. The caching is done after making sure that the ire is not yet
14742 	 * condemned. Also documented in the block comment above ip_output
14743 	 */
14744 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14745 	/* Also, delete the ires cached in SCTP */
14746 	sctp_ire_cache_flush(ipif);
14747 
14748 	/*
14749 	 * Update any other ipifs which have used "our" local address as
14750 	 * a source address. This entails removing and recreating IRE_INTERFACE
14751 	 * entries for such ipifs.
14752 	 */
14753 	if (ipif->ipif_isv6)
14754 		ipif_update_other_ipifs_v6(ipif);
14755 	else
14756 		ipif_update_other_ipifs(ipif);
14757 
14758 	/*
14759 	 * neighbor-discovery or arp entries for this interface.
14760 	 */
14761 	ipif_ndp_down(ipif);
14762 
14763 	/*
14764 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14765 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14766 	 * and ill_delete -> ipif_free -> ipif_down
14767 	 */
14768 	if (mp == NULL) {
14769 		ASSERT(q == NULL);
14770 		return (0);
14771 	}
14772 
14773 	if (CONN_Q(q)) {
14774 		connp = Q_TO_CONN(q);
14775 		mutex_enter(&connp->conn_lock);
14776 	} else {
14777 		connp = NULL;
14778 	}
14779 	mutex_enter(&ill->ill_lock);
14780 	/*
14781 	 * Are there any ire's pointing to this ipif that are still active ?
14782 	 * If this is the last ipif going down, are there any ire's pointing
14783 	 * to this ill that are still active ?
14784 	 */
14785 	if (ipif_is_quiescent(ipif)) {
14786 		mutex_exit(&ill->ill_lock);
14787 		if (connp != NULL)
14788 			mutex_exit(&connp->conn_lock);
14789 		return (0);
14790 	}
14791 
14792 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14793 	    ill->ill_name, (void *)ill));
14794 	/*
14795 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14796 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14797 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14798 	 */
14799 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14800 	if (!success) {
14801 		/* The conn is closing. So just return */
14802 		ASSERT(connp != NULL);
14803 		mutex_exit(&ill->ill_lock);
14804 		mutex_exit(&connp->conn_lock);
14805 		return (EINTR);
14806 	}
14807 
14808 	mutex_exit(&ill->ill_lock);
14809 	if (connp != NULL)
14810 		mutex_exit(&connp->conn_lock);
14811 	return (EINPROGRESS);
14812 }
14813 
14814 void
14815 ipif_down_tail(ipif_t *ipif)
14816 {
14817 	ill_t	*ill = ipif->ipif_ill;
14818 
14819 	/*
14820 	 * Skip any loopback interface (null wq).
14821 	 * If this is the last logical interface on the ill
14822 	 * have ill_dl_down tell the driver we are gone (unbind)
14823 	 * Note that lun 0 can ipif_down even though
14824 	 * there are other logical units that are up.
14825 	 * This occurs e.g. when we change a "significant" IFF_ flag.
14826 	 */
14827 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
14828 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
14829 	    ill->ill_dl_up) {
14830 		ill_dl_down(ill);
14831 	}
14832 	ill->ill_logical_down = 0;
14833 
14834 	/*
14835 	 * Has to be after removing the routes in ipif_down_delete_ire.
14836 	 */
14837 	ipif_resolver_down(ipif);
14838 
14839 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
14840 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
14841 }
14842 
14843 /*
14844  * Bring interface logically down without bringing the physical interface
14845  * down e.g. when the netmask is changed. This avoids long lasting link
14846  * negotiations between an ethernet interface and a certain switches.
14847  */
14848 static int
14849 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14850 {
14851 	/*
14852 	 * The ill_logical_down flag is a transient flag. It is set here
14853 	 * and is cleared once the down has completed in ipif_down_tail.
14854 	 * This flag does not indicate whether the ill stream is in the
14855 	 * DL_BOUND state with the driver. Instead this flag is used by
14856 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
14857 	 * the driver. The state of the ill stream i.e. whether it is
14858 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
14859 	 */
14860 	ipif->ipif_ill->ill_logical_down = 1;
14861 	return (ipif_down(ipif, q, mp));
14862 }
14863 
14864 /*
14865  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
14866  * If the usesrc client ILL is already part of a usesrc group or not,
14867  * in either case a ire_stq with the matching usesrc client ILL will
14868  * locate the IRE's that need to be deleted. We want IREs to be created
14869  * with the new source address.
14870  */
14871 static void
14872 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
14873 {
14874 	ill_t	*ucill = (ill_t *)ill_arg;
14875 
14876 	ASSERT(IAM_WRITER_ILL(ucill));
14877 
14878 	if (ire->ire_stq == NULL)
14879 		return;
14880 
14881 	if ((ire->ire_type == IRE_CACHE) &&
14882 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
14883 		ire_delete(ire);
14884 }
14885 
14886 /*
14887  * ire_walk routine to delete every IRE dependent on the interface
14888  * address that is going down.	(Always called as writer.)
14889  * Works for both v4 and v6.
14890  * In addition for checking for ire_ipif matches it also checks for
14891  * IRE_CACHE entries which have the same source address as the
14892  * disappearing ipif since ipif_select_source might have picked
14893  * that source. Note that ipif_down/ipif_update_other_ipifs takes
14894  * care of any IRE_INTERFACE with the disappearing source address.
14895  */
14896 static void
14897 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
14898 {
14899 	ipif_t	*ipif = (ipif_t *)ipif_arg;
14900 
14901 	ASSERT(IAM_WRITER_IPIF(ipif));
14902 	if (ire->ire_ipif == NULL)
14903 		return;
14904 
14905 	if (ire->ire_ipif != ipif) {
14906 		/*
14907 		 * Look for a matching source address.
14908 		 */
14909 		if (ire->ire_type != IRE_CACHE)
14910 			return;
14911 		if (ipif->ipif_flags & IPIF_NOLOCAL)
14912 			return;
14913 
14914 		if (ire->ire_ipversion == IPV4_VERSION) {
14915 			if (ire->ire_src_addr != ipif->ipif_src_addr)
14916 				return;
14917 		} else {
14918 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
14919 			    &ipif->ipif_v6lcl_addr))
14920 				return;
14921 		}
14922 		ire_delete(ire);
14923 		return;
14924 	}
14925 	/*
14926 	 * ire_delete() will do an ire_flush_cache which will delete
14927 	 * all ire_ipif matches
14928 	 */
14929 	ire_delete(ire);
14930 }
14931 
14932 /*
14933  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
14934  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
14935  * 2) when an interface is brought up or down (on that ill).
14936  * This ensures that the IRE_CACHE entries don't retain stale source
14937  * address selection results.
14938  */
14939 void
14940 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
14941 {
14942 	ill_t	*ill = (ill_t *)ill_arg;
14943 
14944 	ASSERT(IAM_WRITER_ILL(ill));
14945 	ASSERT(ire->ire_type == IRE_CACHE);
14946 
14947 	/*
14948 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14949 	 * ill, but we only want to delete the IRE if ire_ipif matches.
14950 	 */
14951 	ASSERT(ire->ire_ipif != NULL);
14952 	if (ill == ire->ire_ipif->ipif_ill)
14953 		ire_delete(ire);
14954 }
14955 
14956 /*
14957  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
14958  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
14959  * the IPMP ill.
14960  */
14961 void
14962 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
14963 {
14964 	ill_t	*ill = (ill_t *)ill_arg;
14965 
14966 	ASSERT(IAM_WRITER_ILL(ill));
14967 	ASSERT(ire->ire_type == IRE_CACHE);
14968 
14969 	/*
14970 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14971 	 * ill, but we only want to delete the IRE if ire_stq matches.
14972 	 */
14973 	if (ire->ire_stq->q_ptr == ill_arg)
14974 		ire_delete(ire);
14975 }
14976 
14977 /*
14978  * Delete all the IREs whose ire_stq's reference any ill in the same IPMP
14979  * group as `ill_arg'.  Used by ipmp_ill_deactivate() to flush all IRE_CACHE
14980  * entries for the illgrp.
14981  */
14982 void
14983 ill_grp_cache_delete(ire_t *ire, char *ill_arg)
14984 {
14985 	ill_t	*ill = (ill_t *)ill_arg;
14986 
14987 	ASSERT(IAM_WRITER_ILL(ill));
14988 
14989 	if (ire->ire_type == IRE_CACHE &&
14990 	    IS_IN_SAME_ILLGRP((ill_t *)ire->ire_stq->q_ptr, ill)) {
14991 		ire_delete(ire);
14992 	}
14993 }
14994 
14995 /*
14996  * Delete all broadcast IREs with a source address on `ill_arg'.
14997  */
14998 static void
14999 ill_broadcast_delete(ire_t *ire, char *ill_arg)
15000 {
15001 	ill_t *ill = (ill_t *)ill_arg;
15002 
15003 	ASSERT(IAM_WRITER_ILL(ill));
15004 	ASSERT(ire->ire_type == IRE_BROADCAST);
15005 
15006 	if (ire->ire_ipif->ipif_ill == ill)
15007 		ire_delete(ire);
15008 }
15009 
15010 /*
15011  * Initiate deallocate of an IPIF. Always called as writer. Called by
15012  * ill_delete or ip_sioctl_removeif.
15013  */
15014 static void
15015 ipif_free(ipif_t *ipif)
15016 {
15017 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15018 
15019 	ASSERT(IAM_WRITER_IPIF(ipif));
15020 
15021 	if (ipif->ipif_recovery_id != 0)
15022 		(void) untimeout(ipif->ipif_recovery_id);
15023 	ipif->ipif_recovery_id = 0;
15024 
15025 	/* Remove conn references */
15026 	reset_conn_ipif(ipif);
15027 
15028 	/*
15029 	 * Make sure we have valid net and subnet broadcast ire's for the
15030 	 * other ipif's which share them with this ipif.
15031 	 */
15032 	if (!ipif->ipif_isv6)
15033 		ipif_check_bcast_ires(ipif);
15034 
15035 	/*
15036 	 * Take down the interface. We can be called either from ill_delete
15037 	 * or from ip_sioctl_removeif.
15038 	 */
15039 	(void) ipif_down(ipif, NULL, NULL);
15040 
15041 	/*
15042 	 * Now that the interface is down, there's no chance it can still
15043 	 * become a duplicate.  Cancel any timer that may have been set while
15044 	 * tearing down.
15045 	 */
15046 	if (ipif->ipif_recovery_id != 0)
15047 		(void) untimeout(ipif->ipif_recovery_id);
15048 	ipif->ipif_recovery_id = 0;
15049 
15050 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15051 	/* Remove pointers to this ill in the multicast routing tables */
15052 	reset_mrt_vif_ipif(ipif);
15053 	/* If necessary, clear the cached source ipif rotor. */
15054 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15055 		ipif->ipif_ill->ill_src_ipif = NULL;
15056 	rw_exit(&ipst->ips_ill_g_lock);
15057 }
15058 
15059 static void
15060 ipif_free_tail(ipif_t *ipif)
15061 {
15062 	mblk_t	*mp;
15063 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15064 
15065 	/*
15066 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15067 	 */
15068 	mutex_enter(&ipif->ipif_saved_ire_lock);
15069 	mp = ipif->ipif_saved_ire_mp;
15070 	ipif->ipif_saved_ire_mp = NULL;
15071 	mutex_exit(&ipif->ipif_saved_ire_lock);
15072 	freemsg(mp);
15073 
15074 	/*
15075 	 * Need to hold both ill_g_lock and ill_lock while
15076 	 * inserting or removing an ipif from the linked list
15077 	 * of ipifs hanging off the ill.
15078 	 */
15079 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15080 
15081 	ASSERT(ilm_walk_ipif(ipif) == 0);
15082 
15083 #ifdef DEBUG
15084 	ipif_trace_cleanup(ipif);
15085 #endif
15086 
15087 	/* Ask SCTP to take it out of it list */
15088 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15089 
15090 	/* Get it out of the ILL interface list. */
15091 	ipif_remove(ipif);
15092 	rw_exit(&ipst->ips_ill_g_lock);
15093 
15094 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15095 
15096 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15097 	ASSERT(ipif->ipif_recovery_id == 0);
15098 
15099 	/* Free the memory. */
15100 	mi_free(ipif);
15101 }
15102 
15103 /*
15104  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15105  * is zero.
15106  */
15107 void
15108 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15109 {
15110 	char	lbuf[LIFNAMSIZ];
15111 	char	*name;
15112 	size_t	name_len;
15113 
15114 	buf[0] = '\0';
15115 	name = ipif->ipif_ill->ill_name;
15116 	name_len = ipif->ipif_ill->ill_name_length;
15117 	if (ipif->ipif_id != 0) {
15118 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15119 		    ipif->ipif_id);
15120 		name = lbuf;
15121 		name_len = mi_strlen(name) + 1;
15122 	}
15123 	len -= 1;
15124 	buf[len] = '\0';
15125 	len = MIN(len, name_len);
15126 	bcopy(name, buf, len);
15127 }
15128 
15129 /*
15130  * Find an IPIF based on the name passed in.  Names can be of the
15131  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15132  * The <phys> string can have forms like <dev><#> (e.g., le0),
15133  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15134  * When there is no colon, the implied unit id is zero. <phys> must
15135  * correspond to the name of an ILL.  (May be called as writer.)
15136  */
15137 static ipif_t *
15138 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15139     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15140     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15141 {
15142 	char	*cp;
15143 	char	*endp;
15144 	long	id;
15145 	ill_t	*ill;
15146 	ipif_t	*ipif;
15147 	uint_t	ire_type;
15148 	boolean_t did_alloc = B_FALSE;
15149 	ipsq_t	*ipsq;
15150 
15151 	if (error != NULL)
15152 		*error = 0;
15153 
15154 	/*
15155 	 * If the caller wants to us to create the ipif, make sure we have a
15156 	 * valid zoneid
15157 	 */
15158 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15159 
15160 	if (namelen == 0) {
15161 		if (error != NULL)
15162 			*error = ENXIO;
15163 		return (NULL);
15164 	}
15165 
15166 	*exists = B_FALSE;
15167 	/* Look for a colon in the name. */
15168 	endp = &name[namelen];
15169 	for (cp = endp; --cp > name; ) {
15170 		if (*cp == IPIF_SEPARATOR_CHAR)
15171 			break;
15172 	}
15173 
15174 	if (*cp == IPIF_SEPARATOR_CHAR) {
15175 		/*
15176 		 * Reject any non-decimal aliases for logical
15177 		 * interfaces. Aliases with leading zeroes
15178 		 * are also rejected as they introduce ambiguity
15179 		 * in the naming of the interfaces.
15180 		 * In order to confirm with existing semantics,
15181 		 * and to not break any programs/script relying
15182 		 * on that behaviour, if<0>:0 is considered to be
15183 		 * a valid interface.
15184 		 *
15185 		 * If alias has two or more digits and the first
15186 		 * is zero, fail.
15187 		 */
15188 		if (&cp[2] < endp && cp[1] == '0') {
15189 			if (error != NULL)
15190 				*error = EINVAL;
15191 			return (NULL);
15192 		}
15193 	}
15194 
15195 	if (cp <= name) {
15196 		cp = endp;
15197 	} else {
15198 		*cp = '\0';
15199 	}
15200 
15201 	/*
15202 	 * Look up the ILL, based on the portion of the name
15203 	 * before the slash. ill_lookup_on_name returns a held ill.
15204 	 * Temporary to check whether ill exists already. If so
15205 	 * ill_lookup_on_name will clear it.
15206 	 */
15207 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15208 	    q, mp, func, error, &did_alloc, ipst);
15209 	if (cp != endp)
15210 		*cp = IPIF_SEPARATOR_CHAR;
15211 	if (ill == NULL)
15212 		return (NULL);
15213 
15214 	/* Establish the unit number in the name. */
15215 	id = 0;
15216 	if (cp < endp && *endp == '\0') {
15217 		/* If there was a colon, the unit number follows. */
15218 		cp++;
15219 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15220 			ill_refrele(ill);
15221 			if (error != NULL)
15222 				*error = ENXIO;
15223 			return (NULL);
15224 		}
15225 	}
15226 
15227 	GRAB_CONN_LOCK(q);
15228 	mutex_enter(&ill->ill_lock);
15229 	/* Now see if there is an IPIF with this unit number. */
15230 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15231 		if (ipif->ipif_id == id) {
15232 			if (zoneid != ALL_ZONES &&
15233 			    zoneid != ipif->ipif_zoneid &&
15234 			    ipif->ipif_zoneid != ALL_ZONES) {
15235 				mutex_exit(&ill->ill_lock);
15236 				RELEASE_CONN_LOCK(q);
15237 				ill_refrele(ill);
15238 				if (error != NULL)
15239 					*error = ENXIO;
15240 				return (NULL);
15241 			}
15242 			/*
15243 			 * The block comment at the start of ipif_down
15244 			 * explains the use of the macros used below
15245 			 */
15246 			if (IPIF_CAN_LOOKUP(ipif)) {
15247 				ipif_refhold_locked(ipif);
15248 				mutex_exit(&ill->ill_lock);
15249 				if (!did_alloc)
15250 					*exists = B_TRUE;
15251 				/*
15252 				 * Drop locks before calling ill_refrele
15253 				 * since it can potentially call into
15254 				 * ipif_ill_refrele_tail which can end up
15255 				 * in trying to acquire any lock.
15256 				 */
15257 				RELEASE_CONN_LOCK(q);
15258 				ill_refrele(ill);
15259 				return (ipif);
15260 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15261 				ipsq = ill->ill_phyint->phyint_ipsq;
15262 				mutex_enter(&ipsq->ipsq_lock);
15263 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15264 				mutex_exit(&ill->ill_lock);
15265 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15266 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15267 				mutex_exit(&ipsq->ipsq_lock);
15268 				RELEASE_CONN_LOCK(q);
15269 				ill_refrele(ill);
15270 				if (error != NULL)
15271 					*error = EINPROGRESS;
15272 				return (NULL);
15273 			}
15274 		}
15275 	}
15276 	RELEASE_CONN_LOCK(q);
15277 
15278 	if (!do_alloc) {
15279 		mutex_exit(&ill->ill_lock);
15280 		ill_refrele(ill);
15281 		if (error != NULL)
15282 			*error = ENXIO;
15283 		return (NULL);
15284 	}
15285 
15286 	/*
15287 	 * If none found, atomically allocate and return a new one.
15288 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15289 	 * to support "receive only" use of lo0:1 etc. as is still done
15290 	 * below as an initial guess.
15291 	 * However, this is now likely to be overriden later in ipif_up_done()
15292 	 * when we know for sure what address has been configured on the
15293 	 * interface, since we might have more than one loopback interface
15294 	 * with a loopback address, e.g. in the case of zones, and all the
15295 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15296 	 */
15297 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15298 		ire_type = IRE_LOOPBACK;
15299 	else
15300 		ire_type = IRE_LOCAL;
15301 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15302 	if (ipif != NULL)
15303 		ipif_refhold_locked(ipif);
15304 	else if (error != NULL)
15305 		*error = ENOMEM;
15306 	mutex_exit(&ill->ill_lock);
15307 	ill_refrele(ill);
15308 	return (ipif);
15309 }
15310 
15311 /*
15312  * This routine is called whenever a new address comes up on an ipif.  If
15313  * we are configured to respond to address mask requests, then we are supposed
15314  * to broadcast an address mask reply at this time.  This routine is also
15315  * called if we are already up, but a netmask change is made.  This is legal
15316  * but might not make the system manager very popular.	(May be called
15317  * as writer.)
15318  */
15319 void
15320 ipif_mask_reply(ipif_t *ipif)
15321 {
15322 	icmph_t	*icmph;
15323 	ipha_t	*ipha;
15324 	mblk_t	*mp;
15325 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15326 
15327 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15328 
15329 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15330 		return;
15331 
15332 	/* ICMP mask reply is IPv4 only */
15333 	ASSERT(!ipif->ipif_isv6);
15334 	/* ICMP mask reply is not for a loopback interface */
15335 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15336 
15337 	mp = allocb(REPLY_LEN, BPRI_HI);
15338 	if (mp == NULL)
15339 		return;
15340 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15341 
15342 	ipha = (ipha_t *)mp->b_rptr;
15343 	bzero(ipha, REPLY_LEN);
15344 	*ipha = icmp_ipha;
15345 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15346 	ipha->ipha_src = ipif->ipif_src_addr;
15347 	ipha->ipha_dst = ipif->ipif_brd_addr;
15348 	ipha->ipha_length = htons(REPLY_LEN);
15349 	ipha->ipha_ident = 0;
15350 
15351 	icmph = (icmph_t *)&ipha[1];
15352 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15353 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15354 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15355 
15356 	put(ipif->ipif_wq, mp);
15357 
15358 #undef	REPLY_LEN
15359 }
15360 
15361 /*
15362  * When the mtu in the ipif changes, we call this routine through ire_walk
15363  * to update all the relevant IREs.
15364  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15365  */
15366 static void
15367 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15368 {
15369 	ipif_t *ipif = (ipif_t *)ipif_arg;
15370 
15371 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15372 		return;
15373 
15374 	mutex_enter(&ire->ire_lock);
15375 	if (ire->ire_marks & IRE_MARK_PMTU) {
15376 		/* Avoid increasing the PMTU */
15377 		ire->ire_max_frag = MIN(ipif->ipif_mtu, ire->ire_max_frag);
15378 		if (ire->ire_max_frag == ipif->ipif_mtu)
15379 			ire->ire_marks &= ~IRE_MARK_PMTU;
15380 	} else {
15381 		ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15382 	}
15383 	mutex_exit(&ire->ire_lock);
15384 }
15385 
15386 /*
15387  * When the mtu in the ill changes, we call this routine through ire_walk
15388  * to update all the relevant IREs.
15389  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15390  */
15391 void
15392 ill_mtu_change(ire_t *ire, char *ill_arg)
15393 {
15394 	ill_t	*ill = (ill_t *)ill_arg;
15395 
15396 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15397 		return;
15398 
15399 	mutex_enter(&ire->ire_lock);
15400 	if (ire->ire_marks & IRE_MARK_PMTU) {
15401 		/* Avoid increasing the PMTU */
15402 		ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
15403 		    ire->ire_max_frag);
15404 		if (ire->ire_max_frag == ire->ire_ipif->ipif_mtu) {
15405 			ire->ire_marks &= ~IRE_MARK_PMTU;
15406 		}
15407 	} else {
15408 		ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu, IP_MAXPACKET);
15409 	}
15410 	mutex_exit(&ire->ire_lock);
15411 }
15412 
15413 /*
15414  * Join the ipif specific multicast groups.
15415  * Must be called after a mapping has been set up in the resolver.  (Always
15416  * called as writer.)
15417  */
15418 void
15419 ipif_multicast_up(ipif_t *ipif)
15420 {
15421 	int err;
15422 	ill_t *ill;
15423 
15424 	ASSERT(IAM_WRITER_IPIF(ipif));
15425 
15426 	ill = ipif->ipif_ill;
15427 
15428 	ip1dbg(("ipif_multicast_up\n"));
15429 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15430 		return;
15431 
15432 	if (ipif->ipif_isv6) {
15433 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15434 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15435 
15436 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15437 
15438 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15439 			return;
15440 
15441 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15442 
15443 		/*
15444 		 * Join the all hosts multicast address.  We skip this for
15445 		 * underlying IPMP interfaces since they should be invisible.
15446 		 */
15447 		if (!IS_UNDER_IPMP(ill)) {
15448 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15449 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15450 			if (err != 0) {
15451 				ip0dbg(("ipif_multicast_up: "
15452 				    "all_hosts_mcast failed %d\n", err));
15453 				return;
15454 			}
15455 			ipif->ipif_joined_allhosts = 1;
15456 		}
15457 
15458 		/*
15459 		 * Enable multicast for the solicited node multicast address
15460 		 */
15461 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15462 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15463 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15464 			if (err != 0) {
15465 				ip0dbg(("ipif_multicast_up: solicited MC"
15466 				    " failed %d\n", err));
15467 				if (ipif->ipif_joined_allhosts) {
15468 					(void) ip_delmulti_v6(&v6allmc, ill,
15469 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15470 					ipif->ipif_joined_allhosts = 0;
15471 				}
15472 				return;
15473 			}
15474 		}
15475 	} else {
15476 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15477 			return;
15478 
15479 		/* Join the all hosts multicast address */
15480 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15481 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15482 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15483 		if (err) {
15484 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15485 			return;
15486 		}
15487 	}
15488 	ipif->ipif_multicast_up = 1;
15489 }
15490 
15491 /*
15492  * Blow away any multicast groups that we joined in ipif_multicast_up().
15493  * (Explicit memberships are blown away in ill_leave_multicast() when the
15494  * ill is brought down.)
15495  */
15496 void
15497 ipif_multicast_down(ipif_t *ipif)
15498 {
15499 	int err;
15500 
15501 	ASSERT(IAM_WRITER_IPIF(ipif));
15502 
15503 	ip1dbg(("ipif_multicast_down\n"));
15504 	if (!ipif->ipif_multicast_up)
15505 		return;
15506 
15507 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15508 
15509 	if (!ipif->ipif_isv6) {
15510 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15511 		    B_TRUE);
15512 		if (err != 0)
15513 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15514 
15515 		ipif->ipif_multicast_up = 0;
15516 		return;
15517 	}
15518 
15519 	/*
15520 	 * Leave the all-hosts multicast address.
15521 	 */
15522 	if (ipif->ipif_joined_allhosts) {
15523 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15524 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15525 		if (err != 0) {
15526 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15527 			    "failed %d\n", err));
15528 		}
15529 		ipif->ipif_joined_allhosts = 0;
15530 	}
15531 
15532 	/*
15533 	 * Disable multicast for the solicited node multicast address
15534 	 */
15535 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15536 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15537 
15538 		ipv6_multi.s6_addr32[3] |=
15539 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15540 
15541 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15542 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15543 		if (err != 0) {
15544 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15545 			    err));
15546 		}
15547 	}
15548 
15549 	ipif->ipif_multicast_up = 0;
15550 }
15551 
15552 /*
15553  * Used when an interface comes up to recreate any extra routes on this
15554  * interface.
15555  */
15556 static ire_t **
15557 ipif_recover_ire(ipif_t *ipif)
15558 {
15559 	mblk_t	*mp;
15560 	ire_t	**ipif_saved_irep;
15561 	ire_t	**irep;
15562 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15563 
15564 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15565 	    ipif->ipif_id));
15566 
15567 	mutex_enter(&ipif->ipif_saved_ire_lock);
15568 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15569 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15570 	if (ipif_saved_irep == NULL) {
15571 		mutex_exit(&ipif->ipif_saved_ire_lock);
15572 		return (NULL);
15573 	}
15574 
15575 	irep = ipif_saved_irep;
15576 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15577 		ire_t		*ire;
15578 		queue_t		*rfq;
15579 		queue_t		*stq;
15580 		ifrt_t		*ifrt;
15581 		uchar_t		*src_addr;
15582 		uchar_t		*gateway_addr;
15583 		ushort_t	type;
15584 
15585 		/*
15586 		 * When the ire was initially created and then added in
15587 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15588 		 * in the case of a traditional interface route, or as one of
15589 		 * the IRE_OFFSUBNET types (with the exception of
15590 		 * IRE_HOST types ire which is created by icmp_redirect() and
15591 		 * which we don't need to save or recover).  In the case where
15592 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15593 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15594 		 * to satisfy software like GateD and Sun Cluster which creates
15595 		 * routes using the the loopback interface's address as a
15596 		 * gateway.
15597 		 *
15598 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15599 		 * ire_create() will be called in the same way here as
15600 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15601 		 * the route looks like a traditional interface route (where
15602 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15603 		 * the saved ifrt->ifrt_type.  This means that in the case where
15604 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15605 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15606 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15607 		 */
15608 		ifrt = (ifrt_t *)mp->b_rptr;
15609 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15610 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15611 			rfq = NULL;
15612 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15613 			    ? ipif->ipif_rq : ipif->ipif_wq;
15614 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15615 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15616 			    : (uint8_t *)&ipif->ipif_src_addr;
15617 			gateway_addr = NULL;
15618 			type = ipif->ipif_net_type;
15619 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15620 			/* Recover multiroute broadcast IRE. */
15621 			rfq = ipif->ipif_rq;
15622 			stq = ipif->ipif_wq;
15623 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15624 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15625 			    : (uint8_t *)&ipif->ipif_src_addr;
15626 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15627 			type = ifrt->ifrt_type;
15628 		} else {
15629 			rfq = NULL;
15630 			stq = NULL;
15631 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15632 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15633 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15634 			type = ifrt->ifrt_type;
15635 		}
15636 
15637 		/*
15638 		 * Create a copy of the IRE with the saved address and netmask.
15639 		 */
15640 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15641 		    "0x%x/0x%x\n",
15642 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15643 		    ntohl(ifrt->ifrt_addr),
15644 		    ntohl(ifrt->ifrt_mask)));
15645 		ire = ire_create(
15646 		    (uint8_t *)&ifrt->ifrt_addr,
15647 		    (uint8_t *)&ifrt->ifrt_mask,
15648 		    src_addr,
15649 		    gateway_addr,
15650 		    &ifrt->ifrt_max_frag,
15651 		    NULL,
15652 		    rfq,
15653 		    stq,
15654 		    type,
15655 		    ipif,
15656 		    0,
15657 		    0,
15658 		    0,
15659 		    ifrt->ifrt_flags,
15660 		    &ifrt->ifrt_iulp_info,
15661 		    NULL,
15662 		    NULL,
15663 		    ipst);
15664 
15665 		if (ire == NULL) {
15666 			mutex_exit(&ipif->ipif_saved_ire_lock);
15667 			kmem_free(ipif_saved_irep,
15668 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15669 			return (NULL);
15670 		}
15671 
15672 		/*
15673 		 * Some software (for example, GateD and Sun Cluster) attempts
15674 		 * to create (what amount to) IRE_PREFIX routes with the
15675 		 * loopback address as the gateway.  This is primarily done to
15676 		 * set up prefixes with the RTF_REJECT flag set (for example,
15677 		 * when generating aggregate routes.)
15678 		 *
15679 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15680 		 * IRE_LOOPBACK, then we map the request into a
15681 		 * IRE_IF_NORESOLVER.
15682 		 */
15683 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15684 			ire->ire_type = IRE_IF_NORESOLVER;
15685 		/*
15686 		 * ire held by ire_add, will be refreled' towards the
15687 		 * the end of ipif_up_done
15688 		 */
15689 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15690 		*irep = ire;
15691 		irep++;
15692 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15693 	}
15694 	mutex_exit(&ipif->ipif_saved_ire_lock);
15695 	return (ipif_saved_irep);
15696 }
15697 
15698 /*
15699  * Used to set the netmask and broadcast address to default values when the
15700  * interface is brought up.  (Always called as writer.)
15701  */
15702 static void
15703 ipif_set_default(ipif_t *ipif)
15704 {
15705 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15706 
15707 	if (!ipif->ipif_isv6) {
15708 		/*
15709 		 * Interface holds an IPv4 address. Default
15710 		 * mask is the natural netmask.
15711 		 */
15712 		if (!ipif->ipif_net_mask) {
15713 			ipaddr_t	v4mask;
15714 
15715 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15716 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15717 		}
15718 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15719 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15720 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15721 		} else {
15722 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15723 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15724 		}
15725 		/*
15726 		 * NOTE: SunOS 4.X does this even if the broadcast address
15727 		 * has been already set thus we do the same here.
15728 		 */
15729 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15730 			ipaddr_t	v4addr;
15731 
15732 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15733 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15734 		}
15735 	} else {
15736 		/*
15737 		 * Interface holds an IPv6-only address.  Default
15738 		 * mask is all-ones.
15739 		 */
15740 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15741 			ipif->ipif_v6net_mask = ipv6_all_ones;
15742 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15743 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15744 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15745 		} else {
15746 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15747 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15748 		}
15749 	}
15750 }
15751 
15752 /*
15753  * Return 0 if this address can be used as local address without causing
15754  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15755  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15756  * Note that the same IPv6 link-local address is allowed as long as the ills
15757  * are not on the same link.
15758  */
15759 int
15760 ip_addr_availability_check(ipif_t *new_ipif)
15761 {
15762 	in6_addr_t our_v6addr;
15763 	ill_t *ill;
15764 	ipif_t *ipif;
15765 	ill_walk_context_t ctx;
15766 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15767 
15768 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15769 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15770 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15771 
15772 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15773 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15774 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15775 		return (0);
15776 
15777 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15778 
15779 	if (new_ipif->ipif_isv6)
15780 		ill = ILL_START_WALK_V6(&ctx, ipst);
15781 	else
15782 		ill = ILL_START_WALK_V4(&ctx, ipst);
15783 
15784 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15785 		for (ipif = ill->ill_ipif; ipif != NULL;
15786 		    ipif = ipif->ipif_next) {
15787 			if ((ipif == new_ipif) ||
15788 			    !(ipif->ipif_flags & IPIF_UP) ||
15789 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15790 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15791 			    &our_v6addr))
15792 				continue;
15793 
15794 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15795 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15796 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15797 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15798 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15799 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15800 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15801 				continue;
15802 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15803 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15804 				continue;
15805 			else if (new_ipif->ipif_ill == ill)
15806 				return (EADDRINUSE);
15807 			else
15808 				return (EADDRNOTAVAIL);
15809 		}
15810 	}
15811 
15812 	return (0);
15813 }
15814 
15815 /*
15816  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15817  * IREs for the ipif.
15818  * When the routine returns EINPROGRESS then mp has been consumed and
15819  * the ioctl will be acked from ip_rput_dlpi.
15820  */
15821 int
15822 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15823 {
15824 	ill_t		*ill = ipif->ipif_ill;
15825 	boolean_t 	isv6 = ipif->ipif_isv6;
15826 	int		err = 0;
15827 	boolean_t	success;
15828 	uint_t		ipif_orig_id;
15829 	ip_stack_t	*ipst = ill->ill_ipst;
15830 
15831 	ASSERT(IAM_WRITER_IPIF(ipif));
15832 
15833 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15834 
15835 	/* Shouldn't get here if it is already up. */
15836 	if (ipif->ipif_flags & IPIF_UP)
15837 		return (EALREADY);
15838 
15839 	/*
15840 	 * If this is a request to bring up a data address on an interface
15841 	 * under IPMP, then move the address to its IPMP meta-interface and
15842 	 * try to bring it up.  One complication is that the zeroth ipif for
15843 	 * an ill is special, in that every ill always has one, and that code
15844 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15845 	 */
15846 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15847 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15848 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15849 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15850 
15851 		/*
15852 		 * The ipif being brought up should be quiesced.  If it's not,
15853 		 * something has gone amiss and we need to bail out.  (If it's
15854 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15855 		 */
15856 		mutex_enter(&ill->ill_lock);
15857 		if (!ipif_is_quiescent(ipif)) {
15858 			mutex_exit(&ill->ill_lock);
15859 			return (EINVAL);
15860 		}
15861 		mutex_exit(&ill->ill_lock);
15862 
15863 		/*
15864 		 * If we're going to need to allocate ipifs, do it prior
15865 		 * to starting the move (and grabbing locks).
15866 		 */
15867 		if (ipif->ipif_id == 0) {
15868 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15869 			    B_FALSE);
15870 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15871 			    B_FALSE);
15872 			if (moveipif == NULL || stubipif == NULL) {
15873 				mi_free(moveipif);
15874 				mi_free(stubipif);
15875 				return (ENOMEM);
15876 			}
15877 		}
15878 
15879 		/*
15880 		 * Grab or transfer the ipif to move.  During the move, keep
15881 		 * ill_g_lock held to prevent any ill walker threads from
15882 		 * seeing things in an inconsistent state.
15883 		 */
15884 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15885 		if (ipif->ipif_id != 0) {
15886 			ipif_remove(ipif);
15887 		} else {
15888 			ipif_transfer(ipif, moveipif, stubipif);
15889 			ipif = moveipif;
15890 		}
15891 
15892 		/*
15893 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
15894 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
15895 		 * replace that one.  Otherwise, pick the next available slot.
15896 		 */
15897 		ipif->ipif_ill = ipmp_ill;
15898 		ipif_orig_id = ipif->ipif_id;
15899 
15900 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
15901 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
15902 			ipif = ipmp_ill->ill_ipif;
15903 		} else {
15904 			ipif->ipif_id = -1;
15905 			if (ipif_insert(ipif, B_FALSE) != 0) {
15906 				/*
15907 				 * No more available ipif_id's -- put it back
15908 				 * on the original ill and fail the operation.
15909 				 * Since we're writer on the ill, we can be
15910 				 * sure our old slot is still available.
15911 				 */
15912 				ipif->ipif_id = ipif_orig_id;
15913 				ipif->ipif_ill = ill;
15914 				if (ipif_orig_id == 0) {
15915 					ipif_transfer(ipif, ill->ill_ipif,
15916 					    NULL);
15917 				} else {
15918 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
15919 				}
15920 				rw_exit(&ipst->ips_ill_g_lock);
15921 				return (ENOMEM);
15922 			}
15923 		}
15924 		rw_exit(&ipst->ips_ill_g_lock);
15925 
15926 		/*
15927 		 * Tell SCTP that the ipif has moved.  Note that even if we
15928 		 * had to allocate a new ipif, the original sequence id was
15929 		 * preserved and therefore SCTP won't know.
15930 		 */
15931 		sctp_move_ipif(ipif, ill, ipmp_ill);
15932 
15933 		/*
15934 		 * If the ipif being brought up was on slot zero, then we
15935 		 * first need to bring up the placeholder we stuck there.  In
15936 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
15937 		 * to ipif_up() itself, if we successfully bring up the
15938 		 * placeholder, we'll check ill_move_ipif and bring it up too.
15939 		 */
15940 		if (ipif_orig_id == 0) {
15941 			ASSERT(ill->ill_move_ipif == NULL);
15942 			ill->ill_move_ipif = ipif;
15943 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
15944 				ASSERT(ill->ill_move_ipif == NULL);
15945 			if (err != EINPROGRESS)
15946 				ill->ill_move_ipif = NULL;
15947 			return (err);
15948 		}
15949 
15950 		/*
15951 		 * Bring it up on the IPMP ill.
15952 		 */
15953 		return (ipif_up(ipif, q, mp));
15954 	}
15955 
15956 	/* Skip arp/ndp for any loopback interface. */
15957 	if (ill->ill_wq != NULL) {
15958 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
15959 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
15960 
15961 		if (!ill->ill_dl_up) {
15962 			/*
15963 			 * ill_dl_up is not yet set. i.e. we are yet to
15964 			 * DL_BIND with the driver and this is the first
15965 			 * logical interface on the ill to become "up".
15966 			 * Tell the driver to get going (via DL_BIND_REQ).
15967 			 * Note that changing "significant" IFF_ flags
15968 			 * address/netmask etc cause a down/up dance, but
15969 			 * does not cause an unbind (DL_UNBIND) with the driver
15970 			 */
15971 			return (ill_dl_up(ill, ipif, mp, q));
15972 		}
15973 
15974 		/*
15975 		 * ipif_resolver_up may end up sending an
15976 		 * AR_INTERFACE_UP message to ARP, which would, in
15977 		 * turn send a DLPI message to the driver. ioctls are
15978 		 * serialized and so we cannot send more than one
15979 		 * interface up message at a time. If ipif_resolver_up
15980 		 * does send an interface up message to ARP, we get
15981 		 * EINPROGRESS and we will complete in ip_arp_done.
15982 		 */
15983 
15984 		ASSERT(connp != NULL || !CONN_Q(q));
15985 		if (connp != NULL)
15986 			mutex_enter(&connp->conn_lock);
15987 		mutex_enter(&ill->ill_lock);
15988 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
15989 		mutex_exit(&ill->ill_lock);
15990 		if (connp != NULL)
15991 			mutex_exit(&connp->conn_lock);
15992 		if (!success)
15993 			return (EINTR);
15994 
15995 		/*
15996 		 * Crank up the resolver.  For IPv6, this cranks up the
15997 		 * external resolver if one is configured, but even if an
15998 		 * external resolver isn't configured, it must be called to
15999 		 * reset DAD state.  For IPv6, if an external resolver is not
16000 		 * being used, ipif_resolver_up() will never return
16001 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
16002 		 * Note that if an external resolver is being used, there's no
16003 		 * need to call ipif_ndp_up() since it will do nothing.
16004 		 */
16005 		err = ipif_resolver_up(ipif, Res_act_initial);
16006 		if (err == EINPROGRESS) {
16007 			/* We will complete it in ip_arp_done() */
16008 			return (err);
16009 		}
16010 
16011 		if (isv6 && err == 0)
16012 			err = ipif_ndp_up(ipif, B_TRUE);
16013 
16014 		ASSERT(err != EINPROGRESS);
16015 		mp = ipsq_pending_mp_get(ipsq, &connp);
16016 		ASSERT(mp != NULL);
16017 		if (err != 0)
16018 			return (err);
16019 	} else {
16020 		/*
16021 		 * Interfaces without underlying hardware don't do duplicate
16022 		 * address detection.
16023 		 */
16024 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16025 		ipif->ipif_addr_ready = 1;
16026 	}
16027 
16028 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16029 	if (err == 0 && ill->ill_move_ipif != NULL) {
16030 		ipif = ill->ill_move_ipif;
16031 		ill->ill_move_ipif = NULL;
16032 		return (ipif_up(ipif, q, mp));
16033 	}
16034 	return (err);
16035 }
16036 
16037 /*
16038  * Perform a bind for the physical device.
16039  * When the routine returns EINPROGRESS then mp has been consumed and
16040  * the ioctl will be acked from ip_rput_dlpi.
16041  * Allocate an unbind message and save it until ipif_down.
16042  */
16043 static int
16044 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16045 {
16046 	areq_t	*areq;
16047 	mblk_t	*areq_mp = NULL;
16048 	mblk_t	*bind_mp = NULL;
16049 	mblk_t	*unbind_mp = NULL;
16050 	conn_t	*connp;
16051 	boolean_t success;
16052 	uint16_t sap_addr;
16053 
16054 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16055 	ASSERT(IAM_WRITER_ILL(ill));
16056 	ASSERT(mp != NULL);
16057 
16058 	/* Create a resolver cookie for ARP */
16059 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16060 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16061 		if (areq_mp == NULL)
16062 			return (ENOMEM);
16063 
16064 		freemsg(ill->ill_resolver_mp);
16065 		ill->ill_resolver_mp = areq_mp;
16066 		areq = (areq_t *)areq_mp->b_rptr;
16067 		sap_addr = ill->ill_sap;
16068 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16069 	}
16070 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16071 	    DL_BIND_REQ);
16072 	if (bind_mp == NULL)
16073 		goto bad;
16074 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16075 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16076 
16077 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16078 	if (unbind_mp == NULL)
16079 		goto bad;
16080 
16081 	/*
16082 	 * Record state needed to complete this operation when the
16083 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16084 	 */
16085 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16086 	ASSERT(connp != NULL || !CONN_Q(q));
16087 	GRAB_CONN_LOCK(q);
16088 	mutex_enter(&ipif->ipif_ill->ill_lock);
16089 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16090 	mutex_exit(&ipif->ipif_ill->ill_lock);
16091 	RELEASE_CONN_LOCK(q);
16092 	if (!success)
16093 		goto bad;
16094 
16095 	/*
16096 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16097 	 * the interface goes down.
16098 	 */
16099 	ASSERT(ill->ill_unbind_mp == NULL);
16100 	ill->ill_unbind_mp = unbind_mp;
16101 
16102 	ill_dlpi_send(ill, bind_mp);
16103 	/* Send down link-layer capabilities probe if not already done. */
16104 	ill_capability_probe(ill);
16105 
16106 	/*
16107 	 * Sysid used to rely on the fact that netboots set domainname
16108 	 * and the like. Now that miniroot boots aren't strictly netboots
16109 	 * and miniroot network configuration is driven from userland
16110 	 * these things still need to be set. This situation can be detected
16111 	 * by comparing the interface being configured here to the one
16112 	 * dhcifname was set to reference by the boot loader. Once sysid is
16113 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16114 	 */
16115 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16116 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16117 	    (strlen(srpc_domain) == 0)) {
16118 		if (dhcpinit() != 0)
16119 			cmn_err(CE_WARN, "no cached dhcp response");
16120 	}
16121 
16122 	/*
16123 	 * This operation will complete in ip_rput_dlpi with either
16124 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16125 	 */
16126 	return (EINPROGRESS);
16127 bad:
16128 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16129 
16130 	freemsg(bind_mp);
16131 	freemsg(unbind_mp);
16132 	return (ENOMEM);
16133 }
16134 
16135 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16136 
16137 /*
16138  * DLPI and ARP is up.
16139  * Create all the IREs associated with an interface bring up multicast.
16140  * Set the interface flag and finish other initialization
16141  * that potentially had to be differed to after DL_BIND_ACK.
16142  */
16143 int
16144 ipif_up_done(ipif_t *ipif)
16145 {
16146 	ire_t	*ire_array[20];
16147 	ire_t	**irep = ire_array;
16148 	ire_t	**irep1;
16149 	ipaddr_t net_mask = 0;
16150 	ipaddr_t subnet_mask, route_mask;
16151 	ill_t	*ill = ipif->ipif_ill;
16152 	queue_t	*stq;
16153 	ipif_t	 *src_ipif;
16154 	ipif_t   *tmp_ipif;
16155 	boolean_t	flush_ire_cache = B_TRUE;
16156 	int	err = 0;
16157 	ire_t	**ipif_saved_irep = NULL;
16158 	int ipif_saved_ire_cnt;
16159 	int	cnt;
16160 	boolean_t	src_ipif_held = B_FALSE;
16161 	boolean_t	loopback = B_FALSE;
16162 	ip_stack_t	*ipst = ill->ill_ipst;
16163 
16164 	ip1dbg(("ipif_up_done(%s:%u)\n",
16165 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16166 	/* Check if this is a loopback interface */
16167 	if (ipif->ipif_ill->ill_wq == NULL)
16168 		loopback = B_TRUE;
16169 
16170 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16171 	/*
16172 	 * If all other interfaces for this ill are down or DEPRECATED,
16173 	 * or otherwise unsuitable for source address selection, remove
16174 	 * any IRE_CACHE entries for this ill to make sure source
16175 	 * address selection gets to take this new ipif into account.
16176 	 * No need to hold ill_lock while traversing the ipif list since
16177 	 * we are writer
16178 	 */
16179 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16180 	    tmp_ipif = tmp_ipif->ipif_next) {
16181 		if (((tmp_ipif->ipif_flags &
16182 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16183 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16184 		    (tmp_ipif == ipif))
16185 			continue;
16186 		/* first useable pre-existing interface */
16187 		flush_ire_cache = B_FALSE;
16188 		break;
16189 	}
16190 	if (flush_ire_cache)
16191 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16192 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16193 
16194 	/*
16195 	 * Figure out which way the send-to queue should go.  Only
16196 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16197 	 * should show up here.
16198 	 */
16199 	switch (ill->ill_net_type) {
16200 	case IRE_IF_RESOLVER:
16201 		stq = ill->ill_rq;
16202 		break;
16203 	case IRE_IF_NORESOLVER:
16204 	case IRE_LOOPBACK:
16205 		stq = ill->ill_wq;
16206 		break;
16207 	default:
16208 		return (EINVAL);
16209 	}
16210 
16211 	if (IS_LOOPBACK(ill)) {
16212 		/*
16213 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16214 		 * ipif_lookup_on_name(), but in the case of zones we can have
16215 		 * several loopback addresses on lo0. So all the interfaces with
16216 		 * loopback addresses need to be marked IRE_LOOPBACK.
16217 		 */
16218 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16219 		    htonl(INADDR_LOOPBACK))
16220 			ipif->ipif_ire_type = IRE_LOOPBACK;
16221 		else
16222 			ipif->ipif_ire_type = IRE_LOCAL;
16223 	}
16224 
16225 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16226 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16227 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16228 		/*
16229 		 * Can't use our source address. Select a different
16230 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16231 		 */
16232 		src_ipif = ipif_select_source(ipif->ipif_ill,
16233 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16234 		if (src_ipif == NULL)
16235 			src_ipif = ipif;	/* Last resort */
16236 		else
16237 			src_ipif_held = B_TRUE;
16238 	} else {
16239 		src_ipif = ipif;
16240 	}
16241 
16242 	/* Create all the IREs associated with this interface */
16243 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16244 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16245 
16246 		/*
16247 		 * If we're on a labeled system then make sure that zone-
16248 		 * private addresses have proper remote host database entries.
16249 		 */
16250 		if (is_system_labeled() &&
16251 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16252 		    !tsol_check_interface_address(ipif))
16253 			return (EINVAL);
16254 
16255 		/* Register the source address for __sin6_src_id */
16256 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16257 		    ipif->ipif_zoneid, ipst);
16258 		if (err != 0) {
16259 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16260 			return (err);
16261 		}
16262 
16263 		/* If the interface address is set, create the local IRE. */
16264 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16265 		    (void *)ipif,
16266 		    ipif->ipif_ire_type,
16267 		    ntohl(ipif->ipif_lcl_addr)));
16268 		*irep++ = ire_create(
16269 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16270 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16271 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16272 		    NULL,				/* no gateway */
16273 		    &ip_loopback_mtuplus,		/* max frag size */
16274 		    NULL,
16275 		    ipif->ipif_rq,			/* recv-from queue */
16276 		    NULL,				/* no send-to queue */
16277 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16278 		    ipif,
16279 		    0,
16280 		    0,
16281 		    0,
16282 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16283 		    RTF_PRIVATE : 0,
16284 		    &ire_uinfo_null,
16285 		    NULL,
16286 		    NULL,
16287 		    ipst);
16288 	} else {
16289 		ip1dbg((
16290 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16291 		    ipif->ipif_ire_type,
16292 		    ntohl(ipif->ipif_lcl_addr),
16293 		    (uint_t)ipif->ipif_flags));
16294 	}
16295 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16296 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16297 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16298 	} else {
16299 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16300 	}
16301 
16302 	subnet_mask = ipif->ipif_net_mask;
16303 
16304 	/*
16305 	 * If mask was not specified, use natural netmask of
16306 	 * interface address. Also, store this mask back into the
16307 	 * ipif struct.
16308 	 */
16309 	if (subnet_mask == 0) {
16310 		subnet_mask = net_mask;
16311 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16312 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16313 		    ipif->ipif_v6subnet);
16314 	}
16315 
16316 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16317 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16318 	    ipif->ipif_subnet != INADDR_ANY) {
16319 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16320 
16321 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16322 			route_mask = IP_HOST_MASK;
16323 		} else {
16324 			route_mask = subnet_mask;
16325 		}
16326 
16327 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16328 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16329 		    (void *)ipif, (void *)ill,
16330 		    ill->ill_net_type,
16331 		    ntohl(ipif->ipif_subnet)));
16332 		*irep++ = ire_create(
16333 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16334 		    (uchar_t *)&route_mask,		/* mask */
16335 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16336 		    NULL,				/* no gateway */
16337 		    &ipif->ipif_mtu,			/* max frag */
16338 		    NULL,
16339 		    NULL,				/* no recv queue */
16340 		    stq,				/* send-to queue */
16341 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16342 		    ipif,
16343 		    0,
16344 		    0,
16345 		    0,
16346 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16347 		    &ire_uinfo_null,
16348 		    NULL,
16349 		    NULL,
16350 		    ipst);
16351 	}
16352 
16353 	/*
16354 	 * Create any necessary broadcast IREs.
16355 	 */
16356 	if (ipif->ipif_flags & IPIF_BROADCAST)
16357 		irep = ipif_create_bcast_ires(ipif, irep);
16358 
16359 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16360 
16361 	/* If an earlier ire_create failed, get out now */
16362 	for (irep1 = irep; irep1 > ire_array; ) {
16363 		irep1--;
16364 		if (*irep1 == NULL) {
16365 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16366 			err = ENOMEM;
16367 			goto bad;
16368 		}
16369 	}
16370 
16371 	/*
16372 	 * Need to atomically check for IP address availability under
16373 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16374 	 * ills or new ipifs can be added while we are checking availability.
16375 	 */
16376 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16377 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16378 	/* Mark it up, and increment counters. */
16379 	ipif->ipif_flags |= IPIF_UP;
16380 	ill->ill_ipif_up_count++;
16381 	err = ip_addr_availability_check(ipif);
16382 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16383 	rw_exit(&ipst->ips_ill_g_lock);
16384 
16385 	if (err != 0) {
16386 		/*
16387 		 * Our address may already be up on the same ill. In this case,
16388 		 * the ARP entry for our ipif replaced the one for the other
16389 		 * ipif. So we don't want to delete it (otherwise the other ipif
16390 		 * would be unable to send packets).
16391 		 * ip_addr_availability_check() identifies this case for us and
16392 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16393 		 * which is the expected error code.
16394 		 */
16395 		if (err == EADDRINUSE) {
16396 			freemsg(ipif->ipif_arp_del_mp);
16397 			ipif->ipif_arp_del_mp = NULL;
16398 			err = EADDRNOTAVAIL;
16399 		}
16400 		ill->ill_ipif_up_count--;
16401 		ipif->ipif_flags &= ~IPIF_UP;
16402 		goto bad;
16403 	}
16404 
16405 	/*
16406 	 * Add in all newly created IREs.  ire_create_bcast() has
16407 	 * already checked for duplicates of the IRE_BROADCAST type.
16408 	 */
16409 	for (irep1 = irep; irep1 > ire_array; ) {
16410 		irep1--;
16411 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16412 		/*
16413 		 * refheld by ire_add. refele towards the end of the func
16414 		 */
16415 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16416 	}
16417 
16418 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16419 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16420 	ipif_saved_irep = ipif_recover_ire(ipif);
16421 
16422 	if (!loopback) {
16423 		/*
16424 		 * If the broadcast address has been set, make sure it makes
16425 		 * sense based on the interface address.
16426 		 * Only match on ill since we are sharing broadcast addresses.
16427 		 */
16428 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16429 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16430 			ire_t	*ire;
16431 
16432 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16433 			    IRE_BROADCAST, ipif, ALL_ZONES,
16434 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16435 
16436 			if (ire == NULL) {
16437 				/*
16438 				 * If there isn't a matching broadcast IRE,
16439 				 * revert to the default for this netmask.
16440 				 */
16441 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16442 				mutex_enter(&ipif->ipif_ill->ill_lock);
16443 				ipif_set_default(ipif);
16444 				mutex_exit(&ipif->ipif_ill->ill_lock);
16445 			} else {
16446 				ire_refrele(ire);
16447 			}
16448 		}
16449 
16450 	}
16451 
16452 	if (ill->ill_need_recover_multicast) {
16453 		/*
16454 		 * Need to recover all multicast memberships in the driver.
16455 		 * This had to be deferred until we had attached.  The same
16456 		 * code exists in ipif_up_done_v6() to recover IPv6
16457 		 * memberships.
16458 		 *
16459 		 * Note that it would be preferable to unconditionally do the
16460 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16461 		 * that since ill_join_allmulti() depends on ill_dl_up being
16462 		 * set, and it is not set until we receive a DL_BIND_ACK after
16463 		 * having called ill_dl_up().
16464 		 */
16465 		ill_recover_multicast(ill);
16466 	}
16467 
16468 	if (ill->ill_ipif_up_count == 1) {
16469 		/*
16470 		 * Since the interface is now up, it may now be active.
16471 		 */
16472 		if (IS_UNDER_IPMP(ill))
16473 			ipmp_ill_refresh_active(ill);
16474 
16475 		/*
16476 		 * If this is an IPMP interface, we may now be able to
16477 		 * establish ARP entries.
16478 		 */
16479 		if (IS_IPMP(ill))
16480 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16481 	}
16482 
16483 	/* Join the allhosts multicast address */
16484 	ipif_multicast_up(ipif);
16485 
16486 	/*
16487 	 * See if anybody else would benefit from our new ipif.
16488 	 */
16489 	if (!loopback &&
16490 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16491 		ill_update_source_selection(ill);
16492 	}
16493 
16494 	for (irep1 = irep; irep1 > ire_array; ) {
16495 		irep1--;
16496 		if (*irep1 != NULL) {
16497 			/* was held in ire_add */
16498 			ire_refrele(*irep1);
16499 		}
16500 	}
16501 
16502 	cnt = ipif_saved_ire_cnt;
16503 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16504 		if (*irep1 != NULL) {
16505 			/* was held in ire_add */
16506 			ire_refrele(*irep1);
16507 		}
16508 	}
16509 
16510 	if (!loopback && ipif->ipif_addr_ready) {
16511 		/* Broadcast an address mask reply. */
16512 		ipif_mask_reply(ipif);
16513 	}
16514 	if (ipif_saved_irep != NULL) {
16515 		kmem_free(ipif_saved_irep,
16516 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16517 	}
16518 	if (src_ipif_held)
16519 		ipif_refrele(src_ipif);
16520 
16521 	/*
16522 	 * This had to be deferred until we had bound.  Tell routing sockets and
16523 	 * others that this interface is up if it looks like the address has
16524 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16525 	 * duplicate address detection to do its thing.
16526 	 */
16527 	if (ipif->ipif_addr_ready)
16528 		ipif_up_notify(ipif);
16529 	return (0);
16530 
16531 bad:
16532 	ip1dbg(("ipif_up_done: FAILED \n"));
16533 
16534 	while (irep > ire_array) {
16535 		irep--;
16536 		if (*irep != NULL)
16537 			ire_delete(*irep);
16538 	}
16539 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16540 
16541 	if (ipif_saved_irep != NULL) {
16542 		kmem_free(ipif_saved_irep,
16543 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16544 	}
16545 	if (src_ipif_held)
16546 		ipif_refrele(src_ipif);
16547 
16548 	ipif_resolver_down(ipif);
16549 	return (err);
16550 }
16551 
16552 /*
16553  * Turn off the ARP with the ILLF_NOARP flag.
16554  */
16555 static int
16556 ill_arp_off(ill_t *ill)
16557 {
16558 	mblk_t	*arp_off_mp = NULL;
16559 	mblk_t	*arp_on_mp = NULL;
16560 
16561 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16562 
16563 	ASSERT(IAM_WRITER_ILL(ill));
16564 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16565 
16566 	/*
16567 	 * If the on message is still around we've already done
16568 	 * an arp_off without doing an arp_on thus there is no
16569 	 * work needed.
16570 	 */
16571 	if (ill->ill_arp_on_mp != NULL)
16572 		return (0);
16573 
16574 	/*
16575 	 * Allocate an ARP on message (to be saved) and an ARP off message
16576 	 */
16577 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16578 	if (!arp_off_mp)
16579 		return (ENOMEM);
16580 
16581 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16582 	if (!arp_on_mp)
16583 		goto failed;
16584 
16585 	ASSERT(ill->ill_arp_on_mp == NULL);
16586 	ill->ill_arp_on_mp = arp_on_mp;
16587 
16588 	/* Send an AR_INTERFACE_OFF request */
16589 	putnext(ill->ill_rq, arp_off_mp);
16590 	return (0);
16591 failed:
16592 
16593 	if (arp_off_mp)
16594 		freemsg(arp_off_mp);
16595 	return (ENOMEM);
16596 }
16597 
16598 /*
16599  * Turn on ARP by turning off the ILLF_NOARP flag.
16600  */
16601 static int
16602 ill_arp_on(ill_t *ill)
16603 {
16604 	mblk_t	*mp;
16605 
16606 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16607 
16608 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16609 
16610 	ASSERT(IAM_WRITER_ILL(ill));
16611 	/*
16612 	 * Send an AR_INTERFACE_ON request if we have already done
16613 	 * an arp_off (which allocated the message).
16614 	 */
16615 	if (ill->ill_arp_on_mp != NULL) {
16616 		mp = ill->ill_arp_on_mp;
16617 		ill->ill_arp_on_mp = NULL;
16618 		putnext(ill->ill_rq, mp);
16619 	}
16620 	return (0);
16621 }
16622 
16623 /*
16624  * Checks for availbility of a usable source address (if there is one) when the
16625  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16626  * this selection is done regardless of the destination.
16627  */
16628 boolean_t
16629 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16630 {
16631 	uint_t	ifindex;
16632 	ipif_t	*ipif = NULL;
16633 	ill_t	*uill;
16634 	boolean_t isv6;
16635 	ip_stack_t	*ipst = ill->ill_ipst;
16636 
16637 	ASSERT(ill != NULL);
16638 
16639 	isv6 = ill->ill_isv6;
16640 	ifindex = ill->ill_usesrc_ifindex;
16641 	if (ifindex != 0) {
16642 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16643 		    NULL, ipst);
16644 		if (uill == NULL)
16645 			return (NULL);
16646 		mutex_enter(&uill->ill_lock);
16647 		for (ipif = uill->ill_ipif; ipif != NULL;
16648 		    ipif = ipif->ipif_next) {
16649 			if (!IPIF_CAN_LOOKUP(ipif))
16650 				continue;
16651 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16652 				continue;
16653 			if (!(ipif->ipif_flags & IPIF_UP))
16654 				continue;
16655 			if (ipif->ipif_zoneid != zoneid)
16656 				continue;
16657 			if ((isv6 &&
16658 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16659 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16660 				continue;
16661 			mutex_exit(&uill->ill_lock);
16662 			ill_refrele(uill);
16663 			return (B_TRUE);
16664 		}
16665 		mutex_exit(&uill->ill_lock);
16666 		ill_refrele(uill);
16667 	}
16668 	return (B_FALSE);
16669 }
16670 
16671 /*
16672  * IP source address type, sorted from worst to best.  For a given type,
16673  * always prefer IP addresses on the same subnet.  All-zones addresses are
16674  * suboptimal because they pose problems with unlabeled destinations.
16675  */
16676 typedef enum {
16677 	IPIF_NONE,
16678 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16679 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16680 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16681 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16682 	IPIF_DIFFNET,			/* normal and different subnet */
16683 	IPIF_SAMENET			/* normal and same subnet */
16684 } ipif_type_t;
16685 
16686 /*
16687  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16688  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16689  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16690  * the first one, unless IPMP is used in which case we round-robin among them;
16691  * see below for more.
16692  *
16693  * Returns NULL if there is no suitable source address for the ill.
16694  * This only occurs when there is no valid source address for the ill.
16695  */
16696 ipif_t *
16697 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16698 {
16699 	ill_t	*usill = NULL;
16700 	ill_t	*ipmp_ill = NULL;
16701 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16702 	ipif_type_t type, best_type;
16703 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16704 	ip_stack_t *ipst = ill->ill_ipst;
16705 	boolean_t samenet;
16706 
16707 	if (ill->ill_usesrc_ifindex != 0) {
16708 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16709 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16710 		if (usill != NULL)
16711 			ill = usill;	/* Select source from usesrc ILL */
16712 		else
16713 			return (NULL);
16714 	}
16715 
16716 	/*
16717 	 * Test addresses should never be used for source address selection,
16718 	 * so if we were passed one, switch to the IPMP meta-interface.
16719 	 */
16720 	if (IS_UNDER_IPMP(ill)) {
16721 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16722 			ill = ipmp_ill;	/* Select source from IPMP ill */
16723 		else
16724 			return (NULL);
16725 	}
16726 
16727 	/*
16728 	 * If we're dealing with an unlabeled destination on a labeled system,
16729 	 * make sure that we ignore source addresses that are incompatible with
16730 	 * the destination's default label.  That destination's default label
16731 	 * must dominate the minimum label on the source address.
16732 	 */
16733 	dst_rhtp = NULL;
16734 	if (is_system_labeled()) {
16735 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16736 		if (dst_rhtp == NULL)
16737 			return (NULL);
16738 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16739 			TPC_RELE(dst_rhtp);
16740 			dst_rhtp = NULL;
16741 		}
16742 	}
16743 
16744 	/*
16745 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16746 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16747 	 * After selecting the right ipif, under ill_lock make sure ipif is
16748 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16749 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16750 	 * but not under a lock.
16751 	 */
16752 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16753 retry:
16754 	/*
16755 	 * For source address selection, we treat the ipif list as circular
16756 	 * and continue until we get back to where we started.  This allows
16757 	 * IPMP to vary source address selection (which improves inbound load
16758 	 * spreading) by caching its last ending point and starting from
16759 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16760 	 * ills since that can't happen on the IPMP ill.
16761 	 */
16762 	start_ipif = ill->ill_ipif;
16763 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16764 		start_ipif = ill->ill_src_ipif;
16765 
16766 	ipif = start_ipif;
16767 	best_ipif = NULL;
16768 	best_type = IPIF_NONE;
16769 	do {
16770 		if ((next_ipif = ipif->ipif_next) == NULL)
16771 			next_ipif = ill->ill_ipif;
16772 
16773 		if (!IPIF_CAN_LOOKUP(ipif))
16774 			continue;
16775 		/* Always skip NOLOCAL and ANYCAST interfaces */
16776 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16777 			continue;
16778 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16779 			continue;
16780 		if (ipif->ipif_zoneid != zoneid &&
16781 		    ipif->ipif_zoneid != ALL_ZONES)
16782 			continue;
16783 
16784 		/*
16785 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16786 		 * are not valid as source addresses.
16787 		 */
16788 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16789 			continue;
16790 
16791 		/*
16792 		 * Check compatibility of local address for destination's
16793 		 * default label if we're on a labeled system.	Incompatible
16794 		 * addresses can't be used at all.
16795 		 */
16796 		if (dst_rhtp != NULL) {
16797 			boolean_t incompat;
16798 
16799 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16800 			    IPV4_VERSION, B_FALSE);
16801 			if (src_rhtp == NULL)
16802 				continue;
16803 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16804 			    src_rhtp->tpc_tp.tp_doi !=
16805 			    dst_rhtp->tpc_tp.tp_doi ||
16806 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16807 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16808 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16809 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16810 			TPC_RELE(src_rhtp);
16811 			if (incompat)
16812 				continue;
16813 		}
16814 
16815 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16816 
16817 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16818 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16819 			    IPIF_DIFFNET_DEPRECATED;
16820 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16821 			type = samenet ? IPIF_SAMENET_ALLZONES :
16822 			    IPIF_DIFFNET_ALLZONES;
16823 		} else {
16824 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16825 		}
16826 
16827 		if (type > best_type) {
16828 			best_type = type;
16829 			best_ipif = ipif;
16830 			if (best_type == IPIF_SAMENET)
16831 				break; /* can't get better */
16832 		}
16833 	} while ((ipif = next_ipif) != start_ipif);
16834 
16835 	if ((ipif = best_ipif) != NULL) {
16836 		mutex_enter(&ipif->ipif_ill->ill_lock);
16837 		if (!IPIF_CAN_LOOKUP(ipif)) {
16838 			mutex_exit(&ipif->ipif_ill->ill_lock);
16839 			goto retry;
16840 		}
16841 		ipif_refhold_locked(ipif);
16842 
16843 		/*
16844 		 * For IPMP, update the source ipif rotor to the next ipif,
16845 		 * provided we can look it up.  (We must not use it if it's
16846 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16847 		 * ipif_free() checked ill_src_ipif.)
16848 		 */
16849 		if (IS_IPMP(ill) && ipif != NULL) {
16850 			next_ipif = ipif->ipif_next;
16851 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16852 				ill->ill_src_ipif = next_ipif;
16853 			else
16854 				ill->ill_src_ipif = NULL;
16855 		}
16856 		mutex_exit(&ipif->ipif_ill->ill_lock);
16857 	}
16858 
16859 	rw_exit(&ipst->ips_ill_g_lock);
16860 	if (usill != NULL)
16861 		ill_refrele(usill);
16862 	if (ipmp_ill != NULL)
16863 		ill_refrele(ipmp_ill);
16864 	if (dst_rhtp != NULL)
16865 		TPC_RELE(dst_rhtp);
16866 
16867 #ifdef DEBUG
16868 	if (ipif == NULL) {
16869 		char buf1[INET6_ADDRSTRLEN];
16870 
16871 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
16872 		    ill->ill_name,
16873 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
16874 	} else {
16875 		char buf1[INET6_ADDRSTRLEN];
16876 		char buf2[INET6_ADDRSTRLEN];
16877 
16878 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
16879 		    ipif->ipif_ill->ill_name,
16880 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
16881 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
16882 		    buf2, sizeof (buf2))));
16883 	}
16884 #endif /* DEBUG */
16885 	return (ipif);
16886 }
16887 
16888 /*
16889  * If old_ipif is not NULL, see if ipif was derived from old
16890  * ipif and if so, recreate the interface route by re-doing
16891  * source address selection. This happens when ipif_down ->
16892  * ipif_update_other_ipifs calls us.
16893  *
16894  * If old_ipif is NULL, just redo the source address selection
16895  * if needed. This happens when ipif_up_done calls us.
16896  */
16897 static void
16898 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
16899 {
16900 	ire_t *ire;
16901 	ire_t *ipif_ire;
16902 	queue_t *stq;
16903 	ipif_t *nipif;
16904 	ill_t *ill;
16905 	boolean_t need_rele = B_FALSE;
16906 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16907 
16908 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
16909 	ASSERT(IAM_WRITER_IPIF(ipif));
16910 
16911 	ill = ipif->ipif_ill;
16912 	if (!(ipif->ipif_flags &
16913 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16914 		/*
16915 		 * Can't possibly have borrowed the source
16916 		 * from old_ipif.
16917 		 */
16918 		return;
16919 	}
16920 
16921 	/*
16922 	 * Is there any work to be done? No work if the address
16923 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
16924 	 * ipif_select_source() does not borrow addresses from
16925 	 * NOLOCAL and ANYCAST interfaces).
16926 	 */
16927 	if ((old_ipif != NULL) &&
16928 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
16929 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
16930 	    (old_ipif->ipif_flags &
16931 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
16932 		return;
16933 	}
16934 
16935 	/*
16936 	 * Perform the same checks as when creating the
16937 	 * IRE_INTERFACE in ipif_up_done.
16938 	 */
16939 	if (!(ipif->ipif_flags & IPIF_UP))
16940 		return;
16941 
16942 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
16943 	    (ipif->ipif_subnet == INADDR_ANY))
16944 		return;
16945 
16946 	ipif_ire = ipif_to_ire(ipif);
16947 	if (ipif_ire == NULL)
16948 		return;
16949 
16950 	/*
16951 	 * We know that ipif uses some other source for its
16952 	 * IRE_INTERFACE. Is it using the source of this
16953 	 * old_ipif?
16954 	 */
16955 	if (old_ipif != NULL &&
16956 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
16957 		ire_refrele(ipif_ire);
16958 		return;
16959 	}
16960 	if (ip_debug > 2) {
16961 		/* ip1dbg */
16962 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
16963 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
16964 	}
16965 
16966 	stq = ipif_ire->ire_stq;
16967 
16968 	/*
16969 	 * Can't use our source address. Select a different
16970 	 * source address for the IRE_INTERFACE.
16971 	 */
16972 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
16973 	if (nipif == NULL) {
16974 		/* Last resort - all ipif's have IPIF_NOLOCAL */
16975 		nipif = ipif;
16976 	} else {
16977 		need_rele = B_TRUE;
16978 	}
16979 
16980 	ire = ire_create(
16981 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
16982 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
16983 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
16984 	    NULL,				/* no gateway */
16985 	    &ipif->ipif_mtu,			/* max frag */
16986 	    NULL,				/* no src nce */
16987 	    NULL,				/* no recv from queue */
16988 	    stq,				/* send-to queue */
16989 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16990 	    ipif,
16991 	    0,
16992 	    0,
16993 	    0,
16994 	    0,
16995 	    &ire_uinfo_null,
16996 	    NULL,
16997 	    NULL,
16998 	    ipst);
16999 
17000 	if (ire != NULL) {
17001 		ire_t *ret_ire;
17002 		int error;
17003 
17004 		/*
17005 		 * We don't need ipif_ire anymore. We need to delete
17006 		 * before we add so that ire_add does not detect
17007 		 * duplicates.
17008 		 */
17009 		ire_delete(ipif_ire);
17010 		ret_ire = ire;
17011 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
17012 		ASSERT(error == 0);
17013 		ASSERT(ire == ret_ire);
17014 		/* Held in ire_add */
17015 		ire_refrele(ret_ire);
17016 	}
17017 	/*
17018 	 * Either we are falling through from above or could not
17019 	 * allocate a replacement.
17020 	 */
17021 	ire_refrele(ipif_ire);
17022 	if (need_rele)
17023 		ipif_refrele(nipif);
17024 }
17025 
17026 /*
17027  * This old_ipif is going away.
17028  *
17029  * Determine if any other ipif's are using our address as
17030  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17031  * IPIF_DEPRECATED).
17032  * Find the IRE_INTERFACE for such ipifs and recreate them
17033  * to use an different source address following the rules in
17034  * ipif_up_done.
17035  */
17036 static void
17037 ipif_update_other_ipifs(ipif_t *old_ipif)
17038 {
17039 	ipif_t	*ipif;
17040 	ill_t	*ill;
17041 	char	buf[INET6_ADDRSTRLEN];
17042 
17043 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17044 
17045 	ill = old_ipif->ipif_ill;
17046 
17047 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17048 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17049 
17050 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17051 		if (ipif == old_ipif)
17052 			continue;
17053 		ipif_recreate_interface_routes(old_ipif, ipif);
17054 	}
17055 }
17056 
17057 /* ARGSUSED */
17058 int
17059 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17060 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17061 {
17062 	/*
17063 	 * ill_phyint_reinit merged the v4 and v6 into a single
17064 	 * ipsq.  We might not have been able to complete the
17065 	 * operation in ipif_set_values, if we could not become
17066 	 * exclusive.  If so restart it here.
17067 	 */
17068 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17069 }
17070 
17071 /*
17072  * Can operate on either a module or a driver queue.
17073  * Returns an error if not a module queue.
17074  */
17075 /* ARGSUSED */
17076 int
17077 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17078     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17079 {
17080 	queue_t		*q1 = q;
17081 	char 		*cp;
17082 	char		interf_name[LIFNAMSIZ];
17083 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17084 
17085 	if (q->q_next == NULL) {
17086 		ip1dbg((
17087 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17088 		return (EINVAL);
17089 	}
17090 
17091 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17092 		return (EALREADY);
17093 
17094 	do {
17095 		q1 = q1->q_next;
17096 	} while (q1->q_next);
17097 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17098 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17099 
17100 	/*
17101 	 * Here we are not going to delay the ioack until after
17102 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17103 	 * original ioctl message before sending the requests.
17104 	 */
17105 	return (ipif_set_values(q, mp, interf_name, &ppa));
17106 }
17107 
17108 /* ARGSUSED */
17109 int
17110 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17111     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17112 {
17113 	return (ENXIO);
17114 }
17115 
17116 /*
17117  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17118  * minimum (but complete) set exist.  This is necessary when adding or
17119  * removing an interface to/from an IPMP group, since interfaces in an
17120  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17121  * its test address subnets overlap with IPMP data addresses).	It's also
17122  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17123  * interface when the nominated broadcast interface changes.
17124  */
17125 void
17126 ill_refresh_bcast(ill_t *ill)
17127 {
17128 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17129 	ire_t **irep;
17130 	ipif_t *ipif;
17131 
17132 	ASSERT(!ill->ill_isv6);
17133 	ASSERT(IAM_WRITER_ILL(ill));
17134 
17135 	/*
17136 	 * Remove any old broadcast IREs.
17137 	 */
17138 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17139 	    ill_broadcast_delete, ill, ill);
17140 
17141 	/*
17142 	 * Create new ones for any ipifs that are up and broadcast-capable.
17143 	 */
17144 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17145 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17146 		    (IPIF_UP|IPIF_BROADCAST))
17147 			continue;
17148 
17149 		irep = ipif_create_bcast_ires(ipif, ire_array);
17150 		while (irep-- > ire_array) {
17151 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17152 			if (*irep != NULL)
17153 				ire_refrele(*irep);
17154 		}
17155 	}
17156 }
17157 
17158 /*
17159  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17160  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17161  * ire_check_and_create_bcast()).
17162  */
17163 static ire_t **
17164 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17165 {
17166 	ipaddr_t addr;
17167 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17168 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17169 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17170 
17171 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17172 
17173 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17174 
17175 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17176 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17177 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17178 
17179 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17180 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17181 
17182 	/*
17183 	 * For backward compatibility, we create net broadcast IREs based on
17184 	 * the old "IP address class system", since some old machines only
17185 	 * respond to these class derived net broadcast.  However, we must not
17186 	 * create these net broadcast IREs if the subnetmask is shorter than
17187 	 * the IP address class based derived netmask.  Otherwise, we may
17188 	 * create a net broadcast address which is the same as an IP address
17189 	 * on the subnet -- and then TCP will refuse to talk to that address.
17190 	 */
17191 	if (netmask < subnetmask) {
17192 		addr = netmask & ipif->ipif_subnet;
17193 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17194 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17195 		    flags);
17196 	}
17197 
17198 	/*
17199 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17200 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17201 	 * created.  Creating these broadcast IREs will only create confusion
17202 	 * as `addr' will be the same as the IP address.
17203 	 */
17204 	if (subnetmask != 0xFFFFFFFF) {
17205 		addr = ipif->ipif_subnet;
17206 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17207 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17208 		    irep, flags);
17209 	}
17210 
17211 	return (irep);
17212 }
17213 
17214 /*
17215  * Broadcast IRE info structure used in the functions below.  Since we
17216  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17217  */
17218 typedef struct bcast_ireinfo {
17219 	uchar_t		bi_type;	/* BCAST_* value from below */
17220 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17221 			bi_needrep:1,	/* do we need to replace it? */
17222 			bi_haverep:1,	/* have we replaced it? */
17223 			bi_pad:5;
17224 	ipaddr_t	bi_addr;	/* IRE address */
17225 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17226 } bcast_ireinfo_t;
17227 
17228 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17229 
17230 /*
17231  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17232  * return B_TRUE if it should immediately be used to recreate the IRE.
17233  */
17234 static boolean_t
17235 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17236 {
17237 	ipaddr_t addr;
17238 
17239 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17240 
17241 	switch (bireinfop->bi_type) {
17242 	case BCAST_NET:
17243 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17244 		if (addr != bireinfop->bi_addr)
17245 			return (B_FALSE);
17246 		break;
17247 	case BCAST_SUBNET:
17248 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17249 			return (B_FALSE);
17250 		break;
17251 	}
17252 
17253 	bireinfop->bi_needrep = 1;
17254 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17255 		if (bireinfop->bi_backup == NULL)
17256 			bireinfop->bi_backup = ipif;
17257 		return (B_FALSE);
17258 	}
17259 	return (B_TRUE);
17260 }
17261 
17262 /*
17263  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17264  * them ala ire_check_and_create_bcast().
17265  */
17266 static ire_t **
17267 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17268 {
17269 	ipaddr_t mask, addr;
17270 
17271 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17272 
17273 	addr = bireinfop->bi_addr;
17274 	irep = ire_create_bcast(ipif, addr, irep);
17275 
17276 	switch (bireinfop->bi_type) {
17277 	case BCAST_NET:
17278 		mask = ip_net_mask(ipif->ipif_subnet);
17279 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17280 		break;
17281 	case BCAST_SUBNET:
17282 		mask = ipif->ipif_net_mask;
17283 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17284 		break;
17285 	}
17286 
17287 	bireinfop->bi_haverep = 1;
17288 	return (irep);
17289 }
17290 
17291 /*
17292  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17293  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17294  * that are going away are still needed.  If so, have ipif_create_bcast()
17295  * recreate them (except for the deprecated case, as explained below).
17296  */
17297 static ire_t **
17298 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17299     ire_t **irep)
17300 {
17301 	int i;
17302 	ipif_t *ipif;
17303 
17304 	ASSERT(!ill->ill_isv6);
17305 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17306 		/*
17307 		 * Skip this ipif if it's (a) the one being taken down, (b)
17308 		 * not in the same zone, or (c) has no valid local address.
17309 		 */
17310 		if (ipif == test_ipif ||
17311 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17312 		    ipif->ipif_subnet == 0 ||
17313 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17314 		    (IPIF_UP|IPIF_BROADCAST))
17315 			continue;
17316 
17317 		/*
17318 		 * For each dying IRE that hasn't yet been replaced, see if
17319 		 * `ipif' needs it and whether the IRE should be recreated on
17320 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17321 		 * will return B_FALSE even if `ipif' needs the IRE on the
17322 		 * hopes that we'll later find a needy non-deprecated ipif.
17323 		 * However, the ipif is recorded in bi_backup for possible
17324 		 * subsequent use by ipif_check_bcast_ires().
17325 		 */
17326 		for (i = 0; i < BCAST_COUNT; i++) {
17327 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17328 				continue;
17329 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17330 				continue;
17331 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17332 		}
17333 
17334 		/*
17335 		 * If we've replaced all of the broadcast IREs that are going
17336 		 * to be taken down, we know we're done.
17337 		 */
17338 		for (i = 0; i < BCAST_COUNT; i++) {
17339 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17340 				break;
17341 		}
17342 		if (i == BCAST_COUNT)
17343 			break;
17344 	}
17345 	return (irep);
17346 }
17347 
17348 /*
17349  * Check if `test_ipif' (which is going away) is associated with any existing
17350  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17351  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17352  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17353  *
17354  * This is necessary because broadcast IREs are shared.  In particular, a
17355  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17356  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17357  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17358  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17359  * same zone, they will share the same set of broadcast IREs.
17360  *
17361  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17362  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17363  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17364  */
17365 static void
17366 ipif_check_bcast_ires(ipif_t *test_ipif)
17367 {
17368 	ill_t		*ill = test_ipif->ipif_ill;
17369 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17370 	ire_t		**irep1, **irep = &ire_array[0];
17371 	uint_t 		i, willdie;
17372 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17373 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17374 
17375 	ASSERT(!test_ipif->ipif_isv6);
17376 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17377 
17378 	/*
17379 	 * No broadcast IREs for the LOOPBACK interface
17380 	 * or others such as point to point and IPIF_NOXMIT.
17381 	 */
17382 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17383 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17384 		return;
17385 
17386 	bzero(bireinfo, sizeof (bireinfo));
17387 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17388 	bireinfo[0].bi_addr = 0;
17389 
17390 	bireinfo[1].bi_type = BCAST_ALLONES;
17391 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17392 
17393 	bireinfo[2].bi_type = BCAST_NET;
17394 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17395 
17396 	if (test_ipif->ipif_net_mask != 0)
17397 		mask = test_ipif->ipif_net_mask;
17398 	bireinfo[3].bi_type = BCAST_SUBNET;
17399 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17400 
17401 	/*
17402 	 * Figure out what (if any) broadcast IREs will die as a result of
17403 	 * `test_ipif' going away.  If none will die, we're done.
17404 	 */
17405 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17406 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17407 		    test_ipif, ALL_ZONES, NULL,
17408 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17409 		if (ire != NULL) {
17410 			willdie++;
17411 			bireinfo[i].bi_willdie = 1;
17412 			ire_refrele(ire);
17413 		}
17414 	}
17415 
17416 	if (willdie == 0)
17417 		return;
17418 
17419 	/*
17420 	 * Walk through all the ipifs that will be affected by the dying IREs,
17421 	 * and recreate the IREs as necessary. Note that all interfaces in an
17422 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17423 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17424 	 * that broadcast IREs end up on it whenever possible).
17425 	 */
17426 	if (IS_UNDER_IPMP(ill))
17427 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17428 
17429 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17430 
17431 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17432 		ipmp_illgrp_t *illg = ill->ill_grp;
17433 
17434 		ill = list_head(&illg->ig_if);
17435 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17436 			for (i = 0; i < BCAST_COUNT; i++) {
17437 				if (bireinfo[i].bi_willdie &&
17438 				    !bireinfo[i].bi_haverep)
17439 					break;
17440 			}
17441 			if (i == BCAST_COUNT)
17442 				break;
17443 
17444 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17445 		}
17446 	}
17447 
17448 	/*
17449 	 * Scan through the set of broadcast IREs and see if there are any
17450 	 * that we need to replace that have not yet been replaced.  If so,
17451 	 * replace them using the appropriate backup ipif.
17452 	 */
17453 	for (i = 0; i < BCAST_COUNT; i++) {
17454 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17455 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17456 			    &bireinfo[i], irep);
17457 	}
17458 
17459 	/*
17460 	 * If we can't create all of them, don't add any of them.  (Code in
17461 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17462 	 * non-loopback copy and loopback copy for a given address.)
17463 	 */
17464 	for (irep1 = irep; irep1 > ire_array; ) {
17465 		irep1--;
17466 		if (*irep1 == NULL) {
17467 			ip0dbg(("ipif_check_bcast_ires: can't create "
17468 			    "IRE_BROADCAST, memory allocation failure\n"));
17469 			while (irep > ire_array) {
17470 				irep--;
17471 				if (*irep != NULL)
17472 					ire_delete(*irep);
17473 			}
17474 			return;
17475 		}
17476 	}
17477 
17478 	for (irep1 = irep; irep1 > ire_array; ) {
17479 		irep1--;
17480 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17481 			ire_refrele(*irep1);		/* Held in ire_add */
17482 	}
17483 }
17484 
17485 /*
17486  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17487  * from lifr_flags and the name from lifr_name.
17488  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17489  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17490  * Returns EINPROGRESS when mp has been consumed by queueing it on
17491  * ill_pending_mp and the ioctl will complete in ip_rput.
17492  *
17493  * Can operate on either a module or a driver queue.
17494  * Returns an error if not a module queue.
17495  */
17496 /* ARGSUSED */
17497 int
17498 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17499     ip_ioctl_cmd_t *ipip, void *if_req)
17500 {
17501 	ill_t	*ill = q->q_ptr;
17502 	phyint_t *phyi;
17503 	ip_stack_t *ipst;
17504 	struct lifreq *lifr = if_req;
17505 	uint64_t new_flags;
17506 
17507 	ASSERT(ipif != NULL);
17508 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17509 
17510 	if (q->q_next == NULL) {
17511 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17512 		return (EINVAL);
17513 	}
17514 
17515 	/*
17516 	 * If we are not writer on 'q' then this interface exists already
17517 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17518 	 * so return EALREADY.
17519 	 */
17520 	if (ill != ipif->ipif_ill)
17521 		return (EALREADY);
17522 
17523 	if (ill->ill_name[0] != '\0')
17524 		return (EALREADY);
17525 
17526 	/*
17527 	 * If there's another ill already with the requested name, ensure
17528 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17529 	 * fuse together two unrelated ills, which will cause chaos.
17530 	 */
17531 	ipst = ill->ill_ipst;
17532 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17533 	    lifr->lifr_name, NULL);
17534 	if (phyi != NULL) {
17535 		ill_t *ill_mate = phyi->phyint_illv4;
17536 
17537 		if (ill_mate == NULL)
17538 			ill_mate = phyi->phyint_illv6;
17539 		ASSERT(ill_mate != NULL);
17540 
17541 		if (ill_mate->ill_media->ip_m_mac_type !=
17542 		    ill->ill_media->ip_m_mac_type) {
17543 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17544 			    "use the same ill name on differing media\n"));
17545 			return (EINVAL);
17546 		}
17547 	}
17548 
17549 	/*
17550 	 * We start off as IFF_IPV4 in ipif_allocate and become
17551 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
17552 	 * The only flags that we read from user space are IFF_IPV4,
17553 	 * IFF_IPV6, IFF_XRESOLV and IFF_BROADCAST.
17554 	 *
17555 	 * This ill has not been inserted into the global list.
17556 	 * So we are still single threaded and don't need any lock
17557 	 *
17558 	 * Saniy check the flags.
17559 	 */
17560 
17561 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17562 	    ((lifr->lifr_flags & IFF_IPV6) ||
17563 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17564 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
17565 		    "or IPv6 i.e., no broadcast \n"));
17566 		return (EINVAL);
17567 	}
17568 
17569 	new_flags =
17570 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_XRESOLV|IFF_BROADCAST);
17571 
17572 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
17573 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
17574 		    "IFF_IPV4 or IFF_IPV6\n"));
17575 		return (EINVAL);
17576 	}
17577 	/*
17578 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17579 	 */
17580 	if ((new_flags & IFF_XRESOLV) && !(new_flags & IFF_IPV6) &&
17581 	    !(ipif->ipif_isv6)) {
17582 		ip1dbg(("ip_sioctl_slifname: XRESOLV only allowed on "
17583 		    "IPv6 interface\n"));
17584 		return (EINVAL);
17585 	}
17586 
17587 	/*
17588 	 * We always start off as IPv4, so only need to check for IPv6.
17589 	 */
17590 	if ((new_flags & IFF_IPV6) != 0) {
17591 		ill->ill_flags |= ILLF_IPV6;
17592 		ill->ill_flags &= ~ILLF_IPV4;
17593 	}
17594 
17595 	if ((new_flags & IFF_BROADCAST) != 0)
17596 		ipif->ipif_flags |= IPIF_BROADCAST;
17597 	else
17598 		ipif->ipif_flags &= ~IPIF_BROADCAST;
17599 
17600 	if ((new_flags & IFF_XRESOLV) != 0)
17601 		ill->ill_flags |= ILLF_XRESOLV;
17602 	else
17603 		ill->ill_flags &= ~ILLF_XRESOLV;
17604 
17605 	/* We started off as V4. */
17606 	if (ill->ill_flags & ILLF_IPV6) {
17607 		ill->ill_phyint->phyint_illv6 = ill;
17608 		ill->ill_phyint->phyint_illv4 = NULL;
17609 	}
17610 
17611 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17612 }
17613 
17614 /* ARGSUSED */
17615 int
17616 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17617     ip_ioctl_cmd_t *ipip, void *if_req)
17618 {
17619 	/*
17620 	 * ill_phyint_reinit merged the v4 and v6 into a single
17621 	 * ipsq.  We might not have been able to complete the
17622 	 * slifname in ipif_set_values, if we could not become
17623 	 * exclusive.  If so restart it here
17624 	 */
17625 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17626 }
17627 
17628 /*
17629  * Return a pointer to the ipif which matches the index, IP version type and
17630  * zoneid.
17631  */
17632 ipif_t *
17633 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17634     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17635 {
17636 	ill_t	*ill;
17637 	ipif_t	*ipif = NULL;
17638 
17639 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17640 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17641 
17642 	if (err != NULL)
17643 		*err = 0;
17644 
17645 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17646 	if (ill != NULL) {
17647 		mutex_enter(&ill->ill_lock);
17648 		for (ipif = ill->ill_ipif; ipif != NULL;
17649 		    ipif = ipif->ipif_next) {
17650 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17651 			    zoneid == ipif->ipif_zoneid ||
17652 			    ipif->ipif_zoneid == ALL_ZONES)) {
17653 				ipif_refhold_locked(ipif);
17654 				break;
17655 			}
17656 		}
17657 		mutex_exit(&ill->ill_lock);
17658 		ill_refrele(ill);
17659 		if (ipif == NULL && err != NULL)
17660 			*err = ENXIO;
17661 	}
17662 	return (ipif);
17663 }
17664 
17665 /*
17666  * Change an existing physical interface's index. If the new index
17667  * is acceptable we update the index and the phyint_list_avl_by_index tree.
17668  * Finally, we update other systems which may have a dependence on the
17669  * index value.
17670  */
17671 /* ARGSUSED */
17672 int
17673 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17674     ip_ioctl_cmd_t *ipip, void *ifreq)
17675 {
17676 	ill_t		*ill;
17677 	phyint_t	*phyi;
17678 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17679 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17680 	uint_t	old_index, index;
17681 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17682 	avl_index_t	where;
17683 
17684 	if (ipip->ipi_cmd_type == IF_CMD)
17685 		index = ifr->ifr_index;
17686 	else
17687 		index = lifr->lifr_index;
17688 
17689 	/*
17690 	 * Only allow on physical interface. Also, index zero is illegal.
17691 	 */
17692 	ill = ipif->ipif_ill;
17693 	phyi = ill->ill_phyint;
17694 	if (ipif->ipif_id != 0 || index == 0) {
17695 		return (EINVAL);
17696 	}
17697 
17698 	/* If the index is not changing, no work to do */
17699 	if (phyi->phyint_ifindex == index)
17700 		return (0);
17701 
17702 	/*
17703 	 * Use phyint_exists() to determine if the new interface index
17704 	 * is already in use. If the index is unused then we need to
17705 	 * change the phyint's position in the phyint_list_avl_by_index
17706 	 * tree. If we do not do this, subsequent lookups (using the new
17707 	 * index value) will not find the phyint.
17708 	 */
17709 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17710 	if (phyint_exists(index, ipst)) {
17711 		rw_exit(&ipst->ips_ill_g_lock);
17712 		return (EEXIST);
17713 	}
17714 
17715 	/* The new index is unused. Set it in the phyint. */
17716 	old_index = phyi->phyint_ifindex;
17717 	phyi->phyint_ifindex = index;
17718 
17719 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
17720 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17721 	    &index, &where);
17722 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17723 	    phyi, where);
17724 	rw_exit(&ipst->ips_ill_g_lock);
17725 
17726 	/* Update SCTP's ILL list */
17727 	sctp_ill_reindex(ill, old_index);
17728 
17729 	/* Send the routing sockets message */
17730 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17731 	if (ILL_OTHER(ill))
17732 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17733 
17734 	return (0);
17735 }
17736 
17737 /* ARGSUSED */
17738 int
17739 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17740     ip_ioctl_cmd_t *ipip, void *ifreq)
17741 {
17742 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17743 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17744 
17745 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17746 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17747 	/* Get the interface index */
17748 	if (ipip->ipi_cmd_type == IF_CMD) {
17749 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17750 	} else {
17751 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17752 	}
17753 	return (0);
17754 }
17755 
17756 /* ARGSUSED */
17757 int
17758 ip_sioctl_get_lifzone(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 
17763 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17764 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17765 	/* Get the interface zone */
17766 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17767 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17768 	return (0);
17769 }
17770 
17771 /*
17772  * Set the zoneid of an interface.
17773  */
17774 /* ARGSUSED */
17775 int
17776 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17777     ip_ioctl_cmd_t *ipip, void *ifreq)
17778 {
17779 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17780 	int err = 0;
17781 	boolean_t need_up = B_FALSE;
17782 	zone_t *zptr;
17783 	zone_status_t status;
17784 	zoneid_t zoneid;
17785 
17786 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17787 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17788 		if (!is_system_labeled())
17789 			return (ENOTSUP);
17790 		zoneid = GLOBAL_ZONEID;
17791 	}
17792 
17793 	/* cannot assign instance zero to a non-global zone */
17794 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17795 		return (ENOTSUP);
17796 
17797 	/*
17798 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17799 	 * the event of a race with the zone shutdown processing, since IP
17800 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17801 	 * interface will be cleaned up even if the zone is shut down
17802 	 * immediately after the status check. If the interface can't be brought
17803 	 * down right away, and the zone is shut down before the restart
17804 	 * function is called, we resolve the possible races by rechecking the
17805 	 * zone status in the restart function.
17806 	 */
17807 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17808 		return (EINVAL);
17809 	status = zone_status_get(zptr);
17810 	zone_rele(zptr);
17811 
17812 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17813 		return (EINVAL);
17814 
17815 	if (ipif->ipif_flags & IPIF_UP) {
17816 		/*
17817 		 * If the interface is already marked up,
17818 		 * we call ipif_down which will take care
17819 		 * of ditching any IREs that have been set
17820 		 * up based on the old interface address.
17821 		 */
17822 		err = ipif_logical_down(ipif, q, mp);
17823 		if (err == EINPROGRESS)
17824 			return (err);
17825 		ipif_down_tail(ipif);
17826 		need_up = B_TRUE;
17827 	}
17828 
17829 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17830 	return (err);
17831 }
17832 
17833 static int
17834 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17835     queue_t *q, mblk_t *mp, boolean_t need_up)
17836 {
17837 	int	err = 0;
17838 	ip_stack_t	*ipst;
17839 
17840 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17841 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17842 
17843 	if (CONN_Q(q))
17844 		ipst = CONNQ_TO_IPST(q);
17845 	else
17846 		ipst = ILLQ_TO_IPST(q);
17847 
17848 	/*
17849 	 * For exclusive stacks we don't allow a different zoneid than
17850 	 * global.
17851 	 */
17852 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17853 	    zoneid != GLOBAL_ZONEID)
17854 		return (EINVAL);
17855 
17856 	/* Set the new zone id. */
17857 	ipif->ipif_zoneid = zoneid;
17858 
17859 	/* Update sctp list */
17860 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
17861 
17862 	if (need_up) {
17863 		/*
17864 		 * Now bring the interface back up.  If this
17865 		 * is the only IPIF for the ILL, ipif_up
17866 		 * will have to re-bind to the device, so
17867 		 * we may get back EINPROGRESS, in which
17868 		 * case, this IOCTL will get completed in
17869 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
17870 		 */
17871 		err = ipif_up(ipif, q, mp);
17872 	}
17873 	return (err);
17874 }
17875 
17876 /* ARGSUSED */
17877 int
17878 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17879     ip_ioctl_cmd_t *ipip, void *if_req)
17880 {
17881 	struct lifreq *lifr = (struct lifreq *)if_req;
17882 	zoneid_t zoneid;
17883 	zone_t *zptr;
17884 	zone_status_t status;
17885 
17886 	ASSERT(ipif->ipif_id != 0);
17887 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17888 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
17889 		zoneid = GLOBAL_ZONEID;
17890 
17891 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
17892 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17893 
17894 	/*
17895 	 * We recheck the zone status to resolve the following race condition:
17896 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
17897 	 * 2) hme0:1 is up and can't be brought down right away;
17898 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
17899 	 * 3) zone "myzone" is halted; the zone status switches to
17900 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
17901 	 * the interfaces to remove - hme0:1 is not returned because it's not
17902 	 * yet in "myzone", so it won't be removed;
17903 	 * 4) the restart function for SIOCSLIFZONE is called; without the
17904 	 * status check here, we would have hme0:1 in "myzone" after it's been
17905 	 * destroyed.
17906 	 * Note that if the status check fails, we need to bring the interface
17907 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
17908 	 * ipif_up_done[_v6]().
17909 	 */
17910 	status = ZONE_IS_UNINITIALIZED;
17911 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
17912 		status = zone_status_get(zptr);
17913 		zone_rele(zptr);
17914 	}
17915 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
17916 		if (ipif->ipif_isv6) {
17917 			(void) ipif_up_done_v6(ipif);
17918 		} else {
17919 			(void) ipif_up_done(ipif);
17920 		}
17921 		return (EINVAL);
17922 	}
17923 
17924 	ipif_down_tail(ipif);
17925 
17926 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
17927 	    B_TRUE));
17928 }
17929 
17930 /*
17931  * Return the number of addresses on `ill' with one or more of the values
17932  * in `set' set and all of the values in `clear' clear.
17933  */
17934 static uint_t
17935 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
17936 {
17937 	ipif_t	*ipif;
17938 	uint_t	cnt = 0;
17939 
17940 	ASSERT(IAM_WRITER_ILL(ill));
17941 
17942 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
17943 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
17944 			cnt++;
17945 
17946 	return (cnt);
17947 }
17948 
17949 /*
17950  * Return the number of migratable addresses on `ill' that are under
17951  * application control.
17952  */
17953 uint_t
17954 ill_appaddr_cnt(const ill_t *ill)
17955 {
17956 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
17957 	    IPIF_NOFAILOVER));
17958 }
17959 
17960 /*
17961  * Return the number of point-to-point addresses on `ill'.
17962  */
17963 uint_t
17964 ill_ptpaddr_cnt(const ill_t *ill)
17965 {
17966 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
17967 }
17968 
17969 /* ARGSUSED */
17970 int
17971 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17972 	ip_ioctl_cmd_t *ipip, void *ifreq)
17973 {
17974 	struct lifreq	*lifr = ifreq;
17975 
17976 	ASSERT(q->q_next == NULL);
17977 	ASSERT(CONN_Q(q));
17978 
17979 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
17980 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17981 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
17982 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
17983 
17984 	return (0);
17985 }
17986 
17987 /* Find the previous ILL in this usesrc group */
17988 static ill_t *
17989 ill_prev_usesrc(ill_t *uill)
17990 {
17991 	ill_t *ill;
17992 
17993 	for (ill = uill->ill_usesrc_grp_next;
17994 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
17995 	    ill = ill->ill_usesrc_grp_next)
17996 		/* do nothing */;
17997 	return (ill);
17998 }
17999 
18000 /*
18001  * Release all members of the usesrc group. This routine is called
18002  * from ill_delete when the interface being unplumbed is the
18003  * group head.
18004  */
18005 static void
18006 ill_disband_usesrc_group(ill_t *uill)
18007 {
18008 	ill_t *next_ill, *tmp_ill;
18009 	ip_stack_t	*ipst = uill->ill_ipst;
18010 
18011 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18012 	next_ill = uill->ill_usesrc_grp_next;
18013 
18014 	do {
18015 		ASSERT(next_ill != NULL);
18016 		tmp_ill = next_ill->ill_usesrc_grp_next;
18017 		ASSERT(tmp_ill != NULL);
18018 		next_ill->ill_usesrc_grp_next = NULL;
18019 		next_ill->ill_usesrc_ifindex = 0;
18020 		next_ill = tmp_ill;
18021 	} while (next_ill->ill_usesrc_ifindex != 0);
18022 	uill->ill_usesrc_grp_next = NULL;
18023 }
18024 
18025 /*
18026  * Remove the client usesrc ILL from the list and relink to a new list
18027  */
18028 int
18029 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18030 {
18031 	ill_t *ill, *tmp_ill;
18032 	ip_stack_t	*ipst = ucill->ill_ipst;
18033 
18034 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18035 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18036 
18037 	/*
18038 	 * Check if the usesrc client ILL passed in is not already
18039 	 * in use as a usesrc ILL i.e one whose source address is
18040 	 * in use OR a usesrc ILL is not already in use as a usesrc
18041 	 * client ILL
18042 	 */
18043 	if ((ucill->ill_usesrc_ifindex == 0) ||
18044 	    (uill->ill_usesrc_ifindex != 0)) {
18045 		return (-1);
18046 	}
18047 
18048 	ill = ill_prev_usesrc(ucill);
18049 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18050 
18051 	/* Remove from the current list */
18052 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18053 		/* Only two elements in the list */
18054 		ASSERT(ill->ill_usesrc_ifindex == 0);
18055 		ill->ill_usesrc_grp_next = NULL;
18056 	} else {
18057 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18058 	}
18059 
18060 	if (ifindex == 0) {
18061 		ucill->ill_usesrc_ifindex = 0;
18062 		ucill->ill_usesrc_grp_next = NULL;
18063 		return (0);
18064 	}
18065 
18066 	ucill->ill_usesrc_ifindex = ifindex;
18067 	tmp_ill = uill->ill_usesrc_grp_next;
18068 	uill->ill_usesrc_grp_next = ucill;
18069 	ucill->ill_usesrc_grp_next =
18070 	    (tmp_ill != NULL) ? tmp_ill : uill;
18071 	return (0);
18072 }
18073 
18074 /*
18075  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18076  * ip.c for locking details.
18077  */
18078 /* ARGSUSED */
18079 int
18080 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18081     ip_ioctl_cmd_t *ipip, void *ifreq)
18082 {
18083 	struct lifreq *lifr = (struct lifreq *)ifreq;
18084 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18085 	    ill_flag_changed = B_FALSE;
18086 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18087 	int err = 0, ret;
18088 	uint_t ifindex;
18089 	ipsq_t *ipsq = NULL;
18090 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18091 
18092 	ASSERT(IAM_WRITER_IPIF(ipif));
18093 	ASSERT(q->q_next == NULL);
18094 	ASSERT(CONN_Q(q));
18095 
18096 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18097 
18098 	ifindex = lifr->lifr_index;
18099 	if (ifindex == 0) {
18100 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18101 			/* non usesrc group interface, nothing to reset */
18102 			return (0);
18103 		}
18104 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18105 		/* valid reset request */
18106 		reset_flg = B_TRUE;
18107 	}
18108 
18109 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18110 	    ip_process_ioctl, &err, ipst);
18111 	if (usesrc_ill == NULL) {
18112 		return (err);
18113 	}
18114 
18115 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18116 	    NEW_OP, B_TRUE);
18117 	if (ipsq == NULL) {
18118 		err = EINPROGRESS;
18119 		/* Operation enqueued on the ipsq of the usesrc ILL */
18120 		goto done;
18121 	}
18122 
18123 	/* USESRC isn't currently supported with IPMP */
18124 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18125 		err = ENOTSUP;
18126 		goto done;
18127 	}
18128 
18129 	/*
18130 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18131 	 * used by IPMP underlying interfaces, but someone might think it's
18132 	 * more general and try to use it independently with VNI.)
18133 	 */
18134 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18135 		err = ENOTSUP;
18136 		goto done;
18137 	}
18138 
18139 	/*
18140 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18141 	 * already a client then return EINVAL
18142 	 */
18143 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18144 		err = EINVAL;
18145 		goto done;
18146 	}
18147 
18148 	/*
18149 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18150 	 * be then this is a duplicate operation.
18151 	 */
18152 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18153 		err = 0;
18154 		goto done;
18155 	}
18156 
18157 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18158 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18159 	    usesrc_ill->ill_isv6));
18160 
18161 	/*
18162 	 * The next step ensures that no new ires will be created referencing
18163 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18164 	 * we go through an ire walk deleting all ire caches that reference
18165 	 * the client ill. New ires referencing the client ill that are added
18166 	 * to the ire table before the ILL_CHANGING flag is set, will be
18167 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18168 	 * the client ill while the ILL_CHANGING flag is set will be failed
18169 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18170 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18171 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18172 	 * belong to the same usesrc group.
18173 	 */
18174 	mutex_enter(&usesrc_cli_ill->ill_lock);
18175 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18176 	mutex_exit(&usesrc_cli_ill->ill_lock);
18177 	ill_flag_changed = B_TRUE;
18178 
18179 	if (ipif->ipif_isv6)
18180 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18181 		    ALL_ZONES, ipst);
18182 	else
18183 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18184 		    ALL_ZONES, ipst);
18185 
18186 	/*
18187 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18188 	 * and the ill_usesrc_ifindex fields
18189 	 */
18190 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18191 
18192 	if (reset_flg) {
18193 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18194 		if (ret != 0) {
18195 			err = EINVAL;
18196 		}
18197 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18198 		goto done;
18199 	}
18200 
18201 	/*
18202 	 * Four possibilities to consider:
18203 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18204 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18205 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18206 	 * 4. Both are part of their respective usesrc groups
18207 	 */
18208 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18209 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18210 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18211 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18212 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18213 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18214 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18215 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18216 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18217 		/* Insert at head of list */
18218 		usesrc_cli_ill->ill_usesrc_grp_next =
18219 		    usesrc_ill->ill_usesrc_grp_next;
18220 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18221 	} else {
18222 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18223 		    ifindex);
18224 		if (ret != 0)
18225 			err = EINVAL;
18226 	}
18227 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18228 
18229 done:
18230 	if (ill_flag_changed) {
18231 		mutex_enter(&usesrc_cli_ill->ill_lock);
18232 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18233 		mutex_exit(&usesrc_cli_ill->ill_lock);
18234 	}
18235 	if (ipsq != NULL)
18236 		ipsq_exit(ipsq);
18237 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18238 	ill_refrele(usesrc_ill);
18239 	return (err);
18240 }
18241 
18242 /*
18243  * comparison function used by avl.
18244  */
18245 static int
18246 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18247 {
18248 
18249 	uint_t index;
18250 
18251 	ASSERT(phyip != NULL && index_ptr != NULL);
18252 
18253 	index = *((uint_t *)index_ptr);
18254 	/*
18255 	 * let the phyint with the lowest index be on top.
18256 	 */
18257 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18258 		return (1);
18259 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18260 		return (-1);
18261 	return (0);
18262 }
18263 
18264 /*
18265  * comparison function used by avl.
18266  */
18267 static int
18268 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18269 {
18270 	ill_t *ill;
18271 	int res = 0;
18272 
18273 	ASSERT(phyip != NULL && name_ptr != NULL);
18274 
18275 	if (((phyint_t *)phyip)->phyint_illv4)
18276 		ill = ((phyint_t *)phyip)->phyint_illv4;
18277 	else
18278 		ill = ((phyint_t *)phyip)->phyint_illv6;
18279 	ASSERT(ill != NULL);
18280 
18281 	res = strcmp(ill->ill_name, (char *)name_ptr);
18282 	if (res > 0)
18283 		return (1);
18284 	else if (res < 0)
18285 		return (-1);
18286 	return (0);
18287 }
18288 
18289 /*
18290  * This function is called on the unplumb path via ill_glist_delete() when
18291  * there are no ills left on the phyint and thus the phyint can be freed.
18292  */
18293 static void
18294 phyint_free(phyint_t *phyi)
18295 {
18296 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18297 
18298 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18299 
18300 	/*
18301 	 * If this phyint was an IPMP meta-interface, blow away the group.
18302 	 * This is safe to do because all of the illgrps have already been
18303 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18304 	 * If we're cleaning up as a result of failed initialization,
18305 	 * phyint_grp may be NULL.
18306 	 */
18307 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18308 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18309 		ipmp_grp_destroy(phyi->phyint_grp);
18310 		phyi->phyint_grp = NULL;
18311 		rw_exit(&ipst->ips_ipmp_lock);
18312 	}
18313 
18314 	/*
18315 	 * If this interface was under IPMP, take it out of the group.
18316 	 */
18317 	if (phyi->phyint_grp != NULL)
18318 		ipmp_phyint_leave_grp(phyi);
18319 
18320 	/*
18321 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18322 	 * will be freed in ipsq_exit().
18323 	 */
18324 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18325 	phyi->phyint_name[0] = '\0';
18326 
18327 	mi_free(phyi);
18328 }
18329 
18330 /*
18331  * Attach the ill to the phyint structure which can be shared by both
18332  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18333  * function is called from ipif_set_values and ill_lookup_on_name (for
18334  * loopback) where we know the name of the ill. We lookup the ill and if
18335  * there is one present already with the name use that phyint. Otherwise
18336  * reuse the one allocated by ill_init.
18337  */
18338 static void
18339 ill_phyint_reinit(ill_t *ill)
18340 {
18341 	boolean_t isv6 = ill->ill_isv6;
18342 	phyint_t *phyi_old;
18343 	phyint_t *phyi;
18344 	avl_index_t where = 0;
18345 	ill_t	*ill_other = NULL;
18346 	ip_stack_t	*ipst = ill->ill_ipst;
18347 
18348 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18349 
18350 	phyi_old = ill->ill_phyint;
18351 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18352 	    phyi_old->phyint_illv6 == NULL));
18353 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18354 	    phyi_old->phyint_illv4 == NULL));
18355 	ASSERT(phyi_old->phyint_ifindex == 0);
18356 
18357 	/*
18358 	 * Now that our ill has a name, set it in the phyint.
18359 	 */
18360 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18361 
18362 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18363 	    ill->ill_name, &where);
18364 
18365 	/*
18366 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18367 	 *    the global list of ills. So no other thread could have located
18368 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18369 	 * 2. Now locate the other protocol instance of this ill.
18370 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18371 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18372 	 *    of neither ill can change.
18373 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18374 	 *    other ill.
18375 	 * 5. Release all locks.
18376 	 */
18377 
18378 	/*
18379 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18380 	 * we are initializing IPv4.
18381 	 */
18382 	if (phyi != NULL) {
18383 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18384 		ASSERT(ill_other->ill_phyint != NULL);
18385 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18386 		    (!isv6 && ill_other->ill_isv6));
18387 		GRAB_ILL_LOCKS(ill, ill_other);
18388 		/*
18389 		 * We are potentially throwing away phyint_flags which
18390 		 * could be different from the one that we obtain from
18391 		 * ill_other->ill_phyint. But it is okay as we are assuming
18392 		 * that the state maintained within IP is correct.
18393 		 */
18394 		mutex_enter(&phyi->phyint_lock);
18395 		if (isv6) {
18396 			ASSERT(phyi->phyint_illv6 == NULL);
18397 			phyi->phyint_illv6 = ill;
18398 		} else {
18399 			ASSERT(phyi->phyint_illv4 == NULL);
18400 			phyi->phyint_illv4 = ill;
18401 		}
18402 
18403 		/*
18404 		 * Delete the old phyint and make its ipsq eligible
18405 		 * to be freed in ipsq_exit().
18406 		 */
18407 		phyi_old->phyint_illv4 = NULL;
18408 		phyi_old->phyint_illv6 = NULL;
18409 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18410 		phyi_old->phyint_name[0] = '\0';
18411 		mi_free(phyi_old);
18412 	} else {
18413 		mutex_enter(&ill->ill_lock);
18414 		/*
18415 		 * We don't need to acquire any lock, since
18416 		 * the ill is not yet visible globally  and we
18417 		 * have not yet released the ill_g_lock.
18418 		 */
18419 		phyi = phyi_old;
18420 		mutex_enter(&phyi->phyint_lock);
18421 		/* XXX We need a recovery strategy here. */
18422 		if (!phyint_assign_ifindex(phyi, ipst))
18423 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18424 
18425 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18426 		    (void *)phyi, where);
18427 
18428 		(void) avl_find(&ipst->ips_phyint_g_list->
18429 		    phyint_list_avl_by_index,
18430 		    &phyi->phyint_ifindex, &where);
18431 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18432 		    (void *)phyi, where);
18433 	}
18434 
18435 	/*
18436 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18437 	 * pending mp is not affected because that is per ill basis.
18438 	 */
18439 	ill->ill_phyint = phyi;
18440 
18441 	/*
18442 	 * Now that the phyint's ifindex has been assigned, complete the
18443 	 * remaining
18444 	 */
18445 
18446 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18447 	if (ill->ill_isv6) {
18448 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18449 		    ill->ill_phyint->phyint_ifindex;
18450 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18451 	} else {
18452 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18453 	}
18454 
18455 	/*
18456 	 * Generate an event within the hooks framework to indicate that
18457 	 * a new interface has just been added to IP.  For this event to
18458 	 * be generated, the network interface must, at least, have an
18459 	 * ifindex assigned to it.  (We don't generate the event for
18460 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
18461 	 *
18462 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18463 	 * that the ordering of delivered events to listeners matches the
18464 	 * order of them in the kernel.
18465 	 */
18466 	if (!IS_LOOPBACK(ill)) {
18467 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18468 		    ill->ill_name_length);
18469 	}
18470 	RELEASE_ILL_LOCKS(ill, ill_other);
18471 	mutex_exit(&phyi->phyint_lock);
18472 }
18473 
18474 /*
18475  * Notify any downstream modules of the name of this interface.
18476  * An M_IOCTL is used even though we don't expect a successful reply.
18477  * Any reply message from the driver (presumably an M_IOCNAK) will
18478  * eventually get discarded somewhere upstream.  The message format is
18479  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18480  * to IP.
18481  */
18482 static void
18483 ip_ifname_notify(ill_t *ill, queue_t *q)
18484 {
18485 	mblk_t *mp1, *mp2;
18486 	struct iocblk *iocp;
18487 	struct lifreq *lifr;
18488 
18489 	mp1 = mkiocb(SIOCSLIFNAME);
18490 	if (mp1 == NULL)
18491 		return;
18492 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18493 	if (mp2 == NULL) {
18494 		freeb(mp1);
18495 		return;
18496 	}
18497 
18498 	mp1->b_cont = mp2;
18499 	iocp = (struct iocblk *)mp1->b_rptr;
18500 	iocp->ioc_count = sizeof (struct lifreq);
18501 
18502 	lifr = (struct lifreq *)mp2->b_rptr;
18503 	mp2->b_wptr += sizeof (struct lifreq);
18504 	bzero(lifr, sizeof (struct lifreq));
18505 
18506 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18507 	lifr->lifr_ppa = ill->ill_ppa;
18508 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18509 
18510 	putnext(q, mp1);
18511 }
18512 
18513 static int
18514 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18515 {
18516 	int		err;
18517 	ip_stack_t	*ipst = ill->ill_ipst;
18518 	phyint_t	*phyi = ill->ill_phyint;
18519 
18520 	/* Set the obsolete NDD per-interface forwarding name. */
18521 	err = ill_set_ndd_name(ill);
18522 	if (err != 0) {
18523 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18524 		    err);
18525 	}
18526 
18527 	/*
18528 	 * Now that ill_name is set, the configuration for the IPMP
18529 	 * meta-interface can be performed.
18530 	 */
18531 	if (IS_IPMP(ill)) {
18532 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18533 		/*
18534 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18535 		 * meta-interface and we need to create the IPMP group.
18536 		 */
18537 		if (phyi->phyint_grp == NULL) {
18538 			/*
18539 			 * If someone has renamed another IPMP group to have
18540 			 * the same name as our interface, bail.
18541 			 */
18542 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18543 				rw_exit(&ipst->ips_ipmp_lock);
18544 				return (EEXIST);
18545 			}
18546 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18547 			if (phyi->phyint_grp == NULL) {
18548 				rw_exit(&ipst->ips_ipmp_lock);
18549 				return (ENOMEM);
18550 			}
18551 		}
18552 		rw_exit(&ipst->ips_ipmp_lock);
18553 	}
18554 
18555 	/* Tell downstream modules where they are. */
18556 	ip_ifname_notify(ill, q);
18557 
18558 	/*
18559 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18560 	 * Error cases are ENOMEM ...
18561 	 */
18562 	err = ill_dl_phys(ill, ipif, mp, q);
18563 
18564 	/*
18565 	 * If there is no IRE expiration timer running, get one started.
18566 	 * igmp and mld timers will be triggered by the first multicast
18567 	 */
18568 	if (ipst->ips_ip_ire_expire_id == 0) {
18569 		/*
18570 		 * acquire the lock and check again.
18571 		 */
18572 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18573 		if (ipst->ips_ip_ire_expire_id == 0) {
18574 			ipst->ips_ip_ire_expire_id = timeout(
18575 			    ip_trash_timer_expire, ipst,
18576 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18577 		}
18578 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18579 	}
18580 
18581 	if (ill->ill_isv6) {
18582 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18583 		if (ipst->ips_mld_slowtimeout_id == 0) {
18584 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18585 			    (void *)ipst,
18586 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18587 		}
18588 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18589 	} else {
18590 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18591 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18592 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18593 			    (void *)ipst,
18594 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18595 		}
18596 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18597 	}
18598 
18599 	return (err);
18600 }
18601 
18602 /*
18603  * Common routine for ppa and ifname setting. Should be called exclusive.
18604  *
18605  * Returns EINPROGRESS when mp has been consumed by queueing it on
18606  * ill_pending_mp and the ioctl will complete in ip_rput.
18607  *
18608  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18609  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18610  * For SLIFNAME, we pass these values back to the userland.
18611  */
18612 static int
18613 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18614 {
18615 	ill_t	*ill;
18616 	ipif_t	*ipif;
18617 	ipsq_t	*ipsq;
18618 	char	*ppa_ptr;
18619 	char	*old_ptr;
18620 	char	old_char;
18621 	int	error;
18622 	ip_stack_t	*ipst;
18623 
18624 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18625 	ASSERT(q->q_next != NULL);
18626 	ASSERT(interf_name != NULL);
18627 
18628 	ill = (ill_t *)q->q_ptr;
18629 	ipst = ill->ill_ipst;
18630 
18631 	ASSERT(ill->ill_ipst != NULL);
18632 	ASSERT(ill->ill_name[0] == '\0');
18633 	ASSERT(IAM_WRITER_ILL(ill));
18634 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18635 	ASSERT(ill->ill_ppa == UINT_MAX);
18636 
18637 	/* The ppa is sent down by ifconfig or is chosen */
18638 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18639 		return (EINVAL);
18640 	}
18641 
18642 	/*
18643 	 * make sure ppa passed in is same as ppa in the name.
18644 	 * This check is not made when ppa == UINT_MAX in that case ppa
18645 	 * in the name could be anything. System will choose a ppa and
18646 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18647 	 */
18648 	if (*new_ppa_ptr != UINT_MAX) {
18649 		/* stoi changes the pointer */
18650 		old_ptr = ppa_ptr;
18651 		/*
18652 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18653 		 * (they don't have an externally visible ppa).  We assign one
18654 		 * here so that we can manage the interface.  Note that in
18655 		 * the past this value was always 0 for DLPI 1 drivers.
18656 		 */
18657 		if (*new_ppa_ptr == 0)
18658 			*new_ppa_ptr = stoi(&old_ptr);
18659 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18660 			return (EINVAL);
18661 	}
18662 	/*
18663 	 * terminate string before ppa
18664 	 * save char at that location.
18665 	 */
18666 	old_char = ppa_ptr[0];
18667 	ppa_ptr[0] = '\0';
18668 
18669 	ill->ill_ppa = *new_ppa_ptr;
18670 	/*
18671 	 * Finish as much work now as possible before calling ill_glist_insert
18672 	 * which makes the ill globally visible and also merges it with the
18673 	 * other protocol instance of this phyint. The remaining work is
18674 	 * done after entering the ipsq which may happen sometime later.
18675 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18676 	 */
18677 	ipif = ill->ill_ipif;
18678 
18679 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18680 	ipif_assign_seqid(ipif);
18681 
18682 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18683 		ill->ill_flags |= ILLF_IPV4;
18684 
18685 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18686 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18687 
18688 	if (ill->ill_flags & ILLF_IPV6) {
18689 
18690 		ill->ill_isv6 = B_TRUE;
18691 		if (ill->ill_rq != NULL) {
18692 			ill->ill_rq->q_qinfo = &iprinitv6;
18693 			ill->ill_wq->q_qinfo = &ipwinitv6;
18694 		}
18695 
18696 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18697 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18698 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18699 		ipif->ipif_v6subnet = ipv6_all_zeros;
18700 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18701 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18702 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18703 		/*
18704 		 * point-to-point or Non-mulicast capable
18705 		 * interfaces won't do NUD unless explicitly
18706 		 * configured to do so.
18707 		 */
18708 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18709 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18710 			ill->ill_flags |= ILLF_NONUD;
18711 		}
18712 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18713 		if (ill->ill_flags & ILLF_NOARP) {
18714 			/*
18715 			 * Note: xresolv interfaces will eventually need
18716 			 * NOARP set here as well, but that will require
18717 			 * those external resolvers to have some
18718 			 * knowledge of that flag and act appropriately.
18719 			 * Not to be changed at present.
18720 			 */
18721 			ill->ill_flags &= ~ILLF_NOARP;
18722 		}
18723 		/*
18724 		 * Set the ILLF_ROUTER flag according to the global
18725 		 * IPv6 forwarding policy.
18726 		 */
18727 		if (ipst->ips_ipv6_forward != 0)
18728 			ill->ill_flags |= ILLF_ROUTER;
18729 	} else if (ill->ill_flags & ILLF_IPV4) {
18730 		ill->ill_isv6 = B_FALSE;
18731 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18732 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18733 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18734 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18735 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18736 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18737 		/*
18738 		 * Set the ILLF_ROUTER flag according to the global
18739 		 * IPv4 forwarding policy.
18740 		 */
18741 		if (ipst->ips_ip_g_forward != 0)
18742 			ill->ill_flags |= ILLF_ROUTER;
18743 	}
18744 
18745 	ASSERT(ill->ill_phyint != NULL);
18746 
18747 	/*
18748 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18749 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18750 	 */
18751 	if (!ill_allocate_mibs(ill))
18752 		return (ENOMEM);
18753 
18754 	/*
18755 	 * Pick a default sap until we get the DL_INFO_ACK back from
18756 	 * the driver.
18757 	 */
18758 	if (ill->ill_sap == 0) {
18759 		if (ill->ill_isv6)
18760 			ill->ill_sap = IP6_DL_SAP;
18761 		else
18762 			ill->ill_sap = IP_DL_SAP;
18763 	}
18764 
18765 	ill->ill_ifname_pending = 1;
18766 	ill->ill_ifname_pending_err = 0;
18767 
18768 	/*
18769 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18770 	 * that were joined while this ill was not bound to the DLPI link need
18771 	 * to be recovered by ill_recover_multicast().
18772 	 */
18773 	ill->ill_need_recover_multicast = 1;
18774 
18775 	ill_refhold(ill);
18776 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18777 	if ((error = ill_glist_insert(ill, interf_name,
18778 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18779 		ill->ill_ppa = UINT_MAX;
18780 		ill->ill_name[0] = '\0';
18781 		/*
18782 		 * undo null termination done above.
18783 		 */
18784 		ppa_ptr[0] = old_char;
18785 		rw_exit(&ipst->ips_ill_g_lock);
18786 		ill_refrele(ill);
18787 		return (error);
18788 	}
18789 
18790 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18791 
18792 	/*
18793 	 * When we return the buffer pointed to by interf_name should contain
18794 	 * the same name as in ill_name.
18795 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18796 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18797 	 * so copy full name and update the ppa ptr.
18798 	 * When ppa passed in != UINT_MAX all values are correct just undo
18799 	 * null termination, this saves a bcopy.
18800 	 */
18801 	if (*new_ppa_ptr == UINT_MAX) {
18802 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18803 		*new_ppa_ptr = ill->ill_ppa;
18804 	} else {
18805 		/*
18806 		 * undo null termination done above.
18807 		 */
18808 		ppa_ptr[0] = old_char;
18809 	}
18810 
18811 	/* Let SCTP know about this ILL */
18812 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18813 
18814 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18815 	    B_TRUE);
18816 
18817 	rw_exit(&ipst->ips_ill_g_lock);
18818 	ill_refrele(ill);
18819 	if (ipsq == NULL)
18820 		return (EINPROGRESS);
18821 
18822 	/*
18823 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18824 	 */
18825 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18826 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18827 	else
18828 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18829 
18830 	error = ipif_set_values_tail(ill, ipif, mp, q);
18831 	ipsq_exit(ipsq);
18832 	if (error != 0 && error != EINPROGRESS) {
18833 		/*
18834 		 * restore previous values
18835 		 */
18836 		ill->ill_isv6 = B_FALSE;
18837 	}
18838 	return (error);
18839 }
18840 
18841 void
18842 ipif_init(ip_stack_t *ipst)
18843 {
18844 	int i;
18845 
18846 	for (i = 0; i < MAX_G_HEADS; i++) {
18847 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18848 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18849 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
18850 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18851 	}
18852 
18853 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18854 	    ill_phyint_compare_index,
18855 	    sizeof (phyint_t),
18856 	    offsetof(struct phyint, phyint_avl_by_index));
18857 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18858 	    ill_phyint_compare_name,
18859 	    sizeof (phyint_t),
18860 	    offsetof(struct phyint, phyint_avl_by_name));
18861 }
18862 
18863 /*
18864  * Lookup the ipif corresponding to the onlink destination address. For
18865  * point-to-point interfaces, it matches with remote endpoint destination
18866  * address. For point-to-multipoint interfaces it only tries to match the
18867  * destination with the interface's subnet address. The longest, most specific
18868  * match is found to take care of such rare network configurations like -
18869  * le0: 129.146.1.1/16
18870  * le1: 129.146.2.2/24
18871  *
18872  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
18873  * supported on underlying interfaces in an IPMP group, underlying interfaces
18874  * are ignored when looking up a match.  (If we didn't ignore them, we'd
18875  * risk using a test address as a source for outgoing traffic.)
18876  */
18877 ipif_t *
18878 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
18879 {
18880 	ipif_t	*ipif, *best_ipif;
18881 	ill_t	*ill;
18882 	ill_walk_context_t ctx;
18883 
18884 	ASSERT(zoneid != ALL_ZONES);
18885 	best_ipif = NULL;
18886 
18887 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18888 	ill = ILL_START_WALK_V4(&ctx, ipst);
18889 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18890 		if (IS_UNDER_IPMP(ill))
18891 			continue;
18892 		mutex_enter(&ill->ill_lock);
18893 		for (ipif = ill->ill_ipif; ipif != NULL;
18894 		    ipif = ipif->ipif_next) {
18895 			if (!IPIF_CAN_LOOKUP(ipif))
18896 				continue;
18897 			if (ipif->ipif_zoneid != zoneid &&
18898 			    ipif->ipif_zoneid != ALL_ZONES)
18899 				continue;
18900 			/*
18901 			 * Point-to-point case. Look for exact match with
18902 			 * destination address.
18903 			 */
18904 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18905 				if (ipif->ipif_pp_dst_addr == addr) {
18906 					ipif_refhold_locked(ipif);
18907 					mutex_exit(&ill->ill_lock);
18908 					rw_exit(&ipst->ips_ill_g_lock);
18909 					if (best_ipif != NULL)
18910 						ipif_refrele(best_ipif);
18911 					return (ipif);
18912 				}
18913 			} else if (ipif->ipif_subnet == (addr &
18914 			    ipif->ipif_net_mask)) {
18915 				/*
18916 				 * Point-to-multipoint case. Looping through to
18917 				 * find the most specific match. If there are
18918 				 * multiple best match ipif's then prefer ipif's
18919 				 * that are UP. If there is only one best match
18920 				 * ipif and it is DOWN we must still return it.
18921 				 */
18922 				if ((best_ipif == NULL) ||
18923 				    (ipif->ipif_net_mask >
18924 				    best_ipif->ipif_net_mask) ||
18925 				    ((ipif->ipif_net_mask ==
18926 				    best_ipif->ipif_net_mask) &&
18927 				    ((ipif->ipif_flags & IPIF_UP) &&
18928 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
18929 					ipif_refhold_locked(ipif);
18930 					mutex_exit(&ill->ill_lock);
18931 					rw_exit(&ipst->ips_ill_g_lock);
18932 					if (best_ipif != NULL)
18933 						ipif_refrele(best_ipif);
18934 					best_ipif = ipif;
18935 					rw_enter(&ipst->ips_ill_g_lock,
18936 					    RW_READER);
18937 					mutex_enter(&ill->ill_lock);
18938 				}
18939 			}
18940 		}
18941 		mutex_exit(&ill->ill_lock);
18942 	}
18943 	rw_exit(&ipst->ips_ill_g_lock);
18944 	return (best_ipif);
18945 }
18946 
18947 /*
18948  * Save enough information so that we can recreate the IRE if
18949  * the interface goes down and then up.
18950  */
18951 static void
18952 ipif_save_ire(ipif_t *ipif, ire_t *ire)
18953 {
18954 	mblk_t	*save_mp;
18955 
18956 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
18957 	if (save_mp != NULL) {
18958 		ifrt_t	*ifrt;
18959 
18960 		save_mp->b_wptr += sizeof (ifrt_t);
18961 		ifrt = (ifrt_t *)save_mp->b_rptr;
18962 		bzero(ifrt, sizeof (ifrt_t));
18963 		ifrt->ifrt_type = ire->ire_type;
18964 		ifrt->ifrt_addr = ire->ire_addr;
18965 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
18966 		ifrt->ifrt_src_addr = ire->ire_src_addr;
18967 		ifrt->ifrt_mask = ire->ire_mask;
18968 		ifrt->ifrt_flags = ire->ire_flags;
18969 		ifrt->ifrt_max_frag = ire->ire_max_frag;
18970 		mutex_enter(&ipif->ipif_saved_ire_lock);
18971 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
18972 		ipif->ipif_saved_ire_mp = save_mp;
18973 		ipif->ipif_saved_ire_cnt++;
18974 		mutex_exit(&ipif->ipif_saved_ire_lock);
18975 	}
18976 }
18977 
18978 static void
18979 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
18980 {
18981 	mblk_t	**mpp;
18982 	mblk_t	*mp;
18983 	ifrt_t	*ifrt;
18984 
18985 	/* Remove from ipif_saved_ire_mp list if it is there */
18986 	mutex_enter(&ipif->ipif_saved_ire_lock);
18987 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
18988 	    mpp = &(*mpp)->b_cont) {
18989 		/*
18990 		 * On a given ipif, the triple of address, gateway and
18991 		 * mask is unique for each saved IRE (in the case of
18992 		 * ordinary interface routes, the gateway address is
18993 		 * all-zeroes).
18994 		 */
18995 		mp = *mpp;
18996 		ifrt = (ifrt_t *)mp->b_rptr;
18997 		if (ifrt->ifrt_addr == ire->ire_addr &&
18998 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
18999 		    ifrt->ifrt_mask == ire->ire_mask) {
19000 			*mpp = mp->b_cont;
19001 			ipif->ipif_saved_ire_cnt--;
19002 			freeb(mp);
19003 			break;
19004 		}
19005 	}
19006 	mutex_exit(&ipif->ipif_saved_ire_lock);
19007 }
19008 
19009 /*
19010  * IP multirouting broadcast routes handling
19011  * Append CGTP broadcast IREs to regular ones created
19012  * at ifconfig time.
19013  */
19014 static void
19015 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
19016 {
19017 	ire_t *ire_prim;
19018 
19019 	ASSERT(ire != NULL);
19020 	ASSERT(ire_dst != NULL);
19021 
19022 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19023 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19024 	if (ire_prim != NULL) {
19025 		/*
19026 		 * We are in the special case of broadcasts for
19027 		 * CGTP. We add an IRE_BROADCAST that holds
19028 		 * the RTF_MULTIRT flag, the destination
19029 		 * address of ire_dst and the low level
19030 		 * info of ire_prim. In other words, CGTP
19031 		 * broadcast is added to the redundant ipif.
19032 		 */
19033 		ipif_t *ipif_prim;
19034 		ire_t  *bcast_ire;
19035 
19036 		ipif_prim = ire_prim->ire_ipif;
19037 
19038 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19039 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19040 		    (void *)ire_dst, (void *)ire_prim,
19041 		    (void *)ipif_prim));
19042 
19043 		bcast_ire = ire_create(
19044 		    (uchar_t *)&ire->ire_addr,
19045 		    (uchar_t *)&ip_g_all_ones,
19046 		    (uchar_t *)&ire_dst->ire_src_addr,
19047 		    (uchar_t *)&ire->ire_gateway_addr,
19048 		    &ipif_prim->ipif_mtu,
19049 		    NULL,
19050 		    ipif_prim->ipif_rq,
19051 		    ipif_prim->ipif_wq,
19052 		    IRE_BROADCAST,
19053 		    ipif_prim,
19054 		    0,
19055 		    0,
19056 		    0,
19057 		    ire->ire_flags,
19058 		    &ire_uinfo_null,
19059 		    NULL,
19060 		    NULL,
19061 		    ipst);
19062 
19063 		if (bcast_ire != NULL) {
19064 
19065 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19066 			    B_FALSE) == 0) {
19067 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19068 				    "added bcast_ire %p\n",
19069 				    (void *)bcast_ire));
19070 
19071 				ipif_save_ire(bcast_ire->ire_ipif,
19072 				    bcast_ire);
19073 				ire_refrele(bcast_ire);
19074 			}
19075 		}
19076 		ire_refrele(ire_prim);
19077 	}
19078 }
19079 
19080 /*
19081  * IP multirouting broadcast routes handling
19082  * Remove the broadcast ire
19083  */
19084 static void
19085 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19086 {
19087 	ire_t *ire_dst;
19088 
19089 	ASSERT(ire != NULL);
19090 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19091 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19092 	if (ire_dst != NULL) {
19093 		ire_t *ire_prim;
19094 
19095 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19096 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19097 		if (ire_prim != NULL) {
19098 			ipif_t *ipif_prim;
19099 			ire_t  *bcast_ire;
19100 
19101 			ipif_prim = ire_prim->ire_ipif;
19102 
19103 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19104 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19105 			    (void *)ire_dst, (void *)ire_prim,
19106 			    (void *)ipif_prim));
19107 
19108 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19109 			    ire->ire_gateway_addr,
19110 			    IRE_BROADCAST,
19111 			    ipif_prim, ALL_ZONES,
19112 			    NULL,
19113 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19114 			    MATCH_IRE_MASK, ipst);
19115 
19116 			if (bcast_ire != NULL) {
19117 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19118 				    "looked up bcast_ire %p\n",
19119 				    (void *)bcast_ire));
19120 				ipif_remove_ire(bcast_ire->ire_ipif,
19121 				    bcast_ire);
19122 				ire_delete(bcast_ire);
19123 				ire_refrele(bcast_ire);
19124 			}
19125 			ire_refrele(ire_prim);
19126 		}
19127 		ire_refrele(ire_dst);
19128 	}
19129 }
19130 
19131 /*
19132  * IPsec hardware acceleration capabilities related functions.
19133  */
19134 
19135 /*
19136  * Free a per-ill IPsec capabilities structure.
19137  */
19138 static void
19139 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19140 {
19141 	if (capab->auth_hw_algs != NULL)
19142 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19143 	if (capab->encr_hw_algs != NULL)
19144 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19145 	if (capab->encr_algparm != NULL)
19146 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19147 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19148 }
19149 
19150 /*
19151  * Allocate a new per-ill IPsec capabilities structure. This structure
19152  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19153  * an array which specifies, for each algorithm, whether this algorithm
19154  * is supported by the ill or not.
19155  */
19156 static ill_ipsec_capab_t *
19157 ill_ipsec_capab_alloc(void)
19158 {
19159 	ill_ipsec_capab_t *capab;
19160 	uint_t nelems;
19161 
19162 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19163 	if (capab == NULL)
19164 		return (NULL);
19165 
19166 	/* we need one bit per algorithm */
19167 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19168 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19169 
19170 	/* allocate memory to store algorithm flags */
19171 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19172 	if (capab->encr_hw_algs == NULL)
19173 		goto nomem;
19174 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19175 	if (capab->auth_hw_algs == NULL)
19176 		goto nomem;
19177 	/*
19178 	 * Leave encr_algparm NULL for now since we won't need it half
19179 	 * the time
19180 	 */
19181 	return (capab);
19182 
19183 nomem:
19184 	ill_ipsec_capab_free(capab);
19185 	return (NULL);
19186 }
19187 
19188 /*
19189  * Resize capability array.  Since we're exclusive, this is OK.
19190  */
19191 static boolean_t
19192 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19193 {
19194 	ipsec_capab_algparm_t *nalp, *oalp;
19195 	uint32_t olen, nlen;
19196 
19197 	oalp = capab->encr_algparm;
19198 	olen = capab->encr_algparm_size;
19199 
19200 	if (oalp != NULL) {
19201 		if (algid < capab->encr_algparm_end)
19202 			return (B_TRUE);
19203 	}
19204 
19205 	nlen = (algid + 1) * sizeof (*nalp);
19206 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19207 	if (nalp == NULL)
19208 		return (B_FALSE);
19209 
19210 	if (oalp != NULL) {
19211 		bcopy(oalp, nalp, olen);
19212 		kmem_free(oalp, olen);
19213 	}
19214 	capab->encr_algparm = nalp;
19215 	capab->encr_algparm_size = nlen;
19216 	capab->encr_algparm_end = algid + 1;
19217 
19218 	return (B_TRUE);
19219 }
19220 
19221 /*
19222  * Compare the capabilities of the specified ill with the protocol
19223  * and algorithms specified by the SA passed as argument.
19224  * If they match, returns B_TRUE, B_FALSE if they do not match.
19225  *
19226  * The ill can be passed as a pointer to it, or by specifying its index
19227  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19228  *
19229  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19230  * packet is eligible for hardware acceleration, and by
19231  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19232  * to a particular ill.
19233  */
19234 boolean_t
19235 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19236     ipsa_t *sa, netstack_t *ns)
19237 {
19238 	boolean_t sa_isv6;
19239 	uint_t algid;
19240 	struct ill_ipsec_capab_s *cpp;
19241 	boolean_t need_refrele = B_FALSE;
19242 	ip_stack_t	*ipst = ns->netstack_ip;
19243 
19244 	if (ill == NULL) {
19245 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19246 		    NULL, NULL, NULL, ipst);
19247 		if (ill == NULL) {
19248 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19249 			return (B_FALSE);
19250 		}
19251 		need_refrele = B_TRUE;
19252 	}
19253 
19254 	/*
19255 	 * Use the address length specified by the SA to determine
19256 	 * if it corresponds to a IPv6 address, and fail the matching
19257 	 * if the isv6 flag passed as argument does not match.
19258 	 * Note: this check is used for SADB capability checking before
19259 	 * sending SA information to an ill.
19260 	 */
19261 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19262 	if (sa_isv6 != ill_isv6)
19263 		/* protocol mismatch */
19264 		goto done;
19265 
19266 	/*
19267 	 * Check if the ill supports the protocol, algorithm(s) and
19268 	 * key size(s) specified by the SA, and get the pointers to
19269 	 * the algorithms supported by the ill.
19270 	 */
19271 	switch (sa->ipsa_type) {
19272 
19273 	case SADB_SATYPE_ESP:
19274 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19275 			/* ill does not support ESP acceleration */
19276 			goto done;
19277 		cpp = ill->ill_ipsec_capab_esp;
19278 		algid = sa->ipsa_auth_alg;
19279 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19280 			goto done;
19281 		algid = sa->ipsa_encr_alg;
19282 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19283 			goto done;
19284 		if (algid < cpp->encr_algparm_end) {
19285 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19286 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19287 				goto done;
19288 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19289 				goto done;
19290 		}
19291 		break;
19292 
19293 	case SADB_SATYPE_AH:
19294 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19295 			/* ill does not support AH acceleration */
19296 			goto done;
19297 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19298 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19299 			goto done;
19300 		break;
19301 	}
19302 
19303 	if (need_refrele)
19304 		ill_refrele(ill);
19305 	return (B_TRUE);
19306 done:
19307 	if (need_refrele)
19308 		ill_refrele(ill);
19309 	return (B_FALSE);
19310 }
19311 
19312 /*
19313  * Add a new ill to the list of IPsec capable ills.
19314  * Called from ill_capability_ipsec_ack() when an ACK was received
19315  * indicating that IPsec hardware processing was enabled for an ill.
19316  *
19317  * ill must point to the ill for which acceleration was enabled.
19318  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19319  */
19320 static void
19321 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19322 {
19323 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19324 	uint_t sa_type;
19325 	uint_t ipproto;
19326 	ip_stack_t	*ipst = ill->ill_ipst;
19327 
19328 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19329 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19330 
19331 	switch (dl_cap) {
19332 	case DL_CAPAB_IPSEC_AH:
19333 		sa_type = SADB_SATYPE_AH;
19334 		ills = &ipst->ips_ipsec_capab_ills_ah;
19335 		ipproto = IPPROTO_AH;
19336 		break;
19337 	case DL_CAPAB_IPSEC_ESP:
19338 		sa_type = SADB_SATYPE_ESP;
19339 		ills = &ipst->ips_ipsec_capab_ills_esp;
19340 		ipproto = IPPROTO_ESP;
19341 		break;
19342 	}
19343 
19344 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19345 
19346 	/*
19347 	 * Add ill index to list of hardware accelerators. If
19348 	 * already in list, do nothing.
19349 	 */
19350 	for (cur_ill = *ills; cur_ill != NULL &&
19351 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19352 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19353 		;
19354 
19355 	if (cur_ill == NULL) {
19356 		/* if this is a new entry for this ill */
19357 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19358 		if (new_ill == NULL) {
19359 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19360 			return;
19361 		}
19362 
19363 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19364 		new_ill->ill_isv6 = ill->ill_isv6;
19365 		new_ill->next = *ills;
19366 		*ills = new_ill;
19367 	} else if (!sadb_resync) {
19368 		/* not resync'ing SADB and an entry exists for this ill */
19369 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19370 		return;
19371 	}
19372 
19373 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19374 
19375 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19376 		/*
19377 		 * IPsec module for protocol loaded, initiate dump
19378 		 * of the SADB to this ill.
19379 		 */
19380 		sadb_ill_download(ill, sa_type);
19381 }
19382 
19383 /*
19384  * Remove an ill from the list of IPsec capable ills.
19385  */
19386 static void
19387 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19388 {
19389 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19390 	ip_stack_t	*ipst = ill->ill_ipst;
19391 
19392 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19393 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19394 
19395 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19396 	    &ipst->ips_ipsec_capab_ills_esp;
19397 
19398 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19399 
19400 	prev_ill = NULL;
19401 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19402 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19403 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19404 		;
19405 	if (cur_ill == NULL) {
19406 		/* entry not found */
19407 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19408 		return;
19409 	}
19410 	if (prev_ill == NULL) {
19411 		/* entry at front of list */
19412 		*ills = NULL;
19413 	} else {
19414 		prev_ill->next = cur_ill->next;
19415 	}
19416 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19417 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19418 }
19419 
19420 /*
19421  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19422  * supporting the specified IPsec protocol acceleration.
19423  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19424  * We free the mblk and, if sa is non-null, release the held referece.
19425  */
19426 void
19427 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19428     netstack_t *ns)
19429 {
19430 	ipsec_capab_ill_t *ici, *cur_ici;
19431 	ill_t *ill;
19432 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19433 	ip_stack_t	*ipst = ns->netstack_ip;
19434 
19435 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19436 	    ipst->ips_ipsec_capab_ills_esp;
19437 
19438 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19439 
19440 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19441 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19442 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19443 
19444 		/*
19445 		 * Handle the case where the ill goes away while the SADB is
19446 		 * attempting to send messages.  If it's going away, it's
19447 		 * nuking its shadow SADB, so we don't care..
19448 		 */
19449 
19450 		if (ill == NULL)
19451 			continue;
19452 
19453 		if (sa != NULL) {
19454 			/*
19455 			 * Make sure capabilities match before
19456 			 * sending SA to ill.
19457 			 */
19458 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19459 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19460 				ill_refrele(ill);
19461 				continue;
19462 			}
19463 
19464 			mutex_enter(&sa->ipsa_lock);
19465 			sa->ipsa_flags |= IPSA_F_HW;
19466 			mutex_exit(&sa->ipsa_lock);
19467 		}
19468 
19469 		/*
19470 		 * Copy template message, and add it to the front
19471 		 * of the mblk ship list. We want to avoid holding
19472 		 * the ipsec_capab_ills_lock while sending the
19473 		 * message to the ills.
19474 		 *
19475 		 * The b_next and b_prev are temporarily used
19476 		 * to build a list of mblks to be sent down, and to
19477 		 * save the ill to which they must be sent.
19478 		 */
19479 		nmp = copymsg(mp);
19480 		if (nmp == NULL) {
19481 			ill_refrele(ill);
19482 			continue;
19483 		}
19484 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19485 		nmp->b_next = mp_ship_list;
19486 		mp_ship_list = nmp;
19487 		nmp->b_prev = (mblk_t *)ill;
19488 	}
19489 
19490 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19491 
19492 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19493 		/* restore the mblk to a sane state */
19494 		next_mp = nmp->b_next;
19495 		nmp->b_next = NULL;
19496 		ill = (ill_t *)nmp->b_prev;
19497 		nmp->b_prev = NULL;
19498 
19499 		ill_dlpi_send(ill, nmp);
19500 		ill_refrele(ill);
19501 	}
19502 
19503 	if (sa != NULL)
19504 		IPSA_REFRELE(sa);
19505 	freemsg(mp);
19506 }
19507 
19508 /*
19509  * Derive an interface id from the link layer address.
19510  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19511  */
19512 static boolean_t
19513 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19514 {
19515 	char		*addr;
19516 
19517 	if (ill->ill_phys_addr_length != ETHERADDRL)
19518 		return (B_FALSE);
19519 
19520 	/* Form EUI-64 like address */
19521 	addr = (char *)&v6addr->s6_addr32[2];
19522 	bcopy(ill->ill_phys_addr, addr, 3);
19523 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19524 	addr[3] = (char)0xff;
19525 	addr[4] = (char)0xfe;
19526 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19527 	return (B_TRUE);
19528 }
19529 
19530 /* ARGSUSED */
19531 static boolean_t
19532 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19533 {
19534 	return (B_FALSE);
19535 }
19536 
19537 typedef struct ipmp_ifcookie {
19538 	uint32_t	ic_hostid;
19539 	char		ic_ifname[LIFNAMSIZ];
19540 	char		ic_zonename[ZONENAME_MAX];
19541 } ipmp_ifcookie_t;
19542 
19543 /*
19544  * Construct a pseudo-random interface ID for the IPMP interface that's both
19545  * predictable and (almost) guaranteed to be unique.
19546  */
19547 static boolean_t
19548 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19549 {
19550 	zone_t		*zp;
19551 	uint8_t		*addr;
19552 	uchar_t		hash[16];
19553 	ulong_t 	hostid;
19554 	MD5_CTX		ctx;
19555 	ipmp_ifcookie_t	ic = { 0 };
19556 
19557 	ASSERT(IS_IPMP(ill));
19558 
19559 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19560 	ic.ic_hostid = htonl((uint32_t)hostid);
19561 
19562 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19563 
19564 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19565 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19566 		zone_rele(zp);
19567 	}
19568 
19569 	MD5Init(&ctx);
19570 	MD5Update(&ctx, &ic, sizeof (ic));
19571 	MD5Final(hash, &ctx);
19572 
19573 	/*
19574 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19575 	 */
19576 	addr = &v6addr->s6_addr8[8];
19577 	bcopy(hash + 8, addr, sizeof (uint64_t));
19578 	addr[0] &= ~0x2;				/* set local bit */
19579 
19580 	return (B_TRUE);
19581 }
19582 
19583 /* ARGSUSED */
19584 static boolean_t
19585 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19586     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19587 {
19588 	/*
19589 	 * Multicast address mappings used over Ethernet/802.X.
19590 	 * This address is used as a base for mappings.
19591 	 */
19592 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19593 	    0x00, 0x00, 0x00};
19594 
19595 	/*
19596 	 * Extract low order 32 bits from IPv6 multicast address.
19597 	 * Or that into the link layer address, starting from the
19598 	 * second byte.
19599 	 */
19600 	*hw_start = 2;
19601 	v6_extract_mask->s6_addr32[0] = 0;
19602 	v6_extract_mask->s6_addr32[1] = 0;
19603 	v6_extract_mask->s6_addr32[2] = 0;
19604 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19605 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19606 	return (B_TRUE);
19607 }
19608 
19609 /*
19610  * Indicate by return value whether multicast is supported. If not,
19611  * this code should not touch/change any parameters.
19612  */
19613 /* ARGSUSED */
19614 static boolean_t
19615 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19616     uint32_t *hw_start, ipaddr_t *extract_mask)
19617 {
19618 	/*
19619 	 * Multicast address mappings used over Ethernet/802.X.
19620 	 * This address is used as a base for mappings.
19621 	 */
19622 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19623 	    0x00, 0x00, 0x00 };
19624 
19625 	if (phys_length != ETHERADDRL)
19626 		return (B_FALSE);
19627 
19628 	*extract_mask = htonl(0x007fffff);
19629 	*hw_start = 2;
19630 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19631 	return (B_TRUE);
19632 }
19633 
19634 /*
19635  * Derive IPoIB interface id from the link layer address.
19636  */
19637 static boolean_t
19638 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19639 {
19640 	char		*addr;
19641 
19642 	if (ill->ill_phys_addr_length != 20)
19643 		return (B_FALSE);
19644 	addr = (char *)&v6addr->s6_addr32[2];
19645 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19646 	/*
19647 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19648 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19649 	 * rules. In these cases, the IBA considers these GUIDs to be in
19650 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19651 	 * required; vendors are required not to assign global EUI-64's
19652 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19653 	 * of the interface identifier. Whether the GUID is in modified
19654 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19655 	 * bit set to 1.
19656 	 */
19657 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19658 	return (B_TRUE);
19659 }
19660 
19661 /*
19662  * Note on mapping from multicast IP addresses to IPoIB multicast link
19663  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19664  * The format of an IPoIB multicast address is:
19665  *
19666  *  4 byte QPN      Scope Sign.  Pkey
19667  * +--------------------------------------------+
19668  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19669  * +--------------------------------------------+
19670  *
19671  * The Scope and Pkey components are properties of the IBA port and
19672  * network interface. They can be ascertained from the broadcast address.
19673  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19674  */
19675 
19676 static boolean_t
19677 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19678     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19679 {
19680 	/*
19681 	 * Base IPoIB IPv6 multicast address used for mappings.
19682 	 * Does not contain the IBA scope/Pkey values.
19683 	 */
19684 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19685 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19686 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19687 
19688 	/*
19689 	 * Extract low order 80 bits from IPv6 multicast address.
19690 	 * Or that into the link layer address, starting from the
19691 	 * sixth byte.
19692 	 */
19693 	*hw_start = 6;
19694 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19695 
19696 	/*
19697 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19698 	 */
19699 	*(maddr + 5) = *(bphys_addr + 5);
19700 	*(maddr + 8) = *(bphys_addr + 8);
19701 	*(maddr + 9) = *(bphys_addr + 9);
19702 
19703 	v6_extract_mask->s6_addr32[0] = 0;
19704 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19705 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19706 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19707 	return (B_TRUE);
19708 }
19709 
19710 static boolean_t
19711 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19712     uint32_t *hw_start, ipaddr_t *extract_mask)
19713 {
19714 	/*
19715 	 * Base IPoIB IPv4 multicast address used for mappings.
19716 	 * Does not contain the IBA scope/Pkey values.
19717 	 */
19718 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19719 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19720 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19721 
19722 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19723 		return (B_FALSE);
19724 
19725 	/*
19726 	 * Extract low order 28 bits from IPv4 multicast address.
19727 	 * Or that into the link layer address, starting from the
19728 	 * sixteenth byte.
19729 	 */
19730 	*extract_mask = htonl(0x0fffffff);
19731 	*hw_start = 16;
19732 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19733 
19734 	/*
19735 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19736 	 */
19737 	*(maddr + 5) = *(bphys_addr + 5);
19738 	*(maddr + 8) = *(bphys_addr + 8);
19739 	*(maddr + 9) = *(bphys_addr + 9);
19740 	return (B_TRUE);
19741 }
19742 
19743 /*
19744  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19745  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19746  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19747  * the link-local address is preferred.
19748  */
19749 boolean_t
19750 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19751 {
19752 	ipif_t	*ipif;
19753 	ipif_t	*maybe_ipif = NULL;
19754 
19755 	mutex_enter(&ill->ill_lock);
19756 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19757 		mutex_exit(&ill->ill_lock);
19758 		if (ipifp != NULL)
19759 			*ipifp = NULL;
19760 		return (B_FALSE);
19761 	}
19762 
19763 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19764 		if (!IPIF_CAN_LOOKUP(ipif))
19765 			continue;
19766 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19767 		    ipif->ipif_zoneid != ALL_ZONES)
19768 			continue;
19769 		if ((ipif->ipif_flags & flags) != flags)
19770 			continue;
19771 
19772 		if (ipifp == NULL) {
19773 			mutex_exit(&ill->ill_lock);
19774 			ASSERT(maybe_ipif == NULL);
19775 			return (B_TRUE);
19776 		}
19777 		if (!ill->ill_isv6 ||
19778 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19779 			ipif_refhold_locked(ipif);
19780 			mutex_exit(&ill->ill_lock);
19781 			*ipifp = ipif;
19782 			return (B_TRUE);
19783 		}
19784 		if (maybe_ipif == NULL)
19785 			maybe_ipif = ipif;
19786 	}
19787 	if (ipifp != NULL) {
19788 		if (maybe_ipif != NULL)
19789 			ipif_refhold_locked(maybe_ipif);
19790 		*ipifp = maybe_ipif;
19791 	}
19792 	mutex_exit(&ill->ill_lock);
19793 	return (maybe_ipif != NULL);
19794 }
19795 
19796 /*
19797  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19798  * If a pointer to an ipif_t is returned then the caller will need to do
19799  * an ill_refrele().
19800  */
19801 ipif_t *
19802 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19803     ip_stack_t *ipst)
19804 {
19805 	ipif_t *ipif;
19806 	ill_t *ill;
19807 
19808 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19809 	    ipst);
19810 	if (ill == NULL)
19811 		return (NULL);
19812 
19813 	mutex_enter(&ill->ill_lock);
19814 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19815 		mutex_exit(&ill->ill_lock);
19816 		ill_refrele(ill);
19817 		return (NULL);
19818 	}
19819 
19820 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19821 		if (!IPIF_CAN_LOOKUP(ipif))
19822 			continue;
19823 		if (lifidx == ipif->ipif_id) {
19824 			ipif_refhold_locked(ipif);
19825 			break;
19826 		}
19827 	}
19828 
19829 	mutex_exit(&ill->ill_lock);
19830 	ill_refrele(ill);
19831 	return (ipif);
19832 }
19833 
19834 /*
19835  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19836  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19837  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19838  * for details.
19839  */
19840 void
19841 ill_fastpath_flush(ill_t *ill)
19842 {
19843 	ip_stack_t *ipst = ill->ill_ipst;
19844 
19845 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19846 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19847 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19848 }
19849 
19850 /*
19851  * Set the physical address information for `ill' to the contents of the
19852  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
19853  * asynchronous if `ill' cannot immediately be quiesced -- in which case
19854  * EINPROGRESS will be returned.
19855  */
19856 int
19857 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
19858 {
19859 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19860 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
19861 
19862 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19863 
19864 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
19865 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
19866 		/* Changing DL_IPV6_TOKEN is not yet supported */
19867 		return (0);
19868 	}
19869 
19870 	/*
19871 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
19872 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
19873 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
19874 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
19875 	 */
19876 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
19877 		freemsg(mp);
19878 		return (ENOMEM);
19879 	}
19880 
19881 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19882 
19883 	/*
19884 	 * If we can quiesce the ill, then set the address.  If not, then
19885 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
19886 	 */
19887 	ill_down_ipifs(ill, B_TRUE);
19888 	mutex_enter(&ill->ill_lock);
19889 	if (!ill_is_quiescent(ill)) {
19890 		/* call cannot fail since `conn_t *' argument is NULL */
19891 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19892 		    mp, ILL_DOWN);
19893 		mutex_exit(&ill->ill_lock);
19894 		return (EINPROGRESS);
19895 	}
19896 	mutex_exit(&ill->ill_lock);
19897 
19898 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
19899 	return (0);
19900 }
19901 
19902 /*
19903  * Once the ill associated with `q' has quiesced, set its physical address
19904  * information to the values in `addrmp'.  Note that two copies of `addrmp'
19905  * are passed (linked by b_cont), since we sometimes need to save two distinct
19906  * copies in the ill_t, and our context doesn't permit sleeping or allocation
19907  * failure (we'll free the other copy if it's not needed).  Since the ill_t
19908  * is quiesced, we know any stale IREs with the old address information have
19909  * already been removed, so we don't need to call ill_fastpath_flush().
19910  */
19911 /* ARGSUSED */
19912 static void
19913 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
19914 {
19915 	ill_t		*ill = q->q_ptr;
19916 	mblk_t		*addrmp2 = unlinkb(addrmp);
19917 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
19918 	uint_t		addrlen, addroff;
19919 
19920 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19921 
19922 	addroff	= dlindp->dl_addr_offset;
19923 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
19924 
19925 	switch (dlindp->dl_data) {
19926 	case DL_IPV6_LINK_LAYER_ADDR:
19927 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
19928 		freemsg(addrmp2);
19929 		break;
19930 
19931 	case DL_CURR_PHYS_ADDR:
19932 		freemsg(ill->ill_phys_addr_mp);
19933 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
19934 		ill->ill_phys_addr_mp = addrmp;
19935 		ill->ill_phys_addr_length = addrlen;
19936 
19937 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
19938 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
19939 		else
19940 			freemsg(addrmp2);
19941 		break;
19942 	default:
19943 		ASSERT(0);
19944 	}
19945 
19946 	/*
19947 	 * If there are ipifs to bring up, ill_up_ipifs() will return
19948 	 * EINPROGRESS, and ipsq_current_finish() will be called by
19949 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
19950 	 * brought up.
19951 	 */
19952 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
19953 		ipsq_current_finish(ipsq);
19954 }
19955 
19956 /*
19957  * Helper routine for setting the ill_nd_lla fields.
19958  */
19959 void
19960 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
19961 {
19962 	freemsg(ill->ill_nd_lla_mp);
19963 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
19964 	ill->ill_nd_lla_mp = ndmp;
19965 	ill->ill_nd_lla_len = addrlen;
19966 }
19967 
19968 /*
19969  * Replumb the ill.
19970  */
19971 int
19972 ill_replumb(ill_t *ill, mblk_t *mp)
19973 {
19974 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19975 
19976 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19977 
19978 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19979 
19980 	/*
19981 	 * If we can quiesce the ill, then continue.  If not, then
19982 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
19983 	 */
19984 	ill_down_ipifs(ill, B_FALSE);
19985 
19986 	mutex_enter(&ill->ill_lock);
19987 	if (!ill_is_quiescent(ill)) {
19988 		/* call cannot fail since `conn_t *' argument is NULL */
19989 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19990 		    mp, ILL_DOWN);
19991 		mutex_exit(&ill->ill_lock);
19992 		return (EINPROGRESS);
19993 	}
19994 	mutex_exit(&ill->ill_lock);
19995 
19996 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
19997 	return (0);
19998 }
19999 
20000 /* ARGSUSED */
20001 static void
20002 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
20003 {
20004 	ill_t *ill = q->q_ptr;
20005 
20006 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20007 
20008 	ill_down_ipifs_tail(ill);
20009 
20010 	freemsg(ill->ill_replumb_mp);
20011 	ill->ill_replumb_mp = copyb(mp);
20012 
20013 	/*
20014 	 * Successfully quiesced and brought down the interface, now we send
20015 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
20016 	 * DL_NOTE_REPLUMB message.
20017 	 */
20018 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
20019 	    DL_NOTIFY_CONF);
20020 	ASSERT(mp != NULL);
20021 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
20022 	    DL_NOTE_REPLUMB_DONE;
20023 	ill_dlpi_send(ill, mp);
20024 
20025 	/*
20026 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20027 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20028 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20029 	 * brought up.
20030 	 */
20031 	if (ill->ill_replumb_mp == NULL ||
20032 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) != EINPROGRESS) {
20033 		ipsq_current_finish(ipsq);
20034 	}
20035 }
20036 
20037 major_t IP_MAJ;
20038 #define	IP	"ip"
20039 
20040 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20041 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20042 
20043 /*
20044  * Issue REMOVEIF ioctls to have the loopback interfaces
20045  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20046  * the former going away when the user-level processes in the zone
20047  * are killed  * and the latter are cleaned up by the stream head
20048  * str_stack_shutdown callback that undoes all I_PLINKs.
20049  */
20050 void
20051 ip_loopback_cleanup(ip_stack_t *ipst)
20052 {
20053 	int error;
20054 	ldi_handle_t	lh = NULL;
20055 	ldi_ident_t	li = NULL;
20056 	int		rval;
20057 	cred_t		*cr;
20058 	struct strioctl iocb;
20059 	struct lifreq	lifreq;
20060 
20061 	IP_MAJ = ddi_name_to_major(IP);
20062 
20063 #ifdef NS_DEBUG
20064 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20065 	    ipst->ips_netstack->netstack_stackid);
20066 #endif
20067 
20068 	bzero(&lifreq, sizeof (lifreq));
20069 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20070 
20071 	error = ldi_ident_from_major(IP_MAJ, &li);
20072 	if (error) {
20073 #ifdef DEBUG
20074 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20075 		    error);
20076 #endif
20077 		return;
20078 	}
20079 
20080 	cr = zone_get_kcred(netstackid_to_zoneid(
20081 	    ipst->ips_netstack->netstack_stackid));
20082 	ASSERT(cr != NULL);
20083 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20084 	if (error) {
20085 #ifdef DEBUG
20086 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20087 		    error);
20088 #endif
20089 		goto out;
20090 	}
20091 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20092 	iocb.ic_timout = 15;
20093 	iocb.ic_len = sizeof (lifreq);
20094 	iocb.ic_dp = (char *)&lifreq;
20095 
20096 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20097 	/* LINTED - statement has no consequent */
20098 	if (error) {
20099 #ifdef NS_DEBUG
20100 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20101 		    "UDP6 error %d\n", error);
20102 #endif
20103 	}
20104 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20105 	lh = NULL;
20106 
20107 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20108 	if (error) {
20109 #ifdef NS_DEBUG
20110 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20111 		    error);
20112 #endif
20113 		goto out;
20114 	}
20115 
20116 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20117 	iocb.ic_timout = 15;
20118 	iocb.ic_len = sizeof (lifreq);
20119 	iocb.ic_dp = (char *)&lifreq;
20120 
20121 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20122 	/* LINTED - statement has no consequent */
20123 	if (error) {
20124 #ifdef NS_DEBUG
20125 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20126 		    "UDP error %d\n", error);
20127 #endif
20128 	}
20129 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20130 	lh = NULL;
20131 
20132 out:
20133 	/* Close layered handles */
20134 	if (lh)
20135 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20136 	if (li)
20137 		ldi_ident_release(li);
20138 
20139 	crfree(cr);
20140 }
20141 
20142 /*
20143  * This needs to be in-sync with nic_event_t definition
20144  */
20145 static const char *
20146 ill_hook_event2str(nic_event_t event)
20147 {
20148 	switch (event) {
20149 	case NE_PLUMB:
20150 		return ("PLUMB");
20151 	case NE_UNPLUMB:
20152 		return ("UNPLUMB");
20153 	case NE_UP:
20154 		return ("UP");
20155 	case NE_DOWN:
20156 		return ("DOWN");
20157 	case NE_ADDRESS_CHANGE:
20158 		return ("ADDRESS_CHANGE");
20159 	case NE_LIF_UP:
20160 		return ("LIF_UP");
20161 	case NE_LIF_DOWN:
20162 		return ("LIF_DOWN");
20163 	default:
20164 		return ("UNKNOWN");
20165 	}
20166 }
20167 
20168 void
20169 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20170     nic_event_data_t data, size_t datalen)
20171 {
20172 	ip_stack_t		*ipst = ill->ill_ipst;
20173 	hook_nic_event_int_t	*info;
20174 	const char		*str = NULL;
20175 
20176 	/* create a new nic event info */
20177 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20178 		goto fail;
20179 
20180 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20181 	info->hnei_event.hne_lif = lif;
20182 	info->hnei_event.hne_event = event;
20183 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20184 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20185 	info->hnei_event.hne_data = NULL;
20186 	info->hnei_event.hne_datalen = 0;
20187 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20188 
20189 	if (data != NULL && datalen != 0) {
20190 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20191 		if (info->hnei_event.hne_data == NULL)
20192 			goto fail;
20193 		bcopy(data, info->hnei_event.hne_data, datalen);
20194 		info->hnei_event.hne_datalen = datalen;
20195 	}
20196 
20197 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20198 	    DDI_NOSLEEP) == DDI_SUCCESS)
20199 		return;
20200 
20201 fail:
20202 	if (info != NULL) {
20203 		if (info->hnei_event.hne_data != NULL) {
20204 			kmem_free(info->hnei_event.hne_data,
20205 			    info->hnei_event.hne_datalen);
20206 		}
20207 		kmem_free(info, sizeof (hook_nic_event_t));
20208 	}
20209 	str = ill_hook_event2str(event);
20210 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20211 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20212 }
20213 
20214 void
20215 ipif_up_notify(ipif_t *ipif)
20216 {
20217 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20218 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20219 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20220 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20221 	    NE_LIF_UP, NULL, 0);
20222 }
20223