xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 7b79d84636ec82b45f00c982cf6810db81852d17)
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  * Return a pointer to the ill which matches the supplied name.  Note that
4710  * the ill name length includes the null termination character.  (May be
4711  * called as writer.)
4712  * If do_alloc and the interface is "lo0" it will be automatically created.
4713  * Cannot bump up reference on condemned ills. So dup detect can't be done
4714  * using this func.
4715  */
4716 ill_t *
4717 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4718     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4719     ip_stack_t *ipst)
4720 {
4721 	ill_t	*ill;
4722 	ipif_t	*ipif;
4723 	ipsq_t	*ipsq;
4724 	kstat_named_t	*kn;
4725 	boolean_t isloopback;
4726 	in6_addr_t ov6addr;
4727 
4728 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4729 
4730 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4731 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4732 	rw_exit(&ipst->ips_ill_g_lock);
4733 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4734 		return (ill);
4735 
4736 	/*
4737 	 * Couldn't find it.  Does this happen to be a lookup for the
4738 	 * loopback device and are we allowed to allocate it?
4739 	 */
4740 	if (!isloopback || !do_alloc)
4741 		return (NULL);
4742 
4743 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4744 
4745 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4746 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4747 		rw_exit(&ipst->ips_ill_g_lock);
4748 		return (ill);
4749 	}
4750 
4751 	/* Create the loopback device on demand */
4752 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4753 	    sizeof (ipif_loopback_name), BPRI_MED));
4754 	if (ill == NULL)
4755 		goto done;
4756 
4757 	*ill = ill_null;
4758 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4759 	ill->ill_ipst = ipst;
4760 	netstack_hold(ipst->ips_netstack);
4761 	/*
4762 	 * For exclusive stacks we set the zoneid to zero
4763 	 * to make IP operate as if in the global zone.
4764 	 */
4765 	ill->ill_zoneid = GLOBAL_ZONEID;
4766 
4767 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4768 	if (ill->ill_phyint == NULL)
4769 		goto done;
4770 
4771 	if (isv6)
4772 		ill->ill_phyint->phyint_illv6 = ill;
4773 	else
4774 		ill->ill_phyint->phyint_illv4 = ill;
4775 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4776 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4777 	/* Add room for tcp+ip headers */
4778 	if (isv6) {
4779 		ill->ill_isv6 = B_TRUE;
4780 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4781 	} else {
4782 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4783 	}
4784 	if (!ill_allocate_mibs(ill))
4785 		goto done;
4786 	ill->ill_max_mtu = ill->ill_max_frag;
4787 	/*
4788 	 * ipif_loopback_name can't be pointed at directly because its used
4789 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4790 	 * from the glist, ill_glist_delete() sets the first character of
4791 	 * ill_name to '\0'.
4792 	 */
4793 	ill->ill_name = (char *)ill + sizeof (*ill);
4794 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4795 	ill->ill_name_length = sizeof (ipif_loopback_name);
4796 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4797 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4798 
4799 	ill->ill_global_timer = INFINITY;
4800 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4801 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4802 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4803 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4804 
4805 	/* No resolver here. */
4806 	ill->ill_net_type = IRE_LOOPBACK;
4807 
4808 	/* Initialize the ipsq */
4809 	if (!ipsq_init(ill, B_FALSE))
4810 		goto done;
4811 
4812 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4813 	if (ipif == NULL)
4814 		goto done;
4815 
4816 	ill->ill_flags = ILLF_MULTICAST;
4817 
4818 	ov6addr = ipif->ipif_v6lcl_addr;
4819 	/* Set up default loopback address and mask. */
4820 	if (!isv6) {
4821 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4822 
4823 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4824 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4825 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4826 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4827 		    ipif->ipif_v6subnet);
4828 		ill->ill_flags |= ILLF_IPV4;
4829 	} else {
4830 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4831 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4832 		ipif->ipif_v6net_mask = ipv6_all_ones;
4833 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4834 		    ipif->ipif_v6subnet);
4835 		ill->ill_flags |= ILLF_IPV6;
4836 	}
4837 
4838 	/*
4839 	 * Chain us in at the end of the ill list. hold the ill
4840 	 * before we make it globally visible. 1 for the lookup.
4841 	 */
4842 	ill->ill_refcnt = 0;
4843 	ill_refhold(ill);
4844 
4845 	ill->ill_frag_count = 0;
4846 	ill->ill_frag_free_num_pkts = 0;
4847 	ill->ill_last_frag_clean_time = 0;
4848 
4849 	ipsq = ill->ill_phyint->phyint_ipsq;
4850 
4851 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4852 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4853 
4854 	/* Let SCTP know so that it can add this to its list */
4855 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4856 
4857 	/*
4858 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4859 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4860 	 * requires to be after ill_glist_insert() since we need the
4861 	 * ill_index set. Pass on ipv6_loopback as the old address.
4862 	 */
4863 	sctp_update_ipif_addr(ipif, ov6addr);
4864 
4865 	/*
4866 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4867 	 * If so, free our original one.
4868 	 */
4869 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4870 		ipsq_delete(ipsq);
4871 
4872 	/*
4873 	 * Delay this till the ipif is allocated as ipif_allocate
4874 	 * de-references ill_phyint for getting the ifindex. We
4875 	 * can't do this before ipif_allocate because ill_phyint_reinit
4876 	 * -> phyint_assign_ifindex expects ipif to be present.
4877 	 */
4878 	mutex_enter(&ill->ill_phyint->phyint_lock);
4879 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4880 	mutex_exit(&ill->ill_phyint->phyint_lock);
4881 
4882 	if (ipst->ips_loopback_ksp == NULL) {
4883 		/* Export loopback interface statistics */
4884 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4885 		    ipif_loopback_name, "net",
4886 		    KSTAT_TYPE_NAMED, 2, 0,
4887 		    ipst->ips_netstack->netstack_stackid);
4888 		if (ipst->ips_loopback_ksp != NULL) {
4889 			ipst->ips_loopback_ksp->ks_update =
4890 			    loopback_kstat_update;
4891 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4892 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4893 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4894 			ipst->ips_loopback_ksp->ks_private =
4895 			    (void *)(uintptr_t)ipst->ips_netstack->
4896 			    netstack_stackid;
4897 			kstat_install(ipst->ips_loopback_ksp);
4898 		}
4899 	}
4900 
4901 	if (error != NULL)
4902 		*error = 0;
4903 	*did_alloc = B_TRUE;
4904 	rw_exit(&ipst->ips_ill_g_lock);
4905 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4906 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4907 	return (ill);
4908 done:
4909 	if (ill != NULL) {
4910 		if (ill->ill_phyint != NULL) {
4911 			ipsq = ill->ill_phyint->phyint_ipsq;
4912 			if (ipsq != NULL) {
4913 				ipsq->ipsq_phyint = NULL;
4914 				ipsq_delete(ipsq);
4915 			}
4916 			mi_free(ill->ill_phyint);
4917 		}
4918 		ill_free_mib(ill);
4919 		if (ill->ill_ipst != NULL)
4920 			netstack_rele(ill->ill_ipst->ips_netstack);
4921 		mi_free(ill);
4922 	}
4923 	rw_exit(&ipst->ips_ill_g_lock);
4924 	if (error != NULL)
4925 		*error = ENOMEM;
4926 	return (NULL);
4927 }
4928 
4929 /*
4930  * For IPP calls - use the ip_stack_t for global stack.
4931  */
4932 ill_t *
4933 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4934     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4935 {
4936 	ip_stack_t	*ipst;
4937 	ill_t		*ill;
4938 
4939 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4940 	if (ipst == NULL) {
4941 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4942 		return (NULL);
4943 	}
4944 
4945 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4946 	netstack_rele(ipst->ips_netstack);
4947 	return (ill);
4948 }
4949 
4950 /*
4951  * Return a pointer to the ill which matches the index and IP version type.
4952  */
4953 ill_t *
4954 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4955     ipsq_func_t func, int *err, ip_stack_t *ipst)
4956 {
4957 	ill_t	*ill;
4958 	ipsq_t  *ipsq;
4959 	phyint_t *phyi;
4960 
4961 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4962 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4963 
4964 	if (err != NULL)
4965 		*err = 0;
4966 
4967 	/*
4968 	 * Indexes are stored in the phyint - a common structure
4969 	 * to both IPv4 and IPv6.
4970 	 */
4971 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4972 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4973 	    (void *) &index, NULL);
4974 	if (phyi != NULL) {
4975 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4976 		if (ill != NULL) {
4977 			/*
4978 			 * The block comment at the start of ipif_down
4979 			 * explains the use of the macros used below
4980 			 */
4981 			GRAB_CONN_LOCK(q);
4982 			mutex_enter(&ill->ill_lock);
4983 			if (ILL_CAN_LOOKUP(ill)) {
4984 				ill_refhold_locked(ill);
4985 				mutex_exit(&ill->ill_lock);
4986 				RELEASE_CONN_LOCK(q);
4987 				rw_exit(&ipst->ips_ill_g_lock);
4988 				return (ill);
4989 			} else if (ILL_CAN_WAIT(ill, q)) {
4990 				ipsq = ill->ill_phyint->phyint_ipsq;
4991 				mutex_enter(&ipsq->ipsq_lock);
4992 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
4993 				rw_exit(&ipst->ips_ill_g_lock);
4994 				mutex_exit(&ill->ill_lock);
4995 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4996 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
4997 				mutex_exit(&ipsq->ipsq_lock);
4998 				RELEASE_CONN_LOCK(q);
4999 				if (err != NULL)
5000 					*err = EINPROGRESS;
5001 				return (NULL);
5002 			}
5003 			RELEASE_CONN_LOCK(q);
5004 			mutex_exit(&ill->ill_lock);
5005 		}
5006 	}
5007 	rw_exit(&ipst->ips_ill_g_lock);
5008 	if (err != NULL)
5009 		*err = ENXIO;
5010 	return (NULL);
5011 }
5012 
5013 /*
5014  * Return the ifindex next in sequence after the passed in ifindex.
5015  * If there is no next ifindex for the given protocol, return 0.
5016  */
5017 uint_t
5018 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5019 {
5020 	phyint_t *phyi;
5021 	phyint_t *phyi_initial;
5022 	uint_t   ifindex;
5023 
5024 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5025 
5026 	if (index == 0) {
5027 		phyi = avl_first(
5028 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5029 	} else {
5030 		phyi = phyi_initial = avl_find(
5031 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5032 		    (void *) &index, NULL);
5033 	}
5034 
5035 	for (; phyi != NULL;
5036 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5037 	    phyi, AVL_AFTER)) {
5038 		/*
5039 		 * If we're not returning the first interface in the tree
5040 		 * and we still haven't moved past the phyint_t that
5041 		 * corresponds to index, avl_walk needs to be called again
5042 		 */
5043 		if (!((index != 0) && (phyi == phyi_initial))) {
5044 			if (isv6) {
5045 				if ((phyi->phyint_illv6) &&
5046 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5047 				    (phyi->phyint_illv6->ill_isv6 == 1))
5048 					break;
5049 			} else {
5050 				if ((phyi->phyint_illv4) &&
5051 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5052 				    (phyi->phyint_illv4->ill_isv6 == 0))
5053 					break;
5054 			}
5055 		}
5056 	}
5057 
5058 	rw_exit(&ipst->ips_ill_g_lock);
5059 
5060 	if (phyi != NULL)
5061 		ifindex = phyi->phyint_ifindex;
5062 	else
5063 		ifindex = 0;
5064 
5065 	return (ifindex);
5066 }
5067 
5068 /*
5069  * Return the ifindex for the named interface.
5070  * If there is no next ifindex for the interface, return 0.
5071  */
5072 uint_t
5073 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5074 {
5075 	phyint_t	*phyi;
5076 	avl_index_t	where = 0;
5077 	uint_t		ifindex;
5078 
5079 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5080 
5081 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5082 	    name, &where)) == NULL) {
5083 		rw_exit(&ipst->ips_ill_g_lock);
5084 		return (0);
5085 	}
5086 
5087 	ifindex = phyi->phyint_ifindex;
5088 
5089 	rw_exit(&ipst->ips_ill_g_lock);
5090 
5091 	return (ifindex);
5092 }
5093 
5094 /*
5095  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5096  * that gives a running thread a reference to the ill. This reference must be
5097  * released by the thread when it is done accessing the ill and related
5098  * objects. ill_refcnt can not be used to account for static references
5099  * such as other structures pointing to an ill. Callers must generally
5100  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5101  * or be sure that the ill is not being deleted or changing state before
5102  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5103  * ill won't change any of its critical state such as address, netmask etc.
5104  */
5105 void
5106 ill_refhold(ill_t *ill)
5107 {
5108 	mutex_enter(&ill->ill_lock);
5109 	ill->ill_refcnt++;
5110 	ILL_TRACE_REF(ill);
5111 	mutex_exit(&ill->ill_lock);
5112 }
5113 
5114 void
5115 ill_refhold_locked(ill_t *ill)
5116 {
5117 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5118 	ill->ill_refcnt++;
5119 	ILL_TRACE_REF(ill);
5120 }
5121 
5122 int
5123 ill_check_and_refhold(ill_t *ill)
5124 {
5125 	mutex_enter(&ill->ill_lock);
5126 	if (ILL_CAN_LOOKUP(ill)) {
5127 		ill_refhold_locked(ill);
5128 		mutex_exit(&ill->ill_lock);
5129 		return (0);
5130 	}
5131 	mutex_exit(&ill->ill_lock);
5132 	return (ILL_LOOKUP_FAILED);
5133 }
5134 
5135 /*
5136  * Must not be called while holding any locks. Otherwise if this is
5137  * the last reference to be released, there is a chance of recursive mutex
5138  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5139  * to restart an ioctl.
5140  */
5141 void
5142 ill_refrele(ill_t *ill)
5143 {
5144 	mutex_enter(&ill->ill_lock);
5145 	ASSERT(ill->ill_refcnt != 0);
5146 	ill->ill_refcnt--;
5147 	ILL_UNTRACE_REF(ill);
5148 	if (ill->ill_refcnt != 0) {
5149 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5150 		mutex_exit(&ill->ill_lock);
5151 		return;
5152 	}
5153 
5154 	/* Drops the ill_lock */
5155 	ipif_ill_refrele_tail(ill);
5156 }
5157 
5158 /*
5159  * Obtain a weak reference count on the ill. This reference ensures the
5160  * ill won't be freed, but the ill may change any of its critical state
5161  * such as netmask, address etc. Returns an error if the ill has started
5162  * closing.
5163  */
5164 boolean_t
5165 ill_waiter_inc(ill_t *ill)
5166 {
5167 	mutex_enter(&ill->ill_lock);
5168 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5169 		mutex_exit(&ill->ill_lock);
5170 		return (B_FALSE);
5171 	}
5172 	ill->ill_waiters++;
5173 	mutex_exit(&ill->ill_lock);
5174 	return (B_TRUE);
5175 }
5176 
5177 void
5178 ill_waiter_dcr(ill_t *ill)
5179 {
5180 	mutex_enter(&ill->ill_lock);
5181 	ill->ill_waiters--;
5182 	if (ill->ill_waiters == 0)
5183 		cv_broadcast(&ill->ill_cv);
5184 	mutex_exit(&ill->ill_lock);
5185 }
5186 
5187 /*
5188  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5189  * driver.  We construct best guess defaults for lower level information that
5190  * we need.  If an interface is brought up without injection of any overriding
5191  * information from outside, we have to be ready to go with these defaults.
5192  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5193  * we primarely want the dl_provider_style.
5194  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5195  * at which point we assume the other part of the information is valid.
5196  */
5197 void
5198 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5199 {
5200 	uchar_t		*brdcst_addr;
5201 	uint_t		brdcst_addr_length, phys_addr_length;
5202 	t_scalar_t	sap_length;
5203 	dl_info_ack_t	*dlia;
5204 	ip_m_t		*ipm;
5205 	dl_qos_cl_sel1_t *sel1;
5206 	int		min_mtu;
5207 
5208 	ASSERT(IAM_WRITER_ILL(ill));
5209 
5210 	/*
5211 	 * Till the ill is fully up ILL_CHANGING will be set and
5212 	 * the ill is not globally visible. So no need for a lock.
5213 	 */
5214 	dlia = (dl_info_ack_t *)mp->b_rptr;
5215 	ill->ill_mactype = dlia->dl_mac_type;
5216 
5217 	ipm = ip_m_lookup(dlia->dl_mac_type);
5218 	if (ipm == NULL) {
5219 		ipm = ip_m_lookup(DL_OTHER);
5220 		ASSERT(ipm != NULL);
5221 	}
5222 	ill->ill_media = ipm;
5223 
5224 	/*
5225 	 * When the new DLPI stuff is ready we'll pull lengths
5226 	 * from dlia.
5227 	 */
5228 	if (dlia->dl_version == DL_VERSION_2) {
5229 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5230 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5231 		    brdcst_addr_length);
5232 		if (brdcst_addr == NULL) {
5233 			brdcst_addr_length = 0;
5234 		}
5235 		sap_length = dlia->dl_sap_length;
5236 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5237 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5238 		    brdcst_addr_length, sap_length, phys_addr_length));
5239 	} else {
5240 		brdcst_addr_length = 6;
5241 		brdcst_addr = ip_six_byte_all_ones;
5242 		sap_length = -2;
5243 		phys_addr_length = brdcst_addr_length;
5244 	}
5245 
5246 	ill->ill_bcast_addr_length = brdcst_addr_length;
5247 	ill->ill_phys_addr_length = phys_addr_length;
5248 	ill->ill_sap_length = sap_length;
5249 
5250 	/*
5251 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5252 	 * but we must ensure a minimum IP MTU is used since other bits of
5253 	 * IP will fly apart otherwise.
5254 	 */
5255 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5256 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5257 	ill->ill_max_mtu = ill->ill_max_frag;
5258 
5259 	ill->ill_type = ipm->ip_m_type;
5260 
5261 	if (!ill->ill_dlpi_style_set) {
5262 		if (dlia->dl_provider_style == DL_STYLE2)
5263 			ill->ill_needs_attach = 1;
5264 
5265 		/*
5266 		 * Allocate the first ipif on this ill. We don't delay it
5267 		 * further as ioctl handling assumes atleast one ipif to
5268 		 * be present.
5269 		 *
5270 		 * At this point we don't know whether the ill is v4 or v6.
5271 		 * We will know this whan the SIOCSLIFNAME happens and
5272 		 * the correct value for ill_isv6 will be assigned in
5273 		 * ipif_set_values(). We need to hold the ill lock and
5274 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5275 		 * the wakeup.
5276 		 */
5277 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5278 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5279 		mutex_enter(&ill->ill_lock);
5280 		ASSERT(ill->ill_dlpi_style_set == 0);
5281 		ill->ill_dlpi_style_set = 1;
5282 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5283 		cv_broadcast(&ill->ill_cv);
5284 		mutex_exit(&ill->ill_lock);
5285 		freemsg(mp);
5286 		return;
5287 	}
5288 	ASSERT(ill->ill_ipif != NULL);
5289 	/*
5290 	 * We know whether it is IPv4 or IPv6 now, as this is the
5291 	 * second DL_INFO_ACK we are recieving in response to the
5292 	 * DL_INFO_REQ sent in ipif_set_values.
5293 	 */
5294 	if (ill->ill_isv6)
5295 		ill->ill_sap = IP6_DL_SAP;
5296 	else
5297 		ill->ill_sap = IP_DL_SAP;
5298 	/*
5299 	 * Set ipif_mtu which is used to set the IRE's
5300 	 * ire_max_frag value. The driver could have sent
5301 	 * a different mtu from what it sent last time. No
5302 	 * need to call ipif_mtu_change because IREs have
5303 	 * not yet been created.
5304 	 */
5305 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5306 	/*
5307 	 * Clear all the flags that were set based on ill_bcast_addr_length
5308 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5309 	 * changed now and we need to re-evaluate.
5310 	 */
5311 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5312 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5313 
5314 	/*
5315 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5316 	 * changed now.
5317 	 *
5318 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5319 	 * with no underlying interfaces (and thus an unknown broadcast
5320 	 * address length), but we enforce that an interface is broadcast-
5321 	 * capable as part of allowing it to join a group.
5322 	 */
5323 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5324 		if (ill->ill_resolver_mp != NULL)
5325 			freemsg(ill->ill_resolver_mp);
5326 		if (ill->ill_bcast_mp != NULL)
5327 			freemsg(ill->ill_bcast_mp);
5328 		if (ill->ill_flags & ILLF_XRESOLV)
5329 			ill->ill_net_type = IRE_IF_RESOLVER;
5330 		else
5331 			ill->ill_net_type = IRE_IF_NORESOLVER;
5332 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5333 		    ill->ill_phys_addr_length,
5334 		    ill->ill_sap,
5335 		    ill->ill_sap_length);
5336 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5337 
5338 		if (ill->ill_isv6)
5339 			/*
5340 			 * Note: xresolv interfaces will eventually need NOARP
5341 			 * set here as well, but that will require those
5342 			 * external resolvers to have some knowledge of
5343 			 * that flag and act appropriately. Not to be changed
5344 			 * at present.
5345 			 */
5346 			ill->ill_flags |= ILLF_NONUD;
5347 		else
5348 			ill->ill_flags |= ILLF_NOARP;
5349 
5350 		if (ill->ill_phys_addr_length == 0) {
5351 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5352 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5353 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5354 			} else {
5355 				/* pt-pt supports multicast. */
5356 				ill->ill_flags |= ILLF_MULTICAST;
5357 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5358 			}
5359 		}
5360 	} else {
5361 		ill->ill_net_type = IRE_IF_RESOLVER;
5362 		if (ill->ill_bcast_mp != NULL)
5363 			freemsg(ill->ill_bcast_mp);
5364 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5365 		    ill->ill_bcast_addr_length, ill->ill_sap,
5366 		    ill->ill_sap_length);
5367 		/*
5368 		 * Later detect lack of DLPI driver multicast
5369 		 * capability by catching DL_ENABMULTI errors in
5370 		 * ip_rput_dlpi.
5371 		 */
5372 		ill->ill_flags |= ILLF_MULTICAST;
5373 		if (!ill->ill_isv6)
5374 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5375 	}
5376 
5377 	/* For IPMP, PHYI_IPMP should already be set by ipif_allocate() */
5378 	if (ill->ill_mactype == SUNW_DL_IPMP)
5379 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5380 
5381 	/* By default an interface does not support any CoS marking */
5382 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5383 
5384 	/*
5385 	 * If we get QoS information in DL_INFO_ACK, the device supports
5386 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5387 	 */
5388 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5389 	    dlia->dl_qos_length);
5390 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5391 		ill->ill_flags |= ILLF_COS_ENABLED;
5392 	}
5393 
5394 	/* Clear any previous error indication. */
5395 	ill->ill_error = 0;
5396 	freemsg(mp);
5397 }
5398 
5399 /*
5400  * Perform various checks to verify that an address would make sense as a
5401  * local, remote, or subnet interface address.
5402  */
5403 static boolean_t
5404 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5405 {
5406 	ipaddr_t	net_mask;
5407 
5408 	/*
5409 	 * Don't allow all zeroes, or all ones, but allow
5410 	 * all ones netmask.
5411 	 */
5412 	if ((net_mask = ip_net_mask(addr)) == 0)
5413 		return (B_FALSE);
5414 	/* A given netmask overrides the "guess" netmask */
5415 	if (subnet_mask != 0)
5416 		net_mask = subnet_mask;
5417 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5418 	    (addr == (addr | ~net_mask)))) {
5419 		return (B_FALSE);
5420 	}
5421 
5422 	/*
5423 	 * Even if the netmask is all ones, we do not allow address to be
5424 	 * 255.255.255.255
5425 	 */
5426 	if (addr == INADDR_BROADCAST)
5427 		return (B_FALSE);
5428 
5429 	if (CLASSD(addr))
5430 		return (B_FALSE);
5431 
5432 	return (B_TRUE);
5433 }
5434 
5435 #define	V6_IPIF_LINKLOCAL(p)	\
5436 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5437 
5438 /*
5439  * Compare two given ipifs and check if the second one is better than
5440  * the first one using the order of preference (not taking deprecated
5441  * into acount) specified in ipif_lookup_multicast().
5442  */
5443 static boolean_t
5444 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5445 {
5446 	/* Check the least preferred first. */
5447 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5448 		/* If both ipifs are the same, use the first one. */
5449 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5450 			return (B_FALSE);
5451 		else
5452 			return (B_TRUE);
5453 	}
5454 
5455 	/* For IPv6, check for link local address. */
5456 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5457 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5458 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5459 			/* The second one is equal or less preferred. */
5460 			return (B_FALSE);
5461 		} else {
5462 			return (B_TRUE);
5463 		}
5464 	}
5465 
5466 	/* Then check for point to point interface. */
5467 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5468 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5469 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5470 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5471 			return (B_FALSE);
5472 		} else {
5473 			return (B_TRUE);
5474 		}
5475 	}
5476 
5477 	/* old_ipif is a normal interface, so no need to use the new one. */
5478 	return (B_FALSE);
5479 }
5480 
5481 /*
5482  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5483  * The ipif must be up, and its ill must multicast-capable, not
5484  * condemned, not an underlying interface in an IPMP group, and
5485  * not a VNI interface.  Order of preference:
5486  *
5487  * 	1a. normal
5488  * 	1b. normal, but deprecated
5489  * 	2a. point to point
5490  * 	2b. point to point, but deprecated
5491  * 	3a. link local
5492  * 	3b. link local, but deprecated
5493  * 	4. loopback.
5494  */
5495 ipif_t *
5496 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5497 {
5498 	ill_t			*ill;
5499 	ill_walk_context_t	ctx;
5500 	ipif_t			*ipif;
5501 	ipif_t			*saved_ipif = NULL;
5502 	ipif_t			*dep_ipif = NULL;
5503 
5504 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5505 	if (isv6)
5506 		ill = ILL_START_WALK_V6(&ctx, ipst);
5507 	else
5508 		ill = ILL_START_WALK_V4(&ctx, ipst);
5509 
5510 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5511 		mutex_enter(&ill->ill_lock);
5512 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5513 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5514 			mutex_exit(&ill->ill_lock);
5515 			continue;
5516 		}
5517 		for (ipif = ill->ill_ipif; ipif != NULL;
5518 		    ipif = ipif->ipif_next) {
5519 			if (zoneid != ipif->ipif_zoneid &&
5520 			    zoneid != ALL_ZONES &&
5521 			    ipif->ipif_zoneid != ALL_ZONES) {
5522 				continue;
5523 			}
5524 			if (!(ipif->ipif_flags & IPIF_UP) ||
5525 			    !IPIF_CAN_LOOKUP(ipif)) {
5526 				continue;
5527 			}
5528 
5529 			/*
5530 			 * Found one candidate.  If it is deprecated,
5531 			 * remember it in dep_ipif.  If it is not deprecated,
5532 			 * remember it in saved_ipif.
5533 			 */
5534 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5535 				if (dep_ipif == NULL) {
5536 					dep_ipif = ipif;
5537 				} else if (ipif_comp_multi(dep_ipif, ipif,
5538 				    isv6)) {
5539 					/*
5540 					 * If the previous dep_ipif does not
5541 					 * belong to the same ill, we've done
5542 					 * a ipif_refhold() on it.  So we need
5543 					 * to release it.
5544 					 */
5545 					if (dep_ipif->ipif_ill != ill)
5546 						ipif_refrele(dep_ipif);
5547 					dep_ipif = ipif;
5548 				}
5549 				continue;
5550 			}
5551 			if (saved_ipif == NULL) {
5552 				saved_ipif = ipif;
5553 			} else {
5554 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5555 					if (saved_ipif->ipif_ill != ill)
5556 						ipif_refrele(saved_ipif);
5557 					saved_ipif = ipif;
5558 				}
5559 			}
5560 		}
5561 		/*
5562 		 * Before going to the next ill, do a ipif_refhold() on the
5563 		 * saved ones.
5564 		 */
5565 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5566 			ipif_refhold_locked(saved_ipif);
5567 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5568 			ipif_refhold_locked(dep_ipif);
5569 		mutex_exit(&ill->ill_lock);
5570 	}
5571 	rw_exit(&ipst->ips_ill_g_lock);
5572 
5573 	/*
5574 	 * If we have only the saved_ipif, return it.  But if we have both
5575 	 * saved_ipif and dep_ipif, check to see which one is better.
5576 	 */
5577 	if (saved_ipif != NULL) {
5578 		if (dep_ipif != NULL) {
5579 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5580 				ipif_refrele(saved_ipif);
5581 				return (dep_ipif);
5582 			} else {
5583 				ipif_refrele(dep_ipif);
5584 				return (saved_ipif);
5585 			}
5586 		}
5587 		return (saved_ipif);
5588 	} else {
5589 		return (dep_ipif);
5590 	}
5591 }
5592 
5593 /*
5594  * This function is called when an application does not specify an interface
5595  * to be used for multicast traffic (joining a group/sending data).  It
5596  * calls ire_lookup_multi() to look for an interface route for the
5597  * specified multicast group.  Doing this allows the administrator to add
5598  * prefix routes for multicast to indicate which interface to be used for
5599  * multicast traffic in the above scenario.  The route could be for all
5600  * multicast (224.0/4), for a single multicast group (a /32 route) or
5601  * anything in between.  If there is no such multicast route, we just find
5602  * any multicast capable interface and return it.  The returned ipif
5603  * is refhold'ed.
5604  */
5605 ipif_t *
5606 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5607 {
5608 	ire_t			*ire;
5609 	ipif_t			*ipif;
5610 
5611 	ire = ire_lookup_multi(group, zoneid, ipst);
5612 	if (ire != NULL) {
5613 		ipif = ire->ire_ipif;
5614 		ipif_refhold(ipif);
5615 		ire_refrele(ire);
5616 		return (ipif);
5617 	}
5618 
5619 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5620 }
5621 
5622 /*
5623  * Look for an ipif with the specified interface address and destination.
5624  * The destination address is used only for matching point-to-point interfaces.
5625  */
5626 ipif_t *
5627 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5628     ipsq_func_t func, int *error, ip_stack_t *ipst)
5629 {
5630 	ipif_t	*ipif;
5631 	ill_t	*ill;
5632 	ill_walk_context_t ctx;
5633 	ipsq_t	*ipsq;
5634 
5635 	if (error != NULL)
5636 		*error = 0;
5637 
5638 	/*
5639 	 * First match all the point-to-point interfaces
5640 	 * before looking at non-point-to-point interfaces.
5641 	 * This is done to avoid returning non-point-to-point
5642 	 * ipif instead of unnumbered point-to-point ipif.
5643 	 */
5644 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5645 	ill = ILL_START_WALK_V4(&ctx, ipst);
5646 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5647 		GRAB_CONN_LOCK(q);
5648 		mutex_enter(&ill->ill_lock);
5649 		for (ipif = ill->ill_ipif; ipif != NULL;
5650 		    ipif = ipif->ipif_next) {
5651 			/* Allow the ipif to be down */
5652 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5653 			    (ipif->ipif_lcl_addr == if_addr) &&
5654 			    (ipif->ipif_pp_dst_addr == dst)) {
5655 				/*
5656 				 * The block comment at the start of ipif_down
5657 				 * explains the use of the macros used below
5658 				 */
5659 				if (IPIF_CAN_LOOKUP(ipif)) {
5660 					ipif_refhold_locked(ipif);
5661 					mutex_exit(&ill->ill_lock);
5662 					RELEASE_CONN_LOCK(q);
5663 					rw_exit(&ipst->ips_ill_g_lock);
5664 					return (ipif);
5665 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5666 					ipsq = ill->ill_phyint->phyint_ipsq;
5667 					mutex_enter(&ipsq->ipsq_lock);
5668 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5669 					mutex_exit(&ill->ill_lock);
5670 					rw_exit(&ipst->ips_ill_g_lock);
5671 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5672 					    ill);
5673 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5674 					mutex_exit(&ipsq->ipsq_lock);
5675 					RELEASE_CONN_LOCK(q);
5676 					if (error != NULL)
5677 						*error = EINPROGRESS;
5678 					return (NULL);
5679 				}
5680 			}
5681 		}
5682 		mutex_exit(&ill->ill_lock);
5683 		RELEASE_CONN_LOCK(q);
5684 	}
5685 	rw_exit(&ipst->ips_ill_g_lock);
5686 
5687 	/* lookup the ipif based on interface address */
5688 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5689 	    ipst);
5690 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5691 	return (ipif);
5692 }
5693 
5694 /*
5695  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5696  */
5697 static ipif_t *
5698 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5699     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5700     ip_stack_t *ipst)
5701 {
5702 	ipif_t  *ipif;
5703 	ill_t   *ill;
5704 	boolean_t ptp = B_FALSE;
5705 	ipsq_t	*ipsq;
5706 	ill_walk_context_t	ctx;
5707 
5708 	if (error != NULL)
5709 		*error = 0;
5710 
5711 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5712 	/*
5713 	 * Repeat twice, first based on local addresses and
5714 	 * next time for pointopoint.
5715 	 */
5716 repeat:
5717 	ill = ILL_START_WALK_V4(&ctx, ipst);
5718 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5719 		if (match_ill != NULL && ill != match_ill &&
5720 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5721 			continue;
5722 		}
5723 		GRAB_CONN_LOCK(q);
5724 		mutex_enter(&ill->ill_lock);
5725 		for (ipif = ill->ill_ipif; ipif != NULL;
5726 		    ipif = ipif->ipif_next) {
5727 			if (zoneid != ALL_ZONES &&
5728 			    zoneid != ipif->ipif_zoneid &&
5729 			    ipif->ipif_zoneid != ALL_ZONES)
5730 				continue;
5731 			/* Allow the ipif to be down */
5732 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5733 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5734 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5735 			    (ipif->ipif_pp_dst_addr == addr))) {
5736 				/*
5737 				 * The block comment at the start of ipif_down
5738 				 * explains the use of the macros used below
5739 				 */
5740 				if (IPIF_CAN_LOOKUP(ipif)) {
5741 					ipif_refhold_locked(ipif);
5742 					mutex_exit(&ill->ill_lock);
5743 					RELEASE_CONN_LOCK(q);
5744 					rw_exit(&ipst->ips_ill_g_lock);
5745 					return (ipif);
5746 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5747 					ipsq = ill->ill_phyint->phyint_ipsq;
5748 					mutex_enter(&ipsq->ipsq_lock);
5749 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5750 					mutex_exit(&ill->ill_lock);
5751 					rw_exit(&ipst->ips_ill_g_lock);
5752 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5753 					    ill);
5754 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5755 					mutex_exit(&ipsq->ipsq_lock);
5756 					RELEASE_CONN_LOCK(q);
5757 					if (error != NULL)
5758 						*error = EINPROGRESS;
5759 					return (NULL);
5760 				}
5761 			}
5762 		}
5763 		mutex_exit(&ill->ill_lock);
5764 		RELEASE_CONN_LOCK(q);
5765 	}
5766 
5767 	/* If we already did the ptp case, then we are done */
5768 	if (ptp) {
5769 		rw_exit(&ipst->ips_ill_g_lock);
5770 		if (error != NULL)
5771 			*error = ENXIO;
5772 		return (NULL);
5773 	}
5774 	ptp = B_TRUE;
5775 	goto repeat;
5776 }
5777 
5778 /*
5779  * Check if the address exists in the system.
5780  * We don't hold the conn_lock as we will not perform defered ipsqueue
5781  * operation.
5782  */
5783 boolean_t
5784 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5785 {
5786 	ipif_t  *ipif;
5787 	ill_t   *ill;
5788 	ill_walk_context_t	ctx;
5789 
5790 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5791 
5792 	ill = ILL_START_WALK_V4(&ctx, ipst);
5793 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5794 		mutex_enter(&ill->ill_lock);
5795 		for (ipif = ill->ill_ipif; ipif != NULL;
5796 		    ipif = ipif->ipif_next) {
5797 			if (zoneid != ALL_ZONES &&
5798 			    zoneid != ipif->ipif_zoneid &&
5799 			    ipif->ipif_zoneid != ALL_ZONES)
5800 				continue;
5801 			/* Allow the ipif to be down */
5802 			/*
5803 			 * XXX Different from ipif_lookup_addr(), we don't do
5804 			 * twice lookups. As from bind()'s point of view, we
5805 			 * may return once we find a match.
5806 			 */
5807 			if (((ipif->ipif_lcl_addr == addr) &&
5808 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5809 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5810 			    (ipif->ipif_pp_dst_addr == addr))) {
5811 				/*
5812 				 * Allow bind() to be successful even if the
5813 				 * ipif is with IPIF_CHANGING bit set.
5814 				 */
5815 				mutex_exit(&ill->ill_lock);
5816 				rw_exit(&ipst->ips_ill_g_lock);
5817 				return (B_TRUE);
5818 			}
5819 		}
5820 		mutex_exit(&ill->ill_lock);
5821 	}
5822 
5823 	rw_exit(&ipst->ips_ill_g_lock);
5824 	return (B_FALSE);
5825 }
5826 
5827 /*
5828  * Lookup an ipif with the specified address.  For point-to-point links we
5829  * look for matches on either the destination address or the local address,
5830  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5831  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5832  * (or illgrp if `match_ill' is in an IPMP group).
5833  */
5834 ipif_t *
5835 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5836     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5837 {
5838 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
5839 	    func, error, ipst));
5840 }
5841 
5842 /*
5843  * Special abbreviated version of ipif_lookup_addr() that doesn't match
5844  * `match_ill' across the IPMP group.  This function is only needed in some
5845  * corner-cases; almost everything should use ipif_lookup_addr().
5846  */
5847 static ipif_t *
5848 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5849 {
5850 	ASSERT(match_ill != NULL);
5851 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
5852 	    NULL, NULL, NULL, NULL, ipst));
5853 }
5854 
5855 /*
5856  * Look for an ipif with the specified address. For point-point links
5857  * we look for matches on either the destination address and the local
5858  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5859  * is set.
5860  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
5861  * ill (or illgrp if `match_ill' is in an IPMP group).
5862  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5863  */
5864 zoneid_t
5865 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5866 {
5867 	zoneid_t zoneid;
5868 	ipif_t  *ipif;
5869 	ill_t   *ill;
5870 	boolean_t ptp = B_FALSE;
5871 	ill_walk_context_t	ctx;
5872 
5873 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5874 	/*
5875 	 * Repeat twice, first based on local addresses and
5876 	 * next time for pointopoint.
5877 	 */
5878 repeat:
5879 	ill = ILL_START_WALK_V4(&ctx, ipst);
5880 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5881 		if (match_ill != NULL && ill != match_ill &&
5882 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
5883 			continue;
5884 		}
5885 		mutex_enter(&ill->ill_lock);
5886 		for (ipif = ill->ill_ipif; ipif != NULL;
5887 		    ipif = ipif->ipif_next) {
5888 			/* Allow the ipif to be down */
5889 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5890 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5891 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5892 			    (ipif->ipif_pp_dst_addr == addr)) &&
5893 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5894 				zoneid = ipif->ipif_zoneid;
5895 				mutex_exit(&ill->ill_lock);
5896 				rw_exit(&ipst->ips_ill_g_lock);
5897 				/*
5898 				 * If ipif_zoneid was ALL_ZONES then we have
5899 				 * a trusted extensions shared IP address.
5900 				 * In that case GLOBAL_ZONEID works to send.
5901 				 */
5902 				if (zoneid == ALL_ZONES)
5903 					zoneid = GLOBAL_ZONEID;
5904 				return (zoneid);
5905 			}
5906 		}
5907 		mutex_exit(&ill->ill_lock);
5908 	}
5909 
5910 	/* If we already did the ptp case, then we are done */
5911 	if (ptp) {
5912 		rw_exit(&ipst->ips_ill_g_lock);
5913 		return (ALL_ZONES);
5914 	}
5915 	ptp = B_TRUE;
5916 	goto repeat;
5917 }
5918 
5919 /*
5920  * Look for an ipif that matches the specified remote address i.e. the
5921  * ipif that would receive the specified packet.
5922  * First look for directly connected interfaces and then do a recursive
5923  * IRE lookup and pick the first ipif corresponding to the source address in the
5924  * ire.
5925  * Returns: held ipif
5926  */
5927 ipif_t *
5928 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5929 {
5930 	ipif_t	*ipif;
5931 	ire_t	*ire;
5932 	ip_stack_t	*ipst = ill->ill_ipst;
5933 
5934 	ASSERT(!ill->ill_isv6);
5935 
5936 	/*
5937 	 * Someone could be changing this ipif currently or change it
5938 	 * after we return this. Thus  a few packets could use the old
5939 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5940 	 * will atomically be updated or cleaned up with the new value
5941 	 * Thus we don't need a lock to check the flags or other attrs below.
5942 	 */
5943 	mutex_enter(&ill->ill_lock);
5944 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5945 		if (!IPIF_CAN_LOOKUP(ipif))
5946 			continue;
5947 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5948 		    ipif->ipif_zoneid != ALL_ZONES)
5949 			continue;
5950 		/* Allow the ipif to be down */
5951 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5952 			if ((ipif->ipif_pp_dst_addr == addr) ||
5953 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5954 			    ipif->ipif_lcl_addr == addr)) {
5955 				ipif_refhold_locked(ipif);
5956 				mutex_exit(&ill->ill_lock);
5957 				return (ipif);
5958 			}
5959 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5960 			ipif_refhold_locked(ipif);
5961 			mutex_exit(&ill->ill_lock);
5962 			return (ipif);
5963 		}
5964 	}
5965 	mutex_exit(&ill->ill_lock);
5966 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5967 	    NULL, MATCH_IRE_RECURSIVE, ipst);
5968 	if (ire != NULL) {
5969 		/*
5970 		 * The callers of this function wants to know the
5971 		 * interface on which they have to send the replies
5972 		 * back. For IREs that have ire_stq and ire_ipif
5973 		 * derived from different ills, we really don't care
5974 		 * what we return here.
5975 		 */
5976 		ipif = ire->ire_ipif;
5977 		if (ipif != NULL) {
5978 			ipif_refhold(ipif);
5979 			ire_refrele(ire);
5980 			return (ipif);
5981 		}
5982 		ire_refrele(ire);
5983 	}
5984 	/* Pick the first interface */
5985 	ipif = ipif_get_next_ipif(NULL, ill);
5986 	return (ipif);
5987 }
5988 
5989 /*
5990  * This func does not prevent refcnt from increasing. But if
5991  * the caller has taken steps to that effect, then this func
5992  * can be used to determine whether the ill has become quiescent
5993  */
5994 static boolean_t
5995 ill_is_quiescent(ill_t *ill)
5996 {
5997 	ipif_t	*ipif;
5998 
5999 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6000 
6001 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6002 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6003 			return (B_FALSE);
6004 		}
6005 	}
6006 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6007 		return (B_FALSE);
6008 	}
6009 	return (B_TRUE);
6010 }
6011 
6012 boolean_t
6013 ill_is_freeable(ill_t *ill)
6014 {
6015 	ipif_t	*ipif;
6016 
6017 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6018 
6019 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6020 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6021 			return (B_FALSE);
6022 		}
6023 	}
6024 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6025 		return (B_FALSE);
6026 	}
6027 	return (B_TRUE);
6028 }
6029 
6030 /*
6031  * This func does not prevent refcnt from increasing. But if
6032  * the caller has taken steps to that effect, then this func
6033  * can be used to determine whether the ipif has become quiescent
6034  */
6035 static boolean_t
6036 ipif_is_quiescent(ipif_t *ipif)
6037 {
6038 	ill_t *ill;
6039 
6040 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6041 
6042 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6043 		return (B_FALSE);
6044 	}
6045 
6046 	ill = ipif->ipif_ill;
6047 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6048 	    ill->ill_logical_down) {
6049 		return (B_TRUE);
6050 	}
6051 
6052 	/* This is the last ipif going down or being deleted on this ill */
6053 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6054 		return (B_FALSE);
6055 	}
6056 
6057 	return (B_TRUE);
6058 }
6059 
6060 /*
6061  * return true if the ipif can be destroyed: the ipif has to be quiescent
6062  * with zero references from ire/nce/ilm to it.
6063  */
6064 static boolean_t
6065 ipif_is_freeable(ipif_t *ipif)
6066 {
6067 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6068 	ASSERT(ipif->ipif_id != 0);
6069 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6070 }
6071 
6072 /*
6073  * The ipif/ill/ire has been refreled. Do the tail processing.
6074  * Determine if the ipif or ill in question has become quiescent and if so
6075  * wakeup close and/or restart any queued pending ioctl that is waiting
6076  * for the ipif_down (or ill_down)
6077  */
6078 void
6079 ipif_ill_refrele_tail(ill_t *ill)
6080 {
6081 	mblk_t	*mp;
6082 	conn_t	*connp;
6083 	ipsq_t	*ipsq;
6084 	ipxop_t	*ipx;
6085 	ipif_t	*ipif;
6086 	dl_notify_ind_t *dlindp;
6087 
6088 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6089 
6090 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6091 		/* ip_modclose() may be waiting */
6092 		cv_broadcast(&ill->ill_cv);
6093 	}
6094 
6095 	ipsq = ill->ill_phyint->phyint_ipsq;
6096 	mutex_enter(&ipsq->ipsq_lock);
6097 	ipx = ipsq->ipsq_xop;
6098 	mutex_enter(&ipx->ipx_lock);
6099 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6100 		goto unlock;
6101 
6102 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6103 
6104 	ipif = ipx->ipx_pending_ipif;
6105 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6106 		goto unlock;
6107 
6108 	switch (ipx->ipx_waitfor) {
6109 	case IPIF_DOWN:
6110 		if (!ipif_is_quiescent(ipif))
6111 			goto unlock;
6112 		break;
6113 	case IPIF_FREE:
6114 		if (!ipif_is_freeable(ipif))
6115 			goto unlock;
6116 		break;
6117 	case ILL_DOWN:
6118 		if (!ill_is_quiescent(ill))
6119 			goto unlock;
6120 		break;
6121 	case ILL_FREE:
6122 		/*
6123 		 * ILL_FREE is only for loopback; normal ill teardown waits
6124 		 * synchronously in ip_modclose() without using ipx_waitfor,
6125 		 * handled by the cv_broadcast() at the top of this function.
6126 		 */
6127 		if (!ill_is_freeable(ill))
6128 			goto unlock;
6129 		break;
6130 	default:
6131 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6132 		    (void *)ipsq, ipx->ipx_waitfor);
6133 	}
6134 
6135 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6136 	mutex_exit(&ipx->ipx_lock);
6137 	mp = ipsq_pending_mp_get(ipsq, &connp);
6138 	mutex_exit(&ipsq->ipsq_lock);
6139 	mutex_exit(&ill->ill_lock);
6140 
6141 	ASSERT(mp != NULL);
6142 	/*
6143 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6144 	 * we can only get here when the current operation decides it
6145 	 * it needs to quiesce via ipsq_pending_mp_add().
6146 	 */
6147 	switch (mp->b_datap->db_type) {
6148 	case M_PCPROTO:
6149 	case M_PROTO:
6150 		/*
6151 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6152 		 */
6153 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6154 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6155 
6156 		switch (dlindp->dl_notification) {
6157 		case DL_NOTE_PHYS_ADDR:
6158 			qwriter_ip(ill, ill->ill_rq, mp,
6159 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6160 			return;
6161 		case DL_NOTE_REPLUMB:
6162 			qwriter_ip(ill, ill->ill_rq, mp,
6163 			    ill_replumb_tail, CUR_OP, B_TRUE);
6164 			return;
6165 		default:
6166 			ASSERT(0);
6167 			ill_refrele(ill);
6168 		}
6169 		break;
6170 
6171 	case M_ERROR:
6172 	case M_HANGUP:
6173 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6174 		    B_TRUE);
6175 		return;
6176 
6177 	case M_IOCTL:
6178 	case M_IOCDATA:
6179 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6180 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6181 		return;
6182 
6183 	default:
6184 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6185 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6186 	}
6187 	return;
6188 unlock:
6189 	mutex_exit(&ipsq->ipsq_lock);
6190 	mutex_exit(&ipx->ipx_lock);
6191 	mutex_exit(&ill->ill_lock);
6192 }
6193 
6194 #ifdef DEBUG
6195 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6196 static void
6197 th_trace_rrecord(th_trace_t *th_trace)
6198 {
6199 	tr_buf_t *tr_buf;
6200 	uint_t lastref;
6201 
6202 	lastref = th_trace->th_trace_lastref;
6203 	lastref++;
6204 	if (lastref == TR_BUF_MAX)
6205 		lastref = 0;
6206 	th_trace->th_trace_lastref = lastref;
6207 	tr_buf = &th_trace->th_trbuf[lastref];
6208 	tr_buf->tr_time = lbolt;
6209 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6210 }
6211 
6212 static void
6213 th_trace_free(void *value)
6214 {
6215 	th_trace_t *th_trace = value;
6216 
6217 	ASSERT(th_trace->th_refcnt == 0);
6218 	kmem_free(th_trace, sizeof (*th_trace));
6219 }
6220 
6221 /*
6222  * Find or create the per-thread hash table used to track object references.
6223  * The ipst argument is NULL if we shouldn't allocate.
6224  *
6225  * Accesses per-thread data, so there's no need to lock here.
6226  */
6227 static mod_hash_t *
6228 th_trace_gethash(ip_stack_t *ipst)
6229 {
6230 	th_hash_t *thh;
6231 
6232 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6233 		mod_hash_t *mh;
6234 		char name[256];
6235 		size_t objsize, rshift;
6236 		int retv;
6237 
6238 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6239 			return (NULL);
6240 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6241 		    (void *)curthread);
6242 
6243 		/*
6244 		 * We use mod_hash_create_extended here rather than the more
6245 		 * obvious mod_hash_create_ptrhash because the latter has a
6246 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6247 		 * block.
6248 		 */
6249 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6250 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6251 		rshift = highbit(objsize);
6252 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6253 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6254 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6255 		if (mh == NULL) {
6256 			kmem_free(thh, sizeof (*thh));
6257 			return (NULL);
6258 		}
6259 		thh->thh_hash = mh;
6260 		thh->thh_ipst = ipst;
6261 		/*
6262 		 * We trace ills, ipifs, ires, and nces.  All of these are
6263 		 * per-IP-stack, so the lock on the thread list is as well.
6264 		 */
6265 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6266 		list_insert_tail(&ip_thread_list, thh);
6267 		rw_exit(&ip_thread_rwlock);
6268 		retv = tsd_set(ip_thread_data, thh);
6269 		ASSERT(retv == 0);
6270 	}
6271 	return (thh != NULL ? thh->thh_hash : NULL);
6272 }
6273 
6274 boolean_t
6275 th_trace_ref(const void *obj, ip_stack_t *ipst)
6276 {
6277 	th_trace_t *th_trace;
6278 	mod_hash_t *mh;
6279 	mod_hash_val_t val;
6280 
6281 	if ((mh = th_trace_gethash(ipst)) == NULL)
6282 		return (B_FALSE);
6283 
6284 	/*
6285 	 * Attempt to locate the trace buffer for this obj and thread.
6286 	 * If it does not exist, then allocate a new trace buffer and
6287 	 * insert into the hash.
6288 	 */
6289 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6290 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6291 		if (th_trace == NULL)
6292 			return (B_FALSE);
6293 
6294 		th_trace->th_id = curthread;
6295 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6296 		    (mod_hash_val_t)th_trace) != 0) {
6297 			kmem_free(th_trace, sizeof (th_trace_t));
6298 			return (B_FALSE);
6299 		}
6300 	} else {
6301 		th_trace = (th_trace_t *)val;
6302 	}
6303 
6304 	ASSERT(th_trace->th_refcnt >= 0 &&
6305 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6306 
6307 	th_trace->th_refcnt++;
6308 	th_trace_rrecord(th_trace);
6309 	return (B_TRUE);
6310 }
6311 
6312 /*
6313  * For the purpose of tracing a reference release, we assume that global
6314  * tracing is always on and that the same thread initiated the reference hold
6315  * is releasing.
6316  */
6317 void
6318 th_trace_unref(const void *obj)
6319 {
6320 	int retv;
6321 	mod_hash_t *mh;
6322 	th_trace_t *th_trace;
6323 	mod_hash_val_t val;
6324 
6325 	mh = th_trace_gethash(NULL);
6326 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6327 	ASSERT(retv == 0);
6328 	th_trace = (th_trace_t *)val;
6329 
6330 	ASSERT(th_trace->th_refcnt > 0);
6331 	th_trace->th_refcnt--;
6332 	th_trace_rrecord(th_trace);
6333 }
6334 
6335 /*
6336  * If tracing has been disabled, then we assume that the reference counts are
6337  * now useless, and we clear them out before destroying the entries.
6338  */
6339 void
6340 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6341 {
6342 	th_hash_t	*thh;
6343 	mod_hash_t	*mh;
6344 	mod_hash_val_t	val;
6345 	th_trace_t	*th_trace;
6346 	int		retv;
6347 
6348 	rw_enter(&ip_thread_rwlock, RW_READER);
6349 	for (thh = list_head(&ip_thread_list); thh != NULL;
6350 	    thh = list_next(&ip_thread_list, thh)) {
6351 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6352 		    &val) == 0) {
6353 			th_trace = (th_trace_t *)val;
6354 			if (trace_disable)
6355 				th_trace->th_refcnt = 0;
6356 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6357 			ASSERT(retv == 0);
6358 		}
6359 	}
6360 	rw_exit(&ip_thread_rwlock);
6361 }
6362 
6363 void
6364 ipif_trace_ref(ipif_t *ipif)
6365 {
6366 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6367 
6368 	if (ipif->ipif_trace_disable)
6369 		return;
6370 
6371 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6372 		ipif->ipif_trace_disable = B_TRUE;
6373 		ipif_trace_cleanup(ipif);
6374 	}
6375 }
6376 
6377 void
6378 ipif_untrace_ref(ipif_t *ipif)
6379 {
6380 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6381 
6382 	if (!ipif->ipif_trace_disable)
6383 		th_trace_unref(ipif);
6384 }
6385 
6386 void
6387 ill_trace_ref(ill_t *ill)
6388 {
6389 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6390 
6391 	if (ill->ill_trace_disable)
6392 		return;
6393 
6394 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6395 		ill->ill_trace_disable = B_TRUE;
6396 		ill_trace_cleanup(ill);
6397 	}
6398 }
6399 
6400 void
6401 ill_untrace_ref(ill_t *ill)
6402 {
6403 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6404 
6405 	if (!ill->ill_trace_disable)
6406 		th_trace_unref(ill);
6407 }
6408 
6409 /*
6410  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6411  * failure, ipif_trace_disable is set.
6412  */
6413 static void
6414 ipif_trace_cleanup(const ipif_t *ipif)
6415 {
6416 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6417 }
6418 
6419 /*
6420  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6421  * failure, ill_trace_disable is set.
6422  */
6423 static void
6424 ill_trace_cleanup(const ill_t *ill)
6425 {
6426 	th_trace_cleanup(ill, ill->ill_trace_disable);
6427 }
6428 #endif /* DEBUG */
6429 
6430 void
6431 ipif_refhold_locked(ipif_t *ipif)
6432 {
6433 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6434 	ipif->ipif_refcnt++;
6435 	IPIF_TRACE_REF(ipif);
6436 }
6437 
6438 void
6439 ipif_refhold(ipif_t *ipif)
6440 {
6441 	ill_t	*ill;
6442 
6443 	ill = ipif->ipif_ill;
6444 	mutex_enter(&ill->ill_lock);
6445 	ipif->ipif_refcnt++;
6446 	IPIF_TRACE_REF(ipif);
6447 	mutex_exit(&ill->ill_lock);
6448 }
6449 
6450 /*
6451  * Must not be called while holding any locks. Otherwise if this is
6452  * the last reference to be released there is a chance of recursive mutex
6453  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6454  * to restart an ioctl.
6455  */
6456 void
6457 ipif_refrele(ipif_t *ipif)
6458 {
6459 	ill_t	*ill;
6460 
6461 	ill = ipif->ipif_ill;
6462 
6463 	mutex_enter(&ill->ill_lock);
6464 	ASSERT(ipif->ipif_refcnt != 0);
6465 	ipif->ipif_refcnt--;
6466 	IPIF_UNTRACE_REF(ipif);
6467 	if (ipif->ipif_refcnt != 0) {
6468 		mutex_exit(&ill->ill_lock);
6469 		return;
6470 	}
6471 
6472 	/* Drops the ill_lock */
6473 	ipif_ill_refrele_tail(ill);
6474 }
6475 
6476 ipif_t *
6477 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6478 {
6479 	ipif_t	*ipif;
6480 
6481 	mutex_enter(&ill->ill_lock);
6482 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6483 	    ipif != NULL; ipif = ipif->ipif_next) {
6484 		if (!IPIF_CAN_LOOKUP(ipif))
6485 			continue;
6486 		ipif_refhold_locked(ipif);
6487 		mutex_exit(&ill->ill_lock);
6488 		return (ipif);
6489 	}
6490 	mutex_exit(&ill->ill_lock);
6491 	return (NULL);
6492 }
6493 
6494 /*
6495  * TODO: make this table extendible at run time
6496  * Return a pointer to the mac type info for 'mac_type'
6497  */
6498 static ip_m_t *
6499 ip_m_lookup(t_uscalar_t mac_type)
6500 {
6501 	ip_m_t	*ipm;
6502 
6503 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6504 		if (ipm->ip_m_mac_type == mac_type)
6505 			return (ipm);
6506 	return (NULL);
6507 }
6508 
6509 /*
6510  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6511  * ipif_arg is passed in to associate it with the correct interface.
6512  * We may need to restart this operation if the ipif cannot be looked up
6513  * due to an exclusive operation that is currently in progress. The restart
6514  * entry point is specified by 'func'
6515  */
6516 int
6517 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6518     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6519     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6520     struct rtsa_s *sp, ip_stack_t *ipst)
6521 {
6522 	ire_t	*ire;
6523 	ire_t	*gw_ire = NULL;
6524 	ipif_t	*ipif = NULL;
6525 	boolean_t ipif_refheld = B_FALSE;
6526 	uint_t	type;
6527 	int	match_flags = MATCH_IRE_TYPE;
6528 	int	error;
6529 	tsol_gc_t *gc = NULL;
6530 	tsol_gcgrp_t *gcgrp = NULL;
6531 	boolean_t gcgrp_xtraref = B_FALSE;
6532 
6533 	ip1dbg(("ip_rt_add:"));
6534 
6535 	if (ire_arg != NULL)
6536 		*ire_arg = NULL;
6537 
6538 	/*
6539 	 * If this is the case of RTF_HOST being set, then we set the netmask
6540 	 * to all ones (regardless if one was supplied).
6541 	 */
6542 	if (flags & RTF_HOST)
6543 		mask = IP_HOST_MASK;
6544 
6545 	/*
6546 	 * Prevent routes with a zero gateway from being created (since
6547 	 * interfaces can currently be plumbed and brought up no assigned
6548 	 * address).
6549 	 */
6550 	if (gw_addr == 0)
6551 		return (ENETUNREACH);
6552 	/*
6553 	 * Get the ipif, if any, corresponding to the gw_addr
6554 	 */
6555 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6556 	    ipst);
6557 	if (ipif != NULL) {
6558 		if (IS_VNI(ipif->ipif_ill)) {
6559 			ipif_refrele(ipif);
6560 			return (EINVAL);
6561 		}
6562 		ipif_refheld = B_TRUE;
6563 	} else if (error == EINPROGRESS) {
6564 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6565 		return (EINPROGRESS);
6566 	} else {
6567 		error = 0;
6568 	}
6569 
6570 	if (ipif != NULL) {
6571 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6572 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6573 	} else {
6574 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6575 	}
6576 
6577 	/*
6578 	 * GateD will attempt to create routes with a loopback interface
6579 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6580 	 * these routes to be added, but create them as interface routes
6581 	 * since the gateway is an interface address.
6582 	 */
6583 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6584 		flags &= ~RTF_GATEWAY;
6585 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6586 		    mask == IP_HOST_MASK) {
6587 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6588 			    ALL_ZONES, NULL, match_flags, ipst);
6589 			if (ire != NULL) {
6590 				ire_refrele(ire);
6591 				if (ipif_refheld)
6592 					ipif_refrele(ipif);
6593 				return (EEXIST);
6594 			}
6595 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6596 			    "for 0x%x\n", (void *)ipif,
6597 			    ipif->ipif_ire_type,
6598 			    ntohl(ipif->ipif_lcl_addr)));
6599 			ire = ire_create(
6600 			    (uchar_t *)&dst_addr,	/* dest address */
6601 			    (uchar_t *)&mask,		/* mask */
6602 			    (uchar_t *)&ipif->ipif_src_addr,
6603 			    NULL,			/* no gateway */
6604 			    &ipif->ipif_mtu,
6605 			    NULL,
6606 			    ipif->ipif_rq,		/* recv-from queue */
6607 			    NULL,			/* no send-to queue */
6608 			    ipif->ipif_ire_type,	/* LOOPBACK */
6609 			    ipif,
6610 			    0,
6611 			    0,
6612 			    0,
6613 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6614 			    RTF_PRIVATE : 0,
6615 			    &ire_uinfo_null,
6616 			    NULL,
6617 			    NULL,
6618 			    ipst);
6619 
6620 			if (ire == NULL) {
6621 				if (ipif_refheld)
6622 					ipif_refrele(ipif);
6623 				return (ENOMEM);
6624 			}
6625 			error = ire_add(&ire, q, mp, func, B_FALSE);
6626 			if (error == 0)
6627 				goto save_ire;
6628 			if (ipif_refheld)
6629 				ipif_refrele(ipif);
6630 			return (error);
6631 
6632 		}
6633 	}
6634 
6635 	/*
6636 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6637 	 * and the gateway address provided is one of the system's interface
6638 	 * addresses.  By using the routing socket interface and supplying an
6639 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6640 	 * specifying an interface route to be created is available which uses
6641 	 * the interface index that specifies the outgoing interface rather than
6642 	 * the address of an outgoing interface (which may not be able to
6643 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6644 	 * flag, routes can be specified which not only specify the next-hop to
6645 	 * be used when routing to a certain prefix, but also which outgoing
6646 	 * interface should be used.
6647 	 *
6648 	 * Previously, interfaces would have unique addresses assigned to them
6649 	 * and so the address assigned to a particular interface could be used
6650 	 * to identify a particular interface.  One exception to this was the
6651 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6652 	 *
6653 	 * With the advent of IPv6 and its link-local addresses, this
6654 	 * restriction was relaxed and interfaces could share addresses between
6655 	 * themselves.  In fact, typically all of the link-local interfaces on
6656 	 * an IPv6 node or router will have the same link-local address.  In
6657 	 * order to differentiate between these interfaces, the use of an
6658 	 * interface index is necessary and this index can be carried inside a
6659 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6660 	 * of using the interface index, however, is that all of the ipif's that
6661 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6662 	 * cannot be used to differentiate between ipif's (or logical
6663 	 * interfaces) that belong to the same ill (physical interface).
6664 	 *
6665 	 * For example, in the following case involving IPv4 interfaces and
6666 	 * logical interfaces
6667 	 *
6668 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6669 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6670 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6671 	 *
6672 	 * the ipif's corresponding to each of these interface routes can be
6673 	 * uniquely identified by the "gateway" (actually interface address).
6674 	 *
6675 	 * In this case involving multiple IPv6 default routes to a particular
6676 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6677 	 * default route is of interest:
6678 	 *
6679 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6680 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6681 	 */
6682 
6683 	/* RTF_GATEWAY not set */
6684 	if (!(flags & RTF_GATEWAY)) {
6685 		queue_t	*stq;
6686 
6687 		if (sp != NULL) {
6688 			ip2dbg(("ip_rt_add: gateway security attributes "
6689 			    "cannot be set with interface route\n"));
6690 			if (ipif_refheld)
6691 				ipif_refrele(ipif);
6692 			return (EINVAL);
6693 		}
6694 
6695 		/*
6696 		 * As the interface index specified with the RTA_IFP sockaddr is
6697 		 * the same for all ipif's off of an ill, the matching logic
6698 		 * below uses MATCH_IRE_ILL if such an index was specified.
6699 		 * This means that routes sharing the same prefix when added
6700 		 * using a RTA_IFP sockaddr must have distinct interface
6701 		 * indices (namely, they must be on distinct ill's).
6702 		 *
6703 		 * On the other hand, since the gateway address will usually be
6704 		 * different for each ipif on the system, the matching logic
6705 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6706 		 * route.  This means that interface routes for the same prefix
6707 		 * can be created if they belong to distinct ipif's and if a
6708 		 * RTA_IFP sockaddr is not present.
6709 		 */
6710 		if (ipif_arg != NULL) {
6711 			if (ipif_refheld)  {
6712 				ipif_refrele(ipif);
6713 				ipif_refheld = B_FALSE;
6714 			}
6715 			ipif = ipif_arg;
6716 			match_flags |= MATCH_IRE_ILL;
6717 		} else {
6718 			/*
6719 			 * Check the ipif corresponding to the gw_addr
6720 			 */
6721 			if (ipif == NULL)
6722 				return (ENETUNREACH);
6723 			match_flags |= MATCH_IRE_IPIF;
6724 		}
6725 		ASSERT(ipif != NULL);
6726 
6727 		/*
6728 		 * We check for an existing entry at this point.
6729 		 *
6730 		 * Since a netmask isn't passed in via the ioctl interface
6731 		 * (SIOCADDRT), we don't check for a matching netmask in that
6732 		 * case.
6733 		 */
6734 		if (!ioctl_msg)
6735 			match_flags |= MATCH_IRE_MASK;
6736 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6737 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6738 		if (ire != NULL) {
6739 			ire_refrele(ire);
6740 			if (ipif_refheld)
6741 				ipif_refrele(ipif);
6742 			return (EEXIST);
6743 		}
6744 
6745 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6746 		    ? ipif->ipif_rq : ipif->ipif_wq;
6747 
6748 		/*
6749 		 * Create a copy of the IRE_LOOPBACK,
6750 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6751 		 * the modified address and netmask.
6752 		 */
6753 		ire = ire_create(
6754 		    (uchar_t *)&dst_addr,
6755 		    (uint8_t *)&mask,
6756 		    (uint8_t *)&ipif->ipif_src_addr,
6757 		    NULL,
6758 		    &ipif->ipif_mtu,
6759 		    NULL,
6760 		    NULL,
6761 		    stq,
6762 		    ipif->ipif_net_type,
6763 		    ipif,
6764 		    0,
6765 		    0,
6766 		    0,
6767 		    flags,
6768 		    &ire_uinfo_null,
6769 		    NULL,
6770 		    NULL,
6771 		    ipst);
6772 		if (ire == NULL) {
6773 			if (ipif_refheld)
6774 				ipif_refrele(ipif);
6775 			return (ENOMEM);
6776 		}
6777 
6778 		/*
6779 		 * Some software (for example, GateD and Sun Cluster) attempts
6780 		 * to create (what amount to) IRE_PREFIX routes with the
6781 		 * loopback address as the gateway.  This is primarily done to
6782 		 * set up prefixes with the RTF_REJECT flag set (for example,
6783 		 * when generating aggregate routes.)
6784 		 *
6785 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6786 		 * IRE_LOOPBACK, then we map the request into a
6787 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6788 		 * these interface routes, by definition, can only be that.
6789 		 *
6790 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6791 		 * routine, but rather using ire_create() directly.
6792 		 *
6793 		 */
6794 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6795 			ire->ire_type = IRE_IF_NORESOLVER;
6796 			ire->ire_flags |= RTF_BLACKHOLE;
6797 		}
6798 
6799 		error = ire_add(&ire, q, mp, func, B_FALSE);
6800 		if (error == 0)
6801 			goto save_ire;
6802 
6803 		/*
6804 		 * In the result of failure, ire_add() will have already
6805 		 * deleted the ire in question, so there is no need to
6806 		 * do that here.
6807 		 */
6808 		if (ipif_refheld)
6809 			ipif_refrele(ipif);
6810 		return (error);
6811 	}
6812 	if (ipif_refheld) {
6813 		ipif_refrele(ipif);
6814 		ipif_refheld = B_FALSE;
6815 	}
6816 
6817 	/*
6818 	 * Get an interface IRE for the specified gateway.
6819 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6820 	 * gateway, it is currently unreachable and we fail the request
6821 	 * accordingly.
6822 	 */
6823 	ipif = ipif_arg;
6824 	if (ipif_arg != NULL)
6825 		match_flags |= MATCH_IRE_ILL;
6826 again:
6827 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6828 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6829 	if (gw_ire == NULL) {
6830 		/*
6831 		 * With IPMP, we allow host routes to influence in.mpathd's
6832 		 * target selection.  However, if the test addresses are on
6833 		 * their own network, the above lookup will fail since the
6834 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6835 		 * hidden test IREs to be found and try again.
6836 		 */
6837 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6838 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6839 			goto again;
6840 		}
6841 		return (ENETUNREACH);
6842 	}
6843 
6844 	/*
6845 	 * We create one of three types of IREs as a result of this request
6846 	 * based on the netmask.  A netmask of all ones (which is automatically
6847 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6848 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6849 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6850 	 * destination prefix.
6851 	 */
6852 	if (mask == IP_HOST_MASK)
6853 		type = IRE_HOST;
6854 	else if (mask == 0)
6855 		type = IRE_DEFAULT;
6856 	else
6857 		type = IRE_PREFIX;
6858 
6859 	/* check for a duplicate entry */
6860 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6861 	    NULL, ALL_ZONES, 0, NULL,
6862 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
6863 	if (ire != NULL) {
6864 		ire_refrele(gw_ire);
6865 		ire_refrele(ire);
6866 		return (EEXIST);
6867 	}
6868 
6869 	/* Security attribute exists */
6870 	if (sp != NULL) {
6871 		tsol_gcgrp_addr_t ga;
6872 
6873 		/* find or create the gateway credentials group */
6874 		ga.ga_af = AF_INET;
6875 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6876 
6877 		/* we hold reference to it upon success */
6878 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6879 		if (gcgrp == NULL) {
6880 			ire_refrele(gw_ire);
6881 			return (ENOMEM);
6882 		}
6883 
6884 		/*
6885 		 * Create and add the security attribute to the group; a
6886 		 * reference to the group is made upon allocating a new
6887 		 * entry successfully.  If it finds an already-existing
6888 		 * entry for the security attribute in the group, it simply
6889 		 * returns it and no new reference is made to the group.
6890 		 */
6891 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6892 		if (gc == NULL) {
6893 			/* release reference held by gcgrp_lookup */
6894 			GCGRP_REFRELE(gcgrp);
6895 			ire_refrele(gw_ire);
6896 			return (ENOMEM);
6897 		}
6898 	}
6899 
6900 	/* Create the IRE. */
6901 	ire = ire_create(
6902 	    (uchar_t *)&dst_addr,		/* dest address */
6903 	    (uchar_t *)&mask,			/* mask */
6904 	    /* src address assigned by the caller? */
6905 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6906 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
6907 	    (uchar_t *)&gw_addr,		/* gateway address */
6908 	    &gw_ire->ire_max_frag,
6909 	    NULL,				/* no src nce */
6910 	    NULL,				/* no recv-from queue */
6911 	    NULL,				/* no send-to queue */
6912 	    (ushort_t)type,			/* IRE type */
6913 	    ipif_arg,
6914 	    0,
6915 	    0,
6916 	    0,
6917 	    flags,
6918 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6919 	    gc,					/* security attribute */
6920 	    NULL,
6921 	    ipst);
6922 
6923 	/*
6924 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6925 	 * reference to the 'gcgrp'. We can now release the extra reference
6926 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6927 	 */
6928 	if (gcgrp_xtraref)
6929 		GCGRP_REFRELE(gcgrp);
6930 	if (ire == NULL) {
6931 		if (gc != NULL)
6932 			GC_REFRELE(gc);
6933 		ire_refrele(gw_ire);
6934 		return (ENOMEM);
6935 	}
6936 
6937 	/*
6938 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6939 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6940 	 */
6941 
6942 	/* Add the new IRE. */
6943 	error = ire_add(&ire, q, mp, func, B_FALSE);
6944 	if (error != 0) {
6945 		/*
6946 		 * In the result of failure, ire_add() will have already
6947 		 * deleted the ire in question, so there is no need to
6948 		 * do that here.
6949 		 */
6950 		ire_refrele(gw_ire);
6951 		return (error);
6952 	}
6953 
6954 	if (flags & RTF_MULTIRT) {
6955 		/*
6956 		 * Invoke the CGTP (multirouting) filtering module
6957 		 * to add the dst address in the filtering database.
6958 		 * Replicated inbound packets coming from that address
6959 		 * will be filtered to discard the duplicates.
6960 		 * It is not necessary to call the CGTP filter hook
6961 		 * when the dst address is a broadcast or multicast,
6962 		 * because an IP source address cannot be a broadcast
6963 		 * or a multicast.
6964 		 */
6965 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6966 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
6967 		if (ire_dst != NULL) {
6968 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
6969 			ire_refrele(ire_dst);
6970 			goto save_ire;
6971 		}
6972 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
6973 		    !CLASSD(ire->ire_addr)) {
6974 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
6975 			    ipst->ips_netstack->netstack_stackid,
6976 			    ire->ire_addr,
6977 			    ire->ire_gateway_addr,
6978 			    ire->ire_src_addr,
6979 			    gw_ire->ire_src_addr);
6980 			if (res != 0) {
6981 				ire_refrele(gw_ire);
6982 				ire_delete(ire);
6983 				return (res);
6984 			}
6985 		}
6986 	}
6987 
6988 	/*
6989 	 * Now that the prefix IRE entry has been created, delete any
6990 	 * existing gateway IRE cache entries as well as any IRE caches
6991 	 * using the gateway, and force them to be created through
6992 	 * ip_newroute.
6993 	 */
6994 	if (gc != NULL) {
6995 		ASSERT(gcgrp != NULL);
6996 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
6997 	}
6998 
6999 save_ire:
7000 	if (gw_ire != NULL) {
7001 		ire_refrele(gw_ire);
7002 	}
7003 	if (ipif != NULL) {
7004 		/*
7005 		 * Save enough information so that we can recreate the IRE if
7006 		 * the interface goes down and then up.  The metrics associated
7007 		 * with the route will be saved as well when rts_setmetrics() is
7008 		 * called after the IRE has been created.  In the case where
7009 		 * memory cannot be allocated, none of this information will be
7010 		 * saved.
7011 		 */
7012 		ipif_save_ire(ipif, ire);
7013 	}
7014 	if (ioctl_msg)
7015 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7016 	if (ire_arg != NULL) {
7017 		/*
7018 		 * Store the ire that was successfully added into where ire_arg
7019 		 * points to so that callers don't have to look it up
7020 		 * themselves (but they are responsible for ire_refrele()ing
7021 		 * the ire when they are finished with it).
7022 		 */
7023 		*ire_arg = ire;
7024 	} else {
7025 		ire_refrele(ire);		/* Held in ire_add */
7026 	}
7027 	if (ipif_refheld)
7028 		ipif_refrele(ipif);
7029 	return (0);
7030 }
7031 
7032 /*
7033  * ip_rt_delete is called to delete an IPv4 route.
7034  * ipif_arg is passed in to associate it with the correct interface.
7035  * We may need to restart this operation if the ipif cannot be looked up
7036  * due to an exclusive operation that is currently in progress. The restart
7037  * entry point is specified by 'func'
7038  */
7039 /* ARGSUSED4 */
7040 int
7041 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7042     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7043     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7044 {
7045 	ire_t	*ire = NULL;
7046 	ipif_t	*ipif;
7047 	boolean_t ipif_refheld = B_FALSE;
7048 	uint_t	type;
7049 	uint_t	match_flags = MATCH_IRE_TYPE;
7050 	int	err = 0;
7051 
7052 	ip1dbg(("ip_rt_delete:"));
7053 	/*
7054 	 * If this is the case of RTF_HOST being set, then we set the netmask
7055 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7056 	 */
7057 	if (flags & RTF_HOST) {
7058 		mask = IP_HOST_MASK;
7059 		match_flags |= MATCH_IRE_MASK;
7060 	} else if (rtm_addrs & RTA_NETMASK) {
7061 		match_flags |= MATCH_IRE_MASK;
7062 	}
7063 
7064 	/*
7065 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7066 	 * we check if the gateway address is one of our interfaces first,
7067 	 * and fall back on RTF_GATEWAY routes.
7068 	 *
7069 	 * This makes it possible to delete an original
7070 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7071 	 *
7072 	 * As the interface index specified with the RTA_IFP sockaddr is the
7073 	 * same for all ipif's off of an ill, the matching logic below uses
7074 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7075 	 * sharing the same prefix and interface index as the the route
7076 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7077 	 * is specified in the request.
7078 	 *
7079 	 * On the other hand, since the gateway address will usually be
7080 	 * different for each ipif on the system, the matching logic
7081 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7082 	 * route.  This means that interface routes for the same prefix can be
7083 	 * uniquely identified if they belong to distinct ipif's and if a
7084 	 * RTA_IFP sockaddr is not present.
7085 	 *
7086 	 * For more detail on specifying routes by gateway address and by
7087 	 * interface index, see the comments in ip_rt_add().
7088 	 */
7089 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7090 	    ipst);
7091 	if (ipif != NULL)
7092 		ipif_refheld = B_TRUE;
7093 	else if (err == EINPROGRESS)
7094 		return (err);
7095 	else
7096 		err = 0;
7097 	if (ipif != NULL) {
7098 		if (ipif_arg != NULL) {
7099 			if (ipif_refheld) {
7100 				ipif_refrele(ipif);
7101 				ipif_refheld = B_FALSE;
7102 			}
7103 			ipif = ipif_arg;
7104 			match_flags |= MATCH_IRE_ILL;
7105 		} else {
7106 			match_flags |= MATCH_IRE_IPIF;
7107 		}
7108 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7109 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7110 			    ALL_ZONES, NULL, match_flags, ipst);
7111 		}
7112 		if (ire == NULL) {
7113 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7114 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7115 			    match_flags, ipst);
7116 		}
7117 	}
7118 
7119 	if (ire == NULL) {
7120 		/*
7121 		 * At this point, the gateway address is not one of our own
7122 		 * addresses or a matching interface route was not found.  We
7123 		 * set the IRE type to lookup based on whether
7124 		 * this is a host route, a default route or just a prefix.
7125 		 *
7126 		 * If an ipif_arg was passed in, then the lookup is based on an
7127 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7128 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7129 		 * set as the route being looked up is not a traditional
7130 		 * interface route.
7131 		 */
7132 		match_flags &= ~MATCH_IRE_IPIF;
7133 		match_flags |= MATCH_IRE_GW;
7134 		if (ipif_arg != NULL)
7135 			match_flags |= MATCH_IRE_ILL;
7136 		if (mask == IP_HOST_MASK)
7137 			type = IRE_HOST;
7138 		else if (mask == 0)
7139 			type = IRE_DEFAULT;
7140 		else
7141 			type = IRE_PREFIX;
7142 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7143 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7144 	}
7145 
7146 	if (ipif_refheld)
7147 		ipif_refrele(ipif);
7148 
7149 	/* ipif is not refheld anymore */
7150 	if (ire == NULL)
7151 		return (ESRCH);
7152 
7153 	if (ire->ire_flags & RTF_MULTIRT) {
7154 		/*
7155 		 * Invoke the CGTP (multirouting) filtering module
7156 		 * to remove the dst address from the filtering database.
7157 		 * Packets coming from that address will no longer be
7158 		 * filtered to remove duplicates.
7159 		 */
7160 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7161 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7162 			    ipst->ips_netstack->netstack_stackid,
7163 			    ire->ire_addr, ire->ire_gateway_addr);
7164 		}
7165 		ip_cgtp_bcast_delete(ire, ipst);
7166 	}
7167 
7168 	ipif = ire->ire_ipif;
7169 	if (ipif != NULL)
7170 		ipif_remove_ire(ipif, ire);
7171 	if (ioctl_msg)
7172 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7173 	ire_delete(ire);
7174 	ire_refrele(ire);
7175 	return (err);
7176 }
7177 
7178 /*
7179  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7180  */
7181 /* ARGSUSED */
7182 int
7183 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7184     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7185 {
7186 	ipaddr_t dst_addr;
7187 	ipaddr_t gw_addr;
7188 	ipaddr_t mask;
7189 	int error = 0;
7190 	mblk_t *mp1;
7191 	struct rtentry *rt;
7192 	ipif_t *ipif = NULL;
7193 	ip_stack_t	*ipst;
7194 
7195 	ASSERT(q->q_next == NULL);
7196 	ipst = CONNQ_TO_IPST(q);
7197 
7198 	ip1dbg(("ip_siocaddrt:"));
7199 	/* Existence of mp1 verified in ip_wput_nondata */
7200 	mp1 = mp->b_cont->b_cont;
7201 	rt = (struct rtentry *)mp1->b_rptr;
7202 
7203 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7204 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7205 
7206 	/*
7207 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7208 	 * to a particular host address.  In this case, we set the netmask to
7209 	 * all ones for the particular destination address.  Otherwise,
7210 	 * determine the netmask to be used based on dst_addr and the interfaces
7211 	 * in use.
7212 	 */
7213 	if (rt->rt_flags & RTF_HOST) {
7214 		mask = IP_HOST_MASK;
7215 	} else {
7216 		/*
7217 		 * Note that ip_subnet_mask returns a zero mask in the case of
7218 		 * default (an all-zeroes address).
7219 		 */
7220 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7221 	}
7222 
7223 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7224 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7225 	if (ipif != NULL)
7226 		ipif_refrele(ipif);
7227 	return (error);
7228 }
7229 
7230 /*
7231  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7232  */
7233 /* ARGSUSED */
7234 int
7235 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7236     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7237 {
7238 	ipaddr_t dst_addr;
7239 	ipaddr_t gw_addr;
7240 	ipaddr_t mask;
7241 	int error;
7242 	mblk_t *mp1;
7243 	struct rtentry *rt;
7244 	ipif_t *ipif = NULL;
7245 	ip_stack_t	*ipst;
7246 
7247 	ASSERT(q->q_next == NULL);
7248 	ipst = CONNQ_TO_IPST(q);
7249 
7250 	ip1dbg(("ip_siocdelrt:"));
7251 	/* Existence of mp1 verified in ip_wput_nondata */
7252 	mp1 = mp->b_cont->b_cont;
7253 	rt = (struct rtentry *)mp1->b_rptr;
7254 
7255 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7256 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7257 
7258 	/*
7259 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7260 	 * to a particular host address.  In this case, we set the netmask to
7261 	 * all ones for the particular destination address.  Otherwise,
7262 	 * determine the netmask to be used based on dst_addr and the interfaces
7263 	 * in use.
7264 	 */
7265 	if (rt->rt_flags & RTF_HOST) {
7266 		mask = IP_HOST_MASK;
7267 	} else {
7268 		/*
7269 		 * Note that ip_subnet_mask returns a zero mask in the case of
7270 		 * default (an all-zeroes address).
7271 		 */
7272 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7273 	}
7274 
7275 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7276 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7277 	    mp, ip_process_ioctl, ipst);
7278 	if (ipif != NULL)
7279 		ipif_refrele(ipif);
7280 	return (error);
7281 }
7282 
7283 /*
7284  * Enqueue the mp onto the ipsq, chained by b_next.
7285  * b_prev stores the function to be executed later, and b_queue the queue
7286  * where this mp originated.
7287  */
7288 void
7289 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7290     ill_t *pending_ill)
7291 {
7292 	conn_t	*connp;
7293 	ipxop_t *ipx = ipsq->ipsq_xop;
7294 
7295 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7296 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7297 	ASSERT(func != NULL);
7298 
7299 	mp->b_queue = q;
7300 	mp->b_prev = (void *)func;
7301 	mp->b_next = NULL;
7302 
7303 	switch (type) {
7304 	case CUR_OP:
7305 		if (ipx->ipx_mptail != NULL) {
7306 			ASSERT(ipx->ipx_mphead != NULL);
7307 			ipx->ipx_mptail->b_next = mp;
7308 		} else {
7309 			ASSERT(ipx->ipx_mphead == NULL);
7310 			ipx->ipx_mphead = mp;
7311 		}
7312 		ipx->ipx_mptail = mp;
7313 		break;
7314 
7315 	case NEW_OP:
7316 		if (ipsq->ipsq_xopq_mptail != NULL) {
7317 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7318 			ipsq->ipsq_xopq_mptail->b_next = mp;
7319 		} else {
7320 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7321 			ipsq->ipsq_xopq_mphead = mp;
7322 		}
7323 		ipsq->ipsq_xopq_mptail = mp;
7324 		ipx->ipx_ipsq_queued = B_TRUE;
7325 		break;
7326 
7327 	case SWITCH_OP:
7328 		ASSERT(ipsq->ipsq_swxop != NULL);
7329 		/* only one switch operation is currently allowed */
7330 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7331 		ipsq->ipsq_switch_mp = mp;
7332 		ipx->ipx_ipsq_queued = B_TRUE;
7333 		break;
7334 	default:
7335 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7336 	}
7337 
7338 	if (CONN_Q(q) && pending_ill != NULL) {
7339 		connp = Q_TO_CONN(q);
7340 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7341 		connp->conn_oper_pending_ill = pending_ill;
7342 	}
7343 }
7344 
7345 /*
7346  * Dequeue the next message that requested exclusive access to this IPSQ's
7347  * xop.  Specifically:
7348  *
7349  *  1. If we're still processing the current operation on `ipsq', then
7350  *     dequeue the next message for the operation (from ipx_mphead), or
7351  *     return NULL if there are no queued messages for the operation.
7352  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7353  *
7354  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7355  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7356  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7357  *     leaving IPMP groups and require a careful dance -- see the comments
7358  *     in-line below for details.  If we're leaving a group xop or if we're
7359  *     joining a group xop and become writer on it, then we proceed to (3).
7360  *     Otherwise, we return NULL and exit the xop.
7361  *
7362  *  3. For each IPSQ in the xop, return any switch operation stored on
7363  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7364  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7365  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7366  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7367  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7368  *     each phyint in the group, including the IPMP meta-interface phyint.
7369  */
7370 static mblk_t *
7371 ipsq_dq(ipsq_t *ipsq)
7372 {
7373 	ill_t	*illv4, *illv6;
7374 	mblk_t	*mp;
7375 	ipsq_t	*xopipsq;
7376 	ipsq_t	*leftipsq = NULL;
7377 	ipxop_t *ipx;
7378 	phyint_t *phyi = ipsq->ipsq_phyint;
7379 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7380 	boolean_t emptied = B_FALSE;
7381 
7382 	/*
7383 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7384 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7385 	 */
7386 	rw_enter(&ipst->ips_ill_g_lock,
7387 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7388 	mutex_enter(&ipsq->ipsq_lock);
7389 	ipx = ipsq->ipsq_xop;
7390 	mutex_enter(&ipx->ipx_lock);
7391 
7392 	/*
7393 	 * Dequeue the next message associated with the current exclusive
7394 	 * operation, if any.
7395 	 */
7396 	if ((mp = ipx->ipx_mphead) != NULL) {
7397 		ipx->ipx_mphead = mp->b_next;
7398 		if (ipx->ipx_mphead == NULL)
7399 			ipx->ipx_mptail = NULL;
7400 		mp->b_next = (void *)ipsq;
7401 		goto out;
7402 	}
7403 
7404 	if (ipx->ipx_current_ipif != NULL)
7405 		goto empty;
7406 
7407 	if (ipsq->ipsq_swxop != NULL) {
7408 		/*
7409 		 * The exclusive operation that is now being completed has
7410 		 * requested a switch to a different xop.  This happens
7411 		 * when an interface joins or leaves an IPMP group.  Joins
7412 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7413 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7414 		 * (phyint_free()), or interface plumb for an ill type
7415 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7416 		 *
7417 		 * Xop switches are not allowed on the IPMP meta-interface.
7418 		 */
7419 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7420 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7421 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7422 
7423 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7424 			/*
7425 			 * We're switching back to our own xop, so we have two
7426 			 * xop's to drain/exit: our own, and the group xop
7427 			 * that we are leaving.
7428 			 *
7429 			 * First, pull ourselves out of the group ipsq list.
7430 			 * This is safe since we're writer on ill_g_lock.
7431 			 */
7432 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7433 
7434 			xopipsq = ipx->ipx_ipsq;
7435 			while (xopipsq->ipsq_next != ipsq)
7436 				xopipsq = xopipsq->ipsq_next;
7437 
7438 			xopipsq->ipsq_next = ipsq->ipsq_next;
7439 			ipsq->ipsq_next = ipsq;
7440 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7441 			ipsq->ipsq_swxop = NULL;
7442 
7443 			/*
7444 			 * Second, prepare to exit the group xop.  The actual
7445 			 * ipsq_exit() is done at the end of this function
7446 			 * since we cannot hold any locks across ipsq_exit().
7447 			 * Note that although we drop the group's ipx_lock, no
7448 			 * threads can proceed since we're still ipx_writer.
7449 			 */
7450 			leftipsq = xopipsq;
7451 			mutex_exit(&ipx->ipx_lock);
7452 
7453 			/*
7454 			 * Third, set ipx to point to our own xop (which was
7455 			 * inactive and therefore can be entered).
7456 			 */
7457 			ipx = ipsq->ipsq_xop;
7458 			mutex_enter(&ipx->ipx_lock);
7459 			ASSERT(ipx->ipx_writer == NULL);
7460 			ASSERT(ipx->ipx_current_ipif == NULL);
7461 		} else {
7462 			/*
7463 			 * We're switching from our own xop to a group xop.
7464 			 * The requestor of the switch must ensure that the
7465 			 * group xop cannot go away (e.g. by ensuring the
7466 			 * phyint associated with the xop cannot go away).
7467 			 *
7468 			 * If we can become writer on our new xop, then we'll
7469 			 * do the drain.  Otherwise, the current writer of our
7470 			 * new xop will do the drain when it exits.
7471 			 *
7472 			 * First, splice ourselves into the group IPSQ list.
7473 			 * This is safe since we're writer on ill_g_lock.
7474 			 */
7475 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7476 
7477 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7478 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7479 				xopipsq = xopipsq->ipsq_next;
7480 
7481 			xopipsq->ipsq_next = ipsq;
7482 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7483 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7484 			ipsq->ipsq_swxop = NULL;
7485 
7486 			/*
7487 			 * Second, exit our own xop, since it's now unused.
7488 			 * This is safe since we've got the only reference.
7489 			 */
7490 			ASSERT(ipx->ipx_writer == curthread);
7491 			ipx->ipx_writer = NULL;
7492 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7493 			ipx->ipx_ipsq_queued = B_FALSE;
7494 			mutex_exit(&ipx->ipx_lock);
7495 
7496 			/*
7497 			 * Third, set ipx to point to our new xop, and check
7498 			 * if we can become writer on it.  If we cannot, then
7499 			 * the current writer will drain the IPSQ group when
7500 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7501 			 * because we're still holding ipsq_lock.
7502 			 */
7503 			ipx = ipsq->ipsq_xop;
7504 			mutex_enter(&ipx->ipx_lock);
7505 			if (ipx->ipx_writer != NULL ||
7506 			    ipx->ipx_current_ipif != NULL) {
7507 				goto out;
7508 			}
7509 		}
7510 
7511 		/*
7512 		 * Fourth, become writer on our new ipx before we continue
7513 		 * with the drain.  Note that we never dropped ipsq_lock
7514 		 * above, so no other thread could've raced with us to
7515 		 * become writer first.  Also, we're holding ipx_lock, so
7516 		 * no other thread can examine the ipx right now.
7517 		 */
7518 		ASSERT(ipx->ipx_current_ipif == NULL);
7519 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7520 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7521 		ipx->ipx_writer = curthread;
7522 		ipx->ipx_forced = B_FALSE;
7523 #ifdef DEBUG
7524 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7525 #endif
7526 	}
7527 
7528 	xopipsq = ipsq;
7529 	do {
7530 		/*
7531 		 * So that other operations operate on a consistent and
7532 		 * complete phyint, a switch message on an IPSQ must be
7533 		 * handled prior to any other operations on that IPSQ.
7534 		 */
7535 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7536 			xopipsq->ipsq_switch_mp = NULL;
7537 			ASSERT(mp->b_next == NULL);
7538 			mp->b_next = (void *)xopipsq;
7539 			goto out;
7540 		}
7541 
7542 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7543 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7544 			if (xopipsq->ipsq_xopq_mphead == NULL)
7545 				xopipsq->ipsq_xopq_mptail = NULL;
7546 			mp->b_next = (void *)xopipsq;
7547 			goto out;
7548 		}
7549 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7550 empty:
7551 	/*
7552 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7553 	 * new messages can end up on any IPSQ in the xop.
7554 	 */
7555 	ipx->ipx_writer = NULL;
7556 	ipx->ipx_forced = B_FALSE;
7557 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7558 	ipx->ipx_ipsq_queued = B_FALSE;
7559 	emptied = B_TRUE;
7560 #ifdef	DEBUG
7561 	ipx->ipx_depth = 0;
7562 #endif
7563 out:
7564 	mutex_exit(&ipx->ipx_lock);
7565 	mutex_exit(&ipsq->ipsq_lock);
7566 
7567 	/*
7568 	 * If we completely emptied the xop, then wake up any threads waiting
7569 	 * to enter any of the IPSQ's associated with it.
7570 	 */
7571 	if (emptied) {
7572 		xopipsq = ipsq;
7573 		do {
7574 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7575 				continue;
7576 
7577 			illv4 = phyi->phyint_illv4;
7578 			illv6 = phyi->phyint_illv6;
7579 
7580 			GRAB_ILL_LOCKS(illv4, illv6);
7581 			if (illv4 != NULL)
7582 				cv_broadcast(&illv4->ill_cv);
7583 			if (illv6 != NULL)
7584 				cv_broadcast(&illv6->ill_cv);
7585 			RELEASE_ILL_LOCKS(illv4, illv6);
7586 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7587 	}
7588 	rw_exit(&ipst->ips_ill_g_lock);
7589 
7590 	/*
7591 	 * Now that all locks are dropped, exit the IPSQ we left.
7592 	 */
7593 	if (leftipsq != NULL)
7594 		ipsq_exit(leftipsq);
7595 
7596 	return (mp);
7597 }
7598 
7599 /*
7600  * Enter the ipsq corresponding to ill, by waiting synchronously till
7601  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7602  * will have to drain completely before ipsq_enter returns success.
7603  * ipx_current_ipif will be set if some exclusive op is in progress,
7604  * and the ipsq_exit logic will start the next enqueued op after
7605  * completion of the current op. If 'force' is used, we don't wait
7606  * for the enqueued ops. This is needed when a conn_close wants to
7607  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7608  * of an ill can also use this option. But we dont' use it currently.
7609  */
7610 #define	ENTER_SQ_WAIT_TICKS 100
7611 boolean_t
7612 ipsq_enter(ill_t *ill, boolean_t force, int type)
7613 {
7614 	ipsq_t	*ipsq;
7615 	ipxop_t *ipx;
7616 	boolean_t waited_enough = B_FALSE;
7617 
7618 	/*
7619 	 * Note that the relationship between ill and ipsq is fixed as long as
7620 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7621 	 * relationship between the IPSQ and xop cannot change.  However,
7622 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7623 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7624 	 * waking up all ills in the xop when it becomes available.
7625 	 */
7626 	mutex_enter(&ill->ill_lock);
7627 	for (;;) {
7628 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7629 			mutex_exit(&ill->ill_lock);
7630 			return (B_FALSE);
7631 		}
7632 
7633 		ipsq = ill->ill_phyint->phyint_ipsq;
7634 		mutex_enter(&ipsq->ipsq_lock);
7635 		ipx = ipsq->ipsq_xop;
7636 		mutex_enter(&ipx->ipx_lock);
7637 
7638 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7639 		    ipx->ipx_current_ipif == NULL || waited_enough))
7640 			break;
7641 
7642 		if (!force || ipx->ipx_writer != NULL) {
7643 			mutex_exit(&ipx->ipx_lock);
7644 			mutex_exit(&ipsq->ipsq_lock);
7645 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7646 		} else {
7647 			mutex_exit(&ipx->ipx_lock);
7648 			mutex_exit(&ipsq->ipsq_lock);
7649 			(void) cv_timedwait(&ill->ill_cv,
7650 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7651 			waited_enough = B_TRUE;
7652 		}
7653 	}
7654 
7655 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7656 	ASSERT(ipx->ipx_reentry_cnt == 0);
7657 	ipx->ipx_writer = curthread;
7658 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7659 	ipx->ipx_reentry_cnt++;
7660 #ifdef DEBUG
7661 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7662 #endif
7663 	mutex_exit(&ipx->ipx_lock);
7664 	mutex_exit(&ipsq->ipsq_lock);
7665 	mutex_exit(&ill->ill_lock);
7666 	return (B_TRUE);
7667 }
7668 
7669 boolean_t
7670 ill_perim_enter(ill_t *ill)
7671 {
7672 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7673 }
7674 
7675 void
7676 ill_perim_exit(ill_t *ill)
7677 {
7678 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7679 }
7680 
7681 /*
7682  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7683  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7684  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7685  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7686  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7687  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7688  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7689  * up the interface) and are enqueued in ipx_mphead.
7690  *
7691  * If a thread does not want to reenter the ipsq when it is already writer,
7692  * it must make sure that the specified reentry point to be called later
7693  * when the ipsq is empty, nor any code path starting from the specified reentry
7694  * point must never ever try to enter the ipsq again. Otherwise it can lead
7695  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7696  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7697  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7698  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7699  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7700  * ioctl if the current ioctl has completed. If the current ioctl is still
7701  * in progress it simply returns. The current ioctl could be waiting for
7702  * a response from another module (arp or the driver or could be waiting for
7703  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7704  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7705  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7706  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7707  * all associated DLPI operations have completed.
7708  */
7709 
7710 /*
7711  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7712  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7713  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7714  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7715  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7716  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7717  */
7718 ipsq_t *
7719 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7720     ipsq_func_t func, int type, boolean_t reentry_ok)
7721 {
7722 	ipsq_t	*ipsq;
7723 	ipxop_t	*ipx;
7724 
7725 	/* Only 1 of ipif or ill can be specified */
7726 	ASSERT((ipif != NULL) ^ (ill != NULL));
7727 	if (ipif != NULL)
7728 		ill = ipif->ipif_ill;
7729 
7730 	/*
7731 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7732 	 * ipx of an ipsq can't change when ipsq_lock is held.
7733 	 */
7734 	GRAB_CONN_LOCK(q);
7735 	mutex_enter(&ill->ill_lock);
7736 	ipsq = ill->ill_phyint->phyint_ipsq;
7737 	mutex_enter(&ipsq->ipsq_lock);
7738 	ipx = ipsq->ipsq_xop;
7739 	mutex_enter(&ipx->ipx_lock);
7740 
7741 	/*
7742 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7743 	 *    (Note: If the caller does not specify reentry_ok then neither
7744 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7745 	 *    again. Otherwise it can lead to an infinite loop
7746 	 * 2. Enter the ipsq if there is no current writer and this attempted
7747 	 *    entry is part of the current operation
7748 	 * 3. Enter the ipsq if there is no current writer and this is a new
7749 	 *    operation and the operation queue is empty and there is no
7750 	 *    operation currently in progress
7751 	 */
7752 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7753 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7754 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7755 		/* Success. */
7756 		ipx->ipx_reentry_cnt++;
7757 		ipx->ipx_writer = curthread;
7758 		ipx->ipx_forced = B_FALSE;
7759 		mutex_exit(&ipx->ipx_lock);
7760 		mutex_exit(&ipsq->ipsq_lock);
7761 		mutex_exit(&ill->ill_lock);
7762 		RELEASE_CONN_LOCK(q);
7763 #ifdef DEBUG
7764 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7765 #endif
7766 		return (ipsq);
7767 	}
7768 
7769 	if (func != NULL)
7770 		ipsq_enq(ipsq, q, mp, func, type, ill);
7771 
7772 	mutex_exit(&ipx->ipx_lock);
7773 	mutex_exit(&ipsq->ipsq_lock);
7774 	mutex_exit(&ill->ill_lock);
7775 	RELEASE_CONN_LOCK(q);
7776 	return (NULL);
7777 }
7778 
7779 /*
7780  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7781  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7782  * cannot be entered, the mp is queued for completion.
7783  */
7784 void
7785 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7786     boolean_t reentry_ok)
7787 {
7788 	ipsq_t	*ipsq;
7789 
7790 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7791 
7792 	/*
7793 	 * Drop the caller's refhold on the ill.  This is safe since we either
7794 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7795 	 * IPSQ, in which case we return without accessing ill anymore.  This
7796 	 * is needed because func needs to see the correct refcount.
7797 	 * e.g. removeif can work only then.
7798 	 */
7799 	ill_refrele(ill);
7800 	if (ipsq != NULL) {
7801 		(*func)(ipsq, q, mp, NULL);
7802 		ipsq_exit(ipsq);
7803 	}
7804 }
7805 
7806 /*
7807  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7808  * prior to exiting.  Caller must be writer on the specified IPSQ.
7809  */
7810 void
7811 ipsq_exit(ipsq_t *ipsq)
7812 {
7813 	mblk_t *mp;
7814 	ipsq_t *mp_ipsq;
7815 	queue_t	*q;
7816 	phyint_t *phyi;
7817 	ipsq_func_t func;
7818 
7819 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7820 
7821 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7822 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7823 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7824 		return;
7825 	}
7826 
7827 	for (;;) {
7828 		phyi = ipsq->ipsq_phyint;
7829 		mp = ipsq_dq(ipsq);
7830 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7831 
7832 		/*
7833 		 * If we've changed to a new IPSQ, and the phyint associated
7834 		 * with the old one has gone away, free the old IPSQ.  Note
7835 		 * that this cannot happen while the IPSQ is in a group.
7836 		 */
7837 		if (mp_ipsq != ipsq && phyi == NULL) {
7838 			ASSERT(ipsq->ipsq_next == ipsq);
7839 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7840 			ipsq_delete(ipsq);
7841 		}
7842 
7843 		if (mp == NULL)
7844 			break;
7845 
7846 		q = mp->b_queue;
7847 		func = (ipsq_func_t)mp->b_prev;
7848 		ipsq = mp_ipsq;
7849 		mp->b_next = mp->b_prev = NULL;
7850 		mp->b_queue = NULL;
7851 
7852 		/*
7853 		 * If 'q' is an conn queue, it is valid, since we did a
7854 		 * a refhold on the conn at the start of the ioctl.
7855 		 * If 'q' is an ill queue, it is valid, since close of an
7856 		 * ill will clean up its IPSQ.
7857 		 */
7858 		(*func)(ipsq, q, mp, NULL);
7859 	}
7860 }
7861 
7862 /*
7863  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7864  * and `ioccmd'.
7865  */
7866 void
7867 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7868 {
7869 	ill_t *ill = ipif->ipif_ill;
7870 	ipxop_t *ipx = ipsq->ipsq_xop;
7871 
7872 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7873 	ASSERT(ipx->ipx_current_ipif == NULL);
7874 	ASSERT(ipx->ipx_current_ioctl == 0);
7875 
7876 	ipx->ipx_current_done = B_FALSE;
7877 	ipx->ipx_current_ioctl = ioccmd;
7878 	mutex_enter(&ipx->ipx_lock);
7879 	ipx->ipx_current_ipif = ipif;
7880 	mutex_exit(&ipx->ipx_lock);
7881 
7882 	/*
7883 	 * Set IPIF_CHANGING on one or more ipifs associated with the
7884 	 * current exclusive operation.  IPIF_CHANGING prevents any new
7885 	 * references to the ipif (so that the references will eventually
7886 	 * drop to zero) and also prevents any "get" operations (e.g.,
7887 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
7888 	 * operation has completed and the ipif is again in a stable state.
7889 	 *
7890 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
7891 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
7892 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
7893 	 * ipifs will be affected.
7894 	 *
7895 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
7896 	 * IPIF_CONDEMNED internally after identifying the right ipif to
7897 	 * operate on.
7898 	 */
7899 	switch (ioccmd) {
7900 	case SIOCLIFREMOVEIF:
7901 		break;
7902 	case 0:
7903 		mutex_enter(&ill->ill_lock);
7904 		ipif = ipif->ipif_ill->ill_ipif;
7905 		for (; ipif != NULL; ipif = ipif->ipif_next)
7906 			ipif->ipif_state_flags |= IPIF_CHANGING;
7907 		mutex_exit(&ill->ill_lock);
7908 		break;
7909 	default:
7910 		mutex_enter(&ill->ill_lock);
7911 		ipif->ipif_state_flags |= IPIF_CHANGING;
7912 		mutex_exit(&ill->ill_lock);
7913 	}
7914 }
7915 
7916 /*
7917  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
7918  * the next exclusive operation to begin once we ipsq_exit().  However, if
7919  * pending DLPI operations remain, then we will wait for the queue to drain
7920  * before allowing the next exclusive operation to begin.  This ensures that
7921  * DLPI operations from one exclusive operation are never improperly processed
7922  * as part of a subsequent exclusive operation.
7923  */
7924 void
7925 ipsq_current_finish(ipsq_t *ipsq)
7926 {
7927 	ipxop_t	*ipx = ipsq->ipsq_xop;
7928 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
7929 	ipif_t	*ipif = ipx->ipx_current_ipif;
7930 
7931 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7932 
7933 	/*
7934 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
7935 	 * (but in that case, IPIF_CHANGING will already be clear and no
7936 	 * pending DLPI messages can remain).
7937 	 */
7938 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
7939 		ill_t *ill = ipif->ipif_ill;
7940 
7941 		mutex_enter(&ill->ill_lock);
7942 		dlpi_pending = ill->ill_dlpi_pending;
7943 		if (ipx->ipx_current_ioctl == 0) {
7944 			ipif = ill->ill_ipif;
7945 			for (; ipif != NULL; ipif = ipif->ipif_next)
7946 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
7947 		} else {
7948 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
7949 		}
7950 		mutex_exit(&ill->ill_lock);
7951 	}
7952 
7953 	ASSERT(!ipx->ipx_current_done);
7954 	ipx->ipx_current_done = B_TRUE;
7955 	ipx->ipx_current_ioctl = 0;
7956 	if (dlpi_pending == DL_PRIM_INVAL) {
7957 		mutex_enter(&ipx->ipx_lock);
7958 		ipx->ipx_current_ipif = NULL;
7959 		mutex_exit(&ipx->ipx_lock);
7960 	}
7961 }
7962 
7963 /*
7964  * The ill is closing. Flush all messages on the ipsq that originated
7965  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7966  * for this ill since ipsq_enter could not have entered until then.
7967  * New messages can't be queued since the CONDEMNED flag is set.
7968  */
7969 static void
7970 ipsq_flush(ill_t *ill)
7971 {
7972 	queue_t	*q;
7973 	mblk_t	*prev;
7974 	mblk_t	*mp;
7975 	mblk_t	*mp_next;
7976 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7977 
7978 	ASSERT(IAM_WRITER_ILL(ill));
7979 
7980 	/*
7981 	 * Flush any messages sent up by the driver.
7982 	 */
7983 	mutex_enter(&ipx->ipx_lock);
7984 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7985 		mp_next = mp->b_next;
7986 		q = mp->b_queue;
7987 		if (q == ill->ill_rq || q == ill->ill_wq) {
7988 			/* dequeue mp */
7989 			if (prev == NULL)
7990 				ipx->ipx_mphead = mp->b_next;
7991 			else
7992 				prev->b_next = mp->b_next;
7993 			if (ipx->ipx_mptail == mp) {
7994 				ASSERT(mp_next == NULL);
7995 				ipx->ipx_mptail = prev;
7996 			}
7997 			inet_freemsg(mp);
7998 		} else {
7999 			prev = mp;
8000 		}
8001 	}
8002 	mutex_exit(&ipx->ipx_lock);
8003 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8004 	ipsq_xopq_mp_cleanup(ill, NULL);
8005 	ill_pending_mp_cleanup(ill);
8006 }
8007 
8008 /*
8009  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8010  * refhold and return the associated ipif
8011  */
8012 /* ARGSUSED */
8013 int
8014 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8015     cmd_info_t *ci, ipsq_func_t func)
8016 {
8017 	boolean_t exists;
8018 	struct iftun_req *ta;
8019 	ipif_t  *ipif;
8020 	ill_t   *ill;
8021 	boolean_t isv6;
8022 	mblk_t  *mp1;
8023 	int error;
8024 	conn_t  *connp;
8025 	ip_stack_t  *ipst;
8026 
8027 	/* Existence verified in ip_wput_nondata */
8028 	mp1 = mp->b_cont->b_cont;
8029 	ta = (struct iftun_req *)mp1->b_rptr;
8030 	/*
8031 	 * Null terminate the string to protect against buffer
8032 	 * overrun. String was generated by user code and may not
8033 	 * be trusted.
8034 	 */
8035 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8036 
8037 	connp = Q_TO_CONN(q);
8038 	isv6 = connp->conn_af_isv6;
8039 	ipst = connp->conn_netstack->netstack_ip;
8040 
8041 	/* Disallows implicit create */
8042 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8043 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8044 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8045 	if (ipif == NULL)
8046 		return (error);
8047 
8048 	if (ipif->ipif_id != 0) {
8049 		/*
8050 		 * We really don't want to set/get tunnel parameters
8051 		 * on virtual tunnel interfaces.  Only allow the
8052 		 * base tunnel to do these.
8053 		 */
8054 		ipif_refrele(ipif);
8055 		return (EINVAL);
8056 	}
8057 
8058 	/*
8059 	 * Send down to tunnel mod for ioctl processing.
8060 	 * Will finish ioctl in ip_rput_other().
8061 	 */
8062 	ill = ipif->ipif_ill;
8063 	if (ill->ill_net_type == IRE_LOOPBACK) {
8064 		ipif_refrele(ipif);
8065 		return (EOPNOTSUPP);
8066 	}
8067 
8068 	if (ill->ill_wq == NULL) {
8069 		ipif_refrele(ipif);
8070 		return (ENXIO);
8071 	}
8072 	/*
8073 	 * Mark the ioctl as coming from an IPv6 interface for
8074 	 * tun's convenience.
8075 	 */
8076 	if (ill->ill_isv6)
8077 		ta->ifta_flags |= 0x80000000;
8078 	ci->ci_ipif = ipif;
8079 	return (0);
8080 }
8081 
8082 /*
8083  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8084  * and return the associated ipif.
8085  * Return value:
8086  *	Non zero: An error has occurred. ci may not be filled out.
8087  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8088  *	a held ipif in ci.ci_ipif.
8089  */
8090 int
8091 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8092     cmd_info_t *ci, ipsq_func_t func)
8093 {
8094 	char		*name;
8095 	struct ifreq    *ifr;
8096 	struct lifreq    *lifr;
8097 	ipif_t		*ipif = NULL;
8098 	ill_t		*ill;
8099 	conn_t		*connp;
8100 	boolean_t	isv6;
8101 	boolean_t	exists;
8102 	int		err;
8103 	mblk_t		*mp1;
8104 	zoneid_t	zoneid;
8105 	ip_stack_t	*ipst;
8106 
8107 	if (q->q_next != NULL) {
8108 		ill = (ill_t *)q->q_ptr;
8109 		isv6 = ill->ill_isv6;
8110 		connp = NULL;
8111 		zoneid = ALL_ZONES;
8112 		ipst = ill->ill_ipst;
8113 	} else {
8114 		ill = NULL;
8115 		connp = Q_TO_CONN(q);
8116 		isv6 = connp->conn_af_isv6;
8117 		zoneid = connp->conn_zoneid;
8118 		if (zoneid == GLOBAL_ZONEID) {
8119 			/* global zone can access ipifs in all zones */
8120 			zoneid = ALL_ZONES;
8121 		}
8122 		ipst = connp->conn_netstack->netstack_ip;
8123 	}
8124 
8125 	/* Has been checked in ip_wput_nondata */
8126 	mp1 = mp->b_cont->b_cont;
8127 
8128 	if (ipip->ipi_cmd_type == IF_CMD) {
8129 		/* This a old style SIOC[GS]IF* command */
8130 		ifr = (struct ifreq *)mp1->b_rptr;
8131 		/*
8132 		 * Null terminate the string to protect against buffer
8133 		 * overrun. String was generated by user code and may not
8134 		 * be trusted.
8135 		 */
8136 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8137 		name = ifr->ifr_name;
8138 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8139 		ci->ci_sin6 = NULL;
8140 		ci->ci_lifr = (struct lifreq *)ifr;
8141 	} else {
8142 		/* This a new style SIOC[GS]LIF* command */
8143 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8144 		lifr = (struct lifreq *)mp1->b_rptr;
8145 		/*
8146 		 * Null terminate the string to protect against buffer
8147 		 * overrun. String was generated by user code and may not
8148 		 * be trusted.
8149 		 */
8150 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8151 		name = lifr->lifr_name;
8152 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8153 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8154 		ci->ci_lifr = lifr;
8155 	}
8156 
8157 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8158 		/*
8159 		 * The ioctl will be failed if the ioctl comes down
8160 		 * an conn stream
8161 		 */
8162 		if (ill == NULL) {
8163 			/*
8164 			 * Not an ill queue, return EINVAL same as the
8165 			 * old error code.
8166 			 */
8167 			return (ENXIO);
8168 		}
8169 		ipif = ill->ill_ipif;
8170 		ipif_refhold(ipif);
8171 	} else {
8172 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8173 		    &exists, isv6, zoneid,
8174 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8175 		    ipst);
8176 		if (ipif == NULL) {
8177 			if (err == EINPROGRESS)
8178 				return (err);
8179 			err = 0;	/* Ensure we don't use it below */
8180 		}
8181 	}
8182 
8183 	/*
8184 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8185 	 */
8186 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8187 		ipif_refrele(ipif);
8188 		return (ENXIO);
8189 	}
8190 
8191 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8192 	    name[0] == '\0') {
8193 		/*
8194 		 * Handle a or a SIOC?IF* with a null name
8195 		 * during plumb (on the ill queue before the I_PLINK).
8196 		 */
8197 		ipif = ill->ill_ipif;
8198 		ipif_refhold(ipif);
8199 	}
8200 
8201 	if (ipif == NULL)
8202 		return (ENXIO);
8203 
8204 	ci->ci_ipif = ipif;
8205 	return (0);
8206 }
8207 
8208 /*
8209  * Return the total number of ipifs.
8210  */
8211 static uint_t
8212 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8213 {
8214 	uint_t numifs = 0;
8215 	ill_t	*ill;
8216 	ill_walk_context_t	ctx;
8217 	ipif_t	*ipif;
8218 
8219 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8220 	ill = ILL_START_WALK_V4(&ctx, ipst);
8221 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8222 		if (IS_UNDER_IPMP(ill))
8223 			continue;
8224 		for (ipif = ill->ill_ipif; ipif != NULL;
8225 		    ipif = ipif->ipif_next) {
8226 			if (ipif->ipif_zoneid == zoneid ||
8227 			    ipif->ipif_zoneid == ALL_ZONES)
8228 				numifs++;
8229 		}
8230 	}
8231 	rw_exit(&ipst->ips_ill_g_lock);
8232 	return (numifs);
8233 }
8234 
8235 /*
8236  * Return the total number of ipifs.
8237  */
8238 static uint_t
8239 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8240 {
8241 	uint_t numifs = 0;
8242 	ill_t	*ill;
8243 	ipif_t	*ipif;
8244 	ill_walk_context_t	ctx;
8245 
8246 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8247 
8248 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8249 	if (family == AF_INET)
8250 		ill = ILL_START_WALK_V4(&ctx, ipst);
8251 	else if (family == AF_INET6)
8252 		ill = ILL_START_WALK_V6(&ctx, ipst);
8253 	else
8254 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8255 
8256 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8257 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8258 			continue;
8259 
8260 		for (ipif = ill->ill_ipif; ipif != NULL;
8261 		    ipif = ipif->ipif_next) {
8262 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8263 			    !(lifn_flags & LIFC_NOXMIT))
8264 				continue;
8265 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8266 			    !(lifn_flags & LIFC_TEMPORARY))
8267 				continue;
8268 			if (((ipif->ipif_flags &
8269 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8270 			    IPIF_DEPRECATED)) ||
8271 			    IS_LOOPBACK(ill) ||
8272 			    !(ipif->ipif_flags & IPIF_UP)) &&
8273 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8274 				continue;
8275 
8276 			if (zoneid != ipif->ipif_zoneid &&
8277 			    ipif->ipif_zoneid != ALL_ZONES &&
8278 			    (zoneid != GLOBAL_ZONEID ||
8279 			    !(lifn_flags & LIFC_ALLZONES)))
8280 				continue;
8281 
8282 			numifs++;
8283 		}
8284 	}
8285 	rw_exit(&ipst->ips_ill_g_lock);
8286 	return (numifs);
8287 }
8288 
8289 uint_t
8290 ip_get_lifsrcofnum(ill_t *ill)
8291 {
8292 	uint_t numifs = 0;
8293 	ill_t	*ill_head = ill;
8294 	ip_stack_t	*ipst = ill->ill_ipst;
8295 
8296 	/*
8297 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8298 	 * other thread may be trying to relink the ILLs in this usesrc group
8299 	 * and adjusting the ill_usesrc_grp_next pointers
8300 	 */
8301 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8302 	if ((ill->ill_usesrc_ifindex == 0) &&
8303 	    (ill->ill_usesrc_grp_next != NULL)) {
8304 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8305 		    ill = ill->ill_usesrc_grp_next)
8306 			numifs++;
8307 	}
8308 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8309 
8310 	return (numifs);
8311 }
8312 
8313 /* Null values are passed in for ipif, sin, and ifreq */
8314 /* ARGSUSED */
8315 int
8316 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8317     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8318 {
8319 	int *nump;
8320 	conn_t *connp = Q_TO_CONN(q);
8321 
8322 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8323 
8324 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8325 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8326 
8327 	*nump = ip_get_numifs(connp->conn_zoneid,
8328 	    connp->conn_netstack->netstack_ip);
8329 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8330 	return (0);
8331 }
8332 
8333 /* Null values are passed in for ipif, sin, and ifreq */
8334 /* ARGSUSED */
8335 int
8336 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8337     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8338 {
8339 	struct lifnum *lifn;
8340 	mblk_t	*mp1;
8341 	conn_t *connp = Q_TO_CONN(q);
8342 
8343 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8344 
8345 	/* Existence checked in ip_wput_nondata */
8346 	mp1 = mp->b_cont->b_cont;
8347 
8348 	lifn = (struct lifnum *)mp1->b_rptr;
8349 	switch (lifn->lifn_family) {
8350 	case AF_UNSPEC:
8351 	case AF_INET:
8352 	case AF_INET6:
8353 		break;
8354 	default:
8355 		return (EAFNOSUPPORT);
8356 	}
8357 
8358 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8359 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8360 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8361 	return (0);
8362 }
8363 
8364 /* ARGSUSED */
8365 int
8366 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8367     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8368 {
8369 	STRUCT_HANDLE(ifconf, ifc);
8370 	mblk_t *mp1;
8371 	struct iocblk *iocp;
8372 	struct ifreq *ifr;
8373 	ill_walk_context_t	ctx;
8374 	ill_t	*ill;
8375 	ipif_t	*ipif;
8376 	struct sockaddr_in *sin;
8377 	int32_t	ifclen;
8378 	zoneid_t zoneid;
8379 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8380 
8381 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8382 
8383 	ip1dbg(("ip_sioctl_get_ifconf"));
8384 	/* Existence verified in ip_wput_nondata */
8385 	mp1 = mp->b_cont->b_cont;
8386 	iocp = (struct iocblk *)mp->b_rptr;
8387 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8388 
8389 	/*
8390 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8391 	 * the user buffer address and length into which the list of struct
8392 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8393 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8394 	 * the SIOCGIFCONF operation was redefined to simply provide
8395 	 * a large output buffer into which we are supposed to jam the ifreq
8396 	 * array.  The same ioctl command code was used, despite the fact that
8397 	 * both the applications and the kernel code had to change, thus making
8398 	 * it impossible to support both interfaces.
8399 	 *
8400 	 * For reasons not good enough to try to explain, the following
8401 	 * algorithm is used for deciding what to do with one of these:
8402 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8403 	 * form with the output buffer coming down as the continuation message.
8404 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8405 	 * and we have to copy in the ifconf structure to find out how big the
8406 	 * output buffer is and where to copy out to.  Sure no problem...
8407 	 *
8408 	 */
8409 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8410 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8411 		int numifs = 0;
8412 		size_t ifc_bufsize;
8413 
8414 		/*
8415 		 * Must be (better be!) continuation of a TRANSPARENT
8416 		 * IOCTL.  We just copied in the ifconf structure.
8417 		 */
8418 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8419 		    (struct ifconf *)mp1->b_rptr);
8420 
8421 		/*
8422 		 * Allocate a buffer to hold requested information.
8423 		 *
8424 		 * If ifc_len is larger than what is needed, we only
8425 		 * allocate what we will use.
8426 		 *
8427 		 * If ifc_len is smaller than what is needed, return
8428 		 * EINVAL.
8429 		 *
8430 		 * XXX: the ill_t structure can hava 2 counters, for
8431 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8432 		 * number of interfaces for a device, so we don't need
8433 		 * to count them here...
8434 		 */
8435 		numifs = ip_get_numifs(zoneid, ipst);
8436 
8437 		ifclen = STRUCT_FGET(ifc, ifc_len);
8438 		ifc_bufsize = numifs * sizeof (struct ifreq);
8439 		if (ifc_bufsize > ifclen) {
8440 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8441 				/* old behaviour */
8442 				return (EINVAL);
8443 			} else {
8444 				ifc_bufsize = ifclen;
8445 			}
8446 		}
8447 
8448 		mp1 = mi_copyout_alloc(q, mp,
8449 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8450 		if (mp1 == NULL)
8451 			return (ENOMEM);
8452 
8453 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8454 	}
8455 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8456 	/*
8457 	 * the SIOCGIFCONF ioctl only knows about
8458 	 * IPv4 addresses, so don't try to tell
8459 	 * it about interfaces with IPv6-only
8460 	 * addresses. (Last parm 'isv6' is B_FALSE)
8461 	 */
8462 
8463 	ifr = (struct ifreq *)mp1->b_rptr;
8464 
8465 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8466 	ill = ILL_START_WALK_V4(&ctx, ipst);
8467 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8468 		if (IS_UNDER_IPMP(ill))
8469 			continue;
8470 		for (ipif = ill->ill_ipif; ipif != NULL;
8471 		    ipif = ipif->ipif_next) {
8472 			if (zoneid != ipif->ipif_zoneid &&
8473 			    ipif->ipif_zoneid != ALL_ZONES)
8474 				continue;
8475 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8476 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8477 					/* old behaviour */
8478 					rw_exit(&ipst->ips_ill_g_lock);
8479 					return (EINVAL);
8480 				} else {
8481 					goto if_copydone;
8482 				}
8483 			}
8484 			ipif_get_name(ipif, ifr->ifr_name,
8485 			    sizeof (ifr->ifr_name));
8486 			sin = (sin_t *)&ifr->ifr_addr;
8487 			*sin = sin_null;
8488 			sin->sin_family = AF_INET;
8489 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8490 			ifr++;
8491 		}
8492 	}
8493 if_copydone:
8494 	rw_exit(&ipst->ips_ill_g_lock);
8495 	mp1->b_wptr = (uchar_t *)ifr;
8496 
8497 	if (STRUCT_BUF(ifc) != NULL) {
8498 		STRUCT_FSET(ifc, ifc_len,
8499 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8500 	}
8501 	return (0);
8502 }
8503 
8504 /*
8505  * Get the interfaces using the address hosted on the interface passed in,
8506  * as a source adddress
8507  */
8508 /* ARGSUSED */
8509 int
8510 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8511     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8512 {
8513 	mblk_t *mp1;
8514 	ill_t	*ill, *ill_head;
8515 	ipif_t	*ipif, *orig_ipif;
8516 	int	numlifs = 0;
8517 	size_t	lifs_bufsize, lifsmaxlen;
8518 	struct	lifreq *lifr;
8519 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8520 	uint_t	ifindex;
8521 	zoneid_t zoneid;
8522 	int err = 0;
8523 	boolean_t isv6 = B_FALSE;
8524 	struct	sockaddr_in	*sin;
8525 	struct	sockaddr_in6	*sin6;
8526 	STRUCT_HANDLE(lifsrcof, lifs);
8527 	ip_stack_t		*ipst;
8528 
8529 	ipst = CONNQ_TO_IPST(q);
8530 
8531 	ASSERT(q->q_next == NULL);
8532 
8533 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8534 
8535 	/* Existence verified in ip_wput_nondata */
8536 	mp1 = mp->b_cont->b_cont;
8537 
8538 	/*
8539 	 * Must be (better be!) continuation of a TRANSPARENT
8540 	 * IOCTL.  We just copied in the lifsrcof structure.
8541 	 */
8542 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8543 	    (struct lifsrcof *)mp1->b_rptr);
8544 
8545 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8546 		return (EINVAL);
8547 
8548 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8549 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8550 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8551 	    ip_process_ioctl, &err, ipst);
8552 	if (ipif == NULL) {
8553 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8554 		    ifindex));
8555 		return (err);
8556 	}
8557 
8558 	/* Allocate a buffer to hold requested information */
8559 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8560 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8561 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8562 	/* The actual size needed is always returned in lifs_len */
8563 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8564 
8565 	/* If the amount we need is more than what is passed in, abort */
8566 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8567 		ipif_refrele(ipif);
8568 		return (0);
8569 	}
8570 
8571 	mp1 = mi_copyout_alloc(q, mp,
8572 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8573 	if (mp1 == NULL) {
8574 		ipif_refrele(ipif);
8575 		return (ENOMEM);
8576 	}
8577 
8578 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8579 	bzero(mp1->b_rptr, lifs_bufsize);
8580 
8581 	lifr = (struct lifreq *)mp1->b_rptr;
8582 
8583 	ill = ill_head = ipif->ipif_ill;
8584 	orig_ipif = ipif;
8585 
8586 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8587 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8588 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8589 
8590 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8591 	for (; (ill != NULL) && (ill != ill_head);
8592 	    ill = ill->ill_usesrc_grp_next) {
8593 
8594 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8595 			break;
8596 
8597 		ipif = ill->ill_ipif;
8598 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8599 		if (ipif->ipif_isv6) {
8600 			sin6 = (sin6_t *)&lifr->lifr_addr;
8601 			*sin6 = sin6_null;
8602 			sin6->sin6_family = AF_INET6;
8603 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8604 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8605 			    &ipif->ipif_v6net_mask);
8606 		} else {
8607 			sin = (sin_t *)&lifr->lifr_addr;
8608 			*sin = sin_null;
8609 			sin->sin_family = AF_INET;
8610 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8611 			lifr->lifr_addrlen = ip_mask_to_plen(
8612 			    ipif->ipif_net_mask);
8613 		}
8614 		lifr++;
8615 	}
8616 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8617 	rw_exit(&ipst->ips_ill_g_lock);
8618 	ipif_refrele(orig_ipif);
8619 	mp1->b_wptr = (uchar_t *)lifr;
8620 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8621 
8622 	return (0);
8623 }
8624 
8625 /* ARGSUSED */
8626 int
8627 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8628     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8629 {
8630 	mblk_t *mp1;
8631 	int	list;
8632 	ill_t	*ill;
8633 	ipif_t	*ipif;
8634 	int	flags;
8635 	int	numlifs = 0;
8636 	size_t	lifc_bufsize;
8637 	struct	lifreq *lifr;
8638 	sa_family_t	family;
8639 	struct	sockaddr_in	*sin;
8640 	struct	sockaddr_in6	*sin6;
8641 	ill_walk_context_t	ctx;
8642 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8643 	int32_t	lifclen;
8644 	zoneid_t zoneid;
8645 	STRUCT_HANDLE(lifconf, lifc);
8646 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8647 
8648 	ip1dbg(("ip_sioctl_get_lifconf"));
8649 
8650 	ASSERT(q->q_next == NULL);
8651 
8652 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8653 
8654 	/* Existence verified in ip_wput_nondata */
8655 	mp1 = mp->b_cont->b_cont;
8656 
8657 	/*
8658 	 * An extended version of SIOCGIFCONF that takes an
8659 	 * additional address family and flags field.
8660 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8661 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8662 	 * interfaces are omitted.
8663 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8664 	 * unless LIFC_TEMPORARY is specified.
8665 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8666 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8667 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8668 	 * has priority over LIFC_NOXMIT.
8669 	 */
8670 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8671 
8672 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8673 		return (EINVAL);
8674 
8675 	/*
8676 	 * Must be (better be!) continuation of a TRANSPARENT
8677 	 * IOCTL.  We just copied in the lifconf structure.
8678 	 */
8679 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8680 
8681 	family = STRUCT_FGET(lifc, lifc_family);
8682 	flags = STRUCT_FGET(lifc, lifc_flags);
8683 
8684 	switch (family) {
8685 	case AF_UNSPEC:
8686 		/*
8687 		 * walk all ILL's.
8688 		 */
8689 		list = MAX_G_HEADS;
8690 		break;
8691 	case AF_INET:
8692 		/*
8693 		 * walk only IPV4 ILL's.
8694 		 */
8695 		list = IP_V4_G_HEAD;
8696 		break;
8697 	case AF_INET6:
8698 		/*
8699 		 * walk only IPV6 ILL's.
8700 		 */
8701 		list = IP_V6_G_HEAD;
8702 		break;
8703 	default:
8704 		return (EAFNOSUPPORT);
8705 	}
8706 
8707 	/*
8708 	 * Allocate a buffer to hold requested information.
8709 	 *
8710 	 * If lifc_len is larger than what is needed, we only
8711 	 * allocate what we will use.
8712 	 *
8713 	 * If lifc_len is smaller than what is needed, return
8714 	 * EINVAL.
8715 	 */
8716 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8717 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8718 	lifclen = STRUCT_FGET(lifc, lifc_len);
8719 	if (lifc_bufsize > lifclen) {
8720 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8721 			return (EINVAL);
8722 		else
8723 			lifc_bufsize = lifclen;
8724 	}
8725 
8726 	mp1 = mi_copyout_alloc(q, mp,
8727 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8728 	if (mp1 == NULL)
8729 		return (ENOMEM);
8730 
8731 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8732 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8733 
8734 	lifr = (struct lifreq *)mp1->b_rptr;
8735 
8736 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8737 	ill = ill_first(list, list, &ctx, ipst);
8738 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8739 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8740 			continue;
8741 
8742 		for (ipif = ill->ill_ipif; ipif != NULL;
8743 		    ipif = ipif->ipif_next) {
8744 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8745 			    !(flags & LIFC_NOXMIT))
8746 				continue;
8747 
8748 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8749 			    !(flags & LIFC_TEMPORARY))
8750 				continue;
8751 
8752 			if (((ipif->ipif_flags &
8753 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8754 			    IPIF_DEPRECATED)) ||
8755 			    IS_LOOPBACK(ill) ||
8756 			    !(ipif->ipif_flags & IPIF_UP)) &&
8757 			    (flags & LIFC_EXTERNAL_SOURCE))
8758 				continue;
8759 
8760 			if (zoneid != ipif->ipif_zoneid &&
8761 			    ipif->ipif_zoneid != ALL_ZONES &&
8762 			    (zoneid != GLOBAL_ZONEID ||
8763 			    !(flags & LIFC_ALLZONES)))
8764 				continue;
8765 
8766 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8767 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8768 					rw_exit(&ipst->ips_ill_g_lock);
8769 					return (EINVAL);
8770 				} else {
8771 					goto lif_copydone;
8772 				}
8773 			}
8774 
8775 			ipif_get_name(ipif, lifr->lifr_name,
8776 			    sizeof (lifr->lifr_name));
8777 			lifr->lifr_type = ill->ill_type;
8778 			if (ipif->ipif_isv6) {
8779 				sin6 = (sin6_t *)&lifr->lifr_addr;
8780 				*sin6 = sin6_null;
8781 				sin6->sin6_family = AF_INET6;
8782 				sin6->sin6_addr =
8783 				    ipif->ipif_v6lcl_addr;
8784 				lifr->lifr_addrlen =
8785 				    ip_mask_to_plen_v6(
8786 				    &ipif->ipif_v6net_mask);
8787 			} else {
8788 				sin = (sin_t *)&lifr->lifr_addr;
8789 				*sin = sin_null;
8790 				sin->sin_family = AF_INET;
8791 				sin->sin_addr.s_addr =
8792 				    ipif->ipif_lcl_addr;
8793 				lifr->lifr_addrlen =
8794 				    ip_mask_to_plen(
8795 				    ipif->ipif_net_mask);
8796 			}
8797 			lifr++;
8798 		}
8799 	}
8800 lif_copydone:
8801 	rw_exit(&ipst->ips_ill_g_lock);
8802 
8803 	mp1->b_wptr = (uchar_t *)lifr;
8804 	if (STRUCT_BUF(lifc) != NULL) {
8805 		STRUCT_FSET(lifc, lifc_len,
8806 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8807 	}
8808 	return (0);
8809 }
8810 
8811 static void
8812 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8813 {
8814 	ip6_asp_t *table;
8815 	size_t table_size;
8816 	mblk_t *data_mp;
8817 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8818 	ip_stack_t	*ipst;
8819 
8820 	if (q->q_next == NULL)
8821 		ipst = CONNQ_TO_IPST(q);
8822 	else
8823 		ipst = ILLQ_TO_IPST(q);
8824 
8825 	/* These two ioctls are I_STR only */
8826 	if (iocp->ioc_count == TRANSPARENT) {
8827 		miocnak(q, mp, 0, EINVAL);
8828 		return;
8829 	}
8830 
8831 	data_mp = mp->b_cont;
8832 	if (data_mp == NULL) {
8833 		/* The user passed us a NULL argument */
8834 		table = NULL;
8835 		table_size = iocp->ioc_count;
8836 	} else {
8837 		/*
8838 		 * The user provided a table.  The stream head
8839 		 * may have copied in the user data in chunks,
8840 		 * so make sure everything is pulled up
8841 		 * properly.
8842 		 */
8843 		if (MBLKL(data_mp) < iocp->ioc_count) {
8844 			mblk_t *new_data_mp;
8845 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8846 			    NULL) {
8847 				miocnak(q, mp, 0, ENOMEM);
8848 				return;
8849 			}
8850 			freemsg(data_mp);
8851 			data_mp = new_data_mp;
8852 			mp->b_cont = data_mp;
8853 		}
8854 		table = (ip6_asp_t *)data_mp->b_rptr;
8855 		table_size = iocp->ioc_count;
8856 	}
8857 
8858 	switch (iocp->ioc_cmd) {
8859 	case SIOCGIP6ADDRPOLICY:
8860 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8861 		if (iocp->ioc_rval == -1)
8862 			iocp->ioc_error = EINVAL;
8863 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8864 		else if (table != NULL &&
8865 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8866 			ip6_asp_t *src = table;
8867 			ip6_asp32_t *dst = (void *)table;
8868 			int count = table_size / sizeof (ip6_asp_t);
8869 			int i;
8870 
8871 			/*
8872 			 * We need to do an in-place shrink of the array
8873 			 * to match the alignment attributes of the
8874 			 * 32-bit ABI looking at it.
8875 			 */
8876 			/* LINTED: logical expression always true: op "||" */
8877 			ASSERT(sizeof (*src) > sizeof (*dst));
8878 			for (i = 1; i < count; i++)
8879 				bcopy(src + i, dst + i, sizeof (*dst));
8880 		}
8881 #endif
8882 		break;
8883 
8884 	case SIOCSIP6ADDRPOLICY:
8885 		ASSERT(mp->b_prev == NULL);
8886 		mp->b_prev = (void *)q;
8887 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8888 		/*
8889 		 * We pass in the datamodel here so that the ip6_asp_replace()
8890 		 * routine can handle converting from 32-bit to native formats
8891 		 * where necessary.
8892 		 *
8893 		 * A better way to handle this might be to convert the inbound
8894 		 * data structure here, and hang it off a new 'mp'; thus the
8895 		 * ip6_asp_replace() logic would always be dealing with native
8896 		 * format data structures..
8897 		 *
8898 		 * (An even simpler way to handle these ioctls is to just
8899 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8900 		 * and just recompile everything that depends on it.)
8901 		 */
8902 #endif
8903 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
8904 		    iocp->ioc_flag & IOC_MODELS);
8905 		return;
8906 	}
8907 
8908 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8909 	qreply(q, mp);
8910 }
8911 
8912 static void
8913 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8914 {
8915 	mblk_t 		*data_mp;
8916 	struct dstinforeq	*dir;
8917 	uint8_t		*end, *cur;
8918 	in6_addr_t	*daddr, *saddr;
8919 	ipaddr_t	v4daddr;
8920 	ire_t		*ire;
8921 	char		*slabel, *dlabel;
8922 	boolean_t	isipv4;
8923 	int		match_ire;
8924 	ill_t		*dst_ill;
8925 	ipif_t		*src_ipif, *ire_ipif;
8926 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8927 	zoneid_t	zoneid;
8928 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
8929 
8930 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8931 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8932 
8933 	/*
8934 	 * This ioctl is I_STR only, and must have a
8935 	 * data mblk following the M_IOCTL mblk.
8936 	 */
8937 	data_mp = mp->b_cont;
8938 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8939 		miocnak(q, mp, 0, EINVAL);
8940 		return;
8941 	}
8942 
8943 	if (MBLKL(data_mp) < iocp->ioc_count) {
8944 		mblk_t *new_data_mp;
8945 
8946 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8947 			miocnak(q, mp, 0, ENOMEM);
8948 			return;
8949 		}
8950 		freemsg(data_mp);
8951 		data_mp = new_data_mp;
8952 		mp->b_cont = data_mp;
8953 	}
8954 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8955 
8956 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8957 	    end - cur >= sizeof (struct dstinforeq);
8958 	    cur += sizeof (struct dstinforeq)) {
8959 		dir = (struct dstinforeq *)cur;
8960 		daddr = &dir->dir_daddr;
8961 		saddr = &dir->dir_saddr;
8962 
8963 		/*
8964 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8965 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8966 		 * and ipif_select_source[_v6]() do not.
8967 		 */
8968 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8969 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
8970 
8971 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8972 		if (isipv4) {
8973 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8974 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8975 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
8976 		} else {
8977 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8978 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
8979 		}
8980 		if (ire == NULL) {
8981 			dir->dir_dreachable = 0;
8982 
8983 			/* move on to next dst addr */
8984 			continue;
8985 		}
8986 		dir->dir_dreachable = 1;
8987 
8988 		ire_ipif = ire->ire_ipif;
8989 		if (ire_ipif == NULL)
8990 			goto next_dst;
8991 
8992 		/*
8993 		 * We expect to get back an interface ire or a
8994 		 * gateway ire cache entry.  For both types, the
8995 		 * output interface is ire_ipif->ipif_ill.
8996 		 */
8997 		dst_ill = ire_ipif->ipif_ill;
8998 		dir->dir_dmactype = dst_ill->ill_mactype;
8999 
9000 		if (isipv4) {
9001 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9002 		} else {
9003 			src_ipif = ipif_select_source_v6(dst_ill,
9004 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9005 		}
9006 		if (src_ipif == NULL)
9007 			goto next_dst;
9008 
9009 		*saddr = src_ipif->ipif_v6lcl_addr;
9010 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9011 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9012 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9013 		dir->dir_sdeprecated =
9014 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9015 		ipif_refrele(src_ipif);
9016 next_dst:
9017 		ire_refrele(ire);
9018 	}
9019 	miocack(q, mp, iocp->ioc_count, 0);
9020 }
9021 
9022 /*
9023  * Check if this is an address assigned to this machine.
9024  * Skips interfaces that are down by using ire checks.
9025  * Translates mapped addresses to v4 addresses and then
9026  * treats them as such, returning true if the v4 address
9027  * associated with this mapped address is configured.
9028  * Note: Applications will have to be careful what they do
9029  * with the response; use of mapped addresses limits
9030  * what can be done with the socket, especially with
9031  * respect to socket options and ioctls - neither IPv4
9032  * options nor IPv6 sticky options/ancillary data options
9033  * may be used.
9034  */
9035 /* ARGSUSED */
9036 int
9037 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9038     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9039 {
9040 	struct sioc_addrreq *sia;
9041 	sin_t *sin;
9042 	ire_t *ire;
9043 	mblk_t *mp1;
9044 	zoneid_t zoneid;
9045 	ip_stack_t	*ipst;
9046 
9047 	ip1dbg(("ip_sioctl_tmyaddr"));
9048 
9049 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9050 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9051 	ipst = CONNQ_TO_IPST(q);
9052 
9053 	/* Existence verified in ip_wput_nondata */
9054 	mp1 = mp->b_cont->b_cont;
9055 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9056 	sin = (sin_t *)&sia->sa_addr;
9057 	switch (sin->sin_family) {
9058 	case AF_INET6: {
9059 		sin6_t *sin6 = (sin6_t *)sin;
9060 
9061 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9062 			ipaddr_t v4_addr;
9063 
9064 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9065 			    v4_addr);
9066 			ire = ire_ctable_lookup(v4_addr, 0,
9067 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9068 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9069 		} else {
9070 			in6_addr_t v6addr;
9071 
9072 			v6addr = sin6->sin6_addr;
9073 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9074 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9075 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9076 		}
9077 		break;
9078 	}
9079 	case AF_INET: {
9080 		ipaddr_t v4addr;
9081 
9082 		v4addr = sin->sin_addr.s_addr;
9083 		ire = ire_ctable_lookup(v4addr, 0,
9084 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9085 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9086 		break;
9087 	}
9088 	default:
9089 		return (EAFNOSUPPORT);
9090 	}
9091 	if (ire != NULL) {
9092 		sia->sa_res = 1;
9093 		ire_refrele(ire);
9094 	} else {
9095 		sia->sa_res = 0;
9096 	}
9097 	return (0);
9098 }
9099 
9100 /*
9101  * Check if this is an address assigned on-link i.e. neighbor,
9102  * and makes sure it's reachable from the current zone.
9103  * Returns true for my addresses as well.
9104  * Translates mapped addresses to v4 addresses and then
9105  * treats them as such, returning true if the v4 address
9106  * associated with this mapped address is configured.
9107  * Note: Applications will have to be careful what they do
9108  * with the response; use of mapped addresses limits
9109  * what can be done with the socket, especially with
9110  * respect to socket options and ioctls - neither IPv4
9111  * options nor IPv6 sticky options/ancillary data options
9112  * may be used.
9113  */
9114 /* ARGSUSED */
9115 int
9116 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9117     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9118 {
9119 	struct sioc_addrreq *sia;
9120 	sin_t *sin;
9121 	mblk_t	*mp1;
9122 	ire_t *ire = NULL;
9123 	zoneid_t zoneid;
9124 	ip_stack_t	*ipst;
9125 
9126 	ip1dbg(("ip_sioctl_tonlink"));
9127 
9128 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9129 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9130 	ipst = CONNQ_TO_IPST(q);
9131 
9132 	/* Existence verified in ip_wput_nondata */
9133 	mp1 = mp->b_cont->b_cont;
9134 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9135 	sin = (sin_t *)&sia->sa_addr;
9136 
9137 	/*
9138 	 * Match addresses with a zero gateway field to avoid
9139 	 * routes going through a router.
9140 	 * Exclude broadcast and multicast addresses.
9141 	 */
9142 	switch (sin->sin_family) {
9143 	case AF_INET6: {
9144 		sin6_t *sin6 = (sin6_t *)sin;
9145 
9146 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9147 			ipaddr_t v4_addr;
9148 
9149 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9150 			    v4_addr);
9151 			if (!CLASSD(v4_addr)) {
9152 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9153 				    NULL, NULL, zoneid, NULL,
9154 				    MATCH_IRE_GW, ipst);
9155 			}
9156 		} else {
9157 			in6_addr_t v6addr;
9158 			in6_addr_t v6gw;
9159 
9160 			v6addr = sin6->sin6_addr;
9161 			v6gw = ipv6_all_zeros;
9162 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9163 				ire = ire_route_lookup_v6(&v6addr, 0,
9164 				    &v6gw, 0, NULL, NULL, zoneid,
9165 				    NULL, MATCH_IRE_GW, ipst);
9166 			}
9167 		}
9168 		break;
9169 	}
9170 	case AF_INET: {
9171 		ipaddr_t v4addr;
9172 
9173 		v4addr = sin->sin_addr.s_addr;
9174 		if (!CLASSD(v4addr)) {
9175 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9176 			    NULL, NULL, zoneid, NULL,
9177 			    MATCH_IRE_GW, ipst);
9178 		}
9179 		break;
9180 	}
9181 	default:
9182 		return (EAFNOSUPPORT);
9183 	}
9184 	sia->sa_res = 0;
9185 	if (ire != NULL) {
9186 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9187 		    IRE_LOCAL|IRE_LOOPBACK)) {
9188 			sia->sa_res = 1;
9189 		}
9190 		ire_refrele(ire);
9191 	}
9192 	return (0);
9193 }
9194 
9195 /*
9196  * TBD: implement when kernel maintaines a list of site prefixes.
9197  */
9198 /* ARGSUSED */
9199 int
9200 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9201     ip_ioctl_cmd_t *ipip, void *ifreq)
9202 {
9203 	return (ENXIO);
9204 }
9205 
9206 /* ARGSUSED */
9207 int
9208 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9209     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9210 {
9211 	ill_t		*ill;
9212 	mblk_t		*mp1;
9213 	conn_t		*connp;
9214 	boolean_t	success;
9215 
9216 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9217 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9218 	/* ioctl comes down on an conn */
9219 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9220 	connp = Q_TO_CONN(q);
9221 
9222 	mp->b_datap->db_type = M_IOCTL;
9223 
9224 	/*
9225 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9226 	 * The original mp contains contaminated b_next values due to 'mi',
9227 	 * which is needed to do the mi_copy_done. Unfortunately if we
9228 	 * send down the original mblk itself and if we are popped due to an
9229 	 * an unplumb before the response comes back from tunnel,
9230 	 * the streamhead (which does a freemsg) will see this contaminated
9231 	 * message and the assertion in freemsg about non-null b_next/b_prev
9232 	 * will panic a DEBUG kernel.
9233 	 */
9234 	mp1 = copymsg(mp);
9235 	if (mp1 == NULL)
9236 		return (ENOMEM);
9237 
9238 	ill = ipif->ipif_ill;
9239 	mutex_enter(&connp->conn_lock);
9240 	mutex_enter(&ill->ill_lock);
9241 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9242 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9243 		    mp, 0);
9244 	} else {
9245 		success = ill_pending_mp_add(ill, connp, mp);
9246 	}
9247 	mutex_exit(&ill->ill_lock);
9248 	mutex_exit(&connp->conn_lock);
9249 
9250 	if (success) {
9251 		ip1dbg(("sending down tunparam request "));
9252 		putnext(ill->ill_wq, mp1);
9253 		return (EINPROGRESS);
9254 	} else {
9255 		/* The conn has started closing */
9256 		freemsg(mp1);
9257 		return (EINTR);
9258 	}
9259 }
9260 
9261 /*
9262  * ARP IOCTLs.
9263  * How does IP get in the business of fronting ARP configuration/queries?
9264  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9265  * are by tradition passed in through a datagram socket.  That lands in IP.
9266  * As it happens, this is just as well since the interface is quite crude in
9267  * that it passes in no information about protocol or hardware types, or
9268  * interface association.  After making the protocol assumption, IP is in
9269  * the position to look up the name of the ILL, which ARP will need, and
9270  * format a request that can be handled by ARP.  The request is passed up
9271  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9272  * back a response.  ARP supports its own set of more general IOCTLs, in
9273  * case anyone is interested.
9274  */
9275 /* ARGSUSED */
9276 int
9277 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9278     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9279 {
9280 	mblk_t *mp1;
9281 	mblk_t *mp2;
9282 	mblk_t *pending_mp;
9283 	ipaddr_t ipaddr;
9284 	area_t *area;
9285 	struct iocblk *iocp;
9286 	conn_t *connp;
9287 	struct arpreq *ar;
9288 	struct xarpreq *xar;
9289 	int flags, alength;
9290 	uchar_t *lladdr;
9291 	ire_t *ire;
9292 	ip_stack_t *ipst;
9293 	ill_t *ill = ipif->ipif_ill;
9294 	ill_t *proxy_ill = NULL;
9295 	ipmp_arpent_t *entp = NULL;
9296 	boolean_t if_arp_ioctl = B_FALSE;
9297 	boolean_t proxyarp = B_FALSE;
9298 
9299 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9300 	connp = Q_TO_CONN(q);
9301 	ipst = connp->conn_netstack->netstack_ip;
9302 
9303 	if (ipip->ipi_cmd_type == XARP_CMD) {
9304 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9305 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9306 		ar = NULL;
9307 
9308 		flags = xar->xarp_flags;
9309 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9310 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9311 		/*
9312 		 * Validate against user's link layer address length
9313 		 * input and name and addr length limits.
9314 		 */
9315 		alength = ill->ill_phys_addr_length;
9316 		if (ipip->ipi_cmd == SIOCSXARP) {
9317 			if (alength != xar->xarp_ha.sdl_alen ||
9318 			    (alength + xar->xarp_ha.sdl_nlen >
9319 			    sizeof (xar->xarp_ha.sdl_data)))
9320 				return (EINVAL);
9321 		}
9322 	} else {
9323 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9324 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9325 		xar = NULL;
9326 
9327 		flags = ar->arp_flags;
9328 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9329 		/*
9330 		 * Theoretically, the sa_family could tell us what link
9331 		 * layer type this operation is trying to deal with. By
9332 		 * common usage AF_UNSPEC means ethernet. We'll assume
9333 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9334 		 * for now. Our new SIOC*XARP ioctls can be used more
9335 		 * generally.
9336 		 *
9337 		 * If the underlying media happens to have a non 6 byte
9338 		 * address, arp module will fail set/get, but the del
9339 		 * operation will succeed.
9340 		 */
9341 		alength = 6;
9342 		if ((ipip->ipi_cmd != SIOCDARP) &&
9343 		    (alength != ill->ill_phys_addr_length)) {
9344 			return (EINVAL);
9345 		}
9346 	}
9347 
9348 	ipaddr = sin->sin_addr.s_addr;
9349 
9350 	/*
9351 	 * IPMP ARP special handling:
9352 	 *
9353 	 * 1. Since ARP mappings must appear consistent across the group,
9354 	 *    prohibit changing ARP mappings on the underlying interfaces.
9355 	 *
9356 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9357 	 *    IP itself, prohibit changing them.
9358 	 *
9359 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9360 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9361 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9362 	 */
9363 	if (IS_UNDER_IPMP(ill)) {
9364 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9365 			return (EPERM);
9366 	}
9367 	if (IS_IPMP(ill)) {
9368 		ipmp_illgrp_t *illg = ill->ill_grp;
9369 
9370 		switch (ipip->ipi_cmd) {
9371 		case SIOCSARP:
9372 		case SIOCSXARP:
9373 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9374 			if (proxy_ill != NULL) {
9375 				proxyarp = B_TRUE;
9376 				if (!ipmp_ill_is_active(proxy_ill))
9377 					proxy_ill = ipmp_illgrp_next_ill(illg);
9378 				if (proxy_ill != NULL)
9379 					lladdr = proxy_ill->ill_phys_addr;
9380 			}
9381 			/* FALLTHRU */
9382 		case SIOCDARP:
9383 		case SIOCDXARP:
9384 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9385 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9386 			if (ire != NULL) {
9387 				ire_refrele(ire);
9388 				return (EPERM);
9389 			}
9390 		}
9391 	}
9392 
9393 	/*
9394 	 * We are going to pass up to ARP a packet chain that looks
9395 	 * like:
9396 	 *
9397 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9398 	 *
9399 	 * Get a copy of the original IOCTL mblk to head the chain,
9400 	 * to be sent up (in mp1). Also get another copy to store
9401 	 * in the ill_pending_mp list, for matching the response
9402 	 * when it comes back from ARP.
9403 	 */
9404 	mp1 = copyb(mp);
9405 	pending_mp = copymsg(mp);
9406 	if (mp1 == NULL || pending_mp == NULL) {
9407 		if (mp1 != NULL)
9408 			freeb(mp1);
9409 		if (pending_mp != NULL)
9410 			inet_freemsg(pending_mp);
9411 		return (ENOMEM);
9412 	}
9413 
9414 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9415 	    (caddr_t)&ipaddr);
9416 	if (mp2 == NULL) {
9417 		freeb(mp1);
9418 		inet_freemsg(pending_mp);
9419 		return (ENOMEM);
9420 	}
9421 	/* Put together the chain. */
9422 	mp1->b_cont = mp2;
9423 	mp1->b_datap->db_type = M_IOCTL;
9424 	mp2->b_cont = mp;
9425 	mp2->b_datap->db_type = M_DATA;
9426 
9427 	iocp = (struct iocblk *)mp1->b_rptr;
9428 
9429 	/*
9430 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9431 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9432 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9433 	 * ioc_count field; set ioc_count to be correct.
9434 	 */
9435 	iocp->ioc_count = MBLKL(mp1->b_cont);
9436 
9437 	/*
9438 	 * Set the proper command in the ARP message.
9439 	 * Convert the SIOC{G|S|D}ARP calls into our
9440 	 * AR_ENTRY_xxx calls.
9441 	 */
9442 	area = (area_t *)mp2->b_rptr;
9443 	switch (iocp->ioc_cmd) {
9444 	case SIOCDARP:
9445 	case SIOCDXARP:
9446 		/*
9447 		 * We defer deleting the corresponding IRE until
9448 		 * we return from arp.
9449 		 */
9450 		area->area_cmd = AR_ENTRY_DELETE;
9451 		area->area_proto_mask_offset = 0;
9452 		break;
9453 	case SIOCGARP:
9454 	case SIOCGXARP:
9455 		area->area_cmd = AR_ENTRY_SQUERY;
9456 		area->area_proto_mask_offset = 0;
9457 		break;
9458 	case SIOCSARP:
9459 	case SIOCSXARP:
9460 		/*
9461 		 * Delete the corresponding ire to make sure IP will
9462 		 * pick up any change from arp.
9463 		 */
9464 		if (!if_arp_ioctl) {
9465 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9466 		} else {
9467 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9468 			if (ipif != NULL) {
9469 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9470 				    ipst);
9471 				ipif_refrele(ipif);
9472 			}
9473 		}
9474 		break;
9475 	}
9476 	iocp->ioc_cmd = area->area_cmd;
9477 
9478 	/*
9479 	 * Fill in the rest of the ARP operation fields.
9480 	 */
9481 	area->area_hw_addr_length = alength;
9482 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9483 
9484 	/* Translate the flags. */
9485 	if (flags & ATF_PERM)
9486 		area->area_flags |= ACE_F_PERMANENT;
9487 	if (flags & ATF_PUBL)
9488 		area->area_flags |= ACE_F_PUBLISH;
9489 	if (flags & ATF_AUTHORITY)
9490 		area->area_flags |= ACE_F_AUTHORITY;
9491 
9492 	/*
9493 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9494 	 * so that IP can update ARP as the active ills in the group change.
9495 	 */
9496 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9497 	    (area->area_flags & ACE_F_PERMANENT)) {
9498 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9499 
9500 		/*
9501 		 * The second part of the conditional below handles a corner
9502 		 * case: if this is proxy ARP and the IPMP group has no active
9503 		 * interfaces, we can't send the request to ARP now since it
9504 		 * won't be able to build an ACE.  So we return success and
9505 		 * notify ARP about the proxy ARP entry once an interface
9506 		 * becomes active.
9507 		 */
9508 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9509 			mp2->b_cont = NULL;
9510 			inet_freemsg(mp1);
9511 			inet_freemsg(pending_mp);
9512 			return (entp == NULL ? ENOMEM : 0);
9513 		}
9514 	}
9515 
9516 	/*
9517 	 * Before sending 'mp' to ARP, we have to clear the b_next
9518 	 * and b_prev. Otherwise if STREAMS encounters such a message
9519 	 * in freemsg(), (because ARP can close any time) it can cause
9520 	 * a panic. But mi code needs the b_next and b_prev values of
9521 	 * mp->b_cont, to complete the ioctl. So we store it here
9522 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9523 	 * when the response comes down from ARP.
9524 	 */
9525 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9526 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9527 	mp->b_cont->b_next = NULL;
9528 	mp->b_cont->b_prev = NULL;
9529 
9530 	mutex_enter(&connp->conn_lock);
9531 	mutex_enter(&ill->ill_lock);
9532 	/* conn has not yet started closing, hence this can't fail */
9533 	if (ipip->ipi_flags & IPI_WR) {
9534 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9535 		    pending_mp, 0) != 0);
9536 	} else {
9537 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9538 	}
9539 	mutex_exit(&ill->ill_lock);
9540 	mutex_exit(&connp->conn_lock);
9541 
9542 	/*
9543 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9544 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9545 	 */
9546 	putnext(ill->ill_rq, mp1);
9547 
9548 	/*
9549 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9550 	 */
9551 	if (entp != NULL)
9552 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9553 
9554 	return (EINPROGRESS);
9555 }
9556 
9557 /*
9558  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9559  * the associated sin and refhold and return the associated ipif via `ci'.
9560  */
9561 int
9562 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9563     cmd_info_t *ci, ipsq_func_t func)
9564 {
9565 	mblk_t	*mp1;
9566 	int	err;
9567 	sin_t	*sin;
9568 	conn_t	*connp;
9569 	ipif_t	*ipif;
9570 	ire_t	*ire = NULL;
9571 	ill_t	*ill = NULL;
9572 	boolean_t exists;
9573 	ip_stack_t *ipst;
9574 	struct arpreq *ar;
9575 	struct xarpreq *xar;
9576 	struct sockaddr_dl *sdl;
9577 
9578 	/* ioctl comes down on a conn */
9579 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9580 	connp = Q_TO_CONN(q);
9581 	if (connp->conn_af_isv6)
9582 		return (ENXIO);
9583 
9584 	ipst = connp->conn_netstack->netstack_ip;
9585 
9586 	/* Verified in ip_wput_nondata */
9587 	mp1 = mp->b_cont->b_cont;
9588 
9589 	if (ipip->ipi_cmd_type == XARP_CMD) {
9590 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9591 		xar = (struct xarpreq *)mp1->b_rptr;
9592 		sin = (sin_t *)&xar->xarp_pa;
9593 		sdl = &xar->xarp_ha;
9594 
9595 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9596 			return (ENXIO);
9597 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9598 			return (EINVAL);
9599 	} else {
9600 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9601 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9602 		ar = (struct arpreq *)mp1->b_rptr;
9603 		sin = (sin_t *)&ar->arp_pa;
9604 	}
9605 
9606 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9607 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9608 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9609 		    mp, func, &err, ipst);
9610 		if (ipif == NULL)
9611 			return (err);
9612 		if (ipif->ipif_id != 0) {
9613 			ipif_refrele(ipif);
9614 			return (ENXIO);
9615 		}
9616 	} else {
9617 		/*
9618 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9619 		 * of 0: use the IP address to find the ipif.  If the IP
9620 		 * address is an IPMP test address, ire_ftable_lookup() will
9621 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9622 		 */
9623 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9624 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9625 		if (ipif == NULL) {
9626 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9627 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9628 			    MATCH_IRE_TYPE, ipst);
9629 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9630 				if (ire != NULL)
9631 					ire_refrele(ire);
9632 				return (ENXIO);
9633 			}
9634 			ipif = ill->ill_ipif;
9635 			ipif_refhold(ipif);
9636 			ire_refrele(ire);
9637 		}
9638 	}
9639 
9640 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9641 		ipif_refrele(ipif);
9642 		return (ENXIO);
9643 	}
9644 
9645 	ci->ci_sin = sin;
9646 	ci->ci_ipif = ipif;
9647 	return (0);
9648 }
9649 
9650 /*
9651  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9652  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9653  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9654  * up and thus an ill can join that illgrp.
9655  *
9656  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9657  * open()/close() primarily because close() is not allowed to fail or block
9658  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9659  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9660  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9661  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9662  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9663  * state if I_UNLINK didn't occur.
9664  *
9665  * Note that for each plumb/unplumb operation, we may end up here more than
9666  * once because of the way ifconfig works.  However, it's OK to link the same
9667  * illgrp more than once, or unlink an illgrp that's already unlinked.
9668  */
9669 static int
9670 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9671 {
9672 	int err;
9673 	ip_stack_t *ipst = ill->ill_ipst;
9674 
9675 	ASSERT(IS_IPMP(ill));
9676 	ASSERT(IAM_WRITER_ILL(ill));
9677 
9678 	switch (ioccmd) {
9679 	case I_LINK:
9680 		return (ENOTSUP);
9681 
9682 	case I_PLINK:
9683 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9684 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9685 		rw_exit(&ipst->ips_ipmp_lock);
9686 		break;
9687 
9688 	case I_PUNLINK:
9689 		/*
9690 		 * Require all UP ipifs be brought down prior to unlinking the
9691 		 * illgrp so any associated IREs (and other state) is torched.
9692 		 */
9693 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9694 			return (EBUSY);
9695 
9696 		/*
9697 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9698 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9699 		 * join this group.  Specifically: ills trying to join grab
9700 		 * ipmp_lock and bump a "pending join" counter checked by
9701 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9702 		 * joins can occur (since we have ipmp_lock).  Once we drop
9703 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9704 		 * find the illgrp (since we unlinked it) and will return
9705 		 * EAFNOSUPPORT.  This will then take them back through the
9706 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9707 		 * back through I_PLINK above.
9708 		 */
9709 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9710 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9711 		rw_exit(&ipst->ips_ipmp_lock);
9712 		return (err);
9713 	default:
9714 		break;
9715 	}
9716 	return (0);
9717 }
9718 
9719 /*
9720  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9721  * atomically set/clear the muxids. Also complete the ioctl by acking or
9722  * naking it.  Note that the code is structured such that the link type,
9723  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9724  * its clones use the persistent link, while pppd(1M) and perhaps many
9725  * other daemons may use non-persistent link.  When combined with some
9726  * ill_t states, linking and unlinking lower streams may be used as
9727  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9728  */
9729 /* ARGSUSED */
9730 void
9731 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9732 {
9733 	mblk_t		*mp1, *mp2;
9734 	struct linkblk	*li;
9735 	struct ipmx_s	*ipmxp;
9736 	ill_t		*ill;
9737 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9738 	int		err = 0;
9739 	boolean_t	entered_ipsq = B_FALSE;
9740 	boolean_t	islink;
9741 	ip_stack_t	*ipst;
9742 
9743 	if (CONN_Q(q))
9744 		ipst = CONNQ_TO_IPST(q);
9745 	else
9746 		ipst = ILLQ_TO_IPST(q);
9747 
9748 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9749 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9750 
9751 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9752 
9753 	mp1 = mp->b_cont;	/* This is the linkblk info */
9754 	li = (struct linkblk *)mp1->b_rptr;
9755 
9756 	/*
9757 	 * ARP has added this special mblk, and the utility is asking us
9758 	 * to perform consistency checks, and also atomically set the
9759 	 * muxid. Ifconfig is an example.  It achieves this by using
9760 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9761 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9762 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9763 	 * and other comments in this routine for more details.
9764 	 */
9765 	mp2 = mp1->b_cont;	/* This is added by ARP */
9766 
9767 	/*
9768 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9769 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9770 	 * get the special mblk above.  For backward compatibility, we
9771 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9772 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9773 	 * not atomic, and can leave the streams unplumbable if the utility
9774 	 * is interrupted before it does the SIOCSLIFMUXID.
9775 	 */
9776 	if (mp2 == NULL) {
9777 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9778 		if (err == EINPROGRESS)
9779 			return;
9780 		goto done;
9781 	}
9782 
9783 	/*
9784 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9785 	 * ARP has appended this last mblk to tell us whether the lower stream
9786 	 * is an arp-dev stream or an IP module stream.
9787 	 */
9788 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9789 	if (ipmxp->ipmx_arpdev_stream) {
9790 		/*
9791 		 * The lower stream is the arp-dev stream.
9792 		 */
9793 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9794 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9795 		if (ill == NULL) {
9796 			if (err == EINPROGRESS)
9797 				return;
9798 			err = EINVAL;
9799 			goto done;
9800 		}
9801 
9802 		if (ipsq == NULL) {
9803 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9804 			    NEW_OP, B_FALSE);
9805 			if (ipsq == NULL) {
9806 				ill_refrele(ill);
9807 				return;
9808 			}
9809 			entered_ipsq = B_TRUE;
9810 		}
9811 		ASSERT(IAM_WRITER_ILL(ill));
9812 		ill_refrele(ill);
9813 
9814 		/*
9815 		 * To ensure consistency between IP and ARP, the following
9816 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9817 		 * This is because the muxid's are stored in the IP stream on
9818 		 * the ill.
9819 		 *
9820 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9821 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9822 		 * not yet plinked, and it also checks that the corresponding
9823 		 * IP stream is already plinked.
9824 		 *
9825 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9826 		 * punlinking the IP stream. IP does not allow punlink of the
9827 		 * IP stream unless the arp stream has been punlinked.
9828 		 */
9829 		if ((islink &&
9830 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9831 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9832 			err = EINVAL;
9833 			goto done;
9834 		}
9835 
9836 		if (IS_IPMP(ill) &&
9837 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9838 			goto done;
9839 
9840 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9841 	} else {
9842 		/*
9843 		 * The lower stream is probably an IP module stream.  Do
9844 		 * consistency checking.
9845 		 */
9846 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9847 		if (err == EINPROGRESS)
9848 			return;
9849 	}
9850 done:
9851 	if (err == 0)
9852 		miocack(q, mp, 0, 0);
9853 	else
9854 		miocnak(q, mp, 0, err);
9855 
9856 	/* Conn was refheld in ip_sioctl_copyin_setup */
9857 	if (CONN_Q(q))
9858 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9859 	if (entered_ipsq)
9860 		ipsq_exit(ipsq);
9861 }
9862 
9863 /*
9864  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9865  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9866  * module stream).  If `doconsist' is set, then do the extended consistency
9867  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9868  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9869  * an error code on failure.
9870  */
9871 static int
9872 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9873     struct linkblk *li, boolean_t doconsist)
9874 {
9875 	int		err = 0;
9876 	ill_t  		*ill;
9877 	queue_t		*ipwq, *dwq;
9878 	const char	*name;
9879 	struct qinit	*qinfo;
9880 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9881 	boolean_t	entered_ipsq = B_FALSE;
9882 
9883 	/*
9884 	 * Walk the lower stream to verify it's the IP module stream.
9885 	 * The IP module is identified by its name, wput function,
9886 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9887 	 * (li->l_qbot) will not vanish until this ioctl completes.
9888 	 */
9889 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9890 		qinfo = ipwq->q_qinfo;
9891 		name = qinfo->qi_minfo->mi_idname;
9892 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9893 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9894 			break;
9895 		}
9896 	}
9897 
9898 	/*
9899 	 * If this isn't an IP module stream, bail.
9900 	 */
9901 	if (ipwq == NULL)
9902 		return (0);
9903 
9904 	ill = ipwq->q_ptr;
9905 	ASSERT(ill != NULL);
9906 
9907 	if (ipsq == NULL) {
9908 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9909 		    NEW_OP, B_FALSE);
9910 		if (ipsq == NULL)
9911 			return (EINPROGRESS);
9912 		entered_ipsq = B_TRUE;
9913 	}
9914 	ASSERT(IAM_WRITER_ILL(ill));
9915 
9916 	if (doconsist) {
9917 		/*
9918 		 * Consistency checking requires that I_{P}LINK occurs
9919 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9920 		 * occurs prior to clearing ill_arp_muxid.
9921 		 */
9922 		if ((islink && ill->ill_ip_muxid != 0) ||
9923 		    (!islink && ill->ill_arp_muxid != 0)) {
9924 			err = EINVAL;
9925 			goto done;
9926 		}
9927 	}
9928 
9929 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9930 		goto done;
9931 
9932 	/*
9933 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9934 	 * the read queue of the module immediately below IP in the ill.
9935 	 * These are used during the capability negotiation below.
9936 	 */
9937 	ill->ill_lmod_rq = NULL;
9938 	ill->ill_lmod_cnt = 0;
9939 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9940 		ill->ill_lmod_rq = RD(dwq);
9941 		for (; dwq != NULL; dwq = dwq->q_next)
9942 			ill->ill_lmod_cnt++;
9943 	}
9944 
9945 	if (doconsist)
9946 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9947 
9948 	/*
9949 	 * Mark the ipsq busy until the capability operations initiated below
9950 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
9951 	 * returns, but the capability operation may complete asynchronously
9952 	 * much later.
9953 	 */
9954 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
9955 	/*
9956 	 * If there's at least one up ipif on this ill, then we're bound to
9957 	 * the underlying driver via DLPI.  In that case, renegotiate
9958 	 * capabilities to account for any possible change in modules
9959 	 * interposed between IP and the driver.
9960 	 */
9961 	if (ill->ill_ipif_up_count > 0) {
9962 		if (islink)
9963 			ill_capability_probe(ill);
9964 		else
9965 			ill_capability_reset(ill, B_FALSE);
9966 	}
9967 	ipsq_current_finish(ipsq);
9968 done:
9969 	if (entered_ipsq)
9970 		ipsq_exit(ipsq);
9971 
9972 	return (err);
9973 }
9974 
9975 /*
9976  * Search the ioctl command in the ioctl tables and return a pointer
9977  * to the ioctl command information. The ioctl command tables are
9978  * static and fully populated at compile time.
9979  */
9980 ip_ioctl_cmd_t *
9981 ip_sioctl_lookup(int ioc_cmd)
9982 {
9983 	int index;
9984 	ip_ioctl_cmd_t *ipip;
9985 	ip_ioctl_cmd_t *ipip_end;
9986 
9987 	if (ioc_cmd == IPI_DONTCARE)
9988 		return (NULL);
9989 
9990 	/*
9991 	 * Do a 2 step search. First search the indexed table
9992 	 * based on the least significant byte of the ioctl cmd.
9993 	 * If we don't find a match, then search the misc table
9994 	 * serially.
9995 	 */
9996 	index = ioc_cmd & 0xFF;
9997 	if (index < ip_ndx_ioctl_count) {
9998 		ipip = &ip_ndx_ioctl_table[index];
9999 		if (ipip->ipi_cmd == ioc_cmd) {
10000 			/* Found a match in the ndx table */
10001 			return (ipip);
10002 		}
10003 	}
10004 
10005 	/* Search the misc table */
10006 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10007 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10008 		if (ipip->ipi_cmd == ioc_cmd)
10009 			/* Found a match in the misc table */
10010 			return (ipip);
10011 	}
10012 
10013 	return (NULL);
10014 }
10015 
10016 /*
10017  * Wrapper function for resuming deferred ioctl processing
10018  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10019  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10020  */
10021 /* ARGSUSED */
10022 void
10023 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10024     void *dummy_arg)
10025 {
10026 	ip_sioctl_copyin_setup(q, mp);
10027 }
10028 
10029 /*
10030  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10031  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10032  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10033  * We establish here the size of the block to be copied in.  mi_copyin
10034  * arranges for this to happen, an processing continues in ip_wput with
10035  * an M_IOCDATA message.
10036  */
10037 void
10038 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10039 {
10040 	int	copyin_size;
10041 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10042 	ip_ioctl_cmd_t *ipip;
10043 	cred_t *cr;
10044 	ip_stack_t	*ipst;
10045 
10046 	if (CONN_Q(q))
10047 		ipst = CONNQ_TO_IPST(q);
10048 	else
10049 		ipst = ILLQ_TO_IPST(q);
10050 
10051 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10052 	if (ipip == NULL) {
10053 		/*
10054 		 * The ioctl is not one we understand or own.
10055 		 * Pass it along to be processed down stream,
10056 		 * if this is a module instance of IP, else nak
10057 		 * the ioctl.
10058 		 */
10059 		if (q->q_next == NULL) {
10060 			goto nak;
10061 		} else {
10062 			putnext(q, mp);
10063 			return;
10064 		}
10065 	}
10066 
10067 	/*
10068 	 * If this is deferred, then we will do all the checks when we
10069 	 * come back.
10070 	 */
10071 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10072 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10073 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10074 		return;
10075 	}
10076 
10077 	/*
10078 	 * Only allow a very small subset of IP ioctls on this stream if
10079 	 * IP is a module and not a driver. Allowing ioctls to be processed
10080 	 * in this case may cause assert failures or data corruption.
10081 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10082 	 * ioctls allowed on an IP module stream, after which this stream
10083 	 * normally becomes a multiplexor (at which time the stream head
10084 	 * will fail all ioctls).
10085 	 */
10086 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10087 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10088 			/*
10089 			 * Pass common Streams ioctls which the IP
10090 			 * module does not own or consume along to
10091 			 * be processed down stream.
10092 			 */
10093 			putnext(q, mp);
10094 			return;
10095 		} else {
10096 			goto nak;
10097 		}
10098 	}
10099 
10100 	/* Make sure we have ioctl data to process. */
10101 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10102 		goto nak;
10103 
10104 	/*
10105 	 * Prefer dblk credential over ioctl credential; some synthesized
10106 	 * ioctls have kcred set because there's no way to crhold()
10107 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10108 	 * the framework; the caller of ioctl needs to hold the reference
10109 	 * for the duration of the call).
10110 	 */
10111 	cr = msg_getcred(mp, NULL);
10112 	if (cr == NULL)
10113 		cr = iocp->ioc_cr;
10114 
10115 	/* Make sure normal users don't send down privileged ioctls */
10116 	if ((ipip->ipi_flags & IPI_PRIV) &&
10117 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10118 		/* We checked the privilege earlier but log it here */
10119 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10120 		return;
10121 	}
10122 
10123 	/*
10124 	 * The ioctl command tables can only encode fixed length
10125 	 * ioctl data. If the length is variable, the table will
10126 	 * encode the length as zero. Such special cases are handled
10127 	 * below in the switch.
10128 	 */
10129 	if (ipip->ipi_copyin_size != 0) {
10130 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10131 		return;
10132 	}
10133 
10134 	switch (iocp->ioc_cmd) {
10135 	case O_SIOCGIFCONF:
10136 	case SIOCGIFCONF:
10137 		/*
10138 		 * This IOCTL is hilarious.  See comments in
10139 		 * ip_sioctl_get_ifconf for the story.
10140 		 */
10141 		if (iocp->ioc_count == TRANSPARENT)
10142 			copyin_size = SIZEOF_STRUCT(ifconf,
10143 			    iocp->ioc_flag);
10144 		else
10145 			copyin_size = iocp->ioc_count;
10146 		mi_copyin(q, mp, NULL, copyin_size);
10147 		return;
10148 
10149 	case O_SIOCGLIFCONF:
10150 	case SIOCGLIFCONF:
10151 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10152 		mi_copyin(q, mp, NULL, copyin_size);
10153 		return;
10154 
10155 	case SIOCGLIFSRCOF:
10156 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10157 		mi_copyin(q, mp, NULL, copyin_size);
10158 		return;
10159 	case SIOCGIP6ADDRPOLICY:
10160 		ip_sioctl_ip6addrpolicy(q, mp);
10161 		ip6_asp_table_refrele(ipst);
10162 		return;
10163 
10164 	case SIOCSIP6ADDRPOLICY:
10165 		ip_sioctl_ip6addrpolicy(q, mp);
10166 		return;
10167 
10168 	case SIOCGDSTINFO:
10169 		ip_sioctl_dstinfo(q, mp);
10170 		ip6_asp_table_refrele(ipst);
10171 		return;
10172 
10173 	case I_PLINK:
10174 	case I_PUNLINK:
10175 	case I_LINK:
10176 	case I_UNLINK:
10177 		/*
10178 		 * We treat non-persistent link similarly as the persistent
10179 		 * link case, in terms of plumbing/unplumbing, as well as
10180 		 * dynamic re-plumbing events indicator.  See comments
10181 		 * in ip_sioctl_plink() for more.
10182 		 *
10183 		 * Request can be enqueued in the 'ipsq' while waiting
10184 		 * to become exclusive. So bump up the conn ref.
10185 		 */
10186 		if (CONN_Q(q))
10187 			CONN_INC_REF(Q_TO_CONN(q));
10188 		ip_sioctl_plink(NULL, q, mp, NULL);
10189 		return;
10190 
10191 	case ND_GET:
10192 	case ND_SET:
10193 		/*
10194 		 * Use of the nd table requires holding the reader lock.
10195 		 * Modifying the nd table thru nd_load/nd_unload requires
10196 		 * the writer lock.
10197 		 */
10198 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10199 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10200 			rw_exit(&ipst->ips_ip_g_nd_lock);
10201 
10202 			if (iocp->ioc_error)
10203 				iocp->ioc_count = 0;
10204 			mp->b_datap->db_type = M_IOCACK;
10205 			qreply(q, mp);
10206 			return;
10207 		}
10208 		rw_exit(&ipst->ips_ip_g_nd_lock);
10209 		/*
10210 		 * We don't understand this subioctl of ND_GET / ND_SET.
10211 		 * Maybe intended for some driver / module below us
10212 		 */
10213 		if (q->q_next) {
10214 			putnext(q, mp);
10215 		} else {
10216 			iocp->ioc_error = ENOENT;
10217 			mp->b_datap->db_type = M_IOCNAK;
10218 			iocp->ioc_count = 0;
10219 			qreply(q, mp);
10220 		}
10221 		return;
10222 
10223 	case IP_IOCTL:
10224 		ip_wput_ioctl(q, mp);
10225 		return;
10226 	default:
10227 		cmn_err(CE_PANIC, "should not happen ");
10228 	}
10229 nak:
10230 	if (mp->b_cont != NULL) {
10231 		freemsg(mp->b_cont);
10232 		mp->b_cont = NULL;
10233 	}
10234 	iocp->ioc_error = EINVAL;
10235 	mp->b_datap->db_type = M_IOCNAK;
10236 	iocp->ioc_count = 0;
10237 	qreply(q, mp);
10238 }
10239 
10240 /* ip_wput hands off ARP IOCTL responses to us */
10241 /* ARGSUSED3 */
10242 void
10243 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10244 {
10245 	struct arpreq *ar;
10246 	struct xarpreq *xar;
10247 	area_t	*area;
10248 	mblk_t	*area_mp;
10249 	struct iocblk *iocp;
10250 	mblk_t	*orig_ioc_mp, *tmp;
10251 	struct iocblk	*orig_iocp;
10252 	ill_t *ill;
10253 	conn_t *connp = NULL;
10254 	mblk_t *pending_mp;
10255 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10256 	int *flagsp;
10257 	char *storage = NULL;
10258 	sin_t *sin;
10259 	ipaddr_t addr;
10260 	int err;
10261 	ip_stack_t *ipst;
10262 
10263 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10264 	ill = q->q_ptr;
10265 	ASSERT(ill != NULL);
10266 	ipst = ill->ill_ipst;
10267 
10268 	/*
10269 	 * We should get back from ARP a packet chain that looks like:
10270 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10271 	 */
10272 	if (!(area_mp = mp->b_cont) ||
10273 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10274 	    !(orig_ioc_mp = area_mp->b_cont) ||
10275 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10276 		freemsg(mp);
10277 		return;
10278 	}
10279 
10280 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10281 
10282 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10283 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10284 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10285 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10286 		x_arp_ioctl = B_TRUE;
10287 		xar = (struct xarpreq *)tmp->b_rptr;
10288 		sin = (sin_t *)&xar->xarp_pa;
10289 		flagsp = &xar->xarp_flags;
10290 		storage = xar->xarp_ha.sdl_data;
10291 		if (xar->xarp_ha.sdl_nlen != 0)
10292 			ifx_arp_ioctl = B_TRUE;
10293 	} else {
10294 		ar = (struct arpreq *)tmp->b_rptr;
10295 		sin = (sin_t *)&ar->arp_pa;
10296 		flagsp = &ar->arp_flags;
10297 		storage = ar->arp_ha.sa_data;
10298 	}
10299 
10300 	iocp = (struct iocblk *)mp->b_rptr;
10301 
10302 	/*
10303 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10304 	 * Otherwise, we can find it from our ioc_id.
10305 	 */
10306 	if (ipsq != NULL)
10307 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10308 	else
10309 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10310 
10311 	if (pending_mp == NULL) {
10312 		ASSERT(connp == NULL);
10313 		inet_freemsg(mp);
10314 		return;
10315 	}
10316 	ASSERT(connp != NULL);
10317 	q = CONNP_TO_WQ(connp);
10318 
10319 	/* Uncouple the internally generated IOCTL from the original one */
10320 	area = (area_t *)area_mp->b_rptr;
10321 	area_mp->b_cont = NULL;
10322 
10323 	/*
10324 	 * Restore the b_next and b_prev used by mi code. This is needed
10325 	 * to complete the ioctl using mi* functions. We stored them in
10326 	 * the pending mp prior to sending the request to ARP.
10327 	 */
10328 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10329 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10330 	inet_freemsg(pending_mp);
10331 
10332 	/*
10333 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10334 	 * Catch the case where there is an IRE_CACHE by no entry in the
10335 	 * arp table.
10336 	 */
10337 	addr = sin->sin_addr.s_addr;
10338 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10339 		ire_t			*ire;
10340 		dl_unitdata_req_t	*dlup;
10341 		mblk_t			*llmp;
10342 		int			addr_len;
10343 		ill_t			*ipsqill = NULL;
10344 
10345 		if (ifx_arp_ioctl) {
10346 			/*
10347 			 * There's no need to lookup the ill, since
10348 			 * we've already done that when we started
10349 			 * processing the ioctl and sent the message
10350 			 * to ARP on that ill.  So use the ill that
10351 			 * is stored in q->q_ptr.
10352 			 */
10353 			ipsqill = ill;
10354 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10355 			    ipsqill->ill_ipif, ALL_ZONES,
10356 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10357 		} else {
10358 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10359 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10360 			if (ire != NULL)
10361 				ipsqill = ire_to_ill(ire);
10362 		}
10363 
10364 		if ((x_arp_ioctl) && (ipsqill != NULL))
10365 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10366 
10367 		if (ire != NULL) {
10368 			/*
10369 			 * Since the ire obtained from cachetable is used for
10370 			 * mac addr copying below, treat an incomplete ire as if
10371 			 * as if we never found it.
10372 			 */
10373 			if (ire->ire_nce != NULL &&
10374 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10375 				ire_refrele(ire);
10376 				ire = NULL;
10377 				ipsqill = NULL;
10378 				goto errack;
10379 			}
10380 			*flagsp = ATF_INUSE;
10381 			llmp = (ire->ire_nce != NULL ?
10382 			    ire->ire_nce->nce_res_mp : NULL);
10383 			if (llmp != NULL && ipsqill != NULL) {
10384 				uchar_t *macaddr;
10385 
10386 				addr_len = ipsqill->ill_phys_addr_length;
10387 				if (x_arp_ioctl && ((addr_len +
10388 				    ipsqill->ill_name_length) >
10389 				    sizeof (xar->xarp_ha.sdl_data))) {
10390 					ire_refrele(ire);
10391 					freemsg(mp);
10392 					ip_ioctl_finish(q, orig_ioc_mp,
10393 					    EINVAL, NO_COPYOUT, ipsq);
10394 					return;
10395 				}
10396 				*flagsp |= ATF_COM;
10397 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10398 				if (ipsqill->ill_sap_length < 0)
10399 					macaddr = llmp->b_rptr +
10400 					    dlup->dl_dest_addr_offset;
10401 				else
10402 					macaddr = llmp->b_rptr +
10403 					    dlup->dl_dest_addr_offset +
10404 					    ipsqill->ill_sap_length;
10405 				/*
10406 				 * For SIOCGARP, MAC address length
10407 				 * validation has already been done
10408 				 * before the ioctl was issued to ARP to
10409 				 * allow it to progress only on 6 byte
10410 				 * addressable (ethernet like) media. Thus
10411 				 * the mac address copying can not overwrite
10412 				 * the sa_data area below.
10413 				 */
10414 				bcopy(macaddr, storage, addr_len);
10415 			}
10416 			/* Ditch the internal IOCTL. */
10417 			freemsg(mp);
10418 			ire_refrele(ire);
10419 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10420 			return;
10421 		}
10422 	}
10423 
10424 	/*
10425 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10426 	 * on the IPMP meta-interface, ensure any ARP entries added in
10427 	 * ip_sioctl_arp() are deleted.
10428 	 */
10429 	if (IS_IPMP(ill) &&
10430 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10431 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10432 		ipmp_illgrp_t *illg = ill->ill_grp;
10433 		ipmp_arpent_t *entp;
10434 
10435 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10436 			ipmp_illgrp_destroy_arpent(illg, entp);
10437 	}
10438 
10439 	/*
10440 	 * Delete the coresponding IRE_CACHE if any.
10441 	 * Reset the error if there was one (in case there was no entry
10442 	 * in arp.)
10443 	 */
10444 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10445 		ipif_t *ipintf = NULL;
10446 
10447 		if (ifx_arp_ioctl) {
10448 			/*
10449 			 * There's no need to lookup the ill, since
10450 			 * we've already done that when we started
10451 			 * processing the ioctl and sent the message
10452 			 * to ARP on that ill.  So use the ill that
10453 			 * is stored in q->q_ptr.
10454 			 */
10455 			ipintf = ill->ill_ipif;
10456 		}
10457 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10458 			/*
10459 			 * The address in "addr" may be an entry for a
10460 			 * router. If that's true, then any off-net
10461 			 * IRE_CACHE entries that go through the router
10462 			 * with address "addr" must be clobbered. Use
10463 			 * ire_walk to achieve this goal.
10464 			 */
10465 			if (ifx_arp_ioctl)
10466 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10467 				    ire_delete_cache_gw, (char *)&addr, ill);
10468 			else
10469 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10470 				    ALL_ZONES, ipst);
10471 			iocp->ioc_error = 0;
10472 		}
10473 	}
10474 errack:
10475 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10476 		err = iocp->ioc_error;
10477 		freemsg(mp);
10478 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10479 		return;
10480 	}
10481 
10482 	/*
10483 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10484 	 * the area_t into the struct {x}arpreq.
10485 	 */
10486 	if (x_arp_ioctl) {
10487 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10488 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10489 		    sizeof (xar->xarp_ha.sdl_data)) {
10490 			freemsg(mp);
10491 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10492 			    ipsq);
10493 			return;
10494 		}
10495 	}
10496 	*flagsp = ATF_INUSE;
10497 	if (area->area_flags & ACE_F_PERMANENT)
10498 		*flagsp |= ATF_PERM;
10499 	if (area->area_flags & ACE_F_PUBLISH)
10500 		*flagsp |= ATF_PUBL;
10501 	if (area->area_flags & ACE_F_AUTHORITY)
10502 		*flagsp |= ATF_AUTHORITY;
10503 	if (area->area_hw_addr_length != 0) {
10504 		*flagsp |= ATF_COM;
10505 		/*
10506 		 * For SIOCGARP, MAC address length validation has
10507 		 * already been done before the ioctl was issued to ARP
10508 		 * to allow it to progress only on 6 byte addressable
10509 		 * (ethernet like) media. Thus the mac address copying
10510 		 * can not overwrite the sa_data area below.
10511 		 */
10512 		bcopy((char *)area + area->area_hw_addr_offset,
10513 		    storage, area->area_hw_addr_length);
10514 	}
10515 
10516 	/* Ditch the internal IOCTL. */
10517 	freemsg(mp);
10518 	/* Complete the original. */
10519 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10520 }
10521 
10522 /*
10523  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10524  * interface) create the next available logical interface for this
10525  * physical interface.
10526  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10527  * ipif with the specified name.
10528  *
10529  * If the address family is not AF_UNSPEC then set the address as well.
10530  *
10531  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10532  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10533  *
10534  * Executed as a writer on the ill.
10535  * So no lock is needed to traverse the ipif chain, or examine the
10536  * phyint flags.
10537  */
10538 /* ARGSUSED */
10539 int
10540 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10541     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10542 {
10543 	mblk_t	*mp1;
10544 	struct lifreq *lifr;
10545 	boolean_t	isv6;
10546 	boolean_t	exists;
10547 	char 	*name;
10548 	char	*endp;
10549 	char	*cp;
10550 	int	namelen;
10551 	ipif_t	*ipif;
10552 	long	id;
10553 	ipsq_t	*ipsq;
10554 	ill_t	*ill;
10555 	sin_t	*sin;
10556 	int	err = 0;
10557 	boolean_t found_sep = B_FALSE;
10558 	conn_t	*connp;
10559 	zoneid_t zoneid;
10560 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10561 
10562 	ASSERT(q->q_next == NULL);
10563 	ip1dbg(("ip_sioctl_addif\n"));
10564 	/* Existence of mp1 has been checked in ip_wput_nondata */
10565 	mp1 = mp->b_cont->b_cont;
10566 	/*
10567 	 * Null terminate the string to protect against buffer
10568 	 * overrun. String was generated by user code and may not
10569 	 * be trusted.
10570 	 */
10571 	lifr = (struct lifreq *)mp1->b_rptr;
10572 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10573 	name = lifr->lifr_name;
10574 	ASSERT(CONN_Q(q));
10575 	connp = Q_TO_CONN(q);
10576 	isv6 = connp->conn_af_isv6;
10577 	zoneid = connp->conn_zoneid;
10578 	namelen = mi_strlen(name);
10579 	if (namelen == 0)
10580 		return (EINVAL);
10581 
10582 	exists = B_FALSE;
10583 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10584 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10585 		/*
10586 		 * Allow creating lo0 using SIOCLIFADDIF.
10587 		 * can't be any other writer thread. So can pass null below
10588 		 * for the last 4 args to ipif_lookup_name.
10589 		 */
10590 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10591 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10592 		/* Prevent any further action */
10593 		if (ipif == NULL) {
10594 			return (ENOBUFS);
10595 		} else if (!exists) {
10596 			/* We created the ipif now and as writer */
10597 			ipif_refrele(ipif);
10598 			return (0);
10599 		} else {
10600 			ill = ipif->ipif_ill;
10601 			ill_refhold(ill);
10602 			ipif_refrele(ipif);
10603 		}
10604 	} else {
10605 		/* Look for a colon in the name. */
10606 		endp = &name[namelen];
10607 		for (cp = endp; --cp > name; ) {
10608 			if (*cp == IPIF_SEPARATOR_CHAR) {
10609 				found_sep = B_TRUE;
10610 				/*
10611 				 * Reject any non-decimal aliases for plumbing
10612 				 * of logical interfaces. Aliases with leading
10613 				 * zeroes are also rejected as they introduce
10614 				 * ambiguity in the naming of the interfaces.
10615 				 * Comparing with "0" takes care of all such
10616 				 * cases.
10617 				 */
10618 				if ((strncmp("0", cp+1, 1)) == 0)
10619 					return (EINVAL);
10620 
10621 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10622 				    id <= 0 || *endp != '\0') {
10623 					return (EINVAL);
10624 				}
10625 				*cp = '\0';
10626 				break;
10627 			}
10628 		}
10629 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10630 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10631 		if (found_sep)
10632 			*cp = IPIF_SEPARATOR_CHAR;
10633 		if (ill == NULL)
10634 			return (err);
10635 	}
10636 
10637 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10638 	    B_TRUE);
10639 
10640 	/*
10641 	 * Release the refhold due to the lookup, now that we are excl
10642 	 * or we are just returning
10643 	 */
10644 	ill_refrele(ill);
10645 
10646 	if (ipsq == NULL)
10647 		return (EINPROGRESS);
10648 
10649 	/* We are now exclusive on the IPSQ */
10650 	ASSERT(IAM_WRITER_ILL(ill));
10651 
10652 	if (found_sep) {
10653 		/* Now see if there is an IPIF with this unit number. */
10654 		for (ipif = ill->ill_ipif; ipif != NULL;
10655 		    ipif = ipif->ipif_next) {
10656 			if (ipif->ipif_id == id) {
10657 				err = EEXIST;
10658 				goto done;
10659 			}
10660 		}
10661 	}
10662 
10663 	/*
10664 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10665 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10666 	 * instead.
10667 	 */
10668 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10669 	    B_TRUE, B_TRUE)) == NULL) {
10670 		err = ENOBUFS;
10671 		goto done;
10672 	}
10673 
10674 	/* Return created name with ioctl */
10675 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10676 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10677 	ip1dbg(("created %s\n", lifr->lifr_name));
10678 
10679 	/* Set address */
10680 	sin = (sin_t *)&lifr->lifr_addr;
10681 	if (sin->sin_family != AF_UNSPEC) {
10682 		err = ip_sioctl_addr(ipif, sin, q, mp,
10683 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10684 	}
10685 
10686 done:
10687 	ipsq_exit(ipsq);
10688 	return (err);
10689 }
10690 
10691 /*
10692  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10693  * interface) delete it based on the IP address (on this physical interface).
10694  * Otherwise delete it based on the ipif_id.
10695  * Also, special handling to allow a removeif of lo0.
10696  */
10697 /* ARGSUSED */
10698 int
10699 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10700     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10701 {
10702 	conn_t		*connp;
10703 	ill_t		*ill = ipif->ipif_ill;
10704 	boolean_t	 success;
10705 	ip_stack_t	*ipst;
10706 
10707 	ipst = CONNQ_TO_IPST(q);
10708 
10709 	ASSERT(q->q_next == NULL);
10710 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10711 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10712 	ASSERT(IAM_WRITER_IPIF(ipif));
10713 
10714 	connp = Q_TO_CONN(q);
10715 	/*
10716 	 * Special case for unplumbing lo0 (the loopback physical interface).
10717 	 * If unplumbing lo0, the incoming address structure has been
10718 	 * initialized to all zeros. When unplumbing lo0, all its logical
10719 	 * interfaces must be removed too.
10720 	 *
10721 	 * Note that this interface may be called to remove a specific
10722 	 * loopback logical interface (eg, lo0:1). But in that case
10723 	 * ipif->ipif_id != 0 so that the code path for that case is the
10724 	 * same as any other interface (meaning it skips the code directly
10725 	 * below).
10726 	 */
10727 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10728 		if (sin->sin_family == AF_UNSPEC &&
10729 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10730 			/*
10731 			 * Mark it condemned. No new ref. will be made to ill.
10732 			 */
10733 			mutex_enter(&ill->ill_lock);
10734 			ill->ill_state_flags |= ILL_CONDEMNED;
10735 			for (ipif = ill->ill_ipif; ipif != NULL;
10736 			    ipif = ipif->ipif_next) {
10737 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10738 			}
10739 			mutex_exit(&ill->ill_lock);
10740 
10741 			ipif = ill->ill_ipif;
10742 			/* unplumb the loopback interface */
10743 			ill_delete(ill);
10744 			mutex_enter(&connp->conn_lock);
10745 			mutex_enter(&ill->ill_lock);
10746 
10747 			/* Are any references to this ill active */
10748 			if (ill_is_freeable(ill)) {
10749 				mutex_exit(&ill->ill_lock);
10750 				mutex_exit(&connp->conn_lock);
10751 				ill_delete_tail(ill);
10752 				mi_free(ill);
10753 				return (0);
10754 			}
10755 			success = ipsq_pending_mp_add(connp, ipif,
10756 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10757 			mutex_exit(&connp->conn_lock);
10758 			mutex_exit(&ill->ill_lock);
10759 			if (success)
10760 				return (EINPROGRESS);
10761 			else
10762 				return (EINTR);
10763 		}
10764 	}
10765 
10766 	if (ipif->ipif_id == 0) {
10767 		ipsq_t *ipsq;
10768 
10769 		/* Find based on address */
10770 		if (ipif->ipif_isv6) {
10771 			sin6_t *sin6;
10772 
10773 			if (sin->sin_family != AF_INET6)
10774 				return (EAFNOSUPPORT);
10775 
10776 			sin6 = (sin6_t *)sin;
10777 			/* We are a writer, so we should be able to lookup */
10778 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10779 			    ipst);
10780 		} else {
10781 			if (sin->sin_family != AF_INET)
10782 				return (EAFNOSUPPORT);
10783 
10784 			/* We are a writer, so we should be able to lookup */
10785 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10786 			    ipst);
10787 		}
10788 		if (ipif == NULL) {
10789 			return (EADDRNOTAVAIL);
10790 		}
10791 
10792 		/*
10793 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10794 		 * lifr_name of the physical interface but with an ip address
10795 		 * lifr_addr of a logical interface plumbed over it.
10796 		 * So update ipx_current_ipif now that ipif points to the
10797 		 * correct one.
10798 		 */
10799 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10800 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10801 
10802 		/* This is a writer */
10803 		ipif_refrele(ipif);
10804 	}
10805 
10806 	/*
10807 	 * Can not delete instance zero since it is tied to the ill.
10808 	 */
10809 	if (ipif->ipif_id == 0)
10810 		return (EBUSY);
10811 
10812 	mutex_enter(&ill->ill_lock);
10813 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10814 	mutex_exit(&ill->ill_lock);
10815 
10816 	ipif_free(ipif);
10817 
10818 	mutex_enter(&connp->conn_lock);
10819 	mutex_enter(&ill->ill_lock);
10820 
10821 	/* Are any references to this ipif active */
10822 	if (ipif_is_freeable(ipif)) {
10823 		mutex_exit(&ill->ill_lock);
10824 		mutex_exit(&connp->conn_lock);
10825 		ipif_non_duplicate(ipif);
10826 		ipif_down_tail(ipif);
10827 		ipif_free_tail(ipif); /* frees ipif */
10828 		return (0);
10829 	}
10830 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10831 	    IPIF_FREE);
10832 	mutex_exit(&ill->ill_lock);
10833 	mutex_exit(&connp->conn_lock);
10834 	if (success)
10835 		return (EINPROGRESS);
10836 	else
10837 		return (EINTR);
10838 }
10839 
10840 /*
10841  * Restart the removeif ioctl. The refcnt has gone down to 0.
10842  * The ipif is already condemned. So can't find it thru lookups.
10843  */
10844 /* ARGSUSED */
10845 int
10846 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10847     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10848 {
10849 	ill_t *ill = ipif->ipif_ill;
10850 
10851 	ASSERT(IAM_WRITER_IPIF(ipif));
10852 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10853 
10854 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10855 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10856 
10857 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10858 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10859 		ill_delete_tail(ill);
10860 		mi_free(ill);
10861 		return (0);
10862 	}
10863 
10864 	ipif_non_duplicate(ipif);
10865 	ipif_down_tail(ipif);
10866 	ipif_free_tail(ipif);
10867 
10868 	ILL_UNMARK_CHANGING(ill);
10869 	return (0);
10870 }
10871 
10872 /*
10873  * Set the local interface address.
10874  * Allow an address of all zero when the interface is down.
10875  */
10876 /* ARGSUSED */
10877 int
10878 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10879     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10880 {
10881 	int err = 0;
10882 	in6_addr_t v6addr;
10883 	boolean_t need_up = B_FALSE;
10884 
10885 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10886 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10887 
10888 	ASSERT(IAM_WRITER_IPIF(ipif));
10889 
10890 	if (ipif->ipif_isv6) {
10891 		sin6_t *sin6;
10892 		ill_t *ill;
10893 		phyint_t *phyi;
10894 
10895 		if (sin->sin_family != AF_INET6)
10896 			return (EAFNOSUPPORT);
10897 
10898 		sin6 = (sin6_t *)sin;
10899 		v6addr = sin6->sin6_addr;
10900 		ill = ipif->ipif_ill;
10901 		phyi = ill->ill_phyint;
10902 
10903 		/*
10904 		 * Enforce that true multicast interfaces have a link-local
10905 		 * address for logical unit 0.
10906 		 */
10907 		if (ipif->ipif_id == 0 &&
10908 		    (ill->ill_flags & ILLF_MULTICAST) &&
10909 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10910 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10911 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10912 			return (EADDRNOTAVAIL);
10913 		}
10914 
10915 		/*
10916 		 * up interfaces shouldn't have the unspecified address
10917 		 * unless they also have the IPIF_NOLOCAL flags set and
10918 		 * have a subnet assigned.
10919 		 */
10920 		if ((ipif->ipif_flags & IPIF_UP) &&
10921 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10922 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10923 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10924 			return (EADDRNOTAVAIL);
10925 		}
10926 
10927 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10928 			return (EADDRNOTAVAIL);
10929 	} else {
10930 		ipaddr_t addr;
10931 
10932 		if (sin->sin_family != AF_INET)
10933 			return (EAFNOSUPPORT);
10934 
10935 		addr = sin->sin_addr.s_addr;
10936 
10937 		/* Allow 0 as the local address. */
10938 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10939 			return (EADDRNOTAVAIL);
10940 
10941 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10942 	}
10943 
10944 	/*
10945 	 * Even if there is no change we redo things just to rerun
10946 	 * ipif_set_default.
10947 	 */
10948 	if (ipif->ipif_flags & IPIF_UP) {
10949 		/*
10950 		 * Setting a new local address, make sure
10951 		 * we have net and subnet bcast ire's for
10952 		 * the old address if we need them.
10953 		 */
10954 		if (!ipif->ipif_isv6)
10955 			ipif_check_bcast_ires(ipif);
10956 		/*
10957 		 * If the interface is already marked up,
10958 		 * we call ipif_down which will take care
10959 		 * of ditching any IREs that have been set
10960 		 * up based on the old interface address.
10961 		 */
10962 		err = ipif_logical_down(ipif, q, mp);
10963 		if (err == EINPROGRESS)
10964 			return (err);
10965 		ipif_down_tail(ipif);
10966 		need_up = 1;
10967 	}
10968 
10969 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10970 	return (err);
10971 }
10972 
10973 int
10974 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10975     boolean_t need_up)
10976 {
10977 	in6_addr_t v6addr;
10978 	in6_addr_t ov6addr;
10979 	ipaddr_t addr;
10980 	sin6_t	*sin6;
10981 	int	sinlen;
10982 	int	err = 0;
10983 	ill_t	*ill = ipif->ipif_ill;
10984 	boolean_t need_dl_down;
10985 	boolean_t need_arp_down;
10986 	struct iocblk *iocp;
10987 
10988 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
10989 
10990 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10991 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10992 	ASSERT(IAM_WRITER_IPIF(ipif));
10993 
10994 	/* Must cancel any pending timer before taking the ill_lock */
10995 	if (ipif->ipif_recovery_id != 0)
10996 		(void) untimeout(ipif->ipif_recovery_id);
10997 	ipif->ipif_recovery_id = 0;
10998 
10999 	if (ipif->ipif_isv6) {
11000 		sin6 = (sin6_t *)sin;
11001 		v6addr = sin6->sin6_addr;
11002 		sinlen = sizeof (struct sockaddr_in6);
11003 	} else {
11004 		addr = sin->sin_addr.s_addr;
11005 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11006 		sinlen = sizeof (struct sockaddr_in);
11007 	}
11008 	mutex_enter(&ill->ill_lock);
11009 	ov6addr = ipif->ipif_v6lcl_addr;
11010 	ipif->ipif_v6lcl_addr = v6addr;
11011 	sctp_update_ipif_addr(ipif, ov6addr);
11012 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11013 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11014 	} else {
11015 		ipif->ipif_v6src_addr = v6addr;
11016 	}
11017 	ipif->ipif_addr_ready = 0;
11018 
11019 	/*
11020 	 * If the interface was previously marked as a duplicate, then since
11021 	 * we've now got a "new" address, it should no longer be considered a
11022 	 * duplicate -- even if the "new" address is the same as the old one.
11023 	 * Note that if all ipifs are down, we may have a pending ARP down
11024 	 * event to handle.  This is because we want to recover from duplicates
11025 	 * and thus delay tearing down ARP until the duplicates have been
11026 	 * removed or disabled.
11027 	 */
11028 	need_dl_down = need_arp_down = B_FALSE;
11029 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11030 		need_arp_down = !need_up;
11031 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11032 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11033 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11034 			need_dl_down = B_TRUE;
11035 		}
11036 	}
11037 
11038 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11039 	    !ill->ill_is_6to4tun) {
11040 		queue_t *wqp = ill->ill_wq;
11041 
11042 		/*
11043 		 * The local address of this interface is a 6to4 address,
11044 		 * check if this interface is in fact a 6to4 tunnel or just
11045 		 * an interface configured with a 6to4 address.  We are only
11046 		 * interested in the former.
11047 		 */
11048 		if (wqp != NULL) {
11049 			while ((wqp->q_next != NULL) &&
11050 			    (wqp->q_next->q_qinfo != NULL) &&
11051 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11052 
11053 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11054 				    == TUN6TO4_MODID) {
11055 					/* set for use in IP */
11056 					ill->ill_is_6to4tun = 1;
11057 					break;
11058 				}
11059 				wqp = wqp->q_next;
11060 			}
11061 		}
11062 	}
11063 
11064 	ipif_set_default(ipif);
11065 
11066 	/*
11067 	 * When publishing an interface address change event, we only notify
11068 	 * the event listeners of the new address.  It is assumed that if they
11069 	 * actively care about the addresses assigned that they will have
11070 	 * already discovered the previous address assigned (if there was one.)
11071 	 *
11072 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11073 	 */
11074 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11075 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11076 		    NE_ADDRESS_CHANGE, sin, sinlen);
11077 	}
11078 
11079 	mutex_exit(&ill->ill_lock);
11080 
11081 	if (need_up) {
11082 		/*
11083 		 * Now bring the interface back up.  If this
11084 		 * is the only IPIF for the ILL, ipif_up
11085 		 * will have to re-bind to the device, so
11086 		 * we may get back EINPROGRESS, in which
11087 		 * case, this IOCTL will get completed in
11088 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11089 		 */
11090 		err = ipif_up(ipif, q, mp);
11091 	}
11092 
11093 	if (need_dl_down)
11094 		ill_dl_down(ill);
11095 	if (need_arp_down)
11096 		ipif_resolver_down(ipif);
11097 
11098 	return (err);
11099 }
11100 
11101 /*
11102  * Restart entry point to restart the address set operation after the
11103  * refcounts have dropped to zero.
11104  */
11105 /* ARGSUSED */
11106 int
11107 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11108     ip_ioctl_cmd_t *ipip, void *ifreq)
11109 {
11110 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11111 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11112 	ASSERT(IAM_WRITER_IPIF(ipif));
11113 	ipif_down_tail(ipif);
11114 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11115 }
11116 
11117 /* ARGSUSED */
11118 int
11119 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11120     ip_ioctl_cmd_t *ipip, void *if_req)
11121 {
11122 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11123 	struct lifreq *lifr = (struct lifreq *)if_req;
11124 
11125 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11126 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11127 	/*
11128 	 * The net mask and address can't change since we have a
11129 	 * reference to the ipif. So no lock is necessary.
11130 	 */
11131 	if (ipif->ipif_isv6) {
11132 		*sin6 = sin6_null;
11133 		sin6->sin6_family = AF_INET6;
11134 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11135 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11136 		lifr->lifr_addrlen =
11137 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11138 	} else {
11139 		*sin = sin_null;
11140 		sin->sin_family = AF_INET;
11141 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11142 		if (ipip->ipi_cmd_type == LIF_CMD) {
11143 			lifr->lifr_addrlen =
11144 			    ip_mask_to_plen(ipif->ipif_net_mask);
11145 		}
11146 	}
11147 	return (0);
11148 }
11149 
11150 /*
11151  * Set the destination address for a pt-pt interface.
11152  */
11153 /* ARGSUSED */
11154 int
11155 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11156     ip_ioctl_cmd_t *ipip, void *if_req)
11157 {
11158 	int err = 0;
11159 	in6_addr_t v6addr;
11160 	boolean_t need_up = B_FALSE;
11161 
11162 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11163 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11164 	ASSERT(IAM_WRITER_IPIF(ipif));
11165 
11166 	if (ipif->ipif_isv6) {
11167 		sin6_t *sin6;
11168 
11169 		if (sin->sin_family != AF_INET6)
11170 			return (EAFNOSUPPORT);
11171 
11172 		sin6 = (sin6_t *)sin;
11173 		v6addr = sin6->sin6_addr;
11174 
11175 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11176 			return (EADDRNOTAVAIL);
11177 	} else {
11178 		ipaddr_t addr;
11179 
11180 		if (sin->sin_family != AF_INET)
11181 			return (EAFNOSUPPORT);
11182 
11183 		addr = sin->sin_addr.s_addr;
11184 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11185 			return (EADDRNOTAVAIL);
11186 
11187 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11188 	}
11189 
11190 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11191 		return (0);	/* No change */
11192 
11193 	if (ipif->ipif_flags & IPIF_UP) {
11194 		/*
11195 		 * If the interface is already marked up,
11196 		 * we call ipif_down which will take care
11197 		 * of ditching any IREs that have been set
11198 		 * up based on the old pp dst address.
11199 		 */
11200 		err = ipif_logical_down(ipif, q, mp);
11201 		if (err == EINPROGRESS)
11202 			return (err);
11203 		ipif_down_tail(ipif);
11204 		need_up = B_TRUE;
11205 	}
11206 	/*
11207 	 * could return EINPROGRESS. If so ioctl will complete in
11208 	 * ip_rput_dlpi_writer
11209 	 */
11210 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11211 	return (err);
11212 }
11213 
11214 static int
11215 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11216     boolean_t need_up)
11217 {
11218 	in6_addr_t v6addr;
11219 	ill_t	*ill = ipif->ipif_ill;
11220 	int	err = 0;
11221 	boolean_t need_dl_down;
11222 	boolean_t need_arp_down;
11223 
11224 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11225 	    ipif->ipif_id, (void *)ipif));
11226 
11227 	/* Must cancel any pending timer before taking the ill_lock */
11228 	if (ipif->ipif_recovery_id != 0)
11229 		(void) untimeout(ipif->ipif_recovery_id);
11230 	ipif->ipif_recovery_id = 0;
11231 
11232 	if (ipif->ipif_isv6) {
11233 		sin6_t *sin6;
11234 
11235 		sin6 = (sin6_t *)sin;
11236 		v6addr = sin6->sin6_addr;
11237 	} else {
11238 		ipaddr_t addr;
11239 
11240 		addr = sin->sin_addr.s_addr;
11241 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11242 	}
11243 	mutex_enter(&ill->ill_lock);
11244 	/* Set point to point destination address. */
11245 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11246 		/*
11247 		 * Allow this as a means of creating logical
11248 		 * pt-pt interfaces on top of e.g. an Ethernet.
11249 		 * XXX Undocumented HACK for testing.
11250 		 * pt-pt interfaces are created with NUD disabled.
11251 		 */
11252 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11253 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11254 		if (ipif->ipif_isv6)
11255 			ill->ill_flags |= ILLF_NONUD;
11256 	}
11257 
11258 	/*
11259 	 * If the interface was previously marked as a duplicate, then since
11260 	 * we've now got a "new" address, it should no longer be considered a
11261 	 * duplicate -- even if the "new" address is the same as the old one.
11262 	 * Note that if all ipifs are down, we may have a pending ARP down
11263 	 * event to handle.
11264 	 */
11265 	need_dl_down = need_arp_down = B_FALSE;
11266 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11267 		need_arp_down = !need_up;
11268 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11269 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11270 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11271 			need_dl_down = B_TRUE;
11272 		}
11273 	}
11274 
11275 	/* Set the new address. */
11276 	ipif->ipif_v6pp_dst_addr = v6addr;
11277 	/* Make sure subnet tracks pp_dst */
11278 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11279 	mutex_exit(&ill->ill_lock);
11280 
11281 	if (need_up) {
11282 		/*
11283 		 * Now bring the interface back up.  If this
11284 		 * is the only IPIF for the ILL, ipif_up
11285 		 * will have to re-bind to the device, so
11286 		 * we may get back EINPROGRESS, in which
11287 		 * case, this IOCTL will get completed in
11288 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11289 		 */
11290 		err = ipif_up(ipif, q, mp);
11291 	}
11292 
11293 	if (need_dl_down)
11294 		ill_dl_down(ill);
11295 	if (need_arp_down)
11296 		ipif_resolver_down(ipif);
11297 
11298 	return (err);
11299 }
11300 
11301 /*
11302  * Restart entry point to restart the dstaddress set operation after the
11303  * refcounts have dropped to zero.
11304  */
11305 /* ARGSUSED */
11306 int
11307 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11308     ip_ioctl_cmd_t *ipip, void *ifreq)
11309 {
11310 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11311 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11312 	ipif_down_tail(ipif);
11313 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11314 }
11315 
11316 /* ARGSUSED */
11317 int
11318 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11319     ip_ioctl_cmd_t *ipip, void *if_req)
11320 {
11321 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11322 
11323 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11324 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11325 	/*
11326 	 * Get point to point destination address. The addresses can't
11327 	 * change since we hold a reference to the ipif.
11328 	 */
11329 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11330 		return (EADDRNOTAVAIL);
11331 
11332 	if (ipif->ipif_isv6) {
11333 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11334 		*sin6 = sin6_null;
11335 		sin6->sin6_family = AF_INET6;
11336 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11337 	} else {
11338 		*sin = sin_null;
11339 		sin->sin_family = AF_INET;
11340 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11341 	}
11342 	return (0);
11343 }
11344 
11345 /*
11346  * Set interface flags.  Many flags require special handling (e.g.,
11347  * bringing the interface down); see below for details.
11348  *
11349  * NOTE : We really don't enforce that ipif_id zero should be used
11350  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11351  *	  is because applications generally does SICGLIFFLAGS and
11352  *	  ORs in the new flags (that affects the logical) and does a
11353  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11354  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11355  *	  flags that will be turned on is correct with respect to
11356  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11357  */
11358 /* ARGSUSED */
11359 int
11360 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11361     ip_ioctl_cmd_t *ipip, void *if_req)
11362 {
11363 	uint64_t turn_on;
11364 	uint64_t turn_off;
11365 	int	err = 0;
11366 	phyint_t *phyi;
11367 	ill_t *ill;
11368 	uint64_t intf_flags, cantchange_flags;
11369 	boolean_t phyint_flags_modified = B_FALSE;
11370 	uint64_t flags;
11371 	struct ifreq *ifr;
11372 	struct lifreq *lifr;
11373 	boolean_t set_linklocal = B_FALSE;
11374 	boolean_t zero_source = B_FALSE;
11375 
11376 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11377 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11378 
11379 	ASSERT(IAM_WRITER_IPIF(ipif));
11380 
11381 	ill = ipif->ipif_ill;
11382 	phyi = ill->ill_phyint;
11383 
11384 	if (ipip->ipi_cmd_type == IF_CMD) {
11385 		ifr = (struct ifreq *)if_req;
11386 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11387 	} else {
11388 		lifr = (struct lifreq *)if_req;
11389 		flags = lifr->lifr_flags;
11390 	}
11391 
11392 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11393 
11394 	/*
11395 	 * Have the flags been set correctly until now?
11396 	 */
11397 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11398 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11399 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11400 	/*
11401 	 * Compare the new flags to the old, and partition
11402 	 * into those coming on and those going off.
11403 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11404 	 */
11405 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11406 		flags |= intf_flags & ~0xFFFF;
11407 
11408 	/*
11409 	 * Explicitly fail attempts to change flags that are always invalid on
11410 	 * an IPMP meta-interface.
11411 	 */
11412 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11413 		return (EINVAL);
11414 
11415 	/*
11416 	 * Check which flags will change; silently ignore flags which userland
11417 	 * is not allowed to control.  (Because these flags may change between
11418 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11419 	 * control, we need to silently ignore them rather than fail.)
11420 	 */
11421 	cantchange_flags = IFF_CANTCHANGE;
11422 	if (IS_IPMP(ill))
11423 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11424 
11425 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11426 	if (turn_on == 0)
11427 		return (0);	/* No change */
11428 
11429 	turn_off = intf_flags & turn_on;
11430 	turn_on ^= turn_off;
11431 
11432 	/*
11433 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11434 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11435 	 * allow it to be turned off.
11436 	 */
11437 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11438 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11439 		return (EINVAL);
11440 
11441 	if (turn_on & IFF_NOFAILOVER) {
11442 		turn_on |= IFF_DEPRECATED;
11443 		flags |= IFF_DEPRECATED;
11444 	}
11445 
11446 	/*
11447 	 * On underlying interfaces, only allow applications to manage test
11448 	 * addresses -- otherwise, they may get confused when the address
11449 	 * moves as part of being brought up.  Likewise, prevent an
11450 	 * application-managed test address from being converted to a data
11451 	 * address.  To prevent migration of administratively up addresses in
11452 	 * the kernel, we don't allow them to be converted either.
11453 	 */
11454 	if (IS_UNDER_IPMP(ill)) {
11455 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11456 
11457 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11458 			return (EINVAL);
11459 
11460 		if ((turn_off & IFF_NOFAILOVER) &&
11461 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11462 			return (EINVAL);
11463 	}
11464 
11465 	/*
11466 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11467 	 * IPv6 interfaces.
11468 	 */
11469 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11470 		return (EINVAL);
11471 
11472 	/*
11473 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11474 	 */
11475 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11476 		return (EINVAL);
11477 
11478 	/*
11479 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11480 	 * interfaces.  It makes no sense in that context.
11481 	 */
11482 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11483 		return (EINVAL);
11484 
11485 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11486 		zero_source = B_TRUE;
11487 
11488 	/*
11489 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11490 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11491 	 * If the link local address isn't set, and can be set, it will get
11492 	 * set later on in this function.
11493 	 */
11494 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11495 	    (flags & IFF_UP) && !zero_source &&
11496 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11497 		if (ipif_cant_setlinklocal(ipif))
11498 			return (EINVAL);
11499 		set_linklocal = B_TRUE;
11500 	}
11501 
11502 	/*
11503 	 * If we modify physical interface flags, we'll potentially need to
11504 	 * send up two routing socket messages for the changes (one for the
11505 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11506 	 */
11507 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11508 		phyint_flags_modified = B_TRUE;
11509 
11510 	/*
11511 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11512 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11513 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11514 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11515 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11516 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11517 	 * will not be honored.
11518 	 */
11519 	if (turn_on & PHYI_STANDBY) {
11520 		/*
11521 		 * No need to grab ill_g_usesrc_lock here; see the
11522 		 * synchronization notes in ip.c.
11523 		 */
11524 		if (ill->ill_usesrc_grp_next != NULL ||
11525 		    intf_flags & PHYI_INACTIVE)
11526 			return (EINVAL);
11527 		if (!(flags & PHYI_FAILED)) {
11528 			flags |= PHYI_INACTIVE;
11529 			turn_on |= PHYI_INACTIVE;
11530 		}
11531 	}
11532 
11533 	if (turn_off & PHYI_STANDBY) {
11534 		flags &= ~PHYI_INACTIVE;
11535 		turn_off |= PHYI_INACTIVE;
11536 	}
11537 
11538 	/*
11539 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11540 	 * would end up on.
11541 	 */
11542 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11543 	    (PHYI_FAILED | PHYI_INACTIVE))
11544 		return (EINVAL);
11545 
11546 	/*
11547 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11548 	 * status of the interface.
11549 	 */
11550 	if ((turn_on | turn_off) & ILLF_ROUTER)
11551 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11552 
11553 	/*
11554 	 * If the interface is not UP and we are not going to
11555 	 * bring it UP, record the flags and return. When the
11556 	 * interface comes UP later, the right actions will be
11557 	 * taken.
11558 	 */
11559 	if (!(ipif->ipif_flags & IPIF_UP) &&
11560 	    !(turn_on & IPIF_UP)) {
11561 		/* Record new flags in their respective places. */
11562 		mutex_enter(&ill->ill_lock);
11563 		mutex_enter(&ill->ill_phyint->phyint_lock);
11564 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11565 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11566 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11567 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11568 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11569 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11570 		mutex_exit(&ill->ill_lock);
11571 		mutex_exit(&ill->ill_phyint->phyint_lock);
11572 
11573 		/*
11574 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11575 		 * same to the kernel: if any of them has been set by
11576 		 * userland, the interface cannot be used for data traffic.
11577 		 */
11578 		if ((turn_on|turn_off) &
11579 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11580 			ASSERT(!IS_IPMP(ill));
11581 			/*
11582 			 * It's possible the ill is part of an "anonymous"
11583 			 * IPMP group rather than a real group.  In that case,
11584 			 * there are no other interfaces in the group and thus
11585 			 * no need to call ipmp_phyint_refresh_active().
11586 			 */
11587 			if (IS_UNDER_IPMP(ill))
11588 				ipmp_phyint_refresh_active(phyi);
11589 		}
11590 
11591 		if (phyint_flags_modified) {
11592 			if (phyi->phyint_illv4 != NULL) {
11593 				ip_rts_ifmsg(phyi->phyint_illv4->
11594 				    ill_ipif, RTSQ_DEFAULT);
11595 			}
11596 			if (phyi->phyint_illv6 != NULL) {
11597 				ip_rts_ifmsg(phyi->phyint_illv6->
11598 				    ill_ipif, RTSQ_DEFAULT);
11599 			}
11600 		}
11601 		return (0);
11602 	} else if (set_linklocal || zero_source) {
11603 		mutex_enter(&ill->ill_lock);
11604 		if (set_linklocal)
11605 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11606 		if (zero_source)
11607 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11608 		mutex_exit(&ill->ill_lock);
11609 	}
11610 
11611 	/*
11612 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11613 	 * or point-to-point interfaces with an unspecified destination. We do
11614 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11615 	 * have a subnet assigned, which is how in.ndpd currently manages its
11616 	 * onlink prefix list when no addresses are configured with those
11617 	 * prefixes.
11618 	 */
11619 	if (ipif->ipif_isv6 &&
11620 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11621 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11622 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11623 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11624 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11625 		return (EINVAL);
11626 	}
11627 
11628 	/*
11629 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11630 	 * from being brought up.
11631 	 */
11632 	if (!ipif->ipif_isv6 &&
11633 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11634 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11635 		return (EINVAL);
11636 	}
11637 
11638 	/*
11639 	 * The only flag changes that we currently take specific action on are
11640 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11641 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11642 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11643 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11644 	 * of bringing it back up will trigger the address to be moved.
11645 	 */
11646 	if ((turn_on|turn_off) &
11647 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11648 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11649 	    IPIF_NOFAILOVER)) {
11650 		/*
11651 		 * Taking this ipif down, make sure we have
11652 		 * valid net and subnet bcast ire's for other
11653 		 * logical interfaces, if we need them.
11654 		 */
11655 		if (!ipif->ipif_isv6)
11656 			ipif_check_bcast_ires(ipif);
11657 
11658 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11659 		    !(turn_off & IPIF_UP)) {
11660 			if (ipif->ipif_flags & IPIF_UP)
11661 				ill->ill_logical_down = 1;
11662 			turn_on &= ~IPIF_UP;
11663 		}
11664 		err = ipif_down(ipif, q, mp);
11665 		ip1dbg(("ipif_down returns %d err ", err));
11666 		if (err == EINPROGRESS)
11667 			return (err);
11668 		ipif_down_tail(ipif);
11669 	}
11670 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11671 }
11672 
11673 static int
11674 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11675 {
11676 	ill_t	*ill;
11677 	phyint_t *phyi;
11678 	uint64_t turn_on, turn_off;
11679 	uint64_t intf_flags, cantchange_flags;
11680 	boolean_t phyint_flags_modified = B_FALSE;
11681 	int	err = 0;
11682 	boolean_t set_linklocal = B_FALSE;
11683 	boolean_t zero_source = B_FALSE;
11684 
11685 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11686 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11687 
11688 	ASSERT(IAM_WRITER_IPIF(ipif));
11689 
11690 	ill = ipif->ipif_ill;
11691 	phyi = ill->ill_phyint;
11692 
11693 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11694 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11695 	if (IS_IPMP(ill))
11696 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11697 
11698 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11699 	turn_off = intf_flags & turn_on;
11700 	turn_on ^= turn_off;
11701 
11702 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11703 		phyint_flags_modified = B_TRUE;
11704 
11705 	/*
11706 	 * Now we change the flags. Track current value of
11707 	 * other flags in their respective places.
11708 	 */
11709 	mutex_enter(&ill->ill_lock);
11710 	mutex_enter(&phyi->phyint_lock);
11711 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11712 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11713 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11714 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11715 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11716 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11717 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11718 		set_linklocal = B_TRUE;
11719 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11720 	}
11721 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11722 		zero_source = B_TRUE;
11723 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11724 	}
11725 	mutex_exit(&ill->ill_lock);
11726 	mutex_exit(&phyi->phyint_lock);
11727 
11728 	if (set_linklocal)
11729 		(void) ipif_setlinklocal(ipif);
11730 
11731 	if (zero_source)
11732 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11733 	else
11734 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11735 
11736 	/*
11737 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11738 	 * the kernel: if any of them has been set by userland, the interface
11739 	 * cannot be used for data traffic.
11740 	 */
11741 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11742 		ASSERT(!IS_IPMP(ill));
11743 		/*
11744 		 * It's possible the ill is part of an "anonymous" IPMP group
11745 		 * rather than a real group.  In that case, there are no other
11746 		 * interfaces in the group and thus no need for us to call
11747 		 * ipmp_phyint_refresh_active().
11748 		 */
11749 		if (IS_UNDER_IPMP(ill))
11750 			ipmp_phyint_refresh_active(phyi);
11751 	}
11752 
11753 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11754 		/*
11755 		 * XXX ipif_up really does not know whether a phyint flags
11756 		 * was modified or not. So, it sends up information on
11757 		 * only one routing sockets message. As we don't bring up
11758 		 * the interface and also set PHYI_ flags simultaneously
11759 		 * it should be okay.
11760 		 */
11761 		err = ipif_up(ipif, q, mp);
11762 	} else {
11763 		/*
11764 		 * Make sure routing socket sees all changes to the flags.
11765 		 * ipif_up_done* handles this when we use ipif_up.
11766 		 */
11767 		if (phyint_flags_modified) {
11768 			if (phyi->phyint_illv4 != NULL) {
11769 				ip_rts_ifmsg(phyi->phyint_illv4->
11770 				    ill_ipif, RTSQ_DEFAULT);
11771 			}
11772 			if (phyi->phyint_illv6 != NULL) {
11773 				ip_rts_ifmsg(phyi->phyint_illv6->
11774 				    ill_ipif, RTSQ_DEFAULT);
11775 			}
11776 		} else {
11777 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11778 		}
11779 		/*
11780 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11781 		 * this in need_up case.
11782 		 */
11783 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11784 	}
11785 	return (err);
11786 }
11787 
11788 /*
11789  * Restart the flags operation now that the refcounts have dropped to zero.
11790  */
11791 /* ARGSUSED */
11792 int
11793 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11794     ip_ioctl_cmd_t *ipip, void *if_req)
11795 {
11796 	uint64_t flags;
11797 	struct ifreq *ifr = if_req;
11798 	struct lifreq *lifr = if_req;
11799 
11800 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11801 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11802 
11803 	ipif_down_tail(ipif);
11804 	if (ipip->ipi_cmd_type == IF_CMD) {
11805 		/* cast to uint16_t prevents unwanted sign extension */
11806 		flags = (uint16_t)ifr->ifr_flags;
11807 	} else {
11808 		flags = lifr->lifr_flags;
11809 	}
11810 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11811 }
11812 
11813 /*
11814  * Can operate on either a module or a driver queue.
11815  */
11816 /* ARGSUSED */
11817 int
11818 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11819     ip_ioctl_cmd_t *ipip, void *if_req)
11820 {
11821 	/*
11822 	 * Has the flags been set correctly till now ?
11823 	 */
11824 	ill_t *ill = ipif->ipif_ill;
11825 	phyint_t *phyi = ill->ill_phyint;
11826 
11827 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11828 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11829 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11830 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11831 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11832 
11833 	/*
11834 	 * Need a lock since some flags can be set even when there are
11835 	 * references to the ipif.
11836 	 */
11837 	mutex_enter(&ill->ill_lock);
11838 	if (ipip->ipi_cmd_type == IF_CMD) {
11839 		struct ifreq *ifr = (struct ifreq *)if_req;
11840 
11841 		/* Get interface flags (low 16 only). */
11842 		ifr->ifr_flags = ((ipif->ipif_flags |
11843 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11844 	} else {
11845 		struct lifreq *lifr = (struct lifreq *)if_req;
11846 
11847 		/* Get interface flags. */
11848 		lifr->lifr_flags = ipif->ipif_flags |
11849 		    ill->ill_flags | phyi->phyint_flags;
11850 	}
11851 	mutex_exit(&ill->ill_lock);
11852 	return (0);
11853 }
11854 
11855 /* ARGSUSED */
11856 int
11857 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11858     ip_ioctl_cmd_t *ipip, void *if_req)
11859 {
11860 	int mtu;
11861 	int ip_min_mtu;
11862 	struct ifreq	*ifr;
11863 	struct lifreq *lifr;
11864 	ire_t	*ire;
11865 	ip_stack_t *ipst;
11866 
11867 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11868 	    ipif->ipif_id, (void *)ipif));
11869 	if (ipip->ipi_cmd_type == IF_CMD) {
11870 		ifr = (struct ifreq *)if_req;
11871 		mtu = ifr->ifr_metric;
11872 	} else {
11873 		lifr = (struct lifreq *)if_req;
11874 		mtu = lifr->lifr_mtu;
11875 	}
11876 
11877 	if (ipif->ipif_isv6)
11878 		ip_min_mtu = IPV6_MIN_MTU;
11879 	else
11880 		ip_min_mtu = IP_MIN_MTU;
11881 
11882 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11883 		return (EINVAL);
11884 
11885 	/*
11886 	 * Change the MTU size in all relevant ire's.
11887 	 * Mtu change Vs. new ire creation - protocol below.
11888 	 * First change ipif_mtu and the ire_max_frag of the
11889 	 * interface ire. Then do an ire walk and change the
11890 	 * ire_max_frag of all affected ires. During ire_add
11891 	 * under the bucket lock, set the ire_max_frag of the
11892 	 * new ire being created from the ipif/ire from which
11893 	 * it is being derived. If an mtu change happens after
11894 	 * the ire is added, the new ire will be cleaned up.
11895 	 * Conversely if the mtu change happens before the ire
11896 	 * is added, ire_add will see the new value of the mtu.
11897 	 */
11898 	ipif->ipif_mtu = mtu;
11899 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11900 
11901 	if (ipif->ipif_isv6)
11902 		ire = ipif_to_ire_v6(ipif);
11903 	else
11904 		ire = ipif_to_ire(ipif);
11905 	if (ire != NULL) {
11906 		ire->ire_max_frag = ipif->ipif_mtu;
11907 		ire_refrele(ire);
11908 	}
11909 	ipst = ipif->ipif_ill->ill_ipst;
11910 	if (ipif->ipif_flags & IPIF_UP) {
11911 		if (ipif->ipif_isv6)
11912 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11913 			    ipst);
11914 		else
11915 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11916 			    ipst);
11917 	}
11918 	/* Update the MTU in SCTP's list */
11919 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11920 	return (0);
11921 }
11922 
11923 /* Get interface MTU. */
11924 /* ARGSUSED */
11925 int
11926 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11927 	ip_ioctl_cmd_t *ipip, void *if_req)
11928 {
11929 	struct ifreq	*ifr;
11930 	struct lifreq	*lifr;
11931 
11932 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11933 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11934 	if (ipip->ipi_cmd_type == IF_CMD) {
11935 		ifr = (struct ifreq *)if_req;
11936 		ifr->ifr_metric = ipif->ipif_mtu;
11937 	} else {
11938 		lifr = (struct lifreq *)if_req;
11939 		lifr->lifr_mtu = ipif->ipif_mtu;
11940 	}
11941 	return (0);
11942 }
11943 
11944 /* Set interface broadcast address. */
11945 /* ARGSUSED2 */
11946 int
11947 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11948 	ip_ioctl_cmd_t *ipip, void *if_req)
11949 {
11950 	ipaddr_t addr;
11951 	ire_t	*ire;
11952 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11953 
11954 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11955 	    ipif->ipif_id));
11956 
11957 	ASSERT(IAM_WRITER_IPIF(ipif));
11958 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11959 		return (EADDRNOTAVAIL);
11960 
11961 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11962 
11963 	if (sin->sin_family != AF_INET)
11964 		return (EAFNOSUPPORT);
11965 
11966 	addr = sin->sin_addr.s_addr;
11967 	if (ipif->ipif_flags & IPIF_UP) {
11968 		/*
11969 		 * If we are already up, make sure the new
11970 		 * broadcast address makes sense.  If it does,
11971 		 * there should be an IRE for it already.
11972 		 * Don't match on ipif, only on the ill
11973 		 * since we are sharing these now.
11974 		 */
11975 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11976 		    ipif, ALL_ZONES, NULL,
11977 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
11978 		if (ire == NULL) {
11979 			return (EINVAL);
11980 		} else {
11981 			ire_refrele(ire);
11982 		}
11983 	}
11984 	/*
11985 	 * Changing the broadcast addr for this ipif.
11986 	 * Make sure we have valid net and subnet bcast
11987 	 * ire's for other logical interfaces, if needed.
11988 	 */
11989 	if (addr != ipif->ipif_brd_addr)
11990 		ipif_check_bcast_ires(ipif);
11991 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11992 	return (0);
11993 }
11994 
11995 /* Get interface broadcast address. */
11996 /* ARGSUSED */
11997 int
11998 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11999     ip_ioctl_cmd_t *ipip, void *if_req)
12000 {
12001 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12002 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12003 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12004 		return (EADDRNOTAVAIL);
12005 
12006 	/* IPIF_BROADCAST not possible with IPv6 */
12007 	ASSERT(!ipif->ipif_isv6);
12008 	*sin = sin_null;
12009 	sin->sin_family = AF_INET;
12010 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12011 	return (0);
12012 }
12013 
12014 /*
12015  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12016  */
12017 /* ARGSUSED */
12018 int
12019 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12020     ip_ioctl_cmd_t *ipip, void *if_req)
12021 {
12022 	int err = 0;
12023 	in6_addr_t v6mask;
12024 
12025 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12026 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12027 
12028 	ASSERT(IAM_WRITER_IPIF(ipif));
12029 
12030 	if (ipif->ipif_isv6) {
12031 		sin6_t *sin6;
12032 
12033 		if (sin->sin_family != AF_INET6)
12034 			return (EAFNOSUPPORT);
12035 
12036 		sin6 = (sin6_t *)sin;
12037 		v6mask = sin6->sin6_addr;
12038 	} else {
12039 		ipaddr_t mask;
12040 
12041 		if (sin->sin_family != AF_INET)
12042 			return (EAFNOSUPPORT);
12043 
12044 		mask = sin->sin_addr.s_addr;
12045 		V4MASK_TO_V6(mask, v6mask);
12046 	}
12047 
12048 	/*
12049 	 * No big deal if the interface isn't already up, or the mask
12050 	 * isn't really changing, or this is pt-pt.
12051 	 */
12052 	if (!(ipif->ipif_flags & IPIF_UP) ||
12053 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12054 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12055 		ipif->ipif_v6net_mask = v6mask;
12056 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12057 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12058 			    ipif->ipif_v6net_mask,
12059 			    ipif->ipif_v6subnet);
12060 		}
12061 		return (0);
12062 	}
12063 	/*
12064 	 * Make sure we have valid net and subnet broadcast ire's
12065 	 * for the old netmask, if needed by other logical interfaces.
12066 	 */
12067 	if (!ipif->ipif_isv6)
12068 		ipif_check_bcast_ires(ipif);
12069 
12070 	err = ipif_logical_down(ipif, q, mp);
12071 	if (err == EINPROGRESS)
12072 		return (err);
12073 	ipif_down_tail(ipif);
12074 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12075 	return (err);
12076 }
12077 
12078 static int
12079 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12080 {
12081 	in6_addr_t v6mask;
12082 	int err = 0;
12083 
12084 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12085 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12086 
12087 	if (ipif->ipif_isv6) {
12088 		sin6_t *sin6;
12089 
12090 		sin6 = (sin6_t *)sin;
12091 		v6mask = sin6->sin6_addr;
12092 	} else {
12093 		ipaddr_t mask;
12094 
12095 		mask = sin->sin_addr.s_addr;
12096 		V4MASK_TO_V6(mask, v6mask);
12097 	}
12098 
12099 	ipif->ipif_v6net_mask = v6mask;
12100 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12101 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12102 		    ipif->ipif_v6subnet);
12103 	}
12104 	err = ipif_up(ipif, q, mp);
12105 
12106 	if (err == 0 || err == EINPROGRESS) {
12107 		/*
12108 		 * The interface must be DL_BOUND if this packet has to
12109 		 * go out on the wire. Since we only go through a logical
12110 		 * down and are bound with the driver during an internal
12111 		 * down/up that is satisfied.
12112 		 */
12113 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12114 			/* Potentially broadcast an address mask reply. */
12115 			ipif_mask_reply(ipif);
12116 		}
12117 	}
12118 	return (err);
12119 }
12120 
12121 /* ARGSUSED */
12122 int
12123 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12124     ip_ioctl_cmd_t *ipip, void *if_req)
12125 {
12126 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12127 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12128 	ipif_down_tail(ipif);
12129 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12130 }
12131 
12132 /* Get interface net mask. */
12133 /* ARGSUSED */
12134 int
12135 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12136     ip_ioctl_cmd_t *ipip, void *if_req)
12137 {
12138 	struct lifreq *lifr = (struct lifreq *)if_req;
12139 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12140 
12141 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12142 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12143 
12144 	/*
12145 	 * net mask can't change since we have a reference to the ipif.
12146 	 */
12147 	if (ipif->ipif_isv6) {
12148 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12149 		*sin6 = sin6_null;
12150 		sin6->sin6_family = AF_INET6;
12151 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12152 		lifr->lifr_addrlen =
12153 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12154 	} else {
12155 		*sin = sin_null;
12156 		sin->sin_family = AF_INET;
12157 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12158 		if (ipip->ipi_cmd_type == LIF_CMD) {
12159 			lifr->lifr_addrlen =
12160 			    ip_mask_to_plen(ipif->ipif_net_mask);
12161 		}
12162 	}
12163 	return (0);
12164 }
12165 
12166 /* ARGSUSED */
12167 int
12168 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12169     ip_ioctl_cmd_t *ipip, void *if_req)
12170 {
12171 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12172 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12173 
12174 	/*
12175 	 * Since no applications should ever be setting metrics on underlying
12176 	 * interfaces, we explicitly fail to smoke 'em out.
12177 	 */
12178 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12179 		return (EINVAL);
12180 
12181 	/*
12182 	 * Set interface metric.  We don't use this for
12183 	 * anything but we keep track of it in case it is
12184 	 * important to routing applications or such.
12185 	 */
12186 	if (ipip->ipi_cmd_type == IF_CMD) {
12187 		struct ifreq    *ifr;
12188 
12189 		ifr = (struct ifreq *)if_req;
12190 		ipif->ipif_metric = ifr->ifr_metric;
12191 	} else {
12192 		struct lifreq   *lifr;
12193 
12194 		lifr = (struct lifreq *)if_req;
12195 		ipif->ipif_metric = lifr->lifr_metric;
12196 	}
12197 	return (0);
12198 }
12199 
12200 /* ARGSUSED */
12201 int
12202 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12203     ip_ioctl_cmd_t *ipip, void *if_req)
12204 {
12205 	/* Get interface metric. */
12206 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12207 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12208 
12209 	if (ipip->ipi_cmd_type == IF_CMD) {
12210 		struct ifreq    *ifr;
12211 
12212 		ifr = (struct ifreq *)if_req;
12213 		ifr->ifr_metric = ipif->ipif_metric;
12214 	} else {
12215 		struct lifreq   *lifr;
12216 
12217 		lifr = (struct lifreq *)if_req;
12218 		lifr->lifr_metric = ipif->ipif_metric;
12219 	}
12220 
12221 	return (0);
12222 }
12223 
12224 /* ARGSUSED */
12225 int
12226 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12227     ip_ioctl_cmd_t *ipip, void *if_req)
12228 {
12229 
12230 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12231 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12232 	/*
12233 	 * Set the muxid returned from I_PLINK.
12234 	 */
12235 	if (ipip->ipi_cmd_type == IF_CMD) {
12236 		struct ifreq *ifr = (struct ifreq *)if_req;
12237 
12238 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12239 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12240 	} else {
12241 		struct lifreq *lifr = (struct lifreq *)if_req;
12242 
12243 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12244 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12245 	}
12246 	return (0);
12247 }
12248 
12249 /* ARGSUSED */
12250 int
12251 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12252     ip_ioctl_cmd_t *ipip, void *if_req)
12253 {
12254 
12255 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12256 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12257 	/*
12258 	 * Get the muxid saved in ill for I_PUNLINK.
12259 	 */
12260 	if (ipip->ipi_cmd_type == IF_CMD) {
12261 		struct ifreq *ifr = (struct ifreq *)if_req;
12262 
12263 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12264 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12265 	} else {
12266 		struct lifreq *lifr = (struct lifreq *)if_req;
12267 
12268 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12269 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12270 	}
12271 	return (0);
12272 }
12273 
12274 /*
12275  * Set the subnet prefix. Does not modify the broadcast address.
12276  */
12277 /* ARGSUSED */
12278 int
12279 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12280     ip_ioctl_cmd_t *ipip, void *if_req)
12281 {
12282 	int err = 0;
12283 	in6_addr_t v6addr;
12284 	in6_addr_t v6mask;
12285 	boolean_t need_up = B_FALSE;
12286 	int addrlen;
12287 
12288 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12289 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12290 
12291 	ASSERT(IAM_WRITER_IPIF(ipif));
12292 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12293 
12294 	if (ipif->ipif_isv6) {
12295 		sin6_t *sin6;
12296 
12297 		if (sin->sin_family != AF_INET6)
12298 			return (EAFNOSUPPORT);
12299 
12300 		sin6 = (sin6_t *)sin;
12301 		v6addr = sin6->sin6_addr;
12302 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12303 			return (EADDRNOTAVAIL);
12304 	} else {
12305 		ipaddr_t addr;
12306 
12307 		if (sin->sin_family != AF_INET)
12308 			return (EAFNOSUPPORT);
12309 
12310 		addr = sin->sin_addr.s_addr;
12311 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12312 			return (EADDRNOTAVAIL);
12313 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12314 		/* Add 96 bits */
12315 		addrlen += IPV6_ABITS - IP_ABITS;
12316 	}
12317 
12318 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12319 		return (EINVAL);
12320 
12321 	/* Check if bits in the address is set past the mask */
12322 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12323 		return (EINVAL);
12324 
12325 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12326 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12327 		return (0);	/* No change */
12328 
12329 	if (ipif->ipif_flags & IPIF_UP) {
12330 		/*
12331 		 * If the interface is already marked up,
12332 		 * we call ipif_down which will take care
12333 		 * of ditching any IREs that have been set
12334 		 * up based on the old interface address.
12335 		 */
12336 		err = ipif_logical_down(ipif, q, mp);
12337 		if (err == EINPROGRESS)
12338 			return (err);
12339 		ipif_down_tail(ipif);
12340 		need_up = B_TRUE;
12341 	}
12342 
12343 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12344 	return (err);
12345 }
12346 
12347 static int
12348 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12349     queue_t *q, mblk_t *mp, boolean_t need_up)
12350 {
12351 	ill_t	*ill = ipif->ipif_ill;
12352 	int	err = 0;
12353 
12354 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12355 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12356 
12357 	/* Set the new address. */
12358 	mutex_enter(&ill->ill_lock);
12359 	ipif->ipif_v6net_mask = v6mask;
12360 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12361 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12362 		    ipif->ipif_v6subnet);
12363 	}
12364 	mutex_exit(&ill->ill_lock);
12365 
12366 	if (need_up) {
12367 		/*
12368 		 * Now bring the interface back up.  If this
12369 		 * is the only IPIF for the ILL, ipif_up
12370 		 * will have to re-bind to the device, so
12371 		 * we may get back EINPROGRESS, in which
12372 		 * case, this IOCTL will get completed in
12373 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12374 		 */
12375 		err = ipif_up(ipif, q, mp);
12376 		if (err == EINPROGRESS)
12377 			return (err);
12378 	}
12379 	return (err);
12380 }
12381 
12382 /* ARGSUSED */
12383 int
12384 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12385     ip_ioctl_cmd_t *ipip, void *if_req)
12386 {
12387 	int	addrlen;
12388 	in6_addr_t v6addr;
12389 	in6_addr_t v6mask;
12390 	struct lifreq *lifr = (struct lifreq *)if_req;
12391 
12392 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12393 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12394 	ipif_down_tail(ipif);
12395 
12396 	addrlen = lifr->lifr_addrlen;
12397 	if (ipif->ipif_isv6) {
12398 		sin6_t *sin6;
12399 
12400 		sin6 = (sin6_t *)sin;
12401 		v6addr = sin6->sin6_addr;
12402 	} else {
12403 		ipaddr_t addr;
12404 
12405 		addr = sin->sin_addr.s_addr;
12406 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12407 		addrlen += IPV6_ABITS - IP_ABITS;
12408 	}
12409 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12410 
12411 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12412 }
12413 
12414 /* ARGSUSED */
12415 int
12416 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12417     ip_ioctl_cmd_t *ipip, void *if_req)
12418 {
12419 	struct lifreq *lifr = (struct lifreq *)if_req;
12420 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12421 
12422 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12423 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12424 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12425 
12426 	if (ipif->ipif_isv6) {
12427 		*sin6 = sin6_null;
12428 		sin6->sin6_family = AF_INET6;
12429 		sin6->sin6_addr = ipif->ipif_v6subnet;
12430 		lifr->lifr_addrlen =
12431 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12432 	} else {
12433 		*sin = sin_null;
12434 		sin->sin_family = AF_INET;
12435 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12436 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12437 	}
12438 	return (0);
12439 }
12440 
12441 /*
12442  * Set the IPv6 address token.
12443  */
12444 /* ARGSUSED */
12445 int
12446 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12447     ip_ioctl_cmd_t *ipi, void *if_req)
12448 {
12449 	ill_t *ill = ipif->ipif_ill;
12450 	int err;
12451 	in6_addr_t v6addr;
12452 	in6_addr_t v6mask;
12453 	boolean_t need_up = B_FALSE;
12454 	int i;
12455 	sin6_t *sin6 = (sin6_t *)sin;
12456 	struct lifreq *lifr = (struct lifreq *)if_req;
12457 	int addrlen;
12458 
12459 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12460 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12461 	ASSERT(IAM_WRITER_IPIF(ipif));
12462 
12463 	addrlen = lifr->lifr_addrlen;
12464 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12465 	if (ipif->ipif_id != 0)
12466 		return (EINVAL);
12467 
12468 	if (!ipif->ipif_isv6)
12469 		return (EINVAL);
12470 
12471 	if (addrlen > IPV6_ABITS)
12472 		return (EINVAL);
12473 
12474 	v6addr = sin6->sin6_addr;
12475 
12476 	/*
12477 	 * The length of the token is the length from the end.  To get
12478 	 * the proper mask for this, compute the mask of the bits not
12479 	 * in the token; ie. the prefix, and then xor to get the mask.
12480 	 */
12481 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12482 		return (EINVAL);
12483 	for (i = 0; i < 4; i++) {
12484 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12485 	}
12486 
12487 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12488 	    ill->ill_token_length == addrlen)
12489 		return (0);	/* No change */
12490 
12491 	if (ipif->ipif_flags & IPIF_UP) {
12492 		err = ipif_logical_down(ipif, q, mp);
12493 		if (err == EINPROGRESS)
12494 			return (err);
12495 		ipif_down_tail(ipif);
12496 		need_up = B_TRUE;
12497 	}
12498 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12499 	return (err);
12500 }
12501 
12502 static int
12503 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12504     mblk_t *mp, boolean_t need_up)
12505 {
12506 	in6_addr_t v6addr;
12507 	in6_addr_t v6mask;
12508 	ill_t	*ill = ipif->ipif_ill;
12509 	int	i;
12510 	int	err = 0;
12511 
12512 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12513 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12514 	v6addr = sin6->sin6_addr;
12515 	/*
12516 	 * The length of the token is the length from the end.  To get
12517 	 * the proper mask for this, compute the mask of the bits not
12518 	 * in the token; ie. the prefix, and then xor to get the mask.
12519 	 */
12520 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12521 	for (i = 0; i < 4; i++)
12522 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12523 
12524 	mutex_enter(&ill->ill_lock);
12525 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12526 	ill->ill_token_length = addrlen;
12527 	mutex_exit(&ill->ill_lock);
12528 
12529 	if (need_up) {
12530 		/*
12531 		 * Now bring the interface back up.  If this
12532 		 * is the only IPIF for the ILL, ipif_up
12533 		 * will have to re-bind to the device, so
12534 		 * we may get back EINPROGRESS, in which
12535 		 * case, this IOCTL will get completed in
12536 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12537 		 */
12538 		err = ipif_up(ipif, q, mp);
12539 		if (err == EINPROGRESS)
12540 			return (err);
12541 	}
12542 	return (err);
12543 }
12544 
12545 /* ARGSUSED */
12546 int
12547 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12548     ip_ioctl_cmd_t *ipi, void *if_req)
12549 {
12550 	ill_t *ill;
12551 	sin6_t *sin6 = (sin6_t *)sin;
12552 	struct lifreq *lifr = (struct lifreq *)if_req;
12553 
12554 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12555 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12556 	if (ipif->ipif_id != 0)
12557 		return (EINVAL);
12558 
12559 	ill = ipif->ipif_ill;
12560 	if (!ill->ill_isv6)
12561 		return (ENXIO);
12562 
12563 	*sin6 = sin6_null;
12564 	sin6->sin6_family = AF_INET6;
12565 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12566 	sin6->sin6_addr = ill->ill_token;
12567 	lifr->lifr_addrlen = ill->ill_token_length;
12568 	return (0);
12569 }
12570 
12571 /*
12572  * Set (hardware) link specific information that might override
12573  * what was acquired through the DL_INFO_ACK.
12574  * The logic is as follows.
12575  *
12576  * become exclusive
12577  * set CHANGING flag
12578  * change mtu on affected IREs
12579  * clear CHANGING flag
12580  *
12581  * An ire add that occurs before the CHANGING flag is set will have its mtu
12582  * changed by the ip_sioctl_lnkinfo.
12583  *
12584  * During the time the CHANGING flag is set, no new ires will be added to the
12585  * bucket, and ire add will fail (due the CHANGING flag).
12586  *
12587  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12588  * before it is added to the bucket.
12589  *
12590  * Obviously only 1 thread can set the CHANGING flag and we need to become
12591  * exclusive to set the flag.
12592  */
12593 /* ARGSUSED */
12594 int
12595 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12596     ip_ioctl_cmd_t *ipi, void *if_req)
12597 {
12598 	ill_t		*ill = ipif->ipif_ill;
12599 	ipif_t		*nipif;
12600 	int		ip_min_mtu;
12601 	boolean_t	mtu_walk = B_FALSE;
12602 	struct lifreq	*lifr = (struct lifreq *)if_req;
12603 	lif_ifinfo_req_t *lir;
12604 	ire_t		*ire;
12605 
12606 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12607 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12608 	lir = &lifr->lifr_ifinfo;
12609 	ASSERT(IAM_WRITER_IPIF(ipif));
12610 
12611 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12612 	if (ipif->ipif_id != 0)
12613 		return (EINVAL);
12614 
12615 	/* Set interface MTU. */
12616 	if (ipif->ipif_isv6)
12617 		ip_min_mtu = IPV6_MIN_MTU;
12618 	else
12619 		ip_min_mtu = IP_MIN_MTU;
12620 
12621 	/*
12622 	 * Verify values before we set anything. Allow zero to
12623 	 * mean unspecified.
12624 	 */
12625 	if (lir->lir_maxmtu != 0 &&
12626 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12627 	    lir->lir_maxmtu < ip_min_mtu))
12628 		return (EINVAL);
12629 	if (lir->lir_reachtime != 0 &&
12630 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12631 		return (EINVAL);
12632 	if (lir->lir_reachretrans != 0 &&
12633 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12634 		return (EINVAL);
12635 
12636 	mutex_enter(&ill->ill_lock);
12637 	ill->ill_state_flags |= ILL_CHANGING;
12638 	for (nipif = ill->ill_ipif; nipif != NULL;
12639 	    nipif = nipif->ipif_next) {
12640 		nipif->ipif_state_flags |= IPIF_CHANGING;
12641 	}
12642 
12643 	if (lir->lir_maxmtu != 0) {
12644 		ill->ill_max_mtu = lir->lir_maxmtu;
12645 		ill->ill_user_mtu = lir->lir_maxmtu;
12646 		mtu_walk = B_TRUE;
12647 	}
12648 	mutex_exit(&ill->ill_lock);
12649 
12650 	if (lir->lir_reachtime != 0)
12651 		ill->ill_reachable_time = lir->lir_reachtime;
12652 
12653 	if (lir->lir_reachretrans != 0)
12654 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12655 
12656 	ill->ill_max_hops = lir->lir_maxhops;
12657 
12658 	ill->ill_max_buf = ND_MAX_Q;
12659 
12660 	if (mtu_walk) {
12661 		/*
12662 		 * Set the MTU on all ipifs associated with this ill except
12663 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12664 		 */
12665 		for (nipif = ill->ill_ipif; nipif != NULL;
12666 		    nipif = nipif->ipif_next) {
12667 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12668 				continue;
12669 
12670 			nipif->ipif_mtu = ill->ill_max_mtu;
12671 
12672 			if (!(nipif->ipif_flags & IPIF_UP))
12673 				continue;
12674 
12675 			if (nipif->ipif_isv6)
12676 				ire = ipif_to_ire_v6(nipif);
12677 			else
12678 				ire = ipif_to_ire(nipif);
12679 			if (ire != NULL) {
12680 				ire->ire_max_frag = ipif->ipif_mtu;
12681 				ire_refrele(ire);
12682 			}
12683 
12684 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12685 			    nipif, ill);
12686 		}
12687 	}
12688 
12689 	mutex_enter(&ill->ill_lock);
12690 	for (nipif = ill->ill_ipif; nipif != NULL;
12691 	    nipif = nipif->ipif_next) {
12692 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12693 	}
12694 	ILL_UNMARK_CHANGING(ill);
12695 	mutex_exit(&ill->ill_lock);
12696 
12697 	/*
12698 	 * Refresh IPMP meta-interface MTU if necessary.
12699 	 */
12700 	if (IS_UNDER_IPMP(ill))
12701 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12702 
12703 	return (0);
12704 }
12705 
12706 /* ARGSUSED */
12707 int
12708 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12709     ip_ioctl_cmd_t *ipi, void *if_req)
12710 {
12711 	struct lif_ifinfo_req *lir;
12712 	ill_t *ill = ipif->ipif_ill;
12713 
12714 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12715 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12716 	if (ipif->ipif_id != 0)
12717 		return (EINVAL);
12718 
12719 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12720 	lir->lir_maxhops = ill->ill_max_hops;
12721 	lir->lir_reachtime = ill->ill_reachable_time;
12722 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12723 	lir->lir_maxmtu = ill->ill_max_mtu;
12724 
12725 	return (0);
12726 }
12727 
12728 /*
12729  * Return best guess as to the subnet mask for the specified address.
12730  * Based on the subnet masks for all the configured interfaces.
12731  *
12732  * We end up returning a zero mask in the case of default, multicast or
12733  * experimental.
12734  */
12735 static ipaddr_t
12736 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12737 {
12738 	ipaddr_t net_mask;
12739 	ill_t	*ill;
12740 	ipif_t	*ipif;
12741 	ill_walk_context_t ctx;
12742 	ipif_t	*fallback_ipif = NULL;
12743 
12744 	net_mask = ip_net_mask(addr);
12745 	if (net_mask == 0) {
12746 		*ipifp = NULL;
12747 		return (0);
12748 	}
12749 
12750 	/* Let's check to see if this is maybe a local subnet route. */
12751 	/* this function only applies to IPv4 interfaces */
12752 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12753 	ill = ILL_START_WALK_V4(&ctx, ipst);
12754 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12755 		mutex_enter(&ill->ill_lock);
12756 		for (ipif = ill->ill_ipif; ipif != NULL;
12757 		    ipif = ipif->ipif_next) {
12758 			if (!IPIF_CAN_LOOKUP(ipif))
12759 				continue;
12760 			if (!(ipif->ipif_flags & IPIF_UP))
12761 				continue;
12762 			if ((ipif->ipif_subnet & net_mask) ==
12763 			    (addr & net_mask)) {
12764 				/*
12765 				 * Don't trust pt-pt interfaces if there are
12766 				 * other interfaces.
12767 				 */
12768 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12769 					if (fallback_ipif == NULL) {
12770 						ipif_refhold_locked(ipif);
12771 						fallback_ipif = ipif;
12772 					}
12773 					continue;
12774 				}
12775 
12776 				/*
12777 				 * Fine. Just assume the same net mask as the
12778 				 * directly attached subnet interface is using.
12779 				 */
12780 				ipif_refhold_locked(ipif);
12781 				mutex_exit(&ill->ill_lock);
12782 				rw_exit(&ipst->ips_ill_g_lock);
12783 				if (fallback_ipif != NULL)
12784 					ipif_refrele(fallback_ipif);
12785 				*ipifp = ipif;
12786 				return (ipif->ipif_net_mask);
12787 			}
12788 		}
12789 		mutex_exit(&ill->ill_lock);
12790 	}
12791 	rw_exit(&ipst->ips_ill_g_lock);
12792 
12793 	*ipifp = fallback_ipif;
12794 	return ((fallback_ipif != NULL) ?
12795 	    fallback_ipif->ipif_net_mask : net_mask);
12796 }
12797 
12798 /*
12799  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12800  */
12801 static void
12802 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12803 {
12804 	IOCP	iocp;
12805 	ipft_t	*ipft;
12806 	ipllc_t	*ipllc;
12807 	mblk_t	*mp1;
12808 	cred_t	*cr;
12809 	int	error = 0;
12810 	conn_t	*connp;
12811 
12812 	ip1dbg(("ip_wput_ioctl"));
12813 	iocp = (IOCP)mp->b_rptr;
12814 	mp1 = mp->b_cont;
12815 	if (mp1 == NULL) {
12816 		iocp->ioc_error = EINVAL;
12817 		mp->b_datap->db_type = M_IOCNAK;
12818 		iocp->ioc_count = 0;
12819 		qreply(q, mp);
12820 		return;
12821 	}
12822 
12823 	/*
12824 	 * These IOCTLs provide various control capabilities to
12825 	 * upstream agents such as ULPs and processes.	There
12826 	 * are currently two such IOCTLs implemented.  They
12827 	 * are used by TCP to provide update information for
12828 	 * existing IREs and to forcibly delete an IRE for a
12829 	 * host that is not responding, thereby forcing an
12830 	 * attempt at a new route.
12831 	 */
12832 	iocp->ioc_error = EINVAL;
12833 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12834 		goto done;
12835 
12836 	ipllc = (ipllc_t *)mp1->b_rptr;
12837 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12838 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12839 			break;
12840 	}
12841 	/*
12842 	 * prefer credential from mblk over ioctl;
12843 	 * see ip_sioctl_copyin_setup
12844 	 */
12845 	cr = msg_getcred(mp, NULL);
12846 	if (cr == NULL)
12847 		cr = iocp->ioc_cr;
12848 
12849 	/*
12850 	 * Refhold the conn in case the request gets queued up in some lookup
12851 	 */
12852 	ASSERT(CONN_Q(q));
12853 	connp = Q_TO_CONN(q);
12854 	CONN_INC_REF(connp);
12855 	if (ipft->ipft_pfi &&
12856 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12857 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12858 		error = (*ipft->ipft_pfi)(q,
12859 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12860 	}
12861 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12862 		/*
12863 		 * CONN_OPER_PENDING_DONE happens in the function called
12864 		 * through ipft_pfi above.
12865 		 */
12866 		return;
12867 	}
12868 
12869 	CONN_OPER_PENDING_DONE(connp);
12870 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12871 		freemsg(mp);
12872 		return;
12873 	}
12874 	iocp->ioc_error = error;
12875 
12876 done:
12877 	mp->b_datap->db_type = M_IOCACK;
12878 	if (iocp->ioc_error)
12879 		iocp->ioc_count = 0;
12880 	qreply(q, mp);
12881 }
12882 
12883 /*
12884  * Lookup an ipif using the sequence id (ipif_seqid)
12885  */
12886 ipif_t *
12887 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12888 {
12889 	ipif_t *ipif;
12890 
12891 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12892 
12893 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12894 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12895 			return (ipif);
12896 	}
12897 	return (NULL);
12898 }
12899 
12900 /*
12901  * Assign a unique id for the ipif. This is used later when we send
12902  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12903  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12904  * IRE is added, we verify that ipif has not disappeared.
12905  */
12906 
12907 static void
12908 ipif_assign_seqid(ipif_t *ipif)
12909 {
12910 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12911 
12912 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
12913 }
12914 
12915 /*
12916  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
12917  * administratively down (i.e., no DAD), of the same type, and locked.  Note
12918  * that the clone is complete -- including the seqid -- and the expectation is
12919  * that the caller will either free or overwrite `sipif' before it's unlocked.
12920  */
12921 static void
12922 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
12923 {
12924 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
12925 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
12926 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12927 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
12928 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
12929 	ASSERT(sipif->ipif_arp_del_mp == NULL);
12930 	ASSERT(dipif->ipif_arp_del_mp == NULL);
12931 	ASSERT(sipif->ipif_igmp_rpt == NULL);
12932 	ASSERT(dipif->ipif_igmp_rpt == NULL);
12933 	ASSERT(sipif->ipif_multicast_up == 0);
12934 	ASSERT(dipif->ipif_multicast_up == 0);
12935 	ASSERT(sipif->ipif_joined_allhosts == 0);
12936 	ASSERT(dipif->ipif_joined_allhosts == 0);
12937 
12938 	dipif->ipif_mtu = sipif->ipif_mtu;
12939 	dipif->ipif_flags = sipif->ipif_flags;
12940 	dipif->ipif_metric = sipif->ipif_metric;
12941 	dipif->ipif_zoneid = sipif->ipif_zoneid;
12942 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
12943 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
12944 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
12945 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
12946 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
12947 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
12948 
12949 	/*
12950 	 * While dipif is down right now, it might've been up before.  Since
12951 	 * it's changing identity, its packet counters need to be reset.
12952 	 */
12953 	dipif->ipif_ib_pkt_count = 0;
12954 	dipif->ipif_ob_pkt_count = 0;
12955 	dipif->ipif_fo_pkt_count = 0;
12956 
12957 	/*
12958 	 * As per the comment atop the function, we assume that these sipif
12959 	 * fields will be changed before sipif is unlocked.
12960 	 */
12961 	dipif->ipif_seqid = sipif->ipif_seqid;
12962 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
12963 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
12964 	dipif->ipif_state_flags = sipif->ipif_state_flags;
12965 }
12966 
12967 /*
12968  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
12969  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
12970  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
12971  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
12972  * down (i.e., no DAD), of the same type, and unlocked.
12973  */
12974 static void
12975 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
12976 {
12977 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
12978 	ipxop_t *ipx = ipsq->ipsq_xop;
12979 
12980 	ASSERT(sipif != dipif);
12981 	ASSERT(sipif != virgipif);
12982 
12983 	/*
12984 	 * Grab all of the locks that protect the ipif in a defined order.
12985 	 */
12986 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
12987 	if (sipif > dipif) {
12988 		mutex_enter(&sipif->ipif_saved_ire_lock);
12989 		mutex_enter(&dipif->ipif_saved_ire_lock);
12990 	} else {
12991 		mutex_enter(&dipif->ipif_saved_ire_lock);
12992 		mutex_enter(&sipif->ipif_saved_ire_lock);
12993 	}
12994 
12995 	ipif_clone(sipif, dipif);
12996 	if (virgipif != NULL) {
12997 		ipif_clone(virgipif, sipif);
12998 		mi_free(virgipif);
12999 	}
13000 
13001 	mutex_exit(&sipif->ipif_saved_ire_lock);
13002 	mutex_exit(&dipif->ipif_saved_ire_lock);
13003 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13004 
13005 	/*
13006 	 * Transfer ownership of the current xop, if necessary.
13007 	 */
13008 	if (ipx->ipx_current_ipif == sipif) {
13009 		ASSERT(ipx->ipx_pending_ipif == NULL);
13010 		mutex_enter(&ipx->ipx_lock);
13011 		ipx->ipx_current_ipif = dipif;
13012 		mutex_exit(&ipx->ipx_lock);
13013 	}
13014 
13015 	if (virgipif == NULL)
13016 		mi_free(sipif);
13017 }
13018 
13019 /*
13020  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13021  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13022  * be inserted into the first space available in the list. The value of
13023  * ipif_id will then be set to the appropriate value for its position.
13024  */
13025 static int
13026 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13027 {
13028 	ill_t *ill;
13029 	ipif_t *tipif;
13030 	ipif_t **tipifp;
13031 	int id;
13032 	ip_stack_t	*ipst;
13033 
13034 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13035 	    IAM_WRITER_IPIF(ipif));
13036 
13037 	ill = ipif->ipif_ill;
13038 	ASSERT(ill != NULL);
13039 	ipst = ill->ill_ipst;
13040 
13041 	/*
13042 	 * In the case of lo0:0 we already hold the ill_g_lock.
13043 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13044 	 * ipif_insert.
13045 	 */
13046 	if (acquire_g_lock)
13047 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13048 	mutex_enter(&ill->ill_lock);
13049 	id = ipif->ipif_id;
13050 	tipifp = &(ill->ill_ipif);
13051 	if (id == -1) {	/* need to find a real id */
13052 		id = 0;
13053 		while ((tipif = *tipifp) != NULL) {
13054 			ASSERT(tipif->ipif_id >= id);
13055 			if (tipif->ipif_id != id)
13056 				break; /* non-consecutive id */
13057 			id++;
13058 			tipifp = &(tipif->ipif_next);
13059 		}
13060 		/* limit number of logical interfaces */
13061 		if (id >= ipst->ips_ip_addrs_per_if) {
13062 			mutex_exit(&ill->ill_lock);
13063 			if (acquire_g_lock)
13064 				rw_exit(&ipst->ips_ill_g_lock);
13065 			return (-1);
13066 		}
13067 		ipif->ipif_id = id; /* assign new id */
13068 	} else if (id < ipst->ips_ip_addrs_per_if) {
13069 		/* we have a real id; insert ipif in the right place */
13070 		while ((tipif = *tipifp) != NULL) {
13071 			ASSERT(tipif->ipif_id != id);
13072 			if (tipif->ipif_id > id)
13073 				break; /* found correct location */
13074 			tipifp = &(tipif->ipif_next);
13075 		}
13076 	} else {
13077 		mutex_exit(&ill->ill_lock);
13078 		if (acquire_g_lock)
13079 			rw_exit(&ipst->ips_ill_g_lock);
13080 		return (-1);
13081 	}
13082 
13083 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13084 
13085 	ipif->ipif_next = tipif;
13086 	*tipifp = ipif;
13087 	mutex_exit(&ill->ill_lock);
13088 	if (acquire_g_lock)
13089 		rw_exit(&ipst->ips_ill_g_lock);
13090 
13091 	return (0);
13092 }
13093 
13094 static void
13095 ipif_remove(ipif_t *ipif)
13096 {
13097 	ipif_t	**ipifp;
13098 	ill_t	*ill = ipif->ipif_ill;
13099 
13100 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13101 
13102 	mutex_enter(&ill->ill_lock);
13103 	ipifp = &ill->ill_ipif;
13104 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13105 		if (*ipifp == ipif) {
13106 			*ipifp = ipif->ipif_next;
13107 			break;
13108 		}
13109 	}
13110 	mutex_exit(&ill->ill_lock);
13111 }
13112 
13113 /*
13114  * Allocate and initialize a new interface control structure.  (Always
13115  * called as writer.)
13116  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13117  * is not part of the global linked list of ills. ipif_seqid is unique
13118  * in the system and to preserve the uniqueness, it is assigned only
13119  * when ill becomes part of the global list. At that point ill will
13120  * have a name. If it doesn't get assigned here, it will get assigned
13121  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13122  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13123  * the interface flags or any other information from the DL_INFO_ACK for
13124  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13125  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13126  * second DL_INFO_ACK comes in from the driver.
13127  */
13128 static ipif_t *
13129 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13130     boolean_t insert)
13131 {
13132 	ipif_t	*ipif;
13133 	phyint_t *phyi = ill->ill_phyint;
13134 	ip_stack_t *ipst = ill->ill_ipst;
13135 
13136 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13137 	    ill->ill_name, id, (void *)ill));
13138 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13139 
13140 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13141 		return (NULL);
13142 	*ipif = ipif_zero;	/* start clean */
13143 
13144 	ipif->ipif_ill = ill;
13145 	ipif->ipif_id = id;	/* could be -1 */
13146 	/*
13147 	 * Inherit the zoneid from the ill; for the shared stack instance
13148 	 * this is always the global zone
13149 	 */
13150 	ipif->ipif_zoneid = ill->ill_zoneid;
13151 
13152 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13153 
13154 	ipif->ipif_refcnt = 0;
13155 	ipif->ipif_saved_ire_cnt = 0;
13156 
13157 	if (insert) {
13158 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13159 			mi_free(ipif);
13160 			return (NULL);
13161 		}
13162 		/* -1 id should have been replaced by real id */
13163 		id = ipif->ipif_id;
13164 		ASSERT(id >= 0);
13165 	}
13166 
13167 	if (ill->ill_name[0] != '\0')
13168 		ipif_assign_seqid(ipif);
13169 
13170 	/*
13171 	 * If this is ipif zero, configure ill/phyint-wide information.
13172 	 * Defer most configuration until we're guaranteed we're attached.
13173 	 */
13174 	if (id == 0) {
13175 		if (ill->ill_mactype == SUNW_DL_IPMP) {
13176 			/*
13177 			 * Set PHYI_IPMP and also set PHYI_FAILED since there
13178 			 * are no active interfaces.  Similarly, PHYI_RUNNING
13179 			 * isn't set until the group has an active interface.
13180 			 */
13181 			mutex_enter(&phyi->phyint_lock);
13182 			phyi->phyint_flags |= (PHYI_IPMP | PHYI_FAILED);
13183 			mutex_exit(&phyi->phyint_lock);
13184 
13185 			/*
13186 			 * Create the illgrp (which must not exist yet because
13187 			 * the zeroth ipif is created once per ill).  However,
13188 			 * do not not link it to the ipmp_grp_t until I_PLINK
13189 			 * is called; see ip_sioctl_plink_ipmp() for details.
13190 			 */
13191 			if (ipmp_illgrp_create(ill) == NULL) {
13192 				if (insert) {
13193 					rw_enter(&ipst->ips_ill_g_lock,
13194 					    RW_WRITER);
13195 					ipif_remove(ipif);
13196 					rw_exit(&ipst->ips_ill_g_lock);
13197 				}
13198 				mi_free(ipif);
13199 				return (NULL);
13200 			}
13201 		} else {
13202 			/*
13203 			 * By default, PHYI_RUNNING is set when the zeroth
13204 			 * ipif is created.  For other ipifs, we don't touch
13205 			 * it since DLPI notifications may have changed it.
13206 			 */
13207 			mutex_enter(&phyi->phyint_lock);
13208 			phyi->phyint_flags |= PHYI_RUNNING;
13209 			mutex_exit(&phyi->phyint_lock);
13210 		}
13211 	}
13212 
13213 	/*
13214 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13215 	 * The ipif is still not up and can't be looked up until the
13216 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13217 	 */
13218 	mutex_enter(&ill->ill_lock);
13219 	mutex_enter(&phyi->phyint_lock);
13220 
13221 	ipif->ipif_ire_type = ire_type;
13222 
13223 	if (ipif->ipif_isv6) {
13224 		ill->ill_flags |= ILLF_IPV6;
13225 	} else {
13226 		ipaddr_t inaddr_any = INADDR_ANY;
13227 
13228 		ill->ill_flags |= ILLF_IPV4;
13229 
13230 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13231 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13232 		    &ipif->ipif_v6lcl_addr);
13233 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13234 		    &ipif->ipif_v6src_addr);
13235 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13236 		    &ipif->ipif_v6subnet);
13237 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13238 		    &ipif->ipif_v6net_mask);
13239 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13240 		    &ipif->ipif_v6brd_addr);
13241 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13242 		    &ipif->ipif_v6pp_dst_addr);
13243 	}
13244 
13245 	/*
13246 	 * Don't set the interface flags etc. now, will do it in
13247 	 * ip_ll_subnet_defaults.
13248 	 */
13249 	if (!initialize)
13250 		goto out;
13251 
13252 	ipif->ipif_mtu = ill->ill_max_mtu;
13253 
13254 	/*
13255 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13256 	 * with no underlying interfaces (and thus an unknown broadcast
13257 	 * address length), but all interfaces that can be placed into an IPMP
13258 	 * group are required to be broadcast-capable.
13259 	 */
13260 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13261 		/*
13262 		 * Later detect lack of DLPI driver multicast
13263 		 * capability by catching DL_ENABMULTI errors in
13264 		 * ip_rput_dlpi.
13265 		 */
13266 		ill->ill_flags |= ILLF_MULTICAST;
13267 		if (!ipif->ipif_isv6)
13268 			ipif->ipif_flags |= IPIF_BROADCAST;
13269 	} else {
13270 		if (ill->ill_net_type != IRE_LOOPBACK) {
13271 			if (ipif->ipif_isv6)
13272 				/*
13273 				 * Note: xresolv interfaces will eventually need
13274 				 * NOARP set here as well, but that will require
13275 				 * those external resolvers to have some
13276 				 * knowledge of that flag and act appropriately.
13277 				 * Not to be changed at present.
13278 				 */
13279 				ill->ill_flags |= ILLF_NONUD;
13280 			else
13281 				ill->ill_flags |= ILLF_NOARP;
13282 		}
13283 		if (ill->ill_phys_addr_length == 0) {
13284 			if (ill->ill_mactype == SUNW_DL_VNI) {
13285 				ipif->ipif_flags |= IPIF_NOXMIT;
13286 				phyi->phyint_flags |= PHYI_VIRTUAL;
13287 			} else {
13288 				/* pt-pt supports multicast. */
13289 				ill->ill_flags |= ILLF_MULTICAST;
13290 				if (ill->ill_net_type == IRE_LOOPBACK) {
13291 					phyi->phyint_flags |=
13292 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13293 				} else {
13294 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13295 				}
13296 			}
13297 		}
13298 	}
13299 out:
13300 	mutex_exit(&phyi->phyint_lock);
13301 	mutex_exit(&ill->ill_lock);
13302 	return (ipif);
13303 }
13304 
13305 /*
13306  * If appropriate, send a message up to the resolver delete the entry
13307  * for the address of this interface which is going out of business.
13308  * (Always called as writer).
13309  *
13310  * NOTE : We need to check for NULL mps as some of the fields are
13311  *	  initialized only for some interface types. See ipif_resolver_up()
13312  *	  for details.
13313  */
13314 void
13315 ipif_resolver_down(ipif_t *ipif)
13316 {
13317 	mblk_t	*mp;
13318 	ill_t	*ill = ipif->ipif_ill;
13319 
13320 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13321 	ASSERT(IAM_WRITER_IPIF(ipif));
13322 
13323 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13324 		return;
13325 
13326 	/* Delete the mapping for the local address */
13327 	mp = ipif->ipif_arp_del_mp;
13328 	if (mp != NULL) {
13329 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13330 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13331 		putnext(ill->ill_rq, mp);
13332 		ipif->ipif_arp_del_mp = NULL;
13333 	}
13334 
13335 	/*
13336 	 * Make IPMP aware of the deleted data address.
13337 	 */
13338 	if (IS_IPMP(ill))
13339 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13340 
13341 	/*
13342 	 * If this is the last ipif that is going down and there are no
13343 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13344 	 * clean up ARP completely.
13345 	 */
13346 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13347 		/*
13348 		 * If this was the last ipif on an IPMP interface, purge any
13349 		 * IPMP ARP entries associated with it.
13350 		 */
13351 		if (IS_IPMP(ill))
13352 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13353 
13354 		/* Send up AR_INTERFACE_DOWN message */
13355 		mp = ill->ill_arp_down_mp;
13356 		if (mp != NULL) {
13357 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13358 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13359 			    ipif->ipif_id));
13360 			putnext(ill->ill_rq, mp);
13361 			ill->ill_arp_down_mp = NULL;
13362 		}
13363 
13364 		/* Tell ARP to delete the multicast mappings */
13365 		mp = ill->ill_arp_del_mapping_mp;
13366 		if (mp != NULL) {
13367 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13368 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13369 			    ipif->ipif_id));
13370 			putnext(ill->ill_rq, mp);
13371 			ill->ill_arp_del_mapping_mp = NULL;
13372 		}
13373 	}
13374 }
13375 
13376 /*
13377  * Set up the multicast mappings for `ipif' in ARP.  If `arp_add_mapping_mp'
13378  * is non-NULL, then upon success it will contain an mblk that can be passed
13379  * to ARP to create the mapping.  Otherwise, if it's NULL, upon success ARP
13380  * will have already been notified to create the mapping.  Returns zero on
13381  * success, -1 upon failure.
13382  */
13383 int
13384 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13385 {
13386 	mblk_t	*del_mp = NULL;
13387 	mblk_t *add_mp = NULL;
13388 	mblk_t *mp;
13389 	ill_t	*ill = ipif->ipif_ill;
13390 	phyint_t *phyi = ill->ill_phyint;
13391 	ipaddr_t addr, mask, extract_mask = 0;
13392 	arma_t	*arma;
13393 	uint8_t *maddr, *bphys_addr;
13394 	uint32_t hw_start;
13395 	dl_unitdata_req_t *dlur;
13396 
13397 	ASSERT(IAM_WRITER_IPIF(ipif));
13398 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13399 		return (0);
13400 
13401 	/*
13402 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13403 	 * and instead use the ones on the underlying interfaces.
13404 	 */
13405 	if (IS_IPMP(ill))
13406 		return (0);
13407 
13408 	/*
13409 	 * Delete the existing mapping from ARP.  Normally, ipif_down() ->
13410 	 * ipif_resolver_down() will send this up to ARP, but it may be that
13411 	 * we are enabling PHYI_MULTI_BCAST via ip_rput_dlpi_writer().
13412 	 */
13413 	mp = ill->ill_arp_del_mapping_mp;
13414 	if (mp != NULL) {
13415 		ip1dbg(("ipif_arp_setup_multicast: arp cmd %x for %s:%u\n",
13416 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13417 		putnext(ill->ill_rq, mp);
13418 		ill->ill_arp_del_mapping_mp = NULL;
13419 	}
13420 
13421 	if (arp_add_mapping_mp != NULL)
13422 		*arp_add_mapping_mp = NULL;
13423 
13424 	/*
13425 	 * Check that the address is not to long for the constant
13426 	 * length reserved in the template arma_t.
13427 	 */
13428 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13429 		return (-1);
13430 
13431 	/* Add mapping mblk */
13432 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13433 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13434 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13435 	    (caddr_t)&addr);
13436 	if (add_mp == NULL)
13437 		return (-1);
13438 	arma = (arma_t *)add_mp->b_rptr;
13439 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13440 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13441 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13442 
13443 	/*
13444 	 * Determine the broadcast address.
13445 	 */
13446 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13447 	if (ill->ill_sap_length < 0)
13448 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13449 	else
13450 		bphys_addr = (uchar_t *)dlur +
13451 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13452 	/*
13453 	 * Check PHYI_MULTI_BCAST and length of physical
13454 	 * address to determine if we use the mapping or the
13455 	 * broadcast address.
13456 	 */
13457 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13458 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13459 		    bphys_addr, maddr, &hw_start, &extract_mask))
13460 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13461 
13462 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13463 	    (ill->ill_flags & ILLF_MULTICAST)) {
13464 		/* Make sure this will not match the "exact" entry. */
13465 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13466 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13467 		    (caddr_t)&addr);
13468 		if (del_mp == NULL) {
13469 			freemsg(add_mp);
13470 			return (-1);
13471 		}
13472 		bcopy(&extract_mask, (char *)arma +
13473 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13474 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13475 			/* Use link-layer broadcast address for MULTI_BCAST */
13476 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13477 			ip2dbg(("ipif_arp_setup_multicast: adding"
13478 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13479 		} else {
13480 			arma->arma_hw_mapping_start = hw_start;
13481 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13482 			    " ARP setup for %s\n", ill->ill_name));
13483 		}
13484 	} else {
13485 		freemsg(add_mp);
13486 		ASSERT(del_mp == NULL);
13487 		/* It is neither MULTICAST nor MULTI_BCAST */
13488 		return (0);
13489 	}
13490 	ASSERT(add_mp != NULL && del_mp != NULL);
13491 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13492 	ill->ill_arp_del_mapping_mp = del_mp;
13493 	if (arp_add_mapping_mp != NULL) {
13494 		/* The caller just wants the mblks allocated */
13495 		*arp_add_mapping_mp = add_mp;
13496 	} else {
13497 		/* The caller wants us to send it to arp */
13498 		putnext(ill->ill_rq, add_mp);
13499 	}
13500 	return (0);
13501 }
13502 
13503 /*
13504  * Get the resolver set up for a new IP address.  (Always called as writer.)
13505  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13506  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13507  *
13508  * The enumerated value res_act tunes the behavior:
13509  * 	* Res_act_initial: set up all the resolver structures for a new
13510  *	  IP address.
13511  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13512  *	  ARP message in defense of the address.
13513  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13514  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13515  *
13516  * Returns zero on success, or an errno upon failure.
13517  */
13518 int
13519 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13520 {
13521 	mblk_t	*arp_up_mp = NULL;
13522 	mblk_t	*arp_down_mp = NULL;
13523 	mblk_t	*arp_add_mp = NULL;
13524 	mblk_t	*arp_del_mp = NULL;
13525 	mblk_t	*arp_add_mapping_mp = NULL;
13526 	mblk_t	*arp_del_mapping_mp = NULL;
13527 	ill_t	*ill = ipif->ipif_ill;
13528 	int	err = ENOMEM;
13529 	boolean_t added_ipif = B_FALSE;
13530 	boolean_t publish;
13531 	boolean_t was_dup;
13532 
13533 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13534 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13535 	ASSERT(IAM_WRITER_IPIF(ipif));
13536 
13537 	was_dup = B_FALSE;
13538 	if (res_act == Res_act_initial) {
13539 		ipif->ipif_addr_ready = 0;
13540 		/*
13541 		 * We're bringing an interface up here.  There's no way that we
13542 		 * should need to shut down ARP now.
13543 		 */
13544 		mutex_enter(&ill->ill_lock);
13545 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13546 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13547 			ill->ill_ipif_dup_count--;
13548 			was_dup = B_TRUE;
13549 		}
13550 		mutex_exit(&ill->ill_lock);
13551 	}
13552 	if (ipif->ipif_recovery_id != 0)
13553 		(void) untimeout(ipif->ipif_recovery_id);
13554 	ipif->ipif_recovery_id = 0;
13555 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13556 		ipif->ipif_addr_ready = 1;
13557 		return (0);
13558 	}
13559 	/* NDP will set the ipif_addr_ready flag when it's ready */
13560 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13561 		return (0);
13562 
13563 	if (ill->ill_isv6) {
13564 		/*
13565 		 * External resolver for IPv6
13566 		 */
13567 		ASSERT(res_act == Res_act_initial);
13568 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13569 	} else {
13570 		/*
13571 		 * IPv4 arp case. If the ARP stream has already started
13572 		 * closing, fail this request for ARP bringup. Else
13573 		 * record the fact that an ARP bringup is pending.
13574 		 */
13575 		mutex_enter(&ill->ill_lock);
13576 		if (ill->ill_arp_closing) {
13577 			mutex_exit(&ill->ill_lock);
13578 			err = EINVAL;
13579 			goto failed;
13580 		} else {
13581 			if (ill->ill_ipif_up_count == 0 &&
13582 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13583 				ill->ill_arp_bringup_pending = 1;
13584 			mutex_exit(&ill->ill_lock);
13585 		}
13586 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13587 	}
13588 
13589 	if (IS_IPMP(ill) && publish) {
13590 		/*
13591 		 * If we're here via ipif_up(), then the ipif won't be bound
13592 		 * yet -- add it to the group, which will bind it if possible.
13593 		 * (We would add it in ipif_up(), but deleting on failure
13594 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13595 		 * then the ipif has already been added to the group and we
13596 		 * just need to use the binding.
13597 		 */
13598 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13599 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13600 				/*
13601 				 * We couldn't bind the ipif to an ill yet,
13602 				 * so we have nothing to publish.
13603 				 */
13604 				publish = B_FALSE;
13605 			}
13606 			added_ipif = B_TRUE;
13607 		}
13608 	}
13609 
13610 	/*
13611 	 * Add an entry for the local address in ARP only if it
13612 	 * is not UNNUMBERED and it is suitable for publishing.
13613 	 */
13614 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13615 		if (res_act == Res_act_defend) {
13616 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13617 			if (arp_add_mp == NULL)
13618 				goto failed;
13619 			/*
13620 			 * If we're just defending our address now, then
13621 			 * there's no need to set up ARP multicast mappings.
13622 			 * The publish command is enough.
13623 			 */
13624 			goto done;
13625 		}
13626 
13627 		/*
13628 		 * Allocate an ARP add message and an ARP delete message (the
13629 		 * latter is saved for use when the address goes down).
13630 		 */
13631 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13632 			goto failed;
13633 
13634 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13635 			goto failed;
13636 
13637 		if (res_act != Res_act_initial)
13638 			goto arp_setup_multicast;
13639 	} else {
13640 		if (res_act != Res_act_initial)
13641 			goto done;
13642 	}
13643 	/*
13644 	 * Need to bring up ARP or setup multicast mapping only
13645 	 * when the first interface is coming UP.
13646 	 */
13647 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13648 		goto done;
13649 
13650 	/*
13651 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13652 	 */
13653 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13654 	if (arp_down_mp == NULL)
13655 		goto failed;
13656 
13657 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13658 	if (arp_up_mp == NULL)
13659 		goto failed;
13660 
13661 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13662 		goto done;
13663 
13664 arp_setup_multicast:
13665 	/*
13666 	 * Setup the multicast mappings. This function initializes
13667 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13668 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13669 	 */
13670 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13671 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13672 		if (err != 0)
13673 			goto failed;
13674 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13675 		ASSERT(arp_add_mapping_mp != NULL);
13676 	}
13677 done:
13678 	if (arp_up_mp != NULL) {
13679 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13680 		    ill->ill_name, ipif->ipif_id));
13681 		putnext(ill->ill_rq, arp_up_mp);
13682 		arp_up_mp = NULL;
13683 	}
13684 	if (arp_add_mp != NULL) {
13685 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13686 		    ill->ill_name, ipif->ipif_id));
13687 		/*
13688 		 * If it's an extended ARP implementation, then we'll wait to
13689 		 * hear that DAD has finished before using the interface.
13690 		 */
13691 		if (!ill->ill_arp_extend)
13692 			ipif->ipif_addr_ready = 1;
13693 		putnext(ill->ill_rq, arp_add_mp);
13694 		arp_add_mp = NULL;
13695 	} else {
13696 		ipif->ipif_addr_ready = 1;
13697 	}
13698 	if (arp_add_mapping_mp != NULL) {
13699 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13700 		    ill->ill_name, ipif->ipif_id));
13701 		putnext(ill->ill_rq, arp_add_mapping_mp);
13702 		arp_add_mapping_mp = NULL;
13703 	}
13704 
13705 	if (res_act == Res_act_initial) {
13706 		if (ill->ill_flags & ILLF_NOARP)
13707 			err = ill_arp_off(ill);
13708 		else
13709 			err = ill_arp_on(ill);
13710 		if (err != 0) {
13711 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13712 			    err));
13713 			goto failed;
13714 		}
13715 	}
13716 
13717 	if (arp_del_mp != NULL) {
13718 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13719 		ipif->ipif_arp_del_mp = arp_del_mp;
13720 	}
13721 	if (arp_down_mp != NULL) {
13722 		ASSERT(ill->ill_arp_down_mp == NULL);
13723 		ill->ill_arp_down_mp = arp_down_mp;
13724 	}
13725 	if (arp_del_mapping_mp != NULL) {
13726 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13727 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13728 	}
13729 
13730 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13731 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13732 failed:
13733 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13734 	if (added_ipif)
13735 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13736 	freemsg(arp_add_mp);
13737 	freemsg(arp_del_mp);
13738 	freemsg(arp_add_mapping_mp);
13739 	freemsg(arp_up_mp);
13740 	freemsg(arp_down_mp);
13741 	ill->ill_arp_bringup_pending = 0;
13742 	return (err);
13743 }
13744 
13745 /*
13746  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13747  * just gone back up.
13748  */
13749 static void
13750 ipif_arp_start_dad(ipif_t *ipif)
13751 {
13752 	ill_t *ill = ipif->ipif_ill;
13753 	mblk_t *arp_add_mp;
13754 
13755 	/* ACE_F_UNVERIFIED restarts DAD */
13756 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13757 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13758 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13759 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13760 		/*
13761 		 * If we can't contact ARP for some reason, that's not really a
13762 		 * problem.  Just send out the routing socket notification that
13763 		 * DAD completion would have done, and continue.
13764 		 */
13765 		ipif_mask_reply(ipif);
13766 		ipif_up_notify(ipif);
13767 		ipif->ipif_addr_ready = 1;
13768 		return;
13769 	}
13770 
13771 	putnext(ill->ill_rq, arp_add_mp);
13772 }
13773 
13774 static void
13775 ipif_ndp_start_dad(ipif_t *ipif)
13776 {
13777 	nce_t *nce;
13778 
13779 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13780 	    B_FALSE);
13781 	if (nce == NULL)
13782 		return;
13783 
13784 	if (!ndp_restart_dad(nce)) {
13785 		/*
13786 		 * If we can't restart DAD for some reason, that's not really a
13787 		 * problem.  Just send out the routing socket notification that
13788 		 * DAD completion would have done, and continue.
13789 		 */
13790 		ipif_up_notify(ipif);
13791 		ipif->ipif_addr_ready = 1;
13792 	}
13793 	NCE_REFRELE(nce);
13794 }
13795 
13796 /*
13797  * Restart duplicate address detection on all interfaces on the given ill.
13798  *
13799  * This is called when an interface transitions from down to up
13800  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13801  *
13802  * Note that since the underlying physical link has transitioned, we must cause
13803  * at least one routing socket message to be sent here, either via DAD
13804  * completion or just by default on the first ipif.  (If we don't do this, then
13805  * in.mpathd will see long delays when doing link-based failure recovery.)
13806  */
13807 void
13808 ill_restart_dad(ill_t *ill, boolean_t went_up)
13809 {
13810 	ipif_t *ipif;
13811 
13812 	if (ill == NULL)
13813 		return;
13814 
13815 	/*
13816 	 * If layer two doesn't support duplicate address detection, then just
13817 	 * send the routing socket message now and be done with it.
13818 	 */
13819 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13820 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13821 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13822 		return;
13823 	}
13824 
13825 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13826 		if (went_up) {
13827 			if (ipif->ipif_flags & IPIF_UP) {
13828 				if (ill->ill_isv6)
13829 					ipif_ndp_start_dad(ipif);
13830 				else
13831 					ipif_arp_start_dad(ipif);
13832 			} else if (ill->ill_isv6 &&
13833 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13834 				/*
13835 				 * For IPv4, the ARP module itself will
13836 				 * automatically start the DAD process when it
13837 				 * sees DL_NOTE_LINK_UP.  We respond to the
13838 				 * AR_CN_READY at the completion of that task.
13839 				 * For IPv6, we must kick off the bring-up
13840 				 * process now.
13841 				 */
13842 				ndp_do_recovery(ipif);
13843 			} else {
13844 				/*
13845 				 * Unfortunately, the first ipif is "special"
13846 				 * and represents the underlying ill in the
13847 				 * routing socket messages.  Thus, when this
13848 				 * one ipif is down, we must still notify so
13849 				 * that the user knows the IFF_RUNNING status
13850 				 * change.  (If the first ipif is up, then
13851 				 * we'll handle eventual routing socket
13852 				 * notification via DAD completion.)
13853 				 */
13854 				if (ipif == ill->ill_ipif) {
13855 					ip_rts_ifmsg(ill->ill_ipif,
13856 					    RTSQ_DEFAULT);
13857 				}
13858 			}
13859 		} else {
13860 			/*
13861 			 * After link down, we'll need to send a new routing
13862 			 * message when the link comes back, so clear
13863 			 * ipif_addr_ready.
13864 			 */
13865 			ipif->ipif_addr_ready = 0;
13866 		}
13867 	}
13868 
13869 	/*
13870 	 * If we've torn down links, then notify the user right away.
13871 	 */
13872 	if (!went_up)
13873 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13874 }
13875 
13876 static void
13877 ipsq_delete(ipsq_t *ipsq)
13878 {
13879 	ipxop_t *ipx = ipsq->ipsq_xop;
13880 
13881 	ipsq->ipsq_ipst = NULL;
13882 	ASSERT(ipsq->ipsq_phyint == NULL);
13883 	ASSERT(ipsq->ipsq_xop != NULL);
13884 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
13885 	ASSERT(ipx->ipx_pending_mp == NULL);
13886 	kmem_free(ipsq, sizeof (ipsq_t));
13887 }
13888 
13889 static int
13890 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
13891 {
13892 	int err;
13893 	ipif_t *ipif;
13894 
13895 	if (ill == NULL)
13896 		return (0);
13897 
13898 	ASSERT(IAM_WRITER_ILL(ill));
13899 	ill->ill_up_ipifs = B_TRUE;
13900 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13901 		if (ipif->ipif_was_up) {
13902 			if (!(ipif->ipif_flags & IPIF_UP))
13903 				err = ipif_up(ipif, q, mp);
13904 			ipif->ipif_was_up = B_FALSE;
13905 			if (err != 0) {
13906 				ASSERT(err == EINPROGRESS);
13907 				return (err);
13908 			}
13909 		}
13910 	}
13911 	mutex_enter(&ill->ill_lock);
13912 	ill->ill_state_flags &= ~ILL_CHANGING;
13913 	mutex_exit(&ill->ill_lock);
13914 	ill->ill_up_ipifs = B_FALSE;
13915 	return (0);
13916 }
13917 
13918 /*
13919  * This function is called to bring up all the ipifs that were up before
13920  * bringing the ill down via ill_down_ipifs().
13921  */
13922 int
13923 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13924 {
13925 	int err;
13926 
13927 	ASSERT(IAM_WRITER_ILL(ill));
13928 
13929 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
13930 	if (err != 0)
13931 		return (err);
13932 
13933 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
13934 }
13935 
13936 /*
13937  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
13938  * down the ipifs without sending DL_UNBIND_REQ to the driver.
13939  */
13940 static void
13941 ill_down_ipifs(ill_t *ill, boolean_t logical)
13942 {
13943 	ipif_t *ipif;
13944 
13945 	ASSERT(IAM_WRITER_ILL(ill));
13946 
13947 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13948 		/*
13949 		 * We go through the ipif_down logic even if the ipif
13950 		 * is already down, since routes can be added based
13951 		 * on down ipifs. Going through ipif_down once again
13952 		 * will delete any IREs created based on these routes.
13953 		 */
13954 		if (ipif->ipif_flags & IPIF_UP)
13955 			ipif->ipif_was_up = B_TRUE;
13956 
13957 		/*
13958 		 * Need to re-create net/subnet bcast ires if
13959 		 * they are dependent on ipif.
13960 		 */
13961 		if (!ipif->ipif_isv6)
13962 			ipif_check_bcast_ires(ipif);
13963 		if (logical) {
13964 			(void) ipif_logical_down(ipif, NULL, NULL);
13965 			ipif_non_duplicate(ipif);
13966 			ipif_down_tail(ipif);
13967 		} else {
13968 			(void) ipif_down(ipif, NULL, NULL);
13969 		}
13970 	}
13971 }
13972 
13973 /*
13974  * Redo source address selection.  This is called when a
13975  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
13976  */
13977 void
13978 ill_update_source_selection(ill_t *ill)
13979 {
13980 	ipif_t *ipif;
13981 
13982 	ASSERT(IAM_WRITER_ILL(ill));
13983 
13984 	/*
13985 	 * Underlying interfaces are only used for test traffic and thus
13986 	 * should always send with their (deprecated) source addresses.
13987 	 */
13988 	if (IS_UNDER_IPMP(ill))
13989 		return;
13990 
13991 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13992 		if (ill->ill_isv6)
13993 			ipif_recreate_interface_routes_v6(NULL, ipif);
13994 		else
13995 			ipif_recreate_interface_routes(NULL, ipif);
13996 	}
13997 }
13998 
13999 /*
14000  * Finish the group join started in ip_sioctl_groupname().
14001  */
14002 /* ARGSUSED */
14003 static void
14004 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14005 {
14006 	ill_t		*ill = q->q_ptr;
14007 	phyint_t	*phyi = ill->ill_phyint;
14008 	ipmp_grp_t	*grp = phyi->phyint_grp;
14009 	ip_stack_t	*ipst = ill->ill_ipst;
14010 
14011 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14012 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14013 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14014 
14015 	if (phyi->phyint_illv4 != NULL) {
14016 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14017 		VERIFY(grp->gr_pendv4-- > 0);
14018 		rw_exit(&ipst->ips_ipmp_lock);
14019 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14020 	}
14021 	if (phyi->phyint_illv6 != NULL) {
14022 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14023 		VERIFY(grp->gr_pendv6-- > 0);
14024 		rw_exit(&ipst->ips_ipmp_lock);
14025 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14026 	}
14027 	freemsg(mp);
14028 }
14029 
14030 /*
14031  * Process an SIOCSLIFGROUPNAME request.
14032  */
14033 /* ARGSUSED */
14034 int
14035 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14036     ip_ioctl_cmd_t *ipip, void *ifreq)
14037 {
14038 	struct lifreq	*lifr = ifreq;
14039 	ill_t		*ill = ipif->ipif_ill;
14040 	ip_stack_t	*ipst = ill->ill_ipst;
14041 	phyint_t	*phyi = ill->ill_phyint;
14042 	ipmp_grp_t	*grp = phyi->phyint_grp;
14043 	mblk_t		*ipsq_mp;
14044 	int		err = 0;
14045 
14046 	/*
14047 	 * Note that phyint_grp can only change here, where we're exclusive.
14048 	 */
14049 	ASSERT(IAM_WRITER_ILL(ill));
14050 
14051 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14052 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14053 		return (EINVAL);
14054 
14055 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14056 
14057 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14058 
14059 	/*
14060 	 * If the name hasn't changed, there's nothing to do.
14061 	 */
14062 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14063 		goto unlock;
14064 
14065 	/*
14066 	 * Handle requests to rename an IPMP meta-interface.
14067 	 *
14068 	 * Note that creation of the IPMP meta-interface is handled in
14069 	 * userland through the standard plumbing sequence.  As part of the
14070 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14071 	 * the name of the interface (see ipif_set_values_tail()).
14072 	 */
14073 	if (IS_IPMP(ill)) {
14074 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14075 		goto unlock;
14076 	}
14077 
14078 	/*
14079 	 * Handle requests to add or remove an IP interface from a group.
14080 	 */
14081 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14082 		/*
14083 		 * Moves are handled by first removing the interface from
14084 		 * its existing group, and then adding it to another group.
14085 		 * So, fail if it's already in a group.
14086 		 */
14087 		if (IS_UNDER_IPMP(ill)) {
14088 			err = EALREADY;
14089 			goto unlock;
14090 		}
14091 
14092 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14093 		if (grp == NULL) {
14094 			err = ENOENT;
14095 			goto unlock;
14096 		}
14097 
14098 		/*
14099 		 * Check if the phyint and its ills are suitable for
14100 		 * inclusion into the group.
14101 		 */
14102 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14103 			goto unlock;
14104 
14105 		/*
14106 		 * Checks pass; join the group, and enqueue the remaining
14107 		 * illgrp joins for when we've become part of the group xop
14108 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14109 		 * requires an mblk_t to scribble on, and since `mp' will be
14110 		 * freed as part of completing the ioctl, allocate another.
14111 		 */
14112 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14113 			err = ENOMEM;
14114 			goto unlock;
14115 		}
14116 
14117 		/*
14118 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14119 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14120 		 * before ip_join_illgrps() is called back.  See the comments
14121 		 * in ip_sioctl_plink_ipmp() for more.
14122 		 */
14123 		if (phyi->phyint_illv4 != NULL)
14124 			grp->gr_pendv4++;
14125 		if (phyi->phyint_illv6 != NULL)
14126 			grp->gr_pendv6++;
14127 
14128 		rw_exit(&ipst->ips_ipmp_lock);
14129 
14130 		ipmp_phyint_join_grp(phyi, grp);
14131 		ill_refhold(ill);
14132 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14133 		    SWITCH_OP, B_FALSE);
14134 		return (0);
14135 	} else {
14136 		/*
14137 		 * Request to remove the interface from a group.  If the
14138 		 * interface is not in a group, this trivially succeeds.
14139 		 */
14140 		rw_exit(&ipst->ips_ipmp_lock);
14141 		if (IS_UNDER_IPMP(ill))
14142 			ipmp_phyint_leave_grp(phyi);
14143 		return (0);
14144 	}
14145 unlock:
14146 	rw_exit(&ipst->ips_ipmp_lock);
14147 	return (err);
14148 }
14149 
14150 /*
14151  * Process an SIOCGLIFBINDING request.
14152  */
14153 /* ARGSUSED */
14154 int
14155 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14156     ip_ioctl_cmd_t *ipip, void *ifreq)
14157 {
14158 	ill_t		*ill;
14159 	struct lifreq	*lifr = ifreq;
14160 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14161 
14162 	if (!IS_IPMP(ipif->ipif_ill))
14163 		return (EINVAL);
14164 
14165 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14166 	if ((ill = ipif->ipif_bound_ill) == NULL)
14167 		lifr->lifr_binding[0] = '\0';
14168 	else
14169 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
14170 	rw_exit(&ipst->ips_ipmp_lock);
14171 	return (0);
14172 }
14173 
14174 /*
14175  * Process an SIOCGLIFGROUPNAME request.
14176  */
14177 /* ARGSUSED */
14178 int
14179 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14180     ip_ioctl_cmd_t *ipip, void *ifreq)
14181 {
14182 	ipmp_grp_t	*grp;
14183 	struct lifreq	*lifr = ifreq;
14184 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14185 
14186 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14187 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14188 		lifr->lifr_groupname[0] = '\0';
14189 	else
14190 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14191 	rw_exit(&ipst->ips_ipmp_lock);
14192 	return (0);
14193 }
14194 
14195 /*
14196  * Process an SIOCGLIFGROUPINFO request.
14197  */
14198 /* ARGSUSED */
14199 int
14200 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14201     ip_ioctl_cmd_t *ipip, void *dummy)
14202 {
14203 	ipmp_grp_t	*grp;
14204 	lifgroupinfo_t	*lifgr;
14205 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14206 
14207 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14208 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14209 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14210 
14211 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14212 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14213 		rw_exit(&ipst->ips_ipmp_lock);
14214 		return (ENOENT);
14215 	}
14216 	ipmp_grp_info(grp, lifgr);
14217 	rw_exit(&ipst->ips_ipmp_lock);
14218 	return (0);
14219 }
14220 
14221 static void
14222 ill_dl_down(ill_t *ill)
14223 {
14224 	/*
14225 	 * The ill is down; unbind but stay attached since we're still
14226 	 * associated with a PPA. If we have negotiated DLPI capabilites
14227 	 * with the data link service provider (IDS_OK) then reset them.
14228 	 * The interval between unbinding and rebinding is potentially
14229 	 * unbounded hence we cannot assume things will be the same.
14230 	 * The DLPI capabilities will be probed again when the data link
14231 	 * is brought up.
14232 	 */
14233 	mblk_t	*mp = ill->ill_unbind_mp;
14234 
14235 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14236 
14237 	ill->ill_unbind_mp = NULL;
14238 	if (mp != NULL) {
14239 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14240 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14241 		    ill->ill_name));
14242 		mutex_enter(&ill->ill_lock);
14243 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14244 		mutex_exit(&ill->ill_lock);
14245 		/*
14246 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14247 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14248 		 * ill_capability_dld_disable disable rightaway. If this is not
14249 		 * an unplumb operation then the disable happens on receipt of
14250 		 * the capab ack via ip_rput_dlpi_writer ->
14251 		 * ill_capability_ack_thr. In both cases the order of
14252 		 * the operations seen by DLD is capability disable followed
14253 		 * by DL_UNBIND. Also the DLD capability disable needs a
14254 		 * cv_wait'able context.
14255 		 */
14256 		if (ill->ill_state_flags & ILL_CONDEMNED)
14257 			ill_capability_dld_disable(ill);
14258 		ill_capability_reset(ill, B_FALSE);
14259 		ill_dlpi_send(ill, mp);
14260 	}
14261 
14262 	/*
14263 	 * Toss all of our multicast memberships.  We could keep them, but
14264 	 * then we'd have to do bookkeeping of any joins and leaves performed
14265 	 * by the application while the the interface is down (we can't just
14266 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14267 	 * on a downed interface).
14268 	 */
14269 	ill_leave_multicast(ill);
14270 
14271 	mutex_enter(&ill->ill_lock);
14272 	ill->ill_dl_up = 0;
14273 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14274 	mutex_exit(&ill->ill_lock);
14275 }
14276 
14277 static void
14278 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14279 {
14280 	union DL_primitives *dlp;
14281 	t_uscalar_t prim;
14282 	boolean_t waitack = B_FALSE;
14283 
14284 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14285 
14286 	dlp = (union DL_primitives *)mp->b_rptr;
14287 	prim = dlp->dl_primitive;
14288 
14289 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14290 	    dl_primstr(prim), prim, ill->ill_name));
14291 
14292 	switch (prim) {
14293 	case DL_PHYS_ADDR_REQ:
14294 	{
14295 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14296 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14297 		break;
14298 	}
14299 	case DL_BIND_REQ:
14300 		mutex_enter(&ill->ill_lock);
14301 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14302 		mutex_exit(&ill->ill_lock);
14303 		break;
14304 	}
14305 
14306 	/*
14307 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14308 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14309 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14310 	 */
14311 	mutex_enter(&ill->ill_lock);
14312 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
14313 	    (prim == DL_UNBIND_REQ)) {
14314 		ill->ill_dlpi_pending = prim;
14315 		waitack = B_TRUE;
14316 	}
14317 
14318 	mutex_exit(&ill->ill_lock);
14319 	putnext(ill->ill_wq, mp);
14320 
14321 	/*
14322 	 * There is no ack for DL_NOTIFY_CONF messages
14323 	 */
14324 	if (waitack && prim == DL_NOTIFY_CONF)
14325 		ill_dlpi_done(ill, prim);
14326 }
14327 
14328 /*
14329  * Helper function for ill_dlpi_send().
14330  */
14331 /* ARGSUSED */
14332 static void
14333 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14334 {
14335 	ill_dlpi_send(q->q_ptr, mp);
14336 }
14337 
14338 /*
14339  * Send a DLPI control message to the driver but make sure there
14340  * is only one outstanding message. Uses ill_dlpi_pending to tell
14341  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14342  * when an ACK or a NAK is received to process the next queued message.
14343  */
14344 void
14345 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14346 {
14347 	mblk_t **mpp;
14348 
14349 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14350 
14351 	/*
14352 	 * To ensure that any DLPI requests for current exclusive operation
14353 	 * are always completely sent before any DLPI messages for other
14354 	 * operations, require writer access before enqueuing.
14355 	 */
14356 	if (!IAM_WRITER_ILL(ill)) {
14357 		ill_refhold(ill);
14358 		/* qwriter_ip() does the ill_refrele() */
14359 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14360 		    NEW_OP, B_TRUE);
14361 		return;
14362 	}
14363 
14364 	mutex_enter(&ill->ill_lock);
14365 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14366 		/* Must queue message. Tail insertion */
14367 		mpp = &ill->ill_dlpi_deferred;
14368 		while (*mpp != NULL)
14369 			mpp = &((*mpp)->b_next);
14370 
14371 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14372 		    ill->ill_name));
14373 
14374 		*mpp = mp;
14375 		mutex_exit(&ill->ill_lock);
14376 		return;
14377 	}
14378 	mutex_exit(&ill->ill_lock);
14379 	ill_dlpi_dispatch(ill, mp);
14380 }
14381 
14382 static void
14383 ill_capability_send(ill_t *ill, mblk_t *mp)
14384 {
14385 	ill->ill_capab_pending_cnt++;
14386 	ill_dlpi_send(ill, mp);
14387 }
14388 
14389 void
14390 ill_capability_done(ill_t *ill)
14391 {
14392 	ASSERT(ill->ill_capab_pending_cnt != 0);
14393 
14394 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14395 
14396 	ill->ill_capab_pending_cnt--;
14397 	if (ill->ill_capab_pending_cnt == 0 &&
14398 	    ill->ill_dlpi_capab_state == IDCS_OK)
14399 		ill_capability_reset_alloc(ill);
14400 }
14401 
14402 /*
14403  * Send all deferred DLPI messages without waiting for their ACKs.
14404  */
14405 void
14406 ill_dlpi_send_deferred(ill_t *ill)
14407 {
14408 	mblk_t *mp, *nextmp;
14409 
14410 	/*
14411 	 * Clear ill_dlpi_pending so that the message is not queued in
14412 	 * ill_dlpi_send().
14413 	 */
14414 	mutex_enter(&ill->ill_lock);
14415 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14416 	mp = ill->ill_dlpi_deferred;
14417 	ill->ill_dlpi_deferred = NULL;
14418 	mutex_exit(&ill->ill_lock);
14419 
14420 	for (; mp != NULL; mp = nextmp) {
14421 		nextmp = mp->b_next;
14422 		mp->b_next = NULL;
14423 		ill_dlpi_send(ill, mp);
14424 	}
14425 }
14426 
14427 /*
14428  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14429  */
14430 boolean_t
14431 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14432 {
14433 	t_uscalar_t pending;
14434 
14435 	mutex_enter(&ill->ill_lock);
14436 	if (ill->ill_dlpi_pending == prim) {
14437 		mutex_exit(&ill->ill_lock);
14438 		return (B_TRUE);
14439 	}
14440 
14441 	/*
14442 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14443 	 * without waiting, so don't print any warnings in that case.
14444 	 */
14445 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14446 		mutex_exit(&ill->ill_lock);
14447 		return (B_FALSE);
14448 	}
14449 	pending = ill->ill_dlpi_pending;
14450 	mutex_exit(&ill->ill_lock);
14451 
14452 	if (pending == DL_PRIM_INVAL) {
14453 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14454 		    "received unsolicited ack for %s on %s\n",
14455 		    dl_primstr(prim), ill->ill_name);
14456 	} else {
14457 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14458 		    "received unexpected ack for %s on %s (expecting %s)\n",
14459 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14460 	}
14461 	return (B_FALSE);
14462 }
14463 
14464 /*
14465  * Complete the current DLPI operation associated with `prim' on `ill' and
14466  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14467  * operations and the ill's current exclusive IPSQ operation has finished
14468  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14469  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14470  * the comments above ipsq_current_finish() for details.
14471  */
14472 void
14473 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14474 {
14475 	mblk_t *mp;
14476 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14477 	ipxop_t *ipx = ipsq->ipsq_xop;
14478 
14479 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14480 	mutex_enter(&ill->ill_lock);
14481 
14482 	ASSERT(prim != DL_PRIM_INVAL);
14483 	ASSERT(ill->ill_dlpi_pending == prim);
14484 
14485 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14486 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14487 
14488 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14489 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14490 		if (ipx->ipx_current_done) {
14491 			mutex_enter(&ipx->ipx_lock);
14492 			ipx->ipx_current_ipif = NULL;
14493 			mutex_exit(&ipx->ipx_lock);
14494 		}
14495 		cv_signal(&ill->ill_cv);
14496 		mutex_exit(&ill->ill_lock);
14497 		return;
14498 	}
14499 
14500 	ill->ill_dlpi_deferred = mp->b_next;
14501 	mp->b_next = NULL;
14502 	mutex_exit(&ill->ill_lock);
14503 
14504 	ill_dlpi_dispatch(ill, mp);
14505 }
14506 
14507 void
14508 conn_delete_ire(conn_t *connp, caddr_t arg)
14509 {
14510 	ipif_t	*ipif = (ipif_t *)arg;
14511 	ire_t	*ire;
14512 
14513 	/*
14514 	 * Look at the cached ires on conns which has pointers to ipifs.
14515 	 * We just call ire_refrele which clears up the reference
14516 	 * to ire. Called when a conn closes. Also called from ipif_free
14517 	 * to cleanup indirect references to the stale ipif via the cached ire.
14518 	 */
14519 	mutex_enter(&connp->conn_lock);
14520 	ire = connp->conn_ire_cache;
14521 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14522 		connp->conn_ire_cache = NULL;
14523 		mutex_exit(&connp->conn_lock);
14524 		IRE_REFRELE_NOTR(ire);
14525 		return;
14526 	}
14527 	mutex_exit(&connp->conn_lock);
14528 
14529 }
14530 
14531 /*
14532  * Some operations (e.g., ipif_down()) conditionally delete a number
14533  * of IREs. Those IREs may have been previously cached in the conn structure.
14534  * This ipcl_walk() walker function releases all references to such IREs based
14535  * on the condemned flag.
14536  */
14537 /* ARGSUSED */
14538 void
14539 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14540 {
14541 	ire_t	*ire;
14542 
14543 	mutex_enter(&connp->conn_lock);
14544 	ire = connp->conn_ire_cache;
14545 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14546 		connp->conn_ire_cache = NULL;
14547 		mutex_exit(&connp->conn_lock);
14548 		IRE_REFRELE_NOTR(ire);
14549 		return;
14550 	}
14551 	mutex_exit(&connp->conn_lock);
14552 }
14553 
14554 /*
14555  * Take down a specific interface, but don't lose any information about it.
14556  * (Always called as writer.)
14557  * This function goes through the down sequence even if the interface is
14558  * already down. There are 2 reasons.
14559  * a. Currently we permit interface routes that depend on down interfaces
14560  *    to be added. This behaviour itself is questionable. However it appears
14561  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14562  *    time. We go thru the cleanup in order to remove these routes.
14563  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14564  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14565  *    down, but we need to cleanup i.e. do ill_dl_down and
14566  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14567  *
14568  * IP-MT notes:
14569  *
14570  * Model of reference to interfaces.
14571  *
14572  * The following members in ipif_t track references to the ipif.
14573  *	int     ipif_refcnt;    Active reference count
14574  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14575  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14576  *
14577  * The following members in ill_t track references to the ill.
14578  *	int             ill_refcnt;     active refcnt
14579  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14580  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14581  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14582  *
14583  * Reference to an ipif or ill can be obtained in any of the following ways.
14584  *
14585  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14586  * Pointers to ipif / ill from other data structures viz ire and conn.
14587  * Implicit reference to the ipif / ill by holding a reference to the ire.
14588  *
14589  * The ipif/ill lookup functions return a reference held ipif / ill.
14590  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14591  * This is a purely dynamic reference count associated with threads holding
14592  * references to the ipif / ill. Pointers from other structures do not
14593  * count towards this reference count.
14594  *
14595  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14596  * associated with the ipif/ill. This is incremented whenever a new
14597  * ire is created referencing the ipif/ill. This is done atomically inside
14598  * ire_add_v[46] where the ire is actually added to the ire hash table.
14599  * The count is decremented in ire_inactive where the ire is destroyed.
14600  *
14601  * nce's reference ill's thru nce_ill and the count of nce's associated with
14602  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14603  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14604  * table. Similarly it is decremented in ndp_inactive() where the nce
14605  * is destroyed.
14606  *
14607  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14608  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14609  * in ilm_walker_cleanup() or ilm_delete().
14610  *
14611  * Flow of ioctls involving interface down/up
14612  *
14613  * The following is the sequence of an attempt to set some critical flags on an
14614  * up interface.
14615  * ip_sioctl_flags
14616  * ipif_down
14617  * wait for ipif to be quiescent
14618  * ipif_down_tail
14619  * ip_sioctl_flags_tail
14620  *
14621  * All set ioctls that involve down/up sequence would have a skeleton similar
14622  * to the above. All the *tail functions are called after the refcounts have
14623  * dropped to the appropriate values.
14624  *
14625  * The mechanism to quiesce an ipif is as follows.
14626  *
14627  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14628  * on the ipif. Callers either pass a flag requesting wait or the lookup
14629  *  functions will return NULL.
14630  *
14631  * Delete all ires referencing this ipif
14632  *
14633  * Any thread attempting to do an ipif_refhold on an ipif that has been
14634  * obtained thru a cached pointer will first make sure that
14635  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14636  * increment the refcount.
14637  *
14638  * The above guarantees that the ipif refcount will eventually come down to
14639  * zero and the ipif will quiesce, once all threads that currently hold a
14640  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14641  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14642  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14643  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14644  * in ip.h
14645  *
14646  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14647  *
14648  * Threads trying to lookup an ipif or ill can pass a flag requesting
14649  * wait and restart if the ipif / ill cannot be looked up currently.
14650  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14651  * failure if the ipif is currently undergoing an exclusive operation, and
14652  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14653  * is restarted by ipsq_exit() when the current exclusive operation completes.
14654  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14655  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14656  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14657  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14658  * until we release the ipsq_lock, even though the the ill/ipif state flags
14659  * can change after we drop the ill_lock.
14660  *
14661  * An attempt to send out a packet using an ipif that is currently
14662  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14663  * operation and restart it later when the exclusive condition on the ipif ends.
14664  * This is an example of not passing the wait flag to the lookup functions. For
14665  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14666  * out a multicast packet on that ipif will fail while the ipif is
14667  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14668  * currently IPIF_CHANGING will also fail.
14669  */
14670 int
14671 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14672 {
14673 	ill_t		*ill = ipif->ipif_ill;
14674 	conn_t		*connp;
14675 	boolean_t	success;
14676 	boolean_t	ipif_was_up = B_FALSE;
14677 	ip_stack_t	*ipst = ill->ill_ipst;
14678 
14679 	ASSERT(IAM_WRITER_IPIF(ipif));
14680 
14681 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14682 
14683 	if (ipif->ipif_flags & IPIF_UP) {
14684 		mutex_enter(&ill->ill_lock);
14685 		ipif->ipif_flags &= ~IPIF_UP;
14686 		ASSERT(ill->ill_ipif_up_count > 0);
14687 		--ill->ill_ipif_up_count;
14688 		mutex_exit(&ill->ill_lock);
14689 		ipif_was_up = B_TRUE;
14690 		/* Update status in SCTP's list */
14691 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14692 		ill_nic_event_dispatch(ipif->ipif_ill,
14693 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14694 	}
14695 
14696 	/*
14697 	 * Blow away memberships we established in ipif_multicast_up().
14698 	 */
14699 	ipif_multicast_down(ipif);
14700 
14701 	/*
14702 	 * Remove from the mapping for __sin6_src_id. We insert only
14703 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14704 	 * stored as mapped addresses, we need to check for mapped
14705 	 * INADDR_ANY also.
14706 	 */
14707 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14708 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14709 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14710 		int err;
14711 
14712 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14713 		    ipif->ipif_zoneid, ipst);
14714 		if (err != 0) {
14715 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14716 		}
14717 	}
14718 
14719 	/*
14720 	 * Delete all IRE's pointing at this ipif or its source address.
14721 	 */
14722 	if (ipif->ipif_isv6) {
14723 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14724 		    ipst);
14725 	} else {
14726 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14727 		    ipst);
14728 	}
14729 
14730 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14731 		/*
14732 		 * Since the interface is now down, it may have just become
14733 		 * inactive.  Note that this needs to be done even for a
14734 		 * lll_logical_down(), or ARP entries will not get correctly
14735 		 * restored when the interface comes back up.
14736 		 */
14737 		if (IS_UNDER_IPMP(ill))
14738 			ipmp_ill_refresh_active(ill);
14739 	}
14740 
14741 	/*
14742 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14743 	 * ires have been deleted above. Otherwise a thread could end up
14744 	 * caching an ire in a conn after we have finished the cleanup of the
14745 	 * conn. The caching is done after making sure that the ire is not yet
14746 	 * condemned. Also documented in the block comment above ip_output
14747 	 */
14748 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14749 	/* Also, delete the ires cached in SCTP */
14750 	sctp_ire_cache_flush(ipif);
14751 
14752 	/*
14753 	 * Update any other ipifs which have used "our" local address as
14754 	 * a source address. This entails removing and recreating IRE_INTERFACE
14755 	 * entries for such ipifs.
14756 	 */
14757 	if (ipif->ipif_isv6)
14758 		ipif_update_other_ipifs_v6(ipif);
14759 	else
14760 		ipif_update_other_ipifs(ipif);
14761 
14762 	/*
14763 	 * neighbor-discovery or arp entries for this interface.
14764 	 */
14765 	ipif_ndp_down(ipif);
14766 
14767 	/*
14768 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14769 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14770 	 * and ill_delete -> ipif_free -> ipif_down
14771 	 */
14772 	if (mp == NULL) {
14773 		ASSERT(q == NULL);
14774 		return (0);
14775 	}
14776 
14777 	if (CONN_Q(q)) {
14778 		connp = Q_TO_CONN(q);
14779 		mutex_enter(&connp->conn_lock);
14780 	} else {
14781 		connp = NULL;
14782 	}
14783 	mutex_enter(&ill->ill_lock);
14784 	/*
14785 	 * Are there any ire's pointing to this ipif that are still active ?
14786 	 * If this is the last ipif going down, are there any ire's pointing
14787 	 * to this ill that are still active ?
14788 	 */
14789 	if (ipif_is_quiescent(ipif)) {
14790 		mutex_exit(&ill->ill_lock);
14791 		if (connp != NULL)
14792 			mutex_exit(&connp->conn_lock);
14793 		return (0);
14794 	}
14795 
14796 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14797 	    ill->ill_name, (void *)ill));
14798 	/*
14799 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14800 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14801 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14802 	 */
14803 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14804 	if (!success) {
14805 		/* The conn is closing. So just return */
14806 		ASSERT(connp != NULL);
14807 		mutex_exit(&ill->ill_lock);
14808 		mutex_exit(&connp->conn_lock);
14809 		return (EINTR);
14810 	}
14811 
14812 	mutex_exit(&ill->ill_lock);
14813 	if (connp != NULL)
14814 		mutex_exit(&connp->conn_lock);
14815 	return (EINPROGRESS);
14816 }
14817 
14818 void
14819 ipif_down_tail(ipif_t *ipif)
14820 {
14821 	ill_t	*ill = ipif->ipif_ill;
14822 
14823 	/*
14824 	 * Skip any loopback interface (null wq).
14825 	 * If this is the last logical interface on the ill
14826 	 * have ill_dl_down tell the driver we are gone (unbind)
14827 	 * Note that lun 0 can ipif_down even though
14828 	 * there are other logical units that are up.
14829 	 * This occurs e.g. when we change a "significant" IFF_ flag.
14830 	 */
14831 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
14832 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
14833 	    ill->ill_dl_up) {
14834 		ill_dl_down(ill);
14835 	}
14836 	ill->ill_logical_down = 0;
14837 
14838 	/*
14839 	 * Has to be after removing the routes in ipif_down_delete_ire.
14840 	 */
14841 	ipif_resolver_down(ipif);
14842 
14843 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
14844 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
14845 }
14846 
14847 /*
14848  * Bring interface logically down without bringing the physical interface
14849  * down e.g. when the netmask is changed. This avoids long lasting link
14850  * negotiations between an ethernet interface and a certain switches.
14851  */
14852 static int
14853 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14854 {
14855 	/*
14856 	 * The ill_logical_down flag is a transient flag. It is set here
14857 	 * and is cleared once the down has completed in ipif_down_tail.
14858 	 * This flag does not indicate whether the ill stream is in the
14859 	 * DL_BOUND state with the driver. Instead this flag is used by
14860 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
14861 	 * the driver. The state of the ill stream i.e. whether it is
14862 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
14863 	 */
14864 	ipif->ipif_ill->ill_logical_down = 1;
14865 	return (ipif_down(ipif, q, mp));
14866 }
14867 
14868 /*
14869  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
14870  * If the usesrc client ILL is already part of a usesrc group or not,
14871  * in either case a ire_stq with the matching usesrc client ILL will
14872  * locate the IRE's that need to be deleted. We want IREs to be created
14873  * with the new source address.
14874  */
14875 static void
14876 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
14877 {
14878 	ill_t	*ucill = (ill_t *)ill_arg;
14879 
14880 	ASSERT(IAM_WRITER_ILL(ucill));
14881 
14882 	if (ire->ire_stq == NULL)
14883 		return;
14884 
14885 	if ((ire->ire_type == IRE_CACHE) &&
14886 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
14887 		ire_delete(ire);
14888 }
14889 
14890 /*
14891  * ire_walk routine to delete every IRE dependent on the interface
14892  * address that is going down.	(Always called as writer.)
14893  * Works for both v4 and v6.
14894  * In addition for checking for ire_ipif matches it also checks for
14895  * IRE_CACHE entries which have the same source address as the
14896  * disappearing ipif since ipif_select_source might have picked
14897  * that source. Note that ipif_down/ipif_update_other_ipifs takes
14898  * care of any IRE_INTERFACE with the disappearing source address.
14899  */
14900 static void
14901 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
14902 {
14903 	ipif_t	*ipif = (ipif_t *)ipif_arg;
14904 
14905 	ASSERT(IAM_WRITER_IPIF(ipif));
14906 	if (ire->ire_ipif == NULL)
14907 		return;
14908 
14909 	if (ire->ire_ipif != ipif) {
14910 		/*
14911 		 * Look for a matching source address.
14912 		 */
14913 		if (ire->ire_type != IRE_CACHE)
14914 			return;
14915 		if (ipif->ipif_flags & IPIF_NOLOCAL)
14916 			return;
14917 
14918 		if (ire->ire_ipversion == IPV4_VERSION) {
14919 			if (ire->ire_src_addr != ipif->ipif_src_addr)
14920 				return;
14921 		} else {
14922 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
14923 			    &ipif->ipif_v6lcl_addr))
14924 				return;
14925 		}
14926 		ire_delete(ire);
14927 		return;
14928 	}
14929 	/*
14930 	 * ire_delete() will do an ire_flush_cache which will delete
14931 	 * all ire_ipif matches
14932 	 */
14933 	ire_delete(ire);
14934 }
14935 
14936 /*
14937  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
14938  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
14939  * 2) when an interface is brought up or down (on that ill).
14940  * This ensures that the IRE_CACHE entries don't retain stale source
14941  * address selection results.
14942  */
14943 void
14944 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
14945 {
14946 	ill_t	*ill = (ill_t *)ill_arg;
14947 
14948 	ASSERT(IAM_WRITER_ILL(ill));
14949 	ASSERT(ire->ire_type == IRE_CACHE);
14950 
14951 	/*
14952 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14953 	 * ill, but we only want to delete the IRE if ire_ipif matches.
14954 	 */
14955 	ASSERT(ire->ire_ipif != NULL);
14956 	if (ill == ire->ire_ipif->ipif_ill)
14957 		ire_delete(ire);
14958 }
14959 
14960 /*
14961  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
14962  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
14963  * the IPMP ill.
14964  */
14965 void
14966 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
14967 {
14968 	ill_t	*ill = (ill_t *)ill_arg;
14969 
14970 	ASSERT(IAM_WRITER_ILL(ill));
14971 	ASSERT(ire->ire_type == IRE_CACHE);
14972 
14973 	/*
14974 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
14975 	 * ill, but we only want to delete the IRE if ire_stq matches.
14976 	 */
14977 	if (ire->ire_stq->q_ptr == ill_arg)
14978 		ire_delete(ire);
14979 }
14980 
14981 /*
14982  * Delete all broadcast IREs with a source address on `ill_arg'.
14983  */
14984 static void
14985 ill_broadcast_delete(ire_t *ire, char *ill_arg)
14986 {
14987 	ill_t *ill = (ill_t *)ill_arg;
14988 
14989 	ASSERT(IAM_WRITER_ILL(ill));
14990 	ASSERT(ire->ire_type == IRE_BROADCAST);
14991 
14992 	if (ire->ire_ipif->ipif_ill == ill)
14993 		ire_delete(ire);
14994 }
14995 
14996 /*
14997  * Initiate deallocate of an IPIF. Always called as writer. Called by
14998  * ill_delete or ip_sioctl_removeif.
14999  */
15000 static void
15001 ipif_free(ipif_t *ipif)
15002 {
15003 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15004 
15005 	ASSERT(IAM_WRITER_IPIF(ipif));
15006 
15007 	if (ipif->ipif_recovery_id != 0)
15008 		(void) untimeout(ipif->ipif_recovery_id);
15009 	ipif->ipif_recovery_id = 0;
15010 
15011 	/* Remove conn references */
15012 	reset_conn_ipif(ipif);
15013 
15014 	/*
15015 	 * Make sure we have valid net and subnet broadcast ire's for the
15016 	 * other ipif's which share them with this ipif.
15017 	 */
15018 	if (!ipif->ipif_isv6)
15019 		ipif_check_bcast_ires(ipif);
15020 
15021 	/*
15022 	 * Take down the interface. We can be called either from ill_delete
15023 	 * or from ip_sioctl_removeif.
15024 	 */
15025 	(void) ipif_down(ipif, NULL, NULL);
15026 
15027 	/*
15028 	 * Now that the interface is down, there's no chance it can still
15029 	 * become a duplicate.  Cancel any timer that may have been set while
15030 	 * tearing down.
15031 	 */
15032 	if (ipif->ipif_recovery_id != 0)
15033 		(void) untimeout(ipif->ipif_recovery_id);
15034 	ipif->ipif_recovery_id = 0;
15035 
15036 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15037 	/* Remove pointers to this ill in the multicast routing tables */
15038 	reset_mrt_vif_ipif(ipif);
15039 	/* If necessary, clear the cached source ipif rotor. */
15040 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15041 		ipif->ipif_ill->ill_src_ipif = NULL;
15042 	rw_exit(&ipst->ips_ill_g_lock);
15043 }
15044 
15045 static void
15046 ipif_free_tail(ipif_t *ipif)
15047 {
15048 	mblk_t	*mp;
15049 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15050 
15051 	/*
15052 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15053 	 */
15054 	mutex_enter(&ipif->ipif_saved_ire_lock);
15055 	mp = ipif->ipif_saved_ire_mp;
15056 	ipif->ipif_saved_ire_mp = NULL;
15057 	mutex_exit(&ipif->ipif_saved_ire_lock);
15058 	freemsg(mp);
15059 
15060 	/*
15061 	 * Need to hold both ill_g_lock and ill_lock while
15062 	 * inserting or removing an ipif from the linked list
15063 	 * of ipifs hanging off the ill.
15064 	 */
15065 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15066 
15067 	ASSERT(ilm_walk_ipif(ipif) == 0);
15068 
15069 #ifdef DEBUG
15070 	ipif_trace_cleanup(ipif);
15071 #endif
15072 
15073 	/* Ask SCTP to take it out of it list */
15074 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15075 
15076 	/* Get it out of the ILL interface list. */
15077 	ipif_remove(ipif);
15078 	rw_exit(&ipst->ips_ill_g_lock);
15079 
15080 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15081 
15082 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15083 	ASSERT(ipif->ipif_recovery_id == 0);
15084 
15085 	/* Free the memory. */
15086 	mi_free(ipif);
15087 }
15088 
15089 /*
15090  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15091  * is zero.
15092  */
15093 void
15094 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15095 {
15096 	char	lbuf[LIFNAMSIZ];
15097 	char	*name;
15098 	size_t	name_len;
15099 
15100 	buf[0] = '\0';
15101 	name = ipif->ipif_ill->ill_name;
15102 	name_len = ipif->ipif_ill->ill_name_length;
15103 	if (ipif->ipif_id != 0) {
15104 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15105 		    ipif->ipif_id);
15106 		name = lbuf;
15107 		name_len = mi_strlen(name) + 1;
15108 	}
15109 	len -= 1;
15110 	buf[len] = '\0';
15111 	len = MIN(len, name_len);
15112 	bcopy(name, buf, len);
15113 }
15114 
15115 /*
15116  * Find an IPIF based on the name passed in.  Names can be of the
15117  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15118  * The <phys> string can have forms like <dev><#> (e.g., le0),
15119  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15120  * When there is no colon, the implied unit id is zero. <phys> must
15121  * correspond to the name of an ILL.  (May be called as writer.)
15122  */
15123 static ipif_t *
15124 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15125     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15126     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15127 {
15128 	char	*cp;
15129 	char	*endp;
15130 	long	id;
15131 	ill_t	*ill;
15132 	ipif_t	*ipif;
15133 	uint_t	ire_type;
15134 	boolean_t did_alloc = B_FALSE;
15135 	ipsq_t	*ipsq;
15136 
15137 	if (error != NULL)
15138 		*error = 0;
15139 
15140 	/*
15141 	 * If the caller wants to us to create the ipif, make sure we have a
15142 	 * valid zoneid
15143 	 */
15144 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15145 
15146 	if (namelen == 0) {
15147 		if (error != NULL)
15148 			*error = ENXIO;
15149 		return (NULL);
15150 	}
15151 
15152 	*exists = B_FALSE;
15153 	/* Look for a colon in the name. */
15154 	endp = &name[namelen];
15155 	for (cp = endp; --cp > name; ) {
15156 		if (*cp == IPIF_SEPARATOR_CHAR)
15157 			break;
15158 	}
15159 
15160 	if (*cp == IPIF_SEPARATOR_CHAR) {
15161 		/*
15162 		 * Reject any non-decimal aliases for logical
15163 		 * interfaces. Aliases with leading zeroes
15164 		 * are also rejected as they introduce ambiguity
15165 		 * in the naming of the interfaces.
15166 		 * In order to confirm with existing semantics,
15167 		 * and to not break any programs/script relying
15168 		 * on that behaviour, if<0>:0 is considered to be
15169 		 * a valid interface.
15170 		 *
15171 		 * If alias has two or more digits and the first
15172 		 * is zero, fail.
15173 		 */
15174 		if (&cp[2] < endp && cp[1] == '0') {
15175 			if (error != NULL)
15176 				*error = EINVAL;
15177 			return (NULL);
15178 		}
15179 	}
15180 
15181 	if (cp <= name) {
15182 		cp = endp;
15183 	} else {
15184 		*cp = '\0';
15185 	}
15186 
15187 	/*
15188 	 * Look up the ILL, based on the portion of the name
15189 	 * before the slash. ill_lookup_on_name returns a held ill.
15190 	 * Temporary to check whether ill exists already. If so
15191 	 * ill_lookup_on_name will clear it.
15192 	 */
15193 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15194 	    q, mp, func, error, &did_alloc, ipst);
15195 	if (cp != endp)
15196 		*cp = IPIF_SEPARATOR_CHAR;
15197 	if (ill == NULL)
15198 		return (NULL);
15199 
15200 	/* Establish the unit number in the name. */
15201 	id = 0;
15202 	if (cp < endp && *endp == '\0') {
15203 		/* If there was a colon, the unit number follows. */
15204 		cp++;
15205 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15206 			ill_refrele(ill);
15207 			if (error != NULL)
15208 				*error = ENXIO;
15209 			return (NULL);
15210 		}
15211 	}
15212 
15213 	GRAB_CONN_LOCK(q);
15214 	mutex_enter(&ill->ill_lock);
15215 	/* Now see if there is an IPIF with this unit number. */
15216 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15217 		if (ipif->ipif_id == id) {
15218 			if (zoneid != ALL_ZONES &&
15219 			    zoneid != ipif->ipif_zoneid &&
15220 			    ipif->ipif_zoneid != ALL_ZONES) {
15221 				mutex_exit(&ill->ill_lock);
15222 				RELEASE_CONN_LOCK(q);
15223 				ill_refrele(ill);
15224 				if (error != NULL)
15225 					*error = ENXIO;
15226 				return (NULL);
15227 			}
15228 			/*
15229 			 * The block comment at the start of ipif_down
15230 			 * explains the use of the macros used below
15231 			 */
15232 			if (IPIF_CAN_LOOKUP(ipif)) {
15233 				ipif_refhold_locked(ipif);
15234 				mutex_exit(&ill->ill_lock);
15235 				if (!did_alloc)
15236 					*exists = B_TRUE;
15237 				/*
15238 				 * Drop locks before calling ill_refrele
15239 				 * since it can potentially call into
15240 				 * ipif_ill_refrele_tail which can end up
15241 				 * in trying to acquire any lock.
15242 				 */
15243 				RELEASE_CONN_LOCK(q);
15244 				ill_refrele(ill);
15245 				return (ipif);
15246 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15247 				ipsq = ill->ill_phyint->phyint_ipsq;
15248 				mutex_enter(&ipsq->ipsq_lock);
15249 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15250 				mutex_exit(&ill->ill_lock);
15251 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15252 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15253 				mutex_exit(&ipsq->ipsq_lock);
15254 				RELEASE_CONN_LOCK(q);
15255 				ill_refrele(ill);
15256 				if (error != NULL)
15257 					*error = EINPROGRESS;
15258 				return (NULL);
15259 			}
15260 		}
15261 	}
15262 	RELEASE_CONN_LOCK(q);
15263 
15264 	if (!do_alloc) {
15265 		mutex_exit(&ill->ill_lock);
15266 		ill_refrele(ill);
15267 		if (error != NULL)
15268 			*error = ENXIO;
15269 		return (NULL);
15270 	}
15271 
15272 	/*
15273 	 * If none found, atomically allocate and return a new one.
15274 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15275 	 * to support "receive only" use of lo0:1 etc. as is still done
15276 	 * below as an initial guess.
15277 	 * However, this is now likely to be overriden later in ipif_up_done()
15278 	 * when we know for sure what address has been configured on the
15279 	 * interface, since we might have more than one loopback interface
15280 	 * with a loopback address, e.g. in the case of zones, and all the
15281 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15282 	 */
15283 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15284 		ire_type = IRE_LOOPBACK;
15285 	else
15286 		ire_type = IRE_LOCAL;
15287 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15288 	if (ipif != NULL)
15289 		ipif_refhold_locked(ipif);
15290 	else if (error != NULL)
15291 		*error = ENOMEM;
15292 	mutex_exit(&ill->ill_lock);
15293 	ill_refrele(ill);
15294 	return (ipif);
15295 }
15296 
15297 /*
15298  * This routine is called whenever a new address comes up on an ipif.  If
15299  * we are configured to respond to address mask requests, then we are supposed
15300  * to broadcast an address mask reply at this time.  This routine is also
15301  * called if we are already up, but a netmask change is made.  This is legal
15302  * but might not make the system manager very popular.	(May be called
15303  * as writer.)
15304  */
15305 void
15306 ipif_mask_reply(ipif_t *ipif)
15307 {
15308 	icmph_t	*icmph;
15309 	ipha_t	*ipha;
15310 	mblk_t	*mp;
15311 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15312 
15313 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15314 
15315 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15316 		return;
15317 
15318 	/* ICMP mask reply is IPv4 only */
15319 	ASSERT(!ipif->ipif_isv6);
15320 	/* ICMP mask reply is not for a loopback interface */
15321 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15322 
15323 	mp = allocb(REPLY_LEN, BPRI_HI);
15324 	if (mp == NULL)
15325 		return;
15326 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15327 
15328 	ipha = (ipha_t *)mp->b_rptr;
15329 	bzero(ipha, REPLY_LEN);
15330 	*ipha = icmp_ipha;
15331 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15332 	ipha->ipha_src = ipif->ipif_src_addr;
15333 	ipha->ipha_dst = ipif->ipif_brd_addr;
15334 	ipha->ipha_length = htons(REPLY_LEN);
15335 	ipha->ipha_ident = 0;
15336 
15337 	icmph = (icmph_t *)&ipha[1];
15338 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15339 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15340 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15341 
15342 	put(ipif->ipif_wq, mp);
15343 
15344 #undef	REPLY_LEN
15345 }
15346 
15347 /*
15348  * When the mtu in the ipif changes, we call this routine through ire_walk
15349  * to update all the relevant IREs.
15350  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15351  */
15352 static void
15353 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15354 {
15355 	ipif_t *ipif = (ipif_t *)ipif_arg;
15356 
15357 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15358 		return;
15359 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15360 }
15361 
15362 /*
15363  * When the mtu in the ill changes, we call this routine through ire_walk
15364  * to update all the relevant IREs.
15365  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15366  */
15367 void
15368 ill_mtu_change(ire_t *ire, char *ill_arg)
15369 {
15370 	ill_t	*ill = (ill_t *)ill_arg;
15371 
15372 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15373 		return;
15374 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
15375 }
15376 
15377 /*
15378  * Join the ipif specific multicast groups.
15379  * Must be called after a mapping has been set up in the resolver.  (Always
15380  * called as writer.)
15381  */
15382 void
15383 ipif_multicast_up(ipif_t *ipif)
15384 {
15385 	int err;
15386 	ill_t *ill;
15387 
15388 	ASSERT(IAM_WRITER_IPIF(ipif));
15389 
15390 	ill = ipif->ipif_ill;
15391 
15392 	ip1dbg(("ipif_multicast_up\n"));
15393 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15394 		return;
15395 
15396 	if (ipif->ipif_isv6) {
15397 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15398 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15399 
15400 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15401 
15402 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15403 			return;
15404 
15405 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15406 
15407 		/*
15408 		 * Join the all hosts multicast address.  We skip this for
15409 		 * underlying IPMP interfaces since they should be invisible.
15410 		 */
15411 		if (!IS_UNDER_IPMP(ill)) {
15412 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15413 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15414 			if (err != 0) {
15415 				ip0dbg(("ipif_multicast_up: "
15416 				    "all_hosts_mcast failed %d\n", err));
15417 				return;
15418 			}
15419 			ipif->ipif_joined_allhosts = 1;
15420 		}
15421 
15422 		/*
15423 		 * Enable multicast for the solicited node multicast address
15424 		 */
15425 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15426 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15427 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15428 			if (err != 0) {
15429 				ip0dbg(("ipif_multicast_up: solicited MC"
15430 				    " failed %d\n", err));
15431 				if (ipif->ipif_joined_allhosts) {
15432 					(void) ip_delmulti_v6(&v6allmc, ill,
15433 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15434 					ipif->ipif_joined_allhosts = 0;
15435 				}
15436 				return;
15437 			}
15438 		}
15439 	} else {
15440 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15441 			return;
15442 
15443 		/* Join the all hosts multicast address */
15444 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15445 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15446 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15447 		if (err) {
15448 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15449 			return;
15450 		}
15451 	}
15452 	ipif->ipif_multicast_up = 1;
15453 }
15454 
15455 /*
15456  * Blow away any multicast groups that we joined in ipif_multicast_up().
15457  * (Explicit memberships are blown away in ill_leave_multicast() when the
15458  * ill is brought down.)
15459  */
15460 void
15461 ipif_multicast_down(ipif_t *ipif)
15462 {
15463 	int err;
15464 
15465 	ASSERT(IAM_WRITER_IPIF(ipif));
15466 
15467 	ip1dbg(("ipif_multicast_down\n"));
15468 	if (!ipif->ipif_multicast_up)
15469 		return;
15470 
15471 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15472 
15473 	if (!ipif->ipif_isv6) {
15474 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15475 		    B_TRUE);
15476 		if (err != 0)
15477 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15478 
15479 		ipif->ipif_multicast_up = 0;
15480 		return;
15481 	}
15482 
15483 	/*
15484 	 * Leave the all-hosts multicast address.
15485 	 */
15486 	if (ipif->ipif_joined_allhosts) {
15487 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15488 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15489 		if (err != 0) {
15490 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15491 			    "failed %d\n", err));
15492 		}
15493 		ipif->ipif_joined_allhosts = 0;
15494 	}
15495 
15496 	/*
15497 	 * Disable multicast for the solicited node multicast address
15498 	 */
15499 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15500 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15501 
15502 		ipv6_multi.s6_addr32[3] |=
15503 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15504 
15505 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15506 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15507 		if (err != 0) {
15508 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15509 			    err));
15510 		}
15511 	}
15512 
15513 	ipif->ipif_multicast_up = 0;
15514 }
15515 
15516 /*
15517  * Used when an interface comes up to recreate any extra routes on this
15518  * interface.
15519  */
15520 static ire_t **
15521 ipif_recover_ire(ipif_t *ipif)
15522 {
15523 	mblk_t	*mp;
15524 	ire_t	**ipif_saved_irep;
15525 	ire_t	**irep;
15526 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15527 
15528 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15529 	    ipif->ipif_id));
15530 
15531 	mutex_enter(&ipif->ipif_saved_ire_lock);
15532 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15533 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15534 	if (ipif_saved_irep == NULL) {
15535 		mutex_exit(&ipif->ipif_saved_ire_lock);
15536 		return (NULL);
15537 	}
15538 
15539 	irep = ipif_saved_irep;
15540 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15541 		ire_t		*ire;
15542 		queue_t		*rfq;
15543 		queue_t		*stq;
15544 		ifrt_t		*ifrt;
15545 		uchar_t		*src_addr;
15546 		uchar_t		*gateway_addr;
15547 		ushort_t	type;
15548 
15549 		/*
15550 		 * When the ire was initially created and then added in
15551 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15552 		 * in the case of a traditional interface route, or as one of
15553 		 * the IRE_OFFSUBNET types (with the exception of
15554 		 * IRE_HOST types ire which is created by icmp_redirect() and
15555 		 * which we don't need to save or recover).  In the case where
15556 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15557 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15558 		 * to satisfy software like GateD and Sun Cluster which creates
15559 		 * routes using the the loopback interface's address as a
15560 		 * gateway.
15561 		 *
15562 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15563 		 * ire_create() will be called in the same way here as
15564 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15565 		 * the route looks like a traditional interface route (where
15566 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15567 		 * the saved ifrt->ifrt_type.  This means that in the case where
15568 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15569 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15570 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15571 		 */
15572 		ifrt = (ifrt_t *)mp->b_rptr;
15573 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15574 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15575 			rfq = NULL;
15576 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15577 			    ? ipif->ipif_rq : ipif->ipif_wq;
15578 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15579 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15580 			    : (uint8_t *)&ipif->ipif_src_addr;
15581 			gateway_addr = NULL;
15582 			type = ipif->ipif_net_type;
15583 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15584 			/* Recover multiroute broadcast IRE. */
15585 			rfq = ipif->ipif_rq;
15586 			stq = ipif->ipif_wq;
15587 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15588 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15589 			    : (uint8_t *)&ipif->ipif_src_addr;
15590 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15591 			type = ifrt->ifrt_type;
15592 		} else {
15593 			rfq = NULL;
15594 			stq = NULL;
15595 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15596 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15597 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15598 			type = ifrt->ifrt_type;
15599 		}
15600 
15601 		/*
15602 		 * Create a copy of the IRE with the saved address and netmask.
15603 		 */
15604 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15605 		    "0x%x/0x%x\n",
15606 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15607 		    ntohl(ifrt->ifrt_addr),
15608 		    ntohl(ifrt->ifrt_mask)));
15609 		ire = ire_create(
15610 		    (uint8_t *)&ifrt->ifrt_addr,
15611 		    (uint8_t *)&ifrt->ifrt_mask,
15612 		    src_addr,
15613 		    gateway_addr,
15614 		    &ifrt->ifrt_max_frag,
15615 		    NULL,
15616 		    rfq,
15617 		    stq,
15618 		    type,
15619 		    ipif,
15620 		    0,
15621 		    0,
15622 		    0,
15623 		    ifrt->ifrt_flags,
15624 		    &ifrt->ifrt_iulp_info,
15625 		    NULL,
15626 		    NULL,
15627 		    ipst);
15628 
15629 		if (ire == NULL) {
15630 			mutex_exit(&ipif->ipif_saved_ire_lock);
15631 			kmem_free(ipif_saved_irep,
15632 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15633 			return (NULL);
15634 		}
15635 
15636 		/*
15637 		 * Some software (for example, GateD and Sun Cluster) attempts
15638 		 * to create (what amount to) IRE_PREFIX routes with the
15639 		 * loopback address as the gateway.  This is primarily done to
15640 		 * set up prefixes with the RTF_REJECT flag set (for example,
15641 		 * when generating aggregate routes.)
15642 		 *
15643 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15644 		 * IRE_LOOPBACK, then we map the request into a
15645 		 * IRE_IF_NORESOLVER.
15646 		 */
15647 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15648 			ire->ire_type = IRE_IF_NORESOLVER;
15649 		/*
15650 		 * ire held by ire_add, will be refreled' towards the
15651 		 * the end of ipif_up_done
15652 		 */
15653 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15654 		*irep = ire;
15655 		irep++;
15656 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15657 	}
15658 	mutex_exit(&ipif->ipif_saved_ire_lock);
15659 	return (ipif_saved_irep);
15660 }
15661 
15662 /*
15663  * Used to set the netmask and broadcast address to default values when the
15664  * interface is brought up.  (Always called as writer.)
15665  */
15666 static void
15667 ipif_set_default(ipif_t *ipif)
15668 {
15669 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15670 
15671 	if (!ipif->ipif_isv6) {
15672 		/*
15673 		 * Interface holds an IPv4 address. Default
15674 		 * mask is the natural netmask.
15675 		 */
15676 		if (!ipif->ipif_net_mask) {
15677 			ipaddr_t	v4mask;
15678 
15679 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15680 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15681 		}
15682 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15683 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15684 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15685 		} else {
15686 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15687 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15688 		}
15689 		/*
15690 		 * NOTE: SunOS 4.X does this even if the broadcast address
15691 		 * has been already set thus we do the same here.
15692 		 */
15693 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15694 			ipaddr_t	v4addr;
15695 
15696 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15697 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15698 		}
15699 	} else {
15700 		/*
15701 		 * Interface holds an IPv6-only address.  Default
15702 		 * mask is all-ones.
15703 		 */
15704 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15705 			ipif->ipif_v6net_mask = ipv6_all_ones;
15706 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15707 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15708 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15709 		} else {
15710 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15711 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15712 		}
15713 	}
15714 }
15715 
15716 /*
15717  * Return 0 if this address can be used as local address without causing
15718  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15719  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15720  * Note that the same IPv6 link-local address is allowed as long as the ills
15721  * are not on the same link.
15722  */
15723 int
15724 ip_addr_availability_check(ipif_t *new_ipif)
15725 {
15726 	in6_addr_t our_v6addr;
15727 	ill_t *ill;
15728 	ipif_t *ipif;
15729 	ill_walk_context_t ctx;
15730 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15731 
15732 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15733 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15734 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15735 
15736 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15737 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15738 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15739 		return (0);
15740 
15741 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15742 
15743 	if (new_ipif->ipif_isv6)
15744 		ill = ILL_START_WALK_V6(&ctx, ipst);
15745 	else
15746 		ill = ILL_START_WALK_V4(&ctx, ipst);
15747 
15748 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15749 		for (ipif = ill->ill_ipif; ipif != NULL;
15750 		    ipif = ipif->ipif_next) {
15751 			if ((ipif == new_ipif) ||
15752 			    !(ipif->ipif_flags & IPIF_UP) ||
15753 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15754 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15755 			    &our_v6addr))
15756 				continue;
15757 
15758 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15759 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15760 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15761 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15762 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15763 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15764 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15765 				continue;
15766 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15767 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15768 				continue;
15769 			else if (new_ipif->ipif_ill == ill)
15770 				return (EADDRINUSE);
15771 			else
15772 				return (EADDRNOTAVAIL);
15773 		}
15774 	}
15775 
15776 	return (0);
15777 }
15778 
15779 /*
15780  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15781  * IREs for the ipif.
15782  * When the routine returns EINPROGRESS then mp has been consumed and
15783  * the ioctl will be acked from ip_rput_dlpi.
15784  */
15785 int
15786 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15787 {
15788 	ill_t		*ill = ipif->ipif_ill;
15789 	boolean_t 	isv6 = ipif->ipif_isv6;
15790 	int		err = 0;
15791 	boolean_t	success;
15792 	uint_t		ipif_orig_id;
15793 	ip_stack_t	*ipst = ill->ill_ipst;
15794 
15795 	ASSERT(IAM_WRITER_IPIF(ipif));
15796 
15797 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15798 
15799 	/* Shouldn't get here if it is already up. */
15800 	if (ipif->ipif_flags & IPIF_UP)
15801 		return (EALREADY);
15802 
15803 	/*
15804 	 * If this is a request to bring up a data address on an interface
15805 	 * under IPMP, then move the address to its IPMP meta-interface and
15806 	 * try to bring it up.  One complication is that the zeroth ipif for
15807 	 * an ill is special, in that every ill always has one, and that code
15808 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15809 	 */
15810 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15811 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15812 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15813 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15814 
15815 		/*
15816 		 * The ipif being brought up should be quiesced.  If it's not,
15817 		 * something has gone amiss and we need to bail out.  (If it's
15818 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15819 		 */
15820 		mutex_enter(&ill->ill_lock);
15821 		if (!ipif_is_quiescent(ipif)) {
15822 			mutex_exit(&ill->ill_lock);
15823 			return (EINVAL);
15824 		}
15825 		mutex_exit(&ill->ill_lock);
15826 
15827 		/*
15828 		 * If we're going to need to allocate ipifs, do it prior
15829 		 * to starting the move (and grabbing locks).
15830 		 */
15831 		if (ipif->ipif_id == 0) {
15832 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15833 			    B_FALSE);
15834 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15835 			    B_FALSE);
15836 			if (moveipif == NULL || stubipif == NULL) {
15837 				mi_free(moveipif);
15838 				mi_free(stubipif);
15839 				return (ENOMEM);
15840 			}
15841 		}
15842 
15843 		/*
15844 		 * Grab or transfer the ipif to move.  During the move, keep
15845 		 * ill_g_lock held to prevent any ill walker threads from
15846 		 * seeing things in an inconsistent state.
15847 		 */
15848 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15849 		if (ipif->ipif_id != 0) {
15850 			ipif_remove(ipif);
15851 		} else {
15852 			ipif_transfer(ipif, moveipif, stubipif);
15853 			ipif = moveipif;
15854 		}
15855 
15856 		/*
15857 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
15858 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
15859 		 * replace that one.  Otherwise, pick the next available slot.
15860 		 */
15861 		ipif->ipif_ill = ipmp_ill;
15862 		ipif_orig_id = ipif->ipif_id;
15863 
15864 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
15865 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
15866 			ipif = ipmp_ill->ill_ipif;
15867 		} else {
15868 			ipif->ipif_id = -1;
15869 			if (ipif_insert(ipif, B_FALSE) != 0) {
15870 				/*
15871 				 * No more available ipif_id's -- put it back
15872 				 * on the original ill and fail the operation.
15873 				 * Since we're writer on the ill, we can be
15874 				 * sure our old slot is still available.
15875 				 */
15876 				ipif->ipif_id = ipif_orig_id;
15877 				ipif->ipif_ill = ill;
15878 				if (ipif_orig_id == 0) {
15879 					ipif_transfer(ipif, ill->ill_ipif,
15880 					    NULL);
15881 				} else {
15882 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
15883 				}
15884 				rw_exit(&ipst->ips_ill_g_lock);
15885 				return (ENOMEM);
15886 			}
15887 		}
15888 		rw_exit(&ipst->ips_ill_g_lock);
15889 
15890 		/*
15891 		 * Tell SCTP that the ipif has moved.  Note that even if we
15892 		 * had to allocate a new ipif, the original sequence id was
15893 		 * preserved and therefore SCTP won't know.
15894 		 */
15895 		sctp_move_ipif(ipif, ill, ipmp_ill);
15896 
15897 		/*
15898 		 * If the ipif being brought up was on slot zero, then we
15899 		 * first need to bring up the placeholder we stuck there.  In
15900 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
15901 		 * to ipif_up() itself, if we successfully bring up the
15902 		 * placeholder, we'll check ill_move_ipif and bring it up too.
15903 		 */
15904 		if (ipif_orig_id == 0) {
15905 			ASSERT(ill->ill_move_ipif == NULL);
15906 			ill->ill_move_ipif = ipif;
15907 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
15908 				ASSERT(ill->ill_move_ipif == NULL);
15909 			if (err != EINPROGRESS)
15910 				ill->ill_move_ipif = NULL;
15911 			return (err);
15912 		}
15913 
15914 		/*
15915 		 * Bring it up on the IPMP ill.
15916 		 */
15917 		return (ipif_up(ipif, q, mp));
15918 	}
15919 
15920 	/* Skip arp/ndp for any loopback interface. */
15921 	if (ill->ill_wq != NULL) {
15922 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
15923 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
15924 
15925 		if (!ill->ill_dl_up) {
15926 			/*
15927 			 * ill_dl_up is not yet set. i.e. we are yet to
15928 			 * DL_BIND with the driver and this is the first
15929 			 * logical interface on the ill to become "up".
15930 			 * Tell the driver to get going (via DL_BIND_REQ).
15931 			 * Note that changing "significant" IFF_ flags
15932 			 * address/netmask etc cause a down/up dance, but
15933 			 * does not cause an unbind (DL_UNBIND) with the driver
15934 			 */
15935 			return (ill_dl_up(ill, ipif, mp, q));
15936 		}
15937 
15938 		/*
15939 		 * ipif_resolver_up may end up sending an
15940 		 * AR_INTERFACE_UP message to ARP, which would, in
15941 		 * turn send a DLPI message to the driver. ioctls are
15942 		 * serialized and so we cannot send more than one
15943 		 * interface up message at a time. If ipif_resolver_up
15944 		 * does send an interface up message to ARP, we get
15945 		 * EINPROGRESS and we will complete in ip_arp_done.
15946 		 */
15947 
15948 		ASSERT(connp != NULL || !CONN_Q(q));
15949 		if (connp != NULL)
15950 			mutex_enter(&connp->conn_lock);
15951 		mutex_enter(&ill->ill_lock);
15952 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
15953 		mutex_exit(&ill->ill_lock);
15954 		if (connp != NULL)
15955 			mutex_exit(&connp->conn_lock);
15956 		if (!success)
15957 			return (EINTR);
15958 
15959 		/*
15960 		 * Crank up the resolver.  For IPv6, this cranks up the
15961 		 * external resolver if one is configured, but even if an
15962 		 * external resolver isn't configured, it must be called to
15963 		 * reset DAD state.  For IPv6, if an external resolver is not
15964 		 * being used, ipif_resolver_up() will never return
15965 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
15966 		 * Note that if an external resolver is being used, there's no
15967 		 * need to call ipif_ndp_up() since it will do nothing.
15968 		 */
15969 		err = ipif_resolver_up(ipif, Res_act_initial);
15970 		if (err == EINPROGRESS) {
15971 			/* We will complete it in ip_arp_done() */
15972 			return (err);
15973 		}
15974 
15975 		if (isv6 && err == 0)
15976 			err = ipif_ndp_up(ipif, B_TRUE);
15977 
15978 		ASSERT(err != EINPROGRESS);
15979 		mp = ipsq_pending_mp_get(ipsq, &connp);
15980 		ASSERT(mp != NULL);
15981 		if (err != 0)
15982 			return (err);
15983 	} else {
15984 		/*
15985 		 * Interfaces without underlying hardware don't do duplicate
15986 		 * address detection.
15987 		 */
15988 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
15989 		ipif->ipif_addr_ready = 1;
15990 	}
15991 
15992 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
15993 	if (err == 0 && ill->ill_move_ipif != NULL) {
15994 		ipif = ill->ill_move_ipif;
15995 		ill->ill_move_ipif = NULL;
15996 		return (ipif_up(ipif, q, mp));
15997 	}
15998 	return (err);
15999 }
16000 
16001 /*
16002  * Perform a bind for the physical device.
16003  * When the routine returns EINPROGRESS then mp has been consumed and
16004  * the ioctl will be acked from ip_rput_dlpi.
16005  * Allocate an unbind message and save it until ipif_down.
16006  */
16007 static int
16008 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16009 {
16010 	areq_t	*areq;
16011 	mblk_t	*areq_mp = NULL;
16012 	mblk_t	*bind_mp = NULL;
16013 	mblk_t	*unbind_mp = NULL;
16014 	conn_t	*connp;
16015 	boolean_t success;
16016 	uint16_t sap_addr;
16017 
16018 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16019 	ASSERT(IAM_WRITER_ILL(ill));
16020 	ASSERT(mp != NULL);
16021 
16022 	/* Create a resolver cookie for ARP */
16023 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16024 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16025 		if (areq_mp == NULL)
16026 			return (ENOMEM);
16027 
16028 		freemsg(ill->ill_resolver_mp);
16029 		ill->ill_resolver_mp = areq_mp;
16030 		areq = (areq_t *)areq_mp->b_rptr;
16031 		sap_addr = ill->ill_sap;
16032 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16033 	}
16034 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16035 	    DL_BIND_REQ);
16036 	if (bind_mp == NULL)
16037 		goto bad;
16038 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16039 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16040 
16041 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16042 	if (unbind_mp == NULL)
16043 		goto bad;
16044 
16045 	/*
16046 	 * Record state needed to complete this operation when the
16047 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16048 	 */
16049 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16050 	ASSERT(connp != NULL || !CONN_Q(q));
16051 	GRAB_CONN_LOCK(q);
16052 	mutex_enter(&ipif->ipif_ill->ill_lock);
16053 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16054 	mutex_exit(&ipif->ipif_ill->ill_lock);
16055 	RELEASE_CONN_LOCK(q);
16056 	if (!success)
16057 		goto bad;
16058 
16059 	/*
16060 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16061 	 * the interface goes down.
16062 	 */
16063 	ASSERT(ill->ill_unbind_mp == NULL);
16064 	ill->ill_unbind_mp = unbind_mp;
16065 
16066 	ill_dlpi_send(ill, bind_mp);
16067 	/* Send down link-layer capabilities probe if not already done. */
16068 	ill_capability_probe(ill);
16069 
16070 	/*
16071 	 * Sysid used to rely on the fact that netboots set domainname
16072 	 * and the like. Now that miniroot boots aren't strictly netboots
16073 	 * and miniroot network configuration is driven from userland
16074 	 * these things still need to be set. This situation can be detected
16075 	 * by comparing the interface being configured here to the one
16076 	 * dhcifname was set to reference by the boot loader. Once sysid is
16077 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16078 	 */
16079 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16080 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16081 	    (strlen(srpc_domain) == 0)) {
16082 		if (dhcpinit() != 0)
16083 			cmn_err(CE_WARN, "no cached dhcp response");
16084 	}
16085 
16086 	/*
16087 	 * This operation will complete in ip_rput_dlpi with either
16088 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16089 	 */
16090 	return (EINPROGRESS);
16091 bad:
16092 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16093 
16094 	freemsg(bind_mp);
16095 	freemsg(unbind_mp);
16096 	return (ENOMEM);
16097 }
16098 
16099 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16100 
16101 /*
16102  * DLPI and ARP is up.
16103  * Create all the IREs associated with an interface bring up multicast.
16104  * Set the interface flag and finish other initialization
16105  * that potentially had to be differed to after DL_BIND_ACK.
16106  */
16107 int
16108 ipif_up_done(ipif_t *ipif)
16109 {
16110 	ire_t	*ire_array[20];
16111 	ire_t	**irep = ire_array;
16112 	ire_t	**irep1;
16113 	ipaddr_t net_mask = 0;
16114 	ipaddr_t subnet_mask, route_mask;
16115 	ill_t	*ill = ipif->ipif_ill;
16116 	queue_t	*stq;
16117 	ipif_t	 *src_ipif;
16118 	ipif_t   *tmp_ipif;
16119 	boolean_t	flush_ire_cache = B_TRUE;
16120 	int	err = 0;
16121 	ire_t	**ipif_saved_irep = NULL;
16122 	int ipif_saved_ire_cnt;
16123 	int	cnt;
16124 	boolean_t	src_ipif_held = B_FALSE;
16125 	boolean_t	loopback = B_FALSE;
16126 	ip_stack_t	*ipst = ill->ill_ipst;
16127 
16128 	ip1dbg(("ipif_up_done(%s:%u)\n",
16129 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16130 	/* Check if this is a loopback interface */
16131 	if (ipif->ipif_ill->ill_wq == NULL)
16132 		loopback = B_TRUE;
16133 
16134 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16135 	/*
16136 	 * If all other interfaces for this ill are down or DEPRECATED,
16137 	 * or otherwise unsuitable for source address selection, remove
16138 	 * any IRE_CACHE entries for this ill to make sure source
16139 	 * address selection gets to take this new ipif into account.
16140 	 * No need to hold ill_lock while traversing the ipif list since
16141 	 * we are writer
16142 	 */
16143 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16144 	    tmp_ipif = tmp_ipif->ipif_next) {
16145 		if (((tmp_ipif->ipif_flags &
16146 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16147 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16148 		    (tmp_ipif == ipif))
16149 			continue;
16150 		/* first useable pre-existing interface */
16151 		flush_ire_cache = B_FALSE;
16152 		break;
16153 	}
16154 	if (flush_ire_cache)
16155 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16156 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16157 
16158 	/*
16159 	 * Figure out which way the send-to queue should go.  Only
16160 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16161 	 * should show up here.
16162 	 */
16163 	switch (ill->ill_net_type) {
16164 	case IRE_IF_RESOLVER:
16165 		stq = ill->ill_rq;
16166 		break;
16167 	case IRE_IF_NORESOLVER:
16168 	case IRE_LOOPBACK:
16169 		stq = ill->ill_wq;
16170 		break;
16171 	default:
16172 		return (EINVAL);
16173 	}
16174 
16175 	if (IS_LOOPBACK(ill)) {
16176 		/*
16177 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16178 		 * ipif_lookup_on_name(), but in the case of zones we can have
16179 		 * several loopback addresses on lo0. So all the interfaces with
16180 		 * loopback addresses need to be marked IRE_LOOPBACK.
16181 		 */
16182 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16183 		    htonl(INADDR_LOOPBACK))
16184 			ipif->ipif_ire_type = IRE_LOOPBACK;
16185 		else
16186 			ipif->ipif_ire_type = IRE_LOCAL;
16187 	}
16188 
16189 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16190 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16191 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16192 		/*
16193 		 * Can't use our source address. Select a different
16194 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16195 		 */
16196 		src_ipif = ipif_select_source(ipif->ipif_ill,
16197 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16198 		if (src_ipif == NULL)
16199 			src_ipif = ipif;	/* Last resort */
16200 		else
16201 			src_ipif_held = B_TRUE;
16202 	} else {
16203 		src_ipif = ipif;
16204 	}
16205 
16206 	/* Create all the IREs associated with this interface */
16207 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16208 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16209 
16210 		/*
16211 		 * If we're on a labeled system then make sure that zone-
16212 		 * private addresses have proper remote host database entries.
16213 		 */
16214 		if (is_system_labeled() &&
16215 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16216 		    !tsol_check_interface_address(ipif))
16217 			return (EINVAL);
16218 
16219 		/* Register the source address for __sin6_src_id */
16220 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16221 		    ipif->ipif_zoneid, ipst);
16222 		if (err != 0) {
16223 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16224 			return (err);
16225 		}
16226 
16227 		/* If the interface address is set, create the local IRE. */
16228 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16229 		    (void *)ipif,
16230 		    ipif->ipif_ire_type,
16231 		    ntohl(ipif->ipif_lcl_addr)));
16232 		*irep++ = ire_create(
16233 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16234 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16235 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16236 		    NULL,				/* no gateway */
16237 		    &ip_loopback_mtuplus,		/* max frag size */
16238 		    NULL,
16239 		    ipif->ipif_rq,			/* recv-from queue */
16240 		    NULL,				/* no send-to queue */
16241 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16242 		    ipif,
16243 		    0,
16244 		    0,
16245 		    0,
16246 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16247 		    RTF_PRIVATE : 0,
16248 		    &ire_uinfo_null,
16249 		    NULL,
16250 		    NULL,
16251 		    ipst);
16252 	} else {
16253 		ip1dbg((
16254 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16255 		    ipif->ipif_ire_type,
16256 		    ntohl(ipif->ipif_lcl_addr),
16257 		    (uint_t)ipif->ipif_flags));
16258 	}
16259 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16260 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16261 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16262 	} else {
16263 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16264 	}
16265 
16266 	subnet_mask = ipif->ipif_net_mask;
16267 
16268 	/*
16269 	 * If mask was not specified, use natural netmask of
16270 	 * interface address. Also, store this mask back into the
16271 	 * ipif struct.
16272 	 */
16273 	if (subnet_mask == 0) {
16274 		subnet_mask = net_mask;
16275 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16276 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16277 		    ipif->ipif_v6subnet);
16278 	}
16279 
16280 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16281 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16282 	    ipif->ipif_subnet != INADDR_ANY) {
16283 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16284 
16285 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16286 			route_mask = IP_HOST_MASK;
16287 		} else {
16288 			route_mask = subnet_mask;
16289 		}
16290 
16291 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16292 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16293 		    (void *)ipif, (void *)ill,
16294 		    ill->ill_net_type,
16295 		    ntohl(ipif->ipif_subnet)));
16296 		*irep++ = ire_create(
16297 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16298 		    (uchar_t *)&route_mask,		/* mask */
16299 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16300 		    NULL,				/* no gateway */
16301 		    &ipif->ipif_mtu,			/* max frag */
16302 		    NULL,
16303 		    NULL,				/* no recv queue */
16304 		    stq,				/* send-to queue */
16305 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16306 		    ipif,
16307 		    0,
16308 		    0,
16309 		    0,
16310 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16311 		    &ire_uinfo_null,
16312 		    NULL,
16313 		    NULL,
16314 		    ipst);
16315 	}
16316 
16317 	/*
16318 	 * Create any necessary broadcast IREs.
16319 	 */
16320 	if (ipif->ipif_flags & IPIF_BROADCAST)
16321 		irep = ipif_create_bcast_ires(ipif, irep);
16322 
16323 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16324 
16325 	/* If an earlier ire_create failed, get out now */
16326 	for (irep1 = irep; irep1 > ire_array; ) {
16327 		irep1--;
16328 		if (*irep1 == NULL) {
16329 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16330 			err = ENOMEM;
16331 			goto bad;
16332 		}
16333 	}
16334 
16335 	/*
16336 	 * Need to atomically check for IP address availability under
16337 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16338 	 * ills or new ipifs can be added while we are checking availability.
16339 	 */
16340 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16341 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16342 	/* Mark it up, and increment counters. */
16343 	ipif->ipif_flags |= IPIF_UP;
16344 	ill->ill_ipif_up_count++;
16345 	err = ip_addr_availability_check(ipif);
16346 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16347 	rw_exit(&ipst->ips_ill_g_lock);
16348 
16349 	if (err != 0) {
16350 		/*
16351 		 * Our address may already be up on the same ill. In this case,
16352 		 * the ARP entry for our ipif replaced the one for the other
16353 		 * ipif. So we don't want to delete it (otherwise the other ipif
16354 		 * would be unable to send packets).
16355 		 * ip_addr_availability_check() identifies this case for us and
16356 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16357 		 * which is the expected error code.
16358 		 */
16359 		if (err == EADDRINUSE) {
16360 			freemsg(ipif->ipif_arp_del_mp);
16361 			ipif->ipif_arp_del_mp = NULL;
16362 			err = EADDRNOTAVAIL;
16363 		}
16364 		ill->ill_ipif_up_count--;
16365 		ipif->ipif_flags &= ~IPIF_UP;
16366 		goto bad;
16367 	}
16368 
16369 	/*
16370 	 * Add in all newly created IREs.  ire_create_bcast() has
16371 	 * already checked for duplicates of the IRE_BROADCAST type.
16372 	 */
16373 	for (irep1 = irep; irep1 > ire_array; ) {
16374 		irep1--;
16375 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16376 		/*
16377 		 * refheld by ire_add. refele towards the end of the func
16378 		 */
16379 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16380 	}
16381 
16382 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16383 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16384 	ipif_saved_irep = ipif_recover_ire(ipif);
16385 
16386 	if (!loopback) {
16387 		/*
16388 		 * If the broadcast address has been set, make sure it makes
16389 		 * sense based on the interface address.
16390 		 * Only match on ill since we are sharing broadcast addresses.
16391 		 */
16392 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16393 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16394 			ire_t	*ire;
16395 
16396 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16397 			    IRE_BROADCAST, ipif, ALL_ZONES,
16398 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16399 
16400 			if (ire == NULL) {
16401 				/*
16402 				 * If there isn't a matching broadcast IRE,
16403 				 * revert to the default for this netmask.
16404 				 */
16405 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16406 				mutex_enter(&ipif->ipif_ill->ill_lock);
16407 				ipif_set_default(ipif);
16408 				mutex_exit(&ipif->ipif_ill->ill_lock);
16409 			} else {
16410 				ire_refrele(ire);
16411 			}
16412 		}
16413 
16414 	}
16415 
16416 	if (ill->ill_need_recover_multicast) {
16417 		/*
16418 		 * Need to recover all multicast memberships in the driver.
16419 		 * This had to be deferred until we had attached.  The same
16420 		 * code exists in ipif_up_done_v6() to recover IPv6
16421 		 * memberships.
16422 		 *
16423 		 * Note that it would be preferable to unconditionally do the
16424 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16425 		 * that since ill_join_allmulti() depends on ill_dl_up being
16426 		 * set, and it is not set until we receive a DL_BIND_ACK after
16427 		 * having called ill_dl_up().
16428 		 */
16429 		ill_recover_multicast(ill);
16430 	}
16431 
16432 	if (ill->ill_ipif_up_count == 1) {
16433 		/*
16434 		 * Since the interface is now up, it may now be active.
16435 		 */
16436 		if (IS_UNDER_IPMP(ill))
16437 			ipmp_ill_refresh_active(ill);
16438 
16439 		/*
16440 		 * If this is an IPMP interface, we may now be able to
16441 		 * establish ARP entries.
16442 		 */
16443 		if (IS_IPMP(ill))
16444 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16445 	}
16446 
16447 	/* Join the allhosts multicast address */
16448 	ipif_multicast_up(ipif);
16449 
16450 	/*
16451 	 * See if anybody else would benefit from our new ipif.
16452 	 */
16453 	if (!loopback &&
16454 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16455 		ill_update_source_selection(ill);
16456 	}
16457 
16458 	for (irep1 = irep; irep1 > ire_array; ) {
16459 		irep1--;
16460 		if (*irep1 != NULL) {
16461 			/* was held in ire_add */
16462 			ire_refrele(*irep1);
16463 		}
16464 	}
16465 
16466 	cnt = ipif_saved_ire_cnt;
16467 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16468 		if (*irep1 != NULL) {
16469 			/* was held in ire_add */
16470 			ire_refrele(*irep1);
16471 		}
16472 	}
16473 
16474 	if (!loopback && ipif->ipif_addr_ready) {
16475 		/* Broadcast an address mask reply. */
16476 		ipif_mask_reply(ipif);
16477 	}
16478 	if (ipif_saved_irep != NULL) {
16479 		kmem_free(ipif_saved_irep,
16480 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16481 	}
16482 	if (src_ipif_held)
16483 		ipif_refrele(src_ipif);
16484 
16485 	/*
16486 	 * This had to be deferred until we had bound.  Tell routing sockets and
16487 	 * others that this interface is up if it looks like the address has
16488 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16489 	 * duplicate address detection to do its thing.
16490 	 */
16491 	if (ipif->ipif_addr_ready)
16492 		ipif_up_notify(ipif);
16493 	return (0);
16494 
16495 bad:
16496 	ip1dbg(("ipif_up_done: FAILED \n"));
16497 
16498 	while (irep > ire_array) {
16499 		irep--;
16500 		if (*irep != NULL)
16501 			ire_delete(*irep);
16502 	}
16503 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16504 
16505 	if (ipif_saved_irep != NULL) {
16506 		kmem_free(ipif_saved_irep,
16507 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16508 	}
16509 	if (src_ipif_held)
16510 		ipif_refrele(src_ipif);
16511 
16512 	ipif_resolver_down(ipif);
16513 	return (err);
16514 }
16515 
16516 /*
16517  * Turn off the ARP with the ILLF_NOARP flag.
16518  */
16519 static int
16520 ill_arp_off(ill_t *ill)
16521 {
16522 	mblk_t	*arp_off_mp = NULL;
16523 	mblk_t	*arp_on_mp = NULL;
16524 
16525 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16526 
16527 	ASSERT(IAM_WRITER_ILL(ill));
16528 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16529 
16530 	/*
16531 	 * If the on message is still around we've already done
16532 	 * an arp_off without doing an arp_on thus there is no
16533 	 * work needed.
16534 	 */
16535 	if (ill->ill_arp_on_mp != NULL)
16536 		return (0);
16537 
16538 	/*
16539 	 * Allocate an ARP on message (to be saved) and an ARP off message
16540 	 */
16541 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16542 	if (!arp_off_mp)
16543 		return (ENOMEM);
16544 
16545 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16546 	if (!arp_on_mp)
16547 		goto failed;
16548 
16549 	ASSERT(ill->ill_arp_on_mp == NULL);
16550 	ill->ill_arp_on_mp = arp_on_mp;
16551 
16552 	/* Send an AR_INTERFACE_OFF request */
16553 	putnext(ill->ill_rq, arp_off_mp);
16554 	return (0);
16555 failed:
16556 
16557 	if (arp_off_mp)
16558 		freemsg(arp_off_mp);
16559 	return (ENOMEM);
16560 }
16561 
16562 /*
16563  * Turn on ARP by turning off the ILLF_NOARP flag.
16564  */
16565 static int
16566 ill_arp_on(ill_t *ill)
16567 {
16568 	mblk_t	*mp;
16569 
16570 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16571 
16572 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16573 
16574 	ASSERT(IAM_WRITER_ILL(ill));
16575 	/*
16576 	 * Send an AR_INTERFACE_ON request if we have already done
16577 	 * an arp_off (which allocated the message).
16578 	 */
16579 	if (ill->ill_arp_on_mp != NULL) {
16580 		mp = ill->ill_arp_on_mp;
16581 		ill->ill_arp_on_mp = NULL;
16582 		putnext(ill->ill_rq, mp);
16583 	}
16584 	return (0);
16585 }
16586 
16587 /*
16588  * Checks for availbility of a usable source address (if there is one) when the
16589  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16590  * this selection is done regardless of the destination.
16591  */
16592 boolean_t
16593 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16594 {
16595 	uint_t	ifindex;
16596 	ipif_t	*ipif = NULL;
16597 	ill_t	*uill;
16598 	boolean_t isv6;
16599 	ip_stack_t	*ipst = ill->ill_ipst;
16600 
16601 	ASSERT(ill != NULL);
16602 
16603 	isv6 = ill->ill_isv6;
16604 	ifindex = ill->ill_usesrc_ifindex;
16605 	if (ifindex != 0) {
16606 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16607 		    NULL, ipst);
16608 		if (uill == NULL)
16609 			return (NULL);
16610 		mutex_enter(&uill->ill_lock);
16611 		for (ipif = uill->ill_ipif; ipif != NULL;
16612 		    ipif = ipif->ipif_next) {
16613 			if (!IPIF_CAN_LOOKUP(ipif))
16614 				continue;
16615 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16616 				continue;
16617 			if (!(ipif->ipif_flags & IPIF_UP))
16618 				continue;
16619 			if (ipif->ipif_zoneid != zoneid)
16620 				continue;
16621 			if ((isv6 &&
16622 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16623 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16624 				continue;
16625 			mutex_exit(&uill->ill_lock);
16626 			ill_refrele(uill);
16627 			return (B_TRUE);
16628 		}
16629 		mutex_exit(&uill->ill_lock);
16630 		ill_refrele(uill);
16631 	}
16632 	return (B_FALSE);
16633 }
16634 
16635 /*
16636  * IP source address type, sorted from worst to best.  For a given type,
16637  * always prefer IP addresses on the same subnet.  All-zones addresses are
16638  * suboptimal because they pose problems with unlabeled destinations.
16639  */
16640 typedef enum {
16641 	IPIF_NONE,
16642 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16643 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16644 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16645 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16646 	IPIF_DIFFNET,			/* normal and different subnet */
16647 	IPIF_SAMENET			/* normal and same subnet */
16648 } ipif_type_t;
16649 
16650 /*
16651  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16652  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16653  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16654  * the first one, unless IPMP is used in which case we round-robin among them;
16655  * see below for more.
16656  *
16657  * Returns NULL if there is no suitable source address for the ill.
16658  * This only occurs when there is no valid source address for the ill.
16659  */
16660 ipif_t *
16661 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16662 {
16663 	ill_t	*usill = NULL;
16664 	ill_t	*ipmp_ill = NULL;
16665 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16666 	ipif_type_t type, best_type;
16667 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16668 	ip_stack_t *ipst = ill->ill_ipst;
16669 	boolean_t samenet;
16670 
16671 	if (ill->ill_usesrc_ifindex != 0) {
16672 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16673 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16674 		if (usill != NULL)
16675 			ill = usill;	/* Select source from usesrc ILL */
16676 		else
16677 			return (NULL);
16678 	}
16679 
16680 	/*
16681 	 * Test addresses should never be used for source address selection,
16682 	 * so if we were passed one, switch to the IPMP meta-interface.
16683 	 */
16684 	if (IS_UNDER_IPMP(ill)) {
16685 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16686 			ill = ipmp_ill;	/* Select source from IPMP ill */
16687 		else
16688 			return (NULL);
16689 	}
16690 
16691 	/*
16692 	 * If we're dealing with an unlabeled destination on a labeled system,
16693 	 * make sure that we ignore source addresses that are incompatible with
16694 	 * the destination's default label.  That destination's default label
16695 	 * must dominate the minimum label on the source address.
16696 	 */
16697 	dst_rhtp = NULL;
16698 	if (is_system_labeled()) {
16699 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16700 		if (dst_rhtp == NULL)
16701 			return (NULL);
16702 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16703 			TPC_RELE(dst_rhtp);
16704 			dst_rhtp = NULL;
16705 		}
16706 	}
16707 
16708 	/*
16709 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16710 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16711 	 * After selecting the right ipif, under ill_lock make sure ipif is
16712 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16713 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16714 	 * but not under a lock.
16715 	 */
16716 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16717 retry:
16718 	/*
16719 	 * For source address selection, we treat the ipif list as circular
16720 	 * and continue until we get back to where we started.  This allows
16721 	 * IPMP to vary source address selection (which improves inbound load
16722 	 * spreading) by caching its last ending point and starting from
16723 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16724 	 * ills since that can't happen on the IPMP ill.
16725 	 */
16726 	start_ipif = ill->ill_ipif;
16727 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16728 		start_ipif = ill->ill_src_ipif;
16729 
16730 	ipif = start_ipif;
16731 	best_ipif = NULL;
16732 	best_type = IPIF_NONE;
16733 	do {
16734 		if ((next_ipif = ipif->ipif_next) == NULL)
16735 			next_ipif = ill->ill_ipif;
16736 
16737 		if (!IPIF_CAN_LOOKUP(ipif))
16738 			continue;
16739 		/* Always skip NOLOCAL and ANYCAST interfaces */
16740 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16741 			continue;
16742 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16743 			continue;
16744 		if (ipif->ipif_zoneid != zoneid &&
16745 		    ipif->ipif_zoneid != ALL_ZONES)
16746 			continue;
16747 
16748 		/*
16749 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16750 		 * are not valid as source addresses.
16751 		 */
16752 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16753 			continue;
16754 
16755 		/*
16756 		 * Check compatibility of local address for destination's
16757 		 * default label if we're on a labeled system.	Incompatible
16758 		 * addresses can't be used at all.
16759 		 */
16760 		if (dst_rhtp != NULL) {
16761 			boolean_t incompat;
16762 
16763 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16764 			    IPV4_VERSION, B_FALSE);
16765 			if (src_rhtp == NULL)
16766 				continue;
16767 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16768 			    src_rhtp->tpc_tp.tp_doi !=
16769 			    dst_rhtp->tpc_tp.tp_doi ||
16770 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16771 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16772 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16773 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16774 			TPC_RELE(src_rhtp);
16775 			if (incompat)
16776 				continue;
16777 		}
16778 
16779 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16780 
16781 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16782 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16783 			    IPIF_DIFFNET_DEPRECATED;
16784 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16785 			type = samenet ? IPIF_SAMENET_ALLZONES :
16786 			    IPIF_DIFFNET_ALLZONES;
16787 		} else {
16788 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16789 		}
16790 
16791 		if (type > best_type) {
16792 			best_type = type;
16793 			best_ipif = ipif;
16794 			if (best_type == IPIF_SAMENET)
16795 				break; /* can't get better */
16796 		}
16797 	} while ((ipif = next_ipif) != start_ipif);
16798 
16799 	if ((ipif = best_ipif) != NULL) {
16800 		mutex_enter(&ipif->ipif_ill->ill_lock);
16801 		if (!IPIF_CAN_LOOKUP(ipif)) {
16802 			mutex_exit(&ipif->ipif_ill->ill_lock);
16803 			goto retry;
16804 		}
16805 		ipif_refhold_locked(ipif);
16806 
16807 		/*
16808 		 * For IPMP, update the source ipif rotor to the next ipif,
16809 		 * provided we can look it up.  (We must not use it if it's
16810 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16811 		 * ipif_free() checked ill_src_ipif.)
16812 		 */
16813 		if (IS_IPMP(ill) && ipif != NULL) {
16814 			next_ipif = ipif->ipif_next;
16815 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16816 				ill->ill_src_ipif = next_ipif;
16817 			else
16818 				ill->ill_src_ipif = NULL;
16819 		}
16820 		mutex_exit(&ipif->ipif_ill->ill_lock);
16821 	}
16822 
16823 	rw_exit(&ipst->ips_ill_g_lock);
16824 	if (usill != NULL)
16825 		ill_refrele(usill);
16826 	if (ipmp_ill != NULL)
16827 		ill_refrele(ipmp_ill);
16828 	if (dst_rhtp != NULL)
16829 		TPC_RELE(dst_rhtp);
16830 
16831 #ifdef DEBUG
16832 	if (ipif == NULL) {
16833 		char buf1[INET6_ADDRSTRLEN];
16834 
16835 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
16836 		    ill->ill_name,
16837 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
16838 	} else {
16839 		char buf1[INET6_ADDRSTRLEN];
16840 		char buf2[INET6_ADDRSTRLEN];
16841 
16842 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
16843 		    ipif->ipif_ill->ill_name,
16844 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
16845 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
16846 		    buf2, sizeof (buf2))));
16847 	}
16848 #endif /* DEBUG */
16849 	return (ipif);
16850 }
16851 
16852 /*
16853  * If old_ipif is not NULL, see if ipif was derived from old
16854  * ipif and if so, recreate the interface route by re-doing
16855  * source address selection. This happens when ipif_down ->
16856  * ipif_update_other_ipifs calls us.
16857  *
16858  * If old_ipif is NULL, just redo the source address selection
16859  * if needed. This happens when ipif_up_done calls us.
16860  */
16861 static void
16862 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
16863 {
16864 	ire_t *ire;
16865 	ire_t *ipif_ire;
16866 	queue_t *stq;
16867 	ipif_t *nipif;
16868 	ill_t *ill;
16869 	boolean_t need_rele = B_FALSE;
16870 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16871 
16872 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
16873 	ASSERT(IAM_WRITER_IPIF(ipif));
16874 
16875 	ill = ipif->ipif_ill;
16876 	if (!(ipif->ipif_flags &
16877 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16878 		/*
16879 		 * Can't possibly have borrowed the source
16880 		 * from old_ipif.
16881 		 */
16882 		return;
16883 	}
16884 
16885 	/*
16886 	 * Is there any work to be done? No work if the address
16887 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
16888 	 * ipif_select_source() does not borrow addresses from
16889 	 * NOLOCAL and ANYCAST interfaces).
16890 	 */
16891 	if ((old_ipif != NULL) &&
16892 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
16893 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
16894 	    (old_ipif->ipif_flags &
16895 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
16896 		return;
16897 	}
16898 
16899 	/*
16900 	 * Perform the same checks as when creating the
16901 	 * IRE_INTERFACE in ipif_up_done.
16902 	 */
16903 	if (!(ipif->ipif_flags & IPIF_UP))
16904 		return;
16905 
16906 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
16907 	    (ipif->ipif_subnet == INADDR_ANY))
16908 		return;
16909 
16910 	ipif_ire = ipif_to_ire(ipif);
16911 	if (ipif_ire == NULL)
16912 		return;
16913 
16914 	/*
16915 	 * We know that ipif uses some other source for its
16916 	 * IRE_INTERFACE. Is it using the source of this
16917 	 * old_ipif?
16918 	 */
16919 	if (old_ipif != NULL &&
16920 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
16921 		ire_refrele(ipif_ire);
16922 		return;
16923 	}
16924 	if (ip_debug > 2) {
16925 		/* ip1dbg */
16926 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
16927 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
16928 	}
16929 
16930 	stq = ipif_ire->ire_stq;
16931 
16932 	/*
16933 	 * Can't use our source address. Select a different
16934 	 * source address for the IRE_INTERFACE.
16935 	 */
16936 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
16937 	if (nipif == NULL) {
16938 		/* Last resort - all ipif's have IPIF_NOLOCAL */
16939 		nipif = ipif;
16940 	} else {
16941 		need_rele = B_TRUE;
16942 	}
16943 
16944 	ire = ire_create(
16945 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
16946 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
16947 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
16948 	    NULL,				/* no gateway */
16949 	    &ipif->ipif_mtu,			/* max frag */
16950 	    NULL,				/* no src nce */
16951 	    NULL,				/* no recv from queue */
16952 	    stq,				/* send-to queue */
16953 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16954 	    ipif,
16955 	    0,
16956 	    0,
16957 	    0,
16958 	    0,
16959 	    &ire_uinfo_null,
16960 	    NULL,
16961 	    NULL,
16962 	    ipst);
16963 
16964 	if (ire != NULL) {
16965 		ire_t *ret_ire;
16966 		int error;
16967 
16968 		/*
16969 		 * We don't need ipif_ire anymore. We need to delete
16970 		 * before we add so that ire_add does not detect
16971 		 * duplicates.
16972 		 */
16973 		ire_delete(ipif_ire);
16974 		ret_ire = ire;
16975 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
16976 		ASSERT(error == 0);
16977 		ASSERT(ire == ret_ire);
16978 		/* Held in ire_add */
16979 		ire_refrele(ret_ire);
16980 	}
16981 	/*
16982 	 * Either we are falling through from above or could not
16983 	 * allocate a replacement.
16984 	 */
16985 	ire_refrele(ipif_ire);
16986 	if (need_rele)
16987 		ipif_refrele(nipif);
16988 }
16989 
16990 /*
16991  * This old_ipif is going away.
16992  *
16993  * Determine if any other ipif's are using our address as
16994  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
16995  * IPIF_DEPRECATED).
16996  * Find the IRE_INTERFACE for such ipifs and recreate them
16997  * to use an different source address following the rules in
16998  * ipif_up_done.
16999  */
17000 static void
17001 ipif_update_other_ipifs(ipif_t *old_ipif)
17002 {
17003 	ipif_t	*ipif;
17004 	ill_t	*ill;
17005 	char	buf[INET6_ADDRSTRLEN];
17006 
17007 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17008 
17009 	ill = old_ipif->ipif_ill;
17010 
17011 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17012 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17013 
17014 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17015 		if (ipif == old_ipif)
17016 			continue;
17017 		ipif_recreate_interface_routes(old_ipif, ipif);
17018 	}
17019 }
17020 
17021 /* ARGSUSED */
17022 int
17023 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17024 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17025 {
17026 	/*
17027 	 * ill_phyint_reinit merged the v4 and v6 into a single
17028 	 * ipsq.  We might not have been able to complete the
17029 	 * operation in ipif_set_values, if we could not become
17030 	 * exclusive.  If so restart it here.
17031 	 */
17032 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17033 }
17034 
17035 /*
17036  * Can operate on either a module or a driver queue.
17037  * Returns an error if not a module queue.
17038  */
17039 /* ARGSUSED */
17040 int
17041 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17042     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17043 {
17044 	queue_t		*q1 = q;
17045 	char 		*cp;
17046 	char		interf_name[LIFNAMSIZ];
17047 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17048 
17049 	if (q->q_next == NULL) {
17050 		ip1dbg((
17051 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17052 		return (EINVAL);
17053 	}
17054 
17055 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17056 		return (EALREADY);
17057 
17058 	do {
17059 		q1 = q1->q_next;
17060 	} while (q1->q_next);
17061 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17062 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17063 
17064 	/*
17065 	 * Here we are not going to delay the ioack until after
17066 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17067 	 * original ioctl message before sending the requests.
17068 	 */
17069 	return (ipif_set_values(q, mp, interf_name, &ppa));
17070 }
17071 
17072 /* ARGSUSED */
17073 int
17074 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17075     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17076 {
17077 	return (ENXIO);
17078 }
17079 
17080 /*
17081  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17082  * minimum (but complete) set exist.  This is necessary when adding or
17083  * removing an interface to/from an IPMP group, since interfaces in an
17084  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17085  * its test address subnets overlap with IPMP data addresses).	It's also
17086  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17087  * interface when the nominated broadcast interface changes.
17088  */
17089 void
17090 ill_refresh_bcast(ill_t *ill)
17091 {
17092 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17093 	ire_t **irep;
17094 	ipif_t *ipif;
17095 
17096 	ASSERT(!ill->ill_isv6);
17097 	ASSERT(IAM_WRITER_ILL(ill));
17098 
17099 	/*
17100 	 * Remove any old broadcast IREs.
17101 	 */
17102 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17103 	    ill_broadcast_delete, ill, ill);
17104 
17105 	/*
17106 	 * Create new ones for any ipifs that are up and broadcast-capable.
17107 	 */
17108 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17109 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17110 		    (IPIF_UP|IPIF_BROADCAST))
17111 			continue;
17112 
17113 		irep = ipif_create_bcast_ires(ipif, ire_array);
17114 		while (irep-- > ire_array) {
17115 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17116 			if (*irep != NULL)
17117 				ire_refrele(*irep);
17118 		}
17119 	}
17120 }
17121 
17122 /*
17123  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17124  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17125  * ire_check_and_create_bcast()).
17126  */
17127 static ire_t **
17128 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17129 {
17130 	ipaddr_t addr;
17131 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17132 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17133 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17134 
17135 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17136 
17137 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17138 
17139 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17140 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17141 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17142 
17143 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17144 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17145 
17146 	/*
17147 	 * For backward compatibility, we create net broadcast IREs based on
17148 	 * the old "IP address class system", since some old machines only
17149 	 * respond to these class derived net broadcast.  However, we must not
17150 	 * create these net broadcast IREs if the subnetmask is shorter than
17151 	 * the IP address class based derived netmask.  Otherwise, we may
17152 	 * create a net broadcast address which is the same as an IP address
17153 	 * on the subnet -- and then TCP will refuse to talk to that address.
17154 	 */
17155 	if (netmask < subnetmask) {
17156 		addr = netmask & ipif->ipif_subnet;
17157 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17158 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17159 		    flags);
17160 	}
17161 
17162 	/*
17163 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17164 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17165 	 * created.  Creating these broadcast IREs will only create confusion
17166 	 * as `addr' will be the same as the IP address.
17167 	 */
17168 	if (subnetmask != 0xFFFFFFFF) {
17169 		addr = ipif->ipif_subnet;
17170 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17171 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17172 		    irep, flags);
17173 	}
17174 
17175 	return (irep);
17176 }
17177 
17178 /*
17179  * Broadcast IRE info structure used in the functions below.  Since we
17180  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17181  */
17182 typedef struct bcast_ireinfo {
17183 	uchar_t		bi_type;	/* BCAST_* value from below */
17184 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17185 			bi_needrep:1,	/* do we need to replace it? */
17186 			bi_haverep:1,	/* have we replaced it? */
17187 			bi_pad:5;
17188 	ipaddr_t	bi_addr;	/* IRE address */
17189 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17190 } bcast_ireinfo_t;
17191 
17192 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17193 
17194 /*
17195  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17196  * return B_TRUE if it should immediately be used to recreate the IRE.
17197  */
17198 static boolean_t
17199 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17200 {
17201 	ipaddr_t addr;
17202 
17203 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17204 
17205 	switch (bireinfop->bi_type) {
17206 	case BCAST_NET:
17207 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17208 		if (addr != bireinfop->bi_addr)
17209 			return (B_FALSE);
17210 		break;
17211 	case BCAST_SUBNET:
17212 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17213 			return (B_FALSE);
17214 		break;
17215 	}
17216 
17217 	bireinfop->bi_needrep = 1;
17218 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17219 		if (bireinfop->bi_backup == NULL)
17220 			bireinfop->bi_backup = ipif;
17221 		return (B_FALSE);
17222 	}
17223 	return (B_TRUE);
17224 }
17225 
17226 /*
17227  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17228  * them ala ire_check_and_create_bcast().
17229  */
17230 static ire_t **
17231 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17232 {
17233 	ipaddr_t mask, addr;
17234 
17235 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17236 
17237 	addr = bireinfop->bi_addr;
17238 	irep = ire_create_bcast(ipif, addr, irep);
17239 
17240 	switch (bireinfop->bi_type) {
17241 	case BCAST_NET:
17242 		mask = ip_net_mask(ipif->ipif_subnet);
17243 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17244 		break;
17245 	case BCAST_SUBNET:
17246 		mask = ipif->ipif_net_mask;
17247 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17248 		break;
17249 	}
17250 
17251 	bireinfop->bi_haverep = 1;
17252 	return (irep);
17253 }
17254 
17255 /*
17256  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17257  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17258  * that are going away are still needed.  If so, have ipif_create_bcast()
17259  * recreate them (except for the deprecated case, as explained below).
17260  */
17261 static ire_t **
17262 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17263     ire_t **irep)
17264 {
17265 	int i;
17266 	ipif_t *ipif;
17267 
17268 	ASSERT(!ill->ill_isv6);
17269 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17270 		/*
17271 		 * Skip this ipif if it's (a) the one being taken down, (b)
17272 		 * not in the same zone, or (c) has no valid local address.
17273 		 */
17274 		if (ipif == test_ipif ||
17275 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17276 		    ipif->ipif_subnet == 0 ||
17277 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17278 		    (IPIF_UP|IPIF_BROADCAST))
17279 			continue;
17280 
17281 		/*
17282 		 * For each dying IRE that hasn't yet been replaced, see if
17283 		 * `ipif' needs it and whether the IRE should be recreated on
17284 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17285 		 * will return B_FALSE even if `ipif' needs the IRE on the
17286 		 * hopes that we'll later find a needy non-deprecated ipif.
17287 		 * However, the ipif is recorded in bi_backup for possible
17288 		 * subsequent use by ipif_check_bcast_ires().
17289 		 */
17290 		for (i = 0; i < BCAST_COUNT; i++) {
17291 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17292 				continue;
17293 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17294 				continue;
17295 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17296 		}
17297 
17298 		/*
17299 		 * If we've replaced all of the broadcast IREs that are going
17300 		 * to be taken down, we know we're done.
17301 		 */
17302 		for (i = 0; i < BCAST_COUNT; i++) {
17303 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17304 				break;
17305 		}
17306 		if (i == BCAST_COUNT)
17307 			break;
17308 	}
17309 	return (irep);
17310 }
17311 
17312 /*
17313  * Check if `test_ipif' (which is going away) is associated with any existing
17314  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17315  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17316  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17317  *
17318  * This is necessary because broadcast IREs are shared.  In particular, a
17319  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17320  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17321  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17322  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17323  * same zone, they will share the same set of broadcast IREs.
17324  *
17325  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17326  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17327  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17328  */
17329 static void
17330 ipif_check_bcast_ires(ipif_t *test_ipif)
17331 {
17332 	ill_t		*ill = test_ipif->ipif_ill;
17333 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17334 	ire_t		**irep1, **irep = &ire_array[0];
17335 	uint_t 		i, willdie;
17336 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17337 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17338 
17339 	ASSERT(!test_ipif->ipif_isv6);
17340 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17341 
17342 	/*
17343 	 * No broadcast IREs for the LOOPBACK interface
17344 	 * or others such as point to point and IPIF_NOXMIT.
17345 	 */
17346 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17347 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17348 		return;
17349 
17350 	bzero(bireinfo, sizeof (bireinfo));
17351 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17352 	bireinfo[0].bi_addr = 0;
17353 
17354 	bireinfo[1].bi_type = BCAST_ALLONES;
17355 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17356 
17357 	bireinfo[2].bi_type = BCAST_NET;
17358 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17359 
17360 	if (test_ipif->ipif_net_mask != 0)
17361 		mask = test_ipif->ipif_net_mask;
17362 	bireinfo[3].bi_type = BCAST_SUBNET;
17363 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17364 
17365 	/*
17366 	 * Figure out what (if any) broadcast IREs will die as a result of
17367 	 * `test_ipif' going away.  If none will die, we're done.
17368 	 */
17369 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17370 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17371 		    test_ipif, ALL_ZONES, NULL,
17372 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17373 		if (ire != NULL) {
17374 			willdie++;
17375 			bireinfo[i].bi_willdie = 1;
17376 			ire_refrele(ire);
17377 		}
17378 	}
17379 
17380 	if (willdie == 0)
17381 		return;
17382 
17383 	/*
17384 	 * Walk through all the ipifs that will be affected by the dying IREs,
17385 	 * and recreate the IREs as necessary. Note that all interfaces in an
17386 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17387 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17388 	 * that broadcast IREs end up on it whenever possible).
17389 	 */
17390 	if (IS_UNDER_IPMP(ill))
17391 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17392 
17393 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17394 
17395 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17396 		ipmp_illgrp_t *illg = ill->ill_grp;
17397 
17398 		ill = list_head(&illg->ig_if);
17399 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17400 			for (i = 0; i < BCAST_COUNT; i++) {
17401 				if (bireinfo[i].bi_willdie &&
17402 				    !bireinfo[i].bi_haverep)
17403 					break;
17404 			}
17405 			if (i == BCAST_COUNT)
17406 				break;
17407 
17408 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17409 		}
17410 	}
17411 
17412 	/*
17413 	 * Scan through the set of broadcast IREs and see if there are any
17414 	 * that we need to replace that have not yet been replaced.  If so,
17415 	 * replace them using the appropriate backup ipif.
17416 	 */
17417 	for (i = 0; i < BCAST_COUNT; i++) {
17418 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17419 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17420 			    &bireinfo[i], irep);
17421 	}
17422 
17423 	/*
17424 	 * If we can't create all of them, don't add any of them.  (Code in
17425 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17426 	 * non-loopback copy and loopback copy for a given address.)
17427 	 */
17428 	for (irep1 = irep; irep1 > ire_array; ) {
17429 		irep1--;
17430 		if (*irep1 == NULL) {
17431 			ip0dbg(("ipif_check_bcast_ires: can't create "
17432 			    "IRE_BROADCAST, memory allocation failure\n"));
17433 			while (irep > ire_array) {
17434 				irep--;
17435 				if (*irep != NULL)
17436 					ire_delete(*irep);
17437 			}
17438 			return;
17439 		}
17440 	}
17441 
17442 	for (irep1 = irep; irep1 > ire_array; ) {
17443 		irep1--;
17444 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17445 			ire_refrele(*irep1);		/* Held in ire_add */
17446 	}
17447 }
17448 
17449 /*
17450  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17451  * from lifr_flags and the name from lifr_name.
17452  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17453  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17454  * Returns EINPROGRESS when mp has been consumed by queueing it on
17455  * ill_pending_mp and the ioctl will complete in ip_rput.
17456  *
17457  * Can operate on either a module or a driver queue.
17458  * Returns an error if not a module queue.
17459  */
17460 /* ARGSUSED */
17461 int
17462 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17463     ip_ioctl_cmd_t *ipip, void *if_req)
17464 {
17465 	ill_t	*ill = q->q_ptr;
17466 	phyint_t *phyi;
17467 	ip_stack_t *ipst;
17468 	struct lifreq *lifr = if_req;
17469 
17470 	ASSERT(ipif != NULL);
17471 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17472 
17473 	if (q->q_next == NULL) {
17474 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17475 		return (EINVAL);
17476 	}
17477 
17478 	/*
17479 	 * If we are not writer on 'q' then this interface exists already
17480 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17481 	 * so return EALREADY.
17482 	 */
17483 	if (ill != ipif->ipif_ill)
17484 		return (EALREADY);
17485 
17486 	if (ill->ill_name[0] != '\0')
17487 		return (EALREADY);
17488 
17489 	/*
17490 	 * Set all the flags. Allows all kinds of override. Provide some
17491 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
17492 	 * unless there is either multicast/broadcast support in the driver
17493 	 * or it is a pt-pt link.
17494 	 */
17495 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
17496 		/* Meaningless to IP thus don't allow them to be set. */
17497 		ip1dbg(("ip_setname: EINVAL 1\n"));
17498 		return (EINVAL);
17499 	}
17500 
17501 	/*
17502 	 * If there's another ill already with the requested name, ensure
17503 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17504 	 * fuse together two unrelated ills, which will cause chaos.
17505 	 */
17506 	ipst = ill->ill_ipst;
17507 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17508 	    lifr->lifr_name, NULL);
17509 	if (phyi != NULL) {
17510 		ill_t *ill_mate = phyi->phyint_illv4;
17511 
17512 		if (ill_mate == NULL)
17513 			ill_mate = phyi->phyint_illv6;
17514 		ASSERT(ill_mate != NULL);
17515 
17516 		if (ill_mate->ill_media->ip_m_mac_type !=
17517 		    ill->ill_media->ip_m_mac_type) {
17518 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17519 			    "use the same ill name on differing media\n"));
17520 			return (EINVAL);
17521 		}
17522 	}
17523 
17524 	/*
17525 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
17526 	 * ill_bcast_addr_length info.
17527 	 */
17528 	if (!ill->ill_needs_attach &&
17529 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
17530 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
17531 	    ill->ill_bcast_addr_length == 0)) {
17532 		/* Link not broadcast/pt-pt capable i.e. no multicast */
17533 		ip1dbg(("ip_setname: EINVAL 2\n"));
17534 		return (EINVAL);
17535 	}
17536 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17537 	    ((lifr->lifr_flags & IFF_IPV6) ||
17538 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17539 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
17540 		ip1dbg(("ip_setname: EINVAL 3\n"));
17541 		return (EINVAL);
17542 	}
17543 	if (lifr->lifr_flags & IFF_UP) {
17544 		/* Can only be set with SIOCSLIFFLAGS */
17545 		ip1dbg(("ip_setname: EINVAL 4\n"));
17546 		return (EINVAL);
17547 	}
17548 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
17549 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
17550 		ip1dbg(("ip_setname: EINVAL 5\n"));
17551 		return (EINVAL);
17552 	}
17553 	/*
17554 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17555 	 */
17556 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
17557 	    !(lifr->lifr_flags & IFF_IPV6) &&
17558 	    !(ipif->ipif_isv6)) {
17559 		ip1dbg(("ip_setname: EINVAL 6\n"));
17560 		return (EINVAL);
17561 	}
17562 
17563 	/*
17564 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
17565 	 * we have all the flags here. So, we assign rather than we OR.
17566 	 * We can't OR the flags here because we don't want to set
17567 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
17568 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
17569 	 * on lifr_flags value here.
17570 	 */
17571 	/*
17572 	 * This ill has not been inserted into the global list.
17573 	 * So we are still single threaded and don't need any lock
17574 	 */
17575 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
17576 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
17577 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
17578 
17579 	/* We started off as V4. */
17580 	if (ill->ill_flags & ILLF_IPV6) {
17581 		ill->ill_phyint->phyint_illv6 = ill;
17582 		ill->ill_phyint->phyint_illv4 = NULL;
17583 	}
17584 
17585 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17586 }
17587 
17588 /* ARGSUSED */
17589 int
17590 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17591     ip_ioctl_cmd_t *ipip, void *if_req)
17592 {
17593 	/*
17594 	 * ill_phyint_reinit merged the v4 and v6 into a single
17595 	 * ipsq.  We might not have been able to complete the
17596 	 * slifname in ipif_set_values, if we could not become
17597 	 * exclusive.  If so restart it here
17598 	 */
17599 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17600 }
17601 
17602 /*
17603  * Return a pointer to the ipif which matches the index, IP version type and
17604  * zoneid.
17605  */
17606 ipif_t *
17607 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17608     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17609 {
17610 	ill_t	*ill;
17611 	ipif_t	*ipif = NULL;
17612 
17613 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17614 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17615 
17616 	if (err != NULL)
17617 		*err = 0;
17618 
17619 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17620 	if (ill != NULL) {
17621 		mutex_enter(&ill->ill_lock);
17622 		for (ipif = ill->ill_ipif; ipif != NULL;
17623 		    ipif = ipif->ipif_next) {
17624 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17625 			    zoneid == ipif->ipif_zoneid ||
17626 			    ipif->ipif_zoneid == ALL_ZONES)) {
17627 				ipif_refhold_locked(ipif);
17628 				break;
17629 			}
17630 		}
17631 		mutex_exit(&ill->ill_lock);
17632 		ill_refrele(ill);
17633 		if (ipif == NULL && err != NULL)
17634 			*err = ENXIO;
17635 	}
17636 	return (ipif);
17637 }
17638 
17639 /*
17640  * Change an existing physical interface's index. If the new index
17641  * is acceptable we update the index and the phyint_list_avl_by_index tree.
17642  * Finally, we update other systems which may have a dependence on the
17643  * index value.
17644  */
17645 /* ARGSUSED */
17646 int
17647 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17648     ip_ioctl_cmd_t *ipip, void *ifreq)
17649 {
17650 	ill_t		*ill;
17651 	phyint_t	*phyi;
17652 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17653 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17654 	uint_t	old_index, index;
17655 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17656 	avl_index_t	where;
17657 
17658 	if (ipip->ipi_cmd_type == IF_CMD)
17659 		index = ifr->ifr_index;
17660 	else
17661 		index = lifr->lifr_index;
17662 
17663 	/*
17664 	 * Only allow on physical interface. Also, index zero is illegal.
17665 	 */
17666 	ill = ipif->ipif_ill;
17667 	phyi = ill->ill_phyint;
17668 	if (ipif->ipif_id != 0 || index == 0) {
17669 		return (EINVAL);
17670 	}
17671 
17672 	/* If the index is not changing, no work to do */
17673 	if (phyi->phyint_ifindex == index)
17674 		return (0);
17675 
17676 	/*
17677 	 * Use phyint_exists() to determine if the new interface index
17678 	 * is already in use. If the index is unused then we need to
17679 	 * change the phyint's position in the phyint_list_avl_by_index
17680 	 * tree. If we do not do this, subsequent lookups (using the new
17681 	 * index value) will not find the phyint.
17682 	 */
17683 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17684 	if (phyint_exists(index, ipst)) {
17685 		rw_exit(&ipst->ips_ill_g_lock);
17686 		return (EEXIST);
17687 	}
17688 
17689 	/* The new index is unused. Set it in the phyint. */
17690 	old_index = phyi->phyint_ifindex;
17691 	phyi->phyint_ifindex = index;
17692 
17693 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
17694 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17695 	    &index, &where);
17696 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17697 	    phyi, where);
17698 	rw_exit(&ipst->ips_ill_g_lock);
17699 
17700 	/* Update SCTP's ILL list */
17701 	sctp_ill_reindex(ill, old_index);
17702 
17703 	/* Send the routing sockets message */
17704 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17705 	if (ILL_OTHER(ill))
17706 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17707 
17708 	return (0);
17709 }
17710 
17711 /* ARGSUSED */
17712 int
17713 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17714     ip_ioctl_cmd_t *ipip, void *ifreq)
17715 {
17716 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17717 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17718 
17719 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17720 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17721 	/* Get the interface index */
17722 	if (ipip->ipi_cmd_type == IF_CMD) {
17723 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17724 	} else {
17725 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17726 	}
17727 	return (0);
17728 }
17729 
17730 /* ARGSUSED */
17731 int
17732 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17733     ip_ioctl_cmd_t *ipip, void *ifreq)
17734 {
17735 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17736 
17737 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17738 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17739 	/* Get the interface zone */
17740 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17741 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17742 	return (0);
17743 }
17744 
17745 /*
17746  * Set the zoneid of an interface.
17747  */
17748 /* ARGSUSED */
17749 int
17750 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17751     ip_ioctl_cmd_t *ipip, void *ifreq)
17752 {
17753 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17754 	int err = 0;
17755 	boolean_t need_up = B_FALSE;
17756 	zone_t *zptr;
17757 	zone_status_t status;
17758 	zoneid_t zoneid;
17759 
17760 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17761 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17762 		if (!is_system_labeled())
17763 			return (ENOTSUP);
17764 		zoneid = GLOBAL_ZONEID;
17765 	}
17766 
17767 	/* cannot assign instance zero to a non-global zone */
17768 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17769 		return (ENOTSUP);
17770 
17771 	/*
17772 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17773 	 * the event of a race with the zone shutdown processing, since IP
17774 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17775 	 * interface will be cleaned up even if the zone is shut down
17776 	 * immediately after the status check. If the interface can't be brought
17777 	 * down right away, and the zone is shut down before the restart
17778 	 * function is called, we resolve the possible races by rechecking the
17779 	 * zone status in the restart function.
17780 	 */
17781 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17782 		return (EINVAL);
17783 	status = zone_status_get(zptr);
17784 	zone_rele(zptr);
17785 
17786 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17787 		return (EINVAL);
17788 
17789 	if (ipif->ipif_flags & IPIF_UP) {
17790 		/*
17791 		 * If the interface is already marked up,
17792 		 * we call ipif_down which will take care
17793 		 * of ditching any IREs that have been set
17794 		 * up based on the old interface address.
17795 		 */
17796 		err = ipif_logical_down(ipif, q, mp);
17797 		if (err == EINPROGRESS)
17798 			return (err);
17799 		ipif_down_tail(ipif);
17800 		need_up = B_TRUE;
17801 	}
17802 
17803 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17804 	return (err);
17805 }
17806 
17807 static int
17808 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17809     queue_t *q, mblk_t *mp, boolean_t need_up)
17810 {
17811 	int	err = 0;
17812 	ip_stack_t	*ipst;
17813 
17814 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17815 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17816 
17817 	if (CONN_Q(q))
17818 		ipst = CONNQ_TO_IPST(q);
17819 	else
17820 		ipst = ILLQ_TO_IPST(q);
17821 
17822 	/*
17823 	 * For exclusive stacks we don't allow a different zoneid than
17824 	 * global.
17825 	 */
17826 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17827 	    zoneid != GLOBAL_ZONEID)
17828 		return (EINVAL);
17829 
17830 	/* Set the new zone id. */
17831 	ipif->ipif_zoneid = zoneid;
17832 
17833 	/* Update sctp list */
17834 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
17835 
17836 	if (need_up) {
17837 		/*
17838 		 * Now bring the interface back up.  If this
17839 		 * is the only IPIF for the ILL, ipif_up
17840 		 * will have to re-bind to the device, so
17841 		 * we may get back EINPROGRESS, in which
17842 		 * case, this IOCTL will get completed in
17843 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
17844 		 */
17845 		err = ipif_up(ipif, q, mp);
17846 	}
17847 	return (err);
17848 }
17849 
17850 /* ARGSUSED */
17851 int
17852 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17853     ip_ioctl_cmd_t *ipip, void *if_req)
17854 {
17855 	struct lifreq *lifr = (struct lifreq *)if_req;
17856 	zoneid_t zoneid;
17857 	zone_t *zptr;
17858 	zone_status_t status;
17859 
17860 	ASSERT(ipif->ipif_id != 0);
17861 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17862 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
17863 		zoneid = GLOBAL_ZONEID;
17864 
17865 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
17866 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17867 
17868 	/*
17869 	 * We recheck the zone status to resolve the following race condition:
17870 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
17871 	 * 2) hme0:1 is up and can't be brought down right away;
17872 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
17873 	 * 3) zone "myzone" is halted; the zone status switches to
17874 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
17875 	 * the interfaces to remove - hme0:1 is not returned because it's not
17876 	 * yet in "myzone", so it won't be removed;
17877 	 * 4) the restart function for SIOCSLIFZONE is called; without the
17878 	 * status check here, we would have hme0:1 in "myzone" after it's been
17879 	 * destroyed.
17880 	 * Note that if the status check fails, we need to bring the interface
17881 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
17882 	 * ipif_up_done[_v6]().
17883 	 */
17884 	status = ZONE_IS_UNINITIALIZED;
17885 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
17886 		status = zone_status_get(zptr);
17887 		zone_rele(zptr);
17888 	}
17889 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
17890 		if (ipif->ipif_isv6) {
17891 			(void) ipif_up_done_v6(ipif);
17892 		} else {
17893 			(void) ipif_up_done(ipif);
17894 		}
17895 		return (EINVAL);
17896 	}
17897 
17898 	ipif_down_tail(ipif);
17899 
17900 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
17901 	    B_TRUE));
17902 }
17903 
17904 /*
17905  * Return the number of addresses on `ill' with one or more of the values
17906  * in `set' set and all of the values in `clear' clear.
17907  */
17908 static uint_t
17909 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
17910 {
17911 	ipif_t	*ipif;
17912 	uint_t	cnt = 0;
17913 
17914 	ASSERT(IAM_WRITER_ILL(ill));
17915 
17916 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
17917 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
17918 			cnt++;
17919 
17920 	return (cnt);
17921 }
17922 
17923 /*
17924  * Return the number of migratable addresses on `ill' that are under
17925  * application control.
17926  */
17927 uint_t
17928 ill_appaddr_cnt(const ill_t *ill)
17929 {
17930 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
17931 	    IPIF_NOFAILOVER));
17932 }
17933 
17934 /*
17935  * Return the number of point-to-point addresses on `ill'.
17936  */
17937 uint_t
17938 ill_ptpaddr_cnt(const ill_t *ill)
17939 {
17940 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
17941 }
17942 
17943 /* ARGSUSED */
17944 int
17945 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17946 	ip_ioctl_cmd_t *ipip, void *ifreq)
17947 {
17948 	struct lifreq	*lifr = ifreq;
17949 
17950 	ASSERT(q->q_next == NULL);
17951 	ASSERT(CONN_Q(q));
17952 
17953 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
17954 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17955 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
17956 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
17957 
17958 	return (0);
17959 }
17960 
17961 /* Find the previous ILL in this usesrc group */
17962 static ill_t *
17963 ill_prev_usesrc(ill_t *uill)
17964 {
17965 	ill_t *ill;
17966 
17967 	for (ill = uill->ill_usesrc_grp_next;
17968 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
17969 	    ill = ill->ill_usesrc_grp_next)
17970 		/* do nothing */;
17971 	return (ill);
17972 }
17973 
17974 /*
17975  * Release all members of the usesrc group. This routine is called
17976  * from ill_delete when the interface being unplumbed is the
17977  * group head.
17978  */
17979 static void
17980 ill_disband_usesrc_group(ill_t *uill)
17981 {
17982 	ill_t *next_ill, *tmp_ill;
17983 	ip_stack_t	*ipst = uill->ill_ipst;
17984 
17985 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
17986 	next_ill = uill->ill_usesrc_grp_next;
17987 
17988 	do {
17989 		ASSERT(next_ill != NULL);
17990 		tmp_ill = next_ill->ill_usesrc_grp_next;
17991 		ASSERT(tmp_ill != NULL);
17992 		next_ill->ill_usesrc_grp_next = NULL;
17993 		next_ill->ill_usesrc_ifindex = 0;
17994 		next_ill = tmp_ill;
17995 	} while (next_ill->ill_usesrc_ifindex != 0);
17996 	uill->ill_usesrc_grp_next = NULL;
17997 }
17998 
17999 /*
18000  * Remove the client usesrc ILL from the list and relink to a new list
18001  */
18002 int
18003 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18004 {
18005 	ill_t *ill, *tmp_ill;
18006 	ip_stack_t	*ipst = ucill->ill_ipst;
18007 
18008 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18009 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18010 
18011 	/*
18012 	 * Check if the usesrc client ILL passed in is not already
18013 	 * in use as a usesrc ILL i.e one whose source address is
18014 	 * in use OR a usesrc ILL is not already in use as a usesrc
18015 	 * client ILL
18016 	 */
18017 	if ((ucill->ill_usesrc_ifindex == 0) ||
18018 	    (uill->ill_usesrc_ifindex != 0)) {
18019 		return (-1);
18020 	}
18021 
18022 	ill = ill_prev_usesrc(ucill);
18023 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18024 
18025 	/* Remove from the current list */
18026 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18027 		/* Only two elements in the list */
18028 		ASSERT(ill->ill_usesrc_ifindex == 0);
18029 		ill->ill_usesrc_grp_next = NULL;
18030 	} else {
18031 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18032 	}
18033 
18034 	if (ifindex == 0) {
18035 		ucill->ill_usesrc_ifindex = 0;
18036 		ucill->ill_usesrc_grp_next = NULL;
18037 		return (0);
18038 	}
18039 
18040 	ucill->ill_usesrc_ifindex = ifindex;
18041 	tmp_ill = uill->ill_usesrc_grp_next;
18042 	uill->ill_usesrc_grp_next = ucill;
18043 	ucill->ill_usesrc_grp_next =
18044 	    (tmp_ill != NULL) ? tmp_ill : uill;
18045 	return (0);
18046 }
18047 
18048 /*
18049  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18050  * ip.c for locking details.
18051  */
18052 /* ARGSUSED */
18053 int
18054 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18055     ip_ioctl_cmd_t *ipip, void *ifreq)
18056 {
18057 	struct lifreq *lifr = (struct lifreq *)ifreq;
18058 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18059 	    ill_flag_changed = B_FALSE;
18060 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18061 	int err = 0, ret;
18062 	uint_t ifindex;
18063 	ipsq_t *ipsq = NULL;
18064 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18065 
18066 	ASSERT(IAM_WRITER_IPIF(ipif));
18067 	ASSERT(q->q_next == NULL);
18068 	ASSERT(CONN_Q(q));
18069 
18070 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18071 
18072 	ifindex = lifr->lifr_index;
18073 	if (ifindex == 0) {
18074 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18075 			/* non usesrc group interface, nothing to reset */
18076 			return (0);
18077 		}
18078 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18079 		/* valid reset request */
18080 		reset_flg = B_TRUE;
18081 	}
18082 
18083 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18084 	    ip_process_ioctl, &err, ipst);
18085 	if (usesrc_ill == NULL) {
18086 		return (err);
18087 	}
18088 
18089 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18090 	    NEW_OP, B_TRUE);
18091 	if (ipsq == NULL) {
18092 		err = EINPROGRESS;
18093 		/* Operation enqueued on the ipsq of the usesrc ILL */
18094 		goto done;
18095 	}
18096 
18097 	/* USESRC isn't currently supported with IPMP */
18098 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18099 		err = ENOTSUP;
18100 		goto done;
18101 	}
18102 
18103 	/*
18104 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18105 	 * used by IPMP underlying interfaces, but someone might think it's
18106 	 * more general and try to use it independently with VNI.)
18107 	 */
18108 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18109 		err = ENOTSUP;
18110 		goto done;
18111 	}
18112 
18113 	/*
18114 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18115 	 * already a client then return EINVAL
18116 	 */
18117 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18118 		err = EINVAL;
18119 		goto done;
18120 	}
18121 
18122 	/*
18123 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18124 	 * be then this is a duplicate operation.
18125 	 */
18126 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18127 		err = 0;
18128 		goto done;
18129 	}
18130 
18131 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18132 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18133 	    usesrc_ill->ill_isv6));
18134 
18135 	/*
18136 	 * The next step ensures that no new ires will be created referencing
18137 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18138 	 * we go through an ire walk deleting all ire caches that reference
18139 	 * the client ill. New ires referencing the client ill that are added
18140 	 * to the ire table before the ILL_CHANGING flag is set, will be
18141 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18142 	 * the client ill while the ILL_CHANGING flag is set will be failed
18143 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18144 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18145 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18146 	 * belong to the same usesrc group.
18147 	 */
18148 	mutex_enter(&usesrc_cli_ill->ill_lock);
18149 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18150 	mutex_exit(&usesrc_cli_ill->ill_lock);
18151 	ill_flag_changed = B_TRUE;
18152 
18153 	if (ipif->ipif_isv6)
18154 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18155 		    ALL_ZONES, ipst);
18156 	else
18157 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18158 		    ALL_ZONES, ipst);
18159 
18160 	/*
18161 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18162 	 * and the ill_usesrc_ifindex fields
18163 	 */
18164 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18165 
18166 	if (reset_flg) {
18167 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18168 		if (ret != 0) {
18169 			err = EINVAL;
18170 		}
18171 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18172 		goto done;
18173 	}
18174 
18175 	/*
18176 	 * Four possibilities to consider:
18177 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18178 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18179 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18180 	 * 4. Both are part of their respective usesrc groups
18181 	 */
18182 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18183 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18184 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18185 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18186 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18187 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18188 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18189 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18190 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18191 		/* Insert at head of list */
18192 		usesrc_cli_ill->ill_usesrc_grp_next =
18193 		    usesrc_ill->ill_usesrc_grp_next;
18194 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18195 	} else {
18196 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18197 		    ifindex);
18198 		if (ret != 0)
18199 			err = EINVAL;
18200 	}
18201 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18202 
18203 done:
18204 	if (ill_flag_changed) {
18205 		mutex_enter(&usesrc_cli_ill->ill_lock);
18206 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18207 		mutex_exit(&usesrc_cli_ill->ill_lock);
18208 	}
18209 	if (ipsq != NULL)
18210 		ipsq_exit(ipsq);
18211 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18212 	ill_refrele(usesrc_ill);
18213 	return (err);
18214 }
18215 
18216 /*
18217  * comparison function used by avl.
18218  */
18219 static int
18220 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18221 {
18222 
18223 	uint_t index;
18224 
18225 	ASSERT(phyip != NULL && index_ptr != NULL);
18226 
18227 	index = *((uint_t *)index_ptr);
18228 	/*
18229 	 * let the phyint with the lowest index be on top.
18230 	 */
18231 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18232 		return (1);
18233 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18234 		return (-1);
18235 	return (0);
18236 }
18237 
18238 /*
18239  * comparison function used by avl.
18240  */
18241 static int
18242 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18243 {
18244 	ill_t *ill;
18245 	int res = 0;
18246 
18247 	ASSERT(phyip != NULL && name_ptr != NULL);
18248 
18249 	if (((phyint_t *)phyip)->phyint_illv4)
18250 		ill = ((phyint_t *)phyip)->phyint_illv4;
18251 	else
18252 		ill = ((phyint_t *)phyip)->phyint_illv6;
18253 	ASSERT(ill != NULL);
18254 
18255 	res = strcmp(ill->ill_name, (char *)name_ptr);
18256 	if (res > 0)
18257 		return (1);
18258 	else if (res < 0)
18259 		return (-1);
18260 	return (0);
18261 }
18262 
18263 /*
18264  * This function is called on the unplumb path via ill_glist_delete() when
18265  * there are no ills left on the phyint and thus the phyint can be freed.
18266  */
18267 static void
18268 phyint_free(phyint_t *phyi)
18269 {
18270 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18271 
18272 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18273 
18274 	/*
18275 	 * If this phyint was an IPMP meta-interface, blow away the group.
18276 	 * This is safe to do because all of the illgrps have already been
18277 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18278 	 * If we're cleaning up as a result of failed initialization,
18279 	 * phyint_grp may be NULL.
18280 	 */
18281 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18282 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18283 		ipmp_grp_destroy(phyi->phyint_grp);
18284 		phyi->phyint_grp = NULL;
18285 		rw_exit(&ipst->ips_ipmp_lock);
18286 	}
18287 
18288 	/*
18289 	 * If this interface was under IPMP, take it out of the group.
18290 	 */
18291 	if (phyi->phyint_grp != NULL)
18292 		ipmp_phyint_leave_grp(phyi);
18293 
18294 	/*
18295 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18296 	 * will be freed in ipsq_exit().
18297 	 */
18298 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18299 	phyi->phyint_name[0] = '\0';
18300 
18301 	mi_free(phyi);
18302 }
18303 
18304 /*
18305  * Attach the ill to the phyint structure which can be shared by both
18306  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18307  * function is called from ipif_set_values and ill_lookup_on_name (for
18308  * loopback) where we know the name of the ill. We lookup the ill and if
18309  * there is one present already with the name use that phyint. Otherwise
18310  * reuse the one allocated by ill_init.
18311  */
18312 static void
18313 ill_phyint_reinit(ill_t *ill)
18314 {
18315 	boolean_t isv6 = ill->ill_isv6;
18316 	phyint_t *phyi_old;
18317 	phyint_t *phyi;
18318 	avl_index_t where = 0;
18319 	ill_t	*ill_other = NULL;
18320 	ip_stack_t	*ipst = ill->ill_ipst;
18321 
18322 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18323 
18324 	phyi_old = ill->ill_phyint;
18325 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18326 	    phyi_old->phyint_illv6 == NULL));
18327 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18328 	    phyi_old->phyint_illv4 == NULL));
18329 	ASSERT(phyi_old->phyint_ifindex == 0);
18330 
18331 	/*
18332 	 * Now that our ill has a name, set it in the phyint.
18333 	 */
18334 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18335 
18336 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18337 	    ill->ill_name, &where);
18338 
18339 	/*
18340 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18341 	 *    the global list of ills. So no other thread could have located
18342 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18343 	 * 2. Now locate the other protocol instance of this ill.
18344 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18345 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18346 	 *    of neither ill can change.
18347 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18348 	 *    other ill.
18349 	 * 5. Release all locks.
18350 	 */
18351 
18352 	/*
18353 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18354 	 * we are initializing IPv4.
18355 	 */
18356 	if (phyi != NULL) {
18357 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18358 		ASSERT(ill_other->ill_phyint != NULL);
18359 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18360 		    (!isv6 && ill_other->ill_isv6));
18361 		GRAB_ILL_LOCKS(ill, ill_other);
18362 		/*
18363 		 * We are potentially throwing away phyint_flags which
18364 		 * could be different from the one that we obtain from
18365 		 * ill_other->ill_phyint. But it is okay as we are assuming
18366 		 * that the state maintained within IP is correct.
18367 		 */
18368 		mutex_enter(&phyi->phyint_lock);
18369 		if (isv6) {
18370 			ASSERT(phyi->phyint_illv6 == NULL);
18371 			phyi->phyint_illv6 = ill;
18372 		} else {
18373 			ASSERT(phyi->phyint_illv4 == NULL);
18374 			phyi->phyint_illv4 = ill;
18375 		}
18376 
18377 		/*
18378 		 * Delete the old phyint and make its ipsq eligible
18379 		 * to be freed in ipsq_exit().
18380 		 */
18381 		phyi_old->phyint_illv4 = NULL;
18382 		phyi_old->phyint_illv6 = NULL;
18383 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18384 		phyi_old->phyint_name[0] = '\0';
18385 		mi_free(phyi_old);
18386 	} else {
18387 		mutex_enter(&ill->ill_lock);
18388 		/*
18389 		 * We don't need to acquire any lock, since
18390 		 * the ill is not yet visible globally  and we
18391 		 * have not yet released the ill_g_lock.
18392 		 */
18393 		phyi = phyi_old;
18394 		mutex_enter(&phyi->phyint_lock);
18395 		/* XXX We need a recovery strategy here. */
18396 		if (!phyint_assign_ifindex(phyi, ipst))
18397 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18398 
18399 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18400 		    (void *)phyi, where);
18401 
18402 		(void) avl_find(&ipst->ips_phyint_g_list->
18403 		    phyint_list_avl_by_index,
18404 		    &phyi->phyint_ifindex, &where);
18405 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18406 		    (void *)phyi, where);
18407 	}
18408 
18409 	/*
18410 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18411 	 * pending mp is not affected because that is per ill basis.
18412 	 */
18413 	ill->ill_phyint = phyi;
18414 
18415 	/*
18416 	 * Now that the phyint's ifindex has been assigned, complete the
18417 	 * remaining
18418 	 */
18419 
18420 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18421 	if (ill->ill_isv6) {
18422 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18423 		    ill->ill_phyint->phyint_ifindex;
18424 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18425 	} else {
18426 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18427 	}
18428 
18429 	/*
18430 	 * Generate an event within the hooks framework to indicate that
18431 	 * a new interface has just been added to IP.  For this event to
18432 	 * be generated, the network interface must, at least, have an
18433 	 * ifindex assigned to it.
18434 	 *
18435 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18436 	 * that the ordering of delivered events to listeners matches the
18437 	 * order of them in the kernel.
18438 	 *
18439 	 * This function could be called from ill_lookup_on_name. In that case
18440 	 * the interface is loopback "lo", which will not generate a NIC event.
18441 	 */
18442 	if (ill->ill_name_length <= 2 ||
18443 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
18444 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18445 		    ill->ill_name_length);
18446 	}
18447 	RELEASE_ILL_LOCKS(ill, ill_other);
18448 	mutex_exit(&phyi->phyint_lock);
18449 }
18450 
18451 /*
18452  * Notify any downstream modules of the name of this interface.
18453  * An M_IOCTL is used even though we don't expect a successful reply.
18454  * Any reply message from the driver (presumably an M_IOCNAK) will
18455  * eventually get discarded somewhere upstream.  The message format is
18456  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18457  * to IP.
18458  */
18459 static void
18460 ip_ifname_notify(ill_t *ill, queue_t *q)
18461 {
18462 	mblk_t *mp1, *mp2;
18463 	struct iocblk *iocp;
18464 	struct lifreq *lifr;
18465 
18466 	mp1 = mkiocb(SIOCSLIFNAME);
18467 	if (mp1 == NULL)
18468 		return;
18469 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18470 	if (mp2 == NULL) {
18471 		freeb(mp1);
18472 		return;
18473 	}
18474 
18475 	mp1->b_cont = mp2;
18476 	iocp = (struct iocblk *)mp1->b_rptr;
18477 	iocp->ioc_count = sizeof (struct lifreq);
18478 
18479 	lifr = (struct lifreq *)mp2->b_rptr;
18480 	mp2->b_wptr += sizeof (struct lifreq);
18481 	bzero(lifr, sizeof (struct lifreq));
18482 
18483 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18484 	lifr->lifr_ppa = ill->ill_ppa;
18485 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18486 
18487 	putnext(q, mp1);
18488 }
18489 
18490 static int
18491 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18492 {
18493 	int		err;
18494 	ip_stack_t	*ipst = ill->ill_ipst;
18495 	phyint_t	*phyi = ill->ill_phyint;
18496 
18497 	/* Set the obsolete NDD per-interface forwarding name. */
18498 	err = ill_set_ndd_name(ill);
18499 	if (err != 0) {
18500 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18501 		    err);
18502 	}
18503 
18504 	/*
18505 	 * Now that ill_name is set, the configuration for the IPMP
18506 	 * meta-interface can be performed.
18507 	 */
18508 	if (IS_IPMP(ill)) {
18509 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18510 		/*
18511 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18512 		 * meta-interface and we need to create the IPMP group.
18513 		 */
18514 		if (phyi->phyint_grp == NULL) {
18515 			/*
18516 			 * If someone has renamed another IPMP group to have
18517 			 * the same name as our interface, bail.
18518 			 */
18519 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18520 				rw_exit(&ipst->ips_ipmp_lock);
18521 				return (EEXIST);
18522 			}
18523 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18524 			if (phyi->phyint_grp == NULL) {
18525 				rw_exit(&ipst->ips_ipmp_lock);
18526 				return (ENOMEM);
18527 			}
18528 		}
18529 		rw_exit(&ipst->ips_ipmp_lock);
18530 	}
18531 
18532 	/* Tell downstream modules where they are. */
18533 	ip_ifname_notify(ill, q);
18534 
18535 	/*
18536 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18537 	 * Error cases are ENOMEM ...
18538 	 */
18539 	err = ill_dl_phys(ill, ipif, mp, q);
18540 
18541 	/*
18542 	 * If there is no IRE expiration timer running, get one started.
18543 	 * igmp and mld timers will be triggered by the first multicast
18544 	 */
18545 	if (ipst->ips_ip_ire_expire_id == 0) {
18546 		/*
18547 		 * acquire the lock and check again.
18548 		 */
18549 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18550 		if (ipst->ips_ip_ire_expire_id == 0) {
18551 			ipst->ips_ip_ire_expire_id = timeout(
18552 			    ip_trash_timer_expire, ipst,
18553 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18554 		}
18555 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18556 	}
18557 
18558 	if (ill->ill_isv6) {
18559 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18560 		if (ipst->ips_mld_slowtimeout_id == 0) {
18561 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18562 			    (void *)ipst,
18563 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18564 		}
18565 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18566 	} else {
18567 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18568 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18569 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18570 			    (void *)ipst,
18571 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18572 		}
18573 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18574 	}
18575 
18576 	return (err);
18577 }
18578 
18579 /*
18580  * Common routine for ppa and ifname setting. Should be called exclusive.
18581  *
18582  * Returns EINPROGRESS when mp has been consumed by queueing it on
18583  * ill_pending_mp and the ioctl will complete in ip_rput.
18584  *
18585  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18586  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18587  * For SLIFNAME, we pass these values back to the userland.
18588  */
18589 static int
18590 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18591 {
18592 	ill_t	*ill;
18593 	ipif_t	*ipif;
18594 	ipsq_t	*ipsq;
18595 	char	*ppa_ptr;
18596 	char	*old_ptr;
18597 	char	old_char;
18598 	int	error;
18599 	ip_stack_t	*ipst;
18600 
18601 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18602 	ASSERT(q->q_next != NULL);
18603 	ASSERT(interf_name != NULL);
18604 
18605 	ill = (ill_t *)q->q_ptr;
18606 	ipst = ill->ill_ipst;
18607 
18608 	ASSERT(ill->ill_ipst != NULL);
18609 	ASSERT(ill->ill_name[0] == '\0');
18610 	ASSERT(IAM_WRITER_ILL(ill));
18611 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18612 	ASSERT(ill->ill_ppa == UINT_MAX);
18613 
18614 	/* The ppa is sent down by ifconfig or is chosen */
18615 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18616 		return (EINVAL);
18617 	}
18618 
18619 	/*
18620 	 * make sure ppa passed in is same as ppa in the name.
18621 	 * This check is not made when ppa == UINT_MAX in that case ppa
18622 	 * in the name could be anything. System will choose a ppa and
18623 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18624 	 */
18625 	if (*new_ppa_ptr != UINT_MAX) {
18626 		/* stoi changes the pointer */
18627 		old_ptr = ppa_ptr;
18628 		/*
18629 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18630 		 * (they don't have an externally visible ppa).  We assign one
18631 		 * here so that we can manage the interface.  Note that in
18632 		 * the past this value was always 0 for DLPI 1 drivers.
18633 		 */
18634 		if (*new_ppa_ptr == 0)
18635 			*new_ppa_ptr = stoi(&old_ptr);
18636 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18637 			return (EINVAL);
18638 	}
18639 	/*
18640 	 * terminate string before ppa
18641 	 * save char at that location.
18642 	 */
18643 	old_char = ppa_ptr[0];
18644 	ppa_ptr[0] = '\0';
18645 
18646 	ill->ill_ppa = *new_ppa_ptr;
18647 	/*
18648 	 * Finish as much work now as possible before calling ill_glist_insert
18649 	 * which makes the ill globally visible and also merges it with the
18650 	 * other protocol instance of this phyint. The remaining work is
18651 	 * done after entering the ipsq which may happen sometime later.
18652 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18653 	 */
18654 	ipif = ill->ill_ipif;
18655 
18656 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18657 	ipif_assign_seqid(ipif);
18658 
18659 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18660 		ill->ill_flags |= ILLF_IPV4;
18661 
18662 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18663 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18664 
18665 	if (ill->ill_flags & ILLF_IPV6) {
18666 
18667 		ill->ill_isv6 = B_TRUE;
18668 		if (ill->ill_rq != NULL) {
18669 			ill->ill_rq->q_qinfo = &iprinitv6;
18670 			ill->ill_wq->q_qinfo = &ipwinitv6;
18671 		}
18672 
18673 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18674 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18675 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18676 		ipif->ipif_v6subnet = ipv6_all_zeros;
18677 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18678 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18679 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18680 		/*
18681 		 * point-to-point or Non-mulicast capable
18682 		 * interfaces won't do NUD unless explicitly
18683 		 * configured to do so.
18684 		 */
18685 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18686 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18687 			ill->ill_flags |= ILLF_NONUD;
18688 		}
18689 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18690 		if (ill->ill_flags & ILLF_NOARP) {
18691 			/*
18692 			 * Note: xresolv interfaces will eventually need
18693 			 * NOARP set here as well, but that will require
18694 			 * those external resolvers to have some
18695 			 * knowledge of that flag and act appropriately.
18696 			 * Not to be changed at present.
18697 			 */
18698 			ill->ill_flags &= ~ILLF_NOARP;
18699 		}
18700 		/*
18701 		 * Set the ILLF_ROUTER flag according to the global
18702 		 * IPv6 forwarding policy.
18703 		 */
18704 		if (ipst->ips_ipv6_forward != 0)
18705 			ill->ill_flags |= ILLF_ROUTER;
18706 	} else if (ill->ill_flags & ILLF_IPV4) {
18707 		ill->ill_isv6 = B_FALSE;
18708 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18709 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18710 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18711 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18712 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18713 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18714 		/*
18715 		 * Set the ILLF_ROUTER flag according to the global
18716 		 * IPv4 forwarding policy.
18717 		 */
18718 		if (ipst->ips_ip_g_forward != 0)
18719 			ill->ill_flags |= ILLF_ROUTER;
18720 	}
18721 
18722 	ASSERT(ill->ill_phyint != NULL);
18723 
18724 	/*
18725 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18726 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18727 	 */
18728 	if (!ill_allocate_mibs(ill))
18729 		return (ENOMEM);
18730 
18731 	/*
18732 	 * Pick a default sap until we get the DL_INFO_ACK back from
18733 	 * the driver.
18734 	 */
18735 	if (ill->ill_sap == 0) {
18736 		if (ill->ill_isv6)
18737 			ill->ill_sap = IP6_DL_SAP;
18738 		else
18739 			ill->ill_sap = IP_DL_SAP;
18740 	}
18741 
18742 	ill->ill_ifname_pending = 1;
18743 	ill->ill_ifname_pending_err = 0;
18744 
18745 	/*
18746 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18747 	 * that were joined while this ill was not bound to the DLPI link need
18748 	 * to be recovered by ill_recover_multicast().
18749 	 */
18750 	ill->ill_need_recover_multicast = 1;
18751 
18752 	ill_refhold(ill);
18753 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18754 	if ((error = ill_glist_insert(ill, interf_name,
18755 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18756 		ill->ill_ppa = UINT_MAX;
18757 		ill->ill_name[0] = '\0';
18758 		/*
18759 		 * undo null termination done above.
18760 		 */
18761 		ppa_ptr[0] = old_char;
18762 		rw_exit(&ipst->ips_ill_g_lock);
18763 		ill_refrele(ill);
18764 		return (error);
18765 	}
18766 
18767 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18768 
18769 	/*
18770 	 * When we return the buffer pointed to by interf_name should contain
18771 	 * the same name as in ill_name.
18772 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18773 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18774 	 * so copy full name and update the ppa ptr.
18775 	 * When ppa passed in != UINT_MAX all values are correct just undo
18776 	 * null termination, this saves a bcopy.
18777 	 */
18778 	if (*new_ppa_ptr == UINT_MAX) {
18779 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18780 		*new_ppa_ptr = ill->ill_ppa;
18781 	} else {
18782 		/*
18783 		 * undo null termination done above.
18784 		 */
18785 		ppa_ptr[0] = old_char;
18786 	}
18787 
18788 	/* Let SCTP know about this ILL */
18789 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18790 
18791 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18792 	    B_TRUE);
18793 
18794 	rw_exit(&ipst->ips_ill_g_lock);
18795 	ill_refrele(ill);
18796 	if (ipsq == NULL)
18797 		return (EINPROGRESS);
18798 
18799 	/*
18800 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18801 	 */
18802 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18803 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18804 	else
18805 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18806 
18807 	error = ipif_set_values_tail(ill, ipif, mp, q);
18808 	ipsq_exit(ipsq);
18809 	if (error != 0 && error != EINPROGRESS) {
18810 		/*
18811 		 * restore previous values
18812 		 */
18813 		ill->ill_isv6 = B_FALSE;
18814 	}
18815 	return (error);
18816 }
18817 
18818 void
18819 ipif_init(ip_stack_t *ipst)
18820 {
18821 	int i;
18822 
18823 	for (i = 0; i < MAX_G_HEADS; i++) {
18824 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18825 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18826 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
18827 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18828 	}
18829 
18830 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18831 	    ill_phyint_compare_index,
18832 	    sizeof (phyint_t),
18833 	    offsetof(struct phyint, phyint_avl_by_index));
18834 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18835 	    ill_phyint_compare_name,
18836 	    sizeof (phyint_t),
18837 	    offsetof(struct phyint, phyint_avl_by_name));
18838 }
18839 
18840 /*
18841  * Lookup the ipif corresponding to the onlink destination address. For
18842  * point-to-point interfaces, it matches with remote endpoint destination
18843  * address. For point-to-multipoint interfaces it only tries to match the
18844  * destination with the interface's subnet address. The longest, most specific
18845  * match is found to take care of such rare network configurations like -
18846  * le0: 129.146.1.1/16
18847  * le1: 129.146.2.2/24
18848  *
18849  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
18850  * supported on underlying interfaces in an IPMP group, underlying interfaces
18851  * are ignored when looking up a match.  (If we didn't ignore them, we'd
18852  * risk using a test address as a source for outgoing traffic.)
18853  */
18854 ipif_t *
18855 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
18856 {
18857 	ipif_t	*ipif, *best_ipif;
18858 	ill_t	*ill;
18859 	ill_walk_context_t ctx;
18860 
18861 	ASSERT(zoneid != ALL_ZONES);
18862 	best_ipif = NULL;
18863 
18864 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18865 	ill = ILL_START_WALK_V4(&ctx, ipst);
18866 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18867 		if (IS_UNDER_IPMP(ill))
18868 			continue;
18869 		mutex_enter(&ill->ill_lock);
18870 		for (ipif = ill->ill_ipif; ipif != NULL;
18871 		    ipif = ipif->ipif_next) {
18872 			if (!IPIF_CAN_LOOKUP(ipif))
18873 				continue;
18874 			if (ipif->ipif_zoneid != zoneid &&
18875 			    ipif->ipif_zoneid != ALL_ZONES)
18876 				continue;
18877 			/*
18878 			 * Point-to-point case. Look for exact match with
18879 			 * destination address.
18880 			 */
18881 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18882 				if (ipif->ipif_pp_dst_addr == addr) {
18883 					ipif_refhold_locked(ipif);
18884 					mutex_exit(&ill->ill_lock);
18885 					rw_exit(&ipst->ips_ill_g_lock);
18886 					if (best_ipif != NULL)
18887 						ipif_refrele(best_ipif);
18888 					return (ipif);
18889 				}
18890 			} else if (ipif->ipif_subnet == (addr &
18891 			    ipif->ipif_net_mask)) {
18892 				/*
18893 				 * Point-to-multipoint case. Looping through to
18894 				 * find the most specific match. If there are
18895 				 * multiple best match ipif's then prefer ipif's
18896 				 * that are UP. If there is only one best match
18897 				 * ipif and it is DOWN we must still return it.
18898 				 */
18899 				if ((best_ipif == NULL) ||
18900 				    (ipif->ipif_net_mask >
18901 				    best_ipif->ipif_net_mask) ||
18902 				    ((ipif->ipif_net_mask ==
18903 				    best_ipif->ipif_net_mask) &&
18904 				    ((ipif->ipif_flags & IPIF_UP) &&
18905 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
18906 					ipif_refhold_locked(ipif);
18907 					mutex_exit(&ill->ill_lock);
18908 					rw_exit(&ipst->ips_ill_g_lock);
18909 					if (best_ipif != NULL)
18910 						ipif_refrele(best_ipif);
18911 					best_ipif = ipif;
18912 					rw_enter(&ipst->ips_ill_g_lock,
18913 					    RW_READER);
18914 					mutex_enter(&ill->ill_lock);
18915 				}
18916 			}
18917 		}
18918 		mutex_exit(&ill->ill_lock);
18919 	}
18920 	rw_exit(&ipst->ips_ill_g_lock);
18921 	return (best_ipif);
18922 }
18923 
18924 /*
18925  * Save enough information so that we can recreate the IRE if
18926  * the interface goes down and then up.
18927  */
18928 static void
18929 ipif_save_ire(ipif_t *ipif, ire_t *ire)
18930 {
18931 	mblk_t	*save_mp;
18932 
18933 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
18934 	if (save_mp != NULL) {
18935 		ifrt_t	*ifrt;
18936 
18937 		save_mp->b_wptr += sizeof (ifrt_t);
18938 		ifrt = (ifrt_t *)save_mp->b_rptr;
18939 		bzero(ifrt, sizeof (ifrt_t));
18940 		ifrt->ifrt_type = ire->ire_type;
18941 		ifrt->ifrt_addr = ire->ire_addr;
18942 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
18943 		ifrt->ifrt_src_addr = ire->ire_src_addr;
18944 		ifrt->ifrt_mask = ire->ire_mask;
18945 		ifrt->ifrt_flags = ire->ire_flags;
18946 		ifrt->ifrt_max_frag = ire->ire_max_frag;
18947 		mutex_enter(&ipif->ipif_saved_ire_lock);
18948 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
18949 		ipif->ipif_saved_ire_mp = save_mp;
18950 		ipif->ipif_saved_ire_cnt++;
18951 		mutex_exit(&ipif->ipif_saved_ire_lock);
18952 	}
18953 }
18954 
18955 static void
18956 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
18957 {
18958 	mblk_t	**mpp;
18959 	mblk_t	*mp;
18960 	ifrt_t	*ifrt;
18961 
18962 	/* Remove from ipif_saved_ire_mp list if it is there */
18963 	mutex_enter(&ipif->ipif_saved_ire_lock);
18964 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
18965 	    mpp = &(*mpp)->b_cont) {
18966 		/*
18967 		 * On a given ipif, the triple of address, gateway and
18968 		 * mask is unique for each saved IRE (in the case of
18969 		 * ordinary interface routes, the gateway address is
18970 		 * all-zeroes).
18971 		 */
18972 		mp = *mpp;
18973 		ifrt = (ifrt_t *)mp->b_rptr;
18974 		if (ifrt->ifrt_addr == ire->ire_addr &&
18975 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
18976 		    ifrt->ifrt_mask == ire->ire_mask) {
18977 			*mpp = mp->b_cont;
18978 			ipif->ipif_saved_ire_cnt--;
18979 			freeb(mp);
18980 			break;
18981 		}
18982 	}
18983 	mutex_exit(&ipif->ipif_saved_ire_lock);
18984 }
18985 
18986 /*
18987  * IP multirouting broadcast routes handling
18988  * Append CGTP broadcast IREs to regular ones created
18989  * at ifconfig time.
18990  */
18991 static void
18992 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
18993 {
18994 	ire_t *ire_prim;
18995 
18996 	ASSERT(ire != NULL);
18997 	ASSERT(ire_dst != NULL);
18998 
18999 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19000 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19001 	if (ire_prim != NULL) {
19002 		/*
19003 		 * We are in the special case of broadcasts for
19004 		 * CGTP. We add an IRE_BROADCAST that holds
19005 		 * the RTF_MULTIRT flag, the destination
19006 		 * address of ire_dst and the low level
19007 		 * info of ire_prim. In other words, CGTP
19008 		 * broadcast is added to the redundant ipif.
19009 		 */
19010 		ipif_t *ipif_prim;
19011 		ire_t  *bcast_ire;
19012 
19013 		ipif_prim = ire_prim->ire_ipif;
19014 
19015 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19016 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19017 		    (void *)ire_dst, (void *)ire_prim,
19018 		    (void *)ipif_prim));
19019 
19020 		bcast_ire = ire_create(
19021 		    (uchar_t *)&ire->ire_addr,
19022 		    (uchar_t *)&ip_g_all_ones,
19023 		    (uchar_t *)&ire_dst->ire_src_addr,
19024 		    (uchar_t *)&ire->ire_gateway_addr,
19025 		    &ipif_prim->ipif_mtu,
19026 		    NULL,
19027 		    ipif_prim->ipif_rq,
19028 		    ipif_prim->ipif_wq,
19029 		    IRE_BROADCAST,
19030 		    ipif_prim,
19031 		    0,
19032 		    0,
19033 		    0,
19034 		    ire->ire_flags,
19035 		    &ire_uinfo_null,
19036 		    NULL,
19037 		    NULL,
19038 		    ipst);
19039 
19040 		if (bcast_ire != NULL) {
19041 
19042 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19043 			    B_FALSE) == 0) {
19044 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19045 				    "added bcast_ire %p\n",
19046 				    (void *)bcast_ire));
19047 
19048 				ipif_save_ire(bcast_ire->ire_ipif,
19049 				    bcast_ire);
19050 				ire_refrele(bcast_ire);
19051 			}
19052 		}
19053 		ire_refrele(ire_prim);
19054 	}
19055 }
19056 
19057 /*
19058  * IP multirouting broadcast routes handling
19059  * Remove the broadcast ire
19060  */
19061 static void
19062 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19063 {
19064 	ire_t *ire_dst;
19065 
19066 	ASSERT(ire != NULL);
19067 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19068 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19069 	if (ire_dst != NULL) {
19070 		ire_t *ire_prim;
19071 
19072 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19073 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19074 		if (ire_prim != NULL) {
19075 			ipif_t *ipif_prim;
19076 			ire_t  *bcast_ire;
19077 
19078 			ipif_prim = ire_prim->ire_ipif;
19079 
19080 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19081 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19082 			    (void *)ire_dst, (void *)ire_prim,
19083 			    (void *)ipif_prim));
19084 
19085 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19086 			    ire->ire_gateway_addr,
19087 			    IRE_BROADCAST,
19088 			    ipif_prim, ALL_ZONES,
19089 			    NULL,
19090 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19091 			    MATCH_IRE_MASK, ipst);
19092 
19093 			if (bcast_ire != NULL) {
19094 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19095 				    "looked up bcast_ire %p\n",
19096 				    (void *)bcast_ire));
19097 				ipif_remove_ire(bcast_ire->ire_ipif,
19098 				    bcast_ire);
19099 				ire_delete(bcast_ire);
19100 				ire_refrele(bcast_ire);
19101 			}
19102 			ire_refrele(ire_prim);
19103 		}
19104 		ire_refrele(ire_dst);
19105 	}
19106 }
19107 
19108 /*
19109  * IPsec hardware acceleration capabilities related functions.
19110  */
19111 
19112 /*
19113  * Free a per-ill IPsec capabilities structure.
19114  */
19115 static void
19116 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19117 {
19118 	if (capab->auth_hw_algs != NULL)
19119 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19120 	if (capab->encr_hw_algs != NULL)
19121 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19122 	if (capab->encr_algparm != NULL)
19123 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19124 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19125 }
19126 
19127 /*
19128  * Allocate a new per-ill IPsec capabilities structure. This structure
19129  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19130  * an array which specifies, for each algorithm, whether this algorithm
19131  * is supported by the ill or not.
19132  */
19133 static ill_ipsec_capab_t *
19134 ill_ipsec_capab_alloc(void)
19135 {
19136 	ill_ipsec_capab_t *capab;
19137 	uint_t nelems;
19138 
19139 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19140 	if (capab == NULL)
19141 		return (NULL);
19142 
19143 	/* we need one bit per algorithm */
19144 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19145 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19146 
19147 	/* allocate memory to store algorithm flags */
19148 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19149 	if (capab->encr_hw_algs == NULL)
19150 		goto nomem;
19151 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19152 	if (capab->auth_hw_algs == NULL)
19153 		goto nomem;
19154 	/*
19155 	 * Leave encr_algparm NULL for now since we won't need it half
19156 	 * the time
19157 	 */
19158 	return (capab);
19159 
19160 nomem:
19161 	ill_ipsec_capab_free(capab);
19162 	return (NULL);
19163 }
19164 
19165 /*
19166  * Resize capability array.  Since we're exclusive, this is OK.
19167  */
19168 static boolean_t
19169 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19170 {
19171 	ipsec_capab_algparm_t *nalp, *oalp;
19172 	uint32_t olen, nlen;
19173 
19174 	oalp = capab->encr_algparm;
19175 	olen = capab->encr_algparm_size;
19176 
19177 	if (oalp != NULL) {
19178 		if (algid < capab->encr_algparm_end)
19179 			return (B_TRUE);
19180 	}
19181 
19182 	nlen = (algid + 1) * sizeof (*nalp);
19183 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19184 	if (nalp == NULL)
19185 		return (B_FALSE);
19186 
19187 	if (oalp != NULL) {
19188 		bcopy(oalp, nalp, olen);
19189 		kmem_free(oalp, olen);
19190 	}
19191 	capab->encr_algparm = nalp;
19192 	capab->encr_algparm_size = nlen;
19193 	capab->encr_algparm_end = algid + 1;
19194 
19195 	return (B_TRUE);
19196 }
19197 
19198 /*
19199  * Compare the capabilities of the specified ill with the protocol
19200  * and algorithms specified by the SA passed as argument.
19201  * If they match, returns B_TRUE, B_FALSE if they do not match.
19202  *
19203  * The ill can be passed as a pointer to it, or by specifying its index
19204  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19205  *
19206  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19207  * packet is eligible for hardware acceleration, and by
19208  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19209  * to a particular ill.
19210  */
19211 boolean_t
19212 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19213     ipsa_t *sa, netstack_t *ns)
19214 {
19215 	boolean_t sa_isv6;
19216 	uint_t algid;
19217 	struct ill_ipsec_capab_s *cpp;
19218 	boolean_t need_refrele = B_FALSE;
19219 	ip_stack_t	*ipst = ns->netstack_ip;
19220 
19221 	if (ill == NULL) {
19222 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19223 		    NULL, NULL, NULL, ipst);
19224 		if (ill == NULL) {
19225 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19226 			return (B_FALSE);
19227 		}
19228 		need_refrele = B_TRUE;
19229 	}
19230 
19231 	/*
19232 	 * Use the address length specified by the SA to determine
19233 	 * if it corresponds to a IPv6 address, and fail the matching
19234 	 * if the isv6 flag passed as argument does not match.
19235 	 * Note: this check is used for SADB capability checking before
19236 	 * sending SA information to an ill.
19237 	 */
19238 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19239 	if (sa_isv6 != ill_isv6)
19240 		/* protocol mismatch */
19241 		goto done;
19242 
19243 	/*
19244 	 * Check if the ill supports the protocol, algorithm(s) and
19245 	 * key size(s) specified by the SA, and get the pointers to
19246 	 * the algorithms supported by the ill.
19247 	 */
19248 	switch (sa->ipsa_type) {
19249 
19250 	case SADB_SATYPE_ESP:
19251 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19252 			/* ill does not support ESP acceleration */
19253 			goto done;
19254 		cpp = ill->ill_ipsec_capab_esp;
19255 		algid = sa->ipsa_auth_alg;
19256 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19257 			goto done;
19258 		algid = sa->ipsa_encr_alg;
19259 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19260 			goto done;
19261 		if (algid < cpp->encr_algparm_end) {
19262 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19263 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19264 				goto done;
19265 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19266 				goto done;
19267 		}
19268 		break;
19269 
19270 	case SADB_SATYPE_AH:
19271 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19272 			/* ill does not support AH acceleration */
19273 			goto done;
19274 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19275 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19276 			goto done;
19277 		break;
19278 	}
19279 
19280 	if (need_refrele)
19281 		ill_refrele(ill);
19282 	return (B_TRUE);
19283 done:
19284 	if (need_refrele)
19285 		ill_refrele(ill);
19286 	return (B_FALSE);
19287 }
19288 
19289 /*
19290  * Add a new ill to the list of IPsec capable ills.
19291  * Called from ill_capability_ipsec_ack() when an ACK was received
19292  * indicating that IPsec hardware processing was enabled for an ill.
19293  *
19294  * ill must point to the ill for which acceleration was enabled.
19295  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19296  */
19297 static void
19298 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19299 {
19300 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19301 	uint_t sa_type;
19302 	uint_t ipproto;
19303 	ip_stack_t	*ipst = ill->ill_ipst;
19304 
19305 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19306 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19307 
19308 	switch (dl_cap) {
19309 	case DL_CAPAB_IPSEC_AH:
19310 		sa_type = SADB_SATYPE_AH;
19311 		ills = &ipst->ips_ipsec_capab_ills_ah;
19312 		ipproto = IPPROTO_AH;
19313 		break;
19314 	case DL_CAPAB_IPSEC_ESP:
19315 		sa_type = SADB_SATYPE_ESP;
19316 		ills = &ipst->ips_ipsec_capab_ills_esp;
19317 		ipproto = IPPROTO_ESP;
19318 		break;
19319 	}
19320 
19321 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19322 
19323 	/*
19324 	 * Add ill index to list of hardware accelerators. If
19325 	 * already in list, do nothing.
19326 	 */
19327 	for (cur_ill = *ills; cur_ill != NULL &&
19328 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19329 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19330 		;
19331 
19332 	if (cur_ill == NULL) {
19333 		/* if this is a new entry for this ill */
19334 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19335 		if (new_ill == NULL) {
19336 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19337 			return;
19338 		}
19339 
19340 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19341 		new_ill->ill_isv6 = ill->ill_isv6;
19342 		new_ill->next = *ills;
19343 		*ills = new_ill;
19344 	} else if (!sadb_resync) {
19345 		/* not resync'ing SADB and an entry exists for this ill */
19346 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19347 		return;
19348 	}
19349 
19350 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19351 
19352 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19353 		/*
19354 		 * IPsec module for protocol loaded, initiate dump
19355 		 * of the SADB to this ill.
19356 		 */
19357 		sadb_ill_download(ill, sa_type);
19358 }
19359 
19360 /*
19361  * Remove an ill from the list of IPsec capable ills.
19362  */
19363 static void
19364 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19365 {
19366 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19367 	ip_stack_t	*ipst = ill->ill_ipst;
19368 
19369 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19370 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19371 
19372 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19373 	    &ipst->ips_ipsec_capab_ills_esp;
19374 
19375 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19376 
19377 	prev_ill = NULL;
19378 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19379 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19380 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19381 		;
19382 	if (cur_ill == NULL) {
19383 		/* entry not found */
19384 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19385 		return;
19386 	}
19387 	if (prev_ill == NULL) {
19388 		/* entry at front of list */
19389 		*ills = NULL;
19390 	} else {
19391 		prev_ill->next = cur_ill->next;
19392 	}
19393 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19394 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19395 }
19396 
19397 /*
19398  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19399  * supporting the specified IPsec protocol acceleration.
19400  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19401  * We free the mblk and, if sa is non-null, release the held referece.
19402  */
19403 void
19404 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19405     netstack_t *ns)
19406 {
19407 	ipsec_capab_ill_t *ici, *cur_ici;
19408 	ill_t *ill;
19409 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19410 	ip_stack_t	*ipst = ns->netstack_ip;
19411 
19412 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19413 	    ipst->ips_ipsec_capab_ills_esp;
19414 
19415 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19416 
19417 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19418 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19419 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19420 
19421 		/*
19422 		 * Handle the case where the ill goes away while the SADB is
19423 		 * attempting to send messages.  If it's going away, it's
19424 		 * nuking its shadow SADB, so we don't care..
19425 		 */
19426 
19427 		if (ill == NULL)
19428 			continue;
19429 
19430 		if (sa != NULL) {
19431 			/*
19432 			 * Make sure capabilities match before
19433 			 * sending SA to ill.
19434 			 */
19435 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19436 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19437 				ill_refrele(ill);
19438 				continue;
19439 			}
19440 
19441 			mutex_enter(&sa->ipsa_lock);
19442 			sa->ipsa_flags |= IPSA_F_HW;
19443 			mutex_exit(&sa->ipsa_lock);
19444 		}
19445 
19446 		/*
19447 		 * Copy template message, and add it to the front
19448 		 * of the mblk ship list. We want to avoid holding
19449 		 * the ipsec_capab_ills_lock while sending the
19450 		 * message to the ills.
19451 		 *
19452 		 * The b_next and b_prev are temporarily used
19453 		 * to build a list of mblks to be sent down, and to
19454 		 * save the ill to which they must be sent.
19455 		 */
19456 		nmp = copymsg(mp);
19457 		if (nmp == NULL) {
19458 			ill_refrele(ill);
19459 			continue;
19460 		}
19461 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19462 		nmp->b_next = mp_ship_list;
19463 		mp_ship_list = nmp;
19464 		nmp->b_prev = (mblk_t *)ill;
19465 	}
19466 
19467 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19468 
19469 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19470 		/* restore the mblk to a sane state */
19471 		next_mp = nmp->b_next;
19472 		nmp->b_next = NULL;
19473 		ill = (ill_t *)nmp->b_prev;
19474 		nmp->b_prev = NULL;
19475 
19476 		ill_dlpi_send(ill, nmp);
19477 		ill_refrele(ill);
19478 	}
19479 
19480 	if (sa != NULL)
19481 		IPSA_REFRELE(sa);
19482 	freemsg(mp);
19483 }
19484 
19485 /*
19486  * Derive an interface id from the link layer address.
19487  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19488  */
19489 static boolean_t
19490 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19491 {
19492 	char		*addr;
19493 
19494 	if (ill->ill_phys_addr_length != ETHERADDRL)
19495 		return (B_FALSE);
19496 
19497 	/* Form EUI-64 like address */
19498 	addr = (char *)&v6addr->s6_addr32[2];
19499 	bcopy(ill->ill_phys_addr, addr, 3);
19500 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19501 	addr[3] = (char)0xff;
19502 	addr[4] = (char)0xfe;
19503 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19504 	return (B_TRUE);
19505 }
19506 
19507 /* ARGSUSED */
19508 static boolean_t
19509 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19510 {
19511 	return (B_FALSE);
19512 }
19513 
19514 typedef struct ipmp_ifcookie {
19515 	uint32_t	ic_hostid;
19516 	char		ic_ifname[LIFNAMSIZ];
19517 	char		ic_zonename[ZONENAME_MAX];
19518 } ipmp_ifcookie_t;
19519 
19520 /*
19521  * Construct a pseudo-random interface ID for the IPMP interface that's both
19522  * predictable and (almost) guaranteed to be unique.
19523  */
19524 static boolean_t
19525 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19526 {
19527 	zone_t		*zp;
19528 	uint8_t		*addr;
19529 	uchar_t		hash[16];
19530 	ulong_t 	hostid;
19531 	MD5_CTX		ctx;
19532 	ipmp_ifcookie_t	ic = { 0 };
19533 
19534 	ASSERT(IS_IPMP(ill));
19535 
19536 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19537 	ic.ic_hostid = htonl((uint32_t)hostid);
19538 
19539 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19540 
19541 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19542 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19543 		zone_rele(zp);
19544 	}
19545 
19546 	MD5Init(&ctx);
19547 	MD5Update(&ctx, &ic, sizeof (ic));
19548 	MD5Final(hash, &ctx);
19549 
19550 	/*
19551 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19552 	 */
19553 	addr = &v6addr->s6_addr8[8];
19554 	bcopy(hash + 8, addr, sizeof (uint64_t));
19555 	addr[0] &= ~0x2;				/* set local bit */
19556 
19557 	return (B_TRUE);
19558 }
19559 
19560 /* ARGSUSED */
19561 static boolean_t
19562 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19563     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19564 {
19565 	/*
19566 	 * Multicast address mappings used over Ethernet/802.X.
19567 	 * This address is used as a base for mappings.
19568 	 */
19569 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19570 	    0x00, 0x00, 0x00};
19571 
19572 	/*
19573 	 * Extract low order 32 bits from IPv6 multicast address.
19574 	 * Or that into the link layer address, starting from the
19575 	 * second byte.
19576 	 */
19577 	*hw_start = 2;
19578 	v6_extract_mask->s6_addr32[0] = 0;
19579 	v6_extract_mask->s6_addr32[1] = 0;
19580 	v6_extract_mask->s6_addr32[2] = 0;
19581 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19582 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19583 	return (B_TRUE);
19584 }
19585 
19586 /*
19587  * Indicate by return value whether multicast is supported. If not,
19588  * this code should not touch/change any parameters.
19589  */
19590 /* ARGSUSED */
19591 static boolean_t
19592 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19593     uint32_t *hw_start, ipaddr_t *extract_mask)
19594 {
19595 	/*
19596 	 * Multicast address mappings used over Ethernet/802.X.
19597 	 * This address is used as a base for mappings.
19598 	 */
19599 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19600 	    0x00, 0x00, 0x00 };
19601 
19602 	if (phys_length != ETHERADDRL)
19603 		return (B_FALSE);
19604 
19605 	*extract_mask = htonl(0x007fffff);
19606 	*hw_start = 2;
19607 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19608 	return (B_TRUE);
19609 }
19610 
19611 /*
19612  * Derive IPoIB interface id from the link layer address.
19613  */
19614 static boolean_t
19615 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19616 {
19617 	char		*addr;
19618 
19619 	if (ill->ill_phys_addr_length != 20)
19620 		return (B_FALSE);
19621 	addr = (char *)&v6addr->s6_addr32[2];
19622 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19623 	/*
19624 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19625 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19626 	 * rules. In these cases, the IBA considers these GUIDs to be in
19627 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19628 	 * required; vendors are required not to assign global EUI-64's
19629 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19630 	 * of the interface identifier. Whether the GUID is in modified
19631 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19632 	 * bit set to 1.
19633 	 */
19634 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19635 	return (B_TRUE);
19636 }
19637 
19638 /*
19639  * Note on mapping from multicast IP addresses to IPoIB multicast link
19640  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19641  * The format of an IPoIB multicast address is:
19642  *
19643  *  4 byte QPN      Scope Sign.  Pkey
19644  * +--------------------------------------------+
19645  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19646  * +--------------------------------------------+
19647  *
19648  * The Scope and Pkey components are properties of the IBA port and
19649  * network interface. They can be ascertained from the broadcast address.
19650  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19651  */
19652 
19653 static boolean_t
19654 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19655     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19656 {
19657 	/*
19658 	 * Base IPoIB IPv6 multicast address used for mappings.
19659 	 * Does not contain the IBA scope/Pkey values.
19660 	 */
19661 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19662 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19663 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19664 
19665 	/*
19666 	 * Extract low order 80 bits from IPv6 multicast address.
19667 	 * Or that into the link layer address, starting from the
19668 	 * sixth byte.
19669 	 */
19670 	*hw_start = 6;
19671 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19672 
19673 	/*
19674 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19675 	 */
19676 	*(maddr + 5) = *(bphys_addr + 5);
19677 	*(maddr + 8) = *(bphys_addr + 8);
19678 	*(maddr + 9) = *(bphys_addr + 9);
19679 
19680 	v6_extract_mask->s6_addr32[0] = 0;
19681 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19682 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19683 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19684 	return (B_TRUE);
19685 }
19686 
19687 static boolean_t
19688 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19689     uint32_t *hw_start, ipaddr_t *extract_mask)
19690 {
19691 	/*
19692 	 * Base IPoIB IPv4 multicast address used for mappings.
19693 	 * Does not contain the IBA scope/Pkey values.
19694 	 */
19695 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19696 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19697 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19698 
19699 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19700 		return (B_FALSE);
19701 
19702 	/*
19703 	 * Extract low order 28 bits from IPv4 multicast address.
19704 	 * Or that into the link layer address, starting from the
19705 	 * sixteenth byte.
19706 	 */
19707 	*extract_mask = htonl(0x0fffffff);
19708 	*hw_start = 16;
19709 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19710 
19711 	/*
19712 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19713 	 */
19714 	*(maddr + 5) = *(bphys_addr + 5);
19715 	*(maddr + 8) = *(bphys_addr + 8);
19716 	*(maddr + 9) = *(bphys_addr + 9);
19717 	return (B_TRUE);
19718 }
19719 
19720 /*
19721  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19722  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19723  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19724  * the link-local address is preferred.
19725  */
19726 boolean_t
19727 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19728 {
19729 	ipif_t	*ipif;
19730 	ipif_t	*maybe_ipif = NULL;
19731 
19732 	mutex_enter(&ill->ill_lock);
19733 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19734 		mutex_exit(&ill->ill_lock);
19735 		if (ipifp != NULL)
19736 			*ipifp = NULL;
19737 		return (B_FALSE);
19738 	}
19739 
19740 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19741 		if (!IPIF_CAN_LOOKUP(ipif))
19742 			continue;
19743 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19744 		    ipif->ipif_zoneid != ALL_ZONES)
19745 			continue;
19746 		if ((ipif->ipif_flags & flags) != flags)
19747 			continue;
19748 
19749 		if (ipifp == NULL) {
19750 			mutex_exit(&ill->ill_lock);
19751 			ASSERT(maybe_ipif == NULL);
19752 			return (B_TRUE);
19753 		}
19754 		if (!ill->ill_isv6 ||
19755 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19756 			ipif_refhold_locked(ipif);
19757 			mutex_exit(&ill->ill_lock);
19758 			*ipifp = ipif;
19759 			return (B_TRUE);
19760 		}
19761 		if (maybe_ipif == NULL)
19762 			maybe_ipif = ipif;
19763 	}
19764 	if (ipifp != NULL) {
19765 		if (maybe_ipif != NULL)
19766 			ipif_refhold_locked(maybe_ipif);
19767 		*ipifp = maybe_ipif;
19768 	}
19769 	mutex_exit(&ill->ill_lock);
19770 	return (maybe_ipif != NULL);
19771 }
19772 
19773 /*
19774  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19775  * If a pointer to an ipif_t is returned then the caller will need to do
19776  * an ill_refrele().
19777  */
19778 ipif_t *
19779 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19780     ip_stack_t *ipst)
19781 {
19782 	ipif_t *ipif;
19783 	ill_t *ill;
19784 
19785 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19786 	    ipst);
19787 	if (ill == NULL)
19788 		return (NULL);
19789 
19790 	mutex_enter(&ill->ill_lock);
19791 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19792 		mutex_exit(&ill->ill_lock);
19793 		ill_refrele(ill);
19794 		return (NULL);
19795 	}
19796 
19797 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19798 		if (!IPIF_CAN_LOOKUP(ipif))
19799 			continue;
19800 		if (lifidx == ipif->ipif_id) {
19801 			ipif_refhold_locked(ipif);
19802 			break;
19803 		}
19804 	}
19805 
19806 	mutex_exit(&ill->ill_lock);
19807 	ill_refrele(ill);
19808 	return (ipif);
19809 }
19810 
19811 /*
19812  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19813  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19814  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19815  * for details.
19816  */
19817 void
19818 ill_fastpath_flush(ill_t *ill)
19819 {
19820 	ip_stack_t *ipst = ill->ill_ipst;
19821 
19822 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19823 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19824 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19825 }
19826 
19827 /*
19828  * Set the physical address information for `ill' to the contents of the
19829  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
19830  * asynchronous if `ill' cannot immediately be quiesced -- in which case
19831  * EINPROGRESS will be returned.
19832  */
19833 int
19834 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
19835 {
19836 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19837 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
19838 
19839 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19840 
19841 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
19842 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
19843 		/* Changing DL_IPV6_TOKEN is not yet supported */
19844 		return (0);
19845 	}
19846 
19847 	/*
19848 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
19849 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
19850 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
19851 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
19852 	 */
19853 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
19854 		freemsg(mp);
19855 		return (ENOMEM);
19856 	}
19857 
19858 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19859 
19860 	/*
19861 	 * If we can quiesce the ill, then set the address.  If not, then
19862 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
19863 	 */
19864 	ill_down_ipifs(ill, B_TRUE);
19865 	mutex_enter(&ill->ill_lock);
19866 	if (!ill_is_quiescent(ill)) {
19867 		/* call cannot fail since `conn_t *' argument is NULL */
19868 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19869 		    mp, ILL_DOWN);
19870 		mutex_exit(&ill->ill_lock);
19871 		return (EINPROGRESS);
19872 	}
19873 	mutex_exit(&ill->ill_lock);
19874 
19875 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
19876 	return (0);
19877 }
19878 
19879 /*
19880  * Once the ill associated with `q' has quiesced, set its physical address
19881  * information to the values in `addrmp'.  Note that two copies of `addrmp'
19882  * are passed (linked by b_cont), since we sometimes need to save two distinct
19883  * copies in the ill_t, and our context doesn't permit sleeping or allocation
19884  * failure (we'll free the other copy if it's not needed).  Since the ill_t
19885  * is quiesced, we know any stale IREs with the old address information have
19886  * already been removed, so we don't need to call ill_fastpath_flush().
19887  */
19888 /* ARGSUSED */
19889 static void
19890 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
19891 {
19892 	ill_t		*ill = q->q_ptr;
19893 	mblk_t		*addrmp2 = unlinkb(addrmp);
19894 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
19895 	uint_t		addrlen, addroff;
19896 
19897 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19898 
19899 	addroff	= dlindp->dl_addr_offset;
19900 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
19901 
19902 	switch (dlindp->dl_data) {
19903 	case DL_IPV6_LINK_LAYER_ADDR:
19904 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
19905 		freemsg(addrmp2);
19906 		break;
19907 
19908 	case DL_CURR_PHYS_ADDR:
19909 		freemsg(ill->ill_phys_addr_mp);
19910 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
19911 		ill->ill_phys_addr_mp = addrmp;
19912 		ill->ill_phys_addr_length = addrlen;
19913 
19914 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
19915 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
19916 		else
19917 			freemsg(addrmp2);
19918 		break;
19919 	default:
19920 		ASSERT(0);
19921 	}
19922 
19923 	/*
19924 	 * If there are ipifs to bring up, ill_up_ipifs() will return
19925 	 * EINPROGRESS, and ipsq_current_finish() will be called by
19926 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
19927 	 * brought up.
19928 	 */
19929 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
19930 		ipsq_current_finish(ipsq);
19931 }
19932 
19933 /*
19934  * Helper routine for setting the ill_nd_lla fields.
19935  */
19936 void
19937 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
19938 {
19939 	freemsg(ill->ill_nd_lla_mp);
19940 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
19941 	ill->ill_nd_lla_mp = ndmp;
19942 	ill->ill_nd_lla_len = addrlen;
19943 }
19944 
19945 /*
19946  * Replumb the ill.
19947  */
19948 int
19949 ill_replumb(ill_t *ill, mblk_t *mp)
19950 {
19951 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19952 
19953 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19954 
19955 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19956 
19957 	/*
19958 	 * If we can quiesce the ill, then continue.  If not, then
19959 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
19960 	 */
19961 	ill_down_ipifs(ill, B_FALSE);
19962 
19963 	mutex_enter(&ill->ill_lock);
19964 	if (!ill_is_quiescent(ill)) {
19965 		/* call cannot fail since `conn_t *' argument is NULL */
19966 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19967 		    mp, ILL_DOWN);
19968 		mutex_exit(&ill->ill_lock);
19969 		return (EINPROGRESS);
19970 	}
19971 	mutex_exit(&ill->ill_lock);
19972 
19973 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
19974 	return (0);
19975 }
19976 
19977 /* ARGSUSED */
19978 static void
19979 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
19980 {
19981 	ill_t *ill = q->q_ptr;
19982 
19983 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19984 
19985 	ill_down_ipifs_tail(ill);
19986 
19987 	freemsg(ill->ill_replumb_mp);
19988 	ill->ill_replumb_mp = copyb(mp);
19989 
19990 	/*
19991 	 * Successfully quiesced and brought down the interface, now we send
19992 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
19993 	 * DL_NOTE_REPLUMB message.
19994 	 */
19995 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
19996 	    DL_NOTIFY_CONF);
19997 	ASSERT(mp != NULL);
19998 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
19999 	    DL_NOTE_REPLUMB_DONE;
20000 	ill_dlpi_send(ill, mp);
20001 
20002 	/*
20003 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20004 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20005 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20006 	 * brought up.
20007 	 */
20008 	if (ill->ill_replumb_mp == NULL ||
20009 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) != EINPROGRESS) {
20010 		ipsq_current_finish(ipsq);
20011 	}
20012 }
20013 
20014 major_t IP_MAJ;
20015 #define	IP	"ip"
20016 
20017 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20018 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20019 
20020 /*
20021  * Issue REMOVEIF ioctls to have the loopback interfaces
20022  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20023  * the former going away when the user-level processes in the zone
20024  * are killed  * and the latter are cleaned up by the stream head
20025  * str_stack_shutdown callback that undoes all I_PLINKs.
20026  */
20027 void
20028 ip_loopback_cleanup(ip_stack_t *ipst)
20029 {
20030 	int error;
20031 	ldi_handle_t	lh = NULL;
20032 	ldi_ident_t	li = NULL;
20033 	int		rval;
20034 	cred_t		*cr;
20035 	struct strioctl iocb;
20036 	struct lifreq	lifreq;
20037 
20038 	IP_MAJ = ddi_name_to_major(IP);
20039 
20040 #ifdef NS_DEBUG
20041 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20042 	    ipst->ips_netstack->netstack_stackid);
20043 #endif
20044 
20045 	bzero(&lifreq, sizeof (lifreq));
20046 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20047 
20048 	error = ldi_ident_from_major(IP_MAJ, &li);
20049 	if (error) {
20050 #ifdef DEBUG
20051 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20052 		    error);
20053 #endif
20054 		return;
20055 	}
20056 
20057 	cr = zone_get_kcred(netstackid_to_zoneid(
20058 	    ipst->ips_netstack->netstack_stackid));
20059 	ASSERT(cr != NULL);
20060 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20061 	if (error) {
20062 #ifdef DEBUG
20063 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20064 		    error);
20065 #endif
20066 		goto out;
20067 	}
20068 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20069 	iocb.ic_timout = 15;
20070 	iocb.ic_len = sizeof (lifreq);
20071 	iocb.ic_dp = (char *)&lifreq;
20072 
20073 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20074 	/* LINTED - statement has no consequent */
20075 	if (error) {
20076 #ifdef NS_DEBUG
20077 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20078 		    "UDP6 error %d\n", error);
20079 #endif
20080 	}
20081 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20082 	lh = NULL;
20083 
20084 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20085 	if (error) {
20086 #ifdef NS_DEBUG
20087 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20088 		    error);
20089 #endif
20090 		goto out;
20091 	}
20092 
20093 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20094 	iocb.ic_timout = 15;
20095 	iocb.ic_len = sizeof (lifreq);
20096 	iocb.ic_dp = (char *)&lifreq;
20097 
20098 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20099 	/* LINTED - statement has no consequent */
20100 	if (error) {
20101 #ifdef NS_DEBUG
20102 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20103 		    "UDP error %d\n", error);
20104 #endif
20105 	}
20106 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20107 	lh = NULL;
20108 
20109 out:
20110 	/* Close layered handles */
20111 	if (lh)
20112 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20113 	if (li)
20114 		ldi_ident_release(li);
20115 
20116 	crfree(cr);
20117 }
20118 
20119 /*
20120  * This needs to be in-sync with nic_event_t definition
20121  */
20122 static const char *
20123 ill_hook_event2str(nic_event_t event)
20124 {
20125 	switch (event) {
20126 	case NE_PLUMB:
20127 		return ("PLUMB");
20128 	case NE_UNPLUMB:
20129 		return ("UNPLUMB");
20130 	case NE_UP:
20131 		return ("UP");
20132 	case NE_DOWN:
20133 		return ("DOWN");
20134 	case NE_ADDRESS_CHANGE:
20135 		return ("ADDRESS_CHANGE");
20136 	case NE_LIF_UP:
20137 		return ("LIF_UP");
20138 	case NE_LIF_DOWN:
20139 		return ("LIF_DOWN");
20140 	default:
20141 		return ("UNKNOWN");
20142 	}
20143 }
20144 
20145 void
20146 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20147     nic_event_data_t data, size_t datalen)
20148 {
20149 	ip_stack_t		*ipst = ill->ill_ipst;
20150 	hook_nic_event_int_t	*info;
20151 	const char		*str = NULL;
20152 
20153 	/* create a new nic event info */
20154 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20155 		goto fail;
20156 
20157 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20158 	info->hnei_event.hne_lif = lif;
20159 	info->hnei_event.hne_event = event;
20160 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20161 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20162 	info->hnei_event.hne_data = NULL;
20163 	info->hnei_event.hne_datalen = 0;
20164 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20165 
20166 	if (data != NULL && datalen != 0) {
20167 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20168 		if (info->hnei_event.hne_data == NULL)
20169 			goto fail;
20170 		bcopy(data, info->hnei_event.hne_data, datalen);
20171 		info->hnei_event.hne_datalen = datalen;
20172 	}
20173 
20174 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20175 	    DDI_NOSLEEP) == DDI_SUCCESS)
20176 		return;
20177 
20178 fail:
20179 	if (info != NULL) {
20180 		if (info->hnei_event.hne_data != NULL) {
20181 			kmem_free(info->hnei_event.hne_data,
20182 			    info->hnei_event.hne_datalen);
20183 		}
20184 		kmem_free(info, sizeof (hook_nic_event_t));
20185 	}
20186 	str = ill_hook_event2str(event);
20187 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20188 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20189 }
20190 
20191 void
20192 ipif_up_notify(ipif_t *ipif)
20193 {
20194 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20195 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20196 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20197 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20198 	    NE_LIF_UP, NULL, 0);
20199 }
20200