xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 4372c2457c83ba786dcbd57c9896aa05174f2673)
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 	ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
3732 }
3733 
3734 /*
3735  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
3736  * socket messages for each interface whose flags we change.
3737  */
3738 int
3739 ill_forward_set(ill_t *ill, boolean_t enable)
3740 {
3741 	ipmp_illgrp_t *illg;
3742 	ip_stack_t *ipst = ill->ill_ipst;
3743 
3744 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3745 
3746 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3747 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3748 		return (0);
3749 
3750 	if (IS_LOOPBACK(ill))
3751 		return (EINVAL);
3752 
3753 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
3754 		/*
3755 		 * Update all of the interfaces in the group.
3756 		 */
3757 		illg = ill->ill_grp;
3758 		ill = list_head(&illg->ig_if);
3759 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
3760 			ill_forward_set_on_ill(ill, enable);
3761 
3762 		/*
3763 		 * Update the IPMP meta-interface.
3764 		 */
3765 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
3766 		return (0);
3767 	}
3768 
3769 	ill_forward_set_on_ill(ill, enable);
3770 	return (0);
3771 }
3772 
3773 /*
3774  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3775  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3776  * set or clear.
3777  */
3778 static void
3779 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3780 {
3781 	ipif_t *ipif;
3782 	nce_t *nce;
3783 
3784 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3785 		/*
3786 		 * NOTE: we match across the illgrp because nce's for
3787 		 * addresses on IPMP interfaces have an nce_ill that points to
3788 		 * the bound underlying ill.
3789 		 */
3790 		nce = ndp_lookup_v6(ill, B_TRUE, &ipif->ipif_v6lcl_addr,
3791 		    B_FALSE);
3792 		if (nce != NULL) {
3793 			mutex_enter(&nce->nce_lock);
3794 			if (enable)
3795 				nce->nce_flags |= NCE_F_ISROUTER;
3796 			else
3797 				nce->nce_flags &= ~NCE_F_ISROUTER;
3798 			mutex_exit(&nce->nce_lock);
3799 			NCE_REFRELE(nce);
3800 		}
3801 	}
3802 }
3803 
3804 /*
3805  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3806  * for this ill.  Make sure the v6/v4 question has been answered about this
3807  * ill.  The creation of this ndd variable is only for backwards compatibility.
3808  * The preferred way to control per-interface IP forwarding is through the
3809  * ILLF_ROUTER interface flag.
3810  */
3811 static int
3812 ill_set_ndd_name(ill_t *ill)
3813 {
3814 	char *suffix;
3815 	ip_stack_t	*ipst = ill->ill_ipst;
3816 
3817 	ASSERT(IAM_WRITER_ILL(ill));
3818 
3819 	if (ill->ill_isv6)
3820 		suffix = ipv6_forward_suffix;
3821 	else
3822 		suffix = ipv4_forward_suffix;
3823 
3824 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3825 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3826 	/*
3827 	 * Copies over the '\0'.
3828 	 * Note that strlen(suffix) is always bounded.
3829 	 */
3830 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3831 	    strlen(suffix) + 1);
3832 
3833 	/*
3834 	 * Use of the nd table requires holding the reader lock.
3835 	 * Modifying the nd table thru nd_load/nd_unload requires
3836 	 * the writer lock.
3837 	 */
3838 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3839 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3840 	    nd_ill_forward_set, (caddr_t)ill)) {
3841 		/*
3842 		 * If the nd_load failed, it only meant that it could not
3843 		 * allocate a new bunch of room for further NDD expansion.
3844 		 * Because of that, the ill_ndd_name will be set to 0, and
3845 		 * this interface is at the mercy of the global ip_forwarding
3846 		 * variable.
3847 		 */
3848 		rw_exit(&ipst->ips_ip_g_nd_lock);
3849 		ill->ill_ndd_name = NULL;
3850 		return (ENOMEM);
3851 	}
3852 	rw_exit(&ipst->ips_ip_g_nd_lock);
3853 	return (0);
3854 }
3855 
3856 /*
3857  * Intializes the context structure and returns the first ill in the list
3858  * cuurently start_list and end_list can have values:
3859  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3860  * IP_V4_G_HEAD		Traverse IPV4 list only.
3861  * IP_V6_G_HEAD		Traverse IPV6 list only.
3862  */
3863 
3864 /*
3865  * We don't check for CONDEMNED ills here. Caller must do that if
3866  * necessary under the ill lock.
3867  */
3868 ill_t *
3869 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3870     ip_stack_t *ipst)
3871 {
3872 	ill_if_t *ifp;
3873 	ill_t *ill;
3874 	avl_tree_t *avl_tree;
3875 
3876 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3877 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3878 
3879 	/*
3880 	 * setup the lists to search
3881 	 */
3882 	if (end_list != MAX_G_HEADS) {
3883 		ctx->ctx_current_list = start_list;
3884 		ctx->ctx_last_list = end_list;
3885 	} else {
3886 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3887 		ctx->ctx_current_list = 0;
3888 	}
3889 
3890 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3891 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3892 		if (ifp != (ill_if_t *)
3893 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3894 			avl_tree = &ifp->illif_avl_by_ppa;
3895 			ill = avl_first(avl_tree);
3896 			/*
3897 			 * ill is guaranteed to be non NULL or ifp should have
3898 			 * not existed.
3899 			 */
3900 			ASSERT(ill != NULL);
3901 			return (ill);
3902 		}
3903 		ctx->ctx_current_list++;
3904 	}
3905 
3906 	return (NULL);
3907 }
3908 
3909 /*
3910  * returns the next ill in the list. ill_first() must have been called
3911  * before calling ill_next() or bad things will happen.
3912  */
3913 
3914 /*
3915  * We don't check for CONDEMNED ills here. Caller must do that if
3916  * necessary under the ill lock.
3917  */
3918 ill_t *
3919 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3920 {
3921 	ill_if_t *ifp;
3922 	ill_t *ill;
3923 	ip_stack_t	*ipst = lastill->ill_ipst;
3924 
3925 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3926 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3927 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3928 	    AVL_AFTER)) != NULL) {
3929 		return (ill);
3930 	}
3931 
3932 	/* goto next ill_ifp in the list. */
3933 	ifp = lastill->ill_ifptr->illif_next;
3934 
3935 	/* make sure not at end of circular list */
3936 	while (ifp ==
3937 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3938 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3939 			return (NULL);
3940 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3941 	}
3942 
3943 	return (avl_first(&ifp->illif_avl_by_ppa));
3944 }
3945 
3946 /*
3947  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
3948  * The final number (PPA) must not have any leading zeros.  Upon success, a
3949  * pointer to the start of the PPA is returned; otherwise NULL is returned.
3950  */
3951 static char *
3952 ill_get_ppa_ptr(char *name)
3953 {
3954 	int namelen = strlen(name);
3955 	int end_ndx = namelen - 1;
3956 	int ppa_ndx, i;
3957 
3958 	/*
3959 	 * Check that the first character is [a-zA-Z], and that the last
3960 	 * character is [0-9].
3961 	 */
3962 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
3963 		return (NULL);
3964 
3965 	/*
3966 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
3967 	 */
3968 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
3969 		if (!isdigit(name[ppa_ndx - 1]))
3970 			break;
3971 
3972 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
3973 		return (NULL);
3974 
3975 	/*
3976 	 * Check that the intermediate characters are [a-z0-9.]
3977 	 */
3978 	for (i = 1; i < ppa_ndx; i++) {
3979 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
3980 		    name[i] != '.' && name[i] != '_') {
3981 			return (NULL);
3982 		}
3983 	}
3984 
3985 	return (name + ppa_ndx);
3986 }
3987 
3988 /*
3989  * use avl tree to locate the ill.
3990  */
3991 static ill_t *
3992 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3993     ipsq_func_t func, int *error, ip_stack_t *ipst)
3994 {
3995 	char *ppa_ptr = NULL;
3996 	int len;
3997 	uint_t ppa;
3998 	ill_t *ill = NULL;
3999 	ill_if_t *ifp;
4000 	int list;
4001 	ipsq_t *ipsq;
4002 
4003 	if (error != NULL)
4004 		*error = 0;
4005 
4006 	/*
4007 	 * get ppa ptr
4008 	 */
4009 	if (isv6)
4010 		list = IP_V6_G_HEAD;
4011 	else
4012 		list = IP_V4_G_HEAD;
4013 
4014 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4015 		if (error != NULL)
4016 			*error = ENXIO;
4017 		return (NULL);
4018 	}
4019 
4020 	len = ppa_ptr - name + 1;
4021 
4022 	ppa = stoi(&ppa_ptr);
4023 
4024 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4025 
4026 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4027 		/*
4028 		 * match is done on len - 1 as the name is not null
4029 		 * terminated it contains ppa in addition to the interface
4030 		 * name.
4031 		 */
4032 		if ((ifp->illif_name_len == len) &&
4033 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4034 			break;
4035 		} else {
4036 			ifp = ifp->illif_next;
4037 		}
4038 	}
4039 
4040 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4041 		/*
4042 		 * Even the interface type does not exist.
4043 		 */
4044 		if (error != NULL)
4045 			*error = ENXIO;
4046 		return (NULL);
4047 	}
4048 
4049 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4050 	if (ill != NULL) {
4051 		/*
4052 		 * The block comment at the start of ipif_down
4053 		 * explains the use of the macros used below
4054 		 */
4055 		GRAB_CONN_LOCK(q);
4056 		mutex_enter(&ill->ill_lock);
4057 		if (ILL_CAN_LOOKUP(ill)) {
4058 			ill_refhold_locked(ill);
4059 			mutex_exit(&ill->ill_lock);
4060 			RELEASE_CONN_LOCK(q);
4061 			return (ill);
4062 		} else if (ILL_CAN_WAIT(ill, q)) {
4063 			ipsq = ill->ill_phyint->phyint_ipsq;
4064 			mutex_enter(&ipsq->ipsq_lock);
4065 			mutex_enter(&ipsq->ipsq_xop->ipx_lock);
4066 			mutex_exit(&ill->ill_lock);
4067 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4068 			mutex_exit(&ipsq->ipsq_xop->ipx_lock);
4069 			mutex_exit(&ipsq->ipsq_lock);
4070 			RELEASE_CONN_LOCK(q);
4071 			if (error != NULL)
4072 				*error = EINPROGRESS;
4073 			return (NULL);
4074 		}
4075 		mutex_exit(&ill->ill_lock);
4076 		RELEASE_CONN_LOCK(q);
4077 	}
4078 	if (error != NULL)
4079 		*error = ENXIO;
4080 	return (NULL);
4081 }
4082 
4083 /*
4084  * comparison function for use with avl.
4085  */
4086 static int
4087 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4088 {
4089 	uint_t ppa;
4090 	uint_t ill_ppa;
4091 
4092 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4093 
4094 	ppa = *((uint_t *)ppa_ptr);
4095 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4096 	/*
4097 	 * We want the ill with the lowest ppa to be on the
4098 	 * top.
4099 	 */
4100 	if (ill_ppa < ppa)
4101 		return (1);
4102 	if (ill_ppa > ppa)
4103 		return (-1);
4104 	return (0);
4105 }
4106 
4107 /*
4108  * remove an interface type from the global list.
4109  */
4110 static void
4111 ill_delete_interface_type(ill_if_t *interface)
4112 {
4113 	ASSERT(interface != NULL);
4114 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4115 
4116 	avl_destroy(&interface->illif_avl_by_ppa);
4117 	if (interface->illif_ppa_arena != NULL)
4118 		vmem_destroy(interface->illif_ppa_arena);
4119 
4120 	remque(interface);
4121 
4122 	mi_free(interface);
4123 }
4124 
4125 /*
4126  * remove ill from the global list.
4127  */
4128 static void
4129 ill_glist_delete(ill_t *ill)
4130 {
4131 	ip_stack_t	*ipst;
4132 	phyint_t	*phyi;
4133 
4134 	if (ill == NULL)
4135 		return;
4136 	ipst = ill->ill_ipst;
4137 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4138 
4139 	/*
4140 	 * If the ill was never inserted into the AVL tree
4141 	 * we skip the if branch.
4142 	 */
4143 	if (ill->ill_ifptr != NULL) {
4144 		/*
4145 		 * remove from AVL tree and free ppa number
4146 		 */
4147 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4148 
4149 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4150 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4151 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4152 		}
4153 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4154 			ill_delete_interface_type(ill->ill_ifptr);
4155 		}
4156 
4157 		/*
4158 		 * Indicate ill is no longer in the list.
4159 		 */
4160 		ill->ill_ifptr = NULL;
4161 		ill->ill_name_length = 0;
4162 		ill->ill_name[0] = '\0';
4163 		ill->ill_ppa = UINT_MAX;
4164 	}
4165 
4166 	/* Generate one last event for this ill. */
4167 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4168 	    ill->ill_name_length);
4169 
4170 	ASSERT(ill->ill_phyint != NULL);
4171 	phyi = ill->ill_phyint;
4172 	ill->ill_phyint = NULL;
4173 
4174 	/*
4175 	 * ill_init allocates a phyint always to store the copy
4176 	 * of flags relevant to phyint. At that point in time, we could
4177 	 * not assign the name and hence phyint_illv4/v6 could not be
4178 	 * initialized. Later in ipif_set_values, we assign the name to
4179 	 * the ill, at which point in time we assign phyint_illv4/v6.
4180 	 * Thus we don't rely on phyint_illv6 to be initialized always.
4181 	 */
4182 	if (ill->ill_flags & ILLF_IPV6)
4183 		phyi->phyint_illv6 = NULL;
4184 	else
4185 		phyi->phyint_illv4 = NULL;
4186 
4187 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
4188 		rw_exit(&ipst->ips_ill_g_lock);
4189 		return;
4190 	}
4191 
4192 	/*
4193 	 * There are no ills left on this phyint; pull it out of the phyint
4194 	 * avl trees, and free it.
4195 	 */
4196 	if (phyi->phyint_ifindex > 0) {
4197 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4198 		    phyi);
4199 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4200 		    phyi);
4201 	}
4202 	rw_exit(&ipst->ips_ill_g_lock);
4203 
4204 	phyint_free(phyi);
4205 }
4206 
4207 /*
4208  * allocate a ppa, if the number of plumbed interfaces of this type are
4209  * less than ill_no_arena do a linear search to find a unused ppa.
4210  * When the number goes beyond ill_no_arena switch to using an arena.
4211  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4212  * is the return value for an error condition, so allocation starts at one
4213  * and is decremented by one.
4214  */
4215 static int
4216 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4217 {
4218 	ill_t *tmp_ill;
4219 	uint_t start, end;
4220 	int ppa;
4221 
4222 	if (ifp->illif_ppa_arena == NULL &&
4223 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4224 		/*
4225 		 * Create an arena.
4226 		 */
4227 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4228 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4229 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4230 			/* allocate what has already been assigned */
4231 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4232 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4233 		    tmp_ill, AVL_AFTER)) {
4234 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4235 			    1,		/* size */
4236 			    1,		/* align/quantum */
4237 			    0,		/* phase */
4238 			    0,		/* nocross */
4239 			    /* minaddr */
4240 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4241 			    /* maxaddr */
4242 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4243 			    VM_NOSLEEP|VM_FIRSTFIT);
4244 			if (ppa == 0) {
4245 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4246 				    " failed while switching"));
4247 				vmem_destroy(ifp->illif_ppa_arena);
4248 				ifp->illif_ppa_arena = NULL;
4249 				break;
4250 			}
4251 		}
4252 	}
4253 
4254 	if (ifp->illif_ppa_arena != NULL) {
4255 		if (ill->ill_ppa == UINT_MAX) {
4256 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4257 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4258 			if (ppa == 0)
4259 				return (EAGAIN);
4260 			ill->ill_ppa = --ppa;
4261 		} else {
4262 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4263 			    1, 		/* size */
4264 			    1, 		/* align/quantum */
4265 			    0, 		/* phase */
4266 			    0, 		/* nocross */
4267 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4268 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4269 			    VM_NOSLEEP|VM_FIRSTFIT);
4270 			/*
4271 			 * Most likely the allocation failed because
4272 			 * the requested ppa was in use.
4273 			 */
4274 			if (ppa == 0)
4275 				return (EEXIST);
4276 		}
4277 		return (0);
4278 	}
4279 
4280 	/*
4281 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4282 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4283 	 */
4284 	if (ill->ill_ppa == UINT_MAX) {
4285 		end = UINT_MAX - 1;
4286 		start = 0;
4287 	} else {
4288 		end = start = ill->ill_ppa;
4289 	}
4290 
4291 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4292 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4293 		if (start++ >= end) {
4294 			if (ill->ill_ppa == UINT_MAX)
4295 				return (EAGAIN);
4296 			else
4297 				return (EEXIST);
4298 		}
4299 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4300 	}
4301 	ill->ill_ppa = start;
4302 	return (0);
4303 }
4304 
4305 /*
4306  * Insert ill into the list of configured ill's. Once this function completes,
4307  * the ill is globally visible and is available through lookups. More precisely
4308  * this happens after the caller drops the ill_g_lock.
4309  */
4310 static int
4311 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4312 {
4313 	ill_if_t *ill_interface;
4314 	avl_index_t where = 0;
4315 	int error;
4316 	int name_length;
4317 	int index;
4318 	boolean_t check_length = B_FALSE;
4319 	ip_stack_t	*ipst = ill->ill_ipst;
4320 
4321 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4322 
4323 	name_length = mi_strlen(name) + 1;
4324 
4325 	if (isv6)
4326 		index = IP_V6_G_HEAD;
4327 	else
4328 		index = IP_V4_G_HEAD;
4329 
4330 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4331 	/*
4332 	 * Search for interface type based on name
4333 	 */
4334 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4335 		if ((ill_interface->illif_name_len == name_length) &&
4336 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4337 			break;
4338 		}
4339 		ill_interface = ill_interface->illif_next;
4340 	}
4341 
4342 	/*
4343 	 * Interface type not found, create one.
4344 	 */
4345 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4346 		ill_g_head_t ghead;
4347 
4348 		/*
4349 		 * allocate ill_if_t structure
4350 		 */
4351 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4352 		if (ill_interface == NULL) {
4353 			return (ENOMEM);
4354 		}
4355 
4356 		(void) strcpy(ill_interface->illif_name, name);
4357 		ill_interface->illif_name_len = name_length;
4358 
4359 		avl_create(&ill_interface->illif_avl_by_ppa,
4360 		    ill_compare_ppa, sizeof (ill_t),
4361 		    offsetof(struct ill_s, ill_avl_byppa));
4362 
4363 		/*
4364 		 * link the structure in the back to maintain order
4365 		 * of configuration for ifconfig output.
4366 		 */
4367 		ghead = ipst->ips_ill_g_heads[index];
4368 		insque(ill_interface, ghead.ill_g_list_tail);
4369 	}
4370 
4371 	if (ill->ill_ppa == UINT_MAX)
4372 		check_length = B_TRUE;
4373 
4374 	error = ill_alloc_ppa(ill_interface, ill);
4375 	if (error != 0) {
4376 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4377 			ill_delete_interface_type(ill->ill_ifptr);
4378 		return (error);
4379 	}
4380 
4381 	/*
4382 	 * When the ppa is choosen by the system, check that there is
4383 	 * enough space to insert ppa. if a specific ppa was passed in this
4384 	 * check is not required as the interface name passed in will have
4385 	 * the right ppa in it.
4386 	 */
4387 	if (check_length) {
4388 		/*
4389 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4390 		 */
4391 		char buf[sizeof (uint_t) * 3];
4392 
4393 		/*
4394 		 * convert ppa to string to calculate the amount of space
4395 		 * required for it in the name.
4396 		 */
4397 		numtos(ill->ill_ppa, buf);
4398 
4399 		/* Do we have enough space to insert ppa ? */
4400 
4401 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4402 			/* Free ppa and interface type struct */
4403 			if (ill_interface->illif_ppa_arena != NULL) {
4404 				vmem_free(ill_interface->illif_ppa_arena,
4405 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4406 			}
4407 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4408 				ill_delete_interface_type(ill->ill_ifptr);
4409 
4410 			return (EINVAL);
4411 		}
4412 	}
4413 
4414 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4415 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4416 
4417 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4418 	    &where);
4419 	ill->ill_ifptr = ill_interface;
4420 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4421 
4422 	ill_phyint_reinit(ill);
4423 	return (0);
4424 }
4425 
4426 /* Initialize the per phyint ipsq used for serialization */
4427 static boolean_t
4428 ipsq_init(ill_t *ill, boolean_t enter)
4429 {
4430 	ipsq_t  *ipsq;
4431 	ipxop_t	*ipx;
4432 
4433 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
4434 		return (B_FALSE);
4435 
4436 	ill->ill_phyint->phyint_ipsq = ipsq;
4437 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
4438 	ipx->ipx_ipsq = ipsq;
4439 	ipsq->ipsq_next = ipsq;
4440 	ipsq->ipsq_phyint = ill->ill_phyint;
4441 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4442 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
4443 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4444 	if (enter) {
4445 		ipx->ipx_writer = curthread;
4446 		ipx->ipx_forced = B_FALSE;
4447 		ipx->ipx_reentry_cnt = 1;
4448 #ifdef DEBUG
4449 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
4450 #endif
4451 	}
4452 	return (B_TRUE);
4453 }
4454 
4455 /*
4456  * ill_init is called by ip_open when a device control stream is opened.
4457  * It does a few initializations, and shoots a DL_INFO_REQ message down
4458  * to the driver.  The response is later picked up in ip_rput_dlpi and
4459  * used to set up default mechanisms for talking to the driver.  (Always
4460  * called as writer.)
4461  *
4462  * If this function returns error, ip_open will call ip_close which in
4463  * turn will call ill_delete to clean up any memory allocated here that
4464  * is not yet freed.
4465  */
4466 int
4467 ill_init(queue_t *q, ill_t *ill)
4468 {
4469 	int	count;
4470 	dl_info_req_t	*dlir;
4471 	mblk_t	*info_mp;
4472 	uchar_t *frag_ptr;
4473 
4474 	/*
4475 	 * The ill is initialized to zero by mi_alloc*(). In addition
4476 	 * some fields already contain valid values, initialized in
4477 	 * ip_open(), before we reach here.
4478 	 */
4479 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4480 
4481 	ill->ill_rq = q;
4482 	ill->ill_wq = WR(q);
4483 
4484 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4485 	    BPRI_HI);
4486 	if (info_mp == NULL)
4487 		return (ENOMEM);
4488 
4489 	/*
4490 	 * Allocate sufficient space to contain our fragment hash table and
4491 	 * the device name.
4492 	 */
4493 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4494 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4495 	if (frag_ptr == NULL) {
4496 		freemsg(info_mp);
4497 		return (ENOMEM);
4498 	}
4499 	ill->ill_frag_ptr = frag_ptr;
4500 	ill->ill_frag_free_num_pkts = 0;
4501 	ill->ill_last_frag_clean_time = 0;
4502 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4503 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4504 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4505 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4506 		    NULL, MUTEX_DEFAULT, NULL);
4507 	}
4508 
4509 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4510 	if (ill->ill_phyint == NULL) {
4511 		freemsg(info_mp);
4512 		mi_free(frag_ptr);
4513 		return (ENOMEM);
4514 	}
4515 
4516 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4517 	/*
4518 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4519 	 * at this point because of the following reason. If we can't
4520 	 * enter the ipsq at some point and cv_wait, the writer that
4521 	 * wakes us up tries to locate us using the list of all phyints
4522 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4523 	 * If we don't set it now, we risk a missed wakeup.
4524 	 */
4525 	ill->ill_phyint->phyint_illv4 = ill;
4526 	ill->ill_ppa = UINT_MAX;
4527 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4528 
4529 	if (!ipsq_init(ill, B_TRUE)) {
4530 		freemsg(info_mp);
4531 		mi_free(frag_ptr);
4532 		mi_free(ill->ill_phyint);
4533 		return (ENOMEM);
4534 	}
4535 
4536 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4537 
4538 	/* Frag queue limit stuff */
4539 	ill->ill_frag_count = 0;
4540 	ill->ill_ipf_gen = 0;
4541 
4542 	ill->ill_global_timer = INFINITY;
4543 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4544 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4545 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4546 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4547 
4548 	/*
4549 	 * Initialize IPv6 configuration variables.  The IP module is always
4550 	 * opened as an IPv4 module.  Instead tracking down the cases where
4551 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4552 	 * here for convenience, this has no effect until the ill is set to do
4553 	 * IPv6.
4554 	 */
4555 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4556 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4557 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4558 	ill->ill_max_buf = ND_MAX_Q;
4559 	ill->ill_refcnt = 0;
4560 
4561 	/* Send down the Info Request to the driver. */
4562 	info_mp->b_datap->db_type = M_PCPROTO;
4563 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4564 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4565 	dlir->dl_primitive = DL_INFO_REQ;
4566 
4567 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4568 
4569 	qprocson(q);
4570 	ill_dlpi_send(ill, info_mp);
4571 
4572 	return (0);
4573 }
4574 
4575 /*
4576  * ill_dls_info
4577  * creates datalink socket info from the device.
4578  */
4579 int
4580 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4581 {
4582 	size_t	len;
4583 	ill_t	*ill = ipif->ipif_ill;
4584 
4585 	sdl->sdl_family = AF_LINK;
4586 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4587 	sdl->sdl_type = ill->ill_type;
4588 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4589 	len = strlen(sdl->sdl_data);
4590 	ASSERT(len < 256);
4591 	sdl->sdl_nlen = (uchar_t)len;
4592 	sdl->sdl_alen = ill->ill_phys_addr_length;
4593 	sdl->sdl_slen = 0;
4594 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4595 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4596 
4597 	return (sizeof (struct sockaddr_dl));
4598 }
4599 
4600 /*
4601  * ill_xarp_info
4602  * creates xarp info from the device.
4603  */
4604 static int
4605 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4606 {
4607 	sdl->sdl_family = AF_LINK;
4608 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4609 	sdl->sdl_type = ill->ill_type;
4610 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4611 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4612 	sdl->sdl_alen = ill->ill_phys_addr_length;
4613 	sdl->sdl_slen = 0;
4614 	return (sdl->sdl_nlen);
4615 }
4616 
4617 static int
4618 loopback_kstat_update(kstat_t *ksp, int rw)
4619 {
4620 	kstat_named_t *kn;
4621 	netstackid_t	stackid;
4622 	netstack_t	*ns;
4623 	ip_stack_t	*ipst;
4624 
4625 	if (ksp == NULL || ksp->ks_data == NULL)
4626 		return (EIO);
4627 
4628 	if (rw == KSTAT_WRITE)
4629 		return (EACCES);
4630 
4631 	kn = KSTAT_NAMED_PTR(ksp);
4632 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4633 
4634 	ns = netstack_find_by_stackid(stackid);
4635 	if (ns == NULL)
4636 		return (-1);
4637 
4638 	ipst = ns->netstack_ip;
4639 	if (ipst == NULL) {
4640 		netstack_rele(ns);
4641 		return (-1);
4642 	}
4643 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4644 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4645 	netstack_rele(ns);
4646 	return (0);
4647 }
4648 
4649 /*
4650  * Has ifindex been plumbed already?
4651  */
4652 boolean_t
4653 phyint_exists(uint_t index, ip_stack_t *ipst)
4654 {
4655 	ASSERT(index != 0);
4656 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4657 
4658 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4659 	    &index, NULL) != NULL);
4660 }
4661 
4662 /* Pick a unique ifindex */
4663 boolean_t
4664 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4665 {
4666 	uint_t starting_index;
4667 
4668 	if (!ipst->ips_ill_index_wrap) {
4669 		*indexp = ipst->ips_ill_index++;
4670 		if (ipst->ips_ill_index == 0) {
4671 			/* Reached the uint_t limit Next time wrap  */
4672 			ipst->ips_ill_index_wrap = B_TRUE;
4673 		}
4674 		return (B_TRUE);
4675 	}
4676 
4677 	/*
4678 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4679 	 * at this point and don't want to call any function that attempts
4680 	 * to get the lock again.
4681 	 */
4682 	starting_index = ipst->ips_ill_index++;
4683 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4684 		if (ipst->ips_ill_index != 0 &&
4685 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4686 			/* found unused index - use it */
4687 			*indexp = ipst->ips_ill_index;
4688 			return (B_TRUE);
4689 		}
4690 	}
4691 
4692 	/*
4693 	 * all interface indicies are inuse.
4694 	 */
4695 	return (B_FALSE);
4696 }
4697 
4698 /*
4699  * Assign a unique interface index for the phyint.
4700  */
4701 static boolean_t
4702 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4703 {
4704 	ASSERT(phyi->phyint_ifindex == 0);
4705 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4706 }
4707 
4708 /*
4709  * Initialize the flags on `phyi' as per the provided mactype.
4710  */
4711 static void
4712 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
4713 {
4714 	uint64_t flags = 0;
4715 
4716 	/*
4717 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
4718 	 * we always presume the underlying hardware is working and set
4719 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
4720 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
4721 	 * there are no active interfaces in the group so we set PHYI_FAILED.
4722 	 */
4723 	if (mactype == SUNW_DL_IPMP)
4724 		flags |= PHYI_FAILED;
4725 	else
4726 		flags |= PHYI_RUNNING;
4727 
4728 	switch (mactype) {
4729 	case SUNW_DL_VNI:
4730 		flags |= PHYI_VIRTUAL;
4731 		break;
4732 	case SUNW_DL_IPMP:
4733 		flags |= PHYI_IPMP;
4734 		break;
4735 	case DL_LOOP:
4736 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
4737 		break;
4738 	}
4739 
4740 	mutex_enter(&phyi->phyint_lock);
4741 	phyi->phyint_flags |= flags;
4742 	mutex_exit(&phyi->phyint_lock);
4743 }
4744 
4745 /*
4746  * Return a pointer to the ill which matches the supplied name.  Note that
4747  * the ill name length includes the null termination character.  (May be
4748  * called as writer.)
4749  * If do_alloc and the interface is "lo0" it will be automatically created.
4750  * Cannot bump up reference on condemned ills. So dup detect can't be done
4751  * using this func.
4752  */
4753 ill_t *
4754 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4755     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4756     ip_stack_t *ipst)
4757 {
4758 	ill_t	*ill;
4759 	ipif_t	*ipif;
4760 	ipsq_t	*ipsq;
4761 	kstat_named_t	*kn;
4762 	boolean_t isloopback;
4763 	in6_addr_t ov6addr;
4764 
4765 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4766 
4767 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4768 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4769 	rw_exit(&ipst->ips_ill_g_lock);
4770 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4771 		return (ill);
4772 
4773 	/*
4774 	 * Couldn't find it.  Does this happen to be a lookup for the
4775 	 * loopback device and are we allowed to allocate it?
4776 	 */
4777 	if (!isloopback || !do_alloc)
4778 		return (NULL);
4779 
4780 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4781 
4782 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4783 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4784 		rw_exit(&ipst->ips_ill_g_lock);
4785 		return (ill);
4786 	}
4787 
4788 	/* Create the loopback device on demand */
4789 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4790 	    sizeof (ipif_loopback_name), BPRI_MED));
4791 	if (ill == NULL)
4792 		goto done;
4793 
4794 	*ill = ill_null;
4795 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4796 	ill->ill_ipst = ipst;
4797 	netstack_hold(ipst->ips_netstack);
4798 	/*
4799 	 * For exclusive stacks we set the zoneid to zero
4800 	 * to make IP operate as if in the global zone.
4801 	 */
4802 	ill->ill_zoneid = GLOBAL_ZONEID;
4803 
4804 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4805 	if (ill->ill_phyint == NULL)
4806 		goto done;
4807 
4808 	if (isv6)
4809 		ill->ill_phyint->phyint_illv6 = ill;
4810 	else
4811 		ill->ill_phyint->phyint_illv4 = ill;
4812 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4813 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
4814 
4815 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4816 	/* Add room for tcp+ip headers */
4817 	if (isv6) {
4818 		ill->ill_isv6 = B_TRUE;
4819 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4820 	} else {
4821 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4822 	}
4823 	if (!ill_allocate_mibs(ill))
4824 		goto done;
4825 	ill->ill_max_mtu = ill->ill_max_frag;
4826 	/*
4827 	 * ipif_loopback_name can't be pointed at directly because its used
4828 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4829 	 * from the glist, ill_glist_delete() sets the first character of
4830 	 * ill_name to '\0'.
4831 	 */
4832 	ill->ill_name = (char *)ill + sizeof (*ill);
4833 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4834 	ill->ill_name_length = sizeof (ipif_loopback_name);
4835 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4836 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4837 
4838 	ill->ill_global_timer = INFINITY;
4839 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4840 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4841 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4842 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4843 
4844 	/* No resolver here. */
4845 	ill->ill_net_type = IRE_LOOPBACK;
4846 
4847 	/* Initialize the ipsq */
4848 	if (!ipsq_init(ill, B_FALSE))
4849 		goto done;
4850 
4851 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4852 	if (ipif == NULL)
4853 		goto done;
4854 
4855 	ill->ill_flags = ILLF_MULTICAST;
4856 
4857 	ov6addr = ipif->ipif_v6lcl_addr;
4858 	/* Set up default loopback address and mask. */
4859 	if (!isv6) {
4860 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4861 
4862 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4863 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4864 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4865 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4866 		    ipif->ipif_v6subnet);
4867 		ill->ill_flags |= ILLF_IPV4;
4868 	} else {
4869 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4870 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4871 		ipif->ipif_v6net_mask = ipv6_all_ones;
4872 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4873 		    ipif->ipif_v6subnet);
4874 		ill->ill_flags |= ILLF_IPV6;
4875 	}
4876 
4877 	/*
4878 	 * Chain us in at the end of the ill list. hold the ill
4879 	 * before we make it globally visible. 1 for the lookup.
4880 	 */
4881 	ill->ill_refcnt = 0;
4882 	ill_refhold(ill);
4883 
4884 	ill->ill_frag_count = 0;
4885 	ill->ill_frag_free_num_pkts = 0;
4886 	ill->ill_last_frag_clean_time = 0;
4887 
4888 	ipsq = ill->ill_phyint->phyint_ipsq;
4889 
4890 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4891 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4892 
4893 	/* Let SCTP know so that it can add this to its list */
4894 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4895 
4896 	/*
4897 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4898 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4899 	 * requires to be after ill_glist_insert() since we need the
4900 	 * ill_index set. Pass on ipv6_loopback as the old address.
4901 	 */
4902 	sctp_update_ipif_addr(ipif, ov6addr);
4903 
4904 	/*
4905 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4906 	 * If so, free our original one.
4907 	 */
4908 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4909 		ipsq_delete(ipsq);
4910 
4911 	if (ipst->ips_loopback_ksp == NULL) {
4912 		/* Export loopback interface statistics */
4913 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4914 		    ipif_loopback_name, "net",
4915 		    KSTAT_TYPE_NAMED, 2, 0,
4916 		    ipst->ips_netstack->netstack_stackid);
4917 		if (ipst->ips_loopback_ksp != NULL) {
4918 			ipst->ips_loopback_ksp->ks_update =
4919 			    loopback_kstat_update;
4920 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4921 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4922 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4923 			ipst->ips_loopback_ksp->ks_private =
4924 			    (void *)(uintptr_t)ipst->ips_netstack->
4925 			    netstack_stackid;
4926 			kstat_install(ipst->ips_loopback_ksp);
4927 		}
4928 	}
4929 
4930 	if (error != NULL)
4931 		*error = 0;
4932 	*did_alloc = B_TRUE;
4933 	rw_exit(&ipst->ips_ill_g_lock);
4934 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4935 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4936 	return (ill);
4937 done:
4938 	if (ill != NULL) {
4939 		if (ill->ill_phyint != NULL) {
4940 			ipsq = ill->ill_phyint->phyint_ipsq;
4941 			if (ipsq != NULL) {
4942 				ipsq->ipsq_phyint = NULL;
4943 				ipsq_delete(ipsq);
4944 			}
4945 			mi_free(ill->ill_phyint);
4946 		}
4947 		ill_free_mib(ill);
4948 		if (ill->ill_ipst != NULL)
4949 			netstack_rele(ill->ill_ipst->ips_netstack);
4950 		mi_free(ill);
4951 	}
4952 	rw_exit(&ipst->ips_ill_g_lock);
4953 	if (error != NULL)
4954 		*error = ENOMEM;
4955 	return (NULL);
4956 }
4957 
4958 /*
4959  * For IPP calls - use the ip_stack_t for global stack.
4960  */
4961 ill_t *
4962 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4963     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4964 {
4965 	ip_stack_t	*ipst;
4966 	ill_t		*ill;
4967 
4968 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4969 	if (ipst == NULL) {
4970 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4971 		return (NULL);
4972 	}
4973 
4974 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4975 	netstack_rele(ipst->ips_netstack);
4976 	return (ill);
4977 }
4978 
4979 /*
4980  * Return a pointer to the ill which matches the index and IP version type.
4981  */
4982 ill_t *
4983 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4984     ipsq_func_t func, int *err, ip_stack_t *ipst)
4985 {
4986 	ill_t	*ill;
4987 	ipsq_t  *ipsq;
4988 	phyint_t *phyi;
4989 
4990 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4991 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4992 
4993 	if (err != NULL)
4994 		*err = 0;
4995 
4996 	/*
4997 	 * Indexes are stored in the phyint - a common structure
4998 	 * to both IPv4 and IPv6.
4999 	 */
5000 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5001 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5002 	    (void *) &index, NULL);
5003 	if (phyi != NULL) {
5004 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5005 		if (ill != NULL) {
5006 			/*
5007 			 * The block comment at the start of ipif_down
5008 			 * explains the use of the macros used below
5009 			 */
5010 			GRAB_CONN_LOCK(q);
5011 			mutex_enter(&ill->ill_lock);
5012 			if (ILL_CAN_LOOKUP(ill)) {
5013 				ill_refhold_locked(ill);
5014 				mutex_exit(&ill->ill_lock);
5015 				RELEASE_CONN_LOCK(q);
5016 				rw_exit(&ipst->ips_ill_g_lock);
5017 				return (ill);
5018 			} else if (ILL_CAN_WAIT(ill, q)) {
5019 				ipsq = ill->ill_phyint->phyint_ipsq;
5020 				mutex_enter(&ipsq->ipsq_lock);
5021 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5022 				rw_exit(&ipst->ips_ill_g_lock);
5023 				mutex_exit(&ill->ill_lock);
5024 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5025 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5026 				mutex_exit(&ipsq->ipsq_lock);
5027 				RELEASE_CONN_LOCK(q);
5028 				if (err != NULL)
5029 					*err = EINPROGRESS;
5030 				return (NULL);
5031 			}
5032 			RELEASE_CONN_LOCK(q);
5033 			mutex_exit(&ill->ill_lock);
5034 		}
5035 	}
5036 	rw_exit(&ipst->ips_ill_g_lock);
5037 	if (err != NULL)
5038 		*err = ENXIO;
5039 	return (NULL);
5040 }
5041 
5042 /*
5043  * Return the ifindex next in sequence after the passed in ifindex.
5044  * If there is no next ifindex for the given protocol, return 0.
5045  */
5046 uint_t
5047 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5048 {
5049 	phyint_t *phyi;
5050 	phyint_t *phyi_initial;
5051 	uint_t   ifindex;
5052 
5053 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5054 
5055 	if (index == 0) {
5056 		phyi = avl_first(
5057 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5058 	} else {
5059 		phyi = phyi_initial = avl_find(
5060 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5061 		    (void *) &index, NULL);
5062 	}
5063 
5064 	for (; phyi != NULL;
5065 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5066 	    phyi, AVL_AFTER)) {
5067 		/*
5068 		 * If we're not returning the first interface in the tree
5069 		 * and we still haven't moved past the phyint_t that
5070 		 * corresponds to index, avl_walk needs to be called again
5071 		 */
5072 		if (!((index != 0) && (phyi == phyi_initial))) {
5073 			if (isv6) {
5074 				if ((phyi->phyint_illv6) &&
5075 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5076 				    (phyi->phyint_illv6->ill_isv6 == 1))
5077 					break;
5078 			} else {
5079 				if ((phyi->phyint_illv4) &&
5080 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5081 				    (phyi->phyint_illv4->ill_isv6 == 0))
5082 					break;
5083 			}
5084 		}
5085 	}
5086 
5087 	rw_exit(&ipst->ips_ill_g_lock);
5088 
5089 	if (phyi != NULL)
5090 		ifindex = phyi->phyint_ifindex;
5091 	else
5092 		ifindex = 0;
5093 
5094 	return (ifindex);
5095 }
5096 
5097 /*
5098  * Return the ifindex for the named interface.
5099  * If there is no next ifindex for the interface, return 0.
5100  */
5101 uint_t
5102 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5103 {
5104 	phyint_t	*phyi;
5105 	avl_index_t	where = 0;
5106 	uint_t		ifindex;
5107 
5108 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5109 
5110 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5111 	    name, &where)) == NULL) {
5112 		rw_exit(&ipst->ips_ill_g_lock);
5113 		return (0);
5114 	}
5115 
5116 	ifindex = phyi->phyint_ifindex;
5117 
5118 	rw_exit(&ipst->ips_ill_g_lock);
5119 
5120 	return (ifindex);
5121 }
5122 
5123 /*
5124  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5125  * that gives a running thread a reference to the ill. This reference must be
5126  * released by the thread when it is done accessing the ill and related
5127  * objects. ill_refcnt can not be used to account for static references
5128  * such as other structures pointing to an ill. Callers must generally
5129  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5130  * or be sure that the ill is not being deleted or changing state before
5131  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5132  * ill won't change any of its critical state such as address, netmask etc.
5133  */
5134 void
5135 ill_refhold(ill_t *ill)
5136 {
5137 	mutex_enter(&ill->ill_lock);
5138 	ill->ill_refcnt++;
5139 	ILL_TRACE_REF(ill);
5140 	mutex_exit(&ill->ill_lock);
5141 }
5142 
5143 void
5144 ill_refhold_locked(ill_t *ill)
5145 {
5146 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5147 	ill->ill_refcnt++;
5148 	ILL_TRACE_REF(ill);
5149 }
5150 
5151 int
5152 ill_check_and_refhold(ill_t *ill)
5153 {
5154 	mutex_enter(&ill->ill_lock);
5155 	if (ILL_CAN_LOOKUP(ill)) {
5156 		ill_refhold_locked(ill);
5157 		mutex_exit(&ill->ill_lock);
5158 		return (0);
5159 	}
5160 	mutex_exit(&ill->ill_lock);
5161 	return (ILL_LOOKUP_FAILED);
5162 }
5163 
5164 /*
5165  * Must not be called while holding any locks. Otherwise if this is
5166  * the last reference to be released, there is a chance of recursive mutex
5167  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5168  * to restart an ioctl.
5169  */
5170 void
5171 ill_refrele(ill_t *ill)
5172 {
5173 	mutex_enter(&ill->ill_lock);
5174 	ASSERT(ill->ill_refcnt != 0);
5175 	ill->ill_refcnt--;
5176 	ILL_UNTRACE_REF(ill);
5177 	if (ill->ill_refcnt != 0) {
5178 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5179 		mutex_exit(&ill->ill_lock);
5180 		return;
5181 	}
5182 
5183 	/* Drops the ill_lock */
5184 	ipif_ill_refrele_tail(ill);
5185 }
5186 
5187 /*
5188  * Obtain a weak reference count on the ill. This reference ensures the
5189  * ill won't be freed, but the ill may change any of its critical state
5190  * such as netmask, address etc. Returns an error if the ill has started
5191  * closing.
5192  */
5193 boolean_t
5194 ill_waiter_inc(ill_t *ill)
5195 {
5196 	mutex_enter(&ill->ill_lock);
5197 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5198 		mutex_exit(&ill->ill_lock);
5199 		return (B_FALSE);
5200 	}
5201 	ill->ill_waiters++;
5202 	mutex_exit(&ill->ill_lock);
5203 	return (B_TRUE);
5204 }
5205 
5206 void
5207 ill_waiter_dcr(ill_t *ill)
5208 {
5209 	mutex_enter(&ill->ill_lock);
5210 	ill->ill_waiters--;
5211 	if (ill->ill_waiters == 0)
5212 		cv_broadcast(&ill->ill_cv);
5213 	mutex_exit(&ill->ill_lock);
5214 }
5215 
5216 /*
5217  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5218  * driver.  We construct best guess defaults for lower level information that
5219  * we need.  If an interface is brought up without injection of any overriding
5220  * information from outside, we have to be ready to go with these defaults.
5221  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5222  * we primarely want the dl_provider_style.
5223  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5224  * at which point we assume the other part of the information is valid.
5225  */
5226 void
5227 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5228 {
5229 	uchar_t		*brdcst_addr;
5230 	uint_t		brdcst_addr_length, phys_addr_length;
5231 	t_scalar_t	sap_length;
5232 	dl_info_ack_t	*dlia;
5233 	ip_m_t		*ipm;
5234 	dl_qos_cl_sel1_t *sel1;
5235 	int		min_mtu;
5236 
5237 	ASSERT(IAM_WRITER_ILL(ill));
5238 
5239 	/*
5240 	 * Till the ill is fully up ILL_CHANGING will be set and
5241 	 * the ill is not globally visible. So no need for a lock.
5242 	 */
5243 	dlia = (dl_info_ack_t *)mp->b_rptr;
5244 	ill->ill_mactype = dlia->dl_mac_type;
5245 
5246 	ipm = ip_m_lookup(dlia->dl_mac_type);
5247 	if (ipm == NULL) {
5248 		ipm = ip_m_lookup(DL_OTHER);
5249 		ASSERT(ipm != NULL);
5250 	}
5251 	ill->ill_media = ipm;
5252 
5253 	/*
5254 	 * When the new DLPI stuff is ready we'll pull lengths
5255 	 * from dlia.
5256 	 */
5257 	if (dlia->dl_version == DL_VERSION_2) {
5258 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5259 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5260 		    brdcst_addr_length);
5261 		if (brdcst_addr == NULL) {
5262 			brdcst_addr_length = 0;
5263 		}
5264 		sap_length = dlia->dl_sap_length;
5265 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5266 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5267 		    brdcst_addr_length, sap_length, phys_addr_length));
5268 	} else {
5269 		brdcst_addr_length = 6;
5270 		brdcst_addr = ip_six_byte_all_ones;
5271 		sap_length = -2;
5272 		phys_addr_length = brdcst_addr_length;
5273 	}
5274 
5275 	ill->ill_bcast_addr_length = brdcst_addr_length;
5276 	ill->ill_phys_addr_length = phys_addr_length;
5277 	ill->ill_sap_length = sap_length;
5278 
5279 	/*
5280 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5281 	 * but we must ensure a minimum IP MTU is used since other bits of
5282 	 * IP will fly apart otherwise.
5283 	 */
5284 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5285 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5286 	ill->ill_max_mtu = ill->ill_max_frag;
5287 
5288 	ill->ill_type = ipm->ip_m_type;
5289 
5290 	if (!ill->ill_dlpi_style_set) {
5291 		if (dlia->dl_provider_style == DL_STYLE2)
5292 			ill->ill_needs_attach = 1;
5293 
5294 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
5295 
5296 		/*
5297 		 * Allocate the first ipif on this ill.  We don't delay it
5298 		 * further as ioctl handling assumes at least one ipif exists.
5299 		 *
5300 		 * At this point we don't know whether the ill is v4 or v6.
5301 		 * We will know this whan the SIOCSLIFNAME happens and
5302 		 * the correct value for ill_isv6 will be assigned in
5303 		 * ipif_set_values(). We need to hold the ill lock and
5304 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5305 		 * the wakeup.
5306 		 */
5307 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5308 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5309 		mutex_enter(&ill->ill_lock);
5310 		ASSERT(ill->ill_dlpi_style_set == 0);
5311 		ill->ill_dlpi_style_set = 1;
5312 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5313 		cv_broadcast(&ill->ill_cv);
5314 		mutex_exit(&ill->ill_lock);
5315 		freemsg(mp);
5316 		return;
5317 	}
5318 	ASSERT(ill->ill_ipif != NULL);
5319 	/*
5320 	 * We know whether it is IPv4 or IPv6 now, as this is the
5321 	 * second DL_INFO_ACK we are recieving in response to the
5322 	 * DL_INFO_REQ sent in ipif_set_values.
5323 	 */
5324 	if (ill->ill_isv6)
5325 		ill->ill_sap = IP6_DL_SAP;
5326 	else
5327 		ill->ill_sap = IP_DL_SAP;
5328 	/*
5329 	 * Set ipif_mtu which is used to set the IRE's
5330 	 * ire_max_frag value. The driver could have sent
5331 	 * a different mtu from what it sent last time. No
5332 	 * need to call ipif_mtu_change because IREs have
5333 	 * not yet been created.
5334 	 */
5335 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5336 	/*
5337 	 * Clear all the flags that were set based on ill_bcast_addr_length
5338 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5339 	 * changed now and we need to re-evaluate.
5340 	 */
5341 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5342 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5343 
5344 	/*
5345 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5346 	 * changed now.
5347 	 *
5348 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5349 	 * with no underlying interfaces (and thus an unknown broadcast
5350 	 * address length), but we enforce that an interface is broadcast-
5351 	 * capable as part of allowing it to join a group.
5352 	 */
5353 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5354 		if (ill->ill_resolver_mp != NULL)
5355 			freemsg(ill->ill_resolver_mp);
5356 		if (ill->ill_bcast_mp != NULL)
5357 			freemsg(ill->ill_bcast_mp);
5358 		if (ill->ill_flags & ILLF_XRESOLV)
5359 			ill->ill_net_type = IRE_IF_RESOLVER;
5360 		else
5361 			ill->ill_net_type = IRE_IF_NORESOLVER;
5362 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5363 		    ill->ill_phys_addr_length,
5364 		    ill->ill_sap,
5365 		    ill->ill_sap_length);
5366 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5367 
5368 		if (ill->ill_isv6)
5369 			/*
5370 			 * Note: xresolv interfaces will eventually need NOARP
5371 			 * set here as well, but that will require those
5372 			 * external resolvers to have some knowledge of
5373 			 * that flag and act appropriately. Not to be changed
5374 			 * at present.
5375 			 */
5376 			ill->ill_flags |= ILLF_NONUD;
5377 		else
5378 			ill->ill_flags |= ILLF_NOARP;
5379 
5380 		if (ill->ill_phys_addr_length == 0) {
5381 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5382 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5383 			} else {
5384 				/* pt-pt supports multicast. */
5385 				ill->ill_flags |= ILLF_MULTICAST;
5386 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5387 			}
5388 		}
5389 	} else {
5390 		ill->ill_net_type = IRE_IF_RESOLVER;
5391 		if (ill->ill_bcast_mp != NULL)
5392 			freemsg(ill->ill_bcast_mp);
5393 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5394 		    ill->ill_bcast_addr_length, ill->ill_sap,
5395 		    ill->ill_sap_length);
5396 		/*
5397 		 * Later detect lack of DLPI driver multicast
5398 		 * capability by catching DL_ENABMULTI errors in
5399 		 * ip_rput_dlpi.
5400 		 */
5401 		ill->ill_flags |= ILLF_MULTICAST;
5402 		if (!ill->ill_isv6)
5403 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5404 	}
5405 
5406 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
5407 	if (ill->ill_mactype == SUNW_DL_IPMP)
5408 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5409 
5410 	/* By default an interface does not support any CoS marking */
5411 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5412 
5413 	/*
5414 	 * If we get QoS information in DL_INFO_ACK, the device supports
5415 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5416 	 */
5417 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5418 	    dlia->dl_qos_length);
5419 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5420 		ill->ill_flags |= ILLF_COS_ENABLED;
5421 	}
5422 
5423 	/* Clear any previous error indication. */
5424 	ill->ill_error = 0;
5425 	freemsg(mp);
5426 }
5427 
5428 /*
5429  * Perform various checks to verify that an address would make sense as a
5430  * local, remote, or subnet interface address.
5431  */
5432 static boolean_t
5433 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5434 {
5435 	ipaddr_t	net_mask;
5436 
5437 	/*
5438 	 * Don't allow all zeroes, or all ones, but allow
5439 	 * all ones netmask.
5440 	 */
5441 	if ((net_mask = ip_net_mask(addr)) == 0)
5442 		return (B_FALSE);
5443 	/* A given netmask overrides the "guess" netmask */
5444 	if (subnet_mask != 0)
5445 		net_mask = subnet_mask;
5446 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5447 	    (addr == (addr | ~net_mask)))) {
5448 		return (B_FALSE);
5449 	}
5450 
5451 	/*
5452 	 * Even if the netmask is all ones, we do not allow address to be
5453 	 * 255.255.255.255
5454 	 */
5455 	if (addr == INADDR_BROADCAST)
5456 		return (B_FALSE);
5457 
5458 	if (CLASSD(addr))
5459 		return (B_FALSE);
5460 
5461 	return (B_TRUE);
5462 }
5463 
5464 #define	V6_IPIF_LINKLOCAL(p)	\
5465 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5466 
5467 /*
5468  * Compare two given ipifs and check if the second one is better than
5469  * the first one using the order of preference (not taking deprecated
5470  * into acount) specified in ipif_lookup_multicast().
5471  */
5472 static boolean_t
5473 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5474 {
5475 	/* Check the least preferred first. */
5476 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5477 		/* If both ipifs are the same, use the first one. */
5478 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5479 			return (B_FALSE);
5480 		else
5481 			return (B_TRUE);
5482 	}
5483 
5484 	/* For IPv6, check for link local address. */
5485 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5486 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5487 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5488 			/* The second one is equal or less preferred. */
5489 			return (B_FALSE);
5490 		} else {
5491 			return (B_TRUE);
5492 		}
5493 	}
5494 
5495 	/* Then check for point to point interface. */
5496 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5497 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5498 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5499 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5500 			return (B_FALSE);
5501 		} else {
5502 			return (B_TRUE);
5503 		}
5504 	}
5505 
5506 	/* old_ipif is a normal interface, so no need to use the new one. */
5507 	return (B_FALSE);
5508 }
5509 
5510 /*
5511  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5512  * The ipif must be up, and its ill must multicast-capable, not
5513  * condemned, not an underlying interface in an IPMP group, and
5514  * not a VNI interface.  Order of preference:
5515  *
5516  * 	1a. normal
5517  * 	1b. normal, but deprecated
5518  * 	2a. point to point
5519  * 	2b. point to point, but deprecated
5520  * 	3a. link local
5521  * 	3b. link local, but deprecated
5522  * 	4. loopback.
5523  */
5524 ipif_t *
5525 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5526 {
5527 	ill_t			*ill;
5528 	ill_walk_context_t	ctx;
5529 	ipif_t			*ipif;
5530 	ipif_t			*saved_ipif = NULL;
5531 	ipif_t			*dep_ipif = NULL;
5532 
5533 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5534 	if (isv6)
5535 		ill = ILL_START_WALK_V6(&ctx, ipst);
5536 	else
5537 		ill = ILL_START_WALK_V4(&ctx, ipst);
5538 
5539 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5540 		mutex_enter(&ill->ill_lock);
5541 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5542 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5543 			mutex_exit(&ill->ill_lock);
5544 			continue;
5545 		}
5546 		for (ipif = ill->ill_ipif; ipif != NULL;
5547 		    ipif = ipif->ipif_next) {
5548 			if (zoneid != ipif->ipif_zoneid &&
5549 			    zoneid != ALL_ZONES &&
5550 			    ipif->ipif_zoneid != ALL_ZONES) {
5551 				continue;
5552 			}
5553 			if (!(ipif->ipif_flags & IPIF_UP) ||
5554 			    !IPIF_CAN_LOOKUP(ipif)) {
5555 				continue;
5556 			}
5557 
5558 			/*
5559 			 * Found one candidate.  If it is deprecated,
5560 			 * remember it in dep_ipif.  If it is not deprecated,
5561 			 * remember it in saved_ipif.
5562 			 */
5563 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5564 				if (dep_ipif == NULL) {
5565 					dep_ipif = ipif;
5566 				} else if (ipif_comp_multi(dep_ipif, ipif,
5567 				    isv6)) {
5568 					/*
5569 					 * If the previous dep_ipif does not
5570 					 * belong to the same ill, we've done
5571 					 * a ipif_refhold() on it.  So we need
5572 					 * to release it.
5573 					 */
5574 					if (dep_ipif->ipif_ill != ill)
5575 						ipif_refrele(dep_ipif);
5576 					dep_ipif = ipif;
5577 				}
5578 				continue;
5579 			}
5580 			if (saved_ipif == NULL) {
5581 				saved_ipif = ipif;
5582 			} else {
5583 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5584 					if (saved_ipif->ipif_ill != ill)
5585 						ipif_refrele(saved_ipif);
5586 					saved_ipif = ipif;
5587 				}
5588 			}
5589 		}
5590 		/*
5591 		 * Before going to the next ill, do a ipif_refhold() on the
5592 		 * saved ones.
5593 		 */
5594 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5595 			ipif_refhold_locked(saved_ipif);
5596 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5597 			ipif_refhold_locked(dep_ipif);
5598 		mutex_exit(&ill->ill_lock);
5599 	}
5600 	rw_exit(&ipst->ips_ill_g_lock);
5601 
5602 	/*
5603 	 * If we have only the saved_ipif, return it.  But if we have both
5604 	 * saved_ipif and dep_ipif, check to see which one is better.
5605 	 */
5606 	if (saved_ipif != NULL) {
5607 		if (dep_ipif != NULL) {
5608 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5609 				ipif_refrele(saved_ipif);
5610 				return (dep_ipif);
5611 			} else {
5612 				ipif_refrele(dep_ipif);
5613 				return (saved_ipif);
5614 			}
5615 		}
5616 		return (saved_ipif);
5617 	} else {
5618 		return (dep_ipif);
5619 	}
5620 }
5621 
5622 /*
5623  * This function is called when an application does not specify an interface
5624  * to be used for multicast traffic (joining a group/sending data).  It
5625  * calls ire_lookup_multi() to look for an interface route for the
5626  * specified multicast group.  Doing this allows the administrator to add
5627  * prefix routes for multicast to indicate which interface to be used for
5628  * multicast traffic in the above scenario.  The route could be for all
5629  * multicast (224.0/4), for a single multicast group (a /32 route) or
5630  * anything in between.  If there is no such multicast route, we just find
5631  * any multicast capable interface and return it.  The returned ipif
5632  * is refhold'ed.
5633  */
5634 ipif_t *
5635 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5636 {
5637 	ire_t			*ire;
5638 	ipif_t			*ipif;
5639 
5640 	ire = ire_lookup_multi(group, zoneid, ipst);
5641 	if (ire != NULL) {
5642 		ipif = ire->ire_ipif;
5643 		ipif_refhold(ipif);
5644 		ire_refrele(ire);
5645 		return (ipif);
5646 	}
5647 
5648 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5649 }
5650 
5651 /*
5652  * Look for an ipif with the specified interface address and destination.
5653  * The destination address is used only for matching point-to-point interfaces.
5654  */
5655 ipif_t *
5656 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5657     ipsq_func_t func, int *error, ip_stack_t *ipst)
5658 {
5659 	ipif_t	*ipif;
5660 	ill_t	*ill;
5661 	ill_walk_context_t ctx;
5662 	ipsq_t	*ipsq;
5663 
5664 	if (error != NULL)
5665 		*error = 0;
5666 
5667 	/*
5668 	 * First match all the point-to-point interfaces
5669 	 * before looking at non-point-to-point interfaces.
5670 	 * This is done to avoid returning non-point-to-point
5671 	 * ipif instead of unnumbered point-to-point ipif.
5672 	 */
5673 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5674 	ill = ILL_START_WALK_V4(&ctx, ipst);
5675 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5676 		GRAB_CONN_LOCK(q);
5677 		mutex_enter(&ill->ill_lock);
5678 		for (ipif = ill->ill_ipif; ipif != NULL;
5679 		    ipif = ipif->ipif_next) {
5680 			/* Allow the ipif to be down */
5681 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5682 			    (ipif->ipif_lcl_addr == if_addr) &&
5683 			    (ipif->ipif_pp_dst_addr == dst)) {
5684 				/*
5685 				 * The block comment at the start of ipif_down
5686 				 * explains the use of the macros used below
5687 				 */
5688 				if (IPIF_CAN_LOOKUP(ipif)) {
5689 					ipif_refhold_locked(ipif);
5690 					mutex_exit(&ill->ill_lock);
5691 					RELEASE_CONN_LOCK(q);
5692 					rw_exit(&ipst->ips_ill_g_lock);
5693 					return (ipif);
5694 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5695 					ipsq = ill->ill_phyint->phyint_ipsq;
5696 					mutex_enter(&ipsq->ipsq_lock);
5697 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5698 					mutex_exit(&ill->ill_lock);
5699 					rw_exit(&ipst->ips_ill_g_lock);
5700 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5701 					    ill);
5702 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5703 					mutex_exit(&ipsq->ipsq_lock);
5704 					RELEASE_CONN_LOCK(q);
5705 					if (error != NULL)
5706 						*error = EINPROGRESS;
5707 					return (NULL);
5708 				}
5709 			}
5710 		}
5711 		mutex_exit(&ill->ill_lock);
5712 		RELEASE_CONN_LOCK(q);
5713 	}
5714 	rw_exit(&ipst->ips_ill_g_lock);
5715 
5716 	/* lookup the ipif based on interface address */
5717 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5718 	    ipst);
5719 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5720 	return (ipif);
5721 }
5722 
5723 /*
5724  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5725  */
5726 static ipif_t *
5727 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5728     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5729     ip_stack_t *ipst)
5730 {
5731 	ipif_t  *ipif;
5732 	ill_t   *ill;
5733 	boolean_t ptp = B_FALSE;
5734 	ipsq_t	*ipsq;
5735 	ill_walk_context_t	ctx;
5736 
5737 	if (error != NULL)
5738 		*error = 0;
5739 
5740 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5741 	/*
5742 	 * Repeat twice, first based on local addresses and
5743 	 * next time for pointopoint.
5744 	 */
5745 repeat:
5746 	ill = ILL_START_WALK_V4(&ctx, ipst);
5747 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5748 		if (match_ill != NULL && ill != match_ill &&
5749 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5750 			continue;
5751 		}
5752 		GRAB_CONN_LOCK(q);
5753 		mutex_enter(&ill->ill_lock);
5754 		for (ipif = ill->ill_ipif; ipif != NULL;
5755 		    ipif = ipif->ipif_next) {
5756 			if (zoneid != ALL_ZONES &&
5757 			    zoneid != ipif->ipif_zoneid &&
5758 			    ipif->ipif_zoneid != ALL_ZONES)
5759 				continue;
5760 			/* Allow the ipif to be down */
5761 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5762 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5763 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5764 			    (ipif->ipif_pp_dst_addr == addr))) {
5765 				/*
5766 				 * The block comment at the start of ipif_down
5767 				 * explains the use of the macros used below
5768 				 */
5769 				if (IPIF_CAN_LOOKUP(ipif)) {
5770 					ipif_refhold_locked(ipif);
5771 					mutex_exit(&ill->ill_lock);
5772 					RELEASE_CONN_LOCK(q);
5773 					rw_exit(&ipst->ips_ill_g_lock);
5774 					return (ipif);
5775 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5776 					ipsq = ill->ill_phyint->phyint_ipsq;
5777 					mutex_enter(&ipsq->ipsq_lock);
5778 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5779 					mutex_exit(&ill->ill_lock);
5780 					rw_exit(&ipst->ips_ill_g_lock);
5781 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5782 					    ill);
5783 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5784 					mutex_exit(&ipsq->ipsq_lock);
5785 					RELEASE_CONN_LOCK(q);
5786 					if (error != NULL)
5787 						*error = EINPROGRESS;
5788 					return (NULL);
5789 				}
5790 			}
5791 		}
5792 		mutex_exit(&ill->ill_lock);
5793 		RELEASE_CONN_LOCK(q);
5794 	}
5795 
5796 	/* If we already did the ptp case, then we are done */
5797 	if (ptp) {
5798 		rw_exit(&ipst->ips_ill_g_lock);
5799 		if (error != NULL)
5800 			*error = ENXIO;
5801 		return (NULL);
5802 	}
5803 	ptp = B_TRUE;
5804 	goto repeat;
5805 }
5806 
5807 /*
5808  * Check if the address exists in the system.
5809  * We don't hold the conn_lock as we will not perform defered ipsqueue
5810  * operation.
5811  */
5812 boolean_t
5813 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5814 {
5815 	ipif_t  *ipif;
5816 	ill_t   *ill;
5817 	ill_walk_context_t	ctx;
5818 
5819 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5820 
5821 	ill = ILL_START_WALK_V4(&ctx, ipst);
5822 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5823 		mutex_enter(&ill->ill_lock);
5824 		for (ipif = ill->ill_ipif; ipif != NULL;
5825 		    ipif = ipif->ipif_next) {
5826 			if (zoneid != ALL_ZONES &&
5827 			    zoneid != ipif->ipif_zoneid &&
5828 			    ipif->ipif_zoneid != ALL_ZONES)
5829 				continue;
5830 			/* Allow the ipif to be down */
5831 			/*
5832 			 * XXX Different from ipif_lookup_addr(), we don't do
5833 			 * twice lookups. As from bind()'s point of view, we
5834 			 * may return once we find a match.
5835 			 */
5836 			if (((ipif->ipif_lcl_addr == addr) &&
5837 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5838 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5839 			    (ipif->ipif_pp_dst_addr == addr))) {
5840 				/*
5841 				 * Allow bind() to be successful even if the
5842 				 * ipif is with IPIF_CHANGING bit set.
5843 				 */
5844 				mutex_exit(&ill->ill_lock);
5845 				rw_exit(&ipst->ips_ill_g_lock);
5846 				return (B_TRUE);
5847 			}
5848 		}
5849 		mutex_exit(&ill->ill_lock);
5850 	}
5851 
5852 	rw_exit(&ipst->ips_ill_g_lock);
5853 	return (B_FALSE);
5854 }
5855 
5856 /*
5857  * Lookup an ipif with the specified address.  For point-to-point links we
5858  * look for matches on either the destination address or the local address,
5859  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5860  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5861  * (or illgrp if `match_ill' is in an IPMP group).
5862  */
5863 ipif_t *
5864 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5865     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5866 {
5867 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
5868 	    func, error, ipst));
5869 }
5870 
5871 /*
5872  * Special abbreviated version of ipif_lookup_addr() that doesn't match
5873  * `match_ill' across the IPMP group.  This function is only needed in some
5874  * corner-cases; almost everything should use ipif_lookup_addr().
5875  */
5876 static ipif_t *
5877 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5878 {
5879 	ASSERT(match_ill != NULL);
5880 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
5881 	    NULL, NULL, NULL, NULL, ipst));
5882 }
5883 
5884 /*
5885  * Look for an ipif with the specified address. For point-point links
5886  * we look for matches on either the destination address and the local
5887  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5888  * is set.
5889  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
5890  * ill (or illgrp if `match_ill' is in an IPMP group).
5891  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5892  */
5893 zoneid_t
5894 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5895 {
5896 	zoneid_t zoneid;
5897 	ipif_t  *ipif;
5898 	ill_t   *ill;
5899 	boolean_t ptp = B_FALSE;
5900 	ill_walk_context_t	ctx;
5901 
5902 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5903 	/*
5904 	 * Repeat twice, first based on local addresses and
5905 	 * next time for pointopoint.
5906 	 */
5907 repeat:
5908 	ill = ILL_START_WALK_V4(&ctx, ipst);
5909 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5910 		if (match_ill != NULL && ill != match_ill &&
5911 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
5912 			continue;
5913 		}
5914 		mutex_enter(&ill->ill_lock);
5915 		for (ipif = ill->ill_ipif; ipif != NULL;
5916 		    ipif = ipif->ipif_next) {
5917 			/* Allow the ipif to be down */
5918 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5919 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5920 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5921 			    (ipif->ipif_pp_dst_addr == addr)) &&
5922 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5923 				zoneid = ipif->ipif_zoneid;
5924 				mutex_exit(&ill->ill_lock);
5925 				rw_exit(&ipst->ips_ill_g_lock);
5926 				/*
5927 				 * If ipif_zoneid was ALL_ZONES then we have
5928 				 * a trusted extensions shared IP address.
5929 				 * In that case GLOBAL_ZONEID works to send.
5930 				 */
5931 				if (zoneid == ALL_ZONES)
5932 					zoneid = GLOBAL_ZONEID;
5933 				return (zoneid);
5934 			}
5935 		}
5936 		mutex_exit(&ill->ill_lock);
5937 	}
5938 
5939 	/* If we already did the ptp case, then we are done */
5940 	if (ptp) {
5941 		rw_exit(&ipst->ips_ill_g_lock);
5942 		return (ALL_ZONES);
5943 	}
5944 	ptp = B_TRUE;
5945 	goto repeat;
5946 }
5947 
5948 /*
5949  * Look for an ipif that matches the specified remote address i.e. the
5950  * ipif that would receive the specified packet.
5951  * First look for directly connected interfaces and then do a recursive
5952  * IRE lookup and pick the first ipif corresponding to the source address in the
5953  * ire.
5954  * Returns: held ipif
5955  */
5956 ipif_t *
5957 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5958 {
5959 	ipif_t	*ipif;
5960 	ire_t	*ire;
5961 	ip_stack_t	*ipst = ill->ill_ipst;
5962 
5963 	ASSERT(!ill->ill_isv6);
5964 
5965 	/*
5966 	 * Someone could be changing this ipif currently or change it
5967 	 * after we return this. Thus  a few packets could use the old
5968 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5969 	 * will atomically be updated or cleaned up with the new value
5970 	 * Thus we don't need a lock to check the flags or other attrs below.
5971 	 */
5972 	mutex_enter(&ill->ill_lock);
5973 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5974 		if (!IPIF_CAN_LOOKUP(ipif))
5975 			continue;
5976 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5977 		    ipif->ipif_zoneid != ALL_ZONES)
5978 			continue;
5979 		/* Allow the ipif to be down */
5980 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5981 			if ((ipif->ipif_pp_dst_addr == addr) ||
5982 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5983 			    ipif->ipif_lcl_addr == addr)) {
5984 				ipif_refhold_locked(ipif);
5985 				mutex_exit(&ill->ill_lock);
5986 				return (ipif);
5987 			}
5988 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5989 			ipif_refhold_locked(ipif);
5990 			mutex_exit(&ill->ill_lock);
5991 			return (ipif);
5992 		}
5993 	}
5994 	mutex_exit(&ill->ill_lock);
5995 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5996 	    NULL, MATCH_IRE_RECURSIVE, ipst);
5997 	if (ire != NULL) {
5998 		/*
5999 		 * The callers of this function wants to know the
6000 		 * interface on which they have to send the replies
6001 		 * back. For IREs that have ire_stq and ire_ipif
6002 		 * derived from different ills, we really don't care
6003 		 * what we return here.
6004 		 */
6005 		ipif = ire->ire_ipif;
6006 		if (ipif != NULL) {
6007 			ipif_refhold(ipif);
6008 			ire_refrele(ire);
6009 			return (ipif);
6010 		}
6011 		ire_refrele(ire);
6012 	}
6013 	/* Pick the first interface */
6014 	ipif = ipif_get_next_ipif(NULL, ill);
6015 	return (ipif);
6016 }
6017 
6018 /*
6019  * This func does not prevent refcnt from increasing. But if
6020  * the caller has taken steps to that effect, then this func
6021  * can be used to determine whether the ill has become quiescent
6022  */
6023 static boolean_t
6024 ill_is_quiescent(ill_t *ill)
6025 {
6026 	ipif_t	*ipif;
6027 
6028 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6029 
6030 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6031 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6032 			return (B_FALSE);
6033 		}
6034 	}
6035 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6036 		return (B_FALSE);
6037 	}
6038 	return (B_TRUE);
6039 }
6040 
6041 boolean_t
6042 ill_is_freeable(ill_t *ill)
6043 {
6044 	ipif_t	*ipif;
6045 
6046 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6047 
6048 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6049 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6050 			return (B_FALSE);
6051 		}
6052 	}
6053 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6054 		return (B_FALSE);
6055 	}
6056 	return (B_TRUE);
6057 }
6058 
6059 /*
6060  * This func does not prevent refcnt from increasing. But if
6061  * the caller has taken steps to that effect, then this func
6062  * can be used to determine whether the ipif has become quiescent
6063  */
6064 static boolean_t
6065 ipif_is_quiescent(ipif_t *ipif)
6066 {
6067 	ill_t *ill;
6068 
6069 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6070 
6071 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6072 		return (B_FALSE);
6073 	}
6074 
6075 	ill = ipif->ipif_ill;
6076 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6077 	    ill->ill_logical_down) {
6078 		return (B_TRUE);
6079 	}
6080 
6081 	/* This is the last ipif going down or being deleted on this ill */
6082 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6083 		return (B_FALSE);
6084 	}
6085 
6086 	return (B_TRUE);
6087 }
6088 
6089 /*
6090  * return true if the ipif can be destroyed: the ipif has to be quiescent
6091  * with zero references from ire/nce/ilm to it.
6092  */
6093 static boolean_t
6094 ipif_is_freeable(ipif_t *ipif)
6095 {
6096 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6097 	ASSERT(ipif->ipif_id != 0);
6098 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6099 }
6100 
6101 /*
6102  * The ipif/ill/ire has been refreled. Do the tail processing.
6103  * Determine if the ipif or ill in question has become quiescent and if so
6104  * wakeup close and/or restart any queued pending ioctl that is waiting
6105  * for the ipif_down (or ill_down)
6106  */
6107 void
6108 ipif_ill_refrele_tail(ill_t *ill)
6109 {
6110 	mblk_t	*mp;
6111 	conn_t	*connp;
6112 	ipsq_t	*ipsq;
6113 	ipxop_t	*ipx;
6114 	ipif_t	*ipif;
6115 	dl_notify_ind_t *dlindp;
6116 
6117 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6118 
6119 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6120 		/* ip_modclose() may be waiting */
6121 		cv_broadcast(&ill->ill_cv);
6122 	}
6123 
6124 	ipsq = ill->ill_phyint->phyint_ipsq;
6125 	mutex_enter(&ipsq->ipsq_lock);
6126 	ipx = ipsq->ipsq_xop;
6127 	mutex_enter(&ipx->ipx_lock);
6128 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6129 		goto unlock;
6130 
6131 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6132 
6133 	ipif = ipx->ipx_pending_ipif;
6134 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6135 		goto unlock;
6136 
6137 	switch (ipx->ipx_waitfor) {
6138 	case IPIF_DOWN:
6139 		if (!ipif_is_quiescent(ipif))
6140 			goto unlock;
6141 		break;
6142 	case IPIF_FREE:
6143 		if (!ipif_is_freeable(ipif))
6144 			goto unlock;
6145 		break;
6146 	case ILL_DOWN:
6147 		if (!ill_is_quiescent(ill))
6148 			goto unlock;
6149 		break;
6150 	case ILL_FREE:
6151 		/*
6152 		 * ILL_FREE is only for loopback; normal ill teardown waits
6153 		 * synchronously in ip_modclose() without using ipx_waitfor,
6154 		 * handled by the cv_broadcast() at the top of this function.
6155 		 */
6156 		if (!ill_is_freeable(ill))
6157 			goto unlock;
6158 		break;
6159 	default:
6160 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6161 		    (void *)ipsq, ipx->ipx_waitfor);
6162 	}
6163 
6164 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6165 	mutex_exit(&ipx->ipx_lock);
6166 	mp = ipsq_pending_mp_get(ipsq, &connp);
6167 	mutex_exit(&ipsq->ipsq_lock);
6168 	mutex_exit(&ill->ill_lock);
6169 
6170 	ASSERT(mp != NULL);
6171 	/*
6172 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6173 	 * we can only get here when the current operation decides it
6174 	 * it needs to quiesce via ipsq_pending_mp_add().
6175 	 */
6176 	switch (mp->b_datap->db_type) {
6177 	case M_PCPROTO:
6178 	case M_PROTO:
6179 		/*
6180 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6181 		 */
6182 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6183 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6184 
6185 		switch (dlindp->dl_notification) {
6186 		case DL_NOTE_PHYS_ADDR:
6187 			qwriter_ip(ill, ill->ill_rq, mp,
6188 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6189 			return;
6190 		case DL_NOTE_REPLUMB:
6191 			qwriter_ip(ill, ill->ill_rq, mp,
6192 			    ill_replumb_tail, CUR_OP, B_TRUE);
6193 			return;
6194 		default:
6195 			ASSERT(0);
6196 			ill_refrele(ill);
6197 		}
6198 		break;
6199 
6200 	case M_ERROR:
6201 	case M_HANGUP:
6202 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6203 		    B_TRUE);
6204 		return;
6205 
6206 	case M_IOCTL:
6207 	case M_IOCDATA:
6208 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6209 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6210 		return;
6211 
6212 	default:
6213 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6214 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6215 	}
6216 	return;
6217 unlock:
6218 	mutex_exit(&ipsq->ipsq_lock);
6219 	mutex_exit(&ipx->ipx_lock);
6220 	mutex_exit(&ill->ill_lock);
6221 }
6222 
6223 #ifdef DEBUG
6224 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6225 static void
6226 th_trace_rrecord(th_trace_t *th_trace)
6227 {
6228 	tr_buf_t *tr_buf;
6229 	uint_t lastref;
6230 
6231 	lastref = th_trace->th_trace_lastref;
6232 	lastref++;
6233 	if (lastref == TR_BUF_MAX)
6234 		lastref = 0;
6235 	th_trace->th_trace_lastref = lastref;
6236 	tr_buf = &th_trace->th_trbuf[lastref];
6237 	tr_buf->tr_time = lbolt;
6238 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6239 }
6240 
6241 static void
6242 th_trace_free(void *value)
6243 {
6244 	th_trace_t *th_trace = value;
6245 
6246 	ASSERT(th_trace->th_refcnt == 0);
6247 	kmem_free(th_trace, sizeof (*th_trace));
6248 }
6249 
6250 /*
6251  * Find or create the per-thread hash table used to track object references.
6252  * The ipst argument is NULL if we shouldn't allocate.
6253  *
6254  * Accesses per-thread data, so there's no need to lock here.
6255  */
6256 static mod_hash_t *
6257 th_trace_gethash(ip_stack_t *ipst)
6258 {
6259 	th_hash_t *thh;
6260 
6261 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6262 		mod_hash_t *mh;
6263 		char name[256];
6264 		size_t objsize, rshift;
6265 		int retv;
6266 
6267 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6268 			return (NULL);
6269 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6270 		    (void *)curthread);
6271 
6272 		/*
6273 		 * We use mod_hash_create_extended here rather than the more
6274 		 * obvious mod_hash_create_ptrhash because the latter has a
6275 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6276 		 * block.
6277 		 */
6278 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6279 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6280 		rshift = highbit(objsize);
6281 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6282 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6283 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6284 		if (mh == NULL) {
6285 			kmem_free(thh, sizeof (*thh));
6286 			return (NULL);
6287 		}
6288 		thh->thh_hash = mh;
6289 		thh->thh_ipst = ipst;
6290 		/*
6291 		 * We trace ills, ipifs, ires, and nces.  All of these are
6292 		 * per-IP-stack, so the lock on the thread list is as well.
6293 		 */
6294 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6295 		list_insert_tail(&ip_thread_list, thh);
6296 		rw_exit(&ip_thread_rwlock);
6297 		retv = tsd_set(ip_thread_data, thh);
6298 		ASSERT(retv == 0);
6299 	}
6300 	return (thh != NULL ? thh->thh_hash : NULL);
6301 }
6302 
6303 boolean_t
6304 th_trace_ref(const void *obj, ip_stack_t *ipst)
6305 {
6306 	th_trace_t *th_trace;
6307 	mod_hash_t *mh;
6308 	mod_hash_val_t val;
6309 
6310 	if ((mh = th_trace_gethash(ipst)) == NULL)
6311 		return (B_FALSE);
6312 
6313 	/*
6314 	 * Attempt to locate the trace buffer for this obj and thread.
6315 	 * If it does not exist, then allocate a new trace buffer and
6316 	 * insert into the hash.
6317 	 */
6318 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6319 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6320 		if (th_trace == NULL)
6321 			return (B_FALSE);
6322 
6323 		th_trace->th_id = curthread;
6324 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6325 		    (mod_hash_val_t)th_trace) != 0) {
6326 			kmem_free(th_trace, sizeof (th_trace_t));
6327 			return (B_FALSE);
6328 		}
6329 	} else {
6330 		th_trace = (th_trace_t *)val;
6331 	}
6332 
6333 	ASSERT(th_trace->th_refcnt >= 0 &&
6334 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6335 
6336 	th_trace->th_refcnt++;
6337 	th_trace_rrecord(th_trace);
6338 	return (B_TRUE);
6339 }
6340 
6341 /*
6342  * For the purpose of tracing a reference release, we assume that global
6343  * tracing is always on and that the same thread initiated the reference hold
6344  * is releasing.
6345  */
6346 void
6347 th_trace_unref(const void *obj)
6348 {
6349 	int retv;
6350 	mod_hash_t *mh;
6351 	th_trace_t *th_trace;
6352 	mod_hash_val_t val;
6353 
6354 	mh = th_trace_gethash(NULL);
6355 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6356 	ASSERT(retv == 0);
6357 	th_trace = (th_trace_t *)val;
6358 
6359 	ASSERT(th_trace->th_refcnt > 0);
6360 	th_trace->th_refcnt--;
6361 	th_trace_rrecord(th_trace);
6362 }
6363 
6364 /*
6365  * If tracing has been disabled, then we assume that the reference counts are
6366  * now useless, and we clear them out before destroying the entries.
6367  */
6368 void
6369 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6370 {
6371 	th_hash_t	*thh;
6372 	mod_hash_t	*mh;
6373 	mod_hash_val_t	val;
6374 	th_trace_t	*th_trace;
6375 	int		retv;
6376 
6377 	rw_enter(&ip_thread_rwlock, RW_READER);
6378 	for (thh = list_head(&ip_thread_list); thh != NULL;
6379 	    thh = list_next(&ip_thread_list, thh)) {
6380 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6381 		    &val) == 0) {
6382 			th_trace = (th_trace_t *)val;
6383 			if (trace_disable)
6384 				th_trace->th_refcnt = 0;
6385 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6386 			ASSERT(retv == 0);
6387 		}
6388 	}
6389 	rw_exit(&ip_thread_rwlock);
6390 }
6391 
6392 void
6393 ipif_trace_ref(ipif_t *ipif)
6394 {
6395 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6396 
6397 	if (ipif->ipif_trace_disable)
6398 		return;
6399 
6400 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6401 		ipif->ipif_trace_disable = B_TRUE;
6402 		ipif_trace_cleanup(ipif);
6403 	}
6404 }
6405 
6406 void
6407 ipif_untrace_ref(ipif_t *ipif)
6408 {
6409 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6410 
6411 	if (!ipif->ipif_trace_disable)
6412 		th_trace_unref(ipif);
6413 }
6414 
6415 void
6416 ill_trace_ref(ill_t *ill)
6417 {
6418 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6419 
6420 	if (ill->ill_trace_disable)
6421 		return;
6422 
6423 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6424 		ill->ill_trace_disable = B_TRUE;
6425 		ill_trace_cleanup(ill);
6426 	}
6427 }
6428 
6429 void
6430 ill_untrace_ref(ill_t *ill)
6431 {
6432 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6433 
6434 	if (!ill->ill_trace_disable)
6435 		th_trace_unref(ill);
6436 }
6437 
6438 /*
6439  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6440  * failure, ipif_trace_disable is set.
6441  */
6442 static void
6443 ipif_trace_cleanup(const ipif_t *ipif)
6444 {
6445 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6446 }
6447 
6448 /*
6449  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6450  * failure, ill_trace_disable is set.
6451  */
6452 static void
6453 ill_trace_cleanup(const ill_t *ill)
6454 {
6455 	th_trace_cleanup(ill, ill->ill_trace_disable);
6456 }
6457 #endif /* DEBUG */
6458 
6459 void
6460 ipif_refhold_locked(ipif_t *ipif)
6461 {
6462 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6463 	ipif->ipif_refcnt++;
6464 	IPIF_TRACE_REF(ipif);
6465 }
6466 
6467 void
6468 ipif_refhold(ipif_t *ipif)
6469 {
6470 	ill_t	*ill;
6471 
6472 	ill = ipif->ipif_ill;
6473 	mutex_enter(&ill->ill_lock);
6474 	ipif->ipif_refcnt++;
6475 	IPIF_TRACE_REF(ipif);
6476 	mutex_exit(&ill->ill_lock);
6477 }
6478 
6479 /*
6480  * Must not be called while holding any locks. Otherwise if this is
6481  * the last reference to be released there is a chance of recursive mutex
6482  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6483  * to restart an ioctl.
6484  */
6485 void
6486 ipif_refrele(ipif_t *ipif)
6487 {
6488 	ill_t	*ill;
6489 
6490 	ill = ipif->ipif_ill;
6491 
6492 	mutex_enter(&ill->ill_lock);
6493 	ASSERT(ipif->ipif_refcnt != 0);
6494 	ipif->ipif_refcnt--;
6495 	IPIF_UNTRACE_REF(ipif);
6496 	if (ipif->ipif_refcnt != 0) {
6497 		mutex_exit(&ill->ill_lock);
6498 		return;
6499 	}
6500 
6501 	/* Drops the ill_lock */
6502 	ipif_ill_refrele_tail(ill);
6503 }
6504 
6505 ipif_t *
6506 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6507 {
6508 	ipif_t	*ipif;
6509 
6510 	mutex_enter(&ill->ill_lock);
6511 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6512 	    ipif != NULL; ipif = ipif->ipif_next) {
6513 		if (!IPIF_CAN_LOOKUP(ipif))
6514 			continue;
6515 		ipif_refhold_locked(ipif);
6516 		mutex_exit(&ill->ill_lock);
6517 		return (ipif);
6518 	}
6519 	mutex_exit(&ill->ill_lock);
6520 	return (NULL);
6521 }
6522 
6523 /*
6524  * TODO: make this table extendible at run time
6525  * Return a pointer to the mac type info for 'mac_type'
6526  */
6527 static ip_m_t *
6528 ip_m_lookup(t_uscalar_t mac_type)
6529 {
6530 	ip_m_t	*ipm;
6531 
6532 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6533 		if (ipm->ip_m_mac_type == mac_type)
6534 			return (ipm);
6535 	return (NULL);
6536 }
6537 
6538 /*
6539  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6540  * ipif_arg is passed in to associate it with the correct interface.
6541  * We may need to restart this operation if the ipif cannot be looked up
6542  * due to an exclusive operation that is currently in progress. The restart
6543  * entry point is specified by 'func'
6544  */
6545 int
6546 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6547     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6548     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6549     struct rtsa_s *sp, ip_stack_t *ipst)
6550 {
6551 	ire_t	*ire;
6552 	ire_t	*gw_ire = NULL;
6553 	ipif_t	*ipif = NULL;
6554 	boolean_t ipif_refheld = B_FALSE;
6555 	uint_t	type;
6556 	int	match_flags = MATCH_IRE_TYPE;
6557 	int	error;
6558 	tsol_gc_t *gc = NULL;
6559 	tsol_gcgrp_t *gcgrp = NULL;
6560 	boolean_t gcgrp_xtraref = B_FALSE;
6561 
6562 	ip1dbg(("ip_rt_add:"));
6563 
6564 	if (ire_arg != NULL)
6565 		*ire_arg = NULL;
6566 
6567 	/*
6568 	 * If this is the case of RTF_HOST being set, then we set the netmask
6569 	 * to all ones (regardless if one was supplied).
6570 	 */
6571 	if (flags & RTF_HOST)
6572 		mask = IP_HOST_MASK;
6573 
6574 	/*
6575 	 * Prevent routes with a zero gateway from being created (since
6576 	 * interfaces can currently be plumbed and brought up no assigned
6577 	 * address).
6578 	 */
6579 	if (gw_addr == 0)
6580 		return (ENETUNREACH);
6581 	/*
6582 	 * Get the ipif, if any, corresponding to the gw_addr
6583 	 */
6584 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6585 	    ipst);
6586 	if (ipif != NULL) {
6587 		if (IS_VNI(ipif->ipif_ill)) {
6588 			ipif_refrele(ipif);
6589 			return (EINVAL);
6590 		}
6591 		ipif_refheld = B_TRUE;
6592 	} else if (error == EINPROGRESS) {
6593 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6594 		return (EINPROGRESS);
6595 	} else {
6596 		error = 0;
6597 	}
6598 
6599 	if (ipif != NULL) {
6600 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6601 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6602 	} else {
6603 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6604 	}
6605 
6606 	/*
6607 	 * GateD will attempt to create routes with a loopback interface
6608 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6609 	 * these routes to be added, but create them as interface routes
6610 	 * since the gateway is an interface address.
6611 	 */
6612 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6613 		flags &= ~RTF_GATEWAY;
6614 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6615 		    mask == IP_HOST_MASK) {
6616 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6617 			    ALL_ZONES, NULL, match_flags, ipst);
6618 			if (ire != NULL) {
6619 				ire_refrele(ire);
6620 				if (ipif_refheld)
6621 					ipif_refrele(ipif);
6622 				return (EEXIST);
6623 			}
6624 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
6625 			    "for 0x%x\n", (void *)ipif,
6626 			    ipif->ipif_ire_type,
6627 			    ntohl(ipif->ipif_lcl_addr)));
6628 			ire = ire_create(
6629 			    (uchar_t *)&dst_addr,	/* dest address */
6630 			    (uchar_t *)&mask,		/* mask */
6631 			    (uchar_t *)&ipif->ipif_src_addr,
6632 			    NULL,			/* no gateway */
6633 			    &ipif->ipif_mtu,
6634 			    NULL,
6635 			    ipif->ipif_rq,		/* recv-from queue */
6636 			    NULL,			/* no send-to queue */
6637 			    ipif->ipif_ire_type,	/* LOOPBACK */
6638 			    ipif,
6639 			    0,
6640 			    0,
6641 			    0,
6642 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6643 			    RTF_PRIVATE : 0,
6644 			    &ire_uinfo_null,
6645 			    NULL,
6646 			    NULL,
6647 			    ipst);
6648 
6649 			if (ire == NULL) {
6650 				if (ipif_refheld)
6651 					ipif_refrele(ipif);
6652 				return (ENOMEM);
6653 			}
6654 			error = ire_add(&ire, q, mp, func, B_FALSE);
6655 			if (error == 0)
6656 				goto save_ire;
6657 			if (ipif_refheld)
6658 				ipif_refrele(ipif);
6659 			return (error);
6660 
6661 		}
6662 	}
6663 
6664 	/*
6665 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6666 	 * and the gateway address provided is one of the system's interface
6667 	 * addresses.  By using the routing socket interface and supplying an
6668 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6669 	 * specifying an interface route to be created is available which uses
6670 	 * the interface index that specifies the outgoing interface rather than
6671 	 * the address of an outgoing interface (which may not be able to
6672 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6673 	 * flag, routes can be specified which not only specify the next-hop to
6674 	 * be used when routing to a certain prefix, but also which outgoing
6675 	 * interface should be used.
6676 	 *
6677 	 * Previously, interfaces would have unique addresses assigned to them
6678 	 * and so the address assigned to a particular interface could be used
6679 	 * to identify a particular interface.  One exception to this was the
6680 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6681 	 *
6682 	 * With the advent of IPv6 and its link-local addresses, this
6683 	 * restriction was relaxed and interfaces could share addresses between
6684 	 * themselves.  In fact, typically all of the link-local interfaces on
6685 	 * an IPv6 node or router will have the same link-local address.  In
6686 	 * order to differentiate between these interfaces, the use of an
6687 	 * interface index is necessary and this index can be carried inside a
6688 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6689 	 * of using the interface index, however, is that all of the ipif's that
6690 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6691 	 * cannot be used to differentiate between ipif's (or logical
6692 	 * interfaces) that belong to the same ill (physical interface).
6693 	 *
6694 	 * For example, in the following case involving IPv4 interfaces and
6695 	 * logical interfaces
6696 	 *
6697 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6698 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6699 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6700 	 *
6701 	 * the ipif's corresponding to each of these interface routes can be
6702 	 * uniquely identified by the "gateway" (actually interface address).
6703 	 *
6704 	 * In this case involving multiple IPv6 default routes to a particular
6705 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6706 	 * default route is of interest:
6707 	 *
6708 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6709 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6710 	 */
6711 
6712 	/* RTF_GATEWAY not set */
6713 	if (!(flags & RTF_GATEWAY)) {
6714 		queue_t	*stq;
6715 
6716 		if (sp != NULL) {
6717 			ip2dbg(("ip_rt_add: gateway security attributes "
6718 			    "cannot be set with interface route\n"));
6719 			if (ipif_refheld)
6720 				ipif_refrele(ipif);
6721 			return (EINVAL);
6722 		}
6723 
6724 		/*
6725 		 * As the interface index specified with the RTA_IFP sockaddr is
6726 		 * the same for all ipif's off of an ill, the matching logic
6727 		 * below uses MATCH_IRE_ILL if such an index was specified.
6728 		 * This means that routes sharing the same prefix when added
6729 		 * using a RTA_IFP sockaddr must have distinct interface
6730 		 * indices (namely, they must be on distinct ill's).
6731 		 *
6732 		 * On the other hand, since the gateway address will usually be
6733 		 * different for each ipif on the system, the matching logic
6734 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6735 		 * route.  This means that interface routes for the same prefix
6736 		 * can be created if they belong to distinct ipif's and if a
6737 		 * RTA_IFP sockaddr is not present.
6738 		 */
6739 		if (ipif_arg != NULL) {
6740 			if (ipif_refheld)  {
6741 				ipif_refrele(ipif);
6742 				ipif_refheld = B_FALSE;
6743 			}
6744 			ipif = ipif_arg;
6745 			match_flags |= MATCH_IRE_ILL;
6746 		} else {
6747 			/*
6748 			 * Check the ipif corresponding to the gw_addr
6749 			 */
6750 			if (ipif == NULL)
6751 				return (ENETUNREACH);
6752 			match_flags |= MATCH_IRE_IPIF;
6753 		}
6754 		ASSERT(ipif != NULL);
6755 
6756 		/*
6757 		 * We check for an existing entry at this point.
6758 		 *
6759 		 * Since a netmask isn't passed in via the ioctl interface
6760 		 * (SIOCADDRT), we don't check for a matching netmask in that
6761 		 * case.
6762 		 */
6763 		if (!ioctl_msg)
6764 			match_flags |= MATCH_IRE_MASK;
6765 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6766 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6767 		if (ire != NULL) {
6768 			ire_refrele(ire);
6769 			if (ipif_refheld)
6770 				ipif_refrele(ipif);
6771 			return (EEXIST);
6772 		}
6773 
6774 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6775 		    ? ipif->ipif_rq : ipif->ipif_wq;
6776 
6777 		/*
6778 		 * Create a copy of the IRE_LOOPBACK,
6779 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6780 		 * the modified address and netmask.
6781 		 */
6782 		ire = ire_create(
6783 		    (uchar_t *)&dst_addr,
6784 		    (uint8_t *)&mask,
6785 		    (uint8_t *)&ipif->ipif_src_addr,
6786 		    NULL,
6787 		    &ipif->ipif_mtu,
6788 		    NULL,
6789 		    NULL,
6790 		    stq,
6791 		    ipif->ipif_net_type,
6792 		    ipif,
6793 		    0,
6794 		    0,
6795 		    0,
6796 		    flags,
6797 		    &ire_uinfo_null,
6798 		    NULL,
6799 		    NULL,
6800 		    ipst);
6801 		if (ire == NULL) {
6802 			if (ipif_refheld)
6803 				ipif_refrele(ipif);
6804 			return (ENOMEM);
6805 		}
6806 
6807 		/*
6808 		 * Some software (for example, GateD and Sun Cluster) attempts
6809 		 * to create (what amount to) IRE_PREFIX routes with the
6810 		 * loopback address as the gateway.  This is primarily done to
6811 		 * set up prefixes with the RTF_REJECT flag set (for example,
6812 		 * when generating aggregate routes.)
6813 		 *
6814 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6815 		 * IRE_LOOPBACK, then we map the request into a
6816 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6817 		 * these interface routes, by definition, can only be that.
6818 		 *
6819 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6820 		 * routine, but rather using ire_create() directly.
6821 		 *
6822 		 */
6823 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6824 			ire->ire_type = IRE_IF_NORESOLVER;
6825 			ire->ire_flags |= RTF_BLACKHOLE;
6826 		}
6827 
6828 		error = ire_add(&ire, q, mp, func, B_FALSE);
6829 		if (error == 0)
6830 			goto save_ire;
6831 
6832 		/*
6833 		 * In the result of failure, ire_add() will have already
6834 		 * deleted the ire in question, so there is no need to
6835 		 * do that here.
6836 		 */
6837 		if (ipif_refheld)
6838 			ipif_refrele(ipif);
6839 		return (error);
6840 	}
6841 	if (ipif_refheld) {
6842 		ipif_refrele(ipif);
6843 		ipif_refheld = B_FALSE;
6844 	}
6845 
6846 	/*
6847 	 * Get an interface IRE for the specified gateway.
6848 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6849 	 * gateway, it is currently unreachable and we fail the request
6850 	 * accordingly.
6851 	 */
6852 	ipif = ipif_arg;
6853 	if (ipif_arg != NULL)
6854 		match_flags |= MATCH_IRE_ILL;
6855 again:
6856 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6857 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6858 	if (gw_ire == NULL) {
6859 		/*
6860 		 * With IPMP, we allow host routes to influence in.mpathd's
6861 		 * target selection.  However, if the test addresses are on
6862 		 * their own network, the above lookup will fail since the
6863 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6864 		 * hidden test IREs to be found and try again.
6865 		 */
6866 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6867 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6868 			goto again;
6869 		}
6870 		return (ENETUNREACH);
6871 	}
6872 
6873 	/*
6874 	 * We create one of three types of IREs as a result of this request
6875 	 * based on the netmask.  A netmask of all ones (which is automatically
6876 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6877 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6878 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6879 	 * destination prefix.
6880 	 */
6881 	if (mask == IP_HOST_MASK)
6882 		type = IRE_HOST;
6883 	else if (mask == 0)
6884 		type = IRE_DEFAULT;
6885 	else
6886 		type = IRE_PREFIX;
6887 
6888 	/* check for a duplicate entry */
6889 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6890 	    NULL, ALL_ZONES, 0, NULL,
6891 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
6892 	if (ire != NULL) {
6893 		ire_refrele(gw_ire);
6894 		ire_refrele(ire);
6895 		return (EEXIST);
6896 	}
6897 
6898 	/* Security attribute exists */
6899 	if (sp != NULL) {
6900 		tsol_gcgrp_addr_t ga;
6901 
6902 		/* find or create the gateway credentials group */
6903 		ga.ga_af = AF_INET;
6904 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6905 
6906 		/* we hold reference to it upon success */
6907 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6908 		if (gcgrp == NULL) {
6909 			ire_refrele(gw_ire);
6910 			return (ENOMEM);
6911 		}
6912 
6913 		/*
6914 		 * Create and add the security attribute to the group; a
6915 		 * reference to the group is made upon allocating a new
6916 		 * entry successfully.  If it finds an already-existing
6917 		 * entry for the security attribute in the group, it simply
6918 		 * returns it and no new reference is made to the group.
6919 		 */
6920 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6921 		if (gc == NULL) {
6922 			/* release reference held by gcgrp_lookup */
6923 			GCGRP_REFRELE(gcgrp);
6924 			ire_refrele(gw_ire);
6925 			return (ENOMEM);
6926 		}
6927 	}
6928 
6929 	/* Create the IRE. */
6930 	ire = ire_create(
6931 	    (uchar_t *)&dst_addr,		/* dest address */
6932 	    (uchar_t *)&mask,			/* mask */
6933 	    /* src address assigned by the caller? */
6934 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6935 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
6936 	    (uchar_t *)&gw_addr,		/* gateway address */
6937 	    &gw_ire->ire_max_frag,
6938 	    NULL,				/* no src nce */
6939 	    NULL,				/* no recv-from queue */
6940 	    NULL,				/* no send-to queue */
6941 	    (ushort_t)type,			/* IRE type */
6942 	    ipif_arg,
6943 	    0,
6944 	    0,
6945 	    0,
6946 	    flags,
6947 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6948 	    gc,					/* security attribute */
6949 	    NULL,
6950 	    ipst);
6951 
6952 	/*
6953 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6954 	 * reference to the 'gcgrp'. We can now release the extra reference
6955 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6956 	 */
6957 	if (gcgrp_xtraref)
6958 		GCGRP_REFRELE(gcgrp);
6959 	if (ire == NULL) {
6960 		if (gc != NULL)
6961 			GC_REFRELE(gc);
6962 		ire_refrele(gw_ire);
6963 		return (ENOMEM);
6964 	}
6965 
6966 	/*
6967 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6968 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6969 	 */
6970 
6971 	/* Add the new IRE. */
6972 	error = ire_add(&ire, q, mp, func, B_FALSE);
6973 	if (error != 0) {
6974 		/*
6975 		 * In the result of failure, ire_add() will have already
6976 		 * deleted the ire in question, so there is no need to
6977 		 * do that here.
6978 		 */
6979 		ire_refrele(gw_ire);
6980 		return (error);
6981 	}
6982 
6983 	if (flags & RTF_MULTIRT) {
6984 		/*
6985 		 * Invoke the CGTP (multirouting) filtering module
6986 		 * to add the dst address in the filtering database.
6987 		 * Replicated inbound packets coming from that address
6988 		 * will be filtered to discard the duplicates.
6989 		 * It is not necessary to call the CGTP filter hook
6990 		 * when the dst address is a broadcast or multicast,
6991 		 * because an IP source address cannot be a broadcast
6992 		 * or a multicast.
6993 		 */
6994 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6995 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
6996 		if (ire_dst != NULL) {
6997 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
6998 			ire_refrele(ire_dst);
6999 			goto save_ire;
7000 		}
7001 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7002 		    !CLASSD(ire->ire_addr)) {
7003 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7004 			    ipst->ips_netstack->netstack_stackid,
7005 			    ire->ire_addr,
7006 			    ire->ire_gateway_addr,
7007 			    ire->ire_src_addr,
7008 			    gw_ire->ire_src_addr);
7009 			if (res != 0) {
7010 				ire_refrele(gw_ire);
7011 				ire_delete(ire);
7012 				return (res);
7013 			}
7014 		}
7015 	}
7016 
7017 	/*
7018 	 * Now that the prefix IRE entry has been created, delete any
7019 	 * existing gateway IRE cache entries as well as any IRE caches
7020 	 * using the gateway, and force them to be created through
7021 	 * ip_newroute.
7022 	 */
7023 	if (gc != NULL) {
7024 		ASSERT(gcgrp != NULL);
7025 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7026 	}
7027 
7028 save_ire:
7029 	if (gw_ire != NULL) {
7030 		ire_refrele(gw_ire);
7031 	}
7032 	if (ipif != NULL) {
7033 		/*
7034 		 * Save enough information so that we can recreate the IRE if
7035 		 * the interface goes down and then up.  The metrics associated
7036 		 * with the route will be saved as well when rts_setmetrics() is
7037 		 * called after the IRE has been created.  In the case where
7038 		 * memory cannot be allocated, none of this information will be
7039 		 * saved.
7040 		 */
7041 		ipif_save_ire(ipif, ire);
7042 	}
7043 	if (ioctl_msg)
7044 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7045 	if (ire_arg != NULL) {
7046 		/*
7047 		 * Store the ire that was successfully added into where ire_arg
7048 		 * points to so that callers don't have to look it up
7049 		 * themselves (but they are responsible for ire_refrele()ing
7050 		 * the ire when they are finished with it).
7051 		 */
7052 		*ire_arg = ire;
7053 	} else {
7054 		ire_refrele(ire);		/* Held in ire_add */
7055 	}
7056 	if (ipif_refheld)
7057 		ipif_refrele(ipif);
7058 	return (0);
7059 }
7060 
7061 /*
7062  * ip_rt_delete is called to delete an IPv4 route.
7063  * ipif_arg is passed in to associate it with the correct interface.
7064  * We may need to restart this operation if the ipif cannot be looked up
7065  * due to an exclusive operation that is currently in progress. The restart
7066  * entry point is specified by 'func'
7067  */
7068 /* ARGSUSED4 */
7069 int
7070 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7071     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7072     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7073 {
7074 	ire_t	*ire = NULL;
7075 	ipif_t	*ipif;
7076 	boolean_t ipif_refheld = B_FALSE;
7077 	uint_t	type;
7078 	uint_t	match_flags = MATCH_IRE_TYPE;
7079 	int	err = 0;
7080 
7081 	ip1dbg(("ip_rt_delete:"));
7082 	/*
7083 	 * If this is the case of RTF_HOST being set, then we set the netmask
7084 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7085 	 */
7086 	if (flags & RTF_HOST) {
7087 		mask = IP_HOST_MASK;
7088 		match_flags |= MATCH_IRE_MASK;
7089 	} else if (rtm_addrs & RTA_NETMASK) {
7090 		match_flags |= MATCH_IRE_MASK;
7091 	}
7092 
7093 	/*
7094 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7095 	 * we check if the gateway address is one of our interfaces first,
7096 	 * and fall back on RTF_GATEWAY routes.
7097 	 *
7098 	 * This makes it possible to delete an original
7099 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7100 	 *
7101 	 * As the interface index specified with the RTA_IFP sockaddr is the
7102 	 * same for all ipif's off of an ill, the matching logic below uses
7103 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7104 	 * sharing the same prefix and interface index as the the route
7105 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7106 	 * is specified in the request.
7107 	 *
7108 	 * On the other hand, since the gateway address will usually be
7109 	 * different for each ipif on the system, the matching logic
7110 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7111 	 * route.  This means that interface routes for the same prefix can be
7112 	 * uniquely identified if they belong to distinct ipif's and if a
7113 	 * RTA_IFP sockaddr is not present.
7114 	 *
7115 	 * For more detail on specifying routes by gateway address and by
7116 	 * interface index, see the comments in ip_rt_add().
7117 	 */
7118 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7119 	    ipst);
7120 	if (ipif != NULL)
7121 		ipif_refheld = B_TRUE;
7122 	else if (err == EINPROGRESS)
7123 		return (err);
7124 	else
7125 		err = 0;
7126 	if (ipif != NULL) {
7127 		if (ipif_arg != NULL) {
7128 			if (ipif_refheld) {
7129 				ipif_refrele(ipif);
7130 				ipif_refheld = B_FALSE;
7131 			}
7132 			ipif = ipif_arg;
7133 			match_flags |= MATCH_IRE_ILL;
7134 		} else {
7135 			match_flags |= MATCH_IRE_IPIF;
7136 		}
7137 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7138 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7139 			    ALL_ZONES, NULL, match_flags, ipst);
7140 		}
7141 		if (ire == NULL) {
7142 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7143 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7144 			    match_flags, ipst);
7145 		}
7146 	}
7147 
7148 	if (ire == NULL) {
7149 		/*
7150 		 * At this point, the gateway address is not one of our own
7151 		 * addresses or a matching interface route was not found.  We
7152 		 * set the IRE type to lookup based on whether
7153 		 * this is a host route, a default route or just a prefix.
7154 		 *
7155 		 * If an ipif_arg was passed in, then the lookup is based on an
7156 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7157 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7158 		 * set as the route being looked up is not a traditional
7159 		 * interface route.
7160 		 */
7161 		match_flags &= ~MATCH_IRE_IPIF;
7162 		match_flags |= MATCH_IRE_GW;
7163 		if (ipif_arg != NULL)
7164 			match_flags |= MATCH_IRE_ILL;
7165 		if (mask == IP_HOST_MASK)
7166 			type = IRE_HOST;
7167 		else if (mask == 0)
7168 			type = IRE_DEFAULT;
7169 		else
7170 			type = IRE_PREFIX;
7171 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7172 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7173 	}
7174 
7175 	if (ipif_refheld)
7176 		ipif_refrele(ipif);
7177 
7178 	/* ipif is not refheld anymore */
7179 	if (ire == NULL)
7180 		return (ESRCH);
7181 
7182 	if (ire->ire_flags & RTF_MULTIRT) {
7183 		/*
7184 		 * Invoke the CGTP (multirouting) filtering module
7185 		 * to remove the dst address from the filtering database.
7186 		 * Packets coming from that address will no longer be
7187 		 * filtered to remove duplicates.
7188 		 */
7189 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7190 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7191 			    ipst->ips_netstack->netstack_stackid,
7192 			    ire->ire_addr, ire->ire_gateway_addr);
7193 		}
7194 		ip_cgtp_bcast_delete(ire, ipst);
7195 	}
7196 
7197 	ipif = ire->ire_ipif;
7198 	if (ipif != NULL)
7199 		ipif_remove_ire(ipif, ire);
7200 	if (ioctl_msg)
7201 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7202 	ire_delete(ire);
7203 	ire_refrele(ire);
7204 	return (err);
7205 }
7206 
7207 /*
7208  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7209  */
7210 /* ARGSUSED */
7211 int
7212 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7213     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7214 {
7215 	ipaddr_t dst_addr;
7216 	ipaddr_t gw_addr;
7217 	ipaddr_t mask;
7218 	int error = 0;
7219 	mblk_t *mp1;
7220 	struct rtentry *rt;
7221 	ipif_t *ipif = NULL;
7222 	ip_stack_t	*ipst;
7223 
7224 	ASSERT(q->q_next == NULL);
7225 	ipst = CONNQ_TO_IPST(q);
7226 
7227 	ip1dbg(("ip_siocaddrt:"));
7228 	/* Existence of mp1 verified in ip_wput_nondata */
7229 	mp1 = mp->b_cont->b_cont;
7230 	rt = (struct rtentry *)mp1->b_rptr;
7231 
7232 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7233 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7234 
7235 	/*
7236 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7237 	 * to a particular host address.  In this case, we set the netmask to
7238 	 * all ones for the particular destination address.  Otherwise,
7239 	 * determine the netmask to be used based on dst_addr and the interfaces
7240 	 * in use.
7241 	 */
7242 	if (rt->rt_flags & RTF_HOST) {
7243 		mask = IP_HOST_MASK;
7244 	} else {
7245 		/*
7246 		 * Note that ip_subnet_mask returns a zero mask in the case of
7247 		 * default (an all-zeroes address).
7248 		 */
7249 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7250 	}
7251 
7252 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7253 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7254 	if (ipif != NULL)
7255 		ipif_refrele(ipif);
7256 	return (error);
7257 }
7258 
7259 /*
7260  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7261  */
7262 /* ARGSUSED */
7263 int
7264 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7265     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7266 {
7267 	ipaddr_t dst_addr;
7268 	ipaddr_t gw_addr;
7269 	ipaddr_t mask;
7270 	int error;
7271 	mblk_t *mp1;
7272 	struct rtentry *rt;
7273 	ipif_t *ipif = NULL;
7274 	ip_stack_t	*ipst;
7275 
7276 	ASSERT(q->q_next == NULL);
7277 	ipst = CONNQ_TO_IPST(q);
7278 
7279 	ip1dbg(("ip_siocdelrt:"));
7280 	/* Existence of mp1 verified in ip_wput_nondata */
7281 	mp1 = mp->b_cont->b_cont;
7282 	rt = (struct rtentry *)mp1->b_rptr;
7283 
7284 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7285 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7286 
7287 	/*
7288 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7289 	 * to a particular host address.  In this case, we set the netmask to
7290 	 * all ones for the particular destination address.  Otherwise,
7291 	 * determine the netmask to be used based on dst_addr and the interfaces
7292 	 * in use.
7293 	 */
7294 	if (rt->rt_flags & RTF_HOST) {
7295 		mask = IP_HOST_MASK;
7296 	} else {
7297 		/*
7298 		 * Note that ip_subnet_mask returns a zero mask in the case of
7299 		 * default (an all-zeroes address).
7300 		 */
7301 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7302 	}
7303 
7304 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7305 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7306 	    mp, ip_process_ioctl, ipst);
7307 	if (ipif != NULL)
7308 		ipif_refrele(ipif);
7309 	return (error);
7310 }
7311 
7312 /*
7313  * Enqueue the mp onto the ipsq, chained by b_next.
7314  * b_prev stores the function to be executed later, and b_queue the queue
7315  * where this mp originated.
7316  */
7317 void
7318 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7319     ill_t *pending_ill)
7320 {
7321 	conn_t	*connp;
7322 	ipxop_t *ipx = ipsq->ipsq_xop;
7323 
7324 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7325 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7326 	ASSERT(func != NULL);
7327 
7328 	mp->b_queue = q;
7329 	mp->b_prev = (void *)func;
7330 	mp->b_next = NULL;
7331 
7332 	switch (type) {
7333 	case CUR_OP:
7334 		if (ipx->ipx_mptail != NULL) {
7335 			ASSERT(ipx->ipx_mphead != NULL);
7336 			ipx->ipx_mptail->b_next = mp;
7337 		} else {
7338 			ASSERT(ipx->ipx_mphead == NULL);
7339 			ipx->ipx_mphead = mp;
7340 		}
7341 		ipx->ipx_mptail = mp;
7342 		break;
7343 
7344 	case NEW_OP:
7345 		if (ipsq->ipsq_xopq_mptail != NULL) {
7346 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7347 			ipsq->ipsq_xopq_mptail->b_next = mp;
7348 		} else {
7349 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7350 			ipsq->ipsq_xopq_mphead = mp;
7351 		}
7352 		ipsq->ipsq_xopq_mptail = mp;
7353 		ipx->ipx_ipsq_queued = B_TRUE;
7354 		break;
7355 
7356 	case SWITCH_OP:
7357 		ASSERT(ipsq->ipsq_swxop != NULL);
7358 		/* only one switch operation is currently allowed */
7359 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7360 		ipsq->ipsq_switch_mp = mp;
7361 		ipx->ipx_ipsq_queued = B_TRUE;
7362 		break;
7363 	default:
7364 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7365 	}
7366 
7367 	if (CONN_Q(q) && pending_ill != NULL) {
7368 		connp = Q_TO_CONN(q);
7369 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7370 		connp->conn_oper_pending_ill = pending_ill;
7371 	}
7372 }
7373 
7374 /*
7375  * Dequeue the next message that requested exclusive access to this IPSQ's
7376  * xop.  Specifically:
7377  *
7378  *  1. If we're still processing the current operation on `ipsq', then
7379  *     dequeue the next message for the operation (from ipx_mphead), or
7380  *     return NULL if there are no queued messages for the operation.
7381  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7382  *
7383  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7384  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7385  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7386  *     leaving IPMP groups and require a careful dance -- see the comments
7387  *     in-line below for details.  If we're leaving a group xop or if we're
7388  *     joining a group xop and become writer on it, then we proceed to (3).
7389  *     Otherwise, we return NULL and exit the xop.
7390  *
7391  *  3. For each IPSQ in the xop, return any switch operation stored on
7392  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7393  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7394  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7395  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7396  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7397  *     each phyint in the group, including the IPMP meta-interface phyint.
7398  */
7399 static mblk_t *
7400 ipsq_dq(ipsq_t *ipsq)
7401 {
7402 	ill_t	*illv4, *illv6;
7403 	mblk_t	*mp;
7404 	ipsq_t	*xopipsq;
7405 	ipsq_t	*leftipsq = NULL;
7406 	ipxop_t *ipx;
7407 	phyint_t *phyi = ipsq->ipsq_phyint;
7408 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7409 	boolean_t emptied = B_FALSE;
7410 
7411 	/*
7412 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7413 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7414 	 */
7415 	rw_enter(&ipst->ips_ill_g_lock,
7416 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7417 	mutex_enter(&ipsq->ipsq_lock);
7418 	ipx = ipsq->ipsq_xop;
7419 	mutex_enter(&ipx->ipx_lock);
7420 
7421 	/*
7422 	 * Dequeue the next message associated with the current exclusive
7423 	 * operation, if any.
7424 	 */
7425 	if ((mp = ipx->ipx_mphead) != NULL) {
7426 		ipx->ipx_mphead = mp->b_next;
7427 		if (ipx->ipx_mphead == NULL)
7428 			ipx->ipx_mptail = NULL;
7429 		mp->b_next = (void *)ipsq;
7430 		goto out;
7431 	}
7432 
7433 	if (ipx->ipx_current_ipif != NULL)
7434 		goto empty;
7435 
7436 	if (ipsq->ipsq_swxop != NULL) {
7437 		/*
7438 		 * The exclusive operation that is now being completed has
7439 		 * requested a switch to a different xop.  This happens
7440 		 * when an interface joins or leaves an IPMP group.  Joins
7441 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7442 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7443 		 * (phyint_free()), or interface plumb for an ill type
7444 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7445 		 *
7446 		 * Xop switches are not allowed on the IPMP meta-interface.
7447 		 */
7448 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7449 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7450 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7451 
7452 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7453 			/*
7454 			 * We're switching back to our own xop, so we have two
7455 			 * xop's to drain/exit: our own, and the group xop
7456 			 * that we are leaving.
7457 			 *
7458 			 * First, pull ourselves out of the group ipsq list.
7459 			 * This is safe since we're writer on ill_g_lock.
7460 			 */
7461 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7462 
7463 			xopipsq = ipx->ipx_ipsq;
7464 			while (xopipsq->ipsq_next != ipsq)
7465 				xopipsq = xopipsq->ipsq_next;
7466 
7467 			xopipsq->ipsq_next = ipsq->ipsq_next;
7468 			ipsq->ipsq_next = ipsq;
7469 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7470 			ipsq->ipsq_swxop = NULL;
7471 
7472 			/*
7473 			 * Second, prepare to exit the group xop.  The actual
7474 			 * ipsq_exit() is done at the end of this function
7475 			 * since we cannot hold any locks across ipsq_exit().
7476 			 * Note that although we drop the group's ipx_lock, no
7477 			 * threads can proceed since we're still ipx_writer.
7478 			 */
7479 			leftipsq = xopipsq;
7480 			mutex_exit(&ipx->ipx_lock);
7481 
7482 			/*
7483 			 * Third, set ipx to point to our own xop (which was
7484 			 * inactive and therefore can be entered).
7485 			 */
7486 			ipx = ipsq->ipsq_xop;
7487 			mutex_enter(&ipx->ipx_lock);
7488 			ASSERT(ipx->ipx_writer == NULL);
7489 			ASSERT(ipx->ipx_current_ipif == NULL);
7490 		} else {
7491 			/*
7492 			 * We're switching from our own xop to a group xop.
7493 			 * The requestor of the switch must ensure that the
7494 			 * group xop cannot go away (e.g. by ensuring the
7495 			 * phyint associated with the xop cannot go away).
7496 			 *
7497 			 * If we can become writer on our new xop, then we'll
7498 			 * do the drain.  Otherwise, the current writer of our
7499 			 * new xop will do the drain when it exits.
7500 			 *
7501 			 * First, splice ourselves into the group IPSQ list.
7502 			 * This is safe since we're writer on ill_g_lock.
7503 			 */
7504 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7505 
7506 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7507 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7508 				xopipsq = xopipsq->ipsq_next;
7509 
7510 			xopipsq->ipsq_next = ipsq;
7511 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7512 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7513 			ipsq->ipsq_swxop = NULL;
7514 
7515 			/*
7516 			 * Second, exit our own xop, since it's now unused.
7517 			 * This is safe since we've got the only reference.
7518 			 */
7519 			ASSERT(ipx->ipx_writer == curthread);
7520 			ipx->ipx_writer = NULL;
7521 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7522 			ipx->ipx_ipsq_queued = B_FALSE;
7523 			mutex_exit(&ipx->ipx_lock);
7524 
7525 			/*
7526 			 * Third, set ipx to point to our new xop, and check
7527 			 * if we can become writer on it.  If we cannot, then
7528 			 * the current writer will drain the IPSQ group when
7529 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7530 			 * because we're still holding ipsq_lock.
7531 			 */
7532 			ipx = ipsq->ipsq_xop;
7533 			mutex_enter(&ipx->ipx_lock);
7534 			if (ipx->ipx_writer != NULL ||
7535 			    ipx->ipx_current_ipif != NULL) {
7536 				goto out;
7537 			}
7538 		}
7539 
7540 		/*
7541 		 * Fourth, become writer on our new ipx before we continue
7542 		 * with the drain.  Note that we never dropped ipsq_lock
7543 		 * above, so no other thread could've raced with us to
7544 		 * become writer first.  Also, we're holding ipx_lock, so
7545 		 * no other thread can examine the ipx right now.
7546 		 */
7547 		ASSERT(ipx->ipx_current_ipif == NULL);
7548 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7549 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7550 		ipx->ipx_writer = curthread;
7551 		ipx->ipx_forced = B_FALSE;
7552 #ifdef DEBUG
7553 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7554 #endif
7555 	}
7556 
7557 	xopipsq = ipsq;
7558 	do {
7559 		/*
7560 		 * So that other operations operate on a consistent and
7561 		 * complete phyint, a switch message on an IPSQ must be
7562 		 * handled prior to any other operations on that IPSQ.
7563 		 */
7564 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7565 			xopipsq->ipsq_switch_mp = NULL;
7566 			ASSERT(mp->b_next == NULL);
7567 			mp->b_next = (void *)xopipsq;
7568 			goto out;
7569 		}
7570 
7571 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7572 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7573 			if (xopipsq->ipsq_xopq_mphead == NULL)
7574 				xopipsq->ipsq_xopq_mptail = NULL;
7575 			mp->b_next = (void *)xopipsq;
7576 			goto out;
7577 		}
7578 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7579 empty:
7580 	/*
7581 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7582 	 * new messages can end up on any IPSQ in the xop.
7583 	 */
7584 	ipx->ipx_writer = NULL;
7585 	ipx->ipx_forced = B_FALSE;
7586 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7587 	ipx->ipx_ipsq_queued = B_FALSE;
7588 	emptied = B_TRUE;
7589 #ifdef	DEBUG
7590 	ipx->ipx_depth = 0;
7591 #endif
7592 out:
7593 	mutex_exit(&ipx->ipx_lock);
7594 	mutex_exit(&ipsq->ipsq_lock);
7595 
7596 	/*
7597 	 * If we completely emptied the xop, then wake up any threads waiting
7598 	 * to enter any of the IPSQ's associated with it.
7599 	 */
7600 	if (emptied) {
7601 		xopipsq = ipsq;
7602 		do {
7603 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7604 				continue;
7605 
7606 			illv4 = phyi->phyint_illv4;
7607 			illv6 = phyi->phyint_illv6;
7608 
7609 			GRAB_ILL_LOCKS(illv4, illv6);
7610 			if (illv4 != NULL)
7611 				cv_broadcast(&illv4->ill_cv);
7612 			if (illv6 != NULL)
7613 				cv_broadcast(&illv6->ill_cv);
7614 			RELEASE_ILL_LOCKS(illv4, illv6);
7615 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7616 	}
7617 	rw_exit(&ipst->ips_ill_g_lock);
7618 
7619 	/*
7620 	 * Now that all locks are dropped, exit the IPSQ we left.
7621 	 */
7622 	if (leftipsq != NULL)
7623 		ipsq_exit(leftipsq);
7624 
7625 	return (mp);
7626 }
7627 
7628 /*
7629  * Enter the ipsq corresponding to ill, by waiting synchronously till
7630  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7631  * will have to drain completely before ipsq_enter returns success.
7632  * ipx_current_ipif will be set if some exclusive op is in progress,
7633  * and the ipsq_exit logic will start the next enqueued op after
7634  * completion of the current op. If 'force' is used, we don't wait
7635  * for the enqueued ops. This is needed when a conn_close wants to
7636  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7637  * of an ill can also use this option. But we dont' use it currently.
7638  */
7639 #define	ENTER_SQ_WAIT_TICKS 100
7640 boolean_t
7641 ipsq_enter(ill_t *ill, boolean_t force, int type)
7642 {
7643 	ipsq_t	*ipsq;
7644 	ipxop_t *ipx;
7645 	boolean_t waited_enough = B_FALSE;
7646 
7647 	/*
7648 	 * Note that the relationship between ill and ipsq is fixed as long as
7649 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7650 	 * relationship between the IPSQ and xop cannot change.  However,
7651 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7652 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7653 	 * waking up all ills in the xop when it becomes available.
7654 	 */
7655 	mutex_enter(&ill->ill_lock);
7656 	for (;;) {
7657 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7658 			mutex_exit(&ill->ill_lock);
7659 			return (B_FALSE);
7660 		}
7661 
7662 		ipsq = ill->ill_phyint->phyint_ipsq;
7663 		mutex_enter(&ipsq->ipsq_lock);
7664 		ipx = ipsq->ipsq_xop;
7665 		mutex_enter(&ipx->ipx_lock);
7666 
7667 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7668 		    ipx->ipx_current_ipif == NULL || waited_enough))
7669 			break;
7670 
7671 		if (!force || ipx->ipx_writer != NULL) {
7672 			mutex_exit(&ipx->ipx_lock);
7673 			mutex_exit(&ipsq->ipsq_lock);
7674 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7675 		} else {
7676 			mutex_exit(&ipx->ipx_lock);
7677 			mutex_exit(&ipsq->ipsq_lock);
7678 			(void) cv_timedwait(&ill->ill_cv,
7679 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7680 			waited_enough = B_TRUE;
7681 		}
7682 	}
7683 
7684 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7685 	ASSERT(ipx->ipx_reentry_cnt == 0);
7686 	ipx->ipx_writer = curthread;
7687 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7688 	ipx->ipx_reentry_cnt++;
7689 #ifdef DEBUG
7690 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7691 #endif
7692 	mutex_exit(&ipx->ipx_lock);
7693 	mutex_exit(&ipsq->ipsq_lock);
7694 	mutex_exit(&ill->ill_lock);
7695 	return (B_TRUE);
7696 }
7697 
7698 boolean_t
7699 ill_perim_enter(ill_t *ill)
7700 {
7701 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7702 }
7703 
7704 void
7705 ill_perim_exit(ill_t *ill)
7706 {
7707 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7708 }
7709 
7710 /*
7711  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7712  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7713  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7714  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7715  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7716  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7717  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7718  * up the interface) and are enqueued in ipx_mphead.
7719  *
7720  * If a thread does not want to reenter the ipsq when it is already writer,
7721  * it must make sure that the specified reentry point to be called later
7722  * when the ipsq is empty, nor any code path starting from the specified reentry
7723  * point must never ever try to enter the ipsq again. Otherwise it can lead
7724  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7725  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7726  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7727  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7728  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7729  * ioctl if the current ioctl has completed. If the current ioctl is still
7730  * in progress it simply returns. The current ioctl could be waiting for
7731  * a response from another module (arp or the driver or could be waiting for
7732  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7733  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7734  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7735  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7736  * all associated DLPI operations have completed.
7737  */
7738 
7739 /*
7740  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7741  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7742  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7743  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7744  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7745  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7746  */
7747 ipsq_t *
7748 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7749     ipsq_func_t func, int type, boolean_t reentry_ok)
7750 {
7751 	ipsq_t	*ipsq;
7752 	ipxop_t	*ipx;
7753 
7754 	/* Only 1 of ipif or ill can be specified */
7755 	ASSERT((ipif != NULL) ^ (ill != NULL));
7756 	if (ipif != NULL)
7757 		ill = ipif->ipif_ill;
7758 
7759 	/*
7760 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7761 	 * ipx of an ipsq can't change when ipsq_lock is held.
7762 	 */
7763 	GRAB_CONN_LOCK(q);
7764 	mutex_enter(&ill->ill_lock);
7765 	ipsq = ill->ill_phyint->phyint_ipsq;
7766 	mutex_enter(&ipsq->ipsq_lock);
7767 	ipx = ipsq->ipsq_xop;
7768 	mutex_enter(&ipx->ipx_lock);
7769 
7770 	/*
7771 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7772 	 *    (Note: If the caller does not specify reentry_ok then neither
7773 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7774 	 *    again. Otherwise it can lead to an infinite loop
7775 	 * 2. Enter the ipsq if there is no current writer and this attempted
7776 	 *    entry is part of the current operation
7777 	 * 3. Enter the ipsq if there is no current writer and this is a new
7778 	 *    operation and the operation queue is empty and there is no
7779 	 *    operation currently in progress
7780 	 */
7781 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7782 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7783 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7784 		/* Success. */
7785 		ipx->ipx_reentry_cnt++;
7786 		ipx->ipx_writer = curthread;
7787 		ipx->ipx_forced = B_FALSE;
7788 		mutex_exit(&ipx->ipx_lock);
7789 		mutex_exit(&ipsq->ipsq_lock);
7790 		mutex_exit(&ill->ill_lock);
7791 		RELEASE_CONN_LOCK(q);
7792 #ifdef DEBUG
7793 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7794 #endif
7795 		return (ipsq);
7796 	}
7797 
7798 	if (func != NULL)
7799 		ipsq_enq(ipsq, q, mp, func, type, ill);
7800 
7801 	mutex_exit(&ipx->ipx_lock);
7802 	mutex_exit(&ipsq->ipsq_lock);
7803 	mutex_exit(&ill->ill_lock);
7804 	RELEASE_CONN_LOCK(q);
7805 	return (NULL);
7806 }
7807 
7808 /*
7809  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7810  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7811  * cannot be entered, the mp is queued for completion.
7812  */
7813 void
7814 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7815     boolean_t reentry_ok)
7816 {
7817 	ipsq_t	*ipsq;
7818 
7819 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7820 
7821 	/*
7822 	 * Drop the caller's refhold on the ill.  This is safe since we either
7823 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7824 	 * IPSQ, in which case we return without accessing ill anymore.  This
7825 	 * is needed because func needs to see the correct refcount.
7826 	 * e.g. removeif can work only then.
7827 	 */
7828 	ill_refrele(ill);
7829 	if (ipsq != NULL) {
7830 		(*func)(ipsq, q, mp, NULL);
7831 		ipsq_exit(ipsq);
7832 	}
7833 }
7834 
7835 /*
7836  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7837  * prior to exiting.  Caller must be writer on the specified IPSQ.
7838  */
7839 void
7840 ipsq_exit(ipsq_t *ipsq)
7841 {
7842 	mblk_t *mp;
7843 	ipsq_t *mp_ipsq;
7844 	queue_t	*q;
7845 	phyint_t *phyi;
7846 	ipsq_func_t func;
7847 
7848 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7849 
7850 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7851 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7852 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7853 		return;
7854 	}
7855 
7856 	for (;;) {
7857 		phyi = ipsq->ipsq_phyint;
7858 		mp = ipsq_dq(ipsq);
7859 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7860 
7861 		/*
7862 		 * If we've changed to a new IPSQ, and the phyint associated
7863 		 * with the old one has gone away, free the old IPSQ.  Note
7864 		 * that this cannot happen while the IPSQ is in a group.
7865 		 */
7866 		if (mp_ipsq != ipsq && phyi == NULL) {
7867 			ASSERT(ipsq->ipsq_next == ipsq);
7868 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7869 			ipsq_delete(ipsq);
7870 		}
7871 
7872 		if (mp == NULL)
7873 			break;
7874 
7875 		q = mp->b_queue;
7876 		func = (ipsq_func_t)mp->b_prev;
7877 		ipsq = mp_ipsq;
7878 		mp->b_next = mp->b_prev = NULL;
7879 		mp->b_queue = NULL;
7880 
7881 		/*
7882 		 * If 'q' is an conn queue, it is valid, since we did a
7883 		 * a refhold on the conn at the start of the ioctl.
7884 		 * If 'q' is an ill queue, it is valid, since close of an
7885 		 * ill will clean up its IPSQ.
7886 		 */
7887 		(*func)(ipsq, q, mp, NULL);
7888 	}
7889 }
7890 
7891 /*
7892  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7893  * and `ioccmd'.
7894  */
7895 void
7896 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7897 {
7898 	ill_t *ill = ipif->ipif_ill;
7899 	ipxop_t *ipx = ipsq->ipsq_xop;
7900 
7901 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7902 	ASSERT(ipx->ipx_current_ipif == NULL);
7903 	ASSERT(ipx->ipx_current_ioctl == 0);
7904 
7905 	ipx->ipx_current_done = B_FALSE;
7906 	ipx->ipx_current_ioctl = ioccmd;
7907 	mutex_enter(&ipx->ipx_lock);
7908 	ipx->ipx_current_ipif = ipif;
7909 	mutex_exit(&ipx->ipx_lock);
7910 
7911 	/*
7912 	 * Set IPIF_CHANGING on one or more ipifs associated with the
7913 	 * current exclusive operation.  IPIF_CHANGING prevents any new
7914 	 * references to the ipif (so that the references will eventually
7915 	 * drop to zero) and also prevents any "get" operations (e.g.,
7916 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
7917 	 * operation has completed and the ipif is again in a stable state.
7918 	 *
7919 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
7920 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
7921 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
7922 	 * ipifs will be affected.
7923 	 *
7924 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
7925 	 * IPIF_CONDEMNED internally after identifying the right ipif to
7926 	 * operate on.
7927 	 */
7928 	switch (ioccmd) {
7929 	case SIOCLIFREMOVEIF:
7930 		break;
7931 	case 0:
7932 		mutex_enter(&ill->ill_lock);
7933 		ipif = ipif->ipif_ill->ill_ipif;
7934 		for (; ipif != NULL; ipif = ipif->ipif_next)
7935 			ipif->ipif_state_flags |= IPIF_CHANGING;
7936 		mutex_exit(&ill->ill_lock);
7937 		break;
7938 	default:
7939 		mutex_enter(&ill->ill_lock);
7940 		ipif->ipif_state_flags |= IPIF_CHANGING;
7941 		mutex_exit(&ill->ill_lock);
7942 	}
7943 }
7944 
7945 /*
7946  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
7947  * the next exclusive operation to begin once we ipsq_exit().  However, if
7948  * pending DLPI operations remain, then we will wait for the queue to drain
7949  * before allowing the next exclusive operation to begin.  This ensures that
7950  * DLPI operations from one exclusive operation are never improperly processed
7951  * as part of a subsequent exclusive operation.
7952  */
7953 void
7954 ipsq_current_finish(ipsq_t *ipsq)
7955 {
7956 	ipxop_t	*ipx = ipsq->ipsq_xop;
7957 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
7958 	ipif_t	*ipif = ipx->ipx_current_ipif;
7959 
7960 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7961 
7962 	/*
7963 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
7964 	 * (but in that case, IPIF_CHANGING will already be clear and no
7965 	 * pending DLPI messages can remain).
7966 	 */
7967 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
7968 		ill_t *ill = ipif->ipif_ill;
7969 
7970 		mutex_enter(&ill->ill_lock);
7971 		dlpi_pending = ill->ill_dlpi_pending;
7972 		if (ipx->ipx_current_ioctl == 0) {
7973 			ipif = ill->ill_ipif;
7974 			for (; ipif != NULL; ipif = ipif->ipif_next)
7975 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
7976 		} else {
7977 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
7978 		}
7979 		mutex_exit(&ill->ill_lock);
7980 	}
7981 
7982 	ASSERT(!ipx->ipx_current_done);
7983 	ipx->ipx_current_done = B_TRUE;
7984 	ipx->ipx_current_ioctl = 0;
7985 	if (dlpi_pending == DL_PRIM_INVAL) {
7986 		mutex_enter(&ipx->ipx_lock);
7987 		ipx->ipx_current_ipif = NULL;
7988 		mutex_exit(&ipx->ipx_lock);
7989 	}
7990 }
7991 
7992 /*
7993  * The ill is closing. Flush all messages on the ipsq that originated
7994  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7995  * for this ill since ipsq_enter could not have entered until then.
7996  * New messages can't be queued since the CONDEMNED flag is set.
7997  */
7998 static void
7999 ipsq_flush(ill_t *ill)
8000 {
8001 	queue_t	*q;
8002 	mblk_t	*prev;
8003 	mblk_t	*mp;
8004 	mblk_t	*mp_next;
8005 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
8006 
8007 	ASSERT(IAM_WRITER_ILL(ill));
8008 
8009 	/*
8010 	 * Flush any messages sent up by the driver.
8011 	 */
8012 	mutex_enter(&ipx->ipx_lock);
8013 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
8014 		mp_next = mp->b_next;
8015 		q = mp->b_queue;
8016 		if (q == ill->ill_rq || q == ill->ill_wq) {
8017 			/* dequeue mp */
8018 			if (prev == NULL)
8019 				ipx->ipx_mphead = mp->b_next;
8020 			else
8021 				prev->b_next = mp->b_next;
8022 			if (ipx->ipx_mptail == mp) {
8023 				ASSERT(mp_next == NULL);
8024 				ipx->ipx_mptail = prev;
8025 			}
8026 			inet_freemsg(mp);
8027 		} else {
8028 			prev = mp;
8029 		}
8030 	}
8031 	mutex_exit(&ipx->ipx_lock);
8032 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8033 	ipsq_xopq_mp_cleanup(ill, NULL);
8034 	ill_pending_mp_cleanup(ill);
8035 }
8036 
8037 /*
8038  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8039  * refhold and return the associated ipif
8040  */
8041 /* ARGSUSED */
8042 int
8043 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8044     cmd_info_t *ci, ipsq_func_t func)
8045 {
8046 	boolean_t exists;
8047 	struct iftun_req *ta;
8048 	ipif_t  *ipif;
8049 	ill_t   *ill;
8050 	boolean_t isv6;
8051 	mblk_t  *mp1;
8052 	int error;
8053 	conn_t  *connp;
8054 	ip_stack_t  *ipst;
8055 
8056 	/* Existence verified in ip_wput_nondata */
8057 	mp1 = mp->b_cont->b_cont;
8058 	ta = (struct iftun_req *)mp1->b_rptr;
8059 	/*
8060 	 * Null terminate the string to protect against buffer
8061 	 * overrun. String was generated by user code and may not
8062 	 * be trusted.
8063 	 */
8064 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8065 
8066 	connp = Q_TO_CONN(q);
8067 	isv6 = connp->conn_af_isv6;
8068 	ipst = connp->conn_netstack->netstack_ip;
8069 
8070 	/* Disallows implicit create */
8071 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8072 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8073 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8074 	if (ipif == NULL)
8075 		return (error);
8076 
8077 	if (ipif->ipif_id != 0) {
8078 		/*
8079 		 * We really don't want to set/get tunnel parameters
8080 		 * on virtual tunnel interfaces.  Only allow the
8081 		 * base tunnel to do these.
8082 		 */
8083 		ipif_refrele(ipif);
8084 		return (EINVAL);
8085 	}
8086 
8087 	/*
8088 	 * Send down to tunnel mod for ioctl processing.
8089 	 * Will finish ioctl in ip_rput_other().
8090 	 */
8091 	ill = ipif->ipif_ill;
8092 	if (ill->ill_net_type == IRE_LOOPBACK) {
8093 		ipif_refrele(ipif);
8094 		return (EOPNOTSUPP);
8095 	}
8096 
8097 	if (ill->ill_wq == NULL) {
8098 		ipif_refrele(ipif);
8099 		return (ENXIO);
8100 	}
8101 	/*
8102 	 * Mark the ioctl as coming from an IPv6 interface for
8103 	 * tun's convenience.
8104 	 */
8105 	if (ill->ill_isv6)
8106 		ta->ifta_flags |= 0x80000000;
8107 	ci->ci_ipif = ipif;
8108 	return (0);
8109 }
8110 
8111 /*
8112  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8113  * and return the associated ipif.
8114  * Return value:
8115  *	Non zero: An error has occurred. ci may not be filled out.
8116  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8117  *	a held ipif in ci.ci_ipif.
8118  */
8119 int
8120 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8121     cmd_info_t *ci, ipsq_func_t func)
8122 {
8123 	char		*name;
8124 	struct ifreq    *ifr;
8125 	struct lifreq    *lifr;
8126 	ipif_t		*ipif = NULL;
8127 	ill_t		*ill;
8128 	conn_t		*connp;
8129 	boolean_t	isv6;
8130 	boolean_t	exists;
8131 	int		err;
8132 	mblk_t		*mp1;
8133 	zoneid_t	zoneid;
8134 	ip_stack_t	*ipst;
8135 
8136 	if (q->q_next != NULL) {
8137 		ill = (ill_t *)q->q_ptr;
8138 		isv6 = ill->ill_isv6;
8139 		connp = NULL;
8140 		zoneid = ALL_ZONES;
8141 		ipst = ill->ill_ipst;
8142 	} else {
8143 		ill = NULL;
8144 		connp = Q_TO_CONN(q);
8145 		isv6 = connp->conn_af_isv6;
8146 		zoneid = connp->conn_zoneid;
8147 		if (zoneid == GLOBAL_ZONEID) {
8148 			/* global zone can access ipifs in all zones */
8149 			zoneid = ALL_ZONES;
8150 		}
8151 		ipst = connp->conn_netstack->netstack_ip;
8152 	}
8153 
8154 	/* Has been checked in ip_wput_nondata */
8155 	mp1 = mp->b_cont->b_cont;
8156 
8157 	if (ipip->ipi_cmd_type == IF_CMD) {
8158 		/* This a old style SIOC[GS]IF* command */
8159 		ifr = (struct ifreq *)mp1->b_rptr;
8160 		/*
8161 		 * Null terminate the string to protect against buffer
8162 		 * overrun. String was generated by user code and may not
8163 		 * be trusted.
8164 		 */
8165 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8166 		name = ifr->ifr_name;
8167 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8168 		ci->ci_sin6 = NULL;
8169 		ci->ci_lifr = (struct lifreq *)ifr;
8170 	} else {
8171 		/* This a new style SIOC[GS]LIF* command */
8172 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8173 		lifr = (struct lifreq *)mp1->b_rptr;
8174 		/*
8175 		 * Null terminate the string to protect against buffer
8176 		 * overrun. String was generated by user code and may not
8177 		 * be trusted.
8178 		 */
8179 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8180 		name = lifr->lifr_name;
8181 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8182 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8183 		ci->ci_lifr = lifr;
8184 	}
8185 
8186 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8187 		/*
8188 		 * The ioctl will be failed if the ioctl comes down
8189 		 * an conn stream
8190 		 */
8191 		if (ill == NULL) {
8192 			/*
8193 			 * Not an ill queue, return EINVAL same as the
8194 			 * old error code.
8195 			 */
8196 			return (ENXIO);
8197 		}
8198 		ipif = ill->ill_ipif;
8199 		ipif_refhold(ipif);
8200 	} else {
8201 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8202 		    &exists, isv6, zoneid,
8203 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8204 		    ipst);
8205 		if (ipif == NULL) {
8206 			if (err == EINPROGRESS)
8207 				return (err);
8208 			err = 0;	/* Ensure we don't use it below */
8209 		}
8210 	}
8211 
8212 	/*
8213 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8214 	 */
8215 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8216 		ipif_refrele(ipif);
8217 		return (ENXIO);
8218 	}
8219 
8220 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8221 	    name[0] == '\0') {
8222 		/*
8223 		 * Handle a or a SIOC?IF* with a null name
8224 		 * during plumb (on the ill queue before the I_PLINK).
8225 		 */
8226 		ipif = ill->ill_ipif;
8227 		ipif_refhold(ipif);
8228 	}
8229 
8230 	if (ipif == NULL)
8231 		return (ENXIO);
8232 
8233 	ci->ci_ipif = ipif;
8234 	return (0);
8235 }
8236 
8237 /*
8238  * Return the total number of ipifs.
8239  */
8240 static uint_t
8241 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8242 {
8243 	uint_t numifs = 0;
8244 	ill_t	*ill;
8245 	ill_walk_context_t	ctx;
8246 	ipif_t	*ipif;
8247 
8248 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8249 	ill = ILL_START_WALK_V4(&ctx, ipst);
8250 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8251 		if (IS_UNDER_IPMP(ill))
8252 			continue;
8253 		for (ipif = ill->ill_ipif; ipif != NULL;
8254 		    ipif = ipif->ipif_next) {
8255 			if (ipif->ipif_zoneid == zoneid ||
8256 			    ipif->ipif_zoneid == ALL_ZONES)
8257 				numifs++;
8258 		}
8259 	}
8260 	rw_exit(&ipst->ips_ill_g_lock);
8261 	return (numifs);
8262 }
8263 
8264 /*
8265  * Return the total number of ipifs.
8266  */
8267 static uint_t
8268 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8269 {
8270 	uint_t numifs = 0;
8271 	ill_t	*ill;
8272 	ipif_t	*ipif;
8273 	ill_walk_context_t	ctx;
8274 
8275 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8276 
8277 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8278 	if (family == AF_INET)
8279 		ill = ILL_START_WALK_V4(&ctx, ipst);
8280 	else if (family == AF_INET6)
8281 		ill = ILL_START_WALK_V6(&ctx, ipst);
8282 	else
8283 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8284 
8285 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8286 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8287 			continue;
8288 
8289 		for (ipif = ill->ill_ipif; ipif != NULL;
8290 		    ipif = ipif->ipif_next) {
8291 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8292 			    !(lifn_flags & LIFC_NOXMIT))
8293 				continue;
8294 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8295 			    !(lifn_flags & LIFC_TEMPORARY))
8296 				continue;
8297 			if (((ipif->ipif_flags &
8298 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8299 			    IPIF_DEPRECATED)) ||
8300 			    IS_LOOPBACK(ill) ||
8301 			    !(ipif->ipif_flags & IPIF_UP)) &&
8302 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8303 				continue;
8304 
8305 			if (zoneid != ipif->ipif_zoneid &&
8306 			    ipif->ipif_zoneid != ALL_ZONES &&
8307 			    (zoneid != GLOBAL_ZONEID ||
8308 			    !(lifn_flags & LIFC_ALLZONES)))
8309 				continue;
8310 
8311 			numifs++;
8312 		}
8313 	}
8314 	rw_exit(&ipst->ips_ill_g_lock);
8315 	return (numifs);
8316 }
8317 
8318 uint_t
8319 ip_get_lifsrcofnum(ill_t *ill)
8320 {
8321 	uint_t numifs = 0;
8322 	ill_t	*ill_head = ill;
8323 	ip_stack_t	*ipst = ill->ill_ipst;
8324 
8325 	/*
8326 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8327 	 * other thread may be trying to relink the ILLs in this usesrc group
8328 	 * and adjusting the ill_usesrc_grp_next pointers
8329 	 */
8330 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8331 	if ((ill->ill_usesrc_ifindex == 0) &&
8332 	    (ill->ill_usesrc_grp_next != NULL)) {
8333 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8334 		    ill = ill->ill_usesrc_grp_next)
8335 			numifs++;
8336 	}
8337 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8338 
8339 	return (numifs);
8340 }
8341 
8342 /* Null values are passed in for ipif, sin, and ifreq */
8343 /* ARGSUSED */
8344 int
8345 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8346     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8347 {
8348 	int *nump;
8349 	conn_t *connp = Q_TO_CONN(q);
8350 
8351 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8352 
8353 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8354 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8355 
8356 	*nump = ip_get_numifs(connp->conn_zoneid,
8357 	    connp->conn_netstack->netstack_ip);
8358 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8359 	return (0);
8360 }
8361 
8362 /* Null values are passed in for ipif, sin, and ifreq */
8363 /* ARGSUSED */
8364 int
8365 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8366     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8367 {
8368 	struct lifnum *lifn;
8369 	mblk_t	*mp1;
8370 	conn_t *connp = Q_TO_CONN(q);
8371 
8372 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8373 
8374 	/* Existence checked in ip_wput_nondata */
8375 	mp1 = mp->b_cont->b_cont;
8376 
8377 	lifn = (struct lifnum *)mp1->b_rptr;
8378 	switch (lifn->lifn_family) {
8379 	case AF_UNSPEC:
8380 	case AF_INET:
8381 	case AF_INET6:
8382 		break;
8383 	default:
8384 		return (EAFNOSUPPORT);
8385 	}
8386 
8387 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8388 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8389 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8390 	return (0);
8391 }
8392 
8393 /* ARGSUSED */
8394 int
8395 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8396     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8397 {
8398 	STRUCT_HANDLE(ifconf, ifc);
8399 	mblk_t *mp1;
8400 	struct iocblk *iocp;
8401 	struct ifreq *ifr;
8402 	ill_walk_context_t	ctx;
8403 	ill_t	*ill;
8404 	ipif_t	*ipif;
8405 	struct sockaddr_in *sin;
8406 	int32_t	ifclen;
8407 	zoneid_t zoneid;
8408 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8409 
8410 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8411 
8412 	ip1dbg(("ip_sioctl_get_ifconf"));
8413 	/* Existence verified in ip_wput_nondata */
8414 	mp1 = mp->b_cont->b_cont;
8415 	iocp = (struct iocblk *)mp->b_rptr;
8416 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8417 
8418 	/*
8419 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8420 	 * the user buffer address and length into which the list of struct
8421 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8422 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8423 	 * the SIOCGIFCONF operation was redefined to simply provide
8424 	 * a large output buffer into which we are supposed to jam the ifreq
8425 	 * array.  The same ioctl command code was used, despite the fact that
8426 	 * both the applications and the kernel code had to change, thus making
8427 	 * it impossible to support both interfaces.
8428 	 *
8429 	 * For reasons not good enough to try to explain, the following
8430 	 * algorithm is used for deciding what to do with one of these:
8431 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8432 	 * form with the output buffer coming down as the continuation message.
8433 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8434 	 * and we have to copy in the ifconf structure to find out how big the
8435 	 * output buffer is and where to copy out to.  Sure no problem...
8436 	 *
8437 	 */
8438 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8439 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8440 		int numifs = 0;
8441 		size_t ifc_bufsize;
8442 
8443 		/*
8444 		 * Must be (better be!) continuation of a TRANSPARENT
8445 		 * IOCTL.  We just copied in the ifconf structure.
8446 		 */
8447 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8448 		    (struct ifconf *)mp1->b_rptr);
8449 
8450 		/*
8451 		 * Allocate a buffer to hold requested information.
8452 		 *
8453 		 * If ifc_len is larger than what is needed, we only
8454 		 * allocate what we will use.
8455 		 *
8456 		 * If ifc_len is smaller than what is needed, return
8457 		 * EINVAL.
8458 		 *
8459 		 * XXX: the ill_t structure can hava 2 counters, for
8460 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8461 		 * number of interfaces for a device, so we don't need
8462 		 * to count them here...
8463 		 */
8464 		numifs = ip_get_numifs(zoneid, ipst);
8465 
8466 		ifclen = STRUCT_FGET(ifc, ifc_len);
8467 		ifc_bufsize = numifs * sizeof (struct ifreq);
8468 		if (ifc_bufsize > ifclen) {
8469 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8470 				/* old behaviour */
8471 				return (EINVAL);
8472 			} else {
8473 				ifc_bufsize = ifclen;
8474 			}
8475 		}
8476 
8477 		mp1 = mi_copyout_alloc(q, mp,
8478 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8479 		if (mp1 == NULL)
8480 			return (ENOMEM);
8481 
8482 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8483 	}
8484 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8485 	/*
8486 	 * the SIOCGIFCONF ioctl only knows about
8487 	 * IPv4 addresses, so don't try to tell
8488 	 * it about interfaces with IPv6-only
8489 	 * addresses. (Last parm 'isv6' is B_FALSE)
8490 	 */
8491 
8492 	ifr = (struct ifreq *)mp1->b_rptr;
8493 
8494 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8495 	ill = ILL_START_WALK_V4(&ctx, ipst);
8496 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8497 		if (IS_UNDER_IPMP(ill))
8498 			continue;
8499 		for (ipif = ill->ill_ipif; ipif != NULL;
8500 		    ipif = ipif->ipif_next) {
8501 			if (zoneid != ipif->ipif_zoneid &&
8502 			    ipif->ipif_zoneid != ALL_ZONES)
8503 				continue;
8504 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8505 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8506 					/* old behaviour */
8507 					rw_exit(&ipst->ips_ill_g_lock);
8508 					return (EINVAL);
8509 				} else {
8510 					goto if_copydone;
8511 				}
8512 			}
8513 			ipif_get_name(ipif, ifr->ifr_name,
8514 			    sizeof (ifr->ifr_name));
8515 			sin = (sin_t *)&ifr->ifr_addr;
8516 			*sin = sin_null;
8517 			sin->sin_family = AF_INET;
8518 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8519 			ifr++;
8520 		}
8521 	}
8522 if_copydone:
8523 	rw_exit(&ipst->ips_ill_g_lock);
8524 	mp1->b_wptr = (uchar_t *)ifr;
8525 
8526 	if (STRUCT_BUF(ifc) != NULL) {
8527 		STRUCT_FSET(ifc, ifc_len,
8528 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8529 	}
8530 	return (0);
8531 }
8532 
8533 /*
8534  * Get the interfaces using the address hosted on the interface passed in,
8535  * as a source adddress
8536  */
8537 /* ARGSUSED */
8538 int
8539 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8540     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8541 {
8542 	mblk_t *mp1;
8543 	ill_t	*ill, *ill_head;
8544 	ipif_t	*ipif, *orig_ipif;
8545 	int	numlifs = 0;
8546 	size_t	lifs_bufsize, lifsmaxlen;
8547 	struct	lifreq *lifr;
8548 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8549 	uint_t	ifindex;
8550 	zoneid_t zoneid;
8551 	int err = 0;
8552 	boolean_t isv6 = B_FALSE;
8553 	struct	sockaddr_in	*sin;
8554 	struct	sockaddr_in6	*sin6;
8555 	STRUCT_HANDLE(lifsrcof, lifs);
8556 	ip_stack_t		*ipst;
8557 
8558 	ipst = CONNQ_TO_IPST(q);
8559 
8560 	ASSERT(q->q_next == NULL);
8561 
8562 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8563 
8564 	/* Existence verified in ip_wput_nondata */
8565 	mp1 = mp->b_cont->b_cont;
8566 
8567 	/*
8568 	 * Must be (better be!) continuation of a TRANSPARENT
8569 	 * IOCTL.  We just copied in the lifsrcof structure.
8570 	 */
8571 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8572 	    (struct lifsrcof *)mp1->b_rptr);
8573 
8574 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8575 		return (EINVAL);
8576 
8577 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8578 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8579 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8580 	    ip_process_ioctl, &err, ipst);
8581 	if (ipif == NULL) {
8582 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8583 		    ifindex));
8584 		return (err);
8585 	}
8586 
8587 	/* Allocate a buffer to hold requested information */
8588 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8589 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8590 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8591 	/* The actual size needed is always returned in lifs_len */
8592 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8593 
8594 	/* If the amount we need is more than what is passed in, abort */
8595 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8596 		ipif_refrele(ipif);
8597 		return (0);
8598 	}
8599 
8600 	mp1 = mi_copyout_alloc(q, mp,
8601 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8602 	if (mp1 == NULL) {
8603 		ipif_refrele(ipif);
8604 		return (ENOMEM);
8605 	}
8606 
8607 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8608 	bzero(mp1->b_rptr, lifs_bufsize);
8609 
8610 	lifr = (struct lifreq *)mp1->b_rptr;
8611 
8612 	ill = ill_head = ipif->ipif_ill;
8613 	orig_ipif = ipif;
8614 
8615 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8616 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8617 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8618 
8619 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8620 	for (; (ill != NULL) && (ill != ill_head);
8621 	    ill = ill->ill_usesrc_grp_next) {
8622 
8623 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8624 			break;
8625 
8626 		ipif = ill->ill_ipif;
8627 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8628 		if (ipif->ipif_isv6) {
8629 			sin6 = (sin6_t *)&lifr->lifr_addr;
8630 			*sin6 = sin6_null;
8631 			sin6->sin6_family = AF_INET6;
8632 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8633 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8634 			    &ipif->ipif_v6net_mask);
8635 		} else {
8636 			sin = (sin_t *)&lifr->lifr_addr;
8637 			*sin = sin_null;
8638 			sin->sin_family = AF_INET;
8639 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8640 			lifr->lifr_addrlen = ip_mask_to_plen(
8641 			    ipif->ipif_net_mask);
8642 		}
8643 		lifr++;
8644 	}
8645 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8646 	rw_exit(&ipst->ips_ill_g_lock);
8647 	ipif_refrele(orig_ipif);
8648 	mp1->b_wptr = (uchar_t *)lifr;
8649 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8650 
8651 	return (0);
8652 }
8653 
8654 /* ARGSUSED */
8655 int
8656 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8657     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8658 {
8659 	mblk_t *mp1;
8660 	int	list;
8661 	ill_t	*ill;
8662 	ipif_t	*ipif;
8663 	int	flags;
8664 	int	numlifs = 0;
8665 	size_t	lifc_bufsize;
8666 	struct	lifreq *lifr;
8667 	sa_family_t	family;
8668 	struct	sockaddr_in	*sin;
8669 	struct	sockaddr_in6	*sin6;
8670 	ill_walk_context_t	ctx;
8671 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8672 	int32_t	lifclen;
8673 	zoneid_t zoneid;
8674 	STRUCT_HANDLE(lifconf, lifc);
8675 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8676 
8677 	ip1dbg(("ip_sioctl_get_lifconf"));
8678 
8679 	ASSERT(q->q_next == NULL);
8680 
8681 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8682 
8683 	/* Existence verified in ip_wput_nondata */
8684 	mp1 = mp->b_cont->b_cont;
8685 
8686 	/*
8687 	 * An extended version of SIOCGIFCONF that takes an
8688 	 * additional address family and flags field.
8689 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8690 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8691 	 * interfaces are omitted.
8692 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8693 	 * unless LIFC_TEMPORARY is specified.
8694 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8695 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8696 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8697 	 * has priority over LIFC_NOXMIT.
8698 	 */
8699 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8700 
8701 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8702 		return (EINVAL);
8703 
8704 	/*
8705 	 * Must be (better be!) continuation of a TRANSPARENT
8706 	 * IOCTL.  We just copied in the lifconf structure.
8707 	 */
8708 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8709 
8710 	family = STRUCT_FGET(lifc, lifc_family);
8711 	flags = STRUCT_FGET(lifc, lifc_flags);
8712 
8713 	switch (family) {
8714 	case AF_UNSPEC:
8715 		/*
8716 		 * walk all ILL's.
8717 		 */
8718 		list = MAX_G_HEADS;
8719 		break;
8720 	case AF_INET:
8721 		/*
8722 		 * walk only IPV4 ILL's.
8723 		 */
8724 		list = IP_V4_G_HEAD;
8725 		break;
8726 	case AF_INET6:
8727 		/*
8728 		 * walk only IPV6 ILL's.
8729 		 */
8730 		list = IP_V6_G_HEAD;
8731 		break;
8732 	default:
8733 		return (EAFNOSUPPORT);
8734 	}
8735 
8736 	/*
8737 	 * Allocate a buffer to hold requested information.
8738 	 *
8739 	 * If lifc_len is larger than what is needed, we only
8740 	 * allocate what we will use.
8741 	 *
8742 	 * If lifc_len is smaller than what is needed, return
8743 	 * EINVAL.
8744 	 */
8745 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8746 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8747 	lifclen = STRUCT_FGET(lifc, lifc_len);
8748 	if (lifc_bufsize > lifclen) {
8749 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8750 			return (EINVAL);
8751 		else
8752 			lifc_bufsize = lifclen;
8753 	}
8754 
8755 	mp1 = mi_copyout_alloc(q, mp,
8756 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8757 	if (mp1 == NULL)
8758 		return (ENOMEM);
8759 
8760 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8761 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8762 
8763 	lifr = (struct lifreq *)mp1->b_rptr;
8764 
8765 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8766 	ill = ill_first(list, list, &ctx, ipst);
8767 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8768 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8769 			continue;
8770 
8771 		for (ipif = ill->ill_ipif; ipif != NULL;
8772 		    ipif = ipif->ipif_next) {
8773 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8774 			    !(flags & LIFC_NOXMIT))
8775 				continue;
8776 
8777 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8778 			    !(flags & LIFC_TEMPORARY))
8779 				continue;
8780 
8781 			if (((ipif->ipif_flags &
8782 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8783 			    IPIF_DEPRECATED)) ||
8784 			    IS_LOOPBACK(ill) ||
8785 			    !(ipif->ipif_flags & IPIF_UP)) &&
8786 			    (flags & LIFC_EXTERNAL_SOURCE))
8787 				continue;
8788 
8789 			if (zoneid != ipif->ipif_zoneid &&
8790 			    ipif->ipif_zoneid != ALL_ZONES &&
8791 			    (zoneid != GLOBAL_ZONEID ||
8792 			    !(flags & LIFC_ALLZONES)))
8793 				continue;
8794 
8795 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8796 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8797 					rw_exit(&ipst->ips_ill_g_lock);
8798 					return (EINVAL);
8799 				} else {
8800 					goto lif_copydone;
8801 				}
8802 			}
8803 
8804 			ipif_get_name(ipif, lifr->lifr_name,
8805 			    sizeof (lifr->lifr_name));
8806 			lifr->lifr_type = ill->ill_type;
8807 			if (ipif->ipif_isv6) {
8808 				sin6 = (sin6_t *)&lifr->lifr_addr;
8809 				*sin6 = sin6_null;
8810 				sin6->sin6_family = AF_INET6;
8811 				sin6->sin6_addr =
8812 				    ipif->ipif_v6lcl_addr;
8813 				lifr->lifr_addrlen =
8814 				    ip_mask_to_plen_v6(
8815 				    &ipif->ipif_v6net_mask);
8816 			} else {
8817 				sin = (sin_t *)&lifr->lifr_addr;
8818 				*sin = sin_null;
8819 				sin->sin_family = AF_INET;
8820 				sin->sin_addr.s_addr =
8821 				    ipif->ipif_lcl_addr;
8822 				lifr->lifr_addrlen =
8823 				    ip_mask_to_plen(
8824 				    ipif->ipif_net_mask);
8825 			}
8826 			lifr++;
8827 		}
8828 	}
8829 lif_copydone:
8830 	rw_exit(&ipst->ips_ill_g_lock);
8831 
8832 	mp1->b_wptr = (uchar_t *)lifr;
8833 	if (STRUCT_BUF(lifc) != NULL) {
8834 		STRUCT_FSET(lifc, lifc_len,
8835 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8836 	}
8837 	return (0);
8838 }
8839 
8840 static void
8841 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8842 {
8843 	ip6_asp_t *table;
8844 	size_t table_size;
8845 	mblk_t *data_mp;
8846 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8847 	ip_stack_t	*ipst;
8848 
8849 	if (q->q_next == NULL)
8850 		ipst = CONNQ_TO_IPST(q);
8851 	else
8852 		ipst = ILLQ_TO_IPST(q);
8853 
8854 	/* These two ioctls are I_STR only */
8855 	if (iocp->ioc_count == TRANSPARENT) {
8856 		miocnak(q, mp, 0, EINVAL);
8857 		return;
8858 	}
8859 
8860 	data_mp = mp->b_cont;
8861 	if (data_mp == NULL) {
8862 		/* The user passed us a NULL argument */
8863 		table = NULL;
8864 		table_size = iocp->ioc_count;
8865 	} else {
8866 		/*
8867 		 * The user provided a table.  The stream head
8868 		 * may have copied in the user data in chunks,
8869 		 * so make sure everything is pulled up
8870 		 * properly.
8871 		 */
8872 		if (MBLKL(data_mp) < iocp->ioc_count) {
8873 			mblk_t *new_data_mp;
8874 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8875 			    NULL) {
8876 				miocnak(q, mp, 0, ENOMEM);
8877 				return;
8878 			}
8879 			freemsg(data_mp);
8880 			data_mp = new_data_mp;
8881 			mp->b_cont = data_mp;
8882 		}
8883 		table = (ip6_asp_t *)data_mp->b_rptr;
8884 		table_size = iocp->ioc_count;
8885 	}
8886 
8887 	switch (iocp->ioc_cmd) {
8888 	case SIOCGIP6ADDRPOLICY:
8889 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8890 		if (iocp->ioc_rval == -1)
8891 			iocp->ioc_error = EINVAL;
8892 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8893 		else if (table != NULL &&
8894 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8895 			ip6_asp_t *src = table;
8896 			ip6_asp32_t *dst = (void *)table;
8897 			int count = table_size / sizeof (ip6_asp_t);
8898 			int i;
8899 
8900 			/*
8901 			 * We need to do an in-place shrink of the array
8902 			 * to match the alignment attributes of the
8903 			 * 32-bit ABI looking at it.
8904 			 */
8905 			/* LINTED: logical expression always true: op "||" */
8906 			ASSERT(sizeof (*src) > sizeof (*dst));
8907 			for (i = 1; i < count; i++)
8908 				bcopy(src + i, dst + i, sizeof (*dst));
8909 		}
8910 #endif
8911 		break;
8912 
8913 	case SIOCSIP6ADDRPOLICY:
8914 		ASSERT(mp->b_prev == NULL);
8915 		mp->b_prev = (void *)q;
8916 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8917 		/*
8918 		 * We pass in the datamodel here so that the ip6_asp_replace()
8919 		 * routine can handle converting from 32-bit to native formats
8920 		 * where necessary.
8921 		 *
8922 		 * A better way to handle this might be to convert the inbound
8923 		 * data structure here, and hang it off a new 'mp'; thus the
8924 		 * ip6_asp_replace() logic would always be dealing with native
8925 		 * format data structures..
8926 		 *
8927 		 * (An even simpler way to handle these ioctls is to just
8928 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8929 		 * and just recompile everything that depends on it.)
8930 		 */
8931 #endif
8932 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
8933 		    iocp->ioc_flag & IOC_MODELS);
8934 		return;
8935 	}
8936 
8937 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8938 	qreply(q, mp);
8939 }
8940 
8941 static void
8942 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8943 {
8944 	mblk_t 		*data_mp;
8945 	struct dstinforeq	*dir;
8946 	uint8_t		*end, *cur;
8947 	in6_addr_t	*daddr, *saddr;
8948 	ipaddr_t	v4daddr;
8949 	ire_t		*ire;
8950 	char		*slabel, *dlabel;
8951 	boolean_t	isipv4;
8952 	int		match_ire;
8953 	ill_t		*dst_ill;
8954 	ipif_t		*src_ipif, *ire_ipif;
8955 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8956 	zoneid_t	zoneid;
8957 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
8958 
8959 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8960 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8961 
8962 	/*
8963 	 * This ioctl is I_STR only, and must have a
8964 	 * data mblk following the M_IOCTL mblk.
8965 	 */
8966 	data_mp = mp->b_cont;
8967 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8968 		miocnak(q, mp, 0, EINVAL);
8969 		return;
8970 	}
8971 
8972 	if (MBLKL(data_mp) < iocp->ioc_count) {
8973 		mblk_t *new_data_mp;
8974 
8975 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8976 			miocnak(q, mp, 0, ENOMEM);
8977 			return;
8978 		}
8979 		freemsg(data_mp);
8980 		data_mp = new_data_mp;
8981 		mp->b_cont = data_mp;
8982 	}
8983 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8984 
8985 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8986 	    end - cur >= sizeof (struct dstinforeq);
8987 	    cur += sizeof (struct dstinforeq)) {
8988 		dir = (struct dstinforeq *)cur;
8989 		daddr = &dir->dir_daddr;
8990 		saddr = &dir->dir_saddr;
8991 
8992 		/*
8993 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8994 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8995 		 * and ipif_select_source[_v6]() do not.
8996 		 */
8997 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8998 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
8999 
9000 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9001 		if (isipv4) {
9002 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9003 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9004 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9005 		} else {
9006 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9007 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9008 		}
9009 		if (ire == NULL) {
9010 			dir->dir_dreachable = 0;
9011 
9012 			/* move on to next dst addr */
9013 			continue;
9014 		}
9015 		dir->dir_dreachable = 1;
9016 
9017 		ire_ipif = ire->ire_ipif;
9018 		if (ire_ipif == NULL)
9019 			goto next_dst;
9020 
9021 		/*
9022 		 * We expect to get back an interface ire or a
9023 		 * gateway ire cache entry.  For both types, the
9024 		 * output interface is ire_ipif->ipif_ill.
9025 		 */
9026 		dst_ill = ire_ipif->ipif_ill;
9027 		dir->dir_dmactype = dst_ill->ill_mactype;
9028 
9029 		if (isipv4) {
9030 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9031 		} else {
9032 			src_ipif = ipif_select_source_v6(dst_ill,
9033 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9034 		}
9035 		if (src_ipif == NULL)
9036 			goto next_dst;
9037 
9038 		*saddr = src_ipif->ipif_v6lcl_addr;
9039 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9040 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9041 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9042 		dir->dir_sdeprecated =
9043 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9044 		ipif_refrele(src_ipif);
9045 next_dst:
9046 		ire_refrele(ire);
9047 	}
9048 	miocack(q, mp, iocp->ioc_count, 0);
9049 }
9050 
9051 /*
9052  * Check if this is an address assigned to this machine.
9053  * Skips interfaces that are down by using ire checks.
9054  * Translates mapped addresses to v4 addresses and then
9055  * treats them as such, returning true if the v4 address
9056  * associated with this mapped address is configured.
9057  * Note: Applications will have to be careful what they do
9058  * with the response; use of mapped addresses limits
9059  * what can be done with the socket, especially with
9060  * respect to socket options and ioctls - neither IPv4
9061  * options nor IPv6 sticky options/ancillary data options
9062  * may be used.
9063  */
9064 /* ARGSUSED */
9065 int
9066 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9067     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9068 {
9069 	struct sioc_addrreq *sia;
9070 	sin_t *sin;
9071 	ire_t *ire;
9072 	mblk_t *mp1;
9073 	zoneid_t zoneid;
9074 	ip_stack_t	*ipst;
9075 
9076 	ip1dbg(("ip_sioctl_tmyaddr"));
9077 
9078 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9079 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9080 	ipst = CONNQ_TO_IPST(q);
9081 
9082 	/* Existence verified in ip_wput_nondata */
9083 	mp1 = mp->b_cont->b_cont;
9084 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9085 	sin = (sin_t *)&sia->sa_addr;
9086 	switch (sin->sin_family) {
9087 	case AF_INET6: {
9088 		sin6_t *sin6 = (sin6_t *)sin;
9089 
9090 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9091 			ipaddr_t v4_addr;
9092 
9093 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9094 			    v4_addr);
9095 			ire = ire_ctable_lookup(v4_addr, 0,
9096 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9097 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9098 		} else {
9099 			in6_addr_t v6addr;
9100 
9101 			v6addr = sin6->sin6_addr;
9102 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9103 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9104 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9105 		}
9106 		break;
9107 	}
9108 	case AF_INET: {
9109 		ipaddr_t v4addr;
9110 
9111 		v4addr = sin->sin_addr.s_addr;
9112 		ire = ire_ctable_lookup(v4addr, 0,
9113 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9114 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9115 		break;
9116 	}
9117 	default:
9118 		return (EAFNOSUPPORT);
9119 	}
9120 	if (ire != NULL) {
9121 		sia->sa_res = 1;
9122 		ire_refrele(ire);
9123 	} else {
9124 		sia->sa_res = 0;
9125 	}
9126 	return (0);
9127 }
9128 
9129 /*
9130  * Check if this is an address assigned on-link i.e. neighbor,
9131  * and makes sure it's reachable from the current zone.
9132  * Returns true for my addresses as well.
9133  * Translates mapped addresses to v4 addresses and then
9134  * treats them as such, returning true if the v4 address
9135  * associated with this mapped address is configured.
9136  * Note: Applications will have to be careful what they do
9137  * with the response; use of mapped addresses limits
9138  * what can be done with the socket, especially with
9139  * respect to socket options and ioctls - neither IPv4
9140  * options nor IPv6 sticky options/ancillary data options
9141  * may be used.
9142  */
9143 /* ARGSUSED */
9144 int
9145 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9146     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9147 {
9148 	struct sioc_addrreq *sia;
9149 	sin_t *sin;
9150 	mblk_t	*mp1;
9151 	ire_t *ire = NULL;
9152 	zoneid_t zoneid;
9153 	ip_stack_t	*ipst;
9154 
9155 	ip1dbg(("ip_sioctl_tonlink"));
9156 
9157 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9158 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9159 	ipst = CONNQ_TO_IPST(q);
9160 
9161 	/* Existence verified in ip_wput_nondata */
9162 	mp1 = mp->b_cont->b_cont;
9163 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9164 	sin = (sin_t *)&sia->sa_addr;
9165 
9166 	/*
9167 	 * Match addresses with a zero gateway field to avoid
9168 	 * routes going through a router.
9169 	 * Exclude broadcast and multicast addresses.
9170 	 */
9171 	switch (sin->sin_family) {
9172 	case AF_INET6: {
9173 		sin6_t *sin6 = (sin6_t *)sin;
9174 
9175 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9176 			ipaddr_t v4_addr;
9177 
9178 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9179 			    v4_addr);
9180 			if (!CLASSD(v4_addr)) {
9181 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9182 				    NULL, NULL, zoneid, NULL,
9183 				    MATCH_IRE_GW, ipst);
9184 			}
9185 		} else {
9186 			in6_addr_t v6addr;
9187 			in6_addr_t v6gw;
9188 
9189 			v6addr = sin6->sin6_addr;
9190 			v6gw = ipv6_all_zeros;
9191 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9192 				ire = ire_route_lookup_v6(&v6addr, 0,
9193 				    &v6gw, 0, NULL, NULL, zoneid,
9194 				    NULL, MATCH_IRE_GW, ipst);
9195 			}
9196 		}
9197 		break;
9198 	}
9199 	case AF_INET: {
9200 		ipaddr_t v4addr;
9201 
9202 		v4addr = sin->sin_addr.s_addr;
9203 		if (!CLASSD(v4addr)) {
9204 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9205 			    NULL, NULL, zoneid, NULL,
9206 			    MATCH_IRE_GW, ipst);
9207 		}
9208 		break;
9209 	}
9210 	default:
9211 		return (EAFNOSUPPORT);
9212 	}
9213 	sia->sa_res = 0;
9214 	if (ire != NULL) {
9215 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9216 		    IRE_LOCAL|IRE_LOOPBACK)) {
9217 			sia->sa_res = 1;
9218 		}
9219 		ire_refrele(ire);
9220 	}
9221 	return (0);
9222 }
9223 
9224 /*
9225  * TBD: implement when kernel maintaines a list of site prefixes.
9226  */
9227 /* ARGSUSED */
9228 int
9229 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9230     ip_ioctl_cmd_t *ipip, void *ifreq)
9231 {
9232 	return (ENXIO);
9233 }
9234 
9235 /* ARGSUSED */
9236 int
9237 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9238     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9239 {
9240 	ill_t		*ill;
9241 	mblk_t		*mp1;
9242 	conn_t		*connp;
9243 	boolean_t	success;
9244 
9245 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9246 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9247 	/* ioctl comes down on an conn */
9248 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9249 	connp = Q_TO_CONN(q);
9250 
9251 	mp->b_datap->db_type = M_IOCTL;
9252 
9253 	/*
9254 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9255 	 * The original mp contains contaminated b_next values due to 'mi',
9256 	 * which is needed to do the mi_copy_done. Unfortunately if we
9257 	 * send down the original mblk itself and if we are popped due to an
9258 	 * an unplumb before the response comes back from tunnel,
9259 	 * the streamhead (which does a freemsg) will see this contaminated
9260 	 * message and the assertion in freemsg about non-null b_next/b_prev
9261 	 * will panic a DEBUG kernel.
9262 	 */
9263 	mp1 = copymsg(mp);
9264 	if (mp1 == NULL)
9265 		return (ENOMEM);
9266 
9267 	ill = ipif->ipif_ill;
9268 	mutex_enter(&connp->conn_lock);
9269 	mutex_enter(&ill->ill_lock);
9270 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9271 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9272 		    mp, 0);
9273 	} else {
9274 		success = ill_pending_mp_add(ill, connp, mp);
9275 	}
9276 	mutex_exit(&ill->ill_lock);
9277 	mutex_exit(&connp->conn_lock);
9278 
9279 	if (success) {
9280 		ip1dbg(("sending down tunparam request "));
9281 		putnext(ill->ill_wq, mp1);
9282 		return (EINPROGRESS);
9283 	} else {
9284 		/* The conn has started closing */
9285 		freemsg(mp1);
9286 		return (EINTR);
9287 	}
9288 }
9289 
9290 /*
9291  * ARP IOCTLs.
9292  * How does IP get in the business of fronting ARP configuration/queries?
9293  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9294  * are by tradition passed in through a datagram socket.  That lands in IP.
9295  * As it happens, this is just as well since the interface is quite crude in
9296  * that it passes in no information about protocol or hardware types, or
9297  * interface association.  After making the protocol assumption, IP is in
9298  * the position to look up the name of the ILL, which ARP will need, and
9299  * format a request that can be handled by ARP.  The request is passed up
9300  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9301  * back a response.  ARP supports its own set of more general IOCTLs, in
9302  * case anyone is interested.
9303  */
9304 /* ARGSUSED */
9305 int
9306 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9307     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9308 {
9309 	mblk_t *mp1;
9310 	mblk_t *mp2;
9311 	mblk_t *pending_mp;
9312 	ipaddr_t ipaddr;
9313 	area_t *area;
9314 	struct iocblk *iocp;
9315 	conn_t *connp;
9316 	struct arpreq *ar;
9317 	struct xarpreq *xar;
9318 	int flags, alength;
9319 	uchar_t *lladdr;
9320 	ire_t *ire;
9321 	ip_stack_t *ipst;
9322 	ill_t *ill = ipif->ipif_ill;
9323 	ill_t *proxy_ill = NULL;
9324 	ipmp_arpent_t *entp = NULL;
9325 	boolean_t if_arp_ioctl = B_FALSE;
9326 	boolean_t proxyarp = B_FALSE;
9327 
9328 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9329 	connp = Q_TO_CONN(q);
9330 	ipst = connp->conn_netstack->netstack_ip;
9331 
9332 	if (ipip->ipi_cmd_type == XARP_CMD) {
9333 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9334 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9335 		ar = NULL;
9336 
9337 		flags = xar->xarp_flags;
9338 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9339 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9340 		/*
9341 		 * Validate against user's link layer address length
9342 		 * input and name and addr length limits.
9343 		 */
9344 		alength = ill->ill_phys_addr_length;
9345 		if (ipip->ipi_cmd == SIOCSXARP) {
9346 			if (alength != xar->xarp_ha.sdl_alen ||
9347 			    (alength + xar->xarp_ha.sdl_nlen >
9348 			    sizeof (xar->xarp_ha.sdl_data)))
9349 				return (EINVAL);
9350 		}
9351 	} else {
9352 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9353 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9354 		xar = NULL;
9355 
9356 		flags = ar->arp_flags;
9357 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9358 		/*
9359 		 * Theoretically, the sa_family could tell us what link
9360 		 * layer type this operation is trying to deal with. By
9361 		 * common usage AF_UNSPEC means ethernet. We'll assume
9362 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9363 		 * for now. Our new SIOC*XARP ioctls can be used more
9364 		 * generally.
9365 		 *
9366 		 * If the underlying media happens to have a non 6 byte
9367 		 * address, arp module will fail set/get, but the del
9368 		 * operation will succeed.
9369 		 */
9370 		alength = 6;
9371 		if ((ipip->ipi_cmd != SIOCDARP) &&
9372 		    (alength != ill->ill_phys_addr_length)) {
9373 			return (EINVAL);
9374 		}
9375 	}
9376 
9377 	ipaddr = sin->sin_addr.s_addr;
9378 
9379 	/*
9380 	 * IPMP ARP special handling:
9381 	 *
9382 	 * 1. Since ARP mappings must appear consistent across the group,
9383 	 *    prohibit changing ARP mappings on the underlying interfaces.
9384 	 *
9385 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9386 	 *    IP itself, prohibit changing them.
9387 	 *
9388 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9389 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9390 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9391 	 */
9392 	if (IS_UNDER_IPMP(ill)) {
9393 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9394 			return (EPERM);
9395 	}
9396 	if (IS_IPMP(ill)) {
9397 		ipmp_illgrp_t *illg = ill->ill_grp;
9398 
9399 		switch (ipip->ipi_cmd) {
9400 		case SIOCSARP:
9401 		case SIOCSXARP:
9402 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9403 			if (proxy_ill != NULL) {
9404 				proxyarp = B_TRUE;
9405 				if (!ipmp_ill_is_active(proxy_ill))
9406 					proxy_ill = ipmp_illgrp_next_ill(illg);
9407 				if (proxy_ill != NULL)
9408 					lladdr = proxy_ill->ill_phys_addr;
9409 			}
9410 			/* FALLTHRU */
9411 		case SIOCDARP:
9412 		case SIOCDXARP:
9413 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9414 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9415 			if (ire != NULL) {
9416 				ire_refrele(ire);
9417 				return (EPERM);
9418 			}
9419 		}
9420 	}
9421 
9422 	/*
9423 	 * We are going to pass up to ARP a packet chain that looks
9424 	 * like:
9425 	 *
9426 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9427 	 *
9428 	 * Get a copy of the original IOCTL mblk to head the chain,
9429 	 * to be sent up (in mp1). Also get another copy to store
9430 	 * in the ill_pending_mp list, for matching the response
9431 	 * when it comes back from ARP.
9432 	 */
9433 	mp1 = copyb(mp);
9434 	pending_mp = copymsg(mp);
9435 	if (mp1 == NULL || pending_mp == NULL) {
9436 		if (mp1 != NULL)
9437 			freeb(mp1);
9438 		if (pending_mp != NULL)
9439 			inet_freemsg(pending_mp);
9440 		return (ENOMEM);
9441 	}
9442 
9443 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9444 	    (caddr_t)&ipaddr);
9445 	if (mp2 == NULL) {
9446 		freeb(mp1);
9447 		inet_freemsg(pending_mp);
9448 		return (ENOMEM);
9449 	}
9450 	/* Put together the chain. */
9451 	mp1->b_cont = mp2;
9452 	mp1->b_datap->db_type = M_IOCTL;
9453 	mp2->b_cont = mp;
9454 	mp2->b_datap->db_type = M_DATA;
9455 
9456 	iocp = (struct iocblk *)mp1->b_rptr;
9457 
9458 	/*
9459 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9460 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9461 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9462 	 * ioc_count field; set ioc_count to be correct.
9463 	 */
9464 	iocp->ioc_count = MBLKL(mp1->b_cont);
9465 
9466 	/*
9467 	 * Set the proper command in the ARP message.
9468 	 * Convert the SIOC{G|S|D}ARP calls into our
9469 	 * AR_ENTRY_xxx calls.
9470 	 */
9471 	area = (area_t *)mp2->b_rptr;
9472 	switch (iocp->ioc_cmd) {
9473 	case SIOCDARP:
9474 	case SIOCDXARP:
9475 		/*
9476 		 * We defer deleting the corresponding IRE until
9477 		 * we return from arp.
9478 		 */
9479 		area->area_cmd = AR_ENTRY_DELETE;
9480 		area->area_proto_mask_offset = 0;
9481 		break;
9482 	case SIOCGARP:
9483 	case SIOCGXARP:
9484 		area->area_cmd = AR_ENTRY_SQUERY;
9485 		area->area_proto_mask_offset = 0;
9486 		break;
9487 	case SIOCSARP:
9488 	case SIOCSXARP:
9489 		/*
9490 		 * Delete the corresponding ire to make sure IP will
9491 		 * pick up any change from arp.
9492 		 */
9493 		if (!if_arp_ioctl) {
9494 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9495 		} else {
9496 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9497 			if (ipif != NULL) {
9498 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9499 				    ipst);
9500 				ipif_refrele(ipif);
9501 			}
9502 		}
9503 		break;
9504 	}
9505 	iocp->ioc_cmd = area->area_cmd;
9506 
9507 	/*
9508 	 * Fill in the rest of the ARP operation fields.
9509 	 */
9510 	area->area_hw_addr_length = alength;
9511 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9512 
9513 	/* Translate the flags. */
9514 	if (flags & ATF_PERM)
9515 		area->area_flags |= ACE_F_PERMANENT;
9516 	if (flags & ATF_PUBL)
9517 		area->area_flags |= ACE_F_PUBLISH;
9518 	if (flags & ATF_AUTHORITY)
9519 		area->area_flags |= ACE_F_AUTHORITY;
9520 
9521 	/*
9522 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9523 	 * so that IP can update ARP as the active ills in the group change.
9524 	 */
9525 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9526 	    (area->area_flags & ACE_F_PERMANENT)) {
9527 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9528 
9529 		/*
9530 		 * The second part of the conditional below handles a corner
9531 		 * case: if this is proxy ARP and the IPMP group has no active
9532 		 * interfaces, we can't send the request to ARP now since it
9533 		 * won't be able to build an ACE.  So we return success and
9534 		 * notify ARP about the proxy ARP entry once an interface
9535 		 * becomes active.
9536 		 */
9537 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9538 			mp2->b_cont = NULL;
9539 			inet_freemsg(mp1);
9540 			inet_freemsg(pending_mp);
9541 			return (entp == NULL ? ENOMEM : 0);
9542 		}
9543 	}
9544 
9545 	/*
9546 	 * Before sending 'mp' to ARP, we have to clear the b_next
9547 	 * and b_prev. Otherwise if STREAMS encounters such a message
9548 	 * in freemsg(), (because ARP can close any time) it can cause
9549 	 * a panic. But mi code needs the b_next and b_prev values of
9550 	 * mp->b_cont, to complete the ioctl. So we store it here
9551 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9552 	 * when the response comes down from ARP.
9553 	 */
9554 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9555 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9556 	mp->b_cont->b_next = NULL;
9557 	mp->b_cont->b_prev = NULL;
9558 
9559 	mutex_enter(&connp->conn_lock);
9560 	mutex_enter(&ill->ill_lock);
9561 	/* conn has not yet started closing, hence this can't fail */
9562 	if (ipip->ipi_flags & IPI_WR) {
9563 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9564 		    pending_mp, 0) != 0);
9565 	} else {
9566 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9567 	}
9568 	mutex_exit(&ill->ill_lock);
9569 	mutex_exit(&connp->conn_lock);
9570 
9571 	/*
9572 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9573 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9574 	 */
9575 	putnext(ill->ill_rq, mp1);
9576 
9577 	/*
9578 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9579 	 */
9580 	if (entp != NULL)
9581 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9582 
9583 	return (EINPROGRESS);
9584 }
9585 
9586 /*
9587  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9588  * the associated sin and refhold and return the associated ipif via `ci'.
9589  */
9590 int
9591 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9592     cmd_info_t *ci, ipsq_func_t func)
9593 {
9594 	mblk_t	*mp1;
9595 	int	err;
9596 	sin_t	*sin;
9597 	conn_t	*connp;
9598 	ipif_t	*ipif;
9599 	ire_t	*ire = NULL;
9600 	ill_t	*ill = NULL;
9601 	boolean_t exists;
9602 	ip_stack_t *ipst;
9603 	struct arpreq *ar;
9604 	struct xarpreq *xar;
9605 	struct sockaddr_dl *sdl;
9606 
9607 	/* ioctl comes down on a conn */
9608 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9609 	connp = Q_TO_CONN(q);
9610 	if (connp->conn_af_isv6)
9611 		return (ENXIO);
9612 
9613 	ipst = connp->conn_netstack->netstack_ip;
9614 
9615 	/* Verified in ip_wput_nondata */
9616 	mp1 = mp->b_cont->b_cont;
9617 
9618 	if (ipip->ipi_cmd_type == XARP_CMD) {
9619 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9620 		xar = (struct xarpreq *)mp1->b_rptr;
9621 		sin = (sin_t *)&xar->xarp_pa;
9622 		sdl = &xar->xarp_ha;
9623 
9624 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9625 			return (ENXIO);
9626 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9627 			return (EINVAL);
9628 	} else {
9629 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9630 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9631 		ar = (struct arpreq *)mp1->b_rptr;
9632 		sin = (sin_t *)&ar->arp_pa;
9633 	}
9634 
9635 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9636 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9637 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9638 		    mp, func, &err, ipst);
9639 		if (ipif == NULL)
9640 			return (err);
9641 		if (ipif->ipif_id != 0) {
9642 			ipif_refrele(ipif);
9643 			return (ENXIO);
9644 		}
9645 	} else {
9646 		/*
9647 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9648 		 * of 0: use the IP address to find the ipif.  If the IP
9649 		 * address is an IPMP test address, ire_ftable_lookup() will
9650 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9651 		 */
9652 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9653 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9654 		if (ipif == NULL) {
9655 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9656 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9657 			    MATCH_IRE_TYPE, ipst);
9658 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9659 				if (ire != NULL)
9660 					ire_refrele(ire);
9661 				return (ENXIO);
9662 			}
9663 			ipif = ill->ill_ipif;
9664 			ipif_refhold(ipif);
9665 			ire_refrele(ire);
9666 		}
9667 	}
9668 
9669 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9670 		ipif_refrele(ipif);
9671 		return (ENXIO);
9672 	}
9673 
9674 	ci->ci_sin = sin;
9675 	ci->ci_ipif = ipif;
9676 	return (0);
9677 }
9678 
9679 /*
9680  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9681  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9682  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9683  * up and thus an ill can join that illgrp.
9684  *
9685  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9686  * open()/close() primarily because close() is not allowed to fail or block
9687  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9688  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9689  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9690  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9691  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9692  * state if I_UNLINK didn't occur.
9693  *
9694  * Note that for each plumb/unplumb operation, we may end up here more than
9695  * once because of the way ifconfig works.  However, it's OK to link the same
9696  * illgrp more than once, or unlink an illgrp that's already unlinked.
9697  */
9698 static int
9699 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9700 {
9701 	int err;
9702 	ip_stack_t *ipst = ill->ill_ipst;
9703 
9704 	ASSERT(IS_IPMP(ill));
9705 	ASSERT(IAM_WRITER_ILL(ill));
9706 
9707 	switch (ioccmd) {
9708 	case I_LINK:
9709 		return (ENOTSUP);
9710 
9711 	case I_PLINK:
9712 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9713 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9714 		rw_exit(&ipst->ips_ipmp_lock);
9715 		break;
9716 
9717 	case I_PUNLINK:
9718 		/*
9719 		 * Require all UP ipifs be brought down prior to unlinking the
9720 		 * illgrp so any associated IREs (and other state) is torched.
9721 		 */
9722 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9723 			return (EBUSY);
9724 
9725 		/*
9726 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9727 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9728 		 * join this group.  Specifically: ills trying to join grab
9729 		 * ipmp_lock and bump a "pending join" counter checked by
9730 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9731 		 * joins can occur (since we have ipmp_lock).  Once we drop
9732 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9733 		 * find the illgrp (since we unlinked it) and will return
9734 		 * EAFNOSUPPORT.  This will then take them back through the
9735 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9736 		 * back through I_PLINK above.
9737 		 */
9738 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9739 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9740 		rw_exit(&ipst->ips_ipmp_lock);
9741 		return (err);
9742 	default:
9743 		break;
9744 	}
9745 	return (0);
9746 }
9747 
9748 /*
9749  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9750  * atomically set/clear the muxids. Also complete the ioctl by acking or
9751  * naking it.  Note that the code is structured such that the link type,
9752  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9753  * its clones use the persistent link, while pppd(1M) and perhaps many
9754  * other daemons may use non-persistent link.  When combined with some
9755  * ill_t states, linking and unlinking lower streams may be used as
9756  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9757  */
9758 /* ARGSUSED */
9759 void
9760 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9761 {
9762 	mblk_t		*mp1, *mp2;
9763 	struct linkblk	*li;
9764 	struct ipmx_s	*ipmxp;
9765 	ill_t		*ill;
9766 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9767 	int		err = 0;
9768 	boolean_t	entered_ipsq = B_FALSE;
9769 	boolean_t	islink;
9770 	ip_stack_t	*ipst;
9771 
9772 	if (CONN_Q(q))
9773 		ipst = CONNQ_TO_IPST(q);
9774 	else
9775 		ipst = ILLQ_TO_IPST(q);
9776 
9777 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9778 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9779 
9780 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9781 
9782 	mp1 = mp->b_cont;	/* This is the linkblk info */
9783 	li = (struct linkblk *)mp1->b_rptr;
9784 
9785 	/*
9786 	 * ARP has added this special mblk, and the utility is asking us
9787 	 * to perform consistency checks, and also atomically set the
9788 	 * muxid. Ifconfig is an example.  It achieves this by using
9789 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9790 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9791 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9792 	 * and other comments in this routine for more details.
9793 	 */
9794 	mp2 = mp1->b_cont;	/* This is added by ARP */
9795 
9796 	/*
9797 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9798 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9799 	 * get the special mblk above.  For backward compatibility, we
9800 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9801 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9802 	 * not atomic, and can leave the streams unplumbable if the utility
9803 	 * is interrupted before it does the SIOCSLIFMUXID.
9804 	 */
9805 	if (mp2 == NULL) {
9806 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9807 		if (err == EINPROGRESS)
9808 			return;
9809 		goto done;
9810 	}
9811 
9812 	/*
9813 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9814 	 * ARP has appended this last mblk to tell us whether the lower stream
9815 	 * is an arp-dev stream or an IP module stream.
9816 	 */
9817 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9818 	if (ipmxp->ipmx_arpdev_stream) {
9819 		/*
9820 		 * The lower stream is the arp-dev stream.
9821 		 */
9822 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9823 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9824 		if (ill == NULL) {
9825 			if (err == EINPROGRESS)
9826 				return;
9827 			err = EINVAL;
9828 			goto done;
9829 		}
9830 
9831 		if (ipsq == NULL) {
9832 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9833 			    NEW_OP, B_FALSE);
9834 			if (ipsq == NULL) {
9835 				ill_refrele(ill);
9836 				return;
9837 			}
9838 			entered_ipsq = B_TRUE;
9839 		}
9840 		ASSERT(IAM_WRITER_ILL(ill));
9841 		ill_refrele(ill);
9842 
9843 		/*
9844 		 * To ensure consistency between IP and ARP, the following
9845 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9846 		 * This is because the muxid's are stored in the IP stream on
9847 		 * the ill.
9848 		 *
9849 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9850 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9851 		 * not yet plinked, and it also checks that the corresponding
9852 		 * IP stream is already plinked.
9853 		 *
9854 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9855 		 * punlinking the IP stream. IP does not allow punlink of the
9856 		 * IP stream unless the arp stream has been punlinked.
9857 		 */
9858 		if ((islink &&
9859 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9860 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9861 			err = EINVAL;
9862 			goto done;
9863 		}
9864 
9865 		if (IS_IPMP(ill) &&
9866 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9867 			goto done;
9868 
9869 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9870 	} else {
9871 		/*
9872 		 * The lower stream is probably an IP module stream.  Do
9873 		 * consistency checking.
9874 		 */
9875 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9876 		if (err == EINPROGRESS)
9877 			return;
9878 	}
9879 done:
9880 	if (err == 0)
9881 		miocack(q, mp, 0, 0);
9882 	else
9883 		miocnak(q, mp, 0, err);
9884 
9885 	/* Conn was refheld in ip_sioctl_copyin_setup */
9886 	if (CONN_Q(q))
9887 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9888 	if (entered_ipsq)
9889 		ipsq_exit(ipsq);
9890 }
9891 
9892 /*
9893  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9894  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9895  * module stream).  If `doconsist' is set, then do the extended consistency
9896  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9897  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9898  * an error code on failure.
9899  */
9900 static int
9901 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9902     struct linkblk *li, boolean_t doconsist)
9903 {
9904 	int		err = 0;
9905 	ill_t  		*ill;
9906 	queue_t		*ipwq, *dwq;
9907 	const char	*name;
9908 	struct qinit	*qinfo;
9909 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9910 	boolean_t	entered_ipsq = B_FALSE;
9911 
9912 	/*
9913 	 * Walk the lower stream to verify it's the IP module stream.
9914 	 * The IP module is identified by its name, wput function,
9915 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9916 	 * (li->l_qbot) will not vanish until this ioctl completes.
9917 	 */
9918 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9919 		qinfo = ipwq->q_qinfo;
9920 		name = qinfo->qi_minfo->mi_idname;
9921 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9922 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9923 			break;
9924 		}
9925 	}
9926 
9927 	/*
9928 	 * If this isn't an IP module stream, bail.
9929 	 */
9930 	if (ipwq == NULL)
9931 		return (0);
9932 
9933 	ill = ipwq->q_ptr;
9934 	ASSERT(ill != NULL);
9935 
9936 	if (ipsq == NULL) {
9937 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9938 		    NEW_OP, B_FALSE);
9939 		if (ipsq == NULL)
9940 			return (EINPROGRESS);
9941 		entered_ipsq = B_TRUE;
9942 	}
9943 	ASSERT(IAM_WRITER_ILL(ill));
9944 
9945 	if (doconsist) {
9946 		/*
9947 		 * Consistency checking requires that I_{P}LINK occurs
9948 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9949 		 * occurs prior to clearing ill_arp_muxid.
9950 		 */
9951 		if ((islink && ill->ill_ip_muxid != 0) ||
9952 		    (!islink && ill->ill_arp_muxid != 0)) {
9953 			err = EINVAL;
9954 			goto done;
9955 		}
9956 	}
9957 
9958 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9959 		goto done;
9960 
9961 	/*
9962 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9963 	 * the read queue of the module immediately below IP in the ill.
9964 	 * These are used during the capability negotiation below.
9965 	 */
9966 	ill->ill_lmod_rq = NULL;
9967 	ill->ill_lmod_cnt = 0;
9968 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9969 		ill->ill_lmod_rq = RD(dwq);
9970 		for (; dwq != NULL; dwq = dwq->q_next)
9971 			ill->ill_lmod_cnt++;
9972 	}
9973 
9974 	if (doconsist)
9975 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9976 
9977 	/*
9978 	 * Mark the ipsq busy until the capability operations initiated below
9979 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
9980 	 * returns, but the capability operation may complete asynchronously
9981 	 * much later.
9982 	 */
9983 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
9984 	/*
9985 	 * If there's at least one up ipif on this ill, then we're bound to
9986 	 * the underlying driver via DLPI.  In that case, renegotiate
9987 	 * capabilities to account for any possible change in modules
9988 	 * interposed between IP and the driver.
9989 	 */
9990 	if (ill->ill_ipif_up_count > 0) {
9991 		if (islink)
9992 			ill_capability_probe(ill);
9993 		else
9994 			ill_capability_reset(ill, B_FALSE);
9995 	}
9996 	ipsq_current_finish(ipsq);
9997 done:
9998 	if (entered_ipsq)
9999 		ipsq_exit(ipsq);
10000 
10001 	return (err);
10002 }
10003 
10004 /*
10005  * Search the ioctl command in the ioctl tables and return a pointer
10006  * to the ioctl command information. The ioctl command tables are
10007  * static and fully populated at compile time.
10008  */
10009 ip_ioctl_cmd_t *
10010 ip_sioctl_lookup(int ioc_cmd)
10011 {
10012 	int index;
10013 	ip_ioctl_cmd_t *ipip;
10014 	ip_ioctl_cmd_t *ipip_end;
10015 
10016 	if (ioc_cmd == IPI_DONTCARE)
10017 		return (NULL);
10018 
10019 	/*
10020 	 * Do a 2 step search. First search the indexed table
10021 	 * based on the least significant byte of the ioctl cmd.
10022 	 * If we don't find a match, then search the misc table
10023 	 * serially.
10024 	 */
10025 	index = ioc_cmd & 0xFF;
10026 	if (index < ip_ndx_ioctl_count) {
10027 		ipip = &ip_ndx_ioctl_table[index];
10028 		if (ipip->ipi_cmd == ioc_cmd) {
10029 			/* Found a match in the ndx table */
10030 			return (ipip);
10031 		}
10032 	}
10033 
10034 	/* Search the misc table */
10035 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10036 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10037 		if (ipip->ipi_cmd == ioc_cmd)
10038 			/* Found a match in the misc table */
10039 			return (ipip);
10040 	}
10041 
10042 	return (NULL);
10043 }
10044 
10045 /*
10046  * Wrapper function for resuming deferred ioctl processing
10047  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10048  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10049  */
10050 /* ARGSUSED */
10051 void
10052 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10053     void *dummy_arg)
10054 {
10055 	ip_sioctl_copyin_setup(q, mp);
10056 }
10057 
10058 /*
10059  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10060  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10061  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10062  * We establish here the size of the block to be copied in.  mi_copyin
10063  * arranges for this to happen, an processing continues in ip_wput with
10064  * an M_IOCDATA message.
10065  */
10066 void
10067 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10068 {
10069 	int	copyin_size;
10070 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10071 	ip_ioctl_cmd_t *ipip;
10072 	cred_t *cr;
10073 	ip_stack_t	*ipst;
10074 
10075 	if (CONN_Q(q))
10076 		ipst = CONNQ_TO_IPST(q);
10077 	else
10078 		ipst = ILLQ_TO_IPST(q);
10079 
10080 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10081 	if (ipip == NULL) {
10082 		/*
10083 		 * The ioctl is not one we understand or own.
10084 		 * Pass it along to be processed down stream,
10085 		 * if this is a module instance of IP, else nak
10086 		 * the ioctl.
10087 		 */
10088 		if (q->q_next == NULL) {
10089 			goto nak;
10090 		} else {
10091 			putnext(q, mp);
10092 			return;
10093 		}
10094 	}
10095 
10096 	/*
10097 	 * If this is deferred, then we will do all the checks when we
10098 	 * come back.
10099 	 */
10100 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10101 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10102 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10103 		return;
10104 	}
10105 
10106 	/*
10107 	 * Only allow a very small subset of IP ioctls on this stream if
10108 	 * IP is a module and not a driver. Allowing ioctls to be processed
10109 	 * in this case may cause assert failures or data corruption.
10110 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10111 	 * ioctls allowed on an IP module stream, after which this stream
10112 	 * normally becomes a multiplexor (at which time the stream head
10113 	 * will fail all ioctls).
10114 	 */
10115 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10116 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10117 			/*
10118 			 * Pass common Streams ioctls which the IP
10119 			 * module does not own or consume along to
10120 			 * be processed down stream.
10121 			 */
10122 			putnext(q, mp);
10123 			return;
10124 		} else {
10125 			goto nak;
10126 		}
10127 	}
10128 
10129 	/* Make sure we have ioctl data to process. */
10130 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10131 		goto nak;
10132 
10133 	/*
10134 	 * Prefer dblk credential over ioctl credential; some synthesized
10135 	 * ioctls have kcred set because there's no way to crhold()
10136 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10137 	 * the framework; the caller of ioctl needs to hold the reference
10138 	 * for the duration of the call).
10139 	 */
10140 	cr = msg_getcred(mp, NULL);
10141 	if (cr == NULL)
10142 		cr = iocp->ioc_cr;
10143 
10144 	/* Make sure normal users don't send down privileged ioctls */
10145 	if ((ipip->ipi_flags & IPI_PRIV) &&
10146 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10147 		/* We checked the privilege earlier but log it here */
10148 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10149 		return;
10150 	}
10151 
10152 	/*
10153 	 * The ioctl command tables can only encode fixed length
10154 	 * ioctl data. If the length is variable, the table will
10155 	 * encode the length as zero. Such special cases are handled
10156 	 * below in the switch.
10157 	 */
10158 	if (ipip->ipi_copyin_size != 0) {
10159 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10160 		return;
10161 	}
10162 
10163 	switch (iocp->ioc_cmd) {
10164 	case O_SIOCGIFCONF:
10165 	case SIOCGIFCONF:
10166 		/*
10167 		 * This IOCTL is hilarious.  See comments in
10168 		 * ip_sioctl_get_ifconf for the story.
10169 		 */
10170 		if (iocp->ioc_count == TRANSPARENT)
10171 			copyin_size = SIZEOF_STRUCT(ifconf,
10172 			    iocp->ioc_flag);
10173 		else
10174 			copyin_size = iocp->ioc_count;
10175 		mi_copyin(q, mp, NULL, copyin_size);
10176 		return;
10177 
10178 	case O_SIOCGLIFCONF:
10179 	case SIOCGLIFCONF:
10180 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10181 		mi_copyin(q, mp, NULL, copyin_size);
10182 		return;
10183 
10184 	case SIOCGLIFSRCOF:
10185 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10186 		mi_copyin(q, mp, NULL, copyin_size);
10187 		return;
10188 	case SIOCGIP6ADDRPOLICY:
10189 		ip_sioctl_ip6addrpolicy(q, mp);
10190 		ip6_asp_table_refrele(ipst);
10191 		return;
10192 
10193 	case SIOCSIP6ADDRPOLICY:
10194 		ip_sioctl_ip6addrpolicy(q, mp);
10195 		return;
10196 
10197 	case SIOCGDSTINFO:
10198 		ip_sioctl_dstinfo(q, mp);
10199 		ip6_asp_table_refrele(ipst);
10200 		return;
10201 
10202 	case I_PLINK:
10203 	case I_PUNLINK:
10204 	case I_LINK:
10205 	case I_UNLINK:
10206 		/*
10207 		 * We treat non-persistent link similarly as the persistent
10208 		 * link case, in terms of plumbing/unplumbing, as well as
10209 		 * dynamic re-plumbing events indicator.  See comments
10210 		 * in ip_sioctl_plink() for more.
10211 		 *
10212 		 * Request can be enqueued in the 'ipsq' while waiting
10213 		 * to become exclusive. So bump up the conn ref.
10214 		 */
10215 		if (CONN_Q(q))
10216 			CONN_INC_REF(Q_TO_CONN(q));
10217 		ip_sioctl_plink(NULL, q, mp, NULL);
10218 		return;
10219 
10220 	case ND_GET:
10221 	case ND_SET:
10222 		/*
10223 		 * Use of the nd table requires holding the reader lock.
10224 		 * Modifying the nd table thru nd_load/nd_unload requires
10225 		 * the writer lock.
10226 		 */
10227 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10228 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10229 			rw_exit(&ipst->ips_ip_g_nd_lock);
10230 
10231 			if (iocp->ioc_error)
10232 				iocp->ioc_count = 0;
10233 			mp->b_datap->db_type = M_IOCACK;
10234 			qreply(q, mp);
10235 			return;
10236 		}
10237 		rw_exit(&ipst->ips_ip_g_nd_lock);
10238 		/*
10239 		 * We don't understand this subioctl of ND_GET / ND_SET.
10240 		 * Maybe intended for some driver / module below us
10241 		 */
10242 		if (q->q_next) {
10243 			putnext(q, mp);
10244 		} else {
10245 			iocp->ioc_error = ENOENT;
10246 			mp->b_datap->db_type = M_IOCNAK;
10247 			iocp->ioc_count = 0;
10248 			qreply(q, mp);
10249 		}
10250 		return;
10251 
10252 	case IP_IOCTL:
10253 		ip_wput_ioctl(q, mp);
10254 		return;
10255 	default:
10256 		cmn_err(CE_PANIC, "should not happen ");
10257 	}
10258 nak:
10259 	if (mp->b_cont != NULL) {
10260 		freemsg(mp->b_cont);
10261 		mp->b_cont = NULL;
10262 	}
10263 	iocp->ioc_error = EINVAL;
10264 	mp->b_datap->db_type = M_IOCNAK;
10265 	iocp->ioc_count = 0;
10266 	qreply(q, mp);
10267 }
10268 
10269 /* ip_wput hands off ARP IOCTL responses to us */
10270 /* ARGSUSED3 */
10271 void
10272 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10273 {
10274 	struct arpreq *ar;
10275 	struct xarpreq *xar;
10276 	area_t	*area;
10277 	mblk_t	*area_mp;
10278 	struct iocblk *iocp;
10279 	mblk_t	*orig_ioc_mp, *tmp;
10280 	struct iocblk	*orig_iocp;
10281 	ill_t *ill;
10282 	conn_t *connp = NULL;
10283 	mblk_t *pending_mp;
10284 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10285 	int *flagsp;
10286 	char *storage = NULL;
10287 	sin_t *sin;
10288 	ipaddr_t addr;
10289 	int err;
10290 	ip_stack_t *ipst;
10291 
10292 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10293 	ill = q->q_ptr;
10294 	ASSERT(ill != NULL);
10295 	ipst = ill->ill_ipst;
10296 
10297 	/*
10298 	 * We should get back from ARP a packet chain that looks like:
10299 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10300 	 */
10301 	if (!(area_mp = mp->b_cont) ||
10302 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10303 	    !(orig_ioc_mp = area_mp->b_cont) ||
10304 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10305 		freemsg(mp);
10306 		return;
10307 	}
10308 
10309 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10310 
10311 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10312 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10313 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10314 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10315 		x_arp_ioctl = B_TRUE;
10316 		xar = (struct xarpreq *)tmp->b_rptr;
10317 		sin = (sin_t *)&xar->xarp_pa;
10318 		flagsp = &xar->xarp_flags;
10319 		storage = xar->xarp_ha.sdl_data;
10320 		if (xar->xarp_ha.sdl_nlen != 0)
10321 			ifx_arp_ioctl = B_TRUE;
10322 	} else {
10323 		ar = (struct arpreq *)tmp->b_rptr;
10324 		sin = (sin_t *)&ar->arp_pa;
10325 		flagsp = &ar->arp_flags;
10326 		storage = ar->arp_ha.sa_data;
10327 	}
10328 
10329 	iocp = (struct iocblk *)mp->b_rptr;
10330 
10331 	/*
10332 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10333 	 * Otherwise, we can find it from our ioc_id.
10334 	 */
10335 	if (ipsq != NULL)
10336 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10337 	else
10338 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10339 
10340 	if (pending_mp == NULL) {
10341 		ASSERT(connp == NULL);
10342 		inet_freemsg(mp);
10343 		return;
10344 	}
10345 	ASSERT(connp != NULL);
10346 	q = CONNP_TO_WQ(connp);
10347 
10348 	/* Uncouple the internally generated IOCTL from the original one */
10349 	area = (area_t *)area_mp->b_rptr;
10350 	area_mp->b_cont = NULL;
10351 
10352 	/*
10353 	 * Restore the b_next and b_prev used by mi code. This is needed
10354 	 * to complete the ioctl using mi* functions. We stored them in
10355 	 * the pending mp prior to sending the request to ARP.
10356 	 */
10357 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10358 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10359 	inet_freemsg(pending_mp);
10360 
10361 	/*
10362 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10363 	 * Catch the case where there is an IRE_CACHE by no entry in the
10364 	 * arp table.
10365 	 */
10366 	addr = sin->sin_addr.s_addr;
10367 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10368 		ire_t			*ire;
10369 		dl_unitdata_req_t	*dlup;
10370 		mblk_t			*llmp;
10371 		int			addr_len;
10372 		ill_t			*ipsqill = NULL;
10373 
10374 		if (ifx_arp_ioctl) {
10375 			/*
10376 			 * There's no need to lookup the ill, since
10377 			 * we've already done that when we started
10378 			 * processing the ioctl and sent the message
10379 			 * to ARP on that ill.  So use the ill that
10380 			 * is stored in q->q_ptr.
10381 			 */
10382 			ipsqill = ill;
10383 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10384 			    ipsqill->ill_ipif, ALL_ZONES,
10385 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10386 		} else {
10387 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10388 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10389 			if (ire != NULL)
10390 				ipsqill = ire_to_ill(ire);
10391 		}
10392 
10393 		if ((x_arp_ioctl) && (ipsqill != NULL))
10394 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10395 
10396 		if (ire != NULL) {
10397 			/*
10398 			 * Since the ire obtained from cachetable is used for
10399 			 * mac addr copying below, treat an incomplete ire as if
10400 			 * as if we never found it.
10401 			 */
10402 			if (ire->ire_nce != NULL &&
10403 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10404 				ire_refrele(ire);
10405 				ire = NULL;
10406 				ipsqill = NULL;
10407 				goto errack;
10408 			}
10409 			*flagsp = ATF_INUSE;
10410 			llmp = (ire->ire_nce != NULL ?
10411 			    ire->ire_nce->nce_res_mp : NULL);
10412 			if (llmp != NULL && ipsqill != NULL) {
10413 				uchar_t *macaddr;
10414 
10415 				addr_len = ipsqill->ill_phys_addr_length;
10416 				if (x_arp_ioctl && ((addr_len +
10417 				    ipsqill->ill_name_length) >
10418 				    sizeof (xar->xarp_ha.sdl_data))) {
10419 					ire_refrele(ire);
10420 					freemsg(mp);
10421 					ip_ioctl_finish(q, orig_ioc_mp,
10422 					    EINVAL, NO_COPYOUT, ipsq);
10423 					return;
10424 				}
10425 				*flagsp |= ATF_COM;
10426 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10427 				if (ipsqill->ill_sap_length < 0)
10428 					macaddr = llmp->b_rptr +
10429 					    dlup->dl_dest_addr_offset;
10430 				else
10431 					macaddr = llmp->b_rptr +
10432 					    dlup->dl_dest_addr_offset +
10433 					    ipsqill->ill_sap_length;
10434 				/*
10435 				 * For SIOCGARP, MAC address length
10436 				 * validation has already been done
10437 				 * before the ioctl was issued to ARP to
10438 				 * allow it to progress only on 6 byte
10439 				 * addressable (ethernet like) media. Thus
10440 				 * the mac address copying can not overwrite
10441 				 * the sa_data area below.
10442 				 */
10443 				bcopy(macaddr, storage, addr_len);
10444 			}
10445 			/* Ditch the internal IOCTL. */
10446 			freemsg(mp);
10447 			ire_refrele(ire);
10448 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10449 			return;
10450 		}
10451 	}
10452 
10453 	/*
10454 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10455 	 * on the IPMP meta-interface, ensure any ARP entries added in
10456 	 * ip_sioctl_arp() are deleted.
10457 	 */
10458 	if (IS_IPMP(ill) &&
10459 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10460 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10461 		ipmp_illgrp_t *illg = ill->ill_grp;
10462 		ipmp_arpent_t *entp;
10463 
10464 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10465 			ipmp_illgrp_destroy_arpent(illg, entp);
10466 	}
10467 
10468 	/*
10469 	 * Delete the coresponding IRE_CACHE if any.
10470 	 * Reset the error if there was one (in case there was no entry
10471 	 * in arp.)
10472 	 */
10473 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10474 		ipif_t *ipintf = NULL;
10475 
10476 		if (ifx_arp_ioctl) {
10477 			/*
10478 			 * There's no need to lookup the ill, since
10479 			 * we've already done that when we started
10480 			 * processing the ioctl and sent the message
10481 			 * to ARP on that ill.  So use the ill that
10482 			 * is stored in q->q_ptr.
10483 			 */
10484 			ipintf = ill->ill_ipif;
10485 		}
10486 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10487 			/*
10488 			 * The address in "addr" may be an entry for a
10489 			 * router. If that's true, then any off-net
10490 			 * IRE_CACHE entries that go through the router
10491 			 * with address "addr" must be clobbered. Use
10492 			 * ire_walk to achieve this goal.
10493 			 */
10494 			if (ifx_arp_ioctl)
10495 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10496 				    ire_delete_cache_gw, (char *)&addr, ill);
10497 			else
10498 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10499 				    ALL_ZONES, ipst);
10500 			iocp->ioc_error = 0;
10501 		}
10502 	}
10503 errack:
10504 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10505 		err = iocp->ioc_error;
10506 		freemsg(mp);
10507 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10508 		return;
10509 	}
10510 
10511 	/*
10512 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10513 	 * the area_t into the struct {x}arpreq.
10514 	 */
10515 	if (x_arp_ioctl) {
10516 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10517 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10518 		    sizeof (xar->xarp_ha.sdl_data)) {
10519 			freemsg(mp);
10520 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10521 			    ipsq);
10522 			return;
10523 		}
10524 	}
10525 	*flagsp = ATF_INUSE;
10526 	if (area->area_flags & ACE_F_PERMANENT)
10527 		*flagsp |= ATF_PERM;
10528 	if (area->area_flags & ACE_F_PUBLISH)
10529 		*flagsp |= ATF_PUBL;
10530 	if (area->area_flags & ACE_F_AUTHORITY)
10531 		*flagsp |= ATF_AUTHORITY;
10532 	if (area->area_hw_addr_length != 0) {
10533 		*flagsp |= ATF_COM;
10534 		/*
10535 		 * For SIOCGARP, MAC address length validation has
10536 		 * already been done before the ioctl was issued to ARP
10537 		 * to allow it to progress only on 6 byte addressable
10538 		 * (ethernet like) media. Thus the mac address copying
10539 		 * can not overwrite the sa_data area below.
10540 		 */
10541 		bcopy((char *)area + area->area_hw_addr_offset,
10542 		    storage, area->area_hw_addr_length);
10543 	}
10544 
10545 	/* Ditch the internal IOCTL. */
10546 	freemsg(mp);
10547 	/* Complete the original. */
10548 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10549 }
10550 
10551 /*
10552  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10553  * interface) create the next available logical interface for this
10554  * physical interface.
10555  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10556  * ipif with the specified name.
10557  *
10558  * If the address family is not AF_UNSPEC then set the address as well.
10559  *
10560  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10561  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10562  *
10563  * Executed as a writer on the ill.
10564  * So no lock is needed to traverse the ipif chain, or examine the
10565  * phyint flags.
10566  */
10567 /* ARGSUSED */
10568 int
10569 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10570     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10571 {
10572 	mblk_t	*mp1;
10573 	struct lifreq *lifr;
10574 	boolean_t	isv6;
10575 	boolean_t	exists;
10576 	char 	*name;
10577 	char	*endp;
10578 	char	*cp;
10579 	int	namelen;
10580 	ipif_t	*ipif;
10581 	long	id;
10582 	ipsq_t	*ipsq;
10583 	ill_t	*ill;
10584 	sin_t	*sin;
10585 	int	err = 0;
10586 	boolean_t found_sep = B_FALSE;
10587 	conn_t	*connp;
10588 	zoneid_t zoneid;
10589 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10590 
10591 	ASSERT(q->q_next == NULL);
10592 	ip1dbg(("ip_sioctl_addif\n"));
10593 	/* Existence of mp1 has been checked in ip_wput_nondata */
10594 	mp1 = mp->b_cont->b_cont;
10595 	/*
10596 	 * Null terminate the string to protect against buffer
10597 	 * overrun. String was generated by user code and may not
10598 	 * be trusted.
10599 	 */
10600 	lifr = (struct lifreq *)mp1->b_rptr;
10601 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10602 	name = lifr->lifr_name;
10603 	ASSERT(CONN_Q(q));
10604 	connp = Q_TO_CONN(q);
10605 	isv6 = connp->conn_af_isv6;
10606 	zoneid = connp->conn_zoneid;
10607 	namelen = mi_strlen(name);
10608 	if (namelen == 0)
10609 		return (EINVAL);
10610 
10611 	exists = B_FALSE;
10612 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10613 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10614 		/*
10615 		 * Allow creating lo0 using SIOCLIFADDIF.
10616 		 * can't be any other writer thread. So can pass null below
10617 		 * for the last 4 args to ipif_lookup_name.
10618 		 */
10619 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10620 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10621 		/* Prevent any further action */
10622 		if (ipif == NULL) {
10623 			return (ENOBUFS);
10624 		} else if (!exists) {
10625 			/* We created the ipif now and as writer */
10626 			ipif_refrele(ipif);
10627 			return (0);
10628 		} else {
10629 			ill = ipif->ipif_ill;
10630 			ill_refhold(ill);
10631 			ipif_refrele(ipif);
10632 		}
10633 	} else {
10634 		/* Look for a colon in the name. */
10635 		endp = &name[namelen];
10636 		for (cp = endp; --cp > name; ) {
10637 			if (*cp == IPIF_SEPARATOR_CHAR) {
10638 				found_sep = B_TRUE;
10639 				/*
10640 				 * Reject any non-decimal aliases for plumbing
10641 				 * of logical interfaces. Aliases with leading
10642 				 * zeroes are also rejected as they introduce
10643 				 * ambiguity in the naming of the interfaces.
10644 				 * Comparing with "0" takes care of all such
10645 				 * cases.
10646 				 */
10647 				if ((strncmp("0", cp+1, 1)) == 0)
10648 					return (EINVAL);
10649 
10650 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10651 				    id <= 0 || *endp != '\0') {
10652 					return (EINVAL);
10653 				}
10654 				*cp = '\0';
10655 				break;
10656 			}
10657 		}
10658 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10659 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10660 		if (found_sep)
10661 			*cp = IPIF_SEPARATOR_CHAR;
10662 		if (ill == NULL)
10663 			return (err);
10664 	}
10665 
10666 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10667 	    B_TRUE);
10668 
10669 	/*
10670 	 * Release the refhold due to the lookup, now that we are excl
10671 	 * or we are just returning
10672 	 */
10673 	ill_refrele(ill);
10674 
10675 	if (ipsq == NULL)
10676 		return (EINPROGRESS);
10677 
10678 	/* We are now exclusive on the IPSQ */
10679 	ASSERT(IAM_WRITER_ILL(ill));
10680 
10681 	if (found_sep) {
10682 		/* Now see if there is an IPIF with this unit number. */
10683 		for (ipif = ill->ill_ipif; ipif != NULL;
10684 		    ipif = ipif->ipif_next) {
10685 			if (ipif->ipif_id == id) {
10686 				err = EEXIST;
10687 				goto done;
10688 			}
10689 		}
10690 	}
10691 
10692 	/*
10693 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10694 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10695 	 * instead.
10696 	 */
10697 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10698 	    B_TRUE, B_TRUE)) == NULL) {
10699 		err = ENOBUFS;
10700 		goto done;
10701 	}
10702 
10703 	/* Return created name with ioctl */
10704 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10705 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10706 	ip1dbg(("created %s\n", lifr->lifr_name));
10707 
10708 	/* Set address */
10709 	sin = (sin_t *)&lifr->lifr_addr;
10710 	if (sin->sin_family != AF_UNSPEC) {
10711 		err = ip_sioctl_addr(ipif, sin, q, mp,
10712 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10713 	}
10714 
10715 done:
10716 	ipsq_exit(ipsq);
10717 	return (err);
10718 }
10719 
10720 /*
10721  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10722  * interface) delete it based on the IP address (on this physical interface).
10723  * Otherwise delete it based on the ipif_id.
10724  * Also, special handling to allow a removeif of lo0.
10725  */
10726 /* ARGSUSED */
10727 int
10728 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10729     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10730 {
10731 	conn_t		*connp;
10732 	ill_t		*ill = ipif->ipif_ill;
10733 	boolean_t	 success;
10734 	ip_stack_t	*ipst;
10735 
10736 	ipst = CONNQ_TO_IPST(q);
10737 
10738 	ASSERT(q->q_next == NULL);
10739 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10740 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10741 	ASSERT(IAM_WRITER_IPIF(ipif));
10742 
10743 	connp = Q_TO_CONN(q);
10744 	/*
10745 	 * Special case for unplumbing lo0 (the loopback physical interface).
10746 	 * If unplumbing lo0, the incoming address structure has been
10747 	 * initialized to all zeros. When unplumbing lo0, all its logical
10748 	 * interfaces must be removed too.
10749 	 *
10750 	 * Note that this interface may be called to remove a specific
10751 	 * loopback logical interface (eg, lo0:1). But in that case
10752 	 * ipif->ipif_id != 0 so that the code path for that case is the
10753 	 * same as any other interface (meaning it skips the code directly
10754 	 * below).
10755 	 */
10756 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10757 		if (sin->sin_family == AF_UNSPEC &&
10758 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10759 			/*
10760 			 * Mark it condemned. No new ref. will be made to ill.
10761 			 */
10762 			mutex_enter(&ill->ill_lock);
10763 			ill->ill_state_flags |= ILL_CONDEMNED;
10764 			for (ipif = ill->ill_ipif; ipif != NULL;
10765 			    ipif = ipif->ipif_next) {
10766 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10767 			}
10768 			mutex_exit(&ill->ill_lock);
10769 
10770 			ipif = ill->ill_ipif;
10771 			/* unplumb the loopback interface */
10772 			ill_delete(ill);
10773 			mutex_enter(&connp->conn_lock);
10774 			mutex_enter(&ill->ill_lock);
10775 
10776 			/* Are any references to this ill active */
10777 			if (ill_is_freeable(ill)) {
10778 				mutex_exit(&ill->ill_lock);
10779 				mutex_exit(&connp->conn_lock);
10780 				ill_delete_tail(ill);
10781 				mi_free(ill);
10782 				return (0);
10783 			}
10784 			success = ipsq_pending_mp_add(connp, ipif,
10785 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10786 			mutex_exit(&connp->conn_lock);
10787 			mutex_exit(&ill->ill_lock);
10788 			if (success)
10789 				return (EINPROGRESS);
10790 			else
10791 				return (EINTR);
10792 		}
10793 	}
10794 
10795 	if (ipif->ipif_id == 0) {
10796 		ipsq_t *ipsq;
10797 
10798 		/* Find based on address */
10799 		if (ipif->ipif_isv6) {
10800 			sin6_t *sin6;
10801 
10802 			if (sin->sin_family != AF_INET6)
10803 				return (EAFNOSUPPORT);
10804 
10805 			sin6 = (sin6_t *)sin;
10806 			/* We are a writer, so we should be able to lookup */
10807 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10808 			    ipst);
10809 		} else {
10810 			if (sin->sin_family != AF_INET)
10811 				return (EAFNOSUPPORT);
10812 
10813 			/* We are a writer, so we should be able to lookup */
10814 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10815 			    ipst);
10816 		}
10817 		if (ipif == NULL) {
10818 			return (EADDRNOTAVAIL);
10819 		}
10820 
10821 		/*
10822 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10823 		 * lifr_name of the physical interface but with an ip address
10824 		 * lifr_addr of a logical interface plumbed over it.
10825 		 * So update ipx_current_ipif now that ipif points to the
10826 		 * correct one.
10827 		 */
10828 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10829 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10830 
10831 		/* This is a writer */
10832 		ipif_refrele(ipif);
10833 	}
10834 
10835 	/*
10836 	 * Can not delete instance zero since it is tied to the ill.
10837 	 */
10838 	if (ipif->ipif_id == 0)
10839 		return (EBUSY);
10840 
10841 	mutex_enter(&ill->ill_lock);
10842 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10843 	mutex_exit(&ill->ill_lock);
10844 
10845 	ipif_free(ipif);
10846 
10847 	mutex_enter(&connp->conn_lock);
10848 	mutex_enter(&ill->ill_lock);
10849 
10850 	/* Are any references to this ipif active */
10851 	if (ipif_is_freeable(ipif)) {
10852 		mutex_exit(&ill->ill_lock);
10853 		mutex_exit(&connp->conn_lock);
10854 		ipif_non_duplicate(ipif);
10855 		ipif_down_tail(ipif);
10856 		ipif_free_tail(ipif); /* frees ipif */
10857 		return (0);
10858 	}
10859 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10860 	    IPIF_FREE);
10861 	mutex_exit(&ill->ill_lock);
10862 	mutex_exit(&connp->conn_lock);
10863 	if (success)
10864 		return (EINPROGRESS);
10865 	else
10866 		return (EINTR);
10867 }
10868 
10869 /*
10870  * Restart the removeif ioctl. The refcnt has gone down to 0.
10871  * The ipif is already condemned. So can't find it thru lookups.
10872  */
10873 /* ARGSUSED */
10874 int
10875 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10876     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10877 {
10878 	ill_t *ill = ipif->ipif_ill;
10879 
10880 	ASSERT(IAM_WRITER_IPIF(ipif));
10881 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10882 
10883 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10884 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10885 
10886 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10887 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10888 		ill_delete_tail(ill);
10889 		mi_free(ill);
10890 		return (0);
10891 	}
10892 
10893 	ipif_non_duplicate(ipif);
10894 	ipif_down_tail(ipif);
10895 	ipif_free_tail(ipif);
10896 
10897 	ILL_UNMARK_CHANGING(ill);
10898 	return (0);
10899 }
10900 
10901 /*
10902  * Set the local interface address.
10903  * Allow an address of all zero when the interface is down.
10904  */
10905 /* ARGSUSED */
10906 int
10907 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10908     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10909 {
10910 	int err = 0;
10911 	in6_addr_t v6addr;
10912 	boolean_t need_up = B_FALSE;
10913 
10914 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10915 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10916 
10917 	ASSERT(IAM_WRITER_IPIF(ipif));
10918 
10919 	if (ipif->ipif_isv6) {
10920 		sin6_t *sin6;
10921 		ill_t *ill;
10922 		phyint_t *phyi;
10923 
10924 		if (sin->sin_family != AF_INET6)
10925 			return (EAFNOSUPPORT);
10926 
10927 		sin6 = (sin6_t *)sin;
10928 		v6addr = sin6->sin6_addr;
10929 		ill = ipif->ipif_ill;
10930 		phyi = ill->ill_phyint;
10931 
10932 		/*
10933 		 * Enforce that true multicast interfaces have a link-local
10934 		 * address for logical unit 0.
10935 		 */
10936 		if (ipif->ipif_id == 0 &&
10937 		    (ill->ill_flags & ILLF_MULTICAST) &&
10938 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10939 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10940 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10941 			return (EADDRNOTAVAIL);
10942 		}
10943 
10944 		/*
10945 		 * up interfaces shouldn't have the unspecified address
10946 		 * unless they also have the IPIF_NOLOCAL flags set and
10947 		 * have a subnet assigned.
10948 		 */
10949 		if ((ipif->ipif_flags & IPIF_UP) &&
10950 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10951 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10952 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10953 			return (EADDRNOTAVAIL);
10954 		}
10955 
10956 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10957 			return (EADDRNOTAVAIL);
10958 	} else {
10959 		ipaddr_t addr;
10960 
10961 		if (sin->sin_family != AF_INET)
10962 			return (EAFNOSUPPORT);
10963 
10964 		addr = sin->sin_addr.s_addr;
10965 
10966 		/* Allow 0 as the local address. */
10967 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10968 			return (EADDRNOTAVAIL);
10969 
10970 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10971 	}
10972 
10973 	/*
10974 	 * Even if there is no change we redo things just to rerun
10975 	 * ipif_set_default.
10976 	 */
10977 	if (ipif->ipif_flags & IPIF_UP) {
10978 		/*
10979 		 * Setting a new local address, make sure
10980 		 * we have net and subnet bcast ire's for
10981 		 * the old address if we need them.
10982 		 */
10983 		if (!ipif->ipif_isv6)
10984 			ipif_check_bcast_ires(ipif);
10985 		/*
10986 		 * If the interface is already marked up,
10987 		 * we call ipif_down which will take care
10988 		 * of ditching any IREs that have been set
10989 		 * up based on the old interface address.
10990 		 */
10991 		err = ipif_logical_down(ipif, q, mp);
10992 		if (err == EINPROGRESS)
10993 			return (err);
10994 		ipif_down_tail(ipif);
10995 		need_up = 1;
10996 	}
10997 
10998 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10999 	return (err);
11000 }
11001 
11002 int
11003 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11004     boolean_t need_up)
11005 {
11006 	in6_addr_t v6addr;
11007 	in6_addr_t ov6addr;
11008 	ipaddr_t addr;
11009 	sin6_t	*sin6;
11010 	int	sinlen;
11011 	int	err = 0;
11012 	ill_t	*ill = ipif->ipif_ill;
11013 	boolean_t need_dl_down;
11014 	boolean_t need_arp_down;
11015 	struct iocblk *iocp;
11016 
11017 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11018 
11019 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11020 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11021 	ASSERT(IAM_WRITER_IPIF(ipif));
11022 
11023 	/* Must cancel any pending timer before taking the ill_lock */
11024 	if (ipif->ipif_recovery_id != 0)
11025 		(void) untimeout(ipif->ipif_recovery_id);
11026 	ipif->ipif_recovery_id = 0;
11027 
11028 	if (ipif->ipif_isv6) {
11029 		sin6 = (sin6_t *)sin;
11030 		v6addr = sin6->sin6_addr;
11031 		sinlen = sizeof (struct sockaddr_in6);
11032 	} else {
11033 		addr = sin->sin_addr.s_addr;
11034 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11035 		sinlen = sizeof (struct sockaddr_in);
11036 	}
11037 	mutex_enter(&ill->ill_lock);
11038 	ov6addr = ipif->ipif_v6lcl_addr;
11039 	ipif->ipif_v6lcl_addr = v6addr;
11040 	sctp_update_ipif_addr(ipif, ov6addr);
11041 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11042 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11043 	} else {
11044 		ipif->ipif_v6src_addr = v6addr;
11045 	}
11046 	ipif->ipif_addr_ready = 0;
11047 
11048 	/*
11049 	 * If the interface was previously marked as a duplicate, then since
11050 	 * we've now got a "new" address, it should no longer be considered a
11051 	 * duplicate -- even if the "new" address is the same as the old one.
11052 	 * Note that if all ipifs are down, we may have a pending ARP down
11053 	 * event to handle.  This is because we want to recover from duplicates
11054 	 * and thus delay tearing down ARP until the duplicates have been
11055 	 * removed or disabled.
11056 	 */
11057 	need_dl_down = need_arp_down = B_FALSE;
11058 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11059 		need_arp_down = !need_up;
11060 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11061 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11062 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11063 			need_dl_down = B_TRUE;
11064 		}
11065 	}
11066 
11067 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11068 	    !ill->ill_is_6to4tun) {
11069 		queue_t *wqp = ill->ill_wq;
11070 
11071 		/*
11072 		 * The local address of this interface is a 6to4 address,
11073 		 * check if this interface is in fact a 6to4 tunnel or just
11074 		 * an interface configured with a 6to4 address.  We are only
11075 		 * interested in the former.
11076 		 */
11077 		if (wqp != NULL) {
11078 			while ((wqp->q_next != NULL) &&
11079 			    (wqp->q_next->q_qinfo != NULL) &&
11080 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11081 
11082 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11083 				    == TUN6TO4_MODID) {
11084 					/* set for use in IP */
11085 					ill->ill_is_6to4tun = 1;
11086 					break;
11087 				}
11088 				wqp = wqp->q_next;
11089 			}
11090 		}
11091 	}
11092 
11093 	ipif_set_default(ipif);
11094 
11095 	/*
11096 	 * When publishing an interface address change event, we only notify
11097 	 * the event listeners of the new address.  It is assumed that if they
11098 	 * actively care about the addresses assigned that they will have
11099 	 * already discovered the previous address assigned (if there was one.)
11100 	 *
11101 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11102 	 */
11103 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11104 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11105 		    NE_ADDRESS_CHANGE, sin, sinlen);
11106 	}
11107 
11108 	mutex_exit(&ill->ill_lock);
11109 
11110 	if (need_up) {
11111 		/*
11112 		 * Now bring the interface back up.  If this
11113 		 * is the only IPIF for the ILL, ipif_up
11114 		 * will have to re-bind to the device, so
11115 		 * we may get back EINPROGRESS, in which
11116 		 * case, this IOCTL will get completed in
11117 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11118 		 */
11119 		err = ipif_up(ipif, q, mp);
11120 	}
11121 
11122 	if (need_dl_down)
11123 		ill_dl_down(ill);
11124 	if (need_arp_down)
11125 		ipif_resolver_down(ipif);
11126 
11127 	return (err);
11128 }
11129 
11130 /*
11131  * Restart entry point to restart the address set operation after the
11132  * refcounts have dropped to zero.
11133  */
11134 /* ARGSUSED */
11135 int
11136 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11137     ip_ioctl_cmd_t *ipip, void *ifreq)
11138 {
11139 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11140 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11141 	ASSERT(IAM_WRITER_IPIF(ipif));
11142 	ipif_down_tail(ipif);
11143 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11144 }
11145 
11146 /* ARGSUSED */
11147 int
11148 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11149     ip_ioctl_cmd_t *ipip, void *if_req)
11150 {
11151 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11152 	struct lifreq *lifr = (struct lifreq *)if_req;
11153 
11154 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11155 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11156 	/*
11157 	 * The net mask and address can't change since we have a
11158 	 * reference to the ipif. So no lock is necessary.
11159 	 */
11160 	if (ipif->ipif_isv6) {
11161 		*sin6 = sin6_null;
11162 		sin6->sin6_family = AF_INET6;
11163 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11164 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11165 		lifr->lifr_addrlen =
11166 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11167 	} else {
11168 		*sin = sin_null;
11169 		sin->sin_family = AF_INET;
11170 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11171 		if (ipip->ipi_cmd_type == LIF_CMD) {
11172 			lifr->lifr_addrlen =
11173 			    ip_mask_to_plen(ipif->ipif_net_mask);
11174 		}
11175 	}
11176 	return (0);
11177 }
11178 
11179 /*
11180  * Set the destination address for a pt-pt interface.
11181  */
11182 /* ARGSUSED */
11183 int
11184 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11185     ip_ioctl_cmd_t *ipip, void *if_req)
11186 {
11187 	int err = 0;
11188 	in6_addr_t v6addr;
11189 	boolean_t need_up = B_FALSE;
11190 
11191 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11192 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11193 	ASSERT(IAM_WRITER_IPIF(ipif));
11194 
11195 	if (ipif->ipif_isv6) {
11196 		sin6_t *sin6;
11197 
11198 		if (sin->sin_family != AF_INET6)
11199 			return (EAFNOSUPPORT);
11200 
11201 		sin6 = (sin6_t *)sin;
11202 		v6addr = sin6->sin6_addr;
11203 
11204 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11205 			return (EADDRNOTAVAIL);
11206 	} else {
11207 		ipaddr_t addr;
11208 
11209 		if (sin->sin_family != AF_INET)
11210 			return (EAFNOSUPPORT);
11211 
11212 		addr = sin->sin_addr.s_addr;
11213 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11214 			return (EADDRNOTAVAIL);
11215 
11216 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11217 	}
11218 
11219 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11220 		return (0);	/* No change */
11221 
11222 	if (ipif->ipif_flags & IPIF_UP) {
11223 		/*
11224 		 * If the interface is already marked up,
11225 		 * we call ipif_down which will take care
11226 		 * of ditching any IREs that have been set
11227 		 * up based on the old pp dst address.
11228 		 */
11229 		err = ipif_logical_down(ipif, q, mp);
11230 		if (err == EINPROGRESS)
11231 			return (err);
11232 		ipif_down_tail(ipif);
11233 		need_up = B_TRUE;
11234 	}
11235 	/*
11236 	 * could return EINPROGRESS. If so ioctl will complete in
11237 	 * ip_rput_dlpi_writer
11238 	 */
11239 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11240 	return (err);
11241 }
11242 
11243 static int
11244 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11245     boolean_t need_up)
11246 {
11247 	in6_addr_t v6addr;
11248 	ill_t	*ill = ipif->ipif_ill;
11249 	int	err = 0;
11250 	boolean_t need_dl_down;
11251 	boolean_t need_arp_down;
11252 
11253 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11254 	    ipif->ipif_id, (void *)ipif));
11255 
11256 	/* Must cancel any pending timer before taking the ill_lock */
11257 	if (ipif->ipif_recovery_id != 0)
11258 		(void) untimeout(ipif->ipif_recovery_id);
11259 	ipif->ipif_recovery_id = 0;
11260 
11261 	if (ipif->ipif_isv6) {
11262 		sin6_t *sin6;
11263 
11264 		sin6 = (sin6_t *)sin;
11265 		v6addr = sin6->sin6_addr;
11266 	} else {
11267 		ipaddr_t addr;
11268 
11269 		addr = sin->sin_addr.s_addr;
11270 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11271 	}
11272 	mutex_enter(&ill->ill_lock);
11273 	/* Set point to point destination address. */
11274 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11275 		/*
11276 		 * Allow this as a means of creating logical
11277 		 * pt-pt interfaces on top of e.g. an Ethernet.
11278 		 * XXX Undocumented HACK for testing.
11279 		 * pt-pt interfaces are created with NUD disabled.
11280 		 */
11281 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11282 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11283 		if (ipif->ipif_isv6)
11284 			ill->ill_flags |= ILLF_NONUD;
11285 	}
11286 
11287 	/*
11288 	 * If the interface was previously marked as a duplicate, then since
11289 	 * we've now got a "new" address, it should no longer be considered a
11290 	 * duplicate -- even if the "new" address is the same as the old one.
11291 	 * Note that if all ipifs are down, we may have a pending ARP down
11292 	 * event to handle.
11293 	 */
11294 	need_dl_down = need_arp_down = B_FALSE;
11295 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11296 		need_arp_down = !need_up;
11297 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11298 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11299 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11300 			need_dl_down = B_TRUE;
11301 		}
11302 	}
11303 
11304 	/* Set the new address. */
11305 	ipif->ipif_v6pp_dst_addr = v6addr;
11306 	/* Make sure subnet tracks pp_dst */
11307 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11308 	mutex_exit(&ill->ill_lock);
11309 
11310 	if (need_up) {
11311 		/*
11312 		 * Now bring the interface back up.  If this
11313 		 * is the only IPIF for the ILL, ipif_up
11314 		 * will have to re-bind to the device, so
11315 		 * we may get back EINPROGRESS, in which
11316 		 * case, this IOCTL will get completed in
11317 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11318 		 */
11319 		err = ipif_up(ipif, q, mp);
11320 	}
11321 
11322 	if (need_dl_down)
11323 		ill_dl_down(ill);
11324 	if (need_arp_down)
11325 		ipif_resolver_down(ipif);
11326 
11327 	return (err);
11328 }
11329 
11330 /*
11331  * Restart entry point to restart the dstaddress set operation after the
11332  * refcounts have dropped to zero.
11333  */
11334 /* ARGSUSED */
11335 int
11336 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11337     ip_ioctl_cmd_t *ipip, void *ifreq)
11338 {
11339 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11340 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11341 	ipif_down_tail(ipif);
11342 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11343 }
11344 
11345 /* ARGSUSED */
11346 int
11347 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11348     ip_ioctl_cmd_t *ipip, void *if_req)
11349 {
11350 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11351 
11352 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11353 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11354 	/*
11355 	 * Get point to point destination address. The addresses can't
11356 	 * change since we hold a reference to the ipif.
11357 	 */
11358 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11359 		return (EADDRNOTAVAIL);
11360 
11361 	if (ipif->ipif_isv6) {
11362 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11363 		*sin6 = sin6_null;
11364 		sin6->sin6_family = AF_INET6;
11365 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11366 	} else {
11367 		*sin = sin_null;
11368 		sin->sin_family = AF_INET;
11369 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11370 	}
11371 	return (0);
11372 }
11373 
11374 /*
11375  * Set interface flags.  Many flags require special handling (e.g.,
11376  * bringing the interface down); see below for details.
11377  *
11378  * NOTE : We really don't enforce that ipif_id zero should be used
11379  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11380  *	  is because applications generally does SICGLIFFLAGS and
11381  *	  ORs in the new flags (that affects the logical) and does a
11382  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11383  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11384  *	  flags that will be turned on is correct with respect to
11385  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11386  */
11387 /* ARGSUSED */
11388 int
11389 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11390     ip_ioctl_cmd_t *ipip, void *if_req)
11391 {
11392 	uint64_t turn_on;
11393 	uint64_t turn_off;
11394 	int	err = 0;
11395 	phyint_t *phyi;
11396 	ill_t *ill;
11397 	uint64_t intf_flags, cantchange_flags;
11398 	boolean_t phyint_flags_modified = B_FALSE;
11399 	uint64_t flags;
11400 	struct ifreq *ifr;
11401 	struct lifreq *lifr;
11402 	boolean_t set_linklocal = B_FALSE;
11403 	boolean_t zero_source = B_FALSE;
11404 
11405 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11406 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11407 
11408 	ASSERT(IAM_WRITER_IPIF(ipif));
11409 
11410 	ill = ipif->ipif_ill;
11411 	phyi = ill->ill_phyint;
11412 
11413 	if (ipip->ipi_cmd_type == IF_CMD) {
11414 		ifr = (struct ifreq *)if_req;
11415 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11416 	} else {
11417 		lifr = (struct lifreq *)if_req;
11418 		flags = lifr->lifr_flags;
11419 	}
11420 
11421 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11422 
11423 	/*
11424 	 * Have the flags been set correctly until now?
11425 	 */
11426 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11427 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11428 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11429 	/*
11430 	 * Compare the new flags to the old, and partition
11431 	 * into those coming on and those going off.
11432 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11433 	 */
11434 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11435 		flags |= intf_flags & ~0xFFFF;
11436 
11437 	/*
11438 	 * Explicitly fail attempts to change flags that are always invalid on
11439 	 * an IPMP meta-interface.
11440 	 */
11441 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11442 		return (EINVAL);
11443 
11444 	/*
11445 	 * Check which flags will change; silently ignore flags which userland
11446 	 * is not allowed to control.  (Because these flags may change between
11447 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11448 	 * control, we need to silently ignore them rather than fail.)
11449 	 */
11450 	cantchange_flags = IFF_CANTCHANGE;
11451 	if (IS_IPMP(ill))
11452 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11453 
11454 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11455 	if (turn_on == 0)
11456 		return (0);	/* No change */
11457 
11458 	turn_off = intf_flags & turn_on;
11459 	turn_on ^= turn_off;
11460 
11461 	/*
11462 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11463 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11464 	 * allow it to be turned off.
11465 	 */
11466 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11467 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11468 		return (EINVAL);
11469 
11470 	if (turn_on & IFF_NOFAILOVER) {
11471 		turn_on |= IFF_DEPRECATED;
11472 		flags |= IFF_DEPRECATED;
11473 	}
11474 
11475 	/*
11476 	 * On underlying interfaces, only allow applications to manage test
11477 	 * addresses -- otherwise, they may get confused when the address
11478 	 * moves as part of being brought up.  Likewise, prevent an
11479 	 * application-managed test address from being converted to a data
11480 	 * address.  To prevent migration of administratively up addresses in
11481 	 * the kernel, we don't allow them to be converted either.
11482 	 */
11483 	if (IS_UNDER_IPMP(ill)) {
11484 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11485 
11486 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11487 			return (EINVAL);
11488 
11489 		if ((turn_off & IFF_NOFAILOVER) &&
11490 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11491 			return (EINVAL);
11492 	}
11493 
11494 	/*
11495 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11496 	 * IPv6 interfaces.
11497 	 */
11498 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11499 		return (EINVAL);
11500 
11501 	/*
11502 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11503 	 */
11504 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11505 		return (EINVAL);
11506 
11507 	/*
11508 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11509 	 * interfaces.  It makes no sense in that context.
11510 	 */
11511 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11512 		return (EINVAL);
11513 
11514 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11515 		zero_source = B_TRUE;
11516 
11517 	/*
11518 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11519 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11520 	 * If the link local address isn't set, and can be set, it will get
11521 	 * set later on in this function.
11522 	 */
11523 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11524 	    (flags & IFF_UP) && !zero_source &&
11525 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11526 		if (ipif_cant_setlinklocal(ipif))
11527 			return (EINVAL);
11528 		set_linklocal = B_TRUE;
11529 	}
11530 
11531 	/*
11532 	 * If we modify physical interface flags, we'll potentially need to
11533 	 * send up two routing socket messages for the changes (one for the
11534 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11535 	 */
11536 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11537 		phyint_flags_modified = B_TRUE;
11538 
11539 	/*
11540 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11541 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11542 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11543 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11544 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11545 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11546 	 * will not be honored.
11547 	 */
11548 	if (turn_on & PHYI_STANDBY) {
11549 		/*
11550 		 * No need to grab ill_g_usesrc_lock here; see the
11551 		 * synchronization notes in ip.c.
11552 		 */
11553 		if (ill->ill_usesrc_grp_next != NULL ||
11554 		    intf_flags & PHYI_INACTIVE)
11555 			return (EINVAL);
11556 		if (!(flags & PHYI_FAILED)) {
11557 			flags |= PHYI_INACTIVE;
11558 			turn_on |= PHYI_INACTIVE;
11559 		}
11560 	}
11561 
11562 	if (turn_off & PHYI_STANDBY) {
11563 		flags &= ~PHYI_INACTIVE;
11564 		turn_off |= PHYI_INACTIVE;
11565 	}
11566 
11567 	/*
11568 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11569 	 * would end up on.
11570 	 */
11571 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11572 	    (PHYI_FAILED | PHYI_INACTIVE))
11573 		return (EINVAL);
11574 
11575 	/*
11576 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11577 	 * status of the interface.
11578 	 */
11579 	if ((turn_on | turn_off) & ILLF_ROUTER)
11580 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11581 
11582 	/*
11583 	 * If the interface is not UP and we are not going to
11584 	 * bring it UP, record the flags and return. When the
11585 	 * interface comes UP later, the right actions will be
11586 	 * taken.
11587 	 */
11588 	if (!(ipif->ipif_flags & IPIF_UP) &&
11589 	    !(turn_on & IPIF_UP)) {
11590 		/* Record new flags in their respective places. */
11591 		mutex_enter(&ill->ill_lock);
11592 		mutex_enter(&ill->ill_phyint->phyint_lock);
11593 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11594 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11595 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11596 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11597 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11598 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11599 		mutex_exit(&ill->ill_lock);
11600 		mutex_exit(&ill->ill_phyint->phyint_lock);
11601 
11602 		/*
11603 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11604 		 * same to the kernel: if any of them has been set by
11605 		 * userland, the interface cannot be used for data traffic.
11606 		 */
11607 		if ((turn_on|turn_off) &
11608 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11609 			ASSERT(!IS_IPMP(ill));
11610 			/*
11611 			 * It's possible the ill is part of an "anonymous"
11612 			 * IPMP group rather than a real group.  In that case,
11613 			 * there are no other interfaces in the group and thus
11614 			 * no need to call ipmp_phyint_refresh_active().
11615 			 */
11616 			if (IS_UNDER_IPMP(ill))
11617 				ipmp_phyint_refresh_active(phyi);
11618 		}
11619 
11620 		if (phyint_flags_modified) {
11621 			if (phyi->phyint_illv4 != NULL) {
11622 				ip_rts_ifmsg(phyi->phyint_illv4->
11623 				    ill_ipif, RTSQ_DEFAULT);
11624 			}
11625 			if (phyi->phyint_illv6 != NULL) {
11626 				ip_rts_ifmsg(phyi->phyint_illv6->
11627 				    ill_ipif, RTSQ_DEFAULT);
11628 			}
11629 		}
11630 		return (0);
11631 	} else if (set_linklocal || zero_source) {
11632 		mutex_enter(&ill->ill_lock);
11633 		if (set_linklocal)
11634 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11635 		if (zero_source)
11636 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11637 		mutex_exit(&ill->ill_lock);
11638 	}
11639 
11640 	/*
11641 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11642 	 * or point-to-point interfaces with an unspecified destination. We do
11643 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11644 	 * have a subnet assigned, which is how in.ndpd currently manages its
11645 	 * onlink prefix list when no addresses are configured with those
11646 	 * prefixes.
11647 	 */
11648 	if (ipif->ipif_isv6 &&
11649 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11650 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11651 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11652 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11653 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11654 		return (EINVAL);
11655 	}
11656 
11657 	/*
11658 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11659 	 * from being brought up.
11660 	 */
11661 	if (!ipif->ipif_isv6 &&
11662 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11663 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11664 		return (EINVAL);
11665 	}
11666 
11667 	/*
11668 	 * The only flag changes that we currently take specific action on are
11669 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11670 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11671 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11672 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11673 	 * of bringing it back up will trigger the address to be moved.
11674 	 */
11675 	if ((turn_on|turn_off) &
11676 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11677 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11678 	    IPIF_NOFAILOVER)) {
11679 		/*
11680 		 * Taking this ipif down, make sure we have
11681 		 * valid net and subnet bcast ire's for other
11682 		 * logical interfaces, if we need them.
11683 		 */
11684 		if (!ipif->ipif_isv6)
11685 			ipif_check_bcast_ires(ipif);
11686 
11687 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11688 		    !(turn_off & IPIF_UP)) {
11689 			if (ipif->ipif_flags & IPIF_UP)
11690 				ill->ill_logical_down = 1;
11691 			turn_on &= ~IPIF_UP;
11692 		}
11693 		err = ipif_down(ipif, q, mp);
11694 		ip1dbg(("ipif_down returns %d err ", err));
11695 		if (err == EINPROGRESS)
11696 			return (err);
11697 		ipif_down_tail(ipif);
11698 	}
11699 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11700 }
11701 
11702 static int
11703 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11704 {
11705 	ill_t	*ill;
11706 	phyint_t *phyi;
11707 	uint64_t turn_on, turn_off;
11708 	uint64_t intf_flags, cantchange_flags;
11709 	boolean_t phyint_flags_modified = B_FALSE;
11710 	int	err = 0;
11711 	boolean_t set_linklocal = B_FALSE;
11712 	boolean_t zero_source = B_FALSE;
11713 
11714 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11715 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11716 
11717 	ASSERT(IAM_WRITER_IPIF(ipif));
11718 
11719 	ill = ipif->ipif_ill;
11720 	phyi = ill->ill_phyint;
11721 
11722 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11723 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11724 	if (IS_IPMP(ill))
11725 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11726 
11727 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11728 	turn_off = intf_flags & turn_on;
11729 	turn_on ^= turn_off;
11730 
11731 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11732 		phyint_flags_modified = B_TRUE;
11733 
11734 	/*
11735 	 * Now we change the flags. Track current value of
11736 	 * other flags in their respective places.
11737 	 */
11738 	mutex_enter(&ill->ill_lock);
11739 	mutex_enter(&phyi->phyint_lock);
11740 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11741 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11742 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11743 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11744 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11745 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11746 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11747 		set_linklocal = B_TRUE;
11748 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11749 	}
11750 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11751 		zero_source = B_TRUE;
11752 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11753 	}
11754 	mutex_exit(&ill->ill_lock);
11755 	mutex_exit(&phyi->phyint_lock);
11756 
11757 	if (set_linklocal)
11758 		(void) ipif_setlinklocal(ipif);
11759 
11760 	if (zero_source)
11761 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11762 	else
11763 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11764 
11765 	/*
11766 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11767 	 * the kernel: if any of them has been set by userland, the interface
11768 	 * cannot be used for data traffic.
11769 	 */
11770 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11771 		ASSERT(!IS_IPMP(ill));
11772 		/*
11773 		 * It's possible the ill is part of an "anonymous" IPMP group
11774 		 * rather than a real group.  In that case, there are no other
11775 		 * interfaces in the group and thus no need for us to call
11776 		 * ipmp_phyint_refresh_active().
11777 		 */
11778 		if (IS_UNDER_IPMP(ill))
11779 			ipmp_phyint_refresh_active(phyi);
11780 	}
11781 
11782 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11783 		/*
11784 		 * XXX ipif_up really does not know whether a phyint flags
11785 		 * was modified or not. So, it sends up information on
11786 		 * only one routing sockets message. As we don't bring up
11787 		 * the interface and also set PHYI_ flags simultaneously
11788 		 * it should be okay.
11789 		 */
11790 		err = ipif_up(ipif, q, mp);
11791 	} else {
11792 		/*
11793 		 * Make sure routing socket sees all changes to the flags.
11794 		 * ipif_up_done* handles this when we use ipif_up.
11795 		 */
11796 		if (phyint_flags_modified) {
11797 			if (phyi->phyint_illv4 != NULL) {
11798 				ip_rts_ifmsg(phyi->phyint_illv4->
11799 				    ill_ipif, RTSQ_DEFAULT);
11800 			}
11801 			if (phyi->phyint_illv6 != NULL) {
11802 				ip_rts_ifmsg(phyi->phyint_illv6->
11803 				    ill_ipif, RTSQ_DEFAULT);
11804 			}
11805 		} else {
11806 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11807 		}
11808 		/*
11809 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11810 		 * this in need_up case.
11811 		 */
11812 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11813 	}
11814 	return (err);
11815 }
11816 
11817 /*
11818  * Restart the flags operation now that the refcounts have dropped to zero.
11819  */
11820 /* ARGSUSED */
11821 int
11822 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11823     ip_ioctl_cmd_t *ipip, void *if_req)
11824 {
11825 	uint64_t flags;
11826 	struct ifreq *ifr = if_req;
11827 	struct lifreq *lifr = if_req;
11828 
11829 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11830 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11831 
11832 	ipif_down_tail(ipif);
11833 	if (ipip->ipi_cmd_type == IF_CMD) {
11834 		/* cast to uint16_t prevents unwanted sign extension */
11835 		flags = (uint16_t)ifr->ifr_flags;
11836 	} else {
11837 		flags = lifr->lifr_flags;
11838 	}
11839 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11840 }
11841 
11842 /*
11843  * Can operate on either a module or a driver queue.
11844  */
11845 /* ARGSUSED */
11846 int
11847 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11848     ip_ioctl_cmd_t *ipip, void *if_req)
11849 {
11850 	/*
11851 	 * Has the flags been set correctly till now ?
11852 	 */
11853 	ill_t *ill = ipif->ipif_ill;
11854 	phyint_t *phyi = ill->ill_phyint;
11855 
11856 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11857 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11858 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11859 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11860 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11861 
11862 	/*
11863 	 * Need a lock since some flags can be set even when there are
11864 	 * references to the ipif.
11865 	 */
11866 	mutex_enter(&ill->ill_lock);
11867 	if (ipip->ipi_cmd_type == IF_CMD) {
11868 		struct ifreq *ifr = (struct ifreq *)if_req;
11869 
11870 		/* Get interface flags (low 16 only). */
11871 		ifr->ifr_flags = ((ipif->ipif_flags |
11872 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11873 	} else {
11874 		struct lifreq *lifr = (struct lifreq *)if_req;
11875 
11876 		/* Get interface flags. */
11877 		lifr->lifr_flags = ipif->ipif_flags |
11878 		    ill->ill_flags | phyi->phyint_flags;
11879 	}
11880 	mutex_exit(&ill->ill_lock);
11881 	return (0);
11882 }
11883 
11884 /* ARGSUSED */
11885 int
11886 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11887     ip_ioctl_cmd_t *ipip, void *if_req)
11888 {
11889 	int mtu;
11890 	int ip_min_mtu;
11891 	struct ifreq	*ifr;
11892 	struct lifreq *lifr;
11893 	ire_t	*ire;
11894 	ip_stack_t *ipst;
11895 
11896 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11897 	    ipif->ipif_id, (void *)ipif));
11898 	if (ipip->ipi_cmd_type == IF_CMD) {
11899 		ifr = (struct ifreq *)if_req;
11900 		mtu = ifr->ifr_metric;
11901 	} else {
11902 		lifr = (struct lifreq *)if_req;
11903 		mtu = lifr->lifr_mtu;
11904 	}
11905 
11906 	if (ipif->ipif_isv6)
11907 		ip_min_mtu = IPV6_MIN_MTU;
11908 	else
11909 		ip_min_mtu = IP_MIN_MTU;
11910 
11911 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11912 		return (EINVAL);
11913 
11914 	/*
11915 	 * Change the MTU size in all relevant ire's.
11916 	 * Mtu change Vs. new ire creation - protocol below.
11917 	 * First change ipif_mtu and the ire_max_frag of the
11918 	 * interface ire. Then do an ire walk and change the
11919 	 * ire_max_frag of all affected ires. During ire_add
11920 	 * under the bucket lock, set the ire_max_frag of the
11921 	 * new ire being created from the ipif/ire from which
11922 	 * it is being derived. If an mtu change happens after
11923 	 * the ire is added, the new ire will be cleaned up.
11924 	 * Conversely if the mtu change happens before the ire
11925 	 * is added, ire_add will see the new value of the mtu.
11926 	 */
11927 	ipif->ipif_mtu = mtu;
11928 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11929 
11930 	if (ipif->ipif_isv6)
11931 		ire = ipif_to_ire_v6(ipif);
11932 	else
11933 		ire = ipif_to_ire(ipif);
11934 	if (ire != NULL) {
11935 		ire->ire_max_frag = ipif->ipif_mtu;
11936 		ire_refrele(ire);
11937 	}
11938 	ipst = ipif->ipif_ill->ill_ipst;
11939 	if (ipif->ipif_flags & IPIF_UP) {
11940 		if (ipif->ipif_isv6)
11941 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11942 			    ipst);
11943 		else
11944 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11945 			    ipst);
11946 	}
11947 	/* Update the MTU in SCTP's list */
11948 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11949 	return (0);
11950 }
11951 
11952 /* Get interface MTU. */
11953 /* ARGSUSED */
11954 int
11955 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11956 	ip_ioctl_cmd_t *ipip, void *if_req)
11957 {
11958 	struct ifreq	*ifr;
11959 	struct lifreq	*lifr;
11960 
11961 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11962 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11963 	if (ipip->ipi_cmd_type == IF_CMD) {
11964 		ifr = (struct ifreq *)if_req;
11965 		ifr->ifr_metric = ipif->ipif_mtu;
11966 	} else {
11967 		lifr = (struct lifreq *)if_req;
11968 		lifr->lifr_mtu = ipif->ipif_mtu;
11969 	}
11970 	return (0);
11971 }
11972 
11973 /* Set interface broadcast address. */
11974 /* ARGSUSED2 */
11975 int
11976 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11977 	ip_ioctl_cmd_t *ipip, void *if_req)
11978 {
11979 	ipaddr_t addr;
11980 	ire_t	*ire;
11981 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11982 
11983 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11984 	    ipif->ipif_id));
11985 
11986 	ASSERT(IAM_WRITER_IPIF(ipif));
11987 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11988 		return (EADDRNOTAVAIL);
11989 
11990 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11991 
11992 	if (sin->sin_family != AF_INET)
11993 		return (EAFNOSUPPORT);
11994 
11995 	addr = sin->sin_addr.s_addr;
11996 	if (ipif->ipif_flags & IPIF_UP) {
11997 		/*
11998 		 * If we are already up, make sure the new
11999 		 * broadcast address makes sense.  If it does,
12000 		 * there should be an IRE for it already.
12001 		 * Don't match on ipif, only on the ill
12002 		 * since we are sharing these now.
12003 		 */
12004 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12005 		    ipif, ALL_ZONES, NULL,
12006 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12007 		if (ire == NULL) {
12008 			return (EINVAL);
12009 		} else {
12010 			ire_refrele(ire);
12011 		}
12012 	}
12013 	/*
12014 	 * Changing the broadcast addr for this ipif.
12015 	 * Make sure we have valid net and subnet bcast
12016 	 * ire's for other logical interfaces, if needed.
12017 	 */
12018 	if (addr != ipif->ipif_brd_addr)
12019 		ipif_check_bcast_ires(ipif);
12020 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12021 	return (0);
12022 }
12023 
12024 /* Get interface broadcast address. */
12025 /* ARGSUSED */
12026 int
12027 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12028     ip_ioctl_cmd_t *ipip, void *if_req)
12029 {
12030 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12031 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12032 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12033 		return (EADDRNOTAVAIL);
12034 
12035 	/* IPIF_BROADCAST not possible with IPv6 */
12036 	ASSERT(!ipif->ipif_isv6);
12037 	*sin = sin_null;
12038 	sin->sin_family = AF_INET;
12039 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12040 	return (0);
12041 }
12042 
12043 /*
12044  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12045  */
12046 /* ARGSUSED */
12047 int
12048 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12049     ip_ioctl_cmd_t *ipip, void *if_req)
12050 {
12051 	int err = 0;
12052 	in6_addr_t v6mask;
12053 
12054 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12055 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12056 
12057 	ASSERT(IAM_WRITER_IPIF(ipif));
12058 
12059 	if (ipif->ipif_isv6) {
12060 		sin6_t *sin6;
12061 
12062 		if (sin->sin_family != AF_INET6)
12063 			return (EAFNOSUPPORT);
12064 
12065 		sin6 = (sin6_t *)sin;
12066 		v6mask = sin6->sin6_addr;
12067 	} else {
12068 		ipaddr_t mask;
12069 
12070 		if (sin->sin_family != AF_INET)
12071 			return (EAFNOSUPPORT);
12072 
12073 		mask = sin->sin_addr.s_addr;
12074 		V4MASK_TO_V6(mask, v6mask);
12075 	}
12076 
12077 	/*
12078 	 * No big deal if the interface isn't already up, or the mask
12079 	 * isn't really changing, or this is pt-pt.
12080 	 */
12081 	if (!(ipif->ipif_flags & IPIF_UP) ||
12082 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12083 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12084 		ipif->ipif_v6net_mask = v6mask;
12085 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12086 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12087 			    ipif->ipif_v6net_mask,
12088 			    ipif->ipif_v6subnet);
12089 		}
12090 		return (0);
12091 	}
12092 	/*
12093 	 * Make sure we have valid net and subnet broadcast ire's
12094 	 * for the old netmask, if needed by other logical interfaces.
12095 	 */
12096 	if (!ipif->ipif_isv6)
12097 		ipif_check_bcast_ires(ipif);
12098 
12099 	err = ipif_logical_down(ipif, q, mp);
12100 	if (err == EINPROGRESS)
12101 		return (err);
12102 	ipif_down_tail(ipif);
12103 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12104 	return (err);
12105 }
12106 
12107 static int
12108 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12109 {
12110 	in6_addr_t v6mask;
12111 	int err = 0;
12112 
12113 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12114 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12115 
12116 	if (ipif->ipif_isv6) {
12117 		sin6_t *sin6;
12118 
12119 		sin6 = (sin6_t *)sin;
12120 		v6mask = sin6->sin6_addr;
12121 	} else {
12122 		ipaddr_t mask;
12123 
12124 		mask = sin->sin_addr.s_addr;
12125 		V4MASK_TO_V6(mask, v6mask);
12126 	}
12127 
12128 	ipif->ipif_v6net_mask = v6mask;
12129 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12130 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12131 		    ipif->ipif_v6subnet);
12132 	}
12133 	err = ipif_up(ipif, q, mp);
12134 
12135 	if (err == 0 || err == EINPROGRESS) {
12136 		/*
12137 		 * The interface must be DL_BOUND if this packet has to
12138 		 * go out on the wire. Since we only go through a logical
12139 		 * down and are bound with the driver during an internal
12140 		 * down/up that is satisfied.
12141 		 */
12142 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12143 			/* Potentially broadcast an address mask reply. */
12144 			ipif_mask_reply(ipif);
12145 		}
12146 	}
12147 	return (err);
12148 }
12149 
12150 /* ARGSUSED */
12151 int
12152 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12153     ip_ioctl_cmd_t *ipip, void *if_req)
12154 {
12155 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12156 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12157 	ipif_down_tail(ipif);
12158 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12159 }
12160 
12161 /* Get interface net mask. */
12162 /* ARGSUSED */
12163 int
12164 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12165     ip_ioctl_cmd_t *ipip, void *if_req)
12166 {
12167 	struct lifreq *lifr = (struct lifreq *)if_req;
12168 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12169 
12170 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12171 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12172 
12173 	/*
12174 	 * net mask can't change since we have a reference to the ipif.
12175 	 */
12176 	if (ipif->ipif_isv6) {
12177 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12178 		*sin6 = sin6_null;
12179 		sin6->sin6_family = AF_INET6;
12180 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12181 		lifr->lifr_addrlen =
12182 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12183 	} else {
12184 		*sin = sin_null;
12185 		sin->sin_family = AF_INET;
12186 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12187 		if (ipip->ipi_cmd_type == LIF_CMD) {
12188 			lifr->lifr_addrlen =
12189 			    ip_mask_to_plen(ipif->ipif_net_mask);
12190 		}
12191 	}
12192 	return (0);
12193 }
12194 
12195 /* ARGSUSED */
12196 int
12197 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12198     ip_ioctl_cmd_t *ipip, void *if_req)
12199 {
12200 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12201 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12202 
12203 	/*
12204 	 * Since no applications should ever be setting metrics on underlying
12205 	 * interfaces, we explicitly fail to smoke 'em out.
12206 	 */
12207 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12208 		return (EINVAL);
12209 
12210 	/*
12211 	 * Set interface metric.  We don't use this for
12212 	 * anything but we keep track of it in case it is
12213 	 * important to routing applications or such.
12214 	 */
12215 	if (ipip->ipi_cmd_type == IF_CMD) {
12216 		struct ifreq    *ifr;
12217 
12218 		ifr = (struct ifreq *)if_req;
12219 		ipif->ipif_metric = ifr->ifr_metric;
12220 	} else {
12221 		struct lifreq   *lifr;
12222 
12223 		lifr = (struct lifreq *)if_req;
12224 		ipif->ipif_metric = lifr->lifr_metric;
12225 	}
12226 	return (0);
12227 }
12228 
12229 /* ARGSUSED */
12230 int
12231 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12232     ip_ioctl_cmd_t *ipip, void *if_req)
12233 {
12234 	/* Get interface metric. */
12235 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12236 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12237 
12238 	if (ipip->ipi_cmd_type == IF_CMD) {
12239 		struct ifreq    *ifr;
12240 
12241 		ifr = (struct ifreq *)if_req;
12242 		ifr->ifr_metric = ipif->ipif_metric;
12243 	} else {
12244 		struct lifreq   *lifr;
12245 
12246 		lifr = (struct lifreq *)if_req;
12247 		lifr->lifr_metric = ipif->ipif_metric;
12248 	}
12249 
12250 	return (0);
12251 }
12252 
12253 /* ARGSUSED */
12254 int
12255 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12256     ip_ioctl_cmd_t *ipip, void *if_req)
12257 {
12258 
12259 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12260 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12261 	/*
12262 	 * Set the muxid returned from I_PLINK.
12263 	 */
12264 	if (ipip->ipi_cmd_type == IF_CMD) {
12265 		struct ifreq *ifr = (struct ifreq *)if_req;
12266 
12267 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12268 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12269 	} else {
12270 		struct lifreq *lifr = (struct lifreq *)if_req;
12271 
12272 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12273 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12274 	}
12275 	return (0);
12276 }
12277 
12278 /* ARGSUSED */
12279 int
12280 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12281     ip_ioctl_cmd_t *ipip, void *if_req)
12282 {
12283 
12284 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12285 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12286 	/*
12287 	 * Get the muxid saved in ill for I_PUNLINK.
12288 	 */
12289 	if (ipip->ipi_cmd_type == IF_CMD) {
12290 		struct ifreq *ifr = (struct ifreq *)if_req;
12291 
12292 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12293 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12294 	} else {
12295 		struct lifreq *lifr = (struct lifreq *)if_req;
12296 
12297 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12298 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12299 	}
12300 	return (0);
12301 }
12302 
12303 /*
12304  * Set the subnet prefix. Does not modify the broadcast address.
12305  */
12306 /* ARGSUSED */
12307 int
12308 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12309     ip_ioctl_cmd_t *ipip, void *if_req)
12310 {
12311 	int err = 0;
12312 	in6_addr_t v6addr;
12313 	in6_addr_t v6mask;
12314 	boolean_t need_up = B_FALSE;
12315 	int addrlen;
12316 
12317 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12318 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12319 
12320 	ASSERT(IAM_WRITER_IPIF(ipif));
12321 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12322 
12323 	if (ipif->ipif_isv6) {
12324 		sin6_t *sin6;
12325 
12326 		if (sin->sin_family != AF_INET6)
12327 			return (EAFNOSUPPORT);
12328 
12329 		sin6 = (sin6_t *)sin;
12330 		v6addr = sin6->sin6_addr;
12331 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12332 			return (EADDRNOTAVAIL);
12333 	} else {
12334 		ipaddr_t addr;
12335 
12336 		if (sin->sin_family != AF_INET)
12337 			return (EAFNOSUPPORT);
12338 
12339 		addr = sin->sin_addr.s_addr;
12340 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12341 			return (EADDRNOTAVAIL);
12342 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12343 		/* Add 96 bits */
12344 		addrlen += IPV6_ABITS - IP_ABITS;
12345 	}
12346 
12347 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12348 		return (EINVAL);
12349 
12350 	/* Check if bits in the address is set past the mask */
12351 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12352 		return (EINVAL);
12353 
12354 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12355 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12356 		return (0);	/* No change */
12357 
12358 	if (ipif->ipif_flags & IPIF_UP) {
12359 		/*
12360 		 * If the interface is already marked up,
12361 		 * we call ipif_down which will take care
12362 		 * of ditching any IREs that have been set
12363 		 * up based on the old interface address.
12364 		 */
12365 		err = ipif_logical_down(ipif, q, mp);
12366 		if (err == EINPROGRESS)
12367 			return (err);
12368 		ipif_down_tail(ipif);
12369 		need_up = B_TRUE;
12370 	}
12371 
12372 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12373 	return (err);
12374 }
12375 
12376 static int
12377 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12378     queue_t *q, mblk_t *mp, boolean_t need_up)
12379 {
12380 	ill_t	*ill = ipif->ipif_ill;
12381 	int	err = 0;
12382 
12383 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12384 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12385 
12386 	/* Set the new address. */
12387 	mutex_enter(&ill->ill_lock);
12388 	ipif->ipif_v6net_mask = v6mask;
12389 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12390 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12391 		    ipif->ipif_v6subnet);
12392 	}
12393 	mutex_exit(&ill->ill_lock);
12394 
12395 	if (need_up) {
12396 		/*
12397 		 * Now bring the interface back up.  If this
12398 		 * is the only IPIF for the ILL, ipif_up
12399 		 * will have to re-bind to the device, so
12400 		 * we may get back EINPROGRESS, in which
12401 		 * case, this IOCTL will get completed in
12402 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12403 		 */
12404 		err = ipif_up(ipif, q, mp);
12405 		if (err == EINPROGRESS)
12406 			return (err);
12407 	}
12408 	return (err);
12409 }
12410 
12411 /* ARGSUSED */
12412 int
12413 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12414     ip_ioctl_cmd_t *ipip, void *if_req)
12415 {
12416 	int	addrlen;
12417 	in6_addr_t v6addr;
12418 	in6_addr_t v6mask;
12419 	struct lifreq *lifr = (struct lifreq *)if_req;
12420 
12421 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12422 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12423 	ipif_down_tail(ipif);
12424 
12425 	addrlen = lifr->lifr_addrlen;
12426 	if (ipif->ipif_isv6) {
12427 		sin6_t *sin6;
12428 
12429 		sin6 = (sin6_t *)sin;
12430 		v6addr = sin6->sin6_addr;
12431 	} else {
12432 		ipaddr_t addr;
12433 
12434 		addr = sin->sin_addr.s_addr;
12435 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12436 		addrlen += IPV6_ABITS - IP_ABITS;
12437 	}
12438 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12439 
12440 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12441 }
12442 
12443 /* ARGSUSED */
12444 int
12445 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12446     ip_ioctl_cmd_t *ipip, void *if_req)
12447 {
12448 	struct lifreq *lifr = (struct lifreq *)if_req;
12449 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12450 
12451 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12452 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12453 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12454 
12455 	if (ipif->ipif_isv6) {
12456 		*sin6 = sin6_null;
12457 		sin6->sin6_family = AF_INET6;
12458 		sin6->sin6_addr = ipif->ipif_v6subnet;
12459 		lifr->lifr_addrlen =
12460 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12461 	} else {
12462 		*sin = sin_null;
12463 		sin->sin_family = AF_INET;
12464 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12465 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12466 	}
12467 	return (0);
12468 }
12469 
12470 /*
12471  * Set the IPv6 address token.
12472  */
12473 /* ARGSUSED */
12474 int
12475 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12476     ip_ioctl_cmd_t *ipi, void *if_req)
12477 {
12478 	ill_t *ill = ipif->ipif_ill;
12479 	int err;
12480 	in6_addr_t v6addr;
12481 	in6_addr_t v6mask;
12482 	boolean_t need_up = B_FALSE;
12483 	int i;
12484 	sin6_t *sin6 = (sin6_t *)sin;
12485 	struct lifreq *lifr = (struct lifreq *)if_req;
12486 	int addrlen;
12487 
12488 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12489 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12490 	ASSERT(IAM_WRITER_IPIF(ipif));
12491 
12492 	addrlen = lifr->lifr_addrlen;
12493 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12494 	if (ipif->ipif_id != 0)
12495 		return (EINVAL);
12496 
12497 	if (!ipif->ipif_isv6)
12498 		return (EINVAL);
12499 
12500 	if (addrlen > IPV6_ABITS)
12501 		return (EINVAL);
12502 
12503 	v6addr = sin6->sin6_addr;
12504 
12505 	/*
12506 	 * The length of the token is the length from the end.  To get
12507 	 * the proper mask for this, compute the mask of the bits not
12508 	 * in the token; ie. the prefix, and then xor to get the mask.
12509 	 */
12510 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12511 		return (EINVAL);
12512 	for (i = 0; i < 4; i++) {
12513 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12514 	}
12515 
12516 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12517 	    ill->ill_token_length == addrlen)
12518 		return (0);	/* No change */
12519 
12520 	if (ipif->ipif_flags & IPIF_UP) {
12521 		err = ipif_logical_down(ipif, q, mp);
12522 		if (err == EINPROGRESS)
12523 			return (err);
12524 		ipif_down_tail(ipif);
12525 		need_up = B_TRUE;
12526 	}
12527 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12528 	return (err);
12529 }
12530 
12531 static int
12532 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12533     mblk_t *mp, boolean_t need_up)
12534 {
12535 	in6_addr_t v6addr;
12536 	in6_addr_t v6mask;
12537 	ill_t	*ill = ipif->ipif_ill;
12538 	int	i;
12539 	int	err = 0;
12540 
12541 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12542 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12543 	v6addr = sin6->sin6_addr;
12544 	/*
12545 	 * The length of the token is the length from the end.  To get
12546 	 * the proper mask for this, compute the mask of the bits not
12547 	 * in the token; ie. the prefix, and then xor to get the mask.
12548 	 */
12549 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12550 	for (i = 0; i < 4; i++)
12551 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12552 
12553 	mutex_enter(&ill->ill_lock);
12554 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12555 	ill->ill_token_length = addrlen;
12556 	mutex_exit(&ill->ill_lock);
12557 
12558 	if (need_up) {
12559 		/*
12560 		 * Now bring the interface back up.  If this
12561 		 * is the only IPIF for the ILL, ipif_up
12562 		 * will have to re-bind to the device, so
12563 		 * we may get back EINPROGRESS, in which
12564 		 * case, this IOCTL will get completed in
12565 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12566 		 */
12567 		err = ipif_up(ipif, q, mp);
12568 		if (err == EINPROGRESS)
12569 			return (err);
12570 	}
12571 	return (err);
12572 }
12573 
12574 /* ARGSUSED */
12575 int
12576 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12577     ip_ioctl_cmd_t *ipi, void *if_req)
12578 {
12579 	ill_t *ill;
12580 	sin6_t *sin6 = (sin6_t *)sin;
12581 	struct lifreq *lifr = (struct lifreq *)if_req;
12582 
12583 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12584 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12585 	if (ipif->ipif_id != 0)
12586 		return (EINVAL);
12587 
12588 	ill = ipif->ipif_ill;
12589 	if (!ill->ill_isv6)
12590 		return (ENXIO);
12591 
12592 	*sin6 = sin6_null;
12593 	sin6->sin6_family = AF_INET6;
12594 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12595 	sin6->sin6_addr = ill->ill_token;
12596 	lifr->lifr_addrlen = ill->ill_token_length;
12597 	return (0);
12598 }
12599 
12600 /*
12601  * Set (hardware) link specific information that might override
12602  * what was acquired through the DL_INFO_ACK.
12603  * The logic is as follows.
12604  *
12605  * become exclusive
12606  * set CHANGING flag
12607  * change mtu on affected IREs
12608  * clear CHANGING flag
12609  *
12610  * An ire add that occurs before the CHANGING flag is set will have its mtu
12611  * changed by the ip_sioctl_lnkinfo.
12612  *
12613  * During the time the CHANGING flag is set, no new ires will be added to the
12614  * bucket, and ire add will fail (due the CHANGING flag).
12615  *
12616  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12617  * before it is added to the bucket.
12618  *
12619  * Obviously only 1 thread can set the CHANGING flag and we need to become
12620  * exclusive to set the flag.
12621  */
12622 /* ARGSUSED */
12623 int
12624 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12625     ip_ioctl_cmd_t *ipi, void *if_req)
12626 {
12627 	ill_t		*ill = ipif->ipif_ill;
12628 	ipif_t		*nipif;
12629 	int		ip_min_mtu;
12630 	boolean_t	mtu_walk = B_FALSE;
12631 	struct lifreq	*lifr = (struct lifreq *)if_req;
12632 	lif_ifinfo_req_t *lir;
12633 	ire_t		*ire;
12634 
12635 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12636 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12637 	lir = &lifr->lifr_ifinfo;
12638 	ASSERT(IAM_WRITER_IPIF(ipif));
12639 
12640 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12641 	if (ipif->ipif_id != 0)
12642 		return (EINVAL);
12643 
12644 	/* Set interface MTU. */
12645 	if (ipif->ipif_isv6)
12646 		ip_min_mtu = IPV6_MIN_MTU;
12647 	else
12648 		ip_min_mtu = IP_MIN_MTU;
12649 
12650 	/*
12651 	 * Verify values before we set anything. Allow zero to
12652 	 * mean unspecified.
12653 	 */
12654 	if (lir->lir_maxmtu != 0 &&
12655 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12656 	    lir->lir_maxmtu < ip_min_mtu))
12657 		return (EINVAL);
12658 	if (lir->lir_reachtime != 0 &&
12659 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12660 		return (EINVAL);
12661 	if (lir->lir_reachretrans != 0 &&
12662 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12663 		return (EINVAL);
12664 
12665 	mutex_enter(&ill->ill_lock);
12666 	ill->ill_state_flags |= ILL_CHANGING;
12667 	for (nipif = ill->ill_ipif; nipif != NULL;
12668 	    nipif = nipif->ipif_next) {
12669 		nipif->ipif_state_flags |= IPIF_CHANGING;
12670 	}
12671 
12672 	if (lir->lir_maxmtu != 0) {
12673 		ill->ill_max_mtu = lir->lir_maxmtu;
12674 		ill->ill_user_mtu = lir->lir_maxmtu;
12675 		mtu_walk = B_TRUE;
12676 	}
12677 	mutex_exit(&ill->ill_lock);
12678 
12679 	if (lir->lir_reachtime != 0)
12680 		ill->ill_reachable_time = lir->lir_reachtime;
12681 
12682 	if (lir->lir_reachretrans != 0)
12683 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12684 
12685 	ill->ill_max_hops = lir->lir_maxhops;
12686 
12687 	ill->ill_max_buf = ND_MAX_Q;
12688 
12689 	if (mtu_walk) {
12690 		/*
12691 		 * Set the MTU on all ipifs associated with this ill except
12692 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12693 		 */
12694 		for (nipif = ill->ill_ipif; nipif != NULL;
12695 		    nipif = nipif->ipif_next) {
12696 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12697 				continue;
12698 
12699 			nipif->ipif_mtu = ill->ill_max_mtu;
12700 
12701 			if (!(nipif->ipif_flags & IPIF_UP))
12702 				continue;
12703 
12704 			if (nipif->ipif_isv6)
12705 				ire = ipif_to_ire_v6(nipif);
12706 			else
12707 				ire = ipif_to_ire(nipif);
12708 			if (ire != NULL) {
12709 				ire->ire_max_frag = ipif->ipif_mtu;
12710 				ire_refrele(ire);
12711 			}
12712 
12713 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12714 			    nipif, ill);
12715 		}
12716 	}
12717 
12718 	mutex_enter(&ill->ill_lock);
12719 	for (nipif = ill->ill_ipif; nipif != NULL;
12720 	    nipif = nipif->ipif_next) {
12721 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12722 	}
12723 	ILL_UNMARK_CHANGING(ill);
12724 	mutex_exit(&ill->ill_lock);
12725 
12726 	/*
12727 	 * Refresh IPMP meta-interface MTU if necessary.
12728 	 */
12729 	if (IS_UNDER_IPMP(ill))
12730 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12731 
12732 	return (0);
12733 }
12734 
12735 /* ARGSUSED */
12736 int
12737 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12738     ip_ioctl_cmd_t *ipi, void *if_req)
12739 {
12740 	struct lif_ifinfo_req *lir;
12741 	ill_t *ill = ipif->ipif_ill;
12742 
12743 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12744 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12745 	if (ipif->ipif_id != 0)
12746 		return (EINVAL);
12747 
12748 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12749 	lir->lir_maxhops = ill->ill_max_hops;
12750 	lir->lir_reachtime = ill->ill_reachable_time;
12751 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12752 	lir->lir_maxmtu = ill->ill_max_mtu;
12753 
12754 	return (0);
12755 }
12756 
12757 /*
12758  * Return best guess as to the subnet mask for the specified address.
12759  * Based on the subnet masks for all the configured interfaces.
12760  *
12761  * We end up returning a zero mask in the case of default, multicast or
12762  * experimental.
12763  */
12764 static ipaddr_t
12765 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12766 {
12767 	ipaddr_t net_mask;
12768 	ill_t	*ill;
12769 	ipif_t	*ipif;
12770 	ill_walk_context_t ctx;
12771 	ipif_t	*fallback_ipif = NULL;
12772 
12773 	net_mask = ip_net_mask(addr);
12774 	if (net_mask == 0) {
12775 		*ipifp = NULL;
12776 		return (0);
12777 	}
12778 
12779 	/* Let's check to see if this is maybe a local subnet route. */
12780 	/* this function only applies to IPv4 interfaces */
12781 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12782 	ill = ILL_START_WALK_V4(&ctx, ipst);
12783 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12784 		mutex_enter(&ill->ill_lock);
12785 		for (ipif = ill->ill_ipif; ipif != NULL;
12786 		    ipif = ipif->ipif_next) {
12787 			if (!IPIF_CAN_LOOKUP(ipif))
12788 				continue;
12789 			if (!(ipif->ipif_flags & IPIF_UP))
12790 				continue;
12791 			if ((ipif->ipif_subnet & net_mask) ==
12792 			    (addr & net_mask)) {
12793 				/*
12794 				 * Don't trust pt-pt interfaces if there are
12795 				 * other interfaces.
12796 				 */
12797 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12798 					if (fallback_ipif == NULL) {
12799 						ipif_refhold_locked(ipif);
12800 						fallback_ipif = ipif;
12801 					}
12802 					continue;
12803 				}
12804 
12805 				/*
12806 				 * Fine. Just assume the same net mask as the
12807 				 * directly attached subnet interface is using.
12808 				 */
12809 				ipif_refhold_locked(ipif);
12810 				mutex_exit(&ill->ill_lock);
12811 				rw_exit(&ipst->ips_ill_g_lock);
12812 				if (fallback_ipif != NULL)
12813 					ipif_refrele(fallback_ipif);
12814 				*ipifp = ipif;
12815 				return (ipif->ipif_net_mask);
12816 			}
12817 		}
12818 		mutex_exit(&ill->ill_lock);
12819 	}
12820 	rw_exit(&ipst->ips_ill_g_lock);
12821 
12822 	*ipifp = fallback_ipif;
12823 	return ((fallback_ipif != NULL) ?
12824 	    fallback_ipif->ipif_net_mask : net_mask);
12825 }
12826 
12827 /*
12828  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12829  */
12830 static void
12831 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12832 {
12833 	IOCP	iocp;
12834 	ipft_t	*ipft;
12835 	ipllc_t	*ipllc;
12836 	mblk_t	*mp1;
12837 	cred_t	*cr;
12838 	int	error = 0;
12839 	conn_t	*connp;
12840 
12841 	ip1dbg(("ip_wput_ioctl"));
12842 	iocp = (IOCP)mp->b_rptr;
12843 	mp1 = mp->b_cont;
12844 	if (mp1 == NULL) {
12845 		iocp->ioc_error = EINVAL;
12846 		mp->b_datap->db_type = M_IOCNAK;
12847 		iocp->ioc_count = 0;
12848 		qreply(q, mp);
12849 		return;
12850 	}
12851 
12852 	/*
12853 	 * These IOCTLs provide various control capabilities to
12854 	 * upstream agents such as ULPs and processes.	There
12855 	 * are currently two such IOCTLs implemented.  They
12856 	 * are used by TCP to provide update information for
12857 	 * existing IREs and to forcibly delete an IRE for a
12858 	 * host that is not responding, thereby forcing an
12859 	 * attempt at a new route.
12860 	 */
12861 	iocp->ioc_error = EINVAL;
12862 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12863 		goto done;
12864 
12865 	ipllc = (ipllc_t *)mp1->b_rptr;
12866 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12867 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12868 			break;
12869 	}
12870 	/*
12871 	 * prefer credential from mblk over ioctl;
12872 	 * see ip_sioctl_copyin_setup
12873 	 */
12874 	cr = msg_getcred(mp, NULL);
12875 	if (cr == NULL)
12876 		cr = iocp->ioc_cr;
12877 
12878 	/*
12879 	 * Refhold the conn in case the request gets queued up in some lookup
12880 	 */
12881 	ASSERT(CONN_Q(q));
12882 	connp = Q_TO_CONN(q);
12883 	CONN_INC_REF(connp);
12884 	if (ipft->ipft_pfi &&
12885 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12886 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12887 		error = (*ipft->ipft_pfi)(q,
12888 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12889 	}
12890 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12891 		/*
12892 		 * CONN_OPER_PENDING_DONE happens in the function called
12893 		 * through ipft_pfi above.
12894 		 */
12895 		return;
12896 	}
12897 
12898 	CONN_OPER_PENDING_DONE(connp);
12899 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12900 		freemsg(mp);
12901 		return;
12902 	}
12903 	iocp->ioc_error = error;
12904 
12905 done:
12906 	mp->b_datap->db_type = M_IOCACK;
12907 	if (iocp->ioc_error)
12908 		iocp->ioc_count = 0;
12909 	qreply(q, mp);
12910 }
12911 
12912 /*
12913  * Lookup an ipif using the sequence id (ipif_seqid)
12914  */
12915 ipif_t *
12916 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12917 {
12918 	ipif_t *ipif;
12919 
12920 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12921 
12922 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12923 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12924 			return (ipif);
12925 	}
12926 	return (NULL);
12927 }
12928 
12929 /*
12930  * Assign a unique id for the ipif. This is used later when we send
12931  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12932  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12933  * IRE is added, we verify that ipif has not disappeared.
12934  */
12935 
12936 static void
12937 ipif_assign_seqid(ipif_t *ipif)
12938 {
12939 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12940 
12941 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
12942 }
12943 
12944 /*
12945  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
12946  * administratively down (i.e., no DAD), of the same type, and locked.  Note
12947  * that the clone is complete -- including the seqid -- and the expectation is
12948  * that the caller will either free or overwrite `sipif' before it's unlocked.
12949  */
12950 static void
12951 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
12952 {
12953 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
12954 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
12955 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12956 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12957 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
12958 	ASSERT(sipif->ipif_arp_del_mp == NULL);
12959 	ASSERT(dipif->ipif_arp_del_mp == NULL);
12960 	ASSERT(sipif->ipif_igmp_rpt == NULL);
12961 	ASSERT(dipif->ipif_igmp_rpt == NULL);
12962 	ASSERT(sipif->ipif_multicast_up == 0);
12963 	ASSERT(dipif->ipif_multicast_up == 0);
12964 	ASSERT(sipif->ipif_joined_allhosts == 0);
12965 	ASSERT(dipif->ipif_joined_allhosts == 0);
12966 
12967 	dipif->ipif_mtu = sipif->ipif_mtu;
12968 	dipif->ipif_flags = sipif->ipif_flags;
12969 	dipif->ipif_metric = sipif->ipif_metric;
12970 	dipif->ipif_zoneid = sipif->ipif_zoneid;
12971 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
12972 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
12973 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
12974 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
12975 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
12976 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
12977 
12978 	/*
12979 	 * While dipif is down right now, it might've been up before.  Since
12980 	 * it's changing identity, its packet counters need to be reset.
12981 	 */
12982 	dipif->ipif_ib_pkt_count = 0;
12983 	dipif->ipif_ob_pkt_count = 0;
12984 	dipif->ipif_fo_pkt_count = 0;
12985 
12986 	/*
12987 	 * As per the comment atop the function, we assume that these sipif
12988 	 * fields will be changed before sipif is unlocked.
12989 	 */
12990 	dipif->ipif_seqid = sipif->ipif_seqid;
12991 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
12992 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
12993 	dipif->ipif_state_flags = sipif->ipif_state_flags;
12994 }
12995 
12996 /*
12997  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
12998  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
12999  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
13000  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
13001  * down (i.e., no DAD), of the same type, and unlocked.
13002  */
13003 static void
13004 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
13005 {
13006 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
13007 	ipxop_t *ipx = ipsq->ipsq_xop;
13008 
13009 	ASSERT(sipif != dipif);
13010 	ASSERT(sipif != virgipif);
13011 
13012 	/*
13013 	 * Grab all of the locks that protect the ipif in a defined order.
13014 	 */
13015 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13016 	if (sipif > dipif) {
13017 		mutex_enter(&sipif->ipif_saved_ire_lock);
13018 		mutex_enter(&dipif->ipif_saved_ire_lock);
13019 	} else {
13020 		mutex_enter(&dipif->ipif_saved_ire_lock);
13021 		mutex_enter(&sipif->ipif_saved_ire_lock);
13022 	}
13023 
13024 	ipif_clone(sipif, dipif);
13025 	if (virgipif != NULL) {
13026 		ipif_clone(virgipif, sipif);
13027 		mi_free(virgipif);
13028 	}
13029 
13030 	mutex_exit(&sipif->ipif_saved_ire_lock);
13031 	mutex_exit(&dipif->ipif_saved_ire_lock);
13032 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13033 
13034 	/*
13035 	 * Transfer ownership of the current xop, if necessary.
13036 	 */
13037 	if (ipx->ipx_current_ipif == sipif) {
13038 		ASSERT(ipx->ipx_pending_ipif == NULL);
13039 		mutex_enter(&ipx->ipx_lock);
13040 		ipx->ipx_current_ipif = dipif;
13041 		mutex_exit(&ipx->ipx_lock);
13042 	}
13043 
13044 	if (virgipif == NULL)
13045 		mi_free(sipif);
13046 }
13047 
13048 /*
13049  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13050  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13051  * be inserted into the first space available in the list. The value of
13052  * ipif_id will then be set to the appropriate value for its position.
13053  */
13054 static int
13055 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13056 {
13057 	ill_t *ill;
13058 	ipif_t *tipif;
13059 	ipif_t **tipifp;
13060 	int id;
13061 	ip_stack_t	*ipst;
13062 
13063 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13064 	    IAM_WRITER_IPIF(ipif));
13065 
13066 	ill = ipif->ipif_ill;
13067 	ASSERT(ill != NULL);
13068 	ipst = ill->ill_ipst;
13069 
13070 	/*
13071 	 * In the case of lo0:0 we already hold the ill_g_lock.
13072 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13073 	 * ipif_insert.
13074 	 */
13075 	if (acquire_g_lock)
13076 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13077 	mutex_enter(&ill->ill_lock);
13078 	id = ipif->ipif_id;
13079 	tipifp = &(ill->ill_ipif);
13080 	if (id == -1) {	/* need to find a real id */
13081 		id = 0;
13082 		while ((tipif = *tipifp) != NULL) {
13083 			ASSERT(tipif->ipif_id >= id);
13084 			if (tipif->ipif_id != id)
13085 				break; /* non-consecutive id */
13086 			id++;
13087 			tipifp = &(tipif->ipif_next);
13088 		}
13089 		/* limit number of logical interfaces */
13090 		if (id >= ipst->ips_ip_addrs_per_if) {
13091 			mutex_exit(&ill->ill_lock);
13092 			if (acquire_g_lock)
13093 				rw_exit(&ipst->ips_ill_g_lock);
13094 			return (-1);
13095 		}
13096 		ipif->ipif_id = id; /* assign new id */
13097 	} else if (id < ipst->ips_ip_addrs_per_if) {
13098 		/* we have a real id; insert ipif in the right place */
13099 		while ((tipif = *tipifp) != NULL) {
13100 			ASSERT(tipif->ipif_id != id);
13101 			if (tipif->ipif_id > id)
13102 				break; /* found correct location */
13103 			tipifp = &(tipif->ipif_next);
13104 		}
13105 	} else {
13106 		mutex_exit(&ill->ill_lock);
13107 		if (acquire_g_lock)
13108 			rw_exit(&ipst->ips_ill_g_lock);
13109 		return (-1);
13110 	}
13111 
13112 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13113 
13114 	ipif->ipif_next = tipif;
13115 	*tipifp = ipif;
13116 	mutex_exit(&ill->ill_lock);
13117 	if (acquire_g_lock)
13118 		rw_exit(&ipst->ips_ill_g_lock);
13119 
13120 	return (0);
13121 }
13122 
13123 static void
13124 ipif_remove(ipif_t *ipif)
13125 {
13126 	ipif_t	**ipifp;
13127 	ill_t	*ill = ipif->ipif_ill;
13128 
13129 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13130 
13131 	mutex_enter(&ill->ill_lock);
13132 	ipifp = &ill->ill_ipif;
13133 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13134 		if (*ipifp == ipif) {
13135 			*ipifp = ipif->ipif_next;
13136 			break;
13137 		}
13138 	}
13139 	mutex_exit(&ill->ill_lock);
13140 }
13141 
13142 /*
13143  * Allocate and initialize a new interface control structure.  (Always
13144  * called as writer.)
13145  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13146  * is not part of the global linked list of ills. ipif_seqid is unique
13147  * in the system and to preserve the uniqueness, it is assigned only
13148  * when ill becomes part of the global list. At that point ill will
13149  * have a name. If it doesn't get assigned here, it will get assigned
13150  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13151  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13152  * the interface flags or any other information from the DL_INFO_ACK for
13153  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13154  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13155  * second DL_INFO_ACK comes in from the driver.
13156  */
13157 static ipif_t *
13158 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13159     boolean_t insert)
13160 {
13161 	ipif_t	*ipif;
13162 	ip_stack_t *ipst = ill->ill_ipst;
13163 
13164 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13165 	    ill->ill_name, id, (void *)ill));
13166 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13167 
13168 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13169 		return (NULL);
13170 	*ipif = ipif_zero;	/* start clean */
13171 
13172 	ipif->ipif_ill = ill;
13173 	ipif->ipif_id = id;	/* could be -1 */
13174 	/*
13175 	 * Inherit the zoneid from the ill; for the shared stack instance
13176 	 * this is always the global zone
13177 	 */
13178 	ipif->ipif_zoneid = ill->ill_zoneid;
13179 
13180 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13181 
13182 	ipif->ipif_refcnt = 0;
13183 	ipif->ipif_saved_ire_cnt = 0;
13184 
13185 	if (insert) {
13186 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13187 			mi_free(ipif);
13188 			return (NULL);
13189 		}
13190 		/* -1 id should have been replaced by real id */
13191 		id = ipif->ipif_id;
13192 		ASSERT(id >= 0);
13193 	}
13194 
13195 	if (ill->ill_name[0] != '\0')
13196 		ipif_assign_seqid(ipif);
13197 
13198 	/*
13199 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
13200 	 * (which must not exist yet because the zeroth ipif is created once
13201 	 * per ill).  However, do not not link it to the ipmp_grp_t until
13202 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
13203 	 */
13204 	if (id == 0 && IS_IPMP(ill)) {
13205 		if (ipmp_illgrp_create(ill) == NULL) {
13206 			if (insert) {
13207 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13208 				ipif_remove(ipif);
13209 				rw_exit(&ipst->ips_ill_g_lock);
13210 			}
13211 			mi_free(ipif);
13212 			return (NULL);
13213 		}
13214 	}
13215 
13216 	/*
13217 	 * We grab ill_lock to protect the flag changes.  The ipif is still
13218 	 * not up and can't be looked up until the ioctl completes and the
13219 	 * IPIF_CHANGING flag is cleared.
13220 	 */
13221 	mutex_enter(&ill->ill_lock);
13222 
13223 	ipif->ipif_ire_type = ire_type;
13224 
13225 	if (ipif->ipif_isv6) {
13226 		ill->ill_flags |= ILLF_IPV6;
13227 	} else {
13228 		ipaddr_t inaddr_any = INADDR_ANY;
13229 
13230 		ill->ill_flags |= ILLF_IPV4;
13231 
13232 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13233 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13234 		    &ipif->ipif_v6lcl_addr);
13235 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13236 		    &ipif->ipif_v6src_addr);
13237 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13238 		    &ipif->ipif_v6subnet);
13239 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13240 		    &ipif->ipif_v6net_mask);
13241 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13242 		    &ipif->ipif_v6brd_addr);
13243 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13244 		    &ipif->ipif_v6pp_dst_addr);
13245 	}
13246 
13247 	/*
13248 	 * Don't set the interface flags etc. now, will do it in
13249 	 * ip_ll_subnet_defaults.
13250 	 */
13251 	if (!initialize)
13252 		goto out;
13253 
13254 	ipif->ipif_mtu = ill->ill_max_mtu;
13255 
13256 	/*
13257 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13258 	 * with no underlying interfaces (and thus an unknown broadcast
13259 	 * address length), but all interfaces that can be placed into an IPMP
13260 	 * group are required to be broadcast-capable.
13261 	 */
13262 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13263 		/*
13264 		 * Later detect lack of DLPI driver multicast capability by
13265 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
13266 		 */
13267 		ill->ill_flags |= ILLF_MULTICAST;
13268 		if (!ipif->ipif_isv6)
13269 			ipif->ipif_flags |= IPIF_BROADCAST;
13270 	} else {
13271 		if (ill->ill_net_type != IRE_LOOPBACK) {
13272 			if (ipif->ipif_isv6)
13273 				/*
13274 				 * Note: xresolv interfaces will eventually need
13275 				 * NOARP set here as well, but that will require
13276 				 * those external resolvers to have some
13277 				 * knowledge of that flag and act appropriately.
13278 				 * Not to be changed at present.
13279 				 */
13280 				ill->ill_flags |= ILLF_NONUD;
13281 			else
13282 				ill->ill_flags |= ILLF_NOARP;
13283 		}
13284 		if (ill->ill_phys_addr_length == 0) {
13285 			if (IS_VNI(ill)) {
13286 				ipif->ipif_flags |= IPIF_NOXMIT;
13287 			} else {
13288 				/* pt-pt supports multicast. */
13289 				ill->ill_flags |= ILLF_MULTICAST;
13290 				if (ill->ill_net_type != IRE_LOOPBACK)
13291 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13292 			}
13293 		}
13294 	}
13295 out:
13296 	mutex_exit(&ill->ill_lock);
13297 	return (ipif);
13298 }
13299 
13300 /*
13301  * If appropriate, send a message up to the resolver delete the entry
13302  * for the address of this interface which is going out of business.
13303  * (Always called as writer).
13304  *
13305  * NOTE : We need to check for NULL mps as some of the fields are
13306  *	  initialized only for some interface types. See ipif_resolver_up()
13307  *	  for details.
13308  */
13309 void
13310 ipif_resolver_down(ipif_t *ipif)
13311 {
13312 	mblk_t	*mp;
13313 	ill_t	*ill = ipif->ipif_ill;
13314 
13315 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13316 	ASSERT(IAM_WRITER_IPIF(ipif));
13317 
13318 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13319 		return;
13320 
13321 	/* Delete the mapping for the local address */
13322 	mp = ipif->ipif_arp_del_mp;
13323 	if (mp != NULL) {
13324 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13325 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13326 		putnext(ill->ill_rq, mp);
13327 		ipif->ipif_arp_del_mp = NULL;
13328 	}
13329 
13330 	/*
13331 	 * Make IPMP aware of the deleted data address.
13332 	 */
13333 	if (IS_IPMP(ill))
13334 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13335 
13336 	/*
13337 	 * If this is the last ipif that is going down and there are no
13338 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13339 	 * clean up ARP completely.
13340 	 */
13341 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13342 		/*
13343 		 * If this was the last ipif on an IPMP interface, purge any
13344 		 * IPMP ARP entries associated with it.
13345 		 */
13346 		if (IS_IPMP(ill))
13347 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13348 
13349 		/* Send up AR_INTERFACE_DOWN message */
13350 		mp = ill->ill_arp_down_mp;
13351 		if (mp != NULL) {
13352 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13353 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13354 			    ipif->ipif_id));
13355 			putnext(ill->ill_rq, mp);
13356 			ill->ill_arp_down_mp = NULL;
13357 		}
13358 
13359 		/* Tell ARP to delete the multicast mappings */
13360 		mp = ill->ill_arp_del_mapping_mp;
13361 		if (mp != NULL) {
13362 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13363 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13364 			    ipif->ipif_id));
13365 			putnext(ill->ill_rq, mp);
13366 			ill->ill_arp_del_mapping_mp = NULL;
13367 		}
13368 	}
13369 }
13370 
13371 /*
13372  * Set up the multicast mappings for `ipif' in ARP.  If `arp_add_mapping_mp'
13373  * is non-NULL, then upon success it will contain an mblk that can be passed
13374  * to ARP to create the mapping.  Otherwise, if it's NULL, upon success ARP
13375  * will have already been notified to create the mapping.  Returns zero on
13376  * success, -1 upon failure.
13377  */
13378 int
13379 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13380 {
13381 	mblk_t	*del_mp = NULL;
13382 	mblk_t *add_mp = NULL;
13383 	mblk_t *mp;
13384 	ill_t	*ill = ipif->ipif_ill;
13385 	phyint_t *phyi = ill->ill_phyint;
13386 	ipaddr_t addr, mask, extract_mask = 0;
13387 	arma_t	*arma;
13388 	uint8_t *maddr, *bphys_addr;
13389 	uint32_t hw_start;
13390 	dl_unitdata_req_t *dlur;
13391 
13392 	ASSERT(IAM_WRITER_IPIF(ipif));
13393 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13394 		return (0);
13395 
13396 	/*
13397 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13398 	 * and instead use the ones on the underlying interfaces.
13399 	 */
13400 	if (IS_IPMP(ill))
13401 		return (0);
13402 
13403 	/*
13404 	 * Delete the existing mapping from ARP.  Normally, ipif_down() ->
13405 	 * ipif_resolver_down() will send this up to ARP, but it may be that
13406 	 * we are enabling PHYI_MULTI_BCAST via ip_rput_dlpi_writer().
13407 	 */
13408 	mp = ill->ill_arp_del_mapping_mp;
13409 	if (mp != NULL) {
13410 		ip1dbg(("ipif_arp_setup_multicast: arp cmd %x for %s:%u\n",
13411 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13412 		putnext(ill->ill_rq, mp);
13413 		ill->ill_arp_del_mapping_mp = NULL;
13414 	}
13415 
13416 	if (arp_add_mapping_mp != NULL)
13417 		*arp_add_mapping_mp = NULL;
13418 
13419 	/*
13420 	 * Check that the address is not to long for the constant
13421 	 * length reserved in the template arma_t.
13422 	 */
13423 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13424 		return (-1);
13425 
13426 	/* Add mapping mblk */
13427 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13428 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13429 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13430 	    (caddr_t)&addr);
13431 	if (add_mp == NULL)
13432 		return (-1);
13433 	arma = (arma_t *)add_mp->b_rptr;
13434 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13435 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13436 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13437 
13438 	/*
13439 	 * Determine the broadcast address.
13440 	 */
13441 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13442 	if (ill->ill_sap_length < 0)
13443 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13444 	else
13445 		bphys_addr = (uchar_t *)dlur +
13446 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13447 	/*
13448 	 * Check PHYI_MULTI_BCAST and length of physical
13449 	 * address to determine if we use the mapping or the
13450 	 * broadcast address.
13451 	 */
13452 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13453 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13454 		    bphys_addr, maddr, &hw_start, &extract_mask))
13455 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13456 
13457 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13458 	    (ill->ill_flags & ILLF_MULTICAST)) {
13459 		/* Make sure this will not match the "exact" entry. */
13460 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13461 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13462 		    (caddr_t)&addr);
13463 		if (del_mp == NULL) {
13464 			freemsg(add_mp);
13465 			return (-1);
13466 		}
13467 		bcopy(&extract_mask, (char *)arma +
13468 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13469 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13470 			/* Use link-layer broadcast address for MULTI_BCAST */
13471 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13472 			ip2dbg(("ipif_arp_setup_multicast: adding"
13473 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13474 		} else {
13475 			arma->arma_hw_mapping_start = hw_start;
13476 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13477 			    " ARP setup for %s\n", ill->ill_name));
13478 		}
13479 	} else {
13480 		freemsg(add_mp);
13481 		ASSERT(del_mp == NULL);
13482 		/* It is neither MULTICAST nor MULTI_BCAST */
13483 		return (0);
13484 	}
13485 	ASSERT(add_mp != NULL && del_mp != NULL);
13486 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13487 	ill->ill_arp_del_mapping_mp = del_mp;
13488 	if (arp_add_mapping_mp != NULL) {
13489 		/* The caller just wants the mblks allocated */
13490 		*arp_add_mapping_mp = add_mp;
13491 	} else {
13492 		/* The caller wants us to send it to arp */
13493 		putnext(ill->ill_rq, add_mp);
13494 	}
13495 	return (0);
13496 }
13497 
13498 /*
13499  * Get the resolver set up for a new IP address.  (Always called as writer.)
13500  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13501  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13502  *
13503  * The enumerated value res_act tunes the behavior:
13504  * 	* Res_act_initial: set up all the resolver structures for a new
13505  *	  IP address.
13506  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13507  *	  ARP message in defense of the address.
13508  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13509  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13510  *
13511  * Returns zero on success, or an errno upon failure.
13512  */
13513 int
13514 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13515 {
13516 	mblk_t	*arp_up_mp = NULL;
13517 	mblk_t	*arp_down_mp = NULL;
13518 	mblk_t	*arp_add_mp = NULL;
13519 	mblk_t	*arp_del_mp = NULL;
13520 	mblk_t	*arp_add_mapping_mp = NULL;
13521 	mblk_t	*arp_del_mapping_mp = NULL;
13522 	ill_t	*ill = ipif->ipif_ill;
13523 	int	err = ENOMEM;
13524 	boolean_t added_ipif = B_FALSE;
13525 	boolean_t publish;
13526 	boolean_t was_dup;
13527 
13528 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13529 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13530 	ASSERT(IAM_WRITER_IPIF(ipif));
13531 
13532 	was_dup = B_FALSE;
13533 	if (res_act == Res_act_initial) {
13534 		ipif->ipif_addr_ready = 0;
13535 		/*
13536 		 * We're bringing an interface up here.  There's no way that we
13537 		 * should need to shut down ARP now.
13538 		 */
13539 		mutex_enter(&ill->ill_lock);
13540 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13541 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13542 			ill->ill_ipif_dup_count--;
13543 			was_dup = B_TRUE;
13544 		}
13545 		mutex_exit(&ill->ill_lock);
13546 	}
13547 	if (ipif->ipif_recovery_id != 0)
13548 		(void) untimeout(ipif->ipif_recovery_id);
13549 	ipif->ipif_recovery_id = 0;
13550 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13551 		ipif->ipif_addr_ready = 1;
13552 		return (0);
13553 	}
13554 	/* NDP will set the ipif_addr_ready flag when it's ready */
13555 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13556 		return (0);
13557 
13558 	if (ill->ill_isv6) {
13559 		/*
13560 		 * External resolver for IPv6
13561 		 */
13562 		ASSERT(res_act == Res_act_initial);
13563 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13564 	} else {
13565 		/*
13566 		 * IPv4 arp case. If the ARP stream has already started
13567 		 * closing, fail this request for ARP bringup. Else
13568 		 * record the fact that an ARP bringup is pending.
13569 		 */
13570 		mutex_enter(&ill->ill_lock);
13571 		if (ill->ill_arp_closing) {
13572 			mutex_exit(&ill->ill_lock);
13573 			err = EINVAL;
13574 			goto failed;
13575 		} else {
13576 			if (ill->ill_ipif_up_count == 0 &&
13577 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13578 				ill->ill_arp_bringup_pending = 1;
13579 			mutex_exit(&ill->ill_lock);
13580 		}
13581 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13582 	}
13583 
13584 	if (IS_IPMP(ill) && publish) {
13585 		/*
13586 		 * If we're here via ipif_up(), then the ipif won't be bound
13587 		 * yet -- add it to the group, which will bind it if possible.
13588 		 * (We would add it in ipif_up(), but deleting on failure
13589 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13590 		 * then the ipif has already been added to the group and we
13591 		 * just need to use the binding.
13592 		 */
13593 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13594 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13595 				/*
13596 				 * We couldn't bind the ipif to an ill yet,
13597 				 * so we have nothing to publish.
13598 				 */
13599 				publish = B_FALSE;
13600 			}
13601 			added_ipif = B_TRUE;
13602 		}
13603 	}
13604 
13605 	/*
13606 	 * Add an entry for the local address in ARP only if it
13607 	 * is not UNNUMBERED and it is suitable for publishing.
13608 	 */
13609 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13610 		if (res_act == Res_act_defend) {
13611 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13612 			if (arp_add_mp == NULL)
13613 				goto failed;
13614 			/*
13615 			 * If we're just defending our address now, then
13616 			 * there's no need to set up ARP multicast mappings.
13617 			 * The publish command is enough.
13618 			 */
13619 			goto done;
13620 		}
13621 
13622 		/*
13623 		 * Allocate an ARP add message and an ARP delete message (the
13624 		 * latter is saved for use when the address goes down).
13625 		 */
13626 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13627 			goto failed;
13628 
13629 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13630 			goto failed;
13631 
13632 		if (res_act != Res_act_initial)
13633 			goto arp_setup_multicast;
13634 	} else {
13635 		if (res_act != Res_act_initial)
13636 			goto done;
13637 	}
13638 	/*
13639 	 * Need to bring up ARP or setup multicast mapping only
13640 	 * when the first interface is coming UP.
13641 	 */
13642 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13643 		goto done;
13644 
13645 	/*
13646 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13647 	 */
13648 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13649 	if (arp_down_mp == NULL)
13650 		goto failed;
13651 
13652 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13653 	if (arp_up_mp == NULL)
13654 		goto failed;
13655 
13656 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13657 		goto done;
13658 
13659 arp_setup_multicast:
13660 	/*
13661 	 * Setup the multicast mappings. This function initializes
13662 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13663 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13664 	 */
13665 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13666 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13667 		if (err != 0)
13668 			goto failed;
13669 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13670 		ASSERT(arp_add_mapping_mp != NULL);
13671 	}
13672 done:
13673 	if (arp_up_mp != NULL) {
13674 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13675 		    ill->ill_name, ipif->ipif_id));
13676 		putnext(ill->ill_rq, arp_up_mp);
13677 		arp_up_mp = NULL;
13678 	}
13679 	if (arp_add_mp != NULL) {
13680 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13681 		    ill->ill_name, ipif->ipif_id));
13682 		/*
13683 		 * If it's an extended ARP implementation, then we'll wait to
13684 		 * hear that DAD has finished before using the interface.
13685 		 */
13686 		if (!ill->ill_arp_extend)
13687 			ipif->ipif_addr_ready = 1;
13688 		putnext(ill->ill_rq, arp_add_mp);
13689 		arp_add_mp = NULL;
13690 	} else {
13691 		ipif->ipif_addr_ready = 1;
13692 	}
13693 	if (arp_add_mapping_mp != NULL) {
13694 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13695 		    ill->ill_name, ipif->ipif_id));
13696 		putnext(ill->ill_rq, arp_add_mapping_mp);
13697 		arp_add_mapping_mp = NULL;
13698 	}
13699 
13700 	if (res_act == Res_act_initial) {
13701 		if (ill->ill_flags & ILLF_NOARP)
13702 			err = ill_arp_off(ill);
13703 		else
13704 			err = ill_arp_on(ill);
13705 		if (err != 0) {
13706 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13707 			    err));
13708 			goto failed;
13709 		}
13710 	}
13711 
13712 	if (arp_del_mp != NULL) {
13713 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13714 		ipif->ipif_arp_del_mp = arp_del_mp;
13715 	}
13716 	if (arp_down_mp != NULL) {
13717 		ASSERT(ill->ill_arp_down_mp == NULL);
13718 		ill->ill_arp_down_mp = arp_down_mp;
13719 	}
13720 	if (arp_del_mapping_mp != NULL) {
13721 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13722 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13723 	}
13724 
13725 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13726 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13727 failed:
13728 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13729 	if (added_ipif)
13730 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13731 	freemsg(arp_add_mp);
13732 	freemsg(arp_del_mp);
13733 	freemsg(arp_add_mapping_mp);
13734 	freemsg(arp_up_mp);
13735 	freemsg(arp_down_mp);
13736 	ill->ill_arp_bringup_pending = 0;
13737 	return (err);
13738 }
13739 
13740 /*
13741  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13742  * just gone back up.
13743  */
13744 static void
13745 ipif_arp_start_dad(ipif_t *ipif)
13746 {
13747 	ill_t *ill = ipif->ipif_ill;
13748 	mblk_t *arp_add_mp;
13749 
13750 	/* ACE_F_UNVERIFIED restarts DAD */
13751 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13752 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13753 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13754 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13755 		/*
13756 		 * If we can't contact ARP for some reason, that's not really a
13757 		 * problem.  Just send out the routing socket notification that
13758 		 * DAD completion would have done, and continue.
13759 		 */
13760 		ipif_mask_reply(ipif);
13761 		ipif_up_notify(ipif);
13762 		ipif->ipif_addr_ready = 1;
13763 		return;
13764 	}
13765 
13766 	putnext(ill->ill_rq, arp_add_mp);
13767 }
13768 
13769 static void
13770 ipif_ndp_start_dad(ipif_t *ipif)
13771 {
13772 	nce_t *nce;
13773 
13774 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13775 	    B_FALSE);
13776 	if (nce == NULL)
13777 		return;
13778 
13779 	if (!ndp_restart_dad(nce)) {
13780 		/*
13781 		 * If we can't restart DAD for some reason, that's not really a
13782 		 * problem.  Just send out the routing socket notification that
13783 		 * DAD completion would have done, and continue.
13784 		 */
13785 		ipif_up_notify(ipif);
13786 		ipif->ipif_addr_ready = 1;
13787 	}
13788 	NCE_REFRELE(nce);
13789 }
13790 
13791 /*
13792  * Restart duplicate address detection on all interfaces on the given ill.
13793  *
13794  * This is called when an interface transitions from down to up
13795  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13796  *
13797  * Note that since the underlying physical link has transitioned, we must cause
13798  * at least one routing socket message to be sent here, either via DAD
13799  * completion or just by default on the first ipif.  (If we don't do this, then
13800  * in.mpathd will see long delays when doing link-based failure recovery.)
13801  */
13802 void
13803 ill_restart_dad(ill_t *ill, boolean_t went_up)
13804 {
13805 	ipif_t *ipif;
13806 
13807 	if (ill == NULL)
13808 		return;
13809 
13810 	/*
13811 	 * If layer two doesn't support duplicate address detection, then just
13812 	 * send the routing socket message now and be done with it.
13813 	 */
13814 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13815 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13816 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13817 		return;
13818 	}
13819 
13820 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13821 		if (went_up) {
13822 			if (ipif->ipif_flags & IPIF_UP) {
13823 				if (ill->ill_isv6)
13824 					ipif_ndp_start_dad(ipif);
13825 				else
13826 					ipif_arp_start_dad(ipif);
13827 			} else if (ill->ill_isv6 &&
13828 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13829 				/*
13830 				 * For IPv4, the ARP module itself will
13831 				 * automatically start the DAD process when it
13832 				 * sees DL_NOTE_LINK_UP.  We respond to the
13833 				 * AR_CN_READY at the completion of that task.
13834 				 * For IPv6, we must kick off the bring-up
13835 				 * process now.
13836 				 */
13837 				ndp_do_recovery(ipif);
13838 			} else {
13839 				/*
13840 				 * Unfortunately, the first ipif is "special"
13841 				 * and represents the underlying ill in the
13842 				 * routing socket messages.  Thus, when this
13843 				 * one ipif is down, we must still notify so
13844 				 * that the user knows the IFF_RUNNING status
13845 				 * change.  (If the first ipif is up, then
13846 				 * we'll handle eventual routing socket
13847 				 * notification via DAD completion.)
13848 				 */
13849 				if (ipif == ill->ill_ipif) {
13850 					ip_rts_ifmsg(ill->ill_ipif,
13851 					    RTSQ_DEFAULT);
13852 				}
13853 			}
13854 		} else {
13855 			/*
13856 			 * After link down, we'll need to send a new routing
13857 			 * message when the link comes back, so clear
13858 			 * ipif_addr_ready.
13859 			 */
13860 			ipif->ipif_addr_ready = 0;
13861 		}
13862 	}
13863 
13864 	/*
13865 	 * If we've torn down links, then notify the user right away.
13866 	 */
13867 	if (!went_up)
13868 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13869 }
13870 
13871 static void
13872 ipsq_delete(ipsq_t *ipsq)
13873 {
13874 	ipxop_t *ipx = ipsq->ipsq_xop;
13875 
13876 	ipsq->ipsq_ipst = NULL;
13877 	ASSERT(ipsq->ipsq_phyint == NULL);
13878 	ASSERT(ipsq->ipsq_xop != NULL);
13879 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
13880 	ASSERT(ipx->ipx_pending_mp == NULL);
13881 	kmem_free(ipsq, sizeof (ipsq_t));
13882 }
13883 
13884 static int
13885 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
13886 {
13887 	int err;
13888 	ipif_t *ipif;
13889 
13890 	if (ill == NULL)
13891 		return (0);
13892 
13893 	ASSERT(IAM_WRITER_ILL(ill));
13894 	ill->ill_up_ipifs = B_TRUE;
13895 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13896 		if (ipif->ipif_was_up) {
13897 			if (!(ipif->ipif_flags & IPIF_UP))
13898 				err = ipif_up(ipif, q, mp);
13899 			ipif->ipif_was_up = B_FALSE;
13900 			if (err != 0) {
13901 				ASSERT(err == EINPROGRESS);
13902 				return (err);
13903 			}
13904 		}
13905 	}
13906 	mutex_enter(&ill->ill_lock);
13907 	ill->ill_state_flags &= ~ILL_CHANGING;
13908 	mutex_exit(&ill->ill_lock);
13909 	ill->ill_up_ipifs = B_FALSE;
13910 	return (0);
13911 }
13912 
13913 /*
13914  * This function is called to bring up all the ipifs that were up before
13915  * bringing the ill down via ill_down_ipifs().
13916  */
13917 int
13918 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13919 {
13920 	int err;
13921 
13922 	ASSERT(IAM_WRITER_ILL(ill));
13923 
13924 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
13925 	if (err != 0)
13926 		return (err);
13927 
13928 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
13929 }
13930 
13931 /*
13932  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
13933  * down the ipifs without sending DL_UNBIND_REQ to the driver.
13934  */
13935 static void
13936 ill_down_ipifs(ill_t *ill, boolean_t logical)
13937 {
13938 	ipif_t *ipif;
13939 
13940 	ASSERT(IAM_WRITER_ILL(ill));
13941 
13942 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13943 		/*
13944 		 * We go through the ipif_down logic even if the ipif
13945 		 * is already down, since routes can be added based
13946 		 * on down ipifs. Going through ipif_down once again
13947 		 * will delete any IREs created based on these routes.
13948 		 */
13949 		if (ipif->ipif_flags & IPIF_UP)
13950 			ipif->ipif_was_up = B_TRUE;
13951 
13952 		/*
13953 		 * Need to re-create net/subnet bcast ires if
13954 		 * they are dependent on ipif.
13955 		 */
13956 		if (!ipif->ipif_isv6)
13957 			ipif_check_bcast_ires(ipif);
13958 		if (logical) {
13959 			(void) ipif_logical_down(ipif, NULL, NULL);
13960 			ipif_non_duplicate(ipif);
13961 			ipif_down_tail(ipif);
13962 		} else {
13963 			(void) ipif_down(ipif, NULL, NULL);
13964 		}
13965 	}
13966 }
13967 
13968 /*
13969  * Redo source address selection.  This is called when a
13970  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
13971  */
13972 void
13973 ill_update_source_selection(ill_t *ill)
13974 {
13975 	ipif_t *ipif;
13976 
13977 	ASSERT(IAM_WRITER_ILL(ill));
13978 
13979 	/*
13980 	 * Underlying interfaces are only used for test traffic and thus
13981 	 * should always send with their (deprecated) source addresses.
13982 	 */
13983 	if (IS_UNDER_IPMP(ill))
13984 		return;
13985 
13986 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13987 		if (ill->ill_isv6)
13988 			ipif_recreate_interface_routes_v6(NULL, ipif);
13989 		else
13990 			ipif_recreate_interface_routes(NULL, ipif);
13991 	}
13992 }
13993 
13994 /*
13995  * Finish the group join started in ip_sioctl_groupname().
13996  */
13997 /* ARGSUSED */
13998 static void
13999 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14000 {
14001 	ill_t		*ill = q->q_ptr;
14002 	phyint_t	*phyi = ill->ill_phyint;
14003 	ipmp_grp_t	*grp = phyi->phyint_grp;
14004 	ip_stack_t	*ipst = ill->ill_ipst;
14005 
14006 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14007 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14008 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14009 
14010 	if (phyi->phyint_illv4 != NULL) {
14011 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14012 		VERIFY(grp->gr_pendv4-- > 0);
14013 		rw_exit(&ipst->ips_ipmp_lock);
14014 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14015 	}
14016 	if (phyi->phyint_illv6 != NULL) {
14017 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14018 		VERIFY(grp->gr_pendv6-- > 0);
14019 		rw_exit(&ipst->ips_ipmp_lock);
14020 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14021 	}
14022 	freemsg(mp);
14023 }
14024 
14025 /*
14026  * Process an SIOCSLIFGROUPNAME request.
14027  */
14028 /* ARGSUSED */
14029 int
14030 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14031     ip_ioctl_cmd_t *ipip, void *ifreq)
14032 {
14033 	struct lifreq	*lifr = ifreq;
14034 	ill_t		*ill = ipif->ipif_ill;
14035 	ip_stack_t	*ipst = ill->ill_ipst;
14036 	phyint_t	*phyi = ill->ill_phyint;
14037 	ipmp_grp_t	*grp = phyi->phyint_grp;
14038 	mblk_t		*ipsq_mp;
14039 	int		err = 0;
14040 
14041 	/*
14042 	 * Note that phyint_grp can only change here, where we're exclusive.
14043 	 */
14044 	ASSERT(IAM_WRITER_ILL(ill));
14045 
14046 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14047 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14048 		return (EINVAL);
14049 
14050 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14051 
14052 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14053 
14054 	/*
14055 	 * If the name hasn't changed, there's nothing to do.
14056 	 */
14057 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14058 		goto unlock;
14059 
14060 	/*
14061 	 * Handle requests to rename an IPMP meta-interface.
14062 	 *
14063 	 * Note that creation of the IPMP meta-interface is handled in
14064 	 * userland through the standard plumbing sequence.  As part of the
14065 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14066 	 * the name of the interface (see ipif_set_values_tail()).
14067 	 */
14068 	if (IS_IPMP(ill)) {
14069 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14070 		goto unlock;
14071 	}
14072 
14073 	/*
14074 	 * Handle requests to add or remove an IP interface from a group.
14075 	 */
14076 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14077 		/*
14078 		 * Moves are handled by first removing the interface from
14079 		 * its existing group, and then adding it to another group.
14080 		 * So, fail if it's already in a group.
14081 		 */
14082 		if (IS_UNDER_IPMP(ill)) {
14083 			err = EALREADY;
14084 			goto unlock;
14085 		}
14086 
14087 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14088 		if (grp == NULL) {
14089 			err = ENOENT;
14090 			goto unlock;
14091 		}
14092 
14093 		/*
14094 		 * Check if the phyint and its ills are suitable for
14095 		 * inclusion into the group.
14096 		 */
14097 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14098 			goto unlock;
14099 
14100 		/*
14101 		 * Checks pass; join the group, and enqueue the remaining
14102 		 * illgrp joins for when we've become part of the group xop
14103 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14104 		 * requires an mblk_t to scribble on, and since `mp' will be
14105 		 * freed as part of completing the ioctl, allocate another.
14106 		 */
14107 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14108 			err = ENOMEM;
14109 			goto unlock;
14110 		}
14111 
14112 		/*
14113 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14114 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14115 		 * before ip_join_illgrps() is called back.  See the comments
14116 		 * in ip_sioctl_plink_ipmp() for more.
14117 		 */
14118 		if (phyi->phyint_illv4 != NULL)
14119 			grp->gr_pendv4++;
14120 		if (phyi->phyint_illv6 != NULL)
14121 			grp->gr_pendv6++;
14122 
14123 		rw_exit(&ipst->ips_ipmp_lock);
14124 
14125 		ipmp_phyint_join_grp(phyi, grp);
14126 		ill_refhold(ill);
14127 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14128 		    SWITCH_OP, B_FALSE);
14129 		return (0);
14130 	} else {
14131 		/*
14132 		 * Request to remove the interface from a group.  If the
14133 		 * interface is not in a group, this trivially succeeds.
14134 		 */
14135 		rw_exit(&ipst->ips_ipmp_lock);
14136 		if (IS_UNDER_IPMP(ill))
14137 			ipmp_phyint_leave_grp(phyi);
14138 		return (0);
14139 	}
14140 unlock:
14141 	rw_exit(&ipst->ips_ipmp_lock);
14142 	return (err);
14143 }
14144 
14145 /*
14146  * Process an SIOCGLIFBINDING request.
14147  */
14148 /* ARGSUSED */
14149 int
14150 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14151     ip_ioctl_cmd_t *ipip, void *ifreq)
14152 {
14153 	ill_t		*ill;
14154 	struct lifreq	*lifr = ifreq;
14155 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14156 
14157 	if (!IS_IPMP(ipif->ipif_ill))
14158 		return (EINVAL);
14159 
14160 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14161 	if ((ill = ipif->ipif_bound_ill) == NULL)
14162 		lifr->lifr_binding[0] = '\0';
14163 	else
14164 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
14165 	rw_exit(&ipst->ips_ipmp_lock);
14166 	return (0);
14167 }
14168 
14169 /*
14170  * Process an SIOCGLIFGROUPNAME request.
14171  */
14172 /* ARGSUSED */
14173 int
14174 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14175     ip_ioctl_cmd_t *ipip, void *ifreq)
14176 {
14177 	ipmp_grp_t	*grp;
14178 	struct lifreq	*lifr = ifreq;
14179 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14180 
14181 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14182 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14183 		lifr->lifr_groupname[0] = '\0';
14184 	else
14185 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14186 	rw_exit(&ipst->ips_ipmp_lock);
14187 	return (0);
14188 }
14189 
14190 /*
14191  * Process an SIOCGLIFGROUPINFO request.
14192  */
14193 /* ARGSUSED */
14194 int
14195 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14196     ip_ioctl_cmd_t *ipip, void *dummy)
14197 {
14198 	ipmp_grp_t	*grp;
14199 	lifgroupinfo_t	*lifgr;
14200 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14201 
14202 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14203 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14204 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14205 
14206 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14207 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14208 		rw_exit(&ipst->ips_ipmp_lock);
14209 		return (ENOENT);
14210 	}
14211 	ipmp_grp_info(grp, lifgr);
14212 	rw_exit(&ipst->ips_ipmp_lock);
14213 	return (0);
14214 }
14215 
14216 static void
14217 ill_dl_down(ill_t *ill)
14218 {
14219 	/*
14220 	 * The ill is down; unbind but stay attached since we're still
14221 	 * associated with a PPA. If we have negotiated DLPI capabilites
14222 	 * with the data link service provider (IDS_OK) then reset them.
14223 	 * The interval between unbinding and rebinding is potentially
14224 	 * unbounded hence we cannot assume things will be the same.
14225 	 * The DLPI capabilities will be probed again when the data link
14226 	 * is brought up.
14227 	 */
14228 	mblk_t	*mp = ill->ill_unbind_mp;
14229 
14230 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14231 
14232 	ill->ill_unbind_mp = NULL;
14233 	if (mp != NULL) {
14234 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14235 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14236 		    ill->ill_name));
14237 		mutex_enter(&ill->ill_lock);
14238 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14239 		mutex_exit(&ill->ill_lock);
14240 		/*
14241 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14242 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14243 		 * ill_capability_dld_disable disable rightaway. If this is not
14244 		 * an unplumb operation then the disable happens on receipt of
14245 		 * the capab ack via ip_rput_dlpi_writer ->
14246 		 * ill_capability_ack_thr. In both cases the order of
14247 		 * the operations seen by DLD is capability disable followed
14248 		 * by DL_UNBIND. Also the DLD capability disable needs a
14249 		 * cv_wait'able context.
14250 		 */
14251 		if (ill->ill_state_flags & ILL_CONDEMNED)
14252 			ill_capability_dld_disable(ill);
14253 		ill_capability_reset(ill, B_FALSE);
14254 		ill_dlpi_send(ill, mp);
14255 	}
14256 
14257 	/*
14258 	 * Toss all of our multicast memberships.  We could keep them, but
14259 	 * then we'd have to do bookkeeping of any joins and leaves performed
14260 	 * by the application while the the interface is down (we can't just
14261 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14262 	 * on a downed interface).
14263 	 */
14264 	ill_leave_multicast(ill);
14265 
14266 	mutex_enter(&ill->ill_lock);
14267 	ill->ill_dl_up = 0;
14268 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14269 	mutex_exit(&ill->ill_lock);
14270 }
14271 
14272 static void
14273 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14274 {
14275 	union DL_primitives *dlp;
14276 	t_uscalar_t prim;
14277 	boolean_t waitack = B_FALSE;
14278 
14279 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14280 
14281 	dlp = (union DL_primitives *)mp->b_rptr;
14282 	prim = dlp->dl_primitive;
14283 
14284 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14285 	    dl_primstr(prim), prim, ill->ill_name));
14286 
14287 	switch (prim) {
14288 	case DL_PHYS_ADDR_REQ:
14289 	{
14290 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14291 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14292 		break;
14293 	}
14294 	case DL_BIND_REQ:
14295 		mutex_enter(&ill->ill_lock);
14296 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14297 		mutex_exit(&ill->ill_lock);
14298 		break;
14299 	}
14300 
14301 	/*
14302 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14303 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14304 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14305 	 */
14306 	mutex_enter(&ill->ill_lock);
14307 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
14308 	    (prim == DL_UNBIND_REQ)) {
14309 		ill->ill_dlpi_pending = prim;
14310 		waitack = B_TRUE;
14311 	}
14312 
14313 	mutex_exit(&ill->ill_lock);
14314 	putnext(ill->ill_wq, mp);
14315 
14316 	/*
14317 	 * There is no ack for DL_NOTIFY_CONF messages
14318 	 */
14319 	if (waitack && prim == DL_NOTIFY_CONF)
14320 		ill_dlpi_done(ill, prim);
14321 }
14322 
14323 /*
14324  * Helper function for ill_dlpi_send().
14325  */
14326 /* ARGSUSED */
14327 static void
14328 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14329 {
14330 	ill_dlpi_send(q->q_ptr, mp);
14331 }
14332 
14333 /*
14334  * Send a DLPI control message to the driver but make sure there
14335  * is only one outstanding message. Uses ill_dlpi_pending to tell
14336  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14337  * when an ACK or a NAK is received to process the next queued message.
14338  */
14339 void
14340 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14341 {
14342 	mblk_t **mpp;
14343 
14344 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14345 
14346 	/*
14347 	 * To ensure that any DLPI requests for current exclusive operation
14348 	 * are always completely sent before any DLPI messages for other
14349 	 * operations, require writer access before enqueuing.
14350 	 */
14351 	if (!IAM_WRITER_ILL(ill)) {
14352 		ill_refhold(ill);
14353 		/* qwriter_ip() does the ill_refrele() */
14354 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14355 		    NEW_OP, B_TRUE);
14356 		return;
14357 	}
14358 
14359 	mutex_enter(&ill->ill_lock);
14360 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14361 		/* Must queue message. Tail insertion */
14362 		mpp = &ill->ill_dlpi_deferred;
14363 		while (*mpp != NULL)
14364 			mpp = &((*mpp)->b_next);
14365 
14366 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14367 		    ill->ill_name));
14368 
14369 		*mpp = mp;
14370 		mutex_exit(&ill->ill_lock);
14371 		return;
14372 	}
14373 	mutex_exit(&ill->ill_lock);
14374 	ill_dlpi_dispatch(ill, mp);
14375 }
14376 
14377 static void
14378 ill_capability_send(ill_t *ill, mblk_t *mp)
14379 {
14380 	ill->ill_capab_pending_cnt++;
14381 	ill_dlpi_send(ill, mp);
14382 }
14383 
14384 void
14385 ill_capability_done(ill_t *ill)
14386 {
14387 	ASSERT(ill->ill_capab_pending_cnt != 0);
14388 
14389 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14390 
14391 	ill->ill_capab_pending_cnt--;
14392 	if (ill->ill_capab_pending_cnt == 0 &&
14393 	    ill->ill_dlpi_capab_state == IDCS_OK)
14394 		ill_capability_reset_alloc(ill);
14395 }
14396 
14397 /*
14398  * Send all deferred DLPI messages without waiting for their ACKs.
14399  */
14400 void
14401 ill_dlpi_send_deferred(ill_t *ill)
14402 {
14403 	mblk_t *mp, *nextmp;
14404 
14405 	/*
14406 	 * Clear ill_dlpi_pending so that the message is not queued in
14407 	 * ill_dlpi_send().
14408 	 */
14409 	mutex_enter(&ill->ill_lock);
14410 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14411 	mp = ill->ill_dlpi_deferred;
14412 	ill->ill_dlpi_deferred = NULL;
14413 	mutex_exit(&ill->ill_lock);
14414 
14415 	for (; mp != NULL; mp = nextmp) {
14416 		nextmp = mp->b_next;
14417 		mp->b_next = NULL;
14418 		ill_dlpi_send(ill, mp);
14419 	}
14420 }
14421 
14422 /*
14423  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14424  */
14425 boolean_t
14426 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14427 {
14428 	t_uscalar_t pending;
14429 
14430 	mutex_enter(&ill->ill_lock);
14431 	if (ill->ill_dlpi_pending == prim) {
14432 		mutex_exit(&ill->ill_lock);
14433 		return (B_TRUE);
14434 	}
14435 
14436 	/*
14437 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14438 	 * without waiting, so don't print any warnings in that case.
14439 	 */
14440 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14441 		mutex_exit(&ill->ill_lock);
14442 		return (B_FALSE);
14443 	}
14444 	pending = ill->ill_dlpi_pending;
14445 	mutex_exit(&ill->ill_lock);
14446 
14447 	if (pending == DL_PRIM_INVAL) {
14448 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14449 		    "received unsolicited ack for %s on %s\n",
14450 		    dl_primstr(prim), ill->ill_name);
14451 	} else {
14452 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14453 		    "received unexpected ack for %s on %s (expecting %s)\n",
14454 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14455 	}
14456 	return (B_FALSE);
14457 }
14458 
14459 /*
14460  * Complete the current DLPI operation associated with `prim' on `ill' and
14461  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14462  * operations and the ill's current exclusive IPSQ operation has finished
14463  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14464  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14465  * the comments above ipsq_current_finish() for details.
14466  */
14467 void
14468 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14469 {
14470 	mblk_t *mp;
14471 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14472 	ipxop_t *ipx = ipsq->ipsq_xop;
14473 
14474 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14475 	mutex_enter(&ill->ill_lock);
14476 
14477 	ASSERT(prim != DL_PRIM_INVAL);
14478 	ASSERT(ill->ill_dlpi_pending == prim);
14479 
14480 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14481 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14482 
14483 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14484 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14485 		if (ipx->ipx_current_done) {
14486 			mutex_enter(&ipx->ipx_lock);
14487 			ipx->ipx_current_ipif = NULL;
14488 			mutex_exit(&ipx->ipx_lock);
14489 		}
14490 		cv_signal(&ill->ill_cv);
14491 		mutex_exit(&ill->ill_lock);
14492 		return;
14493 	}
14494 
14495 	ill->ill_dlpi_deferred = mp->b_next;
14496 	mp->b_next = NULL;
14497 	mutex_exit(&ill->ill_lock);
14498 
14499 	ill_dlpi_dispatch(ill, mp);
14500 }
14501 
14502 void
14503 conn_delete_ire(conn_t *connp, caddr_t arg)
14504 {
14505 	ipif_t	*ipif = (ipif_t *)arg;
14506 	ire_t	*ire;
14507 
14508 	/*
14509 	 * Look at the cached ires on conns which has pointers to ipifs.
14510 	 * We just call ire_refrele which clears up the reference
14511 	 * to ire. Called when a conn closes. Also called from ipif_free
14512 	 * to cleanup indirect references to the stale ipif via the cached ire.
14513 	 */
14514 	mutex_enter(&connp->conn_lock);
14515 	ire = connp->conn_ire_cache;
14516 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14517 		connp->conn_ire_cache = NULL;
14518 		mutex_exit(&connp->conn_lock);
14519 		IRE_REFRELE_NOTR(ire);
14520 		return;
14521 	}
14522 	mutex_exit(&connp->conn_lock);
14523 
14524 }
14525 
14526 /*
14527  * Some operations (e.g., ipif_down()) conditionally delete a number
14528  * of IREs. Those IREs may have been previously cached in the conn structure.
14529  * This ipcl_walk() walker function releases all references to such IREs based
14530  * on the condemned flag.
14531  */
14532 /* ARGSUSED */
14533 void
14534 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14535 {
14536 	ire_t	*ire;
14537 
14538 	mutex_enter(&connp->conn_lock);
14539 	ire = connp->conn_ire_cache;
14540 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14541 		connp->conn_ire_cache = NULL;
14542 		mutex_exit(&connp->conn_lock);
14543 		IRE_REFRELE_NOTR(ire);
14544 		return;
14545 	}
14546 	mutex_exit(&connp->conn_lock);
14547 }
14548 
14549 /*
14550  * Take down a specific interface, but don't lose any information about it.
14551  * (Always called as writer.)
14552  * This function goes through the down sequence even if the interface is
14553  * already down. There are 2 reasons.
14554  * a. Currently we permit interface routes that depend on down interfaces
14555  *    to be added. This behaviour itself is questionable. However it appears
14556  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14557  *    time. We go thru the cleanup in order to remove these routes.
14558  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14559  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14560  *    down, but we need to cleanup i.e. do ill_dl_down and
14561  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14562  *
14563  * IP-MT notes:
14564  *
14565  * Model of reference to interfaces.
14566  *
14567  * The following members in ipif_t track references to the ipif.
14568  *	int     ipif_refcnt;    Active reference count
14569  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14570  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14571  *
14572  * The following members in ill_t track references to the ill.
14573  *	int             ill_refcnt;     active refcnt
14574  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14575  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14576  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14577  *
14578  * Reference to an ipif or ill can be obtained in any of the following ways.
14579  *
14580  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14581  * Pointers to ipif / ill from other data structures viz ire and conn.
14582  * Implicit reference to the ipif / ill by holding a reference to the ire.
14583  *
14584  * The ipif/ill lookup functions return a reference held ipif / ill.
14585  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14586  * This is a purely dynamic reference count associated with threads holding
14587  * references to the ipif / ill. Pointers from other structures do not
14588  * count towards this reference count.
14589  *
14590  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14591  * associated with the ipif/ill. This is incremented whenever a new
14592  * ire is created referencing the ipif/ill. This is done atomically inside
14593  * ire_add_v[46] where the ire is actually added to the ire hash table.
14594  * The count is decremented in ire_inactive where the ire is destroyed.
14595  *
14596  * nce's reference ill's thru nce_ill and the count of nce's associated with
14597  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14598  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14599  * table. Similarly it is decremented in ndp_inactive() where the nce
14600  * is destroyed.
14601  *
14602  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14603  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14604  * in ilm_walker_cleanup() or ilm_delete().
14605  *
14606  * Flow of ioctls involving interface down/up
14607  *
14608  * The following is the sequence of an attempt to set some critical flags on an
14609  * up interface.
14610  * ip_sioctl_flags
14611  * ipif_down
14612  * wait for ipif to be quiescent
14613  * ipif_down_tail
14614  * ip_sioctl_flags_tail
14615  *
14616  * All set ioctls that involve down/up sequence would have a skeleton similar
14617  * to the above. All the *tail functions are called after the refcounts have
14618  * dropped to the appropriate values.
14619  *
14620  * The mechanism to quiesce an ipif is as follows.
14621  *
14622  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14623  * on the ipif. Callers either pass a flag requesting wait or the lookup
14624  *  functions will return NULL.
14625  *
14626  * Delete all ires referencing this ipif
14627  *
14628  * Any thread attempting to do an ipif_refhold on an ipif that has been
14629  * obtained thru a cached pointer will first make sure that
14630  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14631  * increment the refcount.
14632  *
14633  * The above guarantees that the ipif refcount will eventually come down to
14634  * zero and the ipif will quiesce, once all threads that currently hold a
14635  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14636  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14637  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14638  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14639  * in ip.h
14640  *
14641  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14642  *
14643  * Threads trying to lookup an ipif or ill can pass a flag requesting
14644  * wait and restart if the ipif / ill cannot be looked up currently.
14645  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14646  * failure if the ipif is currently undergoing an exclusive operation, and
14647  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14648  * is restarted by ipsq_exit() when the current exclusive operation completes.
14649  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14650  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14651  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14652  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14653  * until we release the ipsq_lock, even though the the ill/ipif state flags
14654  * can change after we drop the ill_lock.
14655  *
14656  * An attempt to send out a packet using an ipif that is currently
14657  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14658  * operation and restart it later when the exclusive condition on the ipif ends.
14659  * This is an example of not passing the wait flag to the lookup functions. For
14660  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14661  * out a multicast packet on that ipif will fail while the ipif is
14662  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14663  * currently IPIF_CHANGING will also fail.
14664  */
14665 int
14666 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14667 {
14668 	ill_t		*ill = ipif->ipif_ill;
14669 	conn_t		*connp;
14670 	boolean_t	success;
14671 	boolean_t	ipif_was_up = B_FALSE;
14672 	ip_stack_t	*ipst = ill->ill_ipst;
14673 
14674 	ASSERT(IAM_WRITER_IPIF(ipif));
14675 
14676 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14677 
14678 	if (ipif->ipif_flags & IPIF_UP) {
14679 		mutex_enter(&ill->ill_lock);
14680 		ipif->ipif_flags &= ~IPIF_UP;
14681 		ASSERT(ill->ill_ipif_up_count > 0);
14682 		--ill->ill_ipif_up_count;
14683 		mutex_exit(&ill->ill_lock);
14684 		ipif_was_up = B_TRUE;
14685 		/* Update status in SCTP's list */
14686 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14687 		ill_nic_event_dispatch(ipif->ipif_ill,
14688 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14689 	}
14690 
14691 	/*
14692 	 * Blow away memberships we established in ipif_multicast_up().
14693 	 */
14694 	ipif_multicast_down(ipif);
14695 
14696 	/*
14697 	 * Remove from the mapping for __sin6_src_id. We insert only
14698 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14699 	 * stored as mapped addresses, we need to check for mapped
14700 	 * INADDR_ANY also.
14701 	 */
14702 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14703 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14704 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14705 		int err;
14706 
14707 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14708 		    ipif->ipif_zoneid, ipst);
14709 		if (err != 0) {
14710 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14711 		}
14712 	}
14713 
14714 	/*
14715 	 * Delete all IRE's pointing at this ipif or its source address.
14716 	 */
14717 	if (ipif->ipif_isv6) {
14718 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14719 		    ipst);
14720 	} else {
14721 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14722 		    ipst);
14723 	}
14724 
14725 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14726 		/*
14727 		 * Since the interface is now down, it may have just become
14728 		 * inactive.  Note that this needs to be done even for a
14729 		 * lll_logical_down(), or ARP entries will not get correctly
14730 		 * restored when the interface comes back up.
14731 		 */
14732 		if (IS_UNDER_IPMP(ill))
14733 			ipmp_ill_refresh_active(ill);
14734 	}
14735 
14736 	/*
14737 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14738 	 * ires have been deleted above. Otherwise a thread could end up
14739 	 * caching an ire in a conn after we have finished the cleanup of the
14740 	 * conn. The caching is done after making sure that the ire is not yet
14741 	 * condemned. Also documented in the block comment above ip_output
14742 	 */
14743 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14744 	/* Also, delete the ires cached in SCTP */
14745 	sctp_ire_cache_flush(ipif);
14746 
14747 	/*
14748 	 * Update any other ipifs which have used "our" local address as
14749 	 * a source address. This entails removing and recreating IRE_INTERFACE
14750 	 * entries for such ipifs.
14751 	 */
14752 	if (ipif->ipif_isv6)
14753 		ipif_update_other_ipifs_v6(ipif);
14754 	else
14755 		ipif_update_other_ipifs(ipif);
14756 
14757 	/*
14758 	 * neighbor-discovery or arp entries for this interface.
14759 	 */
14760 	ipif_ndp_down(ipif);
14761 
14762 	/*
14763 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14764 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14765 	 * and ill_delete -> ipif_free -> ipif_down
14766 	 */
14767 	if (mp == NULL) {
14768 		ASSERT(q == NULL);
14769 		return (0);
14770 	}
14771 
14772 	if (CONN_Q(q)) {
14773 		connp = Q_TO_CONN(q);
14774 		mutex_enter(&connp->conn_lock);
14775 	} else {
14776 		connp = NULL;
14777 	}
14778 	mutex_enter(&ill->ill_lock);
14779 	/*
14780 	 * Are there any ire's pointing to this ipif that are still active ?
14781 	 * If this is the last ipif going down, are there any ire's pointing
14782 	 * to this ill that are still active ?
14783 	 */
14784 	if (ipif_is_quiescent(ipif)) {
14785 		mutex_exit(&ill->ill_lock);
14786 		if (connp != NULL)
14787 			mutex_exit(&connp->conn_lock);
14788 		return (0);
14789 	}
14790 
14791 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14792 	    ill->ill_name, (void *)ill));
14793 	/*
14794 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14795 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14796 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14797 	 */
14798 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14799 	if (!success) {
14800 		/* The conn is closing. So just return */
14801 		ASSERT(connp != NULL);
14802 		mutex_exit(&ill->ill_lock);
14803 		mutex_exit(&connp->conn_lock);
14804 		return (EINTR);
14805 	}
14806 
14807 	mutex_exit(&ill->ill_lock);
14808 	if (connp != NULL)
14809 		mutex_exit(&connp->conn_lock);
14810 	return (EINPROGRESS);
14811 }
14812 
14813 void
14814 ipif_down_tail(ipif_t *ipif)
14815 {
14816 	ill_t	*ill = ipif->ipif_ill;
14817 
14818 	/*
14819 	 * Skip any loopback interface (null wq).
14820 	 * If this is the last logical interface on the ill
14821 	 * have ill_dl_down tell the driver we are gone (unbind)
14822 	 * Note that lun 0 can ipif_down even though
14823 	 * there are other logical units that are up.
14824 	 * This occurs e.g. when we change a "significant" IFF_ flag.
14825 	 */
14826 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
14827 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
14828 	    ill->ill_dl_up) {
14829 		ill_dl_down(ill);
14830 	}
14831 	ill->ill_logical_down = 0;
14832 
14833 	/*
14834 	 * Has to be after removing the routes in ipif_down_delete_ire.
14835 	 */
14836 	ipif_resolver_down(ipif);
14837 
14838 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
14839 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
14840 }
14841 
14842 /*
14843  * Bring interface logically down without bringing the physical interface
14844  * down e.g. when the netmask is changed. This avoids long lasting link
14845  * negotiations between an ethernet interface and a certain switches.
14846  */
14847 static int
14848 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14849 {
14850 	/*
14851 	 * The ill_logical_down flag is a transient flag. It is set here
14852 	 * and is cleared once the down has completed in ipif_down_tail.
14853 	 * This flag does not indicate whether the ill stream is in the
14854 	 * DL_BOUND state with the driver. Instead this flag is used by
14855 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
14856 	 * the driver. The state of the ill stream i.e. whether it is
14857 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
14858 	 */
14859 	ipif->ipif_ill->ill_logical_down = 1;
14860 	return (ipif_down(ipif, q, mp));
14861 }
14862 
14863 /*
14864  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
14865  * If the usesrc client ILL is already part of a usesrc group or not,
14866  * in either case a ire_stq with the matching usesrc client ILL will
14867  * locate the IRE's that need to be deleted. We want IREs to be created
14868  * with the new source address.
14869  */
14870 static void
14871 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
14872 {
14873 	ill_t	*ucill = (ill_t *)ill_arg;
14874 
14875 	ASSERT(IAM_WRITER_ILL(ucill));
14876 
14877 	if (ire->ire_stq == NULL)
14878 		return;
14879 
14880 	if ((ire->ire_type == IRE_CACHE) &&
14881 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
14882 		ire_delete(ire);
14883 }
14884 
14885 /*
14886  * ire_walk routine to delete every IRE dependent on the interface
14887  * address that is going down.	(Always called as writer.)
14888  * Works for both v4 and v6.
14889  * In addition for checking for ire_ipif matches it also checks for
14890  * IRE_CACHE entries which have the same source address as the
14891  * disappearing ipif since ipif_select_source might have picked
14892  * that source. Note that ipif_down/ipif_update_other_ipifs takes
14893  * care of any IRE_INTERFACE with the disappearing source address.
14894  */
14895 static void
14896 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
14897 {
14898 	ipif_t	*ipif = (ipif_t *)ipif_arg;
14899 
14900 	ASSERT(IAM_WRITER_IPIF(ipif));
14901 	if (ire->ire_ipif == NULL)
14902 		return;
14903 
14904 	if (ire->ire_ipif != ipif) {
14905 		/*
14906 		 * Look for a matching source address.
14907 		 */
14908 		if (ire->ire_type != IRE_CACHE)
14909 			return;
14910 		if (ipif->ipif_flags & IPIF_NOLOCAL)
14911 			return;
14912 
14913 		if (ire->ire_ipversion == IPV4_VERSION) {
14914 			if (ire->ire_src_addr != ipif->ipif_src_addr)
14915 				return;
14916 		} else {
14917 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
14918 			    &ipif->ipif_v6lcl_addr))
14919 				return;
14920 		}
14921 		ire_delete(ire);
14922 		return;
14923 	}
14924 	/*
14925 	 * ire_delete() will do an ire_flush_cache which will delete
14926 	 * all ire_ipif matches
14927 	 */
14928 	ire_delete(ire);
14929 }
14930 
14931 /*
14932  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
14933  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
14934  * 2) when an interface is brought up or down (on that ill).
14935  * This ensures that the IRE_CACHE entries don't retain stale source
14936  * address selection results.
14937  */
14938 void
14939 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
14940 {
14941 	ill_t	*ill = (ill_t *)ill_arg;
14942 
14943 	ASSERT(IAM_WRITER_ILL(ill));
14944 	ASSERT(ire->ire_type == IRE_CACHE);
14945 
14946 	/*
14947 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14948 	 * ill, but we only want to delete the IRE if ire_ipif matches.
14949 	 */
14950 	ASSERT(ire->ire_ipif != NULL);
14951 	if (ill == ire->ire_ipif->ipif_ill)
14952 		ire_delete(ire);
14953 }
14954 
14955 /*
14956  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
14957  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
14958  * the IPMP ill.
14959  */
14960 void
14961 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
14962 {
14963 	ill_t	*ill = (ill_t *)ill_arg;
14964 
14965 	ASSERT(IAM_WRITER_ILL(ill));
14966 	ASSERT(ire->ire_type == IRE_CACHE);
14967 
14968 	/*
14969 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14970 	 * ill, but we only want to delete the IRE if ire_stq matches.
14971 	 */
14972 	if (ire->ire_stq->q_ptr == ill_arg)
14973 		ire_delete(ire);
14974 }
14975 
14976 /*
14977  * Delete all the IREs whose ire_stq's reference any ill in the same IPMP
14978  * group as `ill_arg'.  Used by ipmp_ill_deactivate() to flush all IRE_CACHE
14979  * entries for the illgrp.
14980  */
14981 void
14982 ill_grp_cache_delete(ire_t *ire, char *ill_arg)
14983 {
14984 	ill_t	*ill = (ill_t *)ill_arg;
14985 
14986 	ASSERT(IAM_WRITER_ILL(ill));
14987 
14988 	if (ire->ire_type == IRE_CACHE &&
14989 	    IS_IN_SAME_ILLGRP((ill_t *)ire->ire_stq->q_ptr, ill)) {
14990 		ire_delete(ire);
14991 	}
14992 }
14993 
14994 /*
14995  * Delete all broadcast IREs with a source address on `ill_arg'.
14996  */
14997 static void
14998 ill_broadcast_delete(ire_t *ire, char *ill_arg)
14999 {
15000 	ill_t *ill = (ill_t *)ill_arg;
15001 
15002 	ASSERT(IAM_WRITER_ILL(ill));
15003 	ASSERT(ire->ire_type == IRE_BROADCAST);
15004 
15005 	if (ire->ire_ipif->ipif_ill == ill)
15006 		ire_delete(ire);
15007 }
15008 
15009 /*
15010  * Initiate deallocate of an IPIF. Always called as writer. Called by
15011  * ill_delete or ip_sioctl_removeif.
15012  */
15013 static void
15014 ipif_free(ipif_t *ipif)
15015 {
15016 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15017 
15018 	ASSERT(IAM_WRITER_IPIF(ipif));
15019 
15020 	if (ipif->ipif_recovery_id != 0)
15021 		(void) untimeout(ipif->ipif_recovery_id);
15022 	ipif->ipif_recovery_id = 0;
15023 
15024 	/* Remove conn references */
15025 	reset_conn_ipif(ipif);
15026 
15027 	/*
15028 	 * Make sure we have valid net and subnet broadcast ire's for the
15029 	 * other ipif's which share them with this ipif.
15030 	 */
15031 	if (!ipif->ipif_isv6)
15032 		ipif_check_bcast_ires(ipif);
15033 
15034 	/*
15035 	 * Take down the interface. We can be called either from ill_delete
15036 	 * or from ip_sioctl_removeif.
15037 	 */
15038 	(void) ipif_down(ipif, NULL, NULL);
15039 
15040 	/*
15041 	 * Now that the interface is down, there's no chance it can still
15042 	 * become a duplicate.  Cancel any timer that may have been set while
15043 	 * tearing down.
15044 	 */
15045 	if (ipif->ipif_recovery_id != 0)
15046 		(void) untimeout(ipif->ipif_recovery_id);
15047 	ipif->ipif_recovery_id = 0;
15048 
15049 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15050 	/* Remove pointers to this ill in the multicast routing tables */
15051 	reset_mrt_vif_ipif(ipif);
15052 	/* If necessary, clear the cached source ipif rotor. */
15053 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15054 		ipif->ipif_ill->ill_src_ipif = NULL;
15055 	rw_exit(&ipst->ips_ill_g_lock);
15056 }
15057 
15058 static void
15059 ipif_free_tail(ipif_t *ipif)
15060 {
15061 	mblk_t	*mp;
15062 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15063 
15064 	/*
15065 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15066 	 */
15067 	mutex_enter(&ipif->ipif_saved_ire_lock);
15068 	mp = ipif->ipif_saved_ire_mp;
15069 	ipif->ipif_saved_ire_mp = NULL;
15070 	mutex_exit(&ipif->ipif_saved_ire_lock);
15071 	freemsg(mp);
15072 
15073 	/*
15074 	 * Need to hold both ill_g_lock and ill_lock while
15075 	 * inserting or removing an ipif from the linked list
15076 	 * of ipifs hanging off the ill.
15077 	 */
15078 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15079 
15080 	ASSERT(ilm_walk_ipif(ipif) == 0);
15081 
15082 #ifdef DEBUG
15083 	ipif_trace_cleanup(ipif);
15084 #endif
15085 
15086 	/* Ask SCTP to take it out of it list */
15087 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15088 
15089 	/* Get it out of the ILL interface list. */
15090 	ipif_remove(ipif);
15091 	rw_exit(&ipst->ips_ill_g_lock);
15092 
15093 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15094 
15095 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15096 	ASSERT(ipif->ipif_recovery_id == 0);
15097 
15098 	/* Free the memory. */
15099 	mi_free(ipif);
15100 }
15101 
15102 /*
15103  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15104  * is zero.
15105  */
15106 void
15107 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15108 {
15109 	char	lbuf[LIFNAMSIZ];
15110 	char	*name;
15111 	size_t	name_len;
15112 
15113 	buf[0] = '\0';
15114 	name = ipif->ipif_ill->ill_name;
15115 	name_len = ipif->ipif_ill->ill_name_length;
15116 	if (ipif->ipif_id != 0) {
15117 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15118 		    ipif->ipif_id);
15119 		name = lbuf;
15120 		name_len = mi_strlen(name) + 1;
15121 	}
15122 	len -= 1;
15123 	buf[len] = '\0';
15124 	len = MIN(len, name_len);
15125 	bcopy(name, buf, len);
15126 }
15127 
15128 /*
15129  * Find an IPIF based on the name passed in.  Names can be of the
15130  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15131  * The <phys> string can have forms like <dev><#> (e.g., le0),
15132  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15133  * When there is no colon, the implied unit id is zero. <phys> must
15134  * correspond to the name of an ILL.  (May be called as writer.)
15135  */
15136 static ipif_t *
15137 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15138     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15139     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15140 {
15141 	char	*cp;
15142 	char	*endp;
15143 	long	id;
15144 	ill_t	*ill;
15145 	ipif_t	*ipif;
15146 	uint_t	ire_type;
15147 	boolean_t did_alloc = B_FALSE;
15148 	ipsq_t	*ipsq;
15149 
15150 	if (error != NULL)
15151 		*error = 0;
15152 
15153 	/*
15154 	 * If the caller wants to us to create the ipif, make sure we have a
15155 	 * valid zoneid
15156 	 */
15157 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15158 
15159 	if (namelen == 0) {
15160 		if (error != NULL)
15161 			*error = ENXIO;
15162 		return (NULL);
15163 	}
15164 
15165 	*exists = B_FALSE;
15166 	/* Look for a colon in the name. */
15167 	endp = &name[namelen];
15168 	for (cp = endp; --cp > name; ) {
15169 		if (*cp == IPIF_SEPARATOR_CHAR)
15170 			break;
15171 	}
15172 
15173 	if (*cp == IPIF_SEPARATOR_CHAR) {
15174 		/*
15175 		 * Reject any non-decimal aliases for logical
15176 		 * interfaces. Aliases with leading zeroes
15177 		 * are also rejected as they introduce ambiguity
15178 		 * in the naming of the interfaces.
15179 		 * In order to confirm with existing semantics,
15180 		 * and to not break any programs/script relying
15181 		 * on that behaviour, if<0>:0 is considered to be
15182 		 * a valid interface.
15183 		 *
15184 		 * If alias has two or more digits and the first
15185 		 * is zero, fail.
15186 		 */
15187 		if (&cp[2] < endp && cp[1] == '0') {
15188 			if (error != NULL)
15189 				*error = EINVAL;
15190 			return (NULL);
15191 		}
15192 	}
15193 
15194 	if (cp <= name) {
15195 		cp = endp;
15196 	} else {
15197 		*cp = '\0';
15198 	}
15199 
15200 	/*
15201 	 * Look up the ILL, based on the portion of the name
15202 	 * before the slash. ill_lookup_on_name returns a held ill.
15203 	 * Temporary to check whether ill exists already. If so
15204 	 * ill_lookup_on_name will clear it.
15205 	 */
15206 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15207 	    q, mp, func, error, &did_alloc, ipst);
15208 	if (cp != endp)
15209 		*cp = IPIF_SEPARATOR_CHAR;
15210 	if (ill == NULL)
15211 		return (NULL);
15212 
15213 	/* Establish the unit number in the name. */
15214 	id = 0;
15215 	if (cp < endp && *endp == '\0') {
15216 		/* If there was a colon, the unit number follows. */
15217 		cp++;
15218 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15219 			ill_refrele(ill);
15220 			if (error != NULL)
15221 				*error = ENXIO;
15222 			return (NULL);
15223 		}
15224 	}
15225 
15226 	GRAB_CONN_LOCK(q);
15227 	mutex_enter(&ill->ill_lock);
15228 	/* Now see if there is an IPIF with this unit number. */
15229 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15230 		if (ipif->ipif_id == id) {
15231 			if (zoneid != ALL_ZONES &&
15232 			    zoneid != ipif->ipif_zoneid &&
15233 			    ipif->ipif_zoneid != ALL_ZONES) {
15234 				mutex_exit(&ill->ill_lock);
15235 				RELEASE_CONN_LOCK(q);
15236 				ill_refrele(ill);
15237 				if (error != NULL)
15238 					*error = ENXIO;
15239 				return (NULL);
15240 			}
15241 			/*
15242 			 * The block comment at the start of ipif_down
15243 			 * explains the use of the macros used below
15244 			 */
15245 			if (IPIF_CAN_LOOKUP(ipif)) {
15246 				ipif_refhold_locked(ipif);
15247 				mutex_exit(&ill->ill_lock);
15248 				if (!did_alloc)
15249 					*exists = B_TRUE;
15250 				/*
15251 				 * Drop locks before calling ill_refrele
15252 				 * since it can potentially call into
15253 				 * ipif_ill_refrele_tail which can end up
15254 				 * in trying to acquire any lock.
15255 				 */
15256 				RELEASE_CONN_LOCK(q);
15257 				ill_refrele(ill);
15258 				return (ipif);
15259 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15260 				ipsq = ill->ill_phyint->phyint_ipsq;
15261 				mutex_enter(&ipsq->ipsq_lock);
15262 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15263 				mutex_exit(&ill->ill_lock);
15264 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15265 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15266 				mutex_exit(&ipsq->ipsq_lock);
15267 				RELEASE_CONN_LOCK(q);
15268 				ill_refrele(ill);
15269 				if (error != NULL)
15270 					*error = EINPROGRESS;
15271 				return (NULL);
15272 			}
15273 		}
15274 	}
15275 	RELEASE_CONN_LOCK(q);
15276 
15277 	if (!do_alloc) {
15278 		mutex_exit(&ill->ill_lock);
15279 		ill_refrele(ill);
15280 		if (error != NULL)
15281 			*error = ENXIO;
15282 		return (NULL);
15283 	}
15284 
15285 	/*
15286 	 * If none found, atomically allocate and return a new one.
15287 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15288 	 * to support "receive only" use of lo0:1 etc. as is still done
15289 	 * below as an initial guess.
15290 	 * However, this is now likely to be overriden later in ipif_up_done()
15291 	 * when we know for sure what address has been configured on the
15292 	 * interface, since we might have more than one loopback interface
15293 	 * with a loopback address, e.g. in the case of zones, and all the
15294 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15295 	 */
15296 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15297 		ire_type = IRE_LOOPBACK;
15298 	else
15299 		ire_type = IRE_LOCAL;
15300 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15301 	if (ipif != NULL)
15302 		ipif_refhold_locked(ipif);
15303 	else if (error != NULL)
15304 		*error = ENOMEM;
15305 	mutex_exit(&ill->ill_lock);
15306 	ill_refrele(ill);
15307 	return (ipif);
15308 }
15309 
15310 /*
15311  * This routine is called whenever a new address comes up on an ipif.  If
15312  * we are configured to respond to address mask requests, then we are supposed
15313  * to broadcast an address mask reply at this time.  This routine is also
15314  * called if we are already up, but a netmask change is made.  This is legal
15315  * but might not make the system manager very popular.	(May be called
15316  * as writer.)
15317  */
15318 void
15319 ipif_mask_reply(ipif_t *ipif)
15320 {
15321 	icmph_t	*icmph;
15322 	ipha_t	*ipha;
15323 	mblk_t	*mp;
15324 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15325 
15326 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15327 
15328 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15329 		return;
15330 
15331 	/* ICMP mask reply is IPv4 only */
15332 	ASSERT(!ipif->ipif_isv6);
15333 	/* ICMP mask reply is not for a loopback interface */
15334 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15335 
15336 	mp = allocb(REPLY_LEN, BPRI_HI);
15337 	if (mp == NULL)
15338 		return;
15339 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15340 
15341 	ipha = (ipha_t *)mp->b_rptr;
15342 	bzero(ipha, REPLY_LEN);
15343 	*ipha = icmp_ipha;
15344 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15345 	ipha->ipha_src = ipif->ipif_src_addr;
15346 	ipha->ipha_dst = ipif->ipif_brd_addr;
15347 	ipha->ipha_length = htons(REPLY_LEN);
15348 	ipha->ipha_ident = 0;
15349 
15350 	icmph = (icmph_t *)&ipha[1];
15351 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15352 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15353 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15354 
15355 	put(ipif->ipif_wq, mp);
15356 
15357 #undef	REPLY_LEN
15358 }
15359 
15360 /*
15361  * When the mtu in the ipif changes, we call this routine through ire_walk
15362  * to update all the relevant IREs.
15363  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15364  */
15365 static void
15366 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15367 {
15368 	ipif_t *ipif = (ipif_t *)ipif_arg;
15369 
15370 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15371 		return;
15372 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15373 }
15374 
15375 /*
15376  * When the mtu in the ill changes, we call this routine through ire_walk
15377  * to update all the relevant IREs.
15378  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15379  */
15380 void
15381 ill_mtu_change(ire_t *ire, char *ill_arg)
15382 {
15383 	ill_t	*ill = (ill_t *)ill_arg;
15384 
15385 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15386 		return;
15387 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
15388 }
15389 
15390 /*
15391  * Join the ipif specific multicast groups.
15392  * Must be called after a mapping has been set up in the resolver.  (Always
15393  * called as writer.)
15394  */
15395 void
15396 ipif_multicast_up(ipif_t *ipif)
15397 {
15398 	int err;
15399 	ill_t *ill;
15400 
15401 	ASSERT(IAM_WRITER_IPIF(ipif));
15402 
15403 	ill = ipif->ipif_ill;
15404 
15405 	ip1dbg(("ipif_multicast_up\n"));
15406 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15407 		return;
15408 
15409 	if (ipif->ipif_isv6) {
15410 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15411 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15412 
15413 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15414 
15415 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15416 			return;
15417 
15418 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15419 
15420 		/*
15421 		 * Join the all hosts multicast address.  We skip this for
15422 		 * underlying IPMP interfaces since they should be invisible.
15423 		 */
15424 		if (!IS_UNDER_IPMP(ill)) {
15425 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15426 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15427 			if (err != 0) {
15428 				ip0dbg(("ipif_multicast_up: "
15429 				    "all_hosts_mcast failed %d\n", err));
15430 				return;
15431 			}
15432 			ipif->ipif_joined_allhosts = 1;
15433 		}
15434 
15435 		/*
15436 		 * Enable multicast for the solicited node multicast address
15437 		 */
15438 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15439 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15440 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15441 			if (err != 0) {
15442 				ip0dbg(("ipif_multicast_up: solicited MC"
15443 				    " failed %d\n", err));
15444 				if (ipif->ipif_joined_allhosts) {
15445 					(void) ip_delmulti_v6(&v6allmc, ill,
15446 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15447 					ipif->ipif_joined_allhosts = 0;
15448 				}
15449 				return;
15450 			}
15451 		}
15452 	} else {
15453 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15454 			return;
15455 
15456 		/* Join the all hosts multicast address */
15457 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15458 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15459 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15460 		if (err) {
15461 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15462 			return;
15463 		}
15464 	}
15465 	ipif->ipif_multicast_up = 1;
15466 }
15467 
15468 /*
15469  * Blow away any multicast groups that we joined in ipif_multicast_up().
15470  * (Explicit memberships are blown away in ill_leave_multicast() when the
15471  * ill is brought down.)
15472  */
15473 void
15474 ipif_multicast_down(ipif_t *ipif)
15475 {
15476 	int err;
15477 
15478 	ASSERT(IAM_WRITER_IPIF(ipif));
15479 
15480 	ip1dbg(("ipif_multicast_down\n"));
15481 	if (!ipif->ipif_multicast_up)
15482 		return;
15483 
15484 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15485 
15486 	if (!ipif->ipif_isv6) {
15487 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15488 		    B_TRUE);
15489 		if (err != 0)
15490 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15491 
15492 		ipif->ipif_multicast_up = 0;
15493 		return;
15494 	}
15495 
15496 	/*
15497 	 * Leave the all-hosts multicast address.
15498 	 */
15499 	if (ipif->ipif_joined_allhosts) {
15500 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15501 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15502 		if (err != 0) {
15503 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15504 			    "failed %d\n", err));
15505 		}
15506 		ipif->ipif_joined_allhosts = 0;
15507 	}
15508 
15509 	/*
15510 	 * Disable multicast for the solicited node multicast address
15511 	 */
15512 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15513 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15514 
15515 		ipv6_multi.s6_addr32[3] |=
15516 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15517 
15518 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15519 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15520 		if (err != 0) {
15521 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15522 			    err));
15523 		}
15524 	}
15525 
15526 	ipif->ipif_multicast_up = 0;
15527 }
15528 
15529 /*
15530  * Used when an interface comes up to recreate any extra routes on this
15531  * interface.
15532  */
15533 static ire_t **
15534 ipif_recover_ire(ipif_t *ipif)
15535 {
15536 	mblk_t	*mp;
15537 	ire_t	**ipif_saved_irep;
15538 	ire_t	**irep;
15539 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15540 
15541 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15542 	    ipif->ipif_id));
15543 
15544 	mutex_enter(&ipif->ipif_saved_ire_lock);
15545 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15546 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15547 	if (ipif_saved_irep == NULL) {
15548 		mutex_exit(&ipif->ipif_saved_ire_lock);
15549 		return (NULL);
15550 	}
15551 
15552 	irep = ipif_saved_irep;
15553 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15554 		ire_t		*ire;
15555 		queue_t		*rfq;
15556 		queue_t		*stq;
15557 		ifrt_t		*ifrt;
15558 		uchar_t		*src_addr;
15559 		uchar_t		*gateway_addr;
15560 		ushort_t	type;
15561 
15562 		/*
15563 		 * When the ire was initially created and then added in
15564 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15565 		 * in the case of a traditional interface route, or as one of
15566 		 * the IRE_OFFSUBNET types (with the exception of
15567 		 * IRE_HOST types ire which is created by icmp_redirect() and
15568 		 * which we don't need to save or recover).  In the case where
15569 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15570 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15571 		 * to satisfy software like GateD and Sun Cluster which creates
15572 		 * routes using the the loopback interface's address as a
15573 		 * gateway.
15574 		 *
15575 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15576 		 * ire_create() will be called in the same way here as
15577 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15578 		 * the route looks like a traditional interface route (where
15579 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15580 		 * the saved ifrt->ifrt_type.  This means that in the case where
15581 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15582 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15583 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15584 		 */
15585 		ifrt = (ifrt_t *)mp->b_rptr;
15586 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15587 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15588 			rfq = NULL;
15589 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15590 			    ? ipif->ipif_rq : ipif->ipif_wq;
15591 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15592 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15593 			    : (uint8_t *)&ipif->ipif_src_addr;
15594 			gateway_addr = NULL;
15595 			type = ipif->ipif_net_type;
15596 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15597 			/* Recover multiroute broadcast IRE. */
15598 			rfq = ipif->ipif_rq;
15599 			stq = ipif->ipif_wq;
15600 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15601 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15602 			    : (uint8_t *)&ipif->ipif_src_addr;
15603 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15604 			type = ifrt->ifrt_type;
15605 		} else {
15606 			rfq = NULL;
15607 			stq = NULL;
15608 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15609 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15610 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15611 			type = ifrt->ifrt_type;
15612 		}
15613 
15614 		/*
15615 		 * Create a copy of the IRE with the saved address and netmask.
15616 		 */
15617 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15618 		    "0x%x/0x%x\n",
15619 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15620 		    ntohl(ifrt->ifrt_addr),
15621 		    ntohl(ifrt->ifrt_mask)));
15622 		ire = ire_create(
15623 		    (uint8_t *)&ifrt->ifrt_addr,
15624 		    (uint8_t *)&ifrt->ifrt_mask,
15625 		    src_addr,
15626 		    gateway_addr,
15627 		    &ifrt->ifrt_max_frag,
15628 		    NULL,
15629 		    rfq,
15630 		    stq,
15631 		    type,
15632 		    ipif,
15633 		    0,
15634 		    0,
15635 		    0,
15636 		    ifrt->ifrt_flags,
15637 		    &ifrt->ifrt_iulp_info,
15638 		    NULL,
15639 		    NULL,
15640 		    ipst);
15641 
15642 		if (ire == NULL) {
15643 			mutex_exit(&ipif->ipif_saved_ire_lock);
15644 			kmem_free(ipif_saved_irep,
15645 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15646 			return (NULL);
15647 		}
15648 
15649 		/*
15650 		 * Some software (for example, GateD and Sun Cluster) attempts
15651 		 * to create (what amount to) IRE_PREFIX routes with the
15652 		 * loopback address as the gateway.  This is primarily done to
15653 		 * set up prefixes with the RTF_REJECT flag set (for example,
15654 		 * when generating aggregate routes.)
15655 		 *
15656 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15657 		 * IRE_LOOPBACK, then we map the request into a
15658 		 * IRE_IF_NORESOLVER.
15659 		 */
15660 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15661 			ire->ire_type = IRE_IF_NORESOLVER;
15662 		/*
15663 		 * ire held by ire_add, will be refreled' towards the
15664 		 * the end of ipif_up_done
15665 		 */
15666 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15667 		*irep = ire;
15668 		irep++;
15669 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15670 	}
15671 	mutex_exit(&ipif->ipif_saved_ire_lock);
15672 	return (ipif_saved_irep);
15673 }
15674 
15675 /*
15676  * Used to set the netmask and broadcast address to default values when the
15677  * interface is brought up.  (Always called as writer.)
15678  */
15679 static void
15680 ipif_set_default(ipif_t *ipif)
15681 {
15682 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15683 
15684 	if (!ipif->ipif_isv6) {
15685 		/*
15686 		 * Interface holds an IPv4 address. Default
15687 		 * mask is the natural netmask.
15688 		 */
15689 		if (!ipif->ipif_net_mask) {
15690 			ipaddr_t	v4mask;
15691 
15692 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15693 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15694 		}
15695 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15696 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15697 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15698 		} else {
15699 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15700 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15701 		}
15702 		/*
15703 		 * NOTE: SunOS 4.X does this even if the broadcast address
15704 		 * has been already set thus we do the same here.
15705 		 */
15706 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15707 			ipaddr_t	v4addr;
15708 
15709 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15710 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15711 		}
15712 	} else {
15713 		/*
15714 		 * Interface holds an IPv6-only address.  Default
15715 		 * mask is all-ones.
15716 		 */
15717 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15718 			ipif->ipif_v6net_mask = ipv6_all_ones;
15719 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15720 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15721 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15722 		} else {
15723 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15724 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15725 		}
15726 	}
15727 }
15728 
15729 /*
15730  * Return 0 if this address can be used as local address without causing
15731  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15732  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15733  * Note that the same IPv6 link-local address is allowed as long as the ills
15734  * are not on the same link.
15735  */
15736 int
15737 ip_addr_availability_check(ipif_t *new_ipif)
15738 {
15739 	in6_addr_t our_v6addr;
15740 	ill_t *ill;
15741 	ipif_t *ipif;
15742 	ill_walk_context_t ctx;
15743 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15744 
15745 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15746 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15747 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15748 
15749 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15750 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15751 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15752 		return (0);
15753 
15754 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15755 
15756 	if (new_ipif->ipif_isv6)
15757 		ill = ILL_START_WALK_V6(&ctx, ipst);
15758 	else
15759 		ill = ILL_START_WALK_V4(&ctx, ipst);
15760 
15761 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15762 		for (ipif = ill->ill_ipif; ipif != NULL;
15763 		    ipif = ipif->ipif_next) {
15764 			if ((ipif == new_ipif) ||
15765 			    !(ipif->ipif_flags & IPIF_UP) ||
15766 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15767 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15768 			    &our_v6addr))
15769 				continue;
15770 
15771 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15772 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15773 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15774 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15775 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15776 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15777 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15778 				continue;
15779 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15780 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15781 				continue;
15782 			else if (new_ipif->ipif_ill == ill)
15783 				return (EADDRINUSE);
15784 			else
15785 				return (EADDRNOTAVAIL);
15786 		}
15787 	}
15788 
15789 	return (0);
15790 }
15791 
15792 /*
15793  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15794  * IREs for the ipif.
15795  * When the routine returns EINPROGRESS then mp has been consumed and
15796  * the ioctl will be acked from ip_rput_dlpi.
15797  */
15798 int
15799 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15800 {
15801 	ill_t		*ill = ipif->ipif_ill;
15802 	boolean_t 	isv6 = ipif->ipif_isv6;
15803 	int		err = 0;
15804 	boolean_t	success;
15805 	uint_t		ipif_orig_id;
15806 	ip_stack_t	*ipst = ill->ill_ipst;
15807 
15808 	ASSERT(IAM_WRITER_IPIF(ipif));
15809 
15810 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15811 
15812 	/* Shouldn't get here if it is already up. */
15813 	if (ipif->ipif_flags & IPIF_UP)
15814 		return (EALREADY);
15815 
15816 	/*
15817 	 * If this is a request to bring up a data address on an interface
15818 	 * under IPMP, then move the address to its IPMP meta-interface and
15819 	 * try to bring it up.  One complication is that the zeroth ipif for
15820 	 * an ill is special, in that every ill always has one, and that code
15821 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15822 	 */
15823 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15824 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15825 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15826 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15827 
15828 		/*
15829 		 * The ipif being brought up should be quiesced.  If it's not,
15830 		 * something has gone amiss and we need to bail out.  (If it's
15831 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15832 		 */
15833 		mutex_enter(&ill->ill_lock);
15834 		if (!ipif_is_quiescent(ipif)) {
15835 			mutex_exit(&ill->ill_lock);
15836 			return (EINVAL);
15837 		}
15838 		mutex_exit(&ill->ill_lock);
15839 
15840 		/*
15841 		 * If we're going to need to allocate ipifs, do it prior
15842 		 * to starting the move (and grabbing locks).
15843 		 */
15844 		if (ipif->ipif_id == 0) {
15845 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15846 			    B_FALSE);
15847 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15848 			    B_FALSE);
15849 			if (moveipif == NULL || stubipif == NULL) {
15850 				mi_free(moveipif);
15851 				mi_free(stubipif);
15852 				return (ENOMEM);
15853 			}
15854 		}
15855 
15856 		/*
15857 		 * Grab or transfer the ipif to move.  During the move, keep
15858 		 * ill_g_lock held to prevent any ill walker threads from
15859 		 * seeing things in an inconsistent state.
15860 		 */
15861 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15862 		if (ipif->ipif_id != 0) {
15863 			ipif_remove(ipif);
15864 		} else {
15865 			ipif_transfer(ipif, moveipif, stubipif);
15866 			ipif = moveipif;
15867 		}
15868 
15869 		/*
15870 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
15871 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
15872 		 * replace that one.  Otherwise, pick the next available slot.
15873 		 */
15874 		ipif->ipif_ill = ipmp_ill;
15875 		ipif_orig_id = ipif->ipif_id;
15876 
15877 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
15878 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
15879 			ipif = ipmp_ill->ill_ipif;
15880 		} else {
15881 			ipif->ipif_id = -1;
15882 			if (ipif_insert(ipif, B_FALSE) != 0) {
15883 				/*
15884 				 * No more available ipif_id's -- put it back
15885 				 * on the original ill and fail the operation.
15886 				 * Since we're writer on the ill, we can be
15887 				 * sure our old slot is still available.
15888 				 */
15889 				ipif->ipif_id = ipif_orig_id;
15890 				ipif->ipif_ill = ill;
15891 				if (ipif_orig_id == 0) {
15892 					ipif_transfer(ipif, ill->ill_ipif,
15893 					    NULL);
15894 				} else {
15895 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
15896 				}
15897 				rw_exit(&ipst->ips_ill_g_lock);
15898 				return (ENOMEM);
15899 			}
15900 		}
15901 		rw_exit(&ipst->ips_ill_g_lock);
15902 
15903 		/*
15904 		 * Tell SCTP that the ipif has moved.  Note that even if we
15905 		 * had to allocate a new ipif, the original sequence id was
15906 		 * preserved and therefore SCTP won't know.
15907 		 */
15908 		sctp_move_ipif(ipif, ill, ipmp_ill);
15909 
15910 		/*
15911 		 * If the ipif being brought up was on slot zero, then we
15912 		 * first need to bring up the placeholder we stuck there.  In
15913 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
15914 		 * to ipif_up() itself, if we successfully bring up the
15915 		 * placeholder, we'll check ill_move_ipif and bring it up too.
15916 		 */
15917 		if (ipif_orig_id == 0) {
15918 			ASSERT(ill->ill_move_ipif == NULL);
15919 			ill->ill_move_ipif = ipif;
15920 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
15921 				ASSERT(ill->ill_move_ipif == NULL);
15922 			if (err != EINPROGRESS)
15923 				ill->ill_move_ipif = NULL;
15924 			return (err);
15925 		}
15926 
15927 		/*
15928 		 * Bring it up on the IPMP ill.
15929 		 */
15930 		return (ipif_up(ipif, q, mp));
15931 	}
15932 
15933 	/* Skip arp/ndp for any loopback interface. */
15934 	if (ill->ill_wq != NULL) {
15935 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
15936 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
15937 
15938 		if (!ill->ill_dl_up) {
15939 			/*
15940 			 * ill_dl_up is not yet set. i.e. we are yet to
15941 			 * DL_BIND with the driver and this is the first
15942 			 * logical interface on the ill to become "up".
15943 			 * Tell the driver to get going (via DL_BIND_REQ).
15944 			 * Note that changing "significant" IFF_ flags
15945 			 * address/netmask etc cause a down/up dance, but
15946 			 * does not cause an unbind (DL_UNBIND) with the driver
15947 			 */
15948 			return (ill_dl_up(ill, ipif, mp, q));
15949 		}
15950 
15951 		/*
15952 		 * ipif_resolver_up may end up sending an
15953 		 * AR_INTERFACE_UP message to ARP, which would, in
15954 		 * turn send a DLPI message to the driver. ioctls are
15955 		 * serialized and so we cannot send more than one
15956 		 * interface up message at a time. If ipif_resolver_up
15957 		 * does send an interface up message to ARP, we get
15958 		 * EINPROGRESS and we will complete in ip_arp_done.
15959 		 */
15960 
15961 		ASSERT(connp != NULL || !CONN_Q(q));
15962 		if (connp != NULL)
15963 			mutex_enter(&connp->conn_lock);
15964 		mutex_enter(&ill->ill_lock);
15965 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
15966 		mutex_exit(&ill->ill_lock);
15967 		if (connp != NULL)
15968 			mutex_exit(&connp->conn_lock);
15969 		if (!success)
15970 			return (EINTR);
15971 
15972 		/*
15973 		 * Crank up the resolver.  For IPv6, this cranks up the
15974 		 * external resolver if one is configured, but even if an
15975 		 * external resolver isn't configured, it must be called to
15976 		 * reset DAD state.  For IPv6, if an external resolver is not
15977 		 * being used, ipif_resolver_up() will never return
15978 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
15979 		 * Note that if an external resolver is being used, there's no
15980 		 * need to call ipif_ndp_up() since it will do nothing.
15981 		 */
15982 		err = ipif_resolver_up(ipif, Res_act_initial);
15983 		if (err == EINPROGRESS) {
15984 			/* We will complete it in ip_arp_done() */
15985 			return (err);
15986 		}
15987 
15988 		if (isv6 && err == 0)
15989 			err = ipif_ndp_up(ipif, B_TRUE);
15990 
15991 		ASSERT(err != EINPROGRESS);
15992 		mp = ipsq_pending_mp_get(ipsq, &connp);
15993 		ASSERT(mp != NULL);
15994 		if (err != 0)
15995 			return (err);
15996 	} else {
15997 		/*
15998 		 * Interfaces without underlying hardware don't do duplicate
15999 		 * address detection.
16000 		 */
16001 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16002 		ipif->ipif_addr_ready = 1;
16003 	}
16004 
16005 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16006 	if (err == 0 && ill->ill_move_ipif != NULL) {
16007 		ipif = ill->ill_move_ipif;
16008 		ill->ill_move_ipif = NULL;
16009 		return (ipif_up(ipif, q, mp));
16010 	}
16011 	return (err);
16012 }
16013 
16014 /*
16015  * Perform a bind for the physical device.
16016  * When the routine returns EINPROGRESS then mp has been consumed and
16017  * the ioctl will be acked from ip_rput_dlpi.
16018  * Allocate an unbind message and save it until ipif_down.
16019  */
16020 static int
16021 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16022 {
16023 	areq_t	*areq;
16024 	mblk_t	*areq_mp = NULL;
16025 	mblk_t	*bind_mp = NULL;
16026 	mblk_t	*unbind_mp = NULL;
16027 	conn_t	*connp;
16028 	boolean_t success;
16029 	uint16_t sap_addr;
16030 
16031 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16032 	ASSERT(IAM_WRITER_ILL(ill));
16033 	ASSERT(mp != NULL);
16034 
16035 	/* Create a resolver cookie for ARP */
16036 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16037 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16038 		if (areq_mp == NULL)
16039 			return (ENOMEM);
16040 
16041 		freemsg(ill->ill_resolver_mp);
16042 		ill->ill_resolver_mp = areq_mp;
16043 		areq = (areq_t *)areq_mp->b_rptr;
16044 		sap_addr = ill->ill_sap;
16045 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16046 	}
16047 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16048 	    DL_BIND_REQ);
16049 	if (bind_mp == NULL)
16050 		goto bad;
16051 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16052 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16053 
16054 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16055 	if (unbind_mp == NULL)
16056 		goto bad;
16057 
16058 	/*
16059 	 * Record state needed to complete this operation when the
16060 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16061 	 */
16062 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16063 	ASSERT(connp != NULL || !CONN_Q(q));
16064 	GRAB_CONN_LOCK(q);
16065 	mutex_enter(&ipif->ipif_ill->ill_lock);
16066 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16067 	mutex_exit(&ipif->ipif_ill->ill_lock);
16068 	RELEASE_CONN_LOCK(q);
16069 	if (!success)
16070 		goto bad;
16071 
16072 	/*
16073 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16074 	 * the interface goes down.
16075 	 */
16076 	ASSERT(ill->ill_unbind_mp == NULL);
16077 	ill->ill_unbind_mp = unbind_mp;
16078 
16079 	ill_dlpi_send(ill, bind_mp);
16080 	/* Send down link-layer capabilities probe if not already done. */
16081 	ill_capability_probe(ill);
16082 
16083 	/*
16084 	 * Sysid used to rely on the fact that netboots set domainname
16085 	 * and the like. Now that miniroot boots aren't strictly netboots
16086 	 * and miniroot network configuration is driven from userland
16087 	 * these things still need to be set. This situation can be detected
16088 	 * by comparing the interface being configured here to the one
16089 	 * dhcifname was set to reference by the boot loader. Once sysid is
16090 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16091 	 */
16092 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16093 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16094 	    (strlen(srpc_domain) == 0)) {
16095 		if (dhcpinit() != 0)
16096 			cmn_err(CE_WARN, "no cached dhcp response");
16097 	}
16098 
16099 	/*
16100 	 * This operation will complete in ip_rput_dlpi with either
16101 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16102 	 */
16103 	return (EINPROGRESS);
16104 bad:
16105 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16106 
16107 	freemsg(bind_mp);
16108 	freemsg(unbind_mp);
16109 	return (ENOMEM);
16110 }
16111 
16112 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16113 
16114 /*
16115  * DLPI and ARP is up.
16116  * Create all the IREs associated with an interface bring up multicast.
16117  * Set the interface flag and finish other initialization
16118  * that potentially had to be differed to after DL_BIND_ACK.
16119  */
16120 int
16121 ipif_up_done(ipif_t *ipif)
16122 {
16123 	ire_t	*ire_array[20];
16124 	ire_t	**irep = ire_array;
16125 	ire_t	**irep1;
16126 	ipaddr_t net_mask = 0;
16127 	ipaddr_t subnet_mask, route_mask;
16128 	ill_t	*ill = ipif->ipif_ill;
16129 	queue_t	*stq;
16130 	ipif_t	 *src_ipif;
16131 	ipif_t   *tmp_ipif;
16132 	boolean_t	flush_ire_cache = B_TRUE;
16133 	int	err = 0;
16134 	ire_t	**ipif_saved_irep = NULL;
16135 	int ipif_saved_ire_cnt;
16136 	int	cnt;
16137 	boolean_t	src_ipif_held = B_FALSE;
16138 	boolean_t	loopback = B_FALSE;
16139 	ip_stack_t	*ipst = ill->ill_ipst;
16140 
16141 	ip1dbg(("ipif_up_done(%s:%u)\n",
16142 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16143 	/* Check if this is a loopback interface */
16144 	if (ipif->ipif_ill->ill_wq == NULL)
16145 		loopback = B_TRUE;
16146 
16147 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16148 	/*
16149 	 * If all other interfaces for this ill are down or DEPRECATED,
16150 	 * or otherwise unsuitable for source address selection, remove
16151 	 * any IRE_CACHE entries for this ill to make sure source
16152 	 * address selection gets to take this new ipif into account.
16153 	 * No need to hold ill_lock while traversing the ipif list since
16154 	 * we are writer
16155 	 */
16156 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16157 	    tmp_ipif = tmp_ipif->ipif_next) {
16158 		if (((tmp_ipif->ipif_flags &
16159 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16160 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16161 		    (tmp_ipif == ipif))
16162 			continue;
16163 		/* first useable pre-existing interface */
16164 		flush_ire_cache = B_FALSE;
16165 		break;
16166 	}
16167 	if (flush_ire_cache)
16168 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16169 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16170 
16171 	/*
16172 	 * Figure out which way the send-to queue should go.  Only
16173 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16174 	 * should show up here.
16175 	 */
16176 	switch (ill->ill_net_type) {
16177 	case IRE_IF_RESOLVER:
16178 		stq = ill->ill_rq;
16179 		break;
16180 	case IRE_IF_NORESOLVER:
16181 	case IRE_LOOPBACK:
16182 		stq = ill->ill_wq;
16183 		break;
16184 	default:
16185 		return (EINVAL);
16186 	}
16187 
16188 	if (IS_LOOPBACK(ill)) {
16189 		/*
16190 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16191 		 * ipif_lookup_on_name(), but in the case of zones we can have
16192 		 * several loopback addresses on lo0. So all the interfaces with
16193 		 * loopback addresses need to be marked IRE_LOOPBACK.
16194 		 */
16195 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16196 		    htonl(INADDR_LOOPBACK))
16197 			ipif->ipif_ire_type = IRE_LOOPBACK;
16198 		else
16199 			ipif->ipif_ire_type = IRE_LOCAL;
16200 	}
16201 
16202 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16203 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16204 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16205 		/*
16206 		 * Can't use our source address. Select a different
16207 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16208 		 */
16209 		src_ipif = ipif_select_source(ipif->ipif_ill,
16210 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16211 		if (src_ipif == NULL)
16212 			src_ipif = ipif;	/* Last resort */
16213 		else
16214 			src_ipif_held = B_TRUE;
16215 	} else {
16216 		src_ipif = ipif;
16217 	}
16218 
16219 	/* Create all the IREs associated with this interface */
16220 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16221 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16222 
16223 		/*
16224 		 * If we're on a labeled system then make sure that zone-
16225 		 * private addresses have proper remote host database entries.
16226 		 */
16227 		if (is_system_labeled() &&
16228 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16229 		    !tsol_check_interface_address(ipif))
16230 			return (EINVAL);
16231 
16232 		/* Register the source address for __sin6_src_id */
16233 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16234 		    ipif->ipif_zoneid, ipst);
16235 		if (err != 0) {
16236 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16237 			return (err);
16238 		}
16239 
16240 		/* If the interface address is set, create the local IRE. */
16241 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16242 		    (void *)ipif,
16243 		    ipif->ipif_ire_type,
16244 		    ntohl(ipif->ipif_lcl_addr)));
16245 		*irep++ = ire_create(
16246 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16247 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16248 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16249 		    NULL,				/* no gateway */
16250 		    &ip_loopback_mtuplus,		/* max frag size */
16251 		    NULL,
16252 		    ipif->ipif_rq,			/* recv-from queue */
16253 		    NULL,				/* no send-to queue */
16254 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16255 		    ipif,
16256 		    0,
16257 		    0,
16258 		    0,
16259 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16260 		    RTF_PRIVATE : 0,
16261 		    &ire_uinfo_null,
16262 		    NULL,
16263 		    NULL,
16264 		    ipst);
16265 	} else {
16266 		ip1dbg((
16267 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16268 		    ipif->ipif_ire_type,
16269 		    ntohl(ipif->ipif_lcl_addr),
16270 		    (uint_t)ipif->ipif_flags));
16271 	}
16272 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16273 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16274 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16275 	} else {
16276 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16277 	}
16278 
16279 	subnet_mask = ipif->ipif_net_mask;
16280 
16281 	/*
16282 	 * If mask was not specified, use natural netmask of
16283 	 * interface address. Also, store this mask back into the
16284 	 * ipif struct.
16285 	 */
16286 	if (subnet_mask == 0) {
16287 		subnet_mask = net_mask;
16288 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16289 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16290 		    ipif->ipif_v6subnet);
16291 	}
16292 
16293 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16294 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16295 	    ipif->ipif_subnet != INADDR_ANY) {
16296 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16297 
16298 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16299 			route_mask = IP_HOST_MASK;
16300 		} else {
16301 			route_mask = subnet_mask;
16302 		}
16303 
16304 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16305 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16306 		    (void *)ipif, (void *)ill,
16307 		    ill->ill_net_type,
16308 		    ntohl(ipif->ipif_subnet)));
16309 		*irep++ = ire_create(
16310 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16311 		    (uchar_t *)&route_mask,		/* mask */
16312 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16313 		    NULL,				/* no gateway */
16314 		    &ipif->ipif_mtu,			/* max frag */
16315 		    NULL,
16316 		    NULL,				/* no recv queue */
16317 		    stq,				/* send-to queue */
16318 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16319 		    ipif,
16320 		    0,
16321 		    0,
16322 		    0,
16323 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16324 		    &ire_uinfo_null,
16325 		    NULL,
16326 		    NULL,
16327 		    ipst);
16328 	}
16329 
16330 	/*
16331 	 * Create any necessary broadcast IREs.
16332 	 */
16333 	if (ipif->ipif_flags & IPIF_BROADCAST)
16334 		irep = ipif_create_bcast_ires(ipif, irep);
16335 
16336 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16337 
16338 	/* If an earlier ire_create failed, get out now */
16339 	for (irep1 = irep; irep1 > ire_array; ) {
16340 		irep1--;
16341 		if (*irep1 == NULL) {
16342 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16343 			err = ENOMEM;
16344 			goto bad;
16345 		}
16346 	}
16347 
16348 	/*
16349 	 * Need to atomically check for IP address availability under
16350 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16351 	 * ills or new ipifs can be added while we are checking availability.
16352 	 */
16353 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16354 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16355 	/* Mark it up, and increment counters. */
16356 	ipif->ipif_flags |= IPIF_UP;
16357 	ill->ill_ipif_up_count++;
16358 	err = ip_addr_availability_check(ipif);
16359 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16360 	rw_exit(&ipst->ips_ill_g_lock);
16361 
16362 	if (err != 0) {
16363 		/*
16364 		 * Our address may already be up on the same ill. In this case,
16365 		 * the ARP entry for our ipif replaced the one for the other
16366 		 * ipif. So we don't want to delete it (otherwise the other ipif
16367 		 * would be unable to send packets).
16368 		 * ip_addr_availability_check() identifies this case for us and
16369 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16370 		 * which is the expected error code.
16371 		 */
16372 		if (err == EADDRINUSE) {
16373 			freemsg(ipif->ipif_arp_del_mp);
16374 			ipif->ipif_arp_del_mp = NULL;
16375 			err = EADDRNOTAVAIL;
16376 		}
16377 		ill->ill_ipif_up_count--;
16378 		ipif->ipif_flags &= ~IPIF_UP;
16379 		goto bad;
16380 	}
16381 
16382 	/*
16383 	 * Add in all newly created IREs.  ire_create_bcast() has
16384 	 * already checked for duplicates of the IRE_BROADCAST type.
16385 	 */
16386 	for (irep1 = irep; irep1 > ire_array; ) {
16387 		irep1--;
16388 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16389 		/*
16390 		 * refheld by ire_add. refele towards the end of the func
16391 		 */
16392 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16393 	}
16394 
16395 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16396 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16397 	ipif_saved_irep = ipif_recover_ire(ipif);
16398 
16399 	if (!loopback) {
16400 		/*
16401 		 * If the broadcast address has been set, make sure it makes
16402 		 * sense based on the interface address.
16403 		 * Only match on ill since we are sharing broadcast addresses.
16404 		 */
16405 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16406 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16407 			ire_t	*ire;
16408 
16409 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16410 			    IRE_BROADCAST, ipif, ALL_ZONES,
16411 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16412 
16413 			if (ire == NULL) {
16414 				/*
16415 				 * If there isn't a matching broadcast IRE,
16416 				 * revert to the default for this netmask.
16417 				 */
16418 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16419 				mutex_enter(&ipif->ipif_ill->ill_lock);
16420 				ipif_set_default(ipif);
16421 				mutex_exit(&ipif->ipif_ill->ill_lock);
16422 			} else {
16423 				ire_refrele(ire);
16424 			}
16425 		}
16426 
16427 	}
16428 
16429 	if (ill->ill_need_recover_multicast) {
16430 		/*
16431 		 * Need to recover all multicast memberships in the driver.
16432 		 * This had to be deferred until we had attached.  The same
16433 		 * code exists in ipif_up_done_v6() to recover IPv6
16434 		 * memberships.
16435 		 *
16436 		 * Note that it would be preferable to unconditionally do the
16437 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16438 		 * that since ill_join_allmulti() depends on ill_dl_up being
16439 		 * set, and it is not set until we receive a DL_BIND_ACK after
16440 		 * having called ill_dl_up().
16441 		 */
16442 		ill_recover_multicast(ill);
16443 	}
16444 
16445 	if (ill->ill_ipif_up_count == 1) {
16446 		/*
16447 		 * Since the interface is now up, it may now be active.
16448 		 */
16449 		if (IS_UNDER_IPMP(ill))
16450 			ipmp_ill_refresh_active(ill);
16451 
16452 		/*
16453 		 * If this is an IPMP interface, we may now be able to
16454 		 * establish ARP entries.
16455 		 */
16456 		if (IS_IPMP(ill))
16457 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16458 	}
16459 
16460 	/* Join the allhosts multicast address */
16461 	ipif_multicast_up(ipif);
16462 
16463 	/*
16464 	 * See if anybody else would benefit from our new ipif.
16465 	 */
16466 	if (!loopback &&
16467 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16468 		ill_update_source_selection(ill);
16469 	}
16470 
16471 	for (irep1 = irep; irep1 > ire_array; ) {
16472 		irep1--;
16473 		if (*irep1 != NULL) {
16474 			/* was held in ire_add */
16475 			ire_refrele(*irep1);
16476 		}
16477 	}
16478 
16479 	cnt = ipif_saved_ire_cnt;
16480 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16481 		if (*irep1 != NULL) {
16482 			/* was held in ire_add */
16483 			ire_refrele(*irep1);
16484 		}
16485 	}
16486 
16487 	if (!loopback && ipif->ipif_addr_ready) {
16488 		/* Broadcast an address mask reply. */
16489 		ipif_mask_reply(ipif);
16490 	}
16491 	if (ipif_saved_irep != NULL) {
16492 		kmem_free(ipif_saved_irep,
16493 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16494 	}
16495 	if (src_ipif_held)
16496 		ipif_refrele(src_ipif);
16497 
16498 	/*
16499 	 * This had to be deferred until we had bound.  Tell routing sockets and
16500 	 * others that this interface is up if it looks like the address has
16501 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16502 	 * duplicate address detection to do its thing.
16503 	 */
16504 	if (ipif->ipif_addr_ready)
16505 		ipif_up_notify(ipif);
16506 	return (0);
16507 
16508 bad:
16509 	ip1dbg(("ipif_up_done: FAILED \n"));
16510 
16511 	while (irep > ire_array) {
16512 		irep--;
16513 		if (*irep != NULL)
16514 			ire_delete(*irep);
16515 	}
16516 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16517 
16518 	if (ipif_saved_irep != NULL) {
16519 		kmem_free(ipif_saved_irep,
16520 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16521 	}
16522 	if (src_ipif_held)
16523 		ipif_refrele(src_ipif);
16524 
16525 	ipif_resolver_down(ipif);
16526 	return (err);
16527 }
16528 
16529 /*
16530  * Turn off the ARP with the ILLF_NOARP flag.
16531  */
16532 static int
16533 ill_arp_off(ill_t *ill)
16534 {
16535 	mblk_t	*arp_off_mp = NULL;
16536 	mblk_t	*arp_on_mp = NULL;
16537 
16538 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16539 
16540 	ASSERT(IAM_WRITER_ILL(ill));
16541 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16542 
16543 	/*
16544 	 * If the on message is still around we've already done
16545 	 * an arp_off without doing an arp_on thus there is no
16546 	 * work needed.
16547 	 */
16548 	if (ill->ill_arp_on_mp != NULL)
16549 		return (0);
16550 
16551 	/*
16552 	 * Allocate an ARP on message (to be saved) and an ARP off message
16553 	 */
16554 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16555 	if (!arp_off_mp)
16556 		return (ENOMEM);
16557 
16558 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16559 	if (!arp_on_mp)
16560 		goto failed;
16561 
16562 	ASSERT(ill->ill_arp_on_mp == NULL);
16563 	ill->ill_arp_on_mp = arp_on_mp;
16564 
16565 	/* Send an AR_INTERFACE_OFF request */
16566 	putnext(ill->ill_rq, arp_off_mp);
16567 	return (0);
16568 failed:
16569 
16570 	if (arp_off_mp)
16571 		freemsg(arp_off_mp);
16572 	return (ENOMEM);
16573 }
16574 
16575 /*
16576  * Turn on ARP by turning off the ILLF_NOARP flag.
16577  */
16578 static int
16579 ill_arp_on(ill_t *ill)
16580 {
16581 	mblk_t	*mp;
16582 
16583 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16584 
16585 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16586 
16587 	ASSERT(IAM_WRITER_ILL(ill));
16588 	/*
16589 	 * Send an AR_INTERFACE_ON request if we have already done
16590 	 * an arp_off (which allocated the message).
16591 	 */
16592 	if (ill->ill_arp_on_mp != NULL) {
16593 		mp = ill->ill_arp_on_mp;
16594 		ill->ill_arp_on_mp = NULL;
16595 		putnext(ill->ill_rq, mp);
16596 	}
16597 	return (0);
16598 }
16599 
16600 /*
16601  * Checks for availbility of a usable source address (if there is one) when the
16602  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16603  * this selection is done regardless of the destination.
16604  */
16605 boolean_t
16606 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16607 {
16608 	uint_t	ifindex;
16609 	ipif_t	*ipif = NULL;
16610 	ill_t	*uill;
16611 	boolean_t isv6;
16612 	ip_stack_t	*ipst = ill->ill_ipst;
16613 
16614 	ASSERT(ill != NULL);
16615 
16616 	isv6 = ill->ill_isv6;
16617 	ifindex = ill->ill_usesrc_ifindex;
16618 	if (ifindex != 0) {
16619 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16620 		    NULL, ipst);
16621 		if (uill == NULL)
16622 			return (NULL);
16623 		mutex_enter(&uill->ill_lock);
16624 		for (ipif = uill->ill_ipif; ipif != NULL;
16625 		    ipif = ipif->ipif_next) {
16626 			if (!IPIF_CAN_LOOKUP(ipif))
16627 				continue;
16628 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16629 				continue;
16630 			if (!(ipif->ipif_flags & IPIF_UP))
16631 				continue;
16632 			if (ipif->ipif_zoneid != zoneid)
16633 				continue;
16634 			if ((isv6 &&
16635 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16636 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16637 				continue;
16638 			mutex_exit(&uill->ill_lock);
16639 			ill_refrele(uill);
16640 			return (B_TRUE);
16641 		}
16642 		mutex_exit(&uill->ill_lock);
16643 		ill_refrele(uill);
16644 	}
16645 	return (B_FALSE);
16646 }
16647 
16648 /*
16649  * IP source address type, sorted from worst to best.  For a given type,
16650  * always prefer IP addresses on the same subnet.  All-zones addresses are
16651  * suboptimal because they pose problems with unlabeled destinations.
16652  */
16653 typedef enum {
16654 	IPIF_NONE,
16655 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16656 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16657 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16658 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16659 	IPIF_DIFFNET,			/* normal and different subnet */
16660 	IPIF_SAMENET			/* normal and same subnet */
16661 } ipif_type_t;
16662 
16663 /*
16664  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16665  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16666  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16667  * the first one, unless IPMP is used in which case we round-robin among them;
16668  * see below for more.
16669  *
16670  * Returns NULL if there is no suitable source address for the ill.
16671  * This only occurs when there is no valid source address for the ill.
16672  */
16673 ipif_t *
16674 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16675 {
16676 	ill_t	*usill = NULL;
16677 	ill_t	*ipmp_ill = NULL;
16678 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16679 	ipif_type_t type, best_type;
16680 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16681 	ip_stack_t *ipst = ill->ill_ipst;
16682 	boolean_t samenet;
16683 
16684 	if (ill->ill_usesrc_ifindex != 0) {
16685 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16686 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16687 		if (usill != NULL)
16688 			ill = usill;	/* Select source from usesrc ILL */
16689 		else
16690 			return (NULL);
16691 	}
16692 
16693 	/*
16694 	 * Test addresses should never be used for source address selection,
16695 	 * so if we were passed one, switch to the IPMP meta-interface.
16696 	 */
16697 	if (IS_UNDER_IPMP(ill)) {
16698 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16699 			ill = ipmp_ill;	/* Select source from IPMP ill */
16700 		else
16701 			return (NULL);
16702 	}
16703 
16704 	/*
16705 	 * If we're dealing with an unlabeled destination on a labeled system,
16706 	 * make sure that we ignore source addresses that are incompatible with
16707 	 * the destination's default label.  That destination's default label
16708 	 * must dominate the minimum label on the source address.
16709 	 */
16710 	dst_rhtp = NULL;
16711 	if (is_system_labeled()) {
16712 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16713 		if (dst_rhtp == NULL)
16714 			return (NULL);
16715 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16716 			TPC_RELE(dst_rhtp);
16717 			dst_rhtp = NULL;
16718 		}
16719 	}
16720 
16721 	/*
16722 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16723 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16724 	 * After selecting the right ipif, under ill_lock make sure ipif is
16725 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16726 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16727 	 * but not under a lock.
16728 	 */
16729 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16730 retry:
16731 	/*
16732 	 * For source address selection, we treat the ipif list as circular
16733 	 * and continue until we get back to where we started.  This allows
16734 	 * IPMP to vary source address selection (which improves inbound load
16735 	 * spreading) by caching its last ending point and starting from
16736 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16737 	 * ills since that can't happen on the IPMP ill.
16738 	 */
16739 	start_ipif = ill->ill_ipif;
16740 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16741 		start_ipif = ill->ill_src_ipif;
16742 
16743 	ipif = start_ipif;
16744 	best_ipif = NULL;
16745 	best_type = IPIF_NONE;
16746 	do {
16747 		if ((next_ipif = ipif->ipif_next) == NULL)
16748 			next_ipif = ill->ill_ipif;
16749 
16750 		if (!IPIF_CAN_LOOKUP(ipif))
16751 			continue;
16752 		/* Always skip NOLOCAL and ANYCAST interfaces */
16753 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16754 			continue;
16755 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16756 			continue;
16757 		if (ipif->ipif_zoneid != zoneid &&
16758 		    ipif->ipif_zoneid != ALL_ZONES)
16759 			continue;
16760 
16761 		/*
16762 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16763 		 * are not valid as source addresses.
16764 		 */
16765 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16766 			continue;
16767 
16768 		/*
16769 		 * Check compatibility of local address for destination's
16770 		 * default label if we're on a labeled system.	Incompatible
16771 		 * addresses can't be used at all.
16772 		 */
16773 		if (dst_rhtp != NULL) {
16774 			boolean_t incompat;
16775 
16776 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16777 			    IPV4_VERSION, B_FALSE);
16778 			if (src_rhtp == NULL)
16779 				continue;
16780 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16781 			    src_rhtp->tpc_tp.tp_doi !=
16782 			    dst_rhtp->tpc_tp.tp_doi ||
16783 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16784 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16785 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16786 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16787 			TPC_RELE(src_rhtp);
16788 			if (incompat)
16789 				continue;
16790 		}
16791 
16792 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16793 
16794 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16795 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16796 			    IPIF_DIFFNET_DEPRECATED;
16797 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16798 			type = samenet ? IPIF_SAMENET_ALLZONES :
16799 			    IPIF_DIFFNET_ALLZONES;
16800 		} else {
16801 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16802 		}
16803 
16804 		if (type > best_type) {
16805 			best_type = type;
16806 			best_ipif = ipif;
16807 			if (best_type == IPIF_SAMENET)
16808 				break; /* can't get better */
16809 		}
16810 	} while ((ipif = next_ipif) != start_ipif);
16811 
16812 	if ((ipif = best_ipif) != NULL) {
16813 		mutex_enter(&ipif->ipif_ill->ill_lock);
16814 		if (!IPIF_CAN_LOOKUP(ipif)) {
16815 			mutex_exit(&ipif->ipif_ill->ill_lock);
16816 			goto retry;
16817 		}
16818 		ipif_refhold_locked(ipif);
16819 
16820 		/*
16821 		 * For IPMP, update the source ipif rotor to the next ipif,
16822 		 * provided we can look it up.  (We must not use it if it's
16823 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16824 		 * ipif_free() checked ill_src_ipif.)
16825 		 */
16826 		if (IS_IPMP(ill) && ipif != NULL) {
16827 			next_ipif = ipif->ipif_next;
16828 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16829 				ill->ill_src_ipif = next_ipif;
16830 			else
16831 				ill->ill_src_ipif = NULL;
16832 		}
16833 		mutex_exit(&ipif->ipif_ill->ill_lock);
16834 	}
16835 
16836 	rw_exit(&ipst->ips_ill_g_lock);
16837 	if (usill != NULL)
16838 		ill_refrele(usill);
16839 	if (ipmp_ill != NULL)
16840 		ill_refrele(ipmp_ill);
16841 	if (dst_rhtp != NULL)
16842 		TPC_RELE(dst_rhtp);
16843 
16844 #ifdef DEBUG
16845 	if (ipif == NULL) {
16846 		char buf1[INET6_ADDRSTRLEN];
16847 
16848 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
16849 		    ill->ill_name,
16850 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
16851 	} else {
16852 		char buf1[INET6_ADDRSTRLEN];
16853 		char buf2[INET6_ADDRSTRLEN];
16854 
16855 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
16856 		    ipif->ipif_ill->ill_name,
16857 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
16858 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
16859 		    buf2, sizeof (buf2))));
16860 	}
16861 #endif /* DEBUG */
16862 	return (ipif);
16863 }
16864 
16865 /*
16866  * If old_ipif is not NULL, see if ipif was derived from old
16867  * ipif and if so, recreate the interface route by re-doing
16868  * source address selection. This happens when ipif_down ->
16869  * ipif_update_other_ipifs calls us.
16870  *
16871  * If old_ipif is NULL, just redo the source address selection
16872  * if needed. This happens when ipif_up_done calls us.
16873  */
16874 static void
16875 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
16876 {
16877 	ire_t *ire;
16878 	ire_t *ipif_ire;
16879 	queue_t *stq;
16880 	ipif_t *nipif;
16881 	ill_t *ill;
16882 	boolean_t need_rele = B_FALSE;
16883 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16884 
16885 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
16886 	ASSERT(IAM_WRITER_IPIF(ipif));
16887 
16888 	ill = ipif->ipif_ill;
16889 	if (!(ipif->ipif_flags &
16890 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16891 		/*
16892 		 * Can't possibly have borrowed the source
16893 		 * from old_ipif.
16894 		 */
16895 		return;
16896 	}
16897 
16898 	/*
16899 	 * Is there any work to be done? No work if the address
16900 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
16901 	 * ipif_select_source() does not borrow addresses from
16902 	 * NOLOCAL and ANYCAST interfaces).
16903 	 */
16904 	if ((old_ipif != NULL) &&
16905 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
16906 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
16907 	    (old_ipif->ipif_flags &
16908 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
16909 		return;
16910 	}
16911 
16912 	/*
16913 	 * Perform the same checks as when creating the
16914 	 * IRE_INTERFACE in ipif_up_done.
16915 	 */
16916 	if (!(ipif->ipif_flags & IPIF_UP))
16917 		return;
16918 
16919 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
16920 	    (ipif->ipif_subnet == INADDR_ANY))
16921 		return;
16922 
16923 	ipif_ire = ipif_to_ire(ipif);
16924 	if (ipif_ire == NULL)
16925 		return;
16926 
16927 	/*
16928 	 * We know that ipif uses some other source for its
16929 	 * IRE_INTERFACE. Is it using the source of this
16930 	 * old_ipif?
16931 	 */
16932 	if (old_ipif != NULL &&
16933 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
16934 		ire_refrele(ipif_ire);
16935 		return;
16936 	}
16937 	if (ip_debug > 2) {
16938 		/* ip1dbg */
16939 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
16940 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
16941 	}
16942 
16943 	stq = ipif_ire->ire_stq;
16944 
16945 	/*
16946 	 * Can't use our source address. Select a different
16947 	 * source address for the IRE_INTERFACE.
16948 	 */
16949 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
16950 	if (nipif == NULL) {
16951 		/* Last resort - all ipif's have IPIF_NOLOCAL */
16952 		nipif = ipif;
16953 	} else {
16954 		need_rele = B_TRUE;
16955 	}
16956 
16957 	ire = ire_create(
16958 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
16959 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
16960 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
16961 	    NULL,				/* no gateway */
16962 	    &ipif->ipif_mtu,			/* max frag */
16963 	    NULL,				/* no src nce */
16964 	    NULL,				/* no recv from queue */
16965 	    stq,				/* send-to queue */
16966 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16967 	    ipif,
16968 	    0,
16969 	    0,
16970 	    0,
16971 	    0,
16972 	    &ire_uinfo_null,
16973 	    NULL,
16974 	    NULL,
16975 	    ipst);
16976 
16977 	if (ire != NULL) {
16978 		ire_t *ret_ire;
16979 		int error;
16980 
16981 		/*
16982 		 * We don't need ipif_ire anymore. We need to delete
16983 		 * before we add so that ire_add does not detect
16984 		 * duplicates.
16985 		 */
16986 		ire_delete(ipif_ire);
16987 		ret_ire = ire;
16988 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
16989 		ASSERT(error == 0);
16990 		ASSERT(ire == ret_ire);
16991 		/* Held in ire_add */
16992 		ire_refrele(ret_ire);
16993 	}
16994 	/*
16995 	 * Either we are falling through from above or could not
16996 	 * allocate a replacement.
16997 	 */
16998 	ire_refrele(ipif_ire);
16999 	if (need_rele)
17000 		ipif_refrele(nipif);
17001 }
17002 
17003 /*
17004  * This old_ipif is going away.
17005  *
17006  * Determine if any other ipif's are using our address as
17007  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17008  * IPIF_DEPRECATED).
17009  * Find the IRE_INTERFACE for such ipifs and recreate them
17010  * to use an different source address following the rules in
17011  * ipif_up_done.
17012  */
17013 static void
17014 ipif_update_other_ipifs(ipif_t *old_ipif)
17015 {
17016 	ipif_t	*ipif;
17017 	ill_t	*ill;
17018 	char	buf[INET6_ADDRSTRLEN];
17019 
17020 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17021 
17022 	ill = old_ipif->ipif_ill;
17023 
17024 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17025 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17026 
17027 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17028 		if (ipif == old_ipif)
17029 			continue;
17030 		ipif_recreate_interface_routes(old_ipif, ipif);
17031 	}
17032 }
17033 
17034 /* ARGSUSED */
17035 int
17036 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17037 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17038 {
17039 	/*
17040 	 * ill_phyint_reinit merged the v4 and v6 into a single
17041 	 * ipsq.  We might not have been able to complete the
17042 	 * operation in ipif_set_values, if we could not become
17043 	 * exclusive.  If so restart it here.
17044 	 */
17045 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17046 }
17047 
17048 /*
17049  * Can operate on either a module or a driver queue.
17050  * Returns an error if not a module queue.
17051  */
17052 /* ARGSUSED */
17053 int
17054 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17055     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17056 {
17057 	queue_t		*q1 = q;
17058 	char 		*cp;
17059 	char		interf_name[LIFNAMSIZ];
17060 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17061 
17062 	if (q->q_next == NULL) {
17063 		ip1dbg((
17064 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17065 		return (EINVAL);
17066 	}
17067 
17068 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17069 		return (EALREADY);
17070 
17071 	do {
17072 		q1 = q1->q_next;
17073 	} while (q1->q_next);
17074 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17075 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17076 
17077 	/*
17078 	 * Here we are not going to delay the ioack until after
17079 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17080 	 * original ioctl message before sending the requests.
17081 	 */
17082 	return (ipif_set_values(q, mp, interf_name, &ppa));
17083 }
17084 
17085 /* ARGSUSED */
17086 int
17087 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17088     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17089 {
17090 	return (ENXIO);
17091 }
17092 
17093 /*
17094  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17095  * minimum (but complete) set exist.  This is necessary when adding or
17096  * removing an interface to/from an IPMP group, since interfaces in an
17097  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17098  * its test address subnets overlap with IPMP data addresses).	It's also
17099  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17100  * interface when the nominated broadcast interface changes.
17101  */
17102 void
17103 ill_refresh_bcast(ill_t *ill)
17104 {
17105 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17106 	ire_t **irep;
17107 	ipif_t *ipif;
17108 
17109 	ASSERT(!ill->ill_isv6);
17110 	ASSERT(IAM_WRITER_ILL(ill));
17111 
17112 	/*
17113 	 * Remove any old broadcast IREs.
17114 	 */
17115 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17116 	    ill_broadcast_delete, ill, ill);
17117 
17118 	/*
17119 	 * Create new ones for any ipifs that are up and broadcast-capable.
17120 	 */
17121 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17122 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17123 		    (IPIF_UP|IPIF_BROADCAST))
17124 			continue;
17125 
17126 		irep = ipif_create_bcast_ires(ipif, ire_array);
17127 		while (irep-- > ire_array) {
17128 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17129 			if (*irep != NULL)
17130 				ire_refrele(*irep);
17131 		}
17132 	}
17133 }
17134 
17135 /*
17136  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17137  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17138  * ire_check_and_create_bcast()).
17139  */
17140 static ire_t **
17141 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17142 {
17143 	ipaddr_t addr;
17144 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17145 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17146 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17147 
17148 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17149 
17150 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17151 
17152 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17153 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17154 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17155 
17156 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17157 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17158 
17159 	/*
17160 	 * For backward compatibility, we create net broadcast IREs based on
17161 	 * the old "IP address class system", since some old machines only
17162 	 * respond to these class derived net broadcast.  However, we must not
17163 	 * create these net broadcast IREs if the subnetmask is shorter than
17164 	 * the IP address class based derived netmask.  Otherwise, we may
17165 	 * create a net broadcast address which is the same as an IP address
17166 	 * on the subnet -- and then TCP will refuse to talk to that address.
17167 	 */
17168 	if (netmask < subnetmask) {
17169 		addr = netmask & ipif->ipif_subnet;
17170 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17171 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17172 		    flags);
17173 	}
17174 
17175 	/*
17176 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17177 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17178 	 * created.  Creating these broadcast IREs will only create confusion
17179 	 * as `addr' will be the same as the IP address.
17180 	 */
17181 	if (subnetmask != 0xFFFFFFFF) {
17182 		addr = ipif->ipif_subnet;
17183 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17184 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17185 		    irep, flags);
17186 	}
17187 
17188 	return (irep);
17189 }
17190 
17191 /*
17192  * Broadcast IRE info structure used in the functions below.  Since we
17193  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17194  */
17195 typedef struct bcast_ireinfo {
17196 	uchar_t		bi_type;	/* BCAST_* value from below */
17197 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17198 			bi_needrep:1,	/* do we need to replace it? */
17199 			bi_haverep:1,	/* have we replaced it? */
17200 			bi_pad:5;
17201 	ipaddr_t	bi_addr;	/* IRE address */
17202 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17203 } bcast_ireinfo_t;
17204 
17205 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17206 
17207 /*
17208  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17209  * return B_TRUE if it should immediately be used to recreate the IRE.
17210  */
17211 static boolean_t
17212 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17213 {
17214 	ipaddr_t addr;
17215 
17216 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17217 
17218 	switch (bireinfop->bi_type) {
17219 	case BCAST_NET:
17220 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17221 		if (addr != bireinfop->bi_addr)
17222 			return (B_FALSE);
17223 		break;
17224 	case BCAST_SUBNET:
17225 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17226 			return (B_FALSE);
17227 		break;
17228 	}
17229 
17230 	bireinfop->bi_needrep = 1;
17231 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17232 		if (bireinfop->bi_backup == NULL)
17233 			bireinfop->bi_backup = ipif;
17234 		return (B_FALSE);
17235 	}
17236 	return (B_TRUE);
17237 }
17238 
17239 /*
17240  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17241  * them ala ire_check_and_create_bcast().
17242  */
17243 static ire_t **
17244 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17245 {
17246 	ipaddr_t mask, addr;
17247 
17248 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17249 
17250 	addr = bireinfop->bi_addr;
17251 	irep = ire_create_bcast(ipif, addr, irep);
17252 
17253 	switch (bireinfop->bi_type) {
17254 	case BCAST_NET:
17255 		mask = ip_net_mask(ipif->ipif_subnet);
17256 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17257 		break;
17258 	case BCAST_SUBNET:
17259 		mask = ipif->ipif_net_mask;
17260 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17261 		break;
17262 	}
17263 
17264 	bireinfop->bi_haverep = 1;
17265 	return (irep);
17266 }
17267 
17268 /*
17269  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17270  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17271  * that are going away are still needed.  If so, have ipif_create_bcast()
17272  * recreate them (except for the deprecated case, as explained below).
17273  */
17274 static ire_t **
17275 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17276     ire_t **irep)
17277 {
17278 	int i;
17279 	ipif_t *ipif;
17280 
17281 	ASSERT(!ill->ill_isv6);
17282 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17283 		/*
17284 		 * Skip this ipif if it's (a) the one being taken down, (b)
17285 		 * not in the same zone, or (c) has no valid local address.
17286 		 */
17287 		if (ipif == test_ipif ||
17288 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17289 		    ipif->ipif_subnet == 0 ||
17290 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17291 		    (IPIF_UP|IPIF_BROADCAST))
17292 			continue;
17293 
17294 		/*
17295 		 * For each dying IRE that hasn't yet been replaced, see if
17296 		 * `ipif' needs it and whether the IRE should be recreated on
17297 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17298 		 * will return B_FALSE even if `ipif' needs the IRE on the
17299 		 * hopes that we'll later find a needy non-deprecated ipif.
17300 		 * However, the ipif is recorded in bi_backup for possible
17301 		 * subsequent use by ipif_check_bcast_ires().
17302 		 */
17303 		for (i = 0; i < BCAST_COUNT; i++) {
17304 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17305 				continue;
17306 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17307 				continue;
17308 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17309 		}
17310 
17311 		/*
17312 		 * If we've replaced all of the broadcast IREs that are going
17313 		 * to be taken down, we know we're done.
17314 		 */
17315 		for (i = 0; i < BCAST_COUNT; i++) {
17316 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17317 				break;
17318 		}
17319 		if (i == BCAST_COUNT)
17320 			break;
17321 	}
17322 	return (irep);
17323 }
17324 
17325 /*
17326  * Check if `test_ipif' (which is going away) is associated with any existing
17327  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17328  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17329  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17330  *
17331  * This is necessary because broadcast IREs are shared.  In particular, a
17332  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17333  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17334  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17335  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17336  * same zone, they will share the same set of broadcast IREs.
17337  *
17338  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17339  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17340  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17341  */
17342 static void
17343 ipif_check_bcast_ires(ipif_t *test_ipif)
17344 {
17345 	ill_t		*ill = test_ipif->ipif_ill;
17346 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17347 	ire_t		**irep1, **irep = &ire_array[0];
17348 	uint_t 		i, willdie;
17349 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17350 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17351 
17352 	ASSERT(!test_ipif->ipif_isv6);
17353 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17354 
17355 	/*
17356 	 * No broadcast IREs for the LOOPBACK interface
17357 	 * or others such as point to point and IPIF_NOXMIT.
17358 	 */
17359 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17360 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17361 		return;
17362 
17363 	bzero(bireinfo, sizeof (bireinfo));
17364 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17365 	bireinfo[0].bi_addr = 0;
17366 
17367 	bireinfo[1].bi_type = BCAST_ALLONES;
17368 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17369 
17370 	bireinfo[2].bi_type = BCAST_NET;
17371 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17372 
17373 	if (test_ipif->ipif_net_mask != 0)
17374 		mask = test_ipif->ipif_net_mask;
17375 	bireinfo[3].bi_type = BCAST_SUBNET;
17376 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17377 
17378 	/*
17379 	 * Figure out what (if any) broadcast IREs will die as a result of
17380 	 * `test_ipif' going away.  If none will die, we're done.
17381 	 */
17382 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17383 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17384 		    test_ipif, ALL_ZONES, NULL,
17385 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17386 		if (ire != NULL) {
17387 			willdie++;
17388 			bireinfo[i].bi_willdie = 1;
17389 			ire_refrele(ire);
17390 		}
17391 	}
17392 
17393 	if (willdie == 0)
17394 		return;
17395 
17396 	/*
17397 	 * Walk through all the ipifs that will be affected by the dying IREs,
17398 	 * and recreate the IREs as necessary. Note that all interfaces in an
17399 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17400 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17401 	 * that broadcast IREs end up on it whenever possible).
17402 	 */
17403 	if (IS_UNDER_IPMP(ill))
17404 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17405 
17406 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17407 
17408 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17409 		ipmp_illgrp_t *illg = ill->ill_grp;
17410 
17411 		ill = list_head(&illg->ig_if);
17412 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17413 			for (i = 0; i < BCAST_COUNT; i++) {
17414 				if (bireinfo[i].bi_willdie &&
17415 				    !bireinfo[i].bi_haverep)
17416 					break;
17417 			}
17418 			if (i == BCAST_COUNT)
17419 				break;
17420 
17421 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17422 		}
17423 	}
17424 
17425 	/*
17426 	 * Scan through the set of broadcast IREs and see if there are any
17427 	 * that we need to replace that have not yet been replaced.  If so,
17428 	 * replace them using the appropriate backup ipif.
17429 	 */
17430 	for (i = 0; i < BCAST_COUNT; i++) {
17431 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17432 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17433 			    &bireinfo[i], irep);
17434 	}
17435 
17436 	/*
17437 	 * If we can't create all of them, don't add any of them.  (Code in
17438 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17439 	 * non-loopback copy and loopback copy for a given address.)
17440 	 */
17441 	for (irep1 = irep; irep1 > ire_array; ) {
17442 		irep1--;
17443 		if (*irep1 == NULL) {
17444 			ip0dbg(("ipif_check_bcast_ires: can't create "
17445 			    "IRE_BROADCAST, memory allocation failure\n"));
17446 			while (irep > ire_array) {
17447 				irep--;
17448 				if (*irep != NULL)
17449 					ire_delete(*irep);
17450 			}
17451 			return;
17452 		}
17453 	}
17454 
17455 	for (irep1 = irep; irep1 > ire_array; ) {
17456 		irep1--;
17457 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17458 			ire_refrele(*irep1);		/* Held in ire_add */
17459 	}
17460 }
17461 
17462 /*
17463  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17464  * from lifr_flags and the name from lifr_name.
17465  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17466  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17467  * Returns EINPROGRESS when mp has been consumed by queueing it on
17468  * ill_pending_mp and the ioctl will complete in ip_rput.
17469  *
17470  * Can operate on either a module or a driver queue.
17471  * Returns an error if not a module queue.
17472  */
17473 /* ARGSUSED */
17474 int
17475 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17476     ip_ioctl_cmd_t *ipip, void *if_req)
17477 {
17478 	ill_t	*ill = q->q_ptr;
17479 	phyint_t *phyi;
17480 	ip_stack_t *ipst;
17481 	struct lifreq *lifr = if_req;
17482 
17483 	ASSERT(ipif != NULL);
17484 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17485 
17486 	if (q->q_next == NULL) {
17487 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17488 		return (EINVAL);
17489 	}
17490 
17491 	/*
17492 	 * If we are not writer on 'q' then this interface exists already
17493 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17494 	 * so return EALREADY.
17495 	 */
17496 	if (ill != ipif->ipif_ill)
17497 		return (EALREADY);
17498 
17499 	if (ill->ill_name[0] != '\0')
17500 		return (EALREADY);
17501 
17502 	/*
17503 	 * Set all the flags. Allows all kinds of override. Provide some
17504 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
17505 	 * unless there is either multicast/broadcast support in the driver
17506 	 * or it is a pt-pt link.
17507 	 */
17508 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
17509 		/* Meaningless to IP thus don't allow them to be set. */
17510 		ip1dbg(("ip_setname: EINVAL 1\n"));
17511 		return (EINVAL);
17512 	}
17513 
17514 	/*
17515 	 * If there's another ill already with the requested name, ensure
17516 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17517 	 * fuse together two unrelated ills, which will cause chaos.
17518 	 */
17519 	ipst = ill->ill_ipst;
17520 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17521 	    lifr->lifr_name, NULL);
17522 	if (phyi != NULL) {
17523 		ill_t *ill_mate = phyi->phyint_illv4;
17524 
17525 		if (ill_mate == NULL)
17526 			ill_mate = phyi->phyint_illv6;
17527 		ASSERT(ill_mate != NULL);
17528 
17529 		if (ill_mate->ill_media->ip_m_mac_type !=
17530 		    ill->ill_media->ip_m_mac_type) {
17531 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17532 			    "use the same ill name on differing media\n"));
17533 			return (EINVAL);
17534 		}
17535 	}
17536 
17537 	/*
17538 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
17539 	 * ill_bcast_addr_length info.
17540 	 */
17541 	if (!ill->ill_needs_attach &&
17542 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
17543 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
17544 	    ill->ill_bcast_addr_length == 0)) {
17545 		/* Link not broadcast/pt-pt capable i.e. no multicast */
17546 		ip1dbg(("ip_setname: EINVAL 2\n"));
17547 		return (EINVAL);
17548 	}
17549 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17550 	    ((lifr->lifr_flags & IFF_IPV6) ||
17551 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17552 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
17553 		ip1dbg(("ip_setname: EINVAL 3\n"));
17554 		return (EINVAL);
17555 	}
17556 	if (lifr->lifr_flags & IFF_UP) {
17557 		/* Can only be set with SIOCSLIFFLAGS */
17558 		ip1dbg(("ip_setname: EINVAL 4\n"));
17559 		return (EINVAL);
17560 	}
17561 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
17562 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
17563 		ip1dbg(("ip_setname: EINVAL 5\n"));
17564 		return (EINVAL);
17565 	}
17566 	/*
17567 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17568 	 */
17569 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
17570 	    !(lifr->lifr_flags & IFF_IPV6) &&
17571 	    !(ipif->ipif_isv6)) {
17572 		ip1dbg(("ip_setname: EINVAL 6\n"));
17573 		return (EINVAL);
17574 	}
17575 
17576 	/*
17577 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
17578 	 * we have all the flags here. So, we assign rather than we OR.
17579 	 * We can't OR the flags here because we don't want to set
17580 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
17581 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
17582 	 * on lifr_flags value here.
17583 	 */
17584 	/*
17585 	 * This ill has not been inserted into the global list.
17586 	 * So we are still single threaded and don't need any lock
17587 	 */
17588 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
17589 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
17590 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
17591 
17592 	/* We started off as V4. */
17593 	if (ill->ill_flags & ILLF_IPV6) {
17594 		ill->ill_phyint->phyint_illv6 = ill;
17595 		ill->ill_phyint->phyint_illv4 = NULL;
17596 	}
17597 
17598 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17599 }
17600 
17601 /* ARGSUSED */
17602 int
17603 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17604     ip_ioctl_cmd_t *ipip, void *if_req)
17605 {
17606 	/*
17607 	 * ill_phyint_reinit merged the v4 and v6 into a single
17608 	 * ipsq.  We might not have been able to complete the
17609 	 * slifname in ipif_set_values, if we could not become
17610 	 * exclusive.  If so restart it here
17611 	 */
17612 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17613 }
17614 
17615 /*
17616  * Return a pointer to the ipif which matches the index, IP version type and
17617  * zoneid.
17618  */
17619 ipif_t *
17620 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17621     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17622 {
17623 	ill_t	*ill;
17624 	ipif_t	*ipif = NULL;
17625 
17626 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17627 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17628 
17629 	if (err != NULL)
17630 		*err = 0;
17631 
17632 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17633 	if (ill != NULL) {
17634 		mutex_enter(&ill->ill_lock);
17635 		for (ipif = ill->ill_ipif; ipif != NULL;
17636 		    ipif = ipif->ipif_next) {
17637 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17638 			    zoneid == ipif->ipif_zoneid ||
17639 			    ipif->ipif_zoneid == ALL_ZONES)) {
17640 				ipif_refhold_locked(ipif);
17641 				break;
17642 			}
17643 		}
17644 		mutex_exit(&ill->ill_lock);
17645 		ill_refrele(ill);
17646 		if (ipif == NULL && err != NULL)
17647 			*err = ENXIO;
17648 	}
17649 	return (ipif);
17650 }
17651 
17652 /*
17653  * Change an existing physical interface's index. If the new index
17654  * is acceptable we update the index and the phyint_list_avl_by_index tree.
17655  * Finally, we update other systems which may have a dependence on the
17656  * index value.
17657  */
17658 /* ARGSUSED */
17659 int
17660 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17661     ip_ioctl_cmd_t *ipip, void *ifreq)
17662 {
17663 	ill_t		*ill;
17664 	phyint_t	*phyi;
17665 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17666 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17667 	uint_t	old_index, index;
17668 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17669 	avl_index_t	where;
17670 
17671 	if (ipip->ipi_cmd_type == IF_CMD)
17672 		index = ifr->ifr_index;
17673 	else
17674 		index = lifr->lifr_index;
17675 
17676 	/*
17677 	 * Only allow on physical interface. Also, index zero is illegal.
17678 	 */
17679 	ill = ipif->ipif_ill;
17680 	phyi = ill->ill_phyint;
17681 	if (ipif->ipif_id != 0 || index == 0) {
17682 		return (EINVAL);
17683 	}
17684 
17685 	/* If the index is not changing, no work to do */
17686 	if (phyi->phyint_ifindex == index)
17687 		return (0);
17688 
17689 	/*
17690 	 * Use phyint_exists() to determine if the new interface index
17691 	 * is already in use. If the index is unused then we need to
17692 	 * change the phyint's position in the phyint_list_avl_by_index
17693 	 * tree. If we do not do this, subsequent lookups (using the new
17694 	 * index value) will not find the phyint.
17695 	 */
17696 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17697 	if (phyint_exists(index, ipst)) {
17698 		rw_exit(&ipst->ips_ill_g_lock);
17699 		return (EEXIST);
17700 	}
17701 
17702 	/* The new index is unused. Set it in the phyint. */
17703 	old_index = phyi->phyint_ifindex;
17704 	phyi->phyint_ifindex = index;
17705 
17706 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
17707 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17708 	    &index, &where);
17709 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17710 	    phyi, where);
17711 	rw_exit(&ipst->ips_ill_g_lock);
17712 
17713 	/* Update SCTP's ILL list */
17714 	sctp_ill_reindex(ill, old_index);
17715 
17716 	/* Send the routing sockets message */
17717 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17718 	if (ILL_OTHER(ill))
17719 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17720 
17721 	return (0);
17722 }
17723 
17724 /* ARGSUSED */
17725 int
17726 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17727     ip_ioctl_cmd_t *ipip, void *ifreq)
17728 {
17729 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17730 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17731 
17732 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17733 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17734 	/* Get the interface index */
17735 	if (ipip->ipi_cmd_type == IF_CMD) {
17736 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17737 	} else {
17738 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17739 	}
17740 	return (0);
17741 }
17742 
17743 /* ARGSUSED */
17744 int
17745 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17746     ip_ioctl_cmd_t *ipip, void *ifreq)
17747 {
17748 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17749 
17750 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17751 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17752 	/* Get the interface zone */
17753 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17754 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17755 	return (0);
17756 }
17757 
17758 /*
17759  * Set the zoneid of an interface.
17760  */
17761 /* ARGSUSED */
17762 int
17763 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17764     ip_ioctl_cmd_t *ipip, void *ifreq)
17765 {
17766 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17767 	int err = 0;
17768 	boolean_t need_up = B_FALSE;
17769 	zone_t *zptr;
17770 	zone_status_t status;
17771 	zoneid_t zoneid;
17772 
17773 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17774 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17775 		if (!is_system_labeled())
17776 			return (ENOTSUP);
17777 		zoneid = GLOBAL_ZONEID;
17778 	}
17779 
17780 	/* cannot assign instance zero to a non-global zone */
17781 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17782 		return (ENOTSUP);
17783 
17784 	/*
17785 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17786 	 * the event of a race with the zone shutdown processing, since IP
17787 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17788 	 * interface will be cleaned up even if the zone is shut down
17789 	 * immediately after the status check. If the interface can't be brought
17790 	 * down right away, and the zone is shut down before the restart
17791 	 * function is called, we resolve the possible races by rechecking the
17792 	 * zone status in the restart function.
17793 	 */
17794 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17795 		return (EINVAL);
17796 	status = zone_status_get(zptr);
17797 	zone_rele(zptr);
17798 
17799 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17800 		return (EINVAL);
17801 
17802 	if (ipif->ipif_flags & IPIF_UP) {
17803 		/*
17804 		 * If the interface is already marked up,
17805 		 * we call ipif_down which will take care
17806 		 * of ditching any IREs that have been set
17807 		 * up based on the old interface address.
17808 		 */
17809 		err = ipif_logical_down(ipif, q, mp);
17810 		if (err == EINPROGRESS)
17811 			return (err);
17812 		ipif_down_tail(ipif);
17813 		need_up = B_TRUE;
17814 	}
17815 
17816 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17817 	return (err);
17818 }
17819 
17820 static int
17821 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17822     queue_t *q, mblk_t *mp, boolean_t need_up)
17823 {
17824 	int	err = 0;
17825 	ip_stack_t	*ipst;
17826 
17827 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17828 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17829 
17830 	if (CONN_Q(q))
17831 		ipst = CONNQ_TO_IPST(q);
17832 	else
17833 		ipst = ILLQ_TO_IPST(q);
17834 
17835 	/*
17836 	 * For exclusive stacks we don't allow a different zoneid than
17837 	 * global.
17838 	 */
17839 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17840 	    zoneid != GLOBAL_ZONEID)
17841 		return (EINVAL);
17842 
17843 	/* Set the new zone id. */
17844 	ipif->ipif_zoneid = zoneid;
17845 
17846 	/* Update sctp list */
17847 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
17848 
17849 	if (need_up) {
17850 		/*
17851 		 * Now bring the interface back up.  If this
17852 		 * is the only IPIF for the ILL, ipif_up
17853 		 * will have to re-bind to the device, so
17854 		 * we may get back EINPROGRESS, in which
17855 		 * case, this IOCTL will get completed in
17856 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
17857 		 */
17858 		err = ipif_up(ipif, q, mp);
17859 	}
17860 	return (err);
17861 }
17862 
17863 /* ARGSUSED */
17864 int
17865 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17866     ip_ioctl_cmd_t *ipip, void *if_req)
17867 {
17868 	struct lifreq *lifr = (struct lifreq *)if_req;
17869 	zoneid_t zoneid;
17870 	zone_t *zptr;
17871 	zone_status_t status;
17872 
17873 	ASSERT(ipif->ipif_id != 0);
17874 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17875 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
17876 		zoneid = GLOBAL_ZONEID;
17877 
17878 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
17879 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17880 
17881 	/*
17882 	 * We recheck the zone status to resolve the following race condition:
17883 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
17884 	 * 2) hme0:1 is up and can't be brought down right away;
17885 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
17886 	 * 3) zone "myzone" is halted; the zone status switches to
17887 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
17888 	 * the interfaces to remove - hme0:1 is not returned because it's not
17889 	 * yet in "myzone", so it won't be removed;
17890 	 * 4) the restart function for SIOCSLIFZONE is called; without the
17891 	 * status check here, we would have hme0:1 in "myzone" after it's been
17892 	 * destroyed.
17893 	 * Note that if the status check fails, we need to bring the interface
17894 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
17895 	 * ipif_up_done[_v6]().
17896 	 */
17897 	status = ZONE_IS_UNINITIALIZED;
17898 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
17899 		status = zone_status_get(zptr);
17900 		zone_rele(zptr);
17901 	}
17902 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
17903 		if (ipif->ipif_isv6) {
17904 			(void) ipif_up_done_v6(ipif);
17905 		} else {
17906 			(void) ipif_up_done(ipif);
17907 		}
17908 		return (EINVAL);
17909 	}
17910 
17911 	ipif_down_tail(ipif);
17912 
17913 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
17914 	    B_TRUE));
17915 }
17916 
17917 /*
17918  * Return the number of addresses on `ill' with one or more of the values
17919  * in `set' set and all of the values in `clear' clear.
17920  */
17921 static uint_t
17922 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
17923 {
17924 	ipif_t	*ipif;
17925 	uint_t	cnt = 0;
17926 
17927 	ASSERT(IAM_WRITER_ILL(ill));
17928 
17929 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
17930 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
17931 			cnt++;
17932 
17933 	return (cnt);
17934 }
17935 
17936 /*
17937  * Return the number of migratable addresses on `ill' that are under
17938  * application control.
17939  */
17940 uint_t
17941 ill_appaddr_cnt(const ill_t *ill)
17942 {
17943 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
17944 	    IPIF_NOFAILOVER));
17945 }
17946 
17947 /*
17948  * Return the number of point-to-point addresses on `ill'.
17949  */
17950 uint_t
17951 ill_ptpaddr_cnt(const ill_t *ill)
17952 {
17953 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
17954 }
17955 
17956 /* ARGSUSED */
17957 int
17958 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17959 	ip_ioctl_cmd_t *ipip, void *ifreq)
17960 {
17961 	struct lifreq	*lifr = ifreq;
17962 
17963 	ASSERT(q->q_next == NULL);
17964 	ASSERT(CONN_Q(q));
17965 
17966 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
17967 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17968 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
17969 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
17970 
17971 	return (0);
17972 }
17973 
17974 /* Find the previous ILL in this usesrc group */
17975 static ill_t *
17976 ill_prev_usesrc(ill_t *uill)
17977 {
17978 	ill_t *ill;
17979 
17980 	for (ill = uill->ill_usesrc_grp_next;
17981 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
17982 	    ill = ill->ill_usesrc_grp_next)
17983 		/* do nothing */;
17984 	return (ill);
17985 }
17986 
17987 /*
17988  * Release all members of the usesrc group. This routine is called
17989  * from ill_delete when the interface being unplumbed is the
17990  * group head.
17991  */
17992 static void
17993 ill_disband_usesrc_group(ill_t *uill)
17994 {
17995 	ill_t *next_ill, *tmp_ill;
17996 	ip_stack_t	*ipst = uill->ill_ipst;
17997 
17998 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
17999 	next_ill = uill->ill_usesrc_grp_next;
18000 
18001 	do {
18002 		ASSERT(next_ill != NULL);
18003 		tmp_ill = next_ill->ill_usesrc_grp_next;
18004 		ASSERT(tmp_ill != NULL);
18005 		next_ill->ill_usesrc_grp_next = NULL;
18006 		next_ill->ill_usesrc_ifindex = 0;
18007 		next_ill = tmp_ill;
18008 	} while (next_ill->ill_usesrc_ifindex != 0);
18009 	uill->ill_usesrc_grp_next = NULL;
18010 }
18011 
18012 /*
18013  * Remove the client usesrc ILL from the list and relink to a new list
18014  */
18015 int
18016 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18017 {
18018 	ill_t *ill, *tmp_ill;
18019 	ip_stack_t	*ipst = ucill->ill_ipst;
18020 
18021 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18022 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18023 
18024 	/*
18025 	 * Check if the usesrc client ILL passed in is not already
18026 	 * in use as a usesrc ILL i.e one whose source address is
18027 	 * in use OR a usesrc ILL is not already in use as a usesrc
18028 	 * client ILL
18029 	 */
18030 	if ((ucill->ill_usesrc_ifindex == 0) ||
18031 	    (uill->ill_usesrc_ifindex != 0)) {
18032 		return (-1);
18033 	}
18034 
18035 	ill = ill_prev_usesrc(ucill);
18036 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18037 
18038 	/* Remove from the current list */
18039 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18040 		/* Only two elements in the list */
18041 		ASSERT(ill->ill_usesrc_ifindex == 0);
18042 		ill->ill_usesrc_grp_next = NULL;
18043 	} else {
18044 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18045 	}
18046 
18047 	if (ifindex == 0) {
18048 		ucill->ill_usesrc_ifindex = 0;
18049 		ucill->ill_usesrc_grp_next = NULL;
18050 		return (0);
18051 	}
18052 
18053 	ucill->ill_usesrc_ifindex = ifindex;
18054 	tmp_ill = uill->ill_usesrc_grp_next;
18055 	uill->ill_usesrc_grp_next = ucill;
18056 	ucill->ill_usesrc_grp_next =
18057 	    (tmp_ill != NULL) ? tmp_ill : uill;
18058 	return (0);
18059 }
18060 
18061 /*
18062  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18063  * ip.c for locking details.
18064  */
18065 /* ARGSUSED */
18066 int
18067 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18068     ip_ioctl_cmd_t *ipip, void *ifreq)
18069 {
18070 	struct lifreq *lifr = (struct lifreq *)ifreq;
18071 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18072 	    ill_flag_changed = B_FALSE;
18073 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18074 	int err = 0, ret;
18075 	uint_t ifindex;
18076 	ipsq_t *ipsq = NULL;
18077 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18078 
18079 	ASSERT(IAM_WRITER_IPIF(ipif));
18080 	ASSERT(q->q_next == NULL);
18081 	ASSERT(CONN_Q(q));
18082 
18083 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18084 
18085 	ifindex = lifr->lifr_index;
18086 	if (ifindex == 0) {
18087 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18088 			/* non usesrc group interface, nothing to reset */
18089 			return (0);
18090 		}
18091 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18092 		/* valid reset request */
18093 		reset_flg = B_TRUE;
18094 	}
18095 
18096 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18097 	    ip_process_ioctl, &err, ipst);
18098 	if (usesrc_ill == NULL) {
18099 		return (err);
18100 	}
18101 
18102 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18103 	    NEW_OP, B_TRUE);
18104 	if (ipsq == NULL) {
18105 		err = EINPROGRESS;
18106 		/* Operation enqueued on the ipsq of the usesrc ILL */
18107 		goto done;
18108 	}
18109 
18110 	/* USESRC isn't currently supported with IPMP */
18111 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18112 		err = ENOTSUP;
18113 		goto done;
18114 	}
18115 
18116 	/*
18117 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18118 	 * used by IPMP underlying interfaces, but someone might think it's
18119 	 * more general and try to use it independently with VNI.)
18120 	 */
18121 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18122 		err = ENOTSUP;
18123 		goto done;
18124 	}
18125 
18126 	/*
18127 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18128 	 * already a client then return EINVAL
18129 	 */
18130 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18131 		err = EINVAL;
18132 		goto done;
18133 	}
18134 
18135 	/*
18136 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18137 	 * be then this is a duplicate operation.
18138 	 */
18139 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18140 		err = 0;
18141 		goto done;
18142 	}
18143 
18144 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18145 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18146 	    usesrc_ill->ill_isv6));
18147 
18148 	/*
18149 	 * The next step ensures that no new ires will be created referencing
18150 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18151 	 * we go through an ire walk deleting all ire caches that reference
18152 	 * the client ill. New ires referencing the client ill that are added
18153 	 * to the ire table before the ILL_CHANGING flag is set, will be
18154 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18155 	 * the client ill while the ILL_CHANGING flag is set will be failed
18156 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18157 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18158 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18159 	 * belong to the same usesrc group.
18160 	 */
18161 	mutex_enter(&usesrc_cli_ill->ill_lock);
18162 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18163 	mutex_exit(&usesrc_cli_ill->ill_lock);
18164 	ill_flag_changed = B_TRUE;
18165 
18166 	if (ipif->ipif_isv6)
18167 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18168 		    ALL_ZONES, ipst);
18169 	else
18170 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18171 		    ALL_ZONES, ipst);
18172 
18173 	/*
18174 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18175 	 * and the ill_usesrc_ifindex fields
18176 	 */
18177 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18178 
18179 	if (reset_flg) {
18180 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18181 		if (ret != 0) {
18182 			err = EINVAL;
18183 		}
18184 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18185 		goto done;
18186 	}
18187 
18188 	/*
18189 	 * Four possibilities to consider:
18190 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18191 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18192 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18193 	 * 4. Both are part of their respective usesrc groups
18194 	 */
18195 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18196 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18197 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18198 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18199 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18200 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18201 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18202 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18203 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18204 		/* Insert at head of list */
18205 		usesrc_cli_ill->ill_usesrc_grp_next =
18206 		    usesrc_ill->ill_usesrc_grp_next;
18207 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18208 	} else {
18209 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18210 		    ifindex);
18211 		if (ret != 0)
18212 			err = EINVAL;
18213 	}
18214 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18215 
18216 done:
18217 	if (ill_flag_changed) {
18218 		mutex_enter(&usesrc_cli_ill->ill_lock);
18219 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18220 		mutex_exit(&usesrc_cli_ill->ill_lock);
18221 	}
18222 	if (ipsq != NULL)
18223 		ipsq_exit(ipsq);
18224 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18225 	ill_refrele(usesrc_ill);
18226 	return (err);
18227 }
18228 
18229 /*
18230  * comparison function used by avl.
18231  */
18232 static int
18233 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18234 {
18235 
18236 	uint_t index;
18237 
18238 	ASSERT(phyip != NULL && index_ptr != NULL);
18239 
18240 	index = *((uint_t *)index_ptr);
18241 	/*
18242 	 * let the phyint with the lowest index be on top.
18243 	 */
18244 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18245 		return (1);
18246 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18247 		return (-1);
18248 	return (0);
18249 }
18250 
18251 /*
18252  * comparison function used by avl.
18253  */
18254 static int
18255 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18256 {
18257 	ill_t *ill;
18258 	int res = 0;
18259 
18260 	ASSERT(phyip != NULL && name_ptr != NULL);
18261 
18262 	if (((phyint_t *)phyip)->phyint_illv4)
18263 		ill = ((phyint_t *)phyip)->phyint_illv4;
18264 	else
18265 		ill = ((phyint_t *)phyip)->phyint_illv6;
18266 	ASSERT(ill != NULL);
18267 
18268 	res = strcmp(ill->ill_name, (char *)name_ptr);
18269 	if (res > 0)
18270 		return (1);
18271 	else if (res < 0)
18272 		return (-1);
18273 	return (0);
18274 }
18275 
18276 /*
18277  * This function is called on the unplumb path via ill_glist_delete() when
18278  * there are no ills left on the phyint and thus the phyint can be freed.
18279  */
18280 static void
18281 phyint_free(phyint_t *phyi)
18282 {
18283 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18284 
18285 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18286 
18287 	/*
18288 	 * If this phyint was an IPMP meta-interface, blow away the group.
18289 	 * This is safe to do because all of the illgrps have already been
18290 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18291 	 * If we're cleaning up as a result of failed initialization,
18292 	 * phyint_grp may be NULL.
18293 	 */
18294 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18295 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18296 		ipmp_grp_destroy(phyi->phyint_grp);
18297 		phyi->phyint_grp = NULL;
18298 		rw_exit(&ipst->ips_ipmp_lock);
18299 	}
18300 
18301 	/*
18302 	 * If this interface was under IPMP, take it out of the group.
18303 	 */
18304 	if (phyi->phyint_grp != NULL)
18305 		ipmp_phyint_leave_grp(phyi);
18306 
18307 	/*
18308 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18309 	 * will be freed in ipsq_exit().
18310 	 */
18311 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18312 	phyi->phyint_name[0] = '\0';
18313 
18314 	mi_free(phyi);
18315 }
18316 
18317 /*
18318  * Attach the ill to the phyint structure which can be shared by both
18319  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18320  * function is called from ipif_set_values and ill_lookup_on_name (for
18321  * loopback) where we know the name of the ill. We lookup the ill and if
18322  * there is one present already with the name use that phyint. Otherwise
18323  * reuse the one allocated by ill_init.
18324  */
18325 static void
18326 ill_phyint_reinit(ill_t *ill)
18327 {
18328 	boolean_t isv6 = ill->ill_isv6;
18329 	phyint_t *phyi_old;
18330 	phyint_t *phyi;
18331 	avl_index_t where = 0;
18332 	ill_t	*ill_other = NULL;
18333 	ip_stack_t	*ipst = ill->ill_ipst;
18334 
18335 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18336 
18337 	phyi_old = ill->ill_phyint;
18338 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18339 	    phyi_old->phyint_illv6 == NULL));
18340 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18341 	    phyi_old->phyint_illv4 == NULL));
18342 	ASSERT(phyi_old->phyint_ifindex == 0);
18343 
18344 	/*
18345 	 * Now that our ill has a name, set it in the phyint.
18346 	 */
18347 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18348 
18349 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18350 	    ill->ill_name, &where);
18351 
18352 	/*
18353 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18354 	 *    the global list of ills. So no other thread could have located
18355 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18356 	 * 2. Now locate the other protocol instance of this ill.
18357 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18358 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18359 	 *    of neither ill can change.
18360 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18361 	 *    other ill.
18362 	 * 5. Release all locks.
18363 	 */
18364 
18365 	/*
18366 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18367 	 * we are initializing IPv4.
18368 	 */
18369 	if (phyi != NULL) {
18370 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18371 		ASSERT(ill_other->ill_phyint != NULL);
18372 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18373 		    (!isv6 && ill_other->ill_isv6));
18374 		GRAB_ILL_LOCKS(ill, ill_other);
18375 		/*
18376 		 * We are potentially throwing away phyint_flags which
18377 		 * could be different from the one that we obtain from
18378 		 * ill_other->ill_phyint. But it is okay as we are assuming
18379 		 * that the state maintained within IP is correct.
18380 		 */
18381 		mutex_enter(&phyi->phyint_lock);
18382 		if (isv6) {
18383 			ASSERT(phyi->phyint_illv6 == NULL);
18384 			phyi->phyint_illv6 = ill;
18385 		} else {
18386 			ASSERT(phyi->phyint_illv4 == NULL);
18387 			phyi->phyint_illv4 = ill;
18388 		}
18389 
18390 		/*
18391 		 * Delete the old phyint and make its ipsq eligible
18392 		 * to be freed in ipsq_exit().
18393 		 */
18394 		phyi_old->phyint_illv4 = NULL;
18395 		phyi_old->phyint_illv6 = NULL;
18396 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18397 		phyi_old->phyint_name[0] = '\0';
18398 		mi_free(phyi_old);
18399 	} else {
18400 		mutex_enter(&ill->ill_lock);
18401 		/*
18402 		 * We don't need to acquire any lock, since
18403 		 * the ill is not yet visible globally  and we
18404 		 * have not yet released the ill_g_lock.
18405 		 */
18406 		phyi = phyi_old;
18407 		mutex_enter(&phyi->phyint_lock);
18408 		/* XXX We need a recovery strategy here. */
18409 		if (!phyint_assign_ifindex(phyi, ipst))
18410 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18411 
18412 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18413 		    (void *)phyi, where);
18414 
18415 		(void) avl_find(&ipst->ips_phyint_g_list->
18416 		    phyint_list_avl_by_index,
18417 		    &phyi->phyint_ifindex, &where);
18418 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18419 		    (void *)phyi, where);
18420 	}
18421 
18422 	/*
18423 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18424 	 * pending mp is not affected because that is per ill basis.
18425 	 */
18426 	ill->ill_phyint = phyi;
18427 
18428 	/*
18429 	 * Now that the phyint's ifindex has been assigned, complete the
18430 	 * remaining
18431 	 */
18432 
18433 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18434 	if (ill->ill_isv6) {
18435 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18436 		    ill->ill_phyint->phyint_ifindex;
18437 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18438 	} else {
18439 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18440 	}
18441 
18442 	/*
18443 	 * Generate an event within the hooks framework to indicate that
18444 	 * a new interface has just been added to IP.  For this event to
18445 	 * be generated, the network interface must, at least, have an
18446 	 * ifindex assigned to it.  (We don't generate the event for
18447 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
18448 	 *
18449 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18450 	 * that the ordering of delivered events to listeners matches the
18451 	 * order of them in the kernel.
18452 	 */
18453 	if (!IS_LOOPBACK(ill)) {
18454 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18455 		    ill->ill_name_length);
18456 	}
18457 	RELEASE_ILL_LOCKS(ill, ill_other);
18458 	mutex_exit(&phyi->phyint_lock);
18459 }
18460 
18461 /*
18462  * Notify any downstream modules of the name of this interface.
18463  * An M_IOCTL is used even though we don't expect a successful reply.
18464  * Any reply message from the driver (presumably an M_IOCNAK) will
18465  * eventually get discarded somewhere upstream.  The message format is
18466  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18467  * to IP.
18468  */
18469 static void
18470 ip_ifname_notify(ill_t *ill, queue_t *q)
18471 {
18472 	mblk_t *mp1, *mp2;
18473 	struct iocblk *iocp;
18474 	struct lifreq *lifr;
18475 
18476 	mp1 = mkiocb(SIOCSLIFNAME);
18477 	if (mp1 == NULL)
18478 		return;
18479 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18480 	if (mp2 == NULL) {
18481 		freeb(mp1);
18482 		return;
18483 	}
18484 
18485 	mp1->b_cont = mp2;
18486 	iocp = (struct iocblk *)mp1->b_rptr;
18487 	iocp->ioc_count = sizeof (struct lifreq);
18488 
18489 	lifr = (struct lifreq *)mp2->b_rptr;
18490 	mp2->b_wptr += sizeof (struct lifreq);
18491 	bzero(lifr, sizeof (struct lifreq));
18492 
18493 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18494 	lifr->lifr_ppa = ill->ill_ppa;
18495 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18496 
18497 	putnext(q, mp1);
18498 }
18499 
18500 static int
18501 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18502 {
18503 	int		err;
18504 	ip_stack_t	*ipst = ill->ill_ipst;
18505 	phyint_t	*phyi = ill->ill_phyint;
18506 
18507 	/* Set the obsolete NDD per-interface forwarding name. */
18508 	err = ill_set_ndd_name(ill);
18509 	if (err != 0) {
18510 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18511 		    err);
18512 	}
18513 
18514 	/*
18515 	 * Now that ill_name is set, the configuration for the IPMP
18516 	 * meta-interface can be performed.
18517 	 */
18518 	if (IS_IPMP(ill)) {
18519 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18520 		/*
18521 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18522 		 * meta-interface and we need to create the IPMP group.
18523 		 */
18524 		if (phyi->phyint_grp == NULL) {
18525 			/*
18526 			 * If someone has renamed another IPMP group to have
18527 			 * the same name as our interface, bail.
18528 			 */
18529 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18530 				rw_exit(&ipst->ips_ipmp_lock);
18531 				return (EEXIST);
18532 			}
18533 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18534 			if (phyi->phyint_grp == NULL) {
18535 				rw_exit(&ipst->ips_ipmp_lock);
18536 				return (ENOMEM);
18537 			}
18538 		}
18539 		rw_exit(&ipst->ips_ipmp_lock);
18540 	}
18541 
18542 	/* Tell downstream modules where they are. */
18543 	ip_ifname_notify(ill, q);
18544 
18545 	/*
18546 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18547 	 * Error cases are ENOMEM ...
18548 	 */
18549 	err = ill_dl_phys(ill, ipif, mp, q);
18550 
18551 	/*
18552 	 * If there is no IRE expiration timer running, get one started.
18553 	 * igmp and mld timers will be triggered by the first multicast
18554 	 */
18555 	if (ipst->ips_ip_ire_expire_id == 0) {
18556 		/*
18557 		 * acquire the lock and check again.
18558 		 */
18559 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18560 		if (ipst->ips_ip_ire_expire_id == 0) {
18561 			ipst->ips_ip_ire_expire_id = timeout(
18562 			    ip_trash_timer_expire, ipst,
18563 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18564 		}
18565 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18566 	}
18567 
18568 	if (ill->ill_isv6) {
18569 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18570 		if (ipst->ips_mld_slowtimeout_id == 0) {
18571 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18572 			    (void *)ipst,
18573 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18574 		}
18575 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18576 	} else {
18577 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18578 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18579 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18580 			    (void *)ipst,
18581 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18582 		}
18583 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18584 	}
18585 
18586 	return (err);
18587 }
18588 
18589 /*
18590  * Common routine for ppa and ifname setting. Should be called exclusive.
18591  *
18592  * Returns EINPROGRESS when mp has been consumed by queueing it on
18593  * ill_pending_mp and the ioctl will complete in ip_rput.
18594  *
18595  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18596  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18597  * For SLIFNAME, we pass these values back to the userland.
18598  */
18599 static int
18600 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18601 {
18602 	ill_t	*ill;
18603 	ipif_t	*ipif;
18604 	ipsq_t	*ipsq;
18605 	char	*ppa_ptr;
18606 	char	*old_ptr;
18607 	char	old_char;
18608 	int	error;
18609 	ip_stack_t	*ipst;
18610 
18611 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18612 	ASSERT(q->q_next != NULL);
18613 	ASSERT(interf_name != NULL);
18614 
18615 	ill = (ill_t *)q->q_ptr;
18616 	ipst = ill->ill_ipst;
18617 
18618 	ASSERT(ill->ill_ipst != NULL);
18619 	ASSERT(ill->ill_name[0] == '\0');
18620 	ASSERT(IAM_WRITER_ILL(ill));
18621 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18622 	ASSERT(ill->ill_ppa == UINT_MAX);
18623 
18624 	/* The ppa is sent down by ifconfig or is chosen */
18625 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18626 		return (EINVAL);
18627 	}
18628 
18629 	/*
18630 	 * make sure ppa passed in is same as ppa in the name.
18631 	 * This check is not made when ppa == UINT_MAX in that case ppa
18632 	 * in the name could be anything. System will choose a ppa and
18633 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18634 	 */
18635 	if (*new_ppa_ptr != UINT_MAX) {
18636 		/* stoi changes the pointer */
18637 		old_ptr = ppa_ptr;
18638 		/*
18639 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18640 		 * (they don't have an externally visible ppa).  We assign one
18641 		 * here so that we can manage the interface.  Note that in
18642 		 * the past this value was always 0 for DLPI 1 drivers.
18643 		 */
18644 		if (*new_ppa_ptr == 0)
18645 			*new_ppa_ptr = stoi(&old_ptr);
18646 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18647 			return (EINVAL);
18648 	}
18649 	/*
18650 	 * terminate string before ppa
18651 	 * save char at that location.
18652 	 */
18653 	old_char = ppa_ptr[0];
18654 	ppa_ptr[0] = '\0';
18655 
18656 	ill->ill_ppa = *new_ppa_ptr;
18657 	/*
18658 	 * Finish as much work now as possible before calling ill_glist_insert
18659 	 * which makes the ill globally visible and also merges it with the
18660 	 * other protocol instance of this phyint. The remaining work is
18661 	 * done after entering the ipsq which may happen sometime later.
18662 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18663 	 */
18664 	ipif = ill->ill_ipif;
18665 
18666 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18667 	ipif_assign_seqid(ipif);
18668 
18669 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18670 		ill->ill_flags |= ILLF_IPV4;
18671 
18672 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18673 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18674 
18675 	if (ill->ill_flags & ILLF_IPV6) {
18676 
18677 		ill->ill_isv6 = B_TRUE;
18678 		if (ill->ill_rq != NULL) {
18679 			ill->ill_rq->q_qinfo = &iprinitv6;
18680 			ill->ill_wq->q_qinfo = &ipwinitv6;
18681 		}
18682 
18683 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18684 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18685 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18686 		ipif->ipif_v6subnet = ipv6_all_zeros;
18687 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18688 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18689 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18690 		/*
18691 		 * point-to-point or Non-mulicast capable
18692 		 * interfaces won't do NUD unless explicitly
18693 		 * configured to do so.
18694 		 */
18695 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18696 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18697 			ill->ill_flags |= ILLF_NONUD;
18698 		}
18699 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18700 		if (ill->ill_flags & ILLF_NOARP) {
18701 			/*
18702 			 * Note: xresolv interfaces will eventually need
18703 			 * NOARP set here as well, but that will require
18704 			 * those external resolvers to have some
18705 			 * knowledge of that flag and act appropriately.
18706 			 * Not to be changed at present.
18707 			 */
18708 			ill->ill_flags &= ~ILLF_NOARP;
18709 		}
18710 		/*
18711 		 * Set the ILLF_ROUTER flag according to the global
18712 		 * IPv6 forwarding policy.
18713 		 */
18714 		if (ipst->ips_ipv6_forward != 0)
18715 			ill->ill_flags |= ILLF_ROUTER;
18716 	} else if (ill->ill_flags & ILLF_IPV4) {
18717 		ill->ill_isv6 = B_FALSE;
18718 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18719 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18720 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18721 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18722 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18723 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18724 		/*
18725 		 * Set the ILLF_ROUTER flag according to the global
18726 		 * IPv4 forwarding policy.
18727 		 */
18728 		if (ipst->ips_ip_g_forward != 0)
18729 			ill->ill_flags |= ILLF_ROUTER;
18730 	}
18731 
18732 	ASSERT(ill->ill_phyint != NULL);
18733 
18734 	/*
18735 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18736 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18737 	 */
18738 	if (!ill_allocate_mibs(ill))
18739 		return (ENOMEM);
18740 
18741 	/*
18742 	 * Pick a default sap until we get the DL_INFO_ACK back from
18743 	 * the driver.
18744 	 */
18745 	if (ill->ill_sap == 0) {
18746 		if (ill->ill_isv6)
18747 			ill->ill_sap = IP6_DL_SAP;
18748 		else
18749 			ill->ill_sap = IP_DL_SAP;
18750 	}
18751 
18752 	ill->ill_ifname_pending = 1;
18753 	ill->ill_ifname_pending_err = 0;
18754 
18755 	/*
18756 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18757 	 * that were joined while this ill was not bound to the DLPI link need
18758 	 * to be recovered by ill_recover_multicast().
18759 	 */
18760 	ill->ill_need_recover_multicast = 1;
18761 
18762 	ill_refhold(ill);
18763 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18764 	if ((error = ill_glist_insert(ill, interf_name,
18765 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18766 		ill->ill_ppa = UINT_MAX;
18767 		ill->ill_name[0] = '\0';
18768 		/*
18769 		 * undo null termination done above.
18770 		 */
18771 		ppa_ptr[0] = old_char;
18772 		rw_exit(&ipst->ips_ill_g_lock);
18773 		ill_refrele(ill);
18774 		return (error);
18775 	}
18776 
18777 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18778 
18779 	/*
18780 	 * When we return the buffer pointed to by interf_name should contain
18781 	 * the same name as in ill_name.
18782 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18783 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18784 	 * so copy full name and update the ppa ptr.
18785 	 * When ppa passed in != UINT_MAX all values are correct just undo
18786 	 * null termination, this saves a bcopy.
18787 	 */
18788 	if (*new_ppa_ptr == UINT_MAX) {
18789 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18790 		*new_ppa_ptr = ill->ill_ppa;
18791 	} else {
18792 		/*
18793 		 * undo null termination done above.
18794 		 */
18795 		ppa_ptr[0] = old_char;
18796 	}
18797 
18798 	/* Let SCTP know about this ILL */
18799 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18800 
18801 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18802 	    B_TRUE);
18803 
18804 	rw_exit(&ipst->ips_ill_g_lock);
18805 	ill_refrele(ill);
18806 	if (ipsq == NULL)
18807 		return (EINPROGRESS);
18808 
18809 	/*
18810 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18811 	 */
18812 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18813 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18814 	else
18815 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18816 
18817 	error = ipif_set_values_tail(ill, ipif, mp, q);
18818 	ipsq_exit(ipsq);
18819 	if (error != 0 && error != EINPROGRESS) {
18820 		/*
18821 		 * restore previous values
18822 		 */
18823 		ill->ill_isv6 = B_FALSE;
18824 	}
18825 	return (error);
18826 }
18827 
18828 void
18829 ipif_init(ip_stack_t *ipst)
18830 {
18831 	int i;
18832 
18833 	for (i = 0; i < MAX_G_HEADS; i++) {
18834 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18835 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18836 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
18837 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18838 	}
18839 
18840 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18841 	    ill_phyint_compare_index,
18842 	    sizeof (phyint_t),
18843 	    offsetof(struct phyint, phyint_avl_by_index));
18844 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18845 	    ill_phyint_compare_name,
18846 	    sizeof (phyint_t),
18847 	    offsetof(struct phyint, phyint_avl_by_name));
18848 }
18849 
18850 /*
18851  * Lookup the ipif corresponding to the onlink destination address. For
18852  * point-to-point interfaces, it matches with remote endpoint destination
18853  * address. For point-to-multipoint interfaces it only tries to match the
18854  * destination with the interface's subnet address. The longest, most specific
18855  * match is found to take care of such rare network configurations like -
18856  * le0: 129.146.1.1/16
18857  * le1: 129.146.2.2/24
18858  *
18859  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
18860  * supported on underlying interfaces in an IPMP group, underlying interfaces
18861  * are ignored when looking up a match.  (If we didn't ignore them, we'd
18862  * risk using a test address as a source for outgoing traffic.)
18863  */
18864 ipif_t *
18865 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
18866 {
18867 	ipif_t	*ipif, *best_ipif;
18868 	ill_t	*ill;
18869 	ill_walk_context_t ctx;
18870 
18871 	ASSERT(zoneid != ALL_ZONES);
18872 	best_ipif = NULL;
18873 
18874 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18875 	ill = ILL_START_WALK_V4(&ctx, ipst);
18876 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18877 		if (IS_UNDER_IPMP(ill))
18878 			continue;
18879 		mutex_enter(&ill->ill_lock);
18880 		for (ipif = ill->ill_ipif; ipif != NULL;
18881 		    ipif = ipif->ipif_next) {
18882 			if (!IPIF_CAN_LOOKUP(ipif))
18883 				continue;
18884 			if (ipif->ipif_zoneid != zoneid &&
18885 			    ipif->ipif_zoneid != ALL_ZONES)
18886 				continue;
18887 			/*
18888 			 * Point-to-point case. Look for exact match with
18889 			 * destination address.
18890 			 */
18891 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18892 				if (ipif->ipif_pp_dst_addr == addr) {
18893 					ipif_refhold_locked(ipif);
18894 					mutex_exit(&ill->ill_lock);
18895 					rw_exit(&ipst->ips_ill_g_lock);
18896 					if (best_ipif != NULL)
18897 						ipif_refrele(best_ipif);
18898 					return (ipif);
18899 				}
18900 			} else if (ipif->ipif_subnet == (addr &
18901 			    ipif->ipif_net_mask)) {
18902 				/*
18903 				 * Point-to-multipoint case. Looping through to
18904 				 * find the most specific match. If there are
18905 				 * multiple best match ipif's then prefer ipif's
18906 				 * that are UP. If there is only one best match
18907 				 * ipif and it is DOWN we must still return it.
18908 				 */
18909 				if ((best_ipif == NULL) ||
18910 				    (ipif->ipif_net_mask >
18911 				    best_ipif->ipif_net_mask) ||
18912 				    ((ipif->ipif_net_mask ==
18913 				    best_ipif->ipif_net_mask) &&
18914 				    ((ipif->ipif_flags & IPIF_UP) &&
18915 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
18916 					ipif_refhold_locked(ipif);
18917 					mutex_exit(&ill->ill_lock);
18918 					rw_exit(&ipst->ips_ill_g_lock);
18919 					if (best_ipif != NULL)
18920 						ipif_refrele(best_ipif);
18921 					best_ipif = ipif;
18922 					rw_enter(&ipst->ips_ill_g_lock,
18923 					    RW_READER);
18924 					mutex_enter(&ill->ill_lock);
18925 				}
18926 			}
18927 		}
18928 		mutex_exit(&ill->ill_lock);
18929 	}
18930 	rw_exit(&ipst->ips_ill_g_lock);
18931 	return (best_ipif);
18932 }
18933 
18934 /*
18935  * Save enough information so that we can recreate the IRE if
18936  * the interface goes down and then up.
18937  */
18938 static void
18939 ipif_save_ire(ipif_t *ipif, ire_t *ire)
18940 {
18941 	mblk_t	*save_mp;
18942 
18943 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
18944 	if (save_mp != NULL) {
18945 		ifrt_t	*ifrt;
18946 
18947 		save_mp->b_wptr += sizeof (ifrt_t);
18948 		ifrt = (ifrt_t *)save_mp->b_rptr;
18949 		bzero(ifrt, sizeof (ifrt_t));
18950 		ifrt->ifrt_type = ire->ire_type;
18951 		ifrt->ifrt_addr = ire->ire_addr;
18952 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
18953 		ifrt->ifrt_src_addr = ire->ire_src_addr;
18954 		ifrt->ifrt_mask = ire->ire_mask;
18955 		ifrt->ifrt_flags = ire->ire_flags;
18956 		ifrt->ifrt_max_frag = ire->ire_max_frag;
18957 		mutex_enter(&ipif->ipif_saved_ire_lock);
18958 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
18959 		ipif->ipif_saved_ire_mp = save_mp;
18960 		ipif->ipif_saved_ire_cnt++;
18961 		mutex_exit(&ipif->ipif_saved_ire_lock);
18962 	}
18963 }
18964 
18965 static void
18966 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
18967 {
18968 	mblk_t	**mpp;
18969 	mblk_t	*mp;
18970 	ifrt_t	*ifrt;
18971 
18972 	/* Remove from ipif_saved_ire_mp list if it is there */
18973 	mutex_enter(&ipif->ipif_saved_ire_lock);
18974 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
18975 	    mpp = &(*mpp)->b_cont) {
18976 		/*
18977 		 * On a given ipif, the triple of address, gateway and
18978 		 * mask is unique for each saved IRE (in the case of
18979 		 * ordinary interface routes, the gateway address is
18980 		 * all-zeroes).
18981 		 */
18982 		mp = *mpp;
18983 		ifrt = (ifrt_t *)mp->b_rptr;
18984 		if (ifrt->ifrt_addr == ire->ire_addr &&
18985 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
18986 		    ifrt->ifrt_mask == ire->ire_mask) {
18987 			*mpp = mp->b_cont;
18988 			ipif->ipif_saved_ire_cnt--;
18989 			freeb(mp);
18990 			break;
18991 		}
18992 	}
18993 	mutex_exit(&ipif->ipif_saved_ire_lock);
18994 }
18995 
18996 /*
18997  * IP multirouting broadcast routes handling
18998  * Append CGTP broadcast IREs to regular ones created
18999  * at ifconfig time.
19000  */
19001 static void
19002 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
19003 {
19004 	ire_t *ire_prim;
19005 
19006 	ASSERT(ire != NULL);
19007 	ASSERT(ire_dst != NULL);
19008 
19009 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19010 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19011 	if (ire_prim != NULL) {
19012 		/*
19013 		 * We are in the special case of broadcasts for
19014 		 * CGTP. We add an IRE_BROADCAST that holds
19015 		 * the RTF_MULTIRT flag, the destination
19016 		 * address of ire_dst and the low level
19017 		 * info of ire_prim. In other words, CGTP
19018 		 * broadcast is added to the redundant ipif.
19019 		 */
19020 		ipif_t *ipif_prim;
19021 		ire_t  *bcast_ire;
19022 
19023 		ipif_prim = ire_prim->ire_ipif;
19024 
19025 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19026 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19027 		    (void *)ire_dst, (void *)ire_prim,
19028 		    (void *)ipif_prim));
19029 
19030 		bcast_ire = ire_create(
19031 		    (uchar_t *)&ire->ire_addr,
19032 		    (uchar_t *)&ip_g_all_ones,
19033 		    (uchar_t *)&ire_dst->ire_src_addr,
19034 		    (uchar_t *)&ire->ire_gateway_addr,
19035 		    &ipif_prim->ipif_mtu,
19036 		    NULL,
19037 		    ipif_prim->ipif_rq,
19038 		    ipif_prim->ipif_wq,
19039 		    IRE_BROADCAST,
19040 		    ipif_prim,
19041 		    0,
19042 		    0,
19043 		    0,
19044 		    ire->ire_flags,
19045 		    &ire_uinfo_null,
19046 		    NULL,
19047 		    NULL,
19048 		    ipst);
19049 
19050 		if (bcast_ire != NULL) {
19051 
19052 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19053 			    B_FALSE) == 0) {
19054 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19055 				    "added bcast_ire %p\n",
19056 				    (void *)bcast_ire));
19057 
19058 				ipif_save_ire(bcast_ire->ire_ipif,
19059 				    bcast_ire);
19060 				ire_refrele(bcast_ire);
19061 			}
19062 		}
19063 		ire_refrele(ire_prim);
19064 	}
19065 }
19066 
19067 /*
19068  * IP multirouting broadcast routes handling
19069  * Remove the broadcast ire
19070  */
19071 static void
19072 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19073 {
19074 	ire_t *ire_dst;
19075 
19076 	ASSERT(ire != NULL);
19077 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19078 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19079 	if (ire_dst != NULL) {
19080 		ire_t *ire_prim;
19081 
19082 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19083 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19084 		if (ire_prim != NULL) {
19085 			ipif_t *ipif_prim;
19086 			ire_t  *bcast_ire;
19087 
19088 			ipif_prim = ire_prim->ire_ipif;
19089 
19090 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19091 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19092 			    (void *)ire_dst, (void *)ire_prim,
19093 			    (void *)ipif_prim));
19094 
19095 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19096 			    ire->ire_gateway_addr,
19097 			    IRE_BROADCAST,
19098 			    ipif_prim, ALL_ZONES,
19099 			    NULL,
19100 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19101 			    MATCH_IRE_MASK, ipst);
19102 
19103 			if (bcast_ire != NULL) {
19104 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19105 				    "looked up bcast_ire %p\n",
19106 				    (void *)bcast_ire));
19107 				ipif_remove_ire(bcast_ire->ire_ipif,
19108 				    bcast_ire);
19109 				ire_delete(bcast_ire);
19110 				ire_refrele(bcast_ire);
19111 			}
19112 			ire_refrele(ire_prim);
19113 		}
19114 		ire_refrele(ire_dst);
19115 	}
19116 }
19117 
19118 /*
19119  * IPsec hardware acceleration capabilities related functions.
19120  */
19121 
19122 /*
19123  * Free a per-ill IPsec capabilities structure.
19124  */
19125 static void
19126 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19127 {
19128 	if (capab->auth_hw_algs != NULL)
19129 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19130 	if (capab->encr_hw_algs != NULL)
19131 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19132 	if (capab->encr_algparm != NULL)
19133 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19134 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19135 }
19136 
19137 /*
19138  * Allocate a new per-ill IPsec capabilities structure. This structure
19139  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19140  * an array which specifies, for each algorithm, whether this algorithm
19141  * is supported by the ill or not.
19142  */
19143 static ill_ipsec_capab_t *
19144 ill_ipsec_capab_alloc(void)
19145 {
19146 	ill_ipsec_capab_t *capab;
19147 	uint_t nelems;
19148 
19149 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19150 	if (capab == NULL)
19151 		return (NULL);
19152 
19153 	/* we need one bit per algorithm */
19154 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19155 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19156 
19157 	/* allocate memory to store algorithm flags */
19158 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19159 	if (capab->encr_hw_algs == NULL)
19160 		goto nomem;
19161 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19162 	if (capab->auth_hw_algs == NULL)
19163 		goto nomem;
19164 	/*
19165 	 * Leave encr_algparm NULL for now since we won't need it half
19166 	 * the time
19167 	 */
19168 	return (capab);
19169 
19170 nomem:
19171 	ill_ipsec_capab_free(capab);
19172 	return (NULL);
19173 }
19174 
19175 /*
19176  * Resize capability array.  Since we're exclusive, this is OK.
19177  */
19178 static boolean_t
19179 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19180 {
19181 	ipsec_capab_algparm_t *nalp, *oalp;
19182 	uint32_t olen, nlen;
19183 
19184 	oalp = capab->encr_algparm;
19185 	olen = capab->encr_algparm_size;
19186 
19187 	if (oalp != NULL) {
19188 		if (algid < capab->encr_algparm_end)
19189 			return (B_TRUE);
19190 	}
19191 
19192 	nlen = (algid + 1) * sizeof (*nalp);
19193 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19194 	if (nalp == NULL)
19195 		return (B_FALSE);
19196 
19197 	if (oalp != NULL) {
19198 		bcopy(oalp, nalp, olen);
19199 		kmem_free(oalp, olen);
19200 	}
19201 	capab->encr_algparm = nalp;
19202 	capab->encr_algparm_size = nlen;
19203 	capab->encr_algparm_end = algid + 1;
19204 
19205 	return (B_TRUE);
19206 }
19207 
19208 /*
19209  * Compare the capabilities of the specified ill with the protocol
19210  * and algorithms specified by the SA passed as argument.
19211  * If they match, returns B_TRUE, B_FALSE if they do not match.
19212  *
19213  * The ill can be passed as a pointer to it, or by specifying its index
19214  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19215  *
19216  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19217  * packet is eligible for hardware acceleration, and by
19218  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19219  * to a particular ill.
19220  */
19221 boolean_t
19222 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19223     ipsa_t *sa, netstack_t *ns)
19224 {
19225 	boolean_t sa_isv6;
19226 	uint_t algid;
19227 	struct ill_ipsec_capab_s *cpp;
19228 	boolean_t need_refrele = B_FALSE;
19229 	ip_stack_t	*ipst = ns->netstack_ip;
19230 
19231 	if (ill == NULL) {
19232 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19233 		    NULL, NULL, NULL, ipst);
19234 		if (ill == NULL) {
19235 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19236 			return (B_FALSE);
19237 		}
19238 		need_refrele = B_TRUE;
19239 	}
19240 
19241 	/*
19242 	 * Use the address length specified by the SA to determine
19243 	 * if it corresponds to a IPv6 address, and fail the matching
19244 	 * if the isv6 flag passed as argument does not match.
19245 	 * Note: this check is used for SADB capability checking before
19246 	 * sending SA information to an ill.
19247 	 */
19248 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19249 	if (sa_isv6 != ill_isv6)
19250 		/* protocol mismatch */
19251 		goto done;
19252 
19253 	/*
19254 	 * Check if the ill supports the protocol, algorithm(s) and
19255 	 * key size(s) specified by the SA, and get the pointers to
19256 	 * the algorithms supported by the ill.
19257 	 */
19258 	switch (sa->ipsa_type) {
19259 
19260 	case SADB_SATYPE_ESP:
19261 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19262 			/* ill does not support ESP acceleration */
19263 			goto done;
19264 		cpp = ill->ill_ipsec_capab_esp;
19265 		algid = sa->ipsa_auth_alg;
19266 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19267 			goto done;
19268 		algid = sa->ipsa_encr_alg;
19269 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19270 			goto done;
19271 		if (algid < cpp->encr_algparm_end) {
19272 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19273 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19274 				goto done;
19275 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19276 				goto done;
19277 		}
19278 		break;
19279 
19280 	case SADB_SATYPE_AH:
19281 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19282 			/* ill does not support AH acceleration */
19283 			goto done;
19284 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19285 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19286 			goto done;
19287 		break;
19288 	}
19289 
19290 	if (need_refrele)
19291 		ill_refrele(ill);
19292 	return (B_TRUE);
19293 done:
19294 	if (need_refrele)
19295 		ill_refrele(ill);
19296 	return (B_FALSE);
19297 }
19298 
19299 /*
19300  * Add a new ill to the list of IPsec capable ills.
19301  * Called from ill_capability_ipsec_ack() when an ACK was received
19302  * indicating that IPsec hardware processing was enabled for an ill.
19303  *
19304  * ill must point to the ill for which acceleration was enabled.
19305  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19306  */
19307 static void
19308 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19309 {
19310 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19311 	uint_t sa_type;
19312 	uint_t ipproto;
19313 	ip_stack_t	*ipst = ill->ill_ipst;
19314 
19315 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19316 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19317 
19318 	switch (dl_cap) {
19319 	case DL_CAPAB_IPSEC_AH:
19320 		sa_type = SADB_SATYPE_AH;
19321 		ills = &ipst->ips_ipsec_capab_ills_ah;
19322 		ipproto = IPPROTO_AH;
19323 		break;
19324 	case DL_CAPAB_IPSEC_ESP:
19325 		sa_type = SADB_SATYPE_ESP;
19326 		ills = &ipst->ips_ipsec_capab_ills_esp;
19327 		ipproto = IPPROTO_ESP;
19328 		break;
19329 	}
19330 
19331 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19332 
19333 	/*
19334 	 * Add ill index to list of hardware accelerators. If
19335 	 * already in list, do nothing.
19336 	 */
19337 	for (cur_ill = *ills; cur_ill != NULL &&
19338 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19339 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19340 		;
19341 
19342 	if (cur_ill == NULL) {
19343 		/* if this is a new entry for this ill */
19344 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19345 		if (new_ill == NULL) {
19346 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19347 			return;
19348 		}
19349 
19350 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19351 		new_ill->ill_isv6 = ill->ill_isv6;
19352 		new_ill->next = *ills;
19353 		*ills = new_ill;
19354 	} else if (!sadb_resync) {
19355 		/* not resync'ing SADB and an entry exists for this ill */
19356 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19357 		return;
19358 	}
19359 
19360 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19361 
19362 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19363 		/*
19364 		 * IPsec module for protocol loaded, initiate dump
19365 		 * of the SADB to this ill.
19366 		 */
19367 		sadb_ill_download(ill, sa_type);
19368 }
19369 
19370 /*
19371  * Remove an ill from the list of IPsec capable ills.
19372  */
19373 static void
19374 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19375 {
19376 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19377 	ip_stack_t	*ipst = ill->ill_ipst;
19378 
19379 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19380 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19381 
19382 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19383 	    &ipst->ips_ipsec_capab_ills_esp;
19384 
19385 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19386 
19387 	prev_ill = NULL;
19388 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19389 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19390 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19391 		;
19392 	if (cur_ill == NULL) {
19393 		/* entry not found */
19394 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19395 		return;
19396 	}
19397 	if (prev_ill == NULL) {
19398 		/* entry at front of list */
19399 		*ills = NULL;
19400 	} else {
19401 		prev_ill->next = cur_ill->next;
19402 	}
19403 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19404 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19405 }
19406 
19407 /*
19408  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19409  * supporting the specified IPsec protocol acceleration.
19410  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19411  * We free the mblk and, if sa is non-null, release the held referece.
19412  */
19413 void
19414 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19415     netstack_t *ns)
19416 {
19417 	ipsec_capab_ill_t *ici, *cur_ici;
19418 	ill_t *ill;
19419 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19420 	ip_stack_t	*ipst = ns->netstack_ip;
19421 
19422 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19423 	    ipst->ips_ipsec_capab_ills_esp;
19424 
19425 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19426 
19427 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19428 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19429 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19430 
19431 		/*
19432 		 * Handle the case where the ill goes away while the SADB is
19433 		 * attempting to send messages.  If it's going away, it's
19434 		 * nuking its shadow SADB, so we don't care..
19435 		 */
19436 
19437 		if (ill == NULL)
19438 			continue;
19439 
19440 		if (sa != NULL) {
19441 			/*
19442 			 * Make sure capabilities match before
19443 			 * sending SA to ill.
19444 			 */
19445 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19446 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19447 				ill_refrele(ill);
19448 				continue;
19449 			}
19450 
19451 			mutex_enter(&sa->ipsa_lock);
19452 			sa->ipsa_flags |= IPSA_F_HW;
19453 			mutex_exit(&sa->ipsa_lock);
19454 		}
19455 
19456 		/*
19457 		 * Copy template message, and add it to the front
19458 		 * of the mblk ship list. We want to avoid holding
19459 		 * the ipsec_capab_ills_lock while sending the
19460 		 * message to the ills.
19461 		 *
19462 		 * The b_next and b_prev are temporarily used
19463 		 * to build a list of mblks to be sent down, and to
19464 		 * save the ill to which they must be sent.
19465 		 */
19466 		nmp = copymsg(mp);
19467 		if (nmp == NULL) {
19468 			ill_refrele(ill);
19469 			continue;
19470 		}
19471 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19472 		nmp->b_next = mp_ship_list;
19473 		mp_ship_list = nmp;
19474 		nmp->b_prev = (mblk_t *)ill;
19475 	}
19476 
19477 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19478 
19479 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19480 		/* restore the mblk to a sane state */
19481 		next_mp = nmp->b_next;
19482 		nmp->b_next = NULL;
19483 		ill = (ill_t *)nmp->b_prev;
19484 		nmp->b_prev = NULL;
19485 
19486 		ill_dlpi_send(ill, nmp);
19487 		ill_refrele(ill);
19488 	}
19489 
19490 	if (sa != NULL)
19491 		IPSA_REFRELE(sa);
19492 	freemsg(mp);
19493 }
19494 
19495 /*
19496  * Derive an interface id from the link layer address.
19497  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19498  */
19499 static boolean_t
19500 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19501 {
19502 	char		*addr;
19503 
19504 	if (ill->ill_phys_addr_length != ETHERADDRL)
19505 		return (B_FALSE);
19506 
19507 	/* Form EUI-64 like address */
19508 	addr = (char *)&v6addr->s6_addr32[2];
19509 	bcopy(ill->ill_phys_addr, addr, 3);
19510 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19511 	addr[3] = (char)0xff;
19512 	addr[4] = (char)0xfe;
19513 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19514 	return (B_TRUE);
19515 }
19516 
19517 /* ARGSUSED */
19518 static boolean_t
19519 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19520 {
19521 	return (B_FALSE);
19522 }
19523 
19524 typedef struct ipmp_ifcookie {
19525 	uint32_t	ic_hostid;
19526 	char		ic_ifname[LIFNAMSIZ];
19527 	char		ic_zonename[ZONENAME_MAX];
19528 } ipmp_ifcookie_t;
19529 
19530 /*
19531  * Construct a pseudo-random interface ID for the IPMP interface that's both
19532  * predictable and (almost) guaranteed to be unique.
19533  */
19534 static boolean_t
19535 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19536 {
19537 	zone_t		*zp;
19538 	uint8_t		*addr;
19539 	uchar_t		hash[16];
19540 	ulong_t 	hostid;
19541 	MD5_CTX		ctx;
19542 	ipmp_ifcookie_t	ic = { 0 };
19543 
19544 	ASSERT(IS_IPMP(ill));
19545 
19546 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19547 	ic.ic_hostid = htonl((uint32_t)hostid);
19548 
19549 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19550 
19551 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19552 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19553 		zone_rele(zp);
19554 	}
19555 
19556 	MD5Init(&ctx);
19557 	MD5Update(&ctx, &ic, sizeof (ic));
19558 	MD5Final(hash, &ctx);
19559 
19560 	/*
19561 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19562 	 */
19563 	addr = &v6addr->s6_addr8[8];
19564 	bcopy(hash + 8, addr, sizeof (uint64_t));
19565 	addr[0] &= ~0x2;				/* set local bit */
19566 
19567 	return (B_TRUE);
19568 }
19569 
19570 /* ARGSUSED */
19571 static boolean_t
19572 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19573     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19574 {
19575 	/*
19576 	 * Multicast address mappings used over Ethernet/802.X.
19577 	 * This address is used as a base for mappings.
19578 	 */
19579 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19580 	    0x00, 0x00, 0x00};
19581 
19582 	/*
19583 	 * Extract low order 32 bits from IPv6 multicast address.
19584 	 * Or that into the link layer address, starting from the
19585 	 * second byte.
19586 	 */
19587 	*hw_start = 2;
19588 	v6_extract_mask->s6_addr32[0] = 0;
19589 	v6_extract_mask->s6_addr32[1] = 0;
19590 	v6_extract_mask->s6_addr32[2] = 0;
19591 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19592 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19593 	return (B_TRUE);
19594 }
19595 
19596 /*
19597  * Indicate by return value whether multicast is supported. If not,
19598  * this code should not touch/change any parameters.
19599  */
19600 /* ARGSUSED */
19601 static boolean_t
19602 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19603     uint32_t *hw_start, ipaddr_t *extract_mask)
19604 {
19605 	/*
19606 	 * Multicast address mappings used over Ethernet/802.X.
19607 	 * This address is used as a base for mappings.
19608 	 */
19609 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19610 	    0x00, 0x00, 0x00 };
19611 
19612 	if (phys_length != ETHERADDRL)
19613 		return (B_FALSE);
19614 
19615 	*extract_mask = htonl(0x007fffff);
19616 	*hw_start = 2;
19617 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19618 	return (B_TRUE);
19619 }
19620 
19621 /*
19622  * Derive IPoIB interface id from the link layer address.
19623  */
19624 static boolean_t
19625 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19626 {
19627 	char		*addr;
19628 
19629 	if (ill->ill_phys_addr_length != 20)
19630 		return (B_FALSE);
19631 	addr = (char *)&v6addr->s6_addr32[2];
19632 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19633 	/*
19634 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19635 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19636 	 * rules. In these cases, the IBA considers these GUIDs to be in
19637 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19638 	 * required; vendors are required not to assign global EUI-64's
19639 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19640 	 * of the interface identifier. Whether the GUID is in modified
19641 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19642 	 * bit set to 1.
19643 	 */
19644 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19645 	return (B_TRUE);
19646 }
19647 
19648 /*
19649  * Note on mapping from multicast IP addresses to IPoIB multicast link
19650  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19651  * The format of an IPoIB multicast address is:
19652  *
19653  *  4 byte QPN      Scope Sign.  Pkey
19654  * +--------------------------------------------+
19655  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19656  * +--------------------------------------------+
19657  *
19658  * The Scope and Pkey components are properties of the IBA port and
19659  * network interface. They can be ascertained from the broadcast address.
19660  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19661  */
19662 
19663 static boolean_t
19664 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19665     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19666 {
19667 	/*
19668 	 * Base IPoIB IPv6 multicast address used for mappings.
19669 	 * Does not contain the IBA scope/Pkey values.
19670 	 */
19671 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19672 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19673 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19674 
19675 	/*
19676 	 * Extract low order 80 bits from IPv6 multicast address.
19677 	 * Or that into the link layer address, starting from the
19678 	 * sixth byte.
19679 	 */
19680 	*hw_start = 6;
19681 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19682 
19683 	/*
19684 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19685 	 */
19686 	*(maddr + 5) = *(bphys_addr + 5);
19687 	*(maddr + 8) = *(bphys_addr + 8);
19688 	*(maddr + 9) = *(bphys_addr + 9);
19689 
19690 	v6_extract_mask->s6_addr32[0] = 0;
19691 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19692 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19693 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19694 	return (B_TRUE);
19695 }
19696 
19697 static boolean_t
19698 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19699     uint32_t *hw_start, ipaddr_t *extract_mask)
19700 {
19701 	/*
19702 	 * Base IPoIB IPv4 multicast address used for mappings.
19703 	 * Does not contain the IBA scope/Pkey values.
19704 	 */
19705 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19706 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19707 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19708 
19709 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19710 		return (B_FALSE);
19711 
19712 	/*
19713 	 * Extract low order 28 bits from IPv4 multicast address.
19714 	 * Or that into the link layer address, starting from the
19715 	 * sixteenth byte.
19716 	 */
19717 	*extract_mask = htonl(0x0fffffff);
19718 	*hw_start = 16;
19719 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19720 
19721 	/*
19722 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19723 	 */
19724 	*(maddr + 5) = *(bphys_addr + 5);
19725 	*(maddr + 8) = *(bphys_addr + 8);
19726 	*(maddr + 9) = *(bphys_addr + 9);
19727 	return (B_TRUE);
19728 }
19729 
19730 /*
19731  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19732  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19733  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19734  * the link-local address is preferred.
19735  */
19736 boolean_t
19737 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19738 {
19739 	ipif_t	*ipif;
19740 	ipif_t	*maybe_ipif = NULL;
19741 
19742 	mutex_enter(&ill->ill_lock);
19743 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19744 		mutex_exit(&ill->ill_lock);
19745 		if (ipifp != NULL)
19746 			*ipifp = NULL;
19747 		return (B_FALSE);
19748 	}
19749 
19750 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19751 		if (!IPIF_CAN_LOOKUP(ipif))
19752 			continue;
19753 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19754 		    ipif->ipif_zoneid != ALL_ZONES)
19755 			continue;
19756 		if ((ipif->ipif_flags & flags) != flags)
19757 			continue;
19758 
19759 		if (ipifp == NULL) {
19760 			mutex_exit(&ill->ill_lock);
19761 			ASSERT(maybe_ipif == NULL);
19762 			return (B_TRUE);
19763 		}
19764 		if (!ill->ill_isv6 ||
19765 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19766 			ipif_refhold_locked(ipif);
19767 			mutex_exit(&ill->ill_lock);
19768 			*ipifp = ipif;
19769 			return (B_TRUE);
19770 		}
19771 		if (maybe_ipif == NULL)
19772 			maybe_ipif = ipif;
19773 	}
19774 	if (ipifp != NULL) {
19775 		if (maybe_ipif != NULL)
19776 			ipif_refhold_locked(maybe_ipif);
19777 		*ipifp = maybe_ipif;
19778 	}
19779 	mutex_exit(&ill->ill_lock);
19780 	return (maybe_ipif != NULL);
19781 }
19782 
19783 /*
19784  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19785  * If a pointer to an ipif_t is returned then the caller will need to do
19786  * an ill_refrele().
19787  */
19788 ipif_t *
19789 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19790     ip_stack_t *ipst)
19791 {
19792 	ipif_t *ipif;
19793 	ill_t *ill;
19794 
19795 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19796 	    ipst);
19797 	if (ill == NULL)
19798 		return (NULL);
19799 
19800 	mutex_enter(&ill->ill_lock);
19801 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19802 		mutex_exit(&ill->ill_lock);
19803 		ill_refrele(ill);
19804 		return (NULL);
19805 	}
19806 
19807 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19808 		if (!IPIF_CAN_LOOKUP(ipif))
19809 			continue;
19810 		if (lifidx == ipif->ipif_id) {
19811 			ipif_refhold_locked(ipif);
19812 			break;
19813 		}
19814 	}
19815 
19816 	mutex_exit(&ill->ill_lock);
19817 	ill_refrele(ill);
19818 	return (ipif);
19819 }
19820 
19821 /*
19822  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19823  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19824  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19825  * for details.
19826  */
19827 void
19828 ill_fastpath_flush(ill_t *ill)
19829 {
19830 	ip_stack_t *ipst = ill->ill_ipst;
19831 
19832 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19833 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19834 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19835 }
19836 
19837 /*
19838  * Set the physical address information for `ill' to the contents of the
19839  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
19840  * asynchronous if `ill' cannot immediately be quiesced -- in which case
19841  * EINPROGRESS will be returned.
19842  */
19843 int
19844 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
19845 {
19846 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19847 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
19848 
19849 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19850 
19851 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
19852 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
19853 		/* Changing DL_IPV6_TOKEN is not yet supported */
19854 		return (0);
19855 	}
19856 
19857 	/*
19858 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
19859 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
19860 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
19861 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
19862 	 */
19863 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
19864 		freemsg(mp);
19865 		return (ENOMEM);
19866 	}
19867 
19868 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19869 
19870 	/*
19871 	 * If we can quiesce the ill, then set the address.  If not, then
19872 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
19873 	 */
19874 	ill_down_ipifs(ill, B_TRUE);
19875 	mutex_enter(&ill->ill_lock);
19876 	if (!ill_is_quiescent(ill)) {
19877 		/* call cannot fail since `conn_t *' argument is NULL */
19878 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19879 		    mp, ILL_DOWN);
19880 		mutex_exit(&ill->ill_lock);
19881 		return (EINPROGRESS);
19882 	}
19883 	mutex_exit(&ill->ill_lock);
19884 
19885 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
19886 	return (0);
19887 }
19888 
19889 /*
19890  * Once the ill associated with `q' has quiesced, set its physical address
19891  * information to the values in `addrmp'.  Note that two copies of `addrmp'
19892  * are passed (linked by b_cont), since we sometimes need to save two distinct
19893  * copies in the ill_t, and our context doesn't permit sleeping or allocation
19894  * failure (we'll free the other copy if it's not needed).  Since the ill_t
19895  * is quiesced, we know any stale IREs with the old address information have
19896  * already been removed, so we don't need to call ill_fastpath_flush().
19897  */
19898 /* ARGSUSED */
19899 static void
19900 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
19901 {
19902 	ill_t		*ill = q->q_ptr;
19903 	mblk_t		*addrmp2 = unlinkb(addrmp);
19904 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
19905 	uint_t		addrlen, addroff;
19906 
19907 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19908 
19909 	addroff	= dlindp->dl_addr_offset;
19910 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
19911 
19912 	switch (dlindp->dl_data) {
19913 	case DL_IPV6_LINK_LAYER_ADDR:
19914 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
19915 		freemsg(addrmp2);
19916 		break;
19917 
19918 	case DL_CURR_PHYS_ADDR:
19919 		freemsg(ill->ill_phys_addr_mp);
19920 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
19921 		ill->ill_phys_addr_mp = addrmp;
19922 		ill->ill_phys_addr_length = addrlen;
19923 
19924 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
19925 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
19926 		else
19927 			freemsg(addrmp2);
19928 		break;
19929 	default:
19930 		ASSERT(0);
19931 	}
19932 
19933 	/*
19934 	 * If there are ipifs to bring up, ill_up_ipifs() will return
19935 	 * EINPROGRESS, and ipsq_current_finish() will be called by
19936 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
19937 	 * brought up.
19938 	 */
19939 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
19940 		ipsq_current_finish(ipsq);
19941 }
19942 
19943 /*
19944  * Helper routine for setting the ill_nd_lla fields.
19945  */
19946 void
19947 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
19948 {
19949 	freemsg(ill->ill_nd_lla_mp);
19950 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
19951 	ill->ill_nd_lla_mp = ndmp;
19952 	ill->ill_nd_lla_len = addrlen;
19953 }
19954 
19955 /*
19956  * Replumb the ill.
19957  */
19958 int
19959 ill_replumb(ill_t *ill, mblk_t *mp)
19960 {
19961 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19962 
19963 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19964 
19965 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19966 
19967 	/*
19968 	 * If we can quiesce the ill, then continue.  If not, then
19969 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
19970 	 */
19971 	ill_down_ipifs(ill, B_FALSE);
19972 
19973 	mutex_enter(&ill->ill_lock);
19974 	if (!ill_is_quiescent(ill)) {
19975 		/* call cannot fail since `conn_t *' argument is NULL */
19976 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19977 		    mp, ILL_DOWN);
19978 		mutex_exit(&ill->ill_lock);
19979 		return (EINPROGRESS);
19980 	}
19981 	mutex_exit(&ill->ill_lock);
19982 
19983 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
19984 	return (0);
19985 }
19986 
19987 /* ARGSUSED */
19988 static void
19989 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
19990 {
19991 	ill_t *ill = q->q_ptr;
19992 
19993 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19994 
19995 	ill_down_ipifs_tail(ill);
19996 
19997 	freemsg(ill->ill_replumb_mp);
19998 	ill->ill_replumb_mp = copyb(mp);
19999 
20000 	/*
20001 	 * Successfully quiesced and brought down the interface, now we send
20002 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
20003 	 * DL_NOTE_REPLUMB message.
20004 	 */
20005 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
20006 	    DL_NOTIFY_CONF);
20007 	ASSERT(mp != NULL);
20008 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
20009 	    DL_NOTE_REPLUMB_DONE;
20010 	ill_dlpi_send(ill, mp);
20011 
20012 	/*
20013 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20014 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20015 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20016 	 * brought up.
20017 	 */
20018 	if (ill->ill_replumb_mp == NULL ||
20019 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) != EINPROGRESS) {
20020 		ipsq_current_finish(ipsq);
20021 	}
20022 }
20023 
20024 major_t IP_MAJ;
20025 #define	IP	"ip"
20026 
20027 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20028 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20029 
20030 /*
20031  * Issue REMOVEIF ioctls to have the loopback interfaces
20032  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20033  * the former going away when the user-level processes in the zone
20034  * are killed  * and the latter are cleaned up by the stream head
20035  * str_stack_shutdown callback that undoes all I_PLINKs.
20036  */
20037 void
20038 ip_loopback_cleanup(ip_stack_t *ipst)
20039 {
20040 	int error;
20041 	ldi_handle_t	lh = NULL;
20042 	ldi_ident_t	li = NULL;
20043 	int		rval;
20044 	cred_t		*cr;
20045 	struct strioctl iocb;
20046 	struct lifreq	lifreq;
20047 
20048 	IP_MAJ = ddi_name_to_major(IP);
20049 
20050 #ifdef NS_DEBUG
20051 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20052 	    ipst->ips_netstack->netstack_stackid);
20053 #endif
20054 
20055 	bzero(&lifreq, sizeof (lifreq));
20056 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20057 
20058 	error = ldi_ident_from_major(IP_MAJ, &li);
20059 	if (error) {
20060 #ifdef DEBUG
20061 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20062 		    error);
20063 #endif
20064 		return;
20065 	}
20066 
20067 	cr = zone_get_kcred(netstackid_to_zoneid(
20068 	    ipst->ips_netstack->netstack_stackid));
20069 	ASSERT(cr != NULL);
20070 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20071 	if (error) {
20072 #ifdef DEBUG
20073 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20074 		    error);
20075 #endif
20076 		goto out;
20077 	}
20078 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20079 	iocb.ic_timout = 15;
20080 	iocb.ic_len = sizeof (lifreq);
20081 	iocb.ic_dp = (char *)&lifreq;
20082 
20083 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20084 	/* LINTED - statement has no consequent */
20085 	if (error) {
20086 #ifdef NS_DEBUG
20087 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20088 		    "UDP6 error %d\n", error);
20089 #endif
20090 	}
20091 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20092 	lh = NULL;
20093 
20094 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20095 	if (error) {
20096 #ifdef NS_DEBUG
20097 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20098 		    error);
20099 #endif
20100 		goto out;
20101 	}
20102 
20103 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20104 	iocb.ic_timout = 15;
20105 	iocb.ic_len = sizeof (lifreq);
20106 	iocb.ic_dp = (char *)&lifreq;
20107 
20108 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20109 	/* LINTED - statement has no consequent */
20110 	if (error) {
20111 #ifdef NS_DEBUG
20112 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20113 		    "UDP error %d\n", error);
20114 #endif
20115 	}
20116 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20117 	lh = NULL;
20118 
20119 out:
20120 	/* Close layered handles */
20121 	if (lh)
20122 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20123 	if (li)
20124 		ldi_ident_release(li);
20125 
20126 	crfree(cr);
20127 }
20128 
20129 /*
20130  * This needs to be in-sync with nic_event_t definition
20131  */
20132 static const char *
20133 ill_hook_event2str(nic_event_t event)
20134 {
20135 	switch (event) {
20136 	case NE_PLUMB:
20137 		return ("PLUMB");
20138 	case NE_UNPLUMB:
20139 		return ("UNPLUMB");
20140 	case NE_UP:
20141 		return ("UP");
20142 	case NE_DOWN:
20143 		return ("DOWN");
20144 	case NE_ADDRESS_CHANGE:
20145 		return ("ADDRESS_CHANGE");
20146 	case NE_LIF_UP:
20147 		return ("LIF_UP");
20148 	case NE_LIF_DOWN:
20149 		return ("LIF_DOWN");
20150 	default:
20151 		return ("UNKNOWN");
20152 	}
20153 }
20154 
20155 void
20156 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20157     nic_event_data_t data, size_t datalen)
20158 {
20159 	ip_stack_t		*ipst = ill->ill_ipst;
20160 	hook_nic_event_int_t	*info;
20161 	const char		*str = NULL;
20162 
20163 	/* create a new nic event info */
20164 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20165 		goto fail;
20166 
20167 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20168 	info->hnei_event.hne_lif = lif;
20169 	info->hnei_event.hne_event = event;
20170 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20171 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20172 	info->hnei_event.hne_data = NULL;
20173 	info->hnei_event.hne_datalen = 0;
20174 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20175 
20176 	if (data != NULL && datalen != 0) {
20177 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20178 		if (info->hnei_event.hne_data == NULL)
20179 			goto fail;
20180 		bcopy(data, info->hnei_event.hne_data, datalen);
20181 		info->hnei_event.hne_datalen = datalen;
20182 	}
20183 
20184 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20185 	    DDI_NOSLEEP) == DDI_SUCCESS)
20186 		return;
20187 
20188 fail:
20189 	if (info != NULL) {
20190 		if (info->hnei_event.hne_data != NULL) {
20191 			kmem_free(info->hnei_event.hne_data,
20192 			    info->hnei_event.hne_datalen);
20193 		}
20194 		kmem_free(info, sizeof (hook_nic_event_t));
20195 	}
20196 	str = ill_hook_event2str(event);
20197 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20198 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20199 }
20200 
20201 void
20202 ipif_up_notify(ipif_t *ipif)
20203 {
20204 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20205 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20206 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20207 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20208 	    NE_LIF_UP, NULL, 0);
20209 }
20210