xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision d42c7aec1963a7ded6694ac33a5bd96422fc8ca7)
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/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/cpuvar.h>
48 #include <sys/time.h>
49 #include <sys/ctype.h>
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 #include <sys/callb.h>
68 #include <sys/md5.h>
69 
70 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
71 #include <inet/mi.h>
72 #include <inet/nd.h>
73 #include <inet/arp.h>
74 #include <inet/mib2.h>
75 #include <inet/ip.h>
76 #include <inet/ip6.h>
77 #include <inet/ip6_asp.h>
78 #include <inet/tcp.h>
79 #include <inet/ip_multi.h>
80 #include <inet/ip_ire.h>
81 #include <inet/ip_ftable.h>
82 #include <inet/ip_rts.h>
83 #include <inet/ip_ndp.h>
84 #include <inet/ip_if.h>
85 #include <inet/ip_impl.h>
86 #include <inet/tun.h>
87 #include <inet/sctp_ip.h>
88 #include <inet/ip_netinfo.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac_client.h>
100 #include <sys/dld.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ill_is_quiescent(ill_t *);
135 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
136 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
137 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
146     mblk_t *mp);
147 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
148     queue_t *q, mblk_t *mp, boolean_t need_up);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160     boolean_t initialize, boolean_t insert);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
172 static void	ipif_set_default(ipif_t *ipif);
173 static int	ipif_set_values(queue_t *q, mblk_t *mp,
174     char *interf_name, uint_t *ppa);
175 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
176     queue_t *q);
177 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
178     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
179     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
180 static void	ipif_update_other_ipifs(ipif_t *old_ipif);
181 
182 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
183 static int	ill_arp_off(ill_t *ill);
184 static int	ill_arp_on(ill_t *ill);
185 static void	ill_delete_interface_type(ill_if_t *);
186 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
187 static void	ill_dl_down(ill_t *ill);
188 static void	ill_down(ill_t *ill);
189 static void	ill_downi(ire_t *ire, char *ill_arg);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static void	ill_phyint_reinit(ill_t *ill);
193 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
194 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
195 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
196 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
197 static ip_v6mapinfo_func_t ip_ether_v6mapinfo, ip_ib_v6mapinfo;
198 static ip_v4mapinfo_func_t ip_ether_v4mapinfo, ip_ib_v4mapinfo;
199 static void	ipif_save_ire(ipif_t *, ire_t *);
200 static void	ipif_remove_ire(ipif_t *, ire_t *);
201 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
202 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
203 static void	phyint_free(phyint_t *);
204 
205 /*
206  * Per-ill IPsec capabilities management.
207  */
208 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
209 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
210 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
211 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
212 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
213 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
214     boolean_t);
215 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
216 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
217 static void ill_capability_mdt_reset_fill(ill_t *, mblk_t *);
218 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
219 static void ill_capability_ipsec_reset_fill(ill_t *, mblk_t *);
220 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
221 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
222 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
223     dl_capability_sub_t *);
224 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
225 static int  ill_capability_ipsec_reset_size(ill_t *, int *, int *, int *,
226     int *);
227 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
228 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
229 		    dl_capability_sub_t *);
230 static void	ill_capability_dld_enable(ill_t *);
231 static void	ill_capability_ack_thr(void *);
232 static void	ill_capability_lso_enable(ill_t *);
233 static void	ill_capability_send(ill_t *, mblk_t *);
234 
235 static ill_t	*ill_prev_usesrc(ill_t *);
236 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
237 static void	ill_disband_usesrc_group(ill_t *);
238 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
239 
240 #ifdef DEBUG
241 static  void    ill_trace_cleanup(const ill_t *);
242 static  void    ipif_trace_cleanup(const ipif_t *);
243 #endif
244 
245 /*
246  * if we go over the memory footprint limit more than once in this msec
247  * interval, we'll start pruning aggressively.
248  */
249 int ip_min_frag_prune_time = 0;
250 
251 /*
252  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
253  * and the IPsec DOI
254  */
255 #define	MAX_IPSEC_ALGS	256
256 
257 #define	BITSPERBYTE	8
258 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
259 
260 #define	IPSEC_ALG_ENABLE(algs, algid) \
261 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
262 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
263 
264 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
265 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
266 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
267 
268 typedef uint8_t ipsec_capab_elem_t;
269 
270 /*
271  * Per-algorithm parameters.  Note that at present, only encryption
272  * algorithms have variable keysize (IKE does not provide a way to negotiate
273  * auth algorithm keysize).
274  *
275  * All sizes here are in bits.
276  */
277 typedef struct
278 {
279 	uint16_t	minkeylen;
280 	uint16_t	maxkeylen;
281 } ipsec_capab_algparm_t;
282 
283 /*
284  * Per-ill capabilities.
285  */
286 struct ill_ipsec_capab_s {
287 	ipsec_capab_elem_t *encr_hw_algs;
288 	ipsec_capab_elem_t *auth_hw_algs;
289 	uint32_t algs_size;	/* size of _hw_algs in bytes */
290 	/* algorithm key lengths */
291 	ipsec_capab_algparm_t *encr_algparm;
292 	uint32_t encr_algparm_size;
293 	uint32_t encr_algparm_end;
294 };
295 
296 /*
297  * The field values are larger than strictly necessary for simple
298  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
299  */
300 static area_t	ip_area_template = {
301 	AR_ENTRY_ADD,			/* area_cmd */
302 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
303 					/* area_name_offset */
304 	/* area_name_length temporarily holds this structure length */
305 	sizeof (area_t),			/* area_name_length */
306 	IP_ARP_PROTO_TYPE,		/* area_proto */
307 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
308 	IP_ADDR_LEN,			/* area_proto_addr_length */
309 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
310 					/* area_proto_mask_offset */
311 	0,				/* area_flags */
312 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
313 					/* area_hw_addr_offset */
314 	/* Zero length hw_addr_length means 'use your idea of the address' */
315 	0				/* area_hw_addr_length */
316 };
317 
318 /*
319  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
320  * support
321  */
322 static area_t	ip6_area_template = {
323 	AR_ENTRY_ADD,			/* area_cmd */
324 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
325 					/* area_name_offset */
326 	/* area_name_length temporarily holds this structure length */
327 	sizeof (area_t),			/* area_name_length */
328 	IP_ARP_PROTO_TYPE,		/* area_proto */
329 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
330 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
331 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
332 					/* area_proto_mask_offset */
333 	0,				/* area_flags */
334 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
335 					/* area_hw_addr_offset */
336 	/* Zero length hw_addr_length means 'use your idea of the address' */
337 	0				/* area_hw_addr_length */
338 };
339 
340 static ared_t	ip_ared_template = {
341 	AR_ENTRY_DELETE,
342 	sizeof (ared_t) + IP_ADDR_LEN,
343 	sizeof (ared_t),
344 	IP_ARP_PROTO_TYPE,
345 	sizeof (ared_t),
346 	IP_ADDR_LEN,
347 	0
348 };
349 
350 static ared_t	ip6_ared_template = {
351 	AR_ENTRY_DELETE,
352 	sizeof (ared_t) + IPV6_ADDR_LEN,
353 	sizeof (ared_t),
354 	IP_ARP_PROTO_TYPE,
355 	sizeof (ared_t),
356 	IPV6_ADDR_LEN,
357 	0
358 };
359 
360 /*
361  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
362  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
363  * areq is used).
364  */
365 static areq_t	ip_areq_template = {
366 	AR_ENTRY_QUERY,			/* cmd */
367 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
368 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
369 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
370 	sizeof (areq_t),			/* target addr offset */
371 	IP_ADDR_LEN,			/* target addr_length */
372 	0,				/* flags */
373 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
374 	IP_ADDR_LEN,			/* sender addr length */
375 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
376 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
377 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
378 	/* anything else filled in by the code */
379 };
380 
381 static arc_t	ip_aru_template = {
382 	AR_INTERFACE_UP,
383 	sizeof (arc_t),		/* Name offset */
384 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
385 };
386 
387 static arc_t	ip_ard_template = {
388 	AR_INTERFACE_DOWN,
389 	sizeof (arc_t),		/* Name offset */
390 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
391 };
392 
393 static arc_t	ip_aron_template = {
394 	AR_INTERFACE_ON,
395 	sizeof (arc_t),		/* Name offset */
396 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
397 };
398 
399 static arc_t	ip_aroff_template = {
400 	AR_INTERFACE_OFF,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arma_t	ip_arma_multi_template = {
406 	AR_MAPPING_ADD,
407 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
408 				/* Name offset */
409 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
410 	IP_ARP_PROTO_TYPE,
411 	sizeof (arma_t),			/* proto_addr_offset */
412 	IP_ADDR_LEN,				/* proto_addr_length */
413 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
414 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
415 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
416 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
417 	IP_MAX_HW_LEN,				/* hw_addr_length */
418 	0,					/* hw_mapping_start */
419 };
420 
421 static ipft_t	ip_ioctl_ftbl[] = {
422 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
423 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
424 		IPFT_F_NO_REPLY },
425 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
426 		IPFT_F_NO_REPLY },
427 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
428 	{ 0 }
429 };
430 
431 /* Simple ICMP IP Header Template */
432 static ipha_t icmp_ipha = {
433 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
434 };
435 
436 /* Flag descriptors for ip_ipif_report */
437 static nv_t	ipif_nv_tbl[] = {
438 	{ IPIF_UP,		"UP" },
439 	{ IPIF_BROADCAST,	"BROADCAST" },
440 	{ ILLF_DEBUG,		"DEBUG" },
441 	{ PHYI_LOOPBACK,	"LOOPBACK" },
442 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
443 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
444 	{ PHYI_RUNNING,		"RUNNING" },
445 	{ ILLF_NOARP,		"NOARP" },
446 	{ PHYI_PROMISC,		"PROMISC" },
447 	{ PHYI_ALLMULTI,	"ALLMULTI" },
448 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
449 	{ ILLF_MULTICAST,	"MULTICAST" },
450 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
451 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
452 	{ IPIF_DHCPRUNNING,	"DHCP" },
453 	{ IPIF_PRIVATE,		"PRIVATE" },
454 	{ IPIF_NOXMIT,		"NOXMIT" },
455 	{ IPIF_NOLOCAL,		"NOLOCAL" },
456 	{ IPIF_DEPRECATED,	"DEPRECATED" },
457 	{ IPIF_PREFERRED,	"PREFERRED" },
458 	{ IPIF_TEMPORARY,	"TEMPORARY" },
459 	{ IPIF_ADDRCONF,	"ADDRCONF" },
460 	{ PHYI_VIRTUAL,		"VIRTUAL" },
461 	{ ILLF_ROUTER,		"ROUTER" },
462 	{ ILLF_NONUD,		"NONUD" },
463 	{ IPIF_ANYCAST,		"ANYCAST" },
464 	{ ILLF_NORTEXCH,	"NORTEXCH" },
465 	{ ILLF_IPV4,		"IPV4" },
466 	{ ILLF_IPV6,		"IPV6" },
467 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
468 	{ PHYI_FAILED,		"FAILED" },
469 	{ PHYI_STANDBY,		"STANDBY" },
470 	{ PHYI_INACTIVE,	"INACTIVE" },
471 	{ PHYI_OFFLINE,		"OFFLINE" },
472 	{ PHYI_IPMP,		"IPMP" }
473 };
474 
475 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
476 
477 static ip_m_t   ip_m_tbl[] = {
478 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
479 	    ip_ether_v6intfid },
480 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
481 	    ip_nodef_v6intfid },
482 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
483 	    ip_nodef_v6intfid },
484 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
485 	    ip_nodef_v6intfid },
486 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
487 	    ip_ether_v6intfid },
488 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
489 	    ip_ib_v6intfid },
490 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL },
491 	{ SUNW_DL_IPMP, IFT_OTHER, NULL, NULL, ip_ipmp_v6intfid },
492 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
493 	    ip_nodef_v6intfid }
494 };
495 
496 static ill_t	ill_null;		/* Empty ILL for init. */
497 char	ipif_loopback_name[] = "lo0";
498 static char *ipv4_forward_suffix = ":ip_forwarding";
499 static char *ipv6_forward_suffix = ":ip6_forwarding";
500 static	sin6_t	sin6_null;	/* Zero address for quick clears */
501 static	sin_t	sin_null;	/* Zero address for quick clears */
502 
503 /* When set search for unused ipif_seqid */
504 static ipif_t	ipif_zero;
505 
506 /*
507  * ppa arena is created after these many
508  * interfaces have been plumbed.
509  */
510 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
511 
512 /*
513  * Allocate per-interface mibs.
514  * Returns true if ok. False otherwise.
515  *  ipsq  may not yet be allocated (loopback case ).
516  */
517 static boolean_t
518 ill_allocate_mibs(ill_t *ill)
519 {
520 	/* Already allocated? */
521 	if (ill->ill_ip_mib != NULL) {
522 		if (ill->ill_isv6)
523 			ASSERT(ill->ill_icmp6_mib != NULL);
524 		return (B_TRUE);
525 	}
526 
527 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
528 	    KM_NOSLEEP);
529 	if (ill->ill_ip_mib == NULL) {
530 		return (B_FALSE);
531 	}
532 
533 	/* Setup static information */
534 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
535 	    sizeof (mib2_ipIfStatsEntry_t));
536 	if (ill->ill_isv6) {
537 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
538 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
539 		    sizeof (mib2_ipv6AddrEntry_t));
540 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
541 		    sizeof (mib2_ipv6RouteEntry_t));
542 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
543 		    sizeof (mib2_ipv6NetToMediaEntry_t));
544 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
545 		    sizeof (ipv6_member_t));
546 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
547 		    sizeof (ipv6_grpsrc_t));
548 	} else {
549 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
550 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
551 		    sizeof (mib2_ipAddrEntry_t));
552 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
553 		    sizeof (mib2_ipRouteEntry_t));
554 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
555 		    sizeof (mib2_ipNetToMediaEntry_t));
556 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
557 		    sizeof (ip_member_t));
558 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
559 		    sizeof (ip_grpsrc_t));
560 
561 		/*
562 		 * For a v4 ill, we are done at this point, because per ill
563 		 * icmp mibs are only used for v6.
564 		 */
565 		return (B_TRUE);
566 	}
567 
568 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
569 	    KM_NOSLEEP);
570 	if (ill->ill_icmp6_mib == NULL) {
571 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
572 		ill->ill_ip_mib = NULL;
573 		return (B_FALSE);
574 	}
575 	/* static icmp info */
576 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
577 	    sizeof (mib2_ipv6IfIcmpEntry_t);
578 	/*
579 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
580 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
581 	 * -> ill_phyint_reinit
582 	 */
583 	return (B_TRUE);
584 }
585 
586 /*
587  * Common code for preparation of ARP commands.  Two points to remember:
588  * 	1) The ill_name is tacked on at the end of the allocated space so
589  *	   the templates name_offset field must contain the total space
590  *	   to allocate less the name length.
591  *
592  *	2) The templates name_length field should contain the *template*
593  *	   length.  We use it as a parameter to bcopy() and then write
594  *	   the real ill_name_length into the name_length field of the copy.
595  * (Always called as writer.)
596  */
597 mblk_t *
598 ill_arp_alloc(ill_t *ill, const uchar_t *template, caddr_t addr)
599 {
600 	arc_t	*arc = (arc_t *)template;
601 	char	*cp;
602 	int	len;
603 	mblk_t	*mp;
604 	uint_t	name_length = ill->ill_name_length;
605 	uint_t	template_len = arc->arc_name_length;
606 
607 	len = arc->arc_name_offset + name_length;
608 	mp = allocb(len, BPRI_HI);
609 	if (mp == NULL)
610 		return (NULL);
611 	cp = (char *)mp->b_rptr;
612 	mp->b_wptr = (uchar_t *)&cp[len];
613 	if (template_len)
614 		bcopy(template, cp, template_len);
615 	if (len > template_len)
616 		bzero(&cp[template_len], len - template_len);
617 	mp->b_datap->db_type = M_PROTO;
618 
619 	arc = (arc_t *)cp;
620 	arc->arc_name_length = name_length;
621 	cp = (char *)arc + arc->arc_name_offset;
622 	bcopy(ill->ill_name, cp, name_length);
623 
624 	if (addr) {
625 		area_t	*area = (area_t *)mp->b_rptr;
626 
627 		cp = (char *)area + area->area_proto_addr_offset;
628 		bcopy(addr, cp, area->area_proto_addr_length);
629 		if (area->area_cmd == AR_ENTRY_ADD) {
630 			cp = (char *)area;
631 			len = area->area_proto_addr_length;
632 			if (area->area_proto_mask_offset)
633 				cp += area->area_proto_mask_offset;
634 			else
635 				cp += area->area_proto_addr_offset + len;
636 			while (len-- > 0)
637 				*cp++ = (char)~0;
638 		}
639 	}
640 	return (mp);
641 }
642 
643 mblk_t *
644 ipif_area_alloc(ipif_t *ipif, uint_t optflags)
645 {
646 	caddr_t	addr;
647 	mblk_t 	*mp;
648 	area_t	*area;
649 	uchar_t	*areap;
650 	ill_t	*ill = ipif->ipif_ill;
651 
652 	if (ill->ill_isv6) {
653 		ASSERT(ill->ill_flags & ILLF_XRESOLV);
654 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
655 		areap = (uchar_t *)&ip6_area_template;
656 	} else {
657 		addr = (caddr_t)&ipif->ipif_lcl_addr;
658 		areap = (uchar_t *)&ip_area_template;
659 	}
660 
661 	if ((mp = ill_arp_alloc(ill, areap, addr)) == NULL)
662 		return (NULL);
663 
664 	/*
665 	 * IPMP requires that the hardware address be included in all
666 	 * AR_ENTRY_ADD requests so that ARP can deduce the arl to send on.
667 	 * If there are no active underlying ills in the group (and thus no
668 	 * hardware address, DAD will be deferred until an underlying ill
669 	 * becomes active.
670 	 */
671 	if (IS_IPMP(ill)) {
672 		if ((ill = ipmp_ipif_hold_bound_ill(ipif)) == NULL) {
673 			freemsg(mp);
674 			return (NULL);
675 		}
676 	} else {
677 		ill_refhold(ill);
678 	}
679 
680 	area = (area_t *)mp->b_rptr;
681 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
682 	area->area_flags |= optflags;
683 	area->area_hw_addr_length = ill->ill_phys_addr_length;
684 	bcopy(ill->ill_phys_addr, mp->b_rptr + area->area_hw_addr_offset,
685 	    area->area_hw_addr_length);
686 
687 	ill_refrele(ill);
688 	return (mp);
689 }
690 
691 mblk_t *
692 ipif_ared_alloc(ipif_t *ipif)
693 {
694 	caddr_t	addr;
695 	uchar_t	*aredp;
696 
697 	if (ipif->ipif_ill->ill_isv6) {
698 		ASSERT(ipif->ipif_ill->ill_flags & ILLF_XRESOLV);
699 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
700 		aredp = (uchar_t *)&ip6_ared_template;
701 	} else {
702 		addr = (caddr_t)&ipif->ipif_lcl_addr;
703 		aredp = (uchar_t *)&ip_ared_template;
704 	}
705 
706 	return (ill_arp_alloc(ipif->ipif_ill, aredp, addr));
707 }
708 
709 mblk_t *
710 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
711 {
712 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
713 	    (char *)&addr));
714 }
715 
716 mblk_t *
717 ill_arie_alloc(ill_t *ill, const char *grifname, const void *template)
718 {
719 	mblk_t	*mp = ill_arp_alloc(ill, template, 0);
720 	arie_t	*arie;
721 
722 	if (mp != NULL) {
723 		arie = (arie_t *)mp->b_rptr;
724 		(void) strlcpy(arie->arie_grifname, grifname, LIFNAMSIZ);
725 	}
726 	return (mp);
727 }
728 
729 /*
730  * Completely vaporize a lower level tap and all associated interfaces.
731  * ill_delete is called only out of ip_close when the device control
732  * stream is being closed.
733  */
734 void
735 ill_delete(ill_t *ill)
736 {
737 	ipif_t	*ipif;
738 	ill_t	*prev_ill;
739 	ip_stack_t	*ipst = ill->ill_ipst;
740 
741 	/*
742 	 * ill_delete may be forcibly entering the ipsq. The previous
743 	 * ioctl may not have completed and may need to be aborted.
744 	 * ipsq_flush takes care of it. If we don't need to enter the
745 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
746 	 * ill_delete_tail is sufficient.
747 	 */
748 	ipsq_flush(ill);
749 
750 	/*
751 	 * Nuke all interfaces.  ipif_free will take down the interface,
752 	 * remove it from the list, and free the data structure.
753 	 * Walk down the ipif list and remove the logical interfaces
754 	 * first before removing the main ipif. We can't unplumb
755 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
756 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
757 	 * POINTOPOINT.
758 	 *
759 	 * If ill_ipif was not properly initialized (i.e low on memory),
760 	 * then no interfaces to clean up. In this case just clean up the
761 	 * ill.
762 	 */
763 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
764 		ipif_free(ipif);
765 
766 	/*
767 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
768 	 * So nobody can be using this mp now. Free the mp allocated for
769 	 * honoring ILLF_NOARP
770 	 */
771 	freemsg(ill->ill_arp_on_mp);
772 	ill->ill_arp_on_mp = NULL;
773 
774 	/* Clean up msgs on pending upcalls for mrouted */
775 	reset_mrt_ill(ill);
776 
777 	/*
778 	 * ipif_free -> reset_conn_ipif will remove all multicast
779 	 * references for IPv4. For IPv6, we need to do it here as
780 	 * it points only at ills.
781 	 */
782 	reset_conn_ill(ill);
783 
784 	/*
785 	 * Remove multicast references added as a result of calls to
786 	 * ip_join_allmulti().
787 	 */
788 	ip_purge_allmulti(ill);
789 
790 	/*
791 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
792 	 */
793 	if (IS_UNDER_IPMP(ill))
794 		ipmp_ill_leave_illgrp(ill);
795 
796 	/*
797 	 * ill_down will arrange to blow off any IRE's dependent on this
798 	 * ILL, and shut down fragmentation reassembly.
799 	 */
800 	ill_down(ill);
801 
802 	/* Let SCTP know, so that it can remove this from its list. */
803 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
804 
805 	/*
806 	 * If an address on this ILL is being used as a source address then
807 	 * clear out the pointers in other ILLs that point to this ILL.
808 	 */
809 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
810 	if (ill->ill_usesrc_grp_next != NULL) {
811 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
812 			ill_disband_usesrc_group(ill);
813 		} else {	/* consumer of the usesrc ILL */
814 			prev_ill = ill_prev_usesrc(ill);
815 			prev_ill->ill_usesrc_grp_next =
816 			    ill->ill_usesrc_grp_next;
817 		}
818 	}
819 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
820 }
821 
822 static void
823 ipif_non_duplicate(ipif_t *ipif)
824 {
825 	ill_t *ill = ipif->ipif_ill;
826 	mutex_enter(&ill->ill_lock);
827 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
828 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
829 		ASSERT(ill->ill_ipif_dup_count > 0);
830 		ill->ill_ipif_dup_count--;
831 	}
832 	mutex_exit(&ill->ill_lock);
833 }
834 
835 /*
836  * ill_delete_tail is called from ip_modclose after all references
837  * to the closing ill are gone. The wait is done in ip_modclose
838  */
839 void
840 ill_delete_tail(ill_t *ill)
841 {
842 	mblk_t	**mpp;
843 	ipif_t	*ipif;
844 	ip_stack_t	*ipst = ill->ill_ipst;
845 
846 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
847 		ipif_non_duplicate(ipif);
848 		ipif_down_tail(ipif);
849 	}
850 
851 	ASSERT(ill->ill_ipif_dup_count == 0 &&
852 	    ill->ill_arp_down_mp == NULL &&
853 	    ill->ill_arp_del_mapping_mp == NULL);
854 
855 	/*
856 	 * If polling capability is enabled (which signifies direct
857 	 * upcall into IP and driver has ill saved as a handle),
858 	 * we need to make sure that unbind has completed before we
859 	 * let the ill disappear and driver no longer has any reference
860 	 * to this ill.
861 	 */
862 	mutex_enter(&ill->ill_lock);
863 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
864 		cv_wait(&ill->ill_cv, &ill->ill_lock);
865 	mutex_exit(&ill->ill_lock);
866 	ASSERT(!(ill->ill_capabilities &
867 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
868 
869 	if (ill->ill_net_type != IRE_LOOPBACK)
870 		qprocsoff(ill->ill_rq);
871 
872 	/*
873 	 * We do an ipsq_flush once again now. New messages could have
874 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
875 	 * could also have landed up if an ioctl thread had looked up
876 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
877 	 * enqueued the ioctl when we did the ipsq_flush last time.
878 	 */
879 	ipsq_flush(ill);
880 
881 	/*
882 	 * Free capabilities.
883 	 */
884 	if (ill->ill_ipsec_capab_ah != NULL) {
885 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
886 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
887 		ill->ill_ipsec_capab_ah = NULL;
888 	}
889 
890 	if (ill->ill_ipsec_capab_esp != NULL) {
891 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
892 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
893 		ill->ill_ipsec_capab_esp = NULL;
894 	}
895 
896 	if (ill->ill_mdt_capab != NULL) {
897 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
898 		ill->ill_mdt_capab = NULL;
899 	}
900 
901 	if (ill->ill_hcksum_capab != NULL) {
902 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
903 		ill->ill_hcksum_capab = NULL;
904 	}
905 
906 	if (ill->ill_zerocopy_capab != NULL) {
907 		kmem_free(ill->ill_zerocopy_capab,
908 		    sizeof (ill_zerocopy_capab_t));
909 		ill->ill_zerocopy_capab = NULL;
910 	}
911 
912 	if (ill->ill_lso_capab != NULL) {
913 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
914 		ill->ill_lso_capab = NULL;
915 	}
916 
917 	if (ill->ill_dld_capab != NULL) {
918 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
919 		ill->ill_dld_capab = NULL;
920 	}
921 
922 	while (ill->ill_ipif != NULL)
923 		ipif_free_tail(ill->ill_ipif);
924 
925 	/*
926 	 * We have removed all references to ilm from conn and the ones joined
927 	 * within the kernel.
928 	 *
929 	 * We don't walk conns, mrts and ires because
930 	 *
931 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
932 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
933 	 *    ill references.
934 	 */
935 	ASSERT(ilm_walk_ill(ill) == 0);
936 
937 	/*
938 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
939 	 * is safe to do because the illgrp has already been unlinked from the
940 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
941 	 */
942 	if (IS_IPMP(ill)) {
943 		ipmp_illgrp_destroy(ill->ill_grp);
944 		ill->ill_grp = NULL;
945 	}
946 
947 	/*
948 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
949 	 * could free the phyint. No more reference to the phyint after this
950 	 * point.
951 	 */
952 	(void) ill_glist_delete(ill);
953 
954 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
955 	if (ill->ill_ndd_name != NULL)
956 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
957 	rw_exit(&ipst->ips_ip_g_nd_lock);
958 
959 	if (ill->ill_frag_ptr != NULL) {
960 		uint_t count;
961 
962 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
963 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
964 		}
965 		mi_free(ill->ill_frag_ptr);
966 		ill->ill_frag_ptr = NULL;
967 		ill->ill_frag_hash_tbl = NULL;
968 	}
969 
970 	freemsg(ill->ill_nd_lla_mp);
971 	/* Free all retained control messages. */
972 	mpp = &ill->ill_first_mp_to_free;
973 	do {
974 		while (mpp[0]) {
975 			mblk_t  *mp;
976 			mblk_t  *mp1;
977 
978 			mp = mpp[0];
979 			mpp[0] = mp->b_next;
980 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
981 				mp1->b_next = NULL;
982 				mp1->b_prev = NULL;
983 			}
984 			freemsg(mp);
985 		}
986 	} while (mpp++ != &ill->ill_last_mp_to_free);
987 
988 	ill_free_mib(ill);
989 
990 #ifdef DEBUG
991 	ill_trace_cleanup(ill);
992 #endif
993 
994 	/* Drop refcnt here */
995 	netstack_rele(ill->ill_ipst->ips_netstack);
996 	ill->ill_ipst = NULL;
997 }
998 
999 static void
1000 ill_free_mib(ill_t *ill)
1001 {
1002 	ip_stack_t *ipst = ill->ill_ipst;
1003 
1004 	/*
1005 	 * MIB statistics must not be lost, so when an interface
1006 	 * goes away the counter values will be added to the global
1007 	 * MIBs.
1008 	 */
1009 	if (ill->ill_ip_mib != NULL) {
1010 		if (ill->ill_isv6) {
1011 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
1012 			    ill->ill_ip_mib);
1013 		} else {
1014 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
1015 			    ill->ill_ip_mib);
1016 		}
1017 
1018 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
1019 		ill->ill_ip_mib = NULL;
1020 	}
1021 	if (ill->ill_icmp6_mib != NULL) {
1022 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
1023 		    ill->ill_icmp6_mib);
1024 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
1025 		ill->ill_icmp6_mib = NULL;
1026 	}
1027 }
1028 
1029 /*
1030  * Concatenate together a physical address and a sap.
1031  *
1032  * Sap_lengths are interpreted as follows:
1033  *   sap_length == 0	==>	no sap
1034  *   sap_length > 0	==>	sap is at the head of the dlpi address
1035  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1036  */
1037 static void
1038 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1039     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1040 {
1041 	uint16_t sap_addr = (uint16_t)sap_src;
1042 
1043 	if (sap_length == 0) {
1044 		if (phys_src == NULL)
1045 			bzero(dst, phys_length);
1046 		else
1047 			bcopy(phys_src, dst, phys_length);
1048 	} else if (sap_length < 0) {
1049 		if (phys_src == NULL)
1050 			bzero(dst, phys_length);
1051 		else
1052 			bcopy(phys_src, dst, phys_length);
1053 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1054 	} else {
1055 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1056 		if (phys_src == NULL)
1057 			bzero((char *)dst + sap_length, phys_length);
1058 		else
1059 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1060 	}
1061 }
1062 
1063 /*
1064  * Generate a dl_unitdata_req mblk for the device and address given.
1065  * addr_length is the length of the physical portion of the address.
1066  * If addr is NULL include an all zero address of the specified length.
1067  * TRUE? In any case, addr_length is taken to be the entire length of the
1068  * dlpi address, including the absolute value of sap_length.
1069  */
1070 mblk_t *
1071 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1072 		t_scalar_t sap_length)
1073 {
1074 	dl_unitdata_req_t *dlur;
1075 	mblk_t	*mp;
1076 	t_scalar_t	abs_sap_length;		/* absolute value */
1077 
1078 	abs_sap_length = ABS(sap_length);
1079 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1080 	    DL_UNITDATA_REQ);
1081 	if (mp == NULL)
1082 		return (NULL);
1083 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1084 	/* HACK: accomodate incompatible DLPI drivers */
1085 	if (addr_length == 8)
1086 		addr_length = 6;
1087 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1088 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1089 	dlur->dl_priority.dl_min = 0;
1090 	dlur->dl_priority.dl_max = 0;
1091 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1092 	    (uchar_t *)&dlur[1]);
1093 	return (mp);
1094 }
1095 
1096 /*
1097  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1098  * Return an error if we already have 1 or more ioctls in progress.
1099  * This is used only for non-exclusive ioctls. Currently this is used
1100  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1101  * and thus need to use ipsq_pending_mp_add.
1102  */
1103 boolean_t
1104 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1105 {
1106 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1107 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1108 	/*
1109 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1110 	 */
1111 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1112 	    (add_mp->b_datap->db_type == M_IOCTL));
1113 
1114 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1115 	/*
1116 	 * Return error if the conn has started closing. The conn
1117 	 * could have finished cleaning up the pending mp list,
1118 	 * If so we should not add another mp to the list negating
1119 	 * the cleanup.
1120 	 */
1121 	if (connp->conn_state_flags & CONN_CLOSING)
1122 		return (B_FALSE);
1123 	/*
1124 	 * Add the pending mp to the head of the list, chained by b_next.
1125 	 * Note down the conn on which the ioctl request came, in b_prev.
1126 	 * This will be used to later get the conn, when we get a response
1127 	 * on the ill queue, from some other module (typically arp)
1128 	 */
1129 	add_mp->b_next = (void *)ill->ill_pending_mp;
1130 	add_mp->b_queue = CONNP_TO_WQ(connp);
1131 	ill->ill_pending_mp = add_mp;
1132 	if (connp != NULL)
1133 		connp->conn_oper_pending_ill = ill;
1134 	return (B_TRUE);
1135 }
1136 
1137 /*
1138  * Retrieve the ill_pending_mp and return it. We have to walk the list
1139  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1140  */
1141 mblk_t *
1142 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1143 {
1144 	mblk_t	*prev = NULL;
1145 	mblk_t	*curr = NULL;
1146 	uint_t	id;
1147 	conn_t	*connp;
1148 
1149 	/*
1150 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1151 	 * up the pending mp, but it does not know the ioc_id and
1152 	 * passes in a zero for it.
1153 	 */
1154 	mutex_enter(&ill->ill_lock);
1155 	if (ioc_id != 0)
1156 		*connpp = NULL;
1157 
1158 	/* Search the list for the appropriate ioctl based on ioc_id */
1159 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1160 	    prev = curr, curr = curr->b_next) {
1161 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1162 		connp = Q_TO_CONN(curr->b_queue);
1163 		/* Match based on the ioc_id or based on the conn */
1164 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1165 			break;
1166 	}
1167 
1168 	if (curr != NULL) {
1169 		/* Unlink the mblk from the pending mp list */
1170 		if (prev != NULL) {
1171 			prev->b_next = curr->b_next;
1172 		} else {
1173 			ASSERT(ill->ill_pending_mp == curr);
1174 			ill->ill_pending_mp = curr->b_next;
1175 		}
1176 
1177 		/*
1178 		 * conn refcnt must have been bumped up at the start of
1179 		 * the ioctl. So we can safely access the conn.
1180 		 */
1181 		ASSERT(CONN_Q(curr->b_queue));
1182 		*connpp = Q_TO_CONN(curr->b_queue);
1183 		curr->b_next = NULL;
1184 		curr->b_queue = NULL;
1185 	}
1186 
1187 	mutex_exit(&ill->ill_lock);
1188 
1189 	return (curr);
1190 }
1191 
1192 /*
1193  * Add the pending mp to the list. There can be only 1 pending mp
1194  * in the list. Any exclusive ioctl that needs to wait for a response
1195  * from another module or driver needs to use this function to set
1196  * the ipx_pending_mp to the ioctl mblk and wait for the response from
1197  * the other module/driver. This is also used while waiting for the
1198  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1199  */
1200 boolean_t
1201 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1202     int waitfor)
1203 {
1204 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
1205 
1206 	ASSERT(IAM_WRITER_IPIF(ipif));
1207 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1208 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1209 	ASSERT(ipx->ipx_pending_mp == NULL);
1210 	/*
1211 	 * The caller may be using a different ipif than the one passed into
1212 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1213 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1214 	 * that `ipx_current_ipif == ipif'.
1215 	 */
1216 	ASSERT(ipx->ipx_current_ipif != NULL);
1217 
1218 	/*
1219 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1220 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1221 	 */
1222 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1223 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1224 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1225 
1226 	if (connp != NULL) {
1227 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1228 		/*
1229 		 * Return error if the conn has started closing. The conn
1230 		 * could have finished cleaning up the pending mp list,
1231 		 * If so we should not add another mp to the list negating
1232 		 * the cleanup.
1233 		 */
1234 		if (connp->conn_state_flags & CONN_CLOSING)
1235 			return (B_FALSE);
1236 	}
1237 	mutex_enter(&ipx->ipx_lock);
1238 	ipx->ipx_pending_ipif = ipif;
1239 	/*
1240 	 * Note down the queue in b_queue. This will be returned by
1241 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1242 	 * the processing
1243 	 */
1244 	add_mp->b_next = NULL;
1245 	add_mp->b_queue = q;
1246 	ipx->ipx_pending_mp = add_mp;
1247 	ipx->ipx_waitfor = waitfor;
1248 	mutex_exit(&ipx->ipx_lock);
1249 
1250 	if (connp != NULL)
1251 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1252 
1253 	return (B_TRUE);
1254 }
1255 
1256 /*
1257  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
1258  * queued in the list.
1259  */
1260 mblk_t *
1261 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1262 {
1263 	mblk_t	*curr = NULL;
1264 	ipxop_t	*ipx = ipsq->ipsq_xop;
1265 
1266 	*connpp = NULL;
1267 	mutex_enter(&ipx->ipx_lock);
1268 	if (ipx->ipx_pending_mp == NULL) {
1269 		mutex_exit(&ipx->ipx_lock);
1270 		return (NULL);
1271 	}
1272 
1273 	/* There can be only 1 such excl message */
1274 	curr = ipx->ipx_pending_mp;
1275 	ASSERT(curr->b_next == NULL);
1276 	ipx->ipx_pending_ipif = NULL;
1277 	ipx->ipx_pending_mp = NULL;
1278 	ipx->ipx_waitfor = 0;
1279 	mutex_exit(&ipx->ipx_lock);
1280 
1281 	if (CONN_Q(curr->b_queue)) {
1282 		/*
1283 		 * This mp did a refhold on the conn, at the start of the ioctl.
1284 		 * So we can safely return a pointer to the conn to the caller.
1285 		 */
1286 		*connpp = Q_TO_CONN(curr->b_queue);
1287 	} else {
1288 		*connpp = NULL;
1289 	}
1290 	curr->b_next = NULL;
1291 	curr->b_prev = NULL;
1292 	return (curr);
1293 }
1294 
1295 /*
1296  * Cleanup the ioctl mp queued in ipx_pending_mp
1297  * - Called in the ill_delete path
1298  * - Called in the M_ERROR or M_HANGUP path on the ill.
1299  * - Called in the conn close path.
1300  */
1301 boolean_t
1302 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1303 {
1304 	mblk_t	*mp;
1305 	ipxop_t	*ipx;
1306 	queue_t	*q;
1307 	ipif_t	*ipif;
1308 
1309 	ASSERT(IAM_WRITER_ILL(ill));
1310 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
1311 
1312 	/*
1313 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
1314 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1315 	 * even if it is meant for another ill, since we have to enqueue
1316 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
1317 	 * If connp is non-null we are called from the conn close path.
1318 	 */
1319 	mutex_enter(&ipx->ipx_lock);
1320 	mp = ipx->ipx_pending_mp;
1321 	if (mp == NULL || (connp != NULL &&
1322 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1323 		mutex_exit(&ipx->ipx_lock);
1324 		return (B_FALSE);
1325 	}
1326 	/* Now remove from the ipx_pending_mp */
1327 	ipx->ipx_pending_mp = NULL;
1328 	q = mp->b_queue;
1329 	mp->b_next = NULL;
1330 	mp->b_prev = NULL;
1331 	mp->b_queue = NULL;
1332 
1333 	ipif = ipx->ipx_pending_ipif;
1334 	ipx->ipx_pending_ipif = NULL;
1335 	ipx->ipx_waitfor = 0;
1336 	ipx->ipx_current_ipif = NULL;
1337 	ipx->ipx_current_ioctl = 0;
1338 	ipx->ipx_current_done = B_TRUE;
1339 	mutex_exit(&ipx->ipx_lock);
1340 
1341 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1342 		if (connp == NULL) {
1343 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1344 		} else {
1345 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1346 			mutex_enter(&ipif->ipif_ill->ill_lock);
1347 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1348 			mutex_exit(&ipif->ipif_ill->ill_lock);
1349 		}
1350 	} else {
1351 		/*
1352 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1353 		 * be just inet_freemsg. we have to restart it
1354 		 * otherwise the thread will be stuck.
1355 		 */
1356 		inet_freemsg(mp);
1357 	}
1358 	return (B_TRUE);
1359 }
1360 
1361 /*
1362  * The ill is closing. Cleanup all the pending mps. Called exclusively
1363  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1364  * knows this ill, and hence nobody can add an mp to this list
1365  */
1366 static void
1367 ill_pending_mp_cleanup(ill_t *ill)
1368 {
1369 	mblk_t	*mp;
1370 	queue_t	*q;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 
1374 	mutex_enter(&ill->ill_lock);
1375 	/*
1376 	 * Every mp on the pending mp list originating from an ioctl
1377 	 * added 1 to the conn refcnt, at the start of the ioctl.
1378 	 * So bump it down now.  See comments in ip_wput_nondata()
1379 	 */
1380 	while (ill->ill_pending_mp != NULL) {
1381 		mp = ill->ill_pending_mp;
1382 		ill->ill_pending_mp = mp->b_next;
1383 		mutex_exit(&ill->ill_lock);
1384 
1385 		q = mp->b_queue;
1386 		ASSERT(CONN_Q(q));
1387 		mp->b_next = NULL;
1388 		mp->b_prev = NULL;
1389 		mp->b_queue = NULL;
1390 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1391 		mutex_enter(&ill->ill_lock);
1392 	}
1393 	ill->ill_pending_ipif = NULL;
1394 
1395 	mutex_exit(&ill->ill_lock);
1396 }
1397 
1398 /*
1399  * Called in the conn close path and ill delete path
1400  */
1401 static void
1402 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1403 {
1404 	ipsq_t	*ipsq;
1405 	mblk_t	*prev;
1406 	mblk_t	*curr;
1407 	mblk_t	*next;
1408 	queue_t	*q;
1409 	mblk_t	*tmp_list = NULL;
1410 
1411 	ASSERT(IAM_WRITER_ILL(ill));
1412 	if (connp != NULL)
1413 		q = CONNP_TO_WQ(connp);
1414 	else
1415 		q = ill->ill_wq;
1416 
1417 	ipsq = ill->ill_phyint->phyint_ipsq;
1418 	/*
1419 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1420 	 * In the case of ioctl from a conn, there can be only 1 mp
1421 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1422 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1423 	 * ioctls meant for this ill form conn's are not flushed. They will
1424 	 * be processed during ipsq_exit and will not find the ill and will
1425 	 * return error.
1426 	 */
1427 	mutex_enter(&ipsq->ipsq_lock);
1428 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1429 	    curr = next) {
1430 		next = curr->b_next;
1431 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1432 			/* Unlink the mblk from the pending mp list */
1433 			if (prev != NULL) {
1434 				prev->b_next = curr->b_next;
1435 			} else {
1436 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1437 				ipsq->ipsq_xopq_mphead = curr->b_next;
1438 			}
1439 			if (ipsq->ipsq_xopq_mptail == curr)
1440 				ipsq->ipsq_xopq_mptail = prev;
1441 			/*
1442 			 * Create a temporary list and release the ipsq lock
1443 			 * New elements are added to the head of the tmp_list
1444 			 */
1445 			curr->b_next = tmp_list;
1446 			tmp_list = curr;
1447 		} else {
1448 			prev = curr;
1449 		}
1450 	}
1451 	mutex_exit(&ipsq->ipsq_lock);
1452 
1453 	while (tmp_list != NULL) {
1454 		curr = tmp_list;
1455 		tmp_list = curr->b_next;
1456 		curr->b_next = NULL;
1457 		curr->b_prev = NULL;
1458 		curr->b_queue = NULL;
1459 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1460 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1461 			    CONN_CLOSE : NO_COPYOUT, NULL);
1462 		} else {
1463 			/*
1464 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1465 			 * this can't be just inet_freemsg. we have to
1466 			 * restart it otherwise the thread will be stuck.
1467 			 */
1468 			inet_freemsg(curr);
1469 		}
1470 	}
1471 }
1472 
1473 /*
1474  * This conn has started closing. Cleanup any pending ioctl from this conn.
1475  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1476  */
1477 void
1478 conn_ioctl_cleanup(conn_t *connp)
1479 {
1480 	mblk_t *curr;
1481 	ipsq_t	*ipsq;
1482 	ill_t	*ill;
1483 	boolean_t refheld;
1484 
1485 	/*
1486 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1487 	 * ioctl has not yet started, the mp is pending in the list headed by
1488 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1489 	 * ipx_pending_mp. If the ioctl timed out in the streamhead but
1490 	 * is currently executing now the mp is not queued anywhere but
1491 	 * conn_oper_pending_ill is null. The conn close will wait
1492 	 * till the conn_ref drops to zero.
1493 	 */
1494 	mutex_enter(&connp->conn_lock);
1495 	ill = connp->conn_oper_pending_ill;
1496 	if (ill == NULL) {
1497 		mutex_exit(&connp->conn_lock);
1498 		return;
1499 	}
1500 
1501 	curr = ill_pending_mp_get(ill, &connp, 0);
1502 	if (curr != NULL) {
1503 		mutex_exit(&connp->conn_lock);
1504 		CONN_DEC_REF(connp);
1505 		inet_freemsg(curr);
1506 		return;
1507 	}
1508 	/*
1509 	 * We may not be able to refhold the ill if the ill/ipif
1510 	 * is changing. But we need to make sure that the ill will
1511 	 * not vanish. So we just bump up the ill_waiter count.
1512 	 */
1513 	refheld = ill_waiter_inc(ill);
1514 	mutex_exit(&connp->conn_lock);
1515 	if (refheld) {
1516 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1517 			ill_waiter_dcr(ill);
1518 			/*
1519 			 * Check whether this ioctl has started and is
1520 			 * pending. If it is not found there then check
1521 			 * whether this ioctl has not even started and is in
1522 			 * the ipsq_xopq list.
1523 			 */
1524 			if (!ipsq_pending_mp_cleanup(ill, connp))
1525 				ipsq_xopq_mp_cleanup(ill, connp);
1526 			ipsq = ill->ill_phyint->phyint_ipsq;
1527 			ipsq_exit(ipsq);
1528 			return;
1529 		}
1530 	}
1531 
1532 	/*
1533 	 * The ill is also closing and we could not bump up the
1534 	 * ill_waiter_count or we could not enter the ipsq. Leave
1535 	 * the cleanup to ill_delete
1536 	 */
1537 	mutex_enter(&connp->conn_lock);
1538 	while (connp->conn_oper_pending_ill != NULL)
1539 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1540 	mutex_exit(&connp->conn_lock);
1541 	if (refheld)
1542 		ill_waiter_dcr(ill);
1543 }
1544 
1545 /*
1546  * ipcl_walk function for cleaning up conn_*_ill fields.
1547  */
1548 static void
1549 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1550 {
1551 	ill_t	*ill = (ill_t *)arg;
1552 	ire_t	*ire;
1553 
1554 	mutex_enter(&connp->conn_lock);
1555 	if (connp->conn_multicast_ill == ill) {
1556 		/* Revert to late binding */
1557 		connp->conn_multicast_ill = NULL;
1558 	}
1559 	if (connp->conn_incoming_ill == ill)
1560 		connp->conn_incoming_ill = NULL;
1561 	if (connp->conn_outgoing_ill == ill)
1562 		connp->conn_outgoing_ill = NULL;
1563 	if (connp->conn_dhcpinit_ill == ill) {
1564 		connp->conn_dhcpinit_ill = NULL;
1565 		ASSERT(ill->ill_dhcpinit != 0);
1566 		atomic_dec_32(&ill->ill_dhcpinit);
1567 	}
1568 	if (connp->conn_ire_cache != NULL) {
1569 		ire = connp->conn_ire_cache;
1570 		/*
1571 		 * Source address selection makes it possible for IRE_CACHE
1572 		 * entries to be created with ire_stq coming from interface X
1573 		 * and ipif coming from interface Y.  Thus whenever interface
1574 		 * X goes down, remove all references to it by checking both
1575 		 * on ire_ipif and ire_stq.
1576 		 */
1577 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1578 		    (ire->ire_type == IRE_CACHE &&
1579 		    ire->ire_stq == ill->ill_wq)) {
1580 			connp->conn_ire_cache = NULL;
1581 			mutex_exit(&connp->conn_lock);
1582 			ire_refrele_notr(ire);
1583 			return;
1584 		}
1585 	}
1586 	mutex_exit(&connp->conn_lock);
1587 }
1588 
1589 /* ARGSUSED */
1590 void
1591 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1592 {
1593 	ill_t	*ill = q->q_ptr;
1594 	ipif_t	*ipif;
1595 
1596 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1597 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1598 		ipif_non_duplicate(ipif);
1599 		ipif_down_tail(ipif);
1600 	}
1601 	freemsg(mp);
1602 	ipsq_current_finish(ipsq);
1603 }
1604 
1605 /*
1606  * ill_down_start is called when we want to down this ill and bring it up again
1607  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1608  * all interfaces, but don't tear down any plumbing.
1609  */
1610 boolean_t
1611 ill_down_start(queue_t *q, mblk_t *mp)
1612 {
1613 	ill_t	*ill = q->q_ptr;
1614 	ipif_t	*ipif;
1615 
1616 	ASSERT(IAM_WRITER_ILL(ill));
1617 
1618 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1619 		(void) ipif_down(ipif, NULL, NULL);
1620 
1621 	ill_down(ill);
1622 
1623 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1624 
1625 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1626 
1627 	/*
1628 	 * Atomically test and add the pending mp if references are active.
1629 	 */
1630 	mutex_enter(&ill->ill_lock);
1631 	if (!ill_is_quiescent(ill)) {
1632 		/* call cannot fail since `conn_t *' argument is NULL */
1633 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1634 		    mp, ILL_DOWN);
1635 		mutex_exit(&ill->ill_lock);
1636 		return (B_FALSE);
1637 	}
1638 	mutex_exit(&ill->ill_lock);
1639 	return (B_TRUE);
1640 }
1641 
1642 static void
1643 ill_down(ill_t *ill)
1644 {
1645 	ip_stack_t	*ipst = ill->ill_ipst;
1646 
1647 	/* Blow off any IREs dependent on this ILL. */
1648 	ire_walk(ill_downi, ill, ipst);
1649 
1650 	/* Remove any conn_*_ill depending on this ill */
1651 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1652 }
1653 
1654 /*
1655  * ire_walk routine used to delete every IRE that depends on queues
1656  * associated with 'ill'.  (Always called as writer.)
1657  */
1658 static void
1659 ill_downi(ire_t *ire, char *ill_arg)
1660 {
1661 	ill_t	*ill = (ill_t *)ill_arg;
1662 
1663 	/*
1664 	 * Source address selection makes it possible for IRE_CACHE
1665 	 * entries to be created with ire_stq coming from interface X
1666 	 * and ipif coming from interface Y.  Thus whenever interface
1667 	 * X goes down, remove all references to it by checking both
1668 	 * on ire_ipif and ire_stq.
1669 	 */
1670 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1671 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1672 		ire_delete(ire);
1673 	}
1674 }
1675 
1676 /*
1677  * Remove ire/nce from the fastpath list.
1678  */
1679 void
1680 ill_fastpath_nack(ill_t *ill)
1681 {
1682 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1683 }
1684 
1685 /* Consume an M_IOCACK of the fastpath probe. */
1686 void
1687 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1688 {
1689 	mblk_t	*mp1 = mp;
1690 
1691 	/*
1692 	 * If this was the first attempt turn on the fastpath probing.
1693 	 */
1694 	mutex_enter(&ill->ill_lock);
1695 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1696 		ill->ill_dlpi_fastpath_state = IDS_OK;
1697 	mutex_exit(&ill->ill_lock);
1698 
1699 	/* Free the M_IOCACK mblk, hold on to the data */
1700 	mp = mp->b_cont;
1701 	freeb(mp1);
1702 	if (mp == NULL)
1703 		return;
1704 	if (mp->b_cont != NULL) {
1705 		/*
1706 		 * Update all IRE's or NCE's that are waiting for
1707 		 * fastpath update.
1708 		 */
1709 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1710 		mp1 = mp->b_cont;
1711 		freeb(mp);
1712 		mp = mp1;
1713 	} else {
1714 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1715 	}
1716 
1717 	freeb(mp);
1718 }
1719 
1720 /*
1721  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1722  * The data portion of the request is a dl_unitdata_req_t template for
1723  * what we would send downstream in the absence of a fastpath confirmation.
1724  */
1725 int
1726 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1727 {
1728 	struct iocblk	*ioc;
1729 	mblk_t	*mp;
1730 
1731 	if (dlur_mp == NULL)
1732 		return (EINVAL);
1733 
1734 	mutex_enter(&ill->ill_lock);
1735 	switch (ill->ill_dlpi_fastpath_state) {
1736 	case IDS_FAILED:
1737 		/*
1738 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1739 		 * support it.
1740 		 */
1741 		mutex_exit(&ill->ill_lock);
1742 		return (ENOTSUP);
1743 	case IDS_UNKNOWN:
1744 		/* This is the first probe */
1745 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1746 		break;
1747 	default:
1748 		break;
1749 	}
1750 	mutex_exit(&ill->ill_lock);
1751 
1752 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1753 		return (EAGAIN);
1754 
1755 	mp->b_cont = copyb(dlur_mp);
1756 	if (mp->b_cont == NULL) {
1757 		freeb(mp);
1758 		return (EAGAIN);
1759 	}
1760 
1761 	ioc = (struct iocblk *)mp->b_rptr;
1762 	ioc->ioc_count = msgdsize(mp->b_cont);
1763 
1764 	putnext(ill->ill_wq, mp);
1765 	return (0);
1766 }
1767 
1768 void
1769 ill_capability_probe(ill_t *ill)
1770 {
1771 	mblk_t	*mp;
1772 
1773 	ASSERT(IAM_WRITER_ILL(ill));
1774 
1775 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1776 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1777 		return;
1778 
1779 	/*
1780 	 * We are starting a new cycle of capability negotiation.
1781 	 * Free up the capab reset messages of any previous incarnation.
1782 	 * We will do a fresh allocation when we get the response to our probe
1783 	 */
1784 	if (ill->ill_capab_reset_mp != NULL) {
1785 		freemsg(ill->ill_capab_reset_mp);
1786 		ill->ill_capab_reset_mp = NULL;
1787 	}
1788 
1789 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1790 
1791 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1792 	if (mp == NULL)
1793 		return;
1794 
1795 	ill_capability_send(ill, mp);
1796 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1797 }
1798 
1799 void
1800 ill_capability_reset(ill_t *ill, boolean_t reneg)
1801 {
1802 	ASSERT(IAM_WRITER_ILL(ill));
1803 
1804 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1805 		return;
1806 
1807 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1808 
1809 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1810 	ill->ill_capab_reset_mp = NULL;
1811 	/*
1812 	 * We turn off all capabilities except those pertaining to
1813 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1814 	 * which will be turned off by the corresponding reset functions.
1815 	 */
1816 	ill->ill_capabilities &= ~(ILL_CAPAB_MDT | ILL_CAPAB_HCKSUM  |
1817 	    ILL_CAPAB_ZEROCOPY | ILL_CAPAB_AH | ILL_CAPAB_ESP);
1818 }
1819 
1820 static void
1821 ill_capability_reset_alloc(ill_t *ill)
1822 {
1823 	mblk_t *mp;
1824 	size_t	size = 0;
1825 	int	err;
1826 	dl_capability_req_t	*capb;
1827 
1828 	ASSERT(IAM_WRITER_ILL(ill));
1829 	ASSERT(ill->ill_capab_reset_mp == NULL);
1830 
1831 	if (ILL_MDT_CAPABLE(ill))
1832 		size += sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1833 
1834 	if (ILL_HCKSUM_CAPABLE(ill)) {
1835 		size += sizeof (dl_capability_sub_t) +
1836 		    sizeof (dl_capab_hcksum_t);
1837 	}
1838 
1839 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1840 		size += sizeof (dl_capability_sub_t) +
1841 		    sizeof (dl_capab_zerocopy_t);
1842 	}
1843 
1844 	if (ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) {
1845 		size += sizeof (dl_capability_sub_t);
1846 		size += ill_capability_ipsec_reset_size(ill, NULL, NULL,
1847 		    NULL, NULL);
1848 	}
1849 
1850 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1851 		size += sizeof (dl_capability_sub_t) +
1852 		    sizeof (dl_capab_dld_t);
1853 	}
1854 
1855 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1856 	    STR_NOSIG, &err);
1857 
1858 	mp->b_datap->db_type = M_PROTO;
1859 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1860 
1861 	capb = (dl_capability_req_t *)mp->b_rptr;
1862 	capb->dl_primitive = DL_CAPABILITY_REQ;
1863 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1864 	capb->dl_sub_length = size;
1865 
1866 	mp->b_wptr += sizeof (dl_capability_req_t);
1867 
1868 	/*
1869 	 * Each handler fills in the corresponding dl_capability_sub_t
1870 	 * inside the mblk,
1871 	 */
1872 	ill_capability_mdt_reset_fill(ill, mp);
1873 	ill_capability_hcksum_reset_fill(ill, mp);
1874 	ill_capability_zerocopy_reset_fill(ill, mp);
1875 	ill_capability_ipsec_reset_fill(ill, mp);
1876 	ill_capability_dld_reset_fill(ill, mp);
1877 
1878 	ill->ill_capab_reset_mp = mp;
1879 }
1880 
1881 static void
1882 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1883 {
1884 	dl_capab_id_t *id_ic;
1885 	uint_t sub_dl_cap = outers->dl_cap;
1886 	dl_capability_sub_t *inners;
1887 	uint8_t *capend;
1888 
1889 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1890 
1891 	/*
1892 	 * Note: range checks here are not absolutely sufficient to
1893 	 * make us robust against malformed messages sent by drivers;
1894 	 * this is in keeping with the rest of IP's dlpi handling.
1895 	 * (Remember, it's coming from something else in the kernel
1896 	 * address space)
1897 	 */
1898 
1899 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1900 	if (capend > mp->b_wptr) {
1901 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1902 		    "malformed sub-capability too long for mblk");
1903 		return;
1904 	}
1905 
1906 	id_ic = (dl_capab_id_t *)(outers + 1);
1907 
1908 	if (outers->dl_length < sizeof (*id_ic) ||
1909 	    (inners = &id_ic->id_subcap,
1910 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1911 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1912 		    "encapsulated capab type %d too long for mblk",
1913 		    inners->dl_cap);
1914 		return;
1915 	}
1916 
1917 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1918 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1919 		    "isn't as expected; pass-thru module(s) detected, "
1920 		    "discarding capability\n", inners->dl_cap));
1921 		return;
1922 	}
1923 
1924 	/* Process the encapsulated sub-capability */
1925 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1926 }
1927 
1928 /*
1929  * Process Multidata Transmit capability negotiation ack received from a
1930  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1931  * DL_CAPABILITY_ACK message.
1932  */
1933 static void
1934 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1935 {
1936 	mblk_t *nmp = NULL;
1937 	dl_capability_req_t *oc;
1938 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1939 	ill_mdt_capab_t **ill_mdt_capab;
1940 	uint_t sub_dl_cap = isub->dl_cap;
1941 	uint8_t *capend;
1942 
1943 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1944 
1945 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1946 
1947 	/*
1948 	 * Note: range checks here are not absolutely sufficient to
1949 	 * make us robust against malformed messages sent by drivers;
1950 	 * this is in keeping with the rest of IP's dlpi handling.
1951 	 * (Remember, it's coming from something else in the kernel
1952 	 * address space)
1953 	 */
1954 
1955 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1956 	if (capend > mp->b_wptr) {
1957 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1958 		    "malformed sub-capability too long for mblk");
1959 		return;
1960 	}
1961 
1962 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1963 
1964 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1965 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1966 		    "unsupported MDT sub-capability (version %d, expected %d)",
1967 		    mdt_ic->mdt_version, MDT_VERSION_2);
1968 		return;
1969 	}
1970 
1971 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1972 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1973 		    "capability isn't as expected; pass-thru module(s) "
1974 		    "detected, discarding capability\n"));
1975 		return;
1976 	}
1977 
1978 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1979 
1980 		if (*ill_mdt_capab == NULL) {
1981 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1982 			    KM_NOSLEEP);
1983 			if (*ill_mdt_capab == NULL) {
1984 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1985 				    "could not enable MDT version %d "
1986 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1987 				    ill->ill_name);
1988 				return;
1989 			}
1990 		}
1991 
1992 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1993 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1994 		    "header spaces, %d max pld bufs, %d span limit)\n",
1995 		    ill->ill_name, MDT_VERSION_2,
1996 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1997 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1998 
1999 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2000 		(*ill_mdt_capab)->ill_mdt_on = 1;
2001 		/*
2002 		 * Round the following values to the nearest 32-bit; ULP
2003 		 * may further adjust them to accomodate for additional
2004 		 * protocol headers.  We pass these values to ULP during
2005 		 * bind time.
2006 		 */
2007 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2008 		    roundup(mdt_ic->mdt_hdr_head, 4);
2009 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2010 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2011 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2012 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2013 
2014 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2015 	} else {
2016 		uint_t size;
2017 		uchar_t *rptr;
2018 
2019 		size = sizeof (dl_capability_req_t) +
2020 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2021 
2022 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2023 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2024 			    "could not enable MDT for %s (ENOMEM)\n",
2025 			    ill->ill_name);
2026 			return;
2027 		}
2028 
2029 		rptr = nmp->b_rptr;
2030 		/* initialize dl_capability_req_t */
2031 		oc = (dl_capability_req_t *)nmp->b_rptr;
2032 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2033 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2034 		    sizeof (dl_capab_mdt_t);
2035 		nmp->b_rptr += sizeof (dl_capability_req_t);
2036 
2037 		/* initialize dl_capability_sub_t */
2038 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2039 		nmp->b_rptr += sizeof (*isub);
2040 
2041 		/* initialize dl_capab_mdt_t */
2042 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2043 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2044 
2045 		nmp->b_rptr = rptr;
2046 
2047 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2048 		    "to enable MDT version %d\n", ill->ill_name,
2049 		    MDT_VERSION_2));
2050 
2051 		/* set ENABLE flag */
2052 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2053 
2054 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2055 		ill_capability_send(ill, nmp);
2056 	}
2057 }
2058 
2059 static void
2060 ill_capability_mdt_reset_fill(ill_t *ill, mblk_t *mp)
2061 {
2062 	dl_capab_mdt_t *mdt_subcap;
2063 	dl_capability_sub_t *dl_subcap;
2064 
2065 	if (!ILL_MDT_CAPABLE(ill))
2066 		return;
2067 
2068 	ASSERT(ill->ill_mdt_capab != NULL);
2069 
2070 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2071 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2072 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2073 
2074 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2075 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2076 	mdt_subcap->mdt_flags = 0;
2077 	mdt_subcap->mdt_hdr_head = 0;
2078 	mdt_subcap->mdt_hdr_tail = 0;
2079 
2080 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2081 }
2082 
2083 static void
2084 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
2085 {
2086 	dl_capability_sub_t *dl_subcap;
2087 
2088 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2089 		return;
2090 
2091 	/*
2092 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
2093 	 * initialized below since it is not used by DLD.
2094 	 */
2095 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2096 	dl_subcap->dl_cap = DL_CAPAB_DLD;
2097 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
2098 
2099 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
2100 }
2101 
2102 /*
2103  * Send a DL_NOTIFY_REQ to the specified ill to enable
2104  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2105  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2106  * acceleration.
2107  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2108  */
2109 static boolean_t
2110 ill_enable_promisc_notify(ill_t *ill)
2111 {
2112 	mblk_t *mp;
2113 	dl_notify_req_t *req;
2114 
2115 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2116 
2117 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2118 	if (mp == NULL)
2119 		return (B_FALSE);
2120 
2121 	req = (dl_notify_req_t *)mp->b_rptr;
2122 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2123 	    DL_NOTE_PROMISC_OFF_PHYS;
2124 
2125 	ill_dlpi_send(ill, mp);
2126 
2127 	return (B_TRUE);
2128 }
2129 
2130 /*
2131  * Allocate an IPsec capability request which will be filled by our
2132  * caller to turn on support for one or more algorithms.
2133  */
2134 static mblk_t *
2135 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2136 {
2137 	mblk_t *nmp;
2138 	dl_capability_req_t	*ocap;
2139 	dl_capab_ipsec_t	*ocip;
2140 	dl_capab_ipsec_t	*icip;
2141 	uint8_t			*ptr;
2142 	icip = (dl_capab_ipsec_t *)(isub + 1);
2143 
2144 	/*
2145 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2146 	 * PROMISC_ON/OFF notification from the provider. We need to
2147 	 * do this before enabling the algorithms to avoid leakage of
2148 	 * cleartext packets.
2149 	 */
2150 
2151 	if (!ill_enable_promisc_notify(ill))
2152 		return (NULL);
2153 
2154 	/*
2155 	 * Allocate new mblk which will contain a new capability
2156 	 * request to enable the capabilities.
2157 	 */
2158 
2159 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2160 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2161 	if (nmp == NULL)
2162 		return (NULL);
2163 
2164 	ptr = nmp->b_rptr;
2165 
2166 	/* initialize dl_capability_req_t */
2167 	ocap = (dl_capability_req_t *)ptr;
2168 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2169 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2170 	ptr += sizeof (dl_capability_req_t);
2171 
2172 	/* initialize dl_capability_sub_t */
2173 	bcopy(isub, ptr, sizeof (*isub));
2174 	ptr += sizeof (*isub);
2175 
2176 	/* initialize dl_capab_ipsec_t */
2177 	ocip = (dl_capab_ipsec_t *)ptr;
2178 	bcopy(icip, ocip, sizeof (*icip));
2179 
2180 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2181 	return (nmp);
2182 }
2183 
2184 /*
2185  * Process an IPsec capability negotiation ack received from a DLS Provider.
2186  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2187  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2188  */
2189 static void
2190 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2191 {
2192 	dl_capab_ipsec_t	*icip;
2193 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2194 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2195 	uint_t cipher, nciphers;
2196 	mblk_t *nmp;
2197 	uint_t alg_len;
2198 	boolean_t need_sadb_dump;
2199 	uint_t sub_dl_cap = isub->dl_cap;
2200 	ill_ipsec_capab_t **ill_capab;
2201 	uint64_t ill_capab_flag;
2202 	uint8_t *capend, *ciphend;
2203 	boolean_t sadb_resync;
2204 
2205 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2206 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2207 
2208 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2209 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2210 		ill_capab_flag = ILL_CAPAB_AH;
2211 	} else {
2212 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2213 		ill_capab_flag = ILL_CAPAB_ESP;
2214 	}
2215 
2216 	/*
2217 	 * If the ill capability structure exists, then this incoming
2218 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2219 	 * If this is so, then we'd need to resynchronize the SADB
2220 	 * after re-enabling the offloaded ciphers.
2221 	 */
2222 	sadb_resync = (*ill_capab != NULL);
2223 
2224 	/*
2225 	 * Note: range checks here are not absolutely sufficient to
2226 	 * make us robust against malformed messages sent by drivers;
2227 	 * this is in keeping with the rest of IP's dlpi handling.
2228 	 * (Remember, it's coming from something else in the kernel
2229 	 * address space)
2230 	 */
2231 
2232 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2233 	if (capend > mp->b_wptr) {
2234 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2235 		    "malformed sub-capability too long for mblk");
2236 		return;
2237 	}
2238 
2239 	/*
2240 	 * There are two types of acks we process here:
2241 	 * 1. acks in reply to a (first form) generic capability req
2242 	 *    (no ENABLE flag set)
2243 	 * 2. acks in reply to a ENABLE capability req.
2244 	 *    (ENABLE flag set)
2245 	 *
2246 	 * We process the subcapability passed as argument as follows:
2247 	 * 1 do initializations
2248 	 *   1.1 initialize nmp = NULL
2249 	 *   1.2 set need_sadb_dump to B_FALSE
2250 	 * 2 for each cipher in subcapability:
2251 	 *   2.1 if ENABLE flag is set:
2252 	 *	2.1.1 update per-ill ipsec capabilities info
2253 	 *	2.1.2 set need_sadb_dump to B_TRUE
2254 	 *   2.2 if ENABLE flag is not set:
2255 	 *	2.2.1 if nmp is NULL:
2256 	 *		2.2.1.1 allocate and initialize nmp
2257 	 *		2.2.1.2 init current pos in nmp
2258 	 *	2.2.2 copy current cipher to current pos in nmp
2259 	 *	2.2.3 set ENABLE flag in nmp
2260 	 *	2.2.4 update current pos
2261 	 * 3 if nmp is not equal to NULL, send enable request
2262 	 *   3.1 send capability request
2263 	 * 4 if need_sadb_dump is B_TRUE
2264 	 *   4.1 enable promiscuous on/off notifications
2265 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2266 	 *	AH or ESP SA's to interface.
2267 	 */
2268 
2269 	nmp = NULL;
2270 	oalg = NULL;
2271 	need_sadb_dump = B_FALSE;
2272 	icip = (dl_capab_ipsec_t *)(isub + 1);
2273 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2274 
2275 	nciphers = icip->cip_nciphers;
2276 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2277 
2278 	if (ciphend > capend) {
2279 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2280 		    "too many ciphers for sub-capability len");
2281 		return;
2282 	}
2283 
2284 	for (cipher = 0; cipher < nciphers; cipher++) {
2285 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2286 
2287 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2288 			/*
2289 			 * TBD: when we provide a way to disable capabilities
2290 			 * from above, need to manage the request-pending state
2291 			 * and fail if we were not expecting this ACK.
2292 			 */
2293 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2294 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2295 
2296 			/*
2297 			 * Update IPsec capabilities for this ill
2298 			 */
2299 
2300 			if (*ill_capab == NULL) {
2301 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2302 				    ("ill_capability_ipsec_ack: "
2303 				    "allocating ipsec_capab for ill\n"));
2304 				*ill_capab = ill_ipsec_capab_alloc();
2305 
2306 				if (*ill_capab == NULL) {
2307 					cmn_err(CE_WARN,
2308 					    "ill_capability_ipsec_ack: "
2309 					    "could not enable IPsec Hardware "
2310 					    "acceleration for %s (ENOMEM)\n",
2311 					    ill->ill_name);
2312 					return;
2313 				}
2314 			}
2315 
2316 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2317 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2318 
2319 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2320 				cmn_err(CE_WARN,
2321 				    "ill_capability_ipsec_ack: "
2322 				    "malformed IPsec algorithm id %d",
2323 				    ialg->alg_prim);
2324 				continue;
2325 			}
2326 
2327 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2328 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2329 				    ialg->alg_prim);
2330 			} else {
2331 				ipsec_capab_algparm_t *alp;
2332 
2333 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2334 				    ialg->alg_prim);
2335 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2336 				    ialg->alg_prim)) {
2337 					cmn_err(CE_WARN,
2338 					    "ill_capability_ipsec_ack: "
2339 					    "no space for IPsec alg id %d",
2340 					    ialg->alg_prim);
2341 					continue;
2342 				}
2343 				alp = &((*ill_capab)->encr_algparm[
2344 				    ialg->alg_prim]);
2345 				alp->minkeylen = ialg->alg_minbits;
2346 				alp->maxkeylen = ialg->alg_maxbits;
2347 			}
2348 			ill->ill_capabilities |= ill_capab_flag;
2349 			/*
2350 			 * indicate that a capability was enabled, which
2351 			 * will be used below to kick off a SADB dump
2352 			 * to the ill.
2353 			 */
2354 			need_sadb_dump = B_TRUE;
2355 		} else {
2356 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2357 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2358 			    ialg->alg_prim));
2359 
2360 			if (nmp == NULL) {
2361 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2362 				if (nmp == NULL) {
2363 					/*
2364 					 * Sending the PROMISC_ON/OFF
2365 					 * notification request failed.
2366 					 * We cannot enable the algorithms
2367 					 * since the Provider will not
2368 					 * notify IP of promiscous mode
2369 					 * changes, which could lead
2370 					 * to leakage of packets.
2371 					 */
2372 					cmn_err(CE_WARN,
2373 					    "ill_capability_ipsec_ack: "
2374 					    "could not enable IPsec Hardware "
2375 					    "acceleration for %s (ENOMEM)\n",
2376 					    ill->ill_name);
2377 					return;
2378 				}
2379 				/* ptr to current output alg specifier */
2380 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2381 			}
2382 
2383 			/*
2384 			 * Copy current alg specifier, set ENABLE
2385 			 * flag, and advance to next output alg.
2386 			 * For now we enable all IPsec capabilities.
2387 			 */
2388 			ASSERT(oalg != NULL);
2389 			bcopy(ialg, oalg, alg_len);
2390 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2391 			nmp->b_wptr += alg_len;
2392 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2393 		}
2394 
2395 		/* move to next input algorithm specifier */
2396 		ialg = (dl_capab_ipsec_alg_t *)
2397 		    ((char *)ialg + alg_len);
2398 	}
2399 
2400 	if (nmp != NULL)
2401 		/*
2402 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2403 		 * IPsec hardware acceleration.
2404 		 */
2405 		ill_capability_send(ill, nmp);
2406 
2407 	if (need_sadb_dump)
2408 		/*
2409 		 * An acknowledgement corresponding to a request to
2410 		 * enable acceleration was received, notify SADB.
2411 		 */
2412 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2413 }
2414 
2415 /*
2416  * Given an mblk with enough space in it, create sub-capability entries for
2417  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2418  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2419  * in preparation for the reset the DL_CAPABILITY_REQ message.
2420  */
2421 static void
2422 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2423     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2424 {
2425 	dl_capab_ipsec_t *oipsec;
2426 	dl_capab_ipsec_alg_t *oalg;
2427 	dl_capability_sub_t *dl_subcap;
2428 	int i, k;
2429 
2430 	ASSERT(nciphers > 0);
2431 	ASSERT(ill_cap != NULL);
2432 	ASSERT(mp != NULL);
2433 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2434 
2435 	/* dl_capability_sub_t for "stype" */
2436 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2437 	dl_subcap->dl_cap = stype;
2438 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2439 	mp->b_wptr += sizeof (dl_capability_sub_t);
2440 
2441 	/* dl_capab_ipsec_t for "stype" */
2442 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2443 	oipsec->cip_version = 1;
2444 	oipsec->cip_nciphers = nciphers;
2445 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2446 
2447 	/* create entries for "stype" AUTH ciphers */
2448 	for (i = 0; i < ill_cap->algs_size; i++) {
2449 		for (k = 0; k < BITSPERBYTE; k++) {
2450 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2451 				continue;
2452 
2453 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2454 			bzero((void *)oalg, sizeof (*oalg));
2455 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2456 			oalg->alg_prim = k + (BITSPERBYTE * i);
2457 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2458 		}
2459 	}
2460 	/* create entries for "stype" ENCR ciphers */
2461 	for (i = 0; i < ill_cap->algs_size; i++) {
2462 		for (k = 0; k < BITSPERBYTE; k++) {
2463 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2464 				continue;
2465 
2466 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2467 			bzero((void *)oalg, sizeof (*oalg));
2468 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2469 			oalg->alg_prim = k + (BITSPERBYTE * i);
2470 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2471 		}
2472 	}
2473 }
2474 
2475 /*
2476  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2477  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2478  * POPC instruction, but our macro is more flexible for an arbitrary length
2479  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2480  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2481  * stays that way, we can reduce the number of iterations required.
2482  */
2483 #define	COUNT_1S(val, sum) {					\
2484 	uint8_t x = val & 0xff;					\
2485 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2486 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2487 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2488 }
2489 
2490 /* ARGSUSED */
2491 static int
2492 ill_capability_ipsec_reset_size(ill_t *ill, int *ah_cntp, int *ah_lenp,
2493     int *esp_cntp, int *esp_lenp)
2494 {
2495 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2496 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2497 	uint64_t ill_capabilities = ill->ill_capabilities;
2498 	int ah_cnt = 0, esp_cnt = 0;
2499 	int ah_len = 0, esp_len = 0;
2500 	int i, size = 0;
2501 
2502 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2503 		return (0);
2504 
2505 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2506 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2507 
2508 	/* Find out the number of ciphers for AH */
2509 	if (cap_ah != NULL) {
2510 		for (i = 0; i < cap_ah->algs_size; i++) {
2511 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2512 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2513 		}
2514 		if (ah_cnt > 0) {
2515 			size += sizeof (dl_capability_sub_t) +
2516 			    sizeof (dl_capab_ipsec_t);
2517 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2518 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2519 			size += ah_len;
2520 		}
2521 	}
2522 
2523 	/* Find out the number of ciphers for ESP */
2524 	if (cap_esp != NULL) {
2525 		for (i = 0; i < cap_esp->algs_size; i++) {
2526 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2527 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2528 		}
2529 		if (esp_cnt > 0) {
2530 			size += sizeof (dl_capability_sub_t) +
2531 			    sizeof (dl_capab_ipsec_t);
2532 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2533 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2534 			size += esp_len;
2535 		}
2536 	}
2537 
2538 	if (ah_cntp != NULL)
2539 		*ah_cntp = ah_cnt;
2540 	if (ah_lenp != NULL)
2541 		*ah_lenp = ah_len;
2542 	if (esp_cntp != NULL)
2543 		*esp_cntp = esp_cnt;
2544 	if (esp_lenp != NULL)
2545 		*esp_lenp = esp_len;
2546 
2547 	return (size);
2548 }
2549 
2550 /* ARGSUSED */
2551 static void
2552 ill_capability_ipsec_reset_fill(ill_t *ill, mblk_t *mp)
2553 {
2554 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2555 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2556 	int ah_cnt = 0, esp_cnt = 0;
2557 	int ah_len = 0, esp_len = 0;
2558 	int size;
2559 
2560 	size = ill_capability_ipsec_reset_size(ill, &ah_cnt, &ah_len,
2561 	    &esp_cnt, &esp_len);
2562 	if (size == 0)
2563 		return;
2564 
2565 	/*
2566 	 * Clear the capability flags for IPsec HA but retain the ill
2567 	 * capability structures since it's possible that another thread
2568 	 * is still referring to them.  The structures only get deallocated
2569 	 * when we destroy the ill.
2570 	 *
2571 	 * Various places check the flags to see if the ill is capable of
2572 	 * hardware acceleration, and by clearing them we ensure that new
2573 	 * outbound IPsec packets are sent down encrypted.
2574 	 */
2575 
2576 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2577 	if (ah_cnt > 0) {
2578 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2579 		    cap_ah, mp);
2580 	}
2581 
2582 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2583 	if (esp_cnt > 0) {
2584 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2585 		    cap_esp, mp);
2586 	}
2587 
2588 	/*
2589 	 * At this point we've composed a bunch of sub-capabilities to be
2590 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2591 	 * by the caller.  Upon receiving this reset message, the driver
2592 	 * must stop inbound decryption (by destroying all inbound SAs)
2593 	 * and let the corresponding packets come in encrypted.
2594 	 */
2595 }
2596 
2597 static void
2598 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2599     boolean_t encapsulated)
2600 {
2601 	boolean_t legacy = B_FALSE;
2602 
2603 	/*
2604 	 * Note that only the following two sub-capabilities may be
2605 	 * considered as "legacy", since their original definitions
2606 	 * do not incorporate the dl_mid_t module ID token, and hence
2607 	 * may require the use of the wrapper sub-capability.
2608 	 */
2609 	switch (subp->dl_cap) {
2610 	case DL_CAPAB_IPSEC_AH:
2611 	case DL_CAPAB_IPSEC_ESP:
2612 		legacy = B_TRUE;
2613 		break;
2614 	}
2615 
2616 	/*
2617 	 * For legacy sub-capabilities which don't incorporate a queue_t
2618 	 * pointer in their structures, discard them if we detect that
2619 	 * there are intermediate modules in between IP and the driver.
2620 	 */
2621 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2622 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2623 		    "%d discarded; %d module(s) present below IP\n",
2624 		    subp->dl_cap, ill->ill_lmod_cnt));
2625 		return;
2626 	}
2627 
2628 	switch (subp->dl_cap) {
2629 	case DL_CAPAB_IPSEC_AH:
2630 	case DL_CAPAB_IPSEC_ESP:
2631 		ill_capability_ipsec_ack(ill, mp, subp);
2632 		break;
2633 	case DL_CAPAB_MDT:
2634 		ill_capability_mdt_ack(ill, mp, subp);
2635 		break;
2636 	case DL_CAPAB_HCKSUM:
2637 		ill_capability_hcksum_ack(ill, mp, subp);
2638 		break;
2639 	case DL_CAPAB_ZEROCOPY:
2640 		ill_capability_zerocopy_ack(ill, mp, subp);
2641 		break;
2642 	case DL_CAPAB_DLD:
2643 		ill_capability_dld_ack(ill, mp, subp);
2644 		break;
2645 	default:
2646 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2647 		    subp->dl_cap));
2648 	}
2649 }
2650 
2651 /*
2652  * Process a hardware checksum offload capability negotiation ack received
2653  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2654  * of a DL_CAPABILITY_ACK message.
2655  */
2656 static void
2657 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2658 {
2659 	dl_capability_req_t	*ocap;
2660 	dl_capab_hcksum_t	*ihck, *ohck;
2661 	ill_hcksum_capab_t	**ill_hcksum;
2662 	mblk_t			*nmp = NULL;
2663 	uint_t			sub_dl_cap = isub->dl_cap;
2664 	uint8_t			*capend;
2665 
2666 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
2667 
2668 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
2669 
2670 	/*
2671 	 * Note: range checks here are not absolutely sufficient to
2672 	 * make us robust against malformed messages sent by drivers;
2673 	 * this is in keeping with the rest of IP's dlpi handling.
2674 	 * (Remember, it's coming from something else in the kernel
2675 	 * address space)
2676 	 */
2677 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2678 	if (capend > mp->b_wptr) {
2679 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2680 		    "malformed sub-capability too long for mblk");
2681 		return;
2682 	}
2683 
2684 	/*
2685 	 * There are two types of acks we process here:
2686 	 * 1. acks in reply to a (first form) generic capability req
2687 	 *    (no ENABLE flag set)
2688 	 * 2. acks in reply to a ENABLE capability req.
2689 	 *    (ENABLE flag set)
2690 	 */
2691 	ihck = (dl_capab_hcksum_t *)(isub + 1);
2692 
2693 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
2694 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
2695 		    "unsupported hardware checksum "
2696 		    "sub-capability (version %d, expected %d)",
2697 		    ihck->hcksum_version, HCKSUM_VERSION_1);
2698 		return;
2699 	}
2700 
2701 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
2702 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
2703 		    "checksum capability isn't as expected; pass-thru "
2704 		    "module(s) detected, discarding capability\n"));
2705 		return;
2706 	}
2707 
2708 #define	CURR_HCKSUM_CAPAB				\
2709 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
2710 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
2711 
2712 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
2713 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
2714 		/* do ENABLE processing */
2715 		if (*ill_hcksum == NULL) {
2716 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
2717 			    KM_NOSLEEP);
2718 
2719 			if (*ill_hcksum == NULL) {
2720 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2721 				    "could not enable hcksum version %d "
2722 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
2723 				    ill->ill_name);
2724 				return;
2725 			}
2726 		}
2727 
2728 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
2729 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
2730 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
2731 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
2732 		    "has enabled hardware checksumming\n ",
2733 		    ill->ill_name));
2734 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
2735 		/*
2736 		 * Enabling hardware checksum offload
2737 		 * Currently IP supports {TCP,UDP}/IPv4
2738 		 * partial and full cksum offload and
2739 		 * IPv4 header checksum offload.
2740 		 * Allocate new mblk which will
2741 		 * contain a new capability request
2742 		 * to enable hardware checksum offload.
2743 		 */
2744 		uint_t	size;
2745 		uchar_t	*rptr;
2746 
2747 		size = sizeof (dl_capability_req_t) +
2748 		    sizeof (dl_capability_sub_t) + isub->dl_length;
2749 
2750 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2751 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2752 			    "could not enable hardware cksum for %s (ENOMEM)\n",
2753 			    ill->ill_name);
2754 			return;
2755 		}
2756 
2757 		rptr = nmp->b_rptr;
2758 		/* initialize dl_capability_req_t */
2759 		ocap = (dl_capability_req_t *)nmp->b_rptr;
2760 		ocap->dl_sub_offset =
2761 		    sizeof (dl_capability_req_t);
2762 		ocap->dl_sub_length =
2763 		    sizeof (dl_capability_sub_t) +
2764 		    isub->dl_length;
2765 		nmp->b_rptr += sizeof (dl_capability_req_t);
2766 
2767 		/* initialize dl_capability_sub_t */
2768 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2769 		nmp->b_rptr += sizeof (*isub);
2770 
2771 		/* initialize dl_capab_hcksum_t */
2772 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
2773 		bcopy(ihck, ohck, sizeof (*ihck));
2774 
2775 		nmp->b_rptr = rptr;
2776 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2777 
2778 		/* Set ENABLE flag */
2779 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
2780 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
2781 
2782 		/*
2783 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2784 		 * hardware checksum acceleration.
2785 		 */
2786 		ill_capability_send(ill, nmp);
2787 	} else {
2788 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
2789 		    "advertised %x hardware checksum capability flags\n",
2790 		    ill->ill_name, ihck->hcksum_txflags));
2791 	}
2792 }
2793 
2794 static void
2795 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
2796 {
2797 	dl_capab_hcksum_t *hck_subcap;
2798 	dl_capability_sub_t *dl_subcap;
2799 
2800 	if (!ILL_HCKSUM_CAPABLE(ill))
2801 		return;
2802 
2803 	ASSERT(ill->ill_hcksum_capab != NULL);
2804 
2805 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2806 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
2807 	dl_subcap->dl_length = sizeof (*hck_subcap);
2808 
2809 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
2810 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
2811 	hck_subcap->hcksum_txflags = 0;
2812 
2813 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
2814 }
2815 
2816 static void
2817 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2818 {
2819 	mblk_t *nmp = NULL;
2820 	dl_capability_req_t *oc;
2821 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
2822 	ill_zerocopy_capab_t **ill_zerocopy_capab;
2823 	uint_t sub_dl_cap = isub->dl_cap;
2824 	uint8_t *capend;
2825 
2826 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
2827 
2828 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
2829 
2830 	/*
2831 	 * Note: range checks here are not absolutely sufficient to
2832 	 * make us robust against malformed messages sent by drivers;
2833 	 * this is in keeping with the rest of IP's dlpi handling.
2834 	 * (Remember, it's coming from something else in the kernel
2835 	 * address space)
2836 	 */
2837 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2838 	if (capend > mp->b_wptr) {
2839 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2840 		    "malformed sub-capability too long for mblk");
2841 		return;
2842 	}
2843 
2844 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
2845 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
2846 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
2847 		    "unsupported ZEROCOPY sub-capability (version %d, "
2848 		    "expected %d)", zc_ic->zerocopy_version,
2849 		    ZEROCOPY_VERSION_1);
2850 		return;
2851 	}
2852 
2853 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
2854 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
2855 		    "capability isn't as expected; pass-thru module(s) "
2856 		    "detected, discarding capability\n"));
2857 		return;
2858 	}
2859 
2860 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
2861 		if (*ill_zerocopy_capab == NULL) {
2862 			*ill_zerocopy_capab =
2863 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
2864 			    KM_NOSLEEP);
2865 
2866 			if (*ill_zerocopy_capab == NULL) {
2867 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2868 				    "could not enable Zero-copy version %d "
2869 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
2870 				    ill->ill_name);
2871 				return;
2872 			}
2873 		}
2874 
2875 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
2876 		    "supports Zero-copy version %d\n", ill->ill_name,
2877 		    ZEROCOPY_VERSION_1));
2878 
2879 		(*ill_zerocopy_capab)->ill_zerocopy_version =
2880 		    zc_ic->zerocopy_version;
2881 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
2882 		    zc_ic->zerocopy_flags;
2883 
2884 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
2885 	} else {
2886 		uint_t size;
2887 		uchar_t *rptr;
2888 
2889 		size = sizeof (dl_capability_req_t) +
2890 		    sizeof (dl_capability_sub_t) +
2891 		    sizeof (dl_capab_zerocopy_t);
2892 
2893 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2894 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2895 			    "could not enable zerocopy for %s (ENOMEM)\n",
2896 			    ill->ill_name);
2897 			return;
2898 		}
2899 
2900 		rptr = nmp->b_rptr;
2901 		/* initialize dl_capability_req_t */
2902 		oc = (dl_capability_req_t *)rptr;
2903 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2904 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2905 		    sizeof (dl_capab_zerocopy_t);
2906 		rptr += sizeof (dl_capability_req_t);
2907 
2908 		/* initialize dl_capability_sub_t */
2909 		bcopy(isub, rptr, sizeof (*isub));
2910 		rptr += sizeof (*isub);
2911 
2912 		/* initialize dl_capab_zerocopy_t */
2913 		zc_oc = (dl_capab_zerocopy_t *)rptr;
2914 		*zc_oc = *zc_ic;
2915 
2916 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
2917 		    "to enable zero-copy version %d\n", ill->ill_name,
2918 		    ZEROCOPY_VERSION_1));
2919 
2920 		/* set VMSAFE_MEM flag */
2921 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
2922 
2923 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
2924 		ill_capability_send(ill, nmp);
2925 	}
2926 }
2927 
2928 static void
2929 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
2930 {
2931 	dl_capab_zerocopy_t *zerocopy_subcap;
2932 	dl_capability_sub_t *dl_subcap;
2933 
2934 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
2935 		return;
2936 
2937 	ASSERT(ill->ill_zerocopy_capab != NULL);
2938 
2939 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2940 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
2941 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
2942 
2943 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
2944 	zerocopy_subcap->zerocopy_version =
2945 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
2946 	zerocopy_subcap->zerocopy_flags = 0;
2947 
2948 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
2949 }
2950 
2951 /*
2952  * DLD capability
2953  * Refer to dld.h for more information regarding the purpose and usage
2954  * of this capability.
2955  */
2956 static void
2957 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2958 {
2959 	dl_capab_dld_t		*dld_ic, dld;
2960 	uint_t			sub_dl_cap = isub->dl_cap;
2961 	uint8_t			*capend;
2962 	ill_dld_capab_t		*idc;
2963 
2964 	ASSERT(IAM_WRITER_ILL(ill));
2965 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
2966 
2967 	/*
2968 	 * Note: range checks here are not absolutely sufficient to
2969 	 * make us robust against malformed messages sent by drivers;
2970 	 * this is in keeping with the rest of IP's dlpi handling.
2971 	 * (Remember, it's coming from something else in the kernel
2972 	 * address space)
2973 	 */
2974 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2975 	if (capend > mp->b_wptr) {
2976 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
2977 		    "malformed sub-capability too long for mblk");
2978 		return;
2979 	}
2980 	dld_ic = (dl_capab_dld_t *)(isub + 1);
2981 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
2982 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
2983 		    "unsupported DLD sub-capability (version %d, "
2984 		    "expected %d)", dld_ic->dld_version,
2985 		    DLD_CURRENT_VERSION);
2986 		return;
2987 	}
2988 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
2989 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
2990 		    "capability isn't as expected; pass-thru module(s) "
2991 		    "detected, discarding capability\n"));
2992 		return;
2993 	}
2994 
2995 	/*
2996 	 * Copy locally to ensure alignment.
2997 	 */
2998 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
2999 
3000 	if ((idc = ill->ill_dld_capab) == NULL) {
3001 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
3002 		if (idc == NULL) {
3003 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
3004 			    "could not enable DLD version %d "
3005 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
3006 			    ill->ill_name);
3007 			return;
3008 		}
3009 		idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
3010 		idc->idc_capab_dh = (void *)dld.dld_capab_handle;
3011 		ill->ill_dld_capab = idc;
3012 	}
3013 	ip1dbg(("ill_capability_dld_ack: interface %s "
3014 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
3015 
3016 	ill_capability_dld_enable(ill);
3017 }
3018 
3019 /*
3020  * Typically capability negotiation between IP and the driver happens via
3021  * DLPI message exchange. However GLD also offers a direct function call
3022  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
3023  * But arbitrary function calls into IP or GLD are not permitted, since both
3024  * of them are protected by their own perimeter mechanism. The perimeter can
3025  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
3026  * these perimeters is IP -> MAC. Thus for example to enable the squeue
3027  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
3028  * to enter the mac perimeter and then do the direct function calls into
3029  * GLD to enable squeue polling. The ring related callbacks from the mac into
3030  * the stack to add, bind, quiesce, restart or cleanup a ring are all
3031  * protected by the mac perimeter.
3032  */
3033 static void
3034 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
3035 {
3036 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3037 	int			err;
3038 
3039 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
3040 	    DLD_ENABLE);
3041 	ASSERT(err == 0);
3042 }
3043 
3044 static void
3045 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
3046 {
3047 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3048 	int			err;
3049 
3050 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
3051 	    DLD_DISABLE);
3052 	ASSERT(err == 0);
3053 }
3054 
3055 boolean_t
3056 ill_mac_perim_held(ill_t *ill)
3057 {
3058 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3059 
3060 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
3061 	    DLD_QUERY));
3062 }
3063 
3064 static void
3065 ill_capability_direct_enable(ill_t *ill)
3066 {
3067 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3068 	ill_dld_direct_t	*idd = &idc->idc_direct;
3069 	dld_capab_direct_t	direct;
3070 	int			rc;
3071 
3072 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3073 
3074 	bzero(&direct, sizeof (direct));
3075 	direct.di_rx_cf = (uintptr_t)ip_input;
3076 	direct.di_rx_ch = ill;
3077 
3078 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
3079 	    DLD_ENABLE);
3080 	if (rc == 0) {
3081 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
3082 		idd->idd_tx_dh = direct.di_tx_dh;
3083 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
3084 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
3085 		/*
3086 		 * One time registration of flow enable callback function
3087 		 */
3088 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
3089 		    ill_flow_enable, ill);
3090 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
3091 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
3092 	} else {
3093 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
3094 		    "capability, rc = %d\n", rc);
3095 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
3096 	}
3097 }
3098 
3099 static void
3100 ill_capability_poll_enable(ill_t *ill)
3101 {
3102 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3103 	dld_capab_poll_t	poll;
3104 	int			rc;
3105 
3106 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3107 
3108 	bzero(&poll, sizeof (poll));
3109 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
3110 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
3111 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
3112 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
3113 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
3114 	poll.poll_ring_ch = ill;
3115 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
3116 	    DLD_ENABLE);
3117 	if (rc == 0) {
3118 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
3119 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
3120 	} else {
3121 		ip1dbg(("warning: could not enable POLL "
3122 		    "capability, rc = %d\n", rc));
3123 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
3124 	}
3125 }
3126 
3127 /*
3128  * Enable the LSO capability.
3129  */
3130 static void
3131 ill_capability_lso_enable(ill_t *ill)
3132 {
3133 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
3134 	dld_capab_lso_t	lso;
3135 	int rc;
3136 
3137 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3138 
3139 	if (ill->ill_lso_capab == NULL) {
3140 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3141 		    KM_NOSLEEP);
3142 		if (ill->ill_lso_capab == NULL) {
3143 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
3144 			    "could not enable LSO for %s (ENOMEM)\n",
3145 			    ill->ill_name);
3146 			return;
3147 		}
3148 	}
3149 
3150 	bzero(&lso, sizeof (lso));
3151 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
3152 	    DLD_ENABLE)) == 0) {
3153 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
3154 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
3155 		ill->ill_capabilities |= ILL_CAPAB_DLD_LSO;
3156 		ip1dbg(("ill_capability_lso_enable: interface %s "
3157 		    "has enabled LSO\n ", ill->ill_name));
3158 	} else {
3159 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
3160 		ill->ill_lso_capab = NULL;
3161 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
3162 	}
3163 }
3164 
3165 static void
3166 ill_capability_dld_enable(ill_t *ill)
3167 {
3168 	mac_perim_handle_t mph;
3169 
3170 	ASSERT(IAM_WRITER_ILL(ill));
3171 
3172 	if (ill->ill_isv6)
3173 		return;
3174 
3175 	ill_mac_perim_enter(ill, &mph);
3176 	if (!ill->ill_isv6) {
3177 		ill_capability_direct_enable(ill);
3178 		ill_capability_poll_enable(ill);
3179 		ill_capability_lso_enable(ill);
3180 	}
3181 	ill->ill_capabilities |= ILL_CAPAB_DLD;
3182 	ill_mac_perim_exit(ill, mph);
3183 }
3184 
3185 static void
3186 ill_capability_dld_disable(ill_t *ill)
3187 {
3188 	ill_dld_capab_t	*idc;
3189 	ill_dld_direct_t *idd;
3190 	mac_perim_handle_t	mph;
3191 
3192 	ASSERT(IAM_WRITER_ILL(ill));
3193 
3194 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
3195 		return;
3196 
3197 	ill_mac_perim_enter(ill, &mph);
3198 
3199 	idc = ill->ill_dld_capab;
3200 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
3201 		/*
3202 		 * For performance we avoid locks in the transmit data path
3203 		 * and don't maintain a count of the number of threads using
3204 		 * direct calls. Thus some threads could be using direct
3205 		 * transmit calls to GLD, even after the capability mechanism
3206 		 * turns it off. This is still safe since the handles used in
3207 		 * the direct calls continue to be valid until the unplumb is
3208 		 * completed. Remove the callback that was added (1-time) at
3209 		 * capab enable time.
3210 		 */
3211 		mutex_enter(&ill->ill_lock);
3212 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
3213 		mutex_exit(&ill->ill_lock);
3214 		if (ill->ill_flownotify_mh != NULL) {
3215 			idd = &idc->idc_direct;
3216 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
3217 			    ill->ill_flownotify_mh);
3218 			ill->ill_flownotify_mh = NULL;
3219 		}
3220 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
3221 		    NULL, DLD_DISABLE);
3222 	}
3223 
3224 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
3225 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
3226 		ip_squeue_clean_all(ill);
3227 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
3228 		    NULL, DLD_DISABLE);
3229 	}
3230 
3231 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_LSO) != 0) {
3232 		ASSERT(ill->ill_lso_capab != NULL);
3233 		/*
3234 		 * Clear the capability flag for LSO but retain the
3235 		 * ill_lso_capab structure since it's possible that another
3236 		 * thread is still referring to it.  The structure only gets
3237 		 * deallocated when we destroy the ill.
3238 		 */
3239 
3240 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_LSO;
3241 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
3242 		    NULL, DLD_DISABLE);
3243 	}
3244 
3245 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
3246 	ill_mac_perim_exit(ill, mph);
3247 }
3248 
3249 /*
3250  * Capability Negotiation protocol
3251  *
3252  * We don't wait for DLPI capability operations to finish during interface
3253  * bringup or teardown. Doing so would introduce more asynchrony and the
3254  * interface up/down operations will need multiple return and restarts.
3255  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
3256  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
3257  * exclusive operation won't start until the DLPI operations of the previous
3258  * exclusive operation complete.
3259  *
3260  * The capability state machine is shown below.
3261  *
3262  * state		next state		event, action
3263  *
3264  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
3265  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
3266  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
3267  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
3268  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
3269  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
3270  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
3271  *						    ill_capability_probe.
3272  */
3273 
3274 /*
3275  * Dedicated thread started from ip_stack_init that handles capability
3276  * disable. This thread ensures the taskq dispatch does not fail by waiting
3277  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
3278  * that direct calls to DLD are done in a cv_waitable context.
3279  */
3280 void
3281 ill_taskq_dispatch(ip_stack_t *ipst)
3282 {
3283 	callb_cpr_t cprinfo;
3284 	char 	name[64];
3285 	mblk_t	*mp;
3286 
3287 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
3288 	    ipst->ips_netstack->netstack_stackid);
3289 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
3290 	    name);
3291 	mutex_enter(&ipst->ips_capab_taskq_lock);
3292 
3293 	for (;;) {
3294 		mp = list_head(&ipst->ips_capab_taskq_list);
3295 		while (mp != NULL) {
3296 			list_remove(&ipst->ips_capab_taskq_list, mp);
3297 			mutex_exit(&ipst->ips_capab_taskq_lock);
3298 			VERIFY(taskq_dispatch(system_taskq,
3299 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
3300 			mutex_enter(&ipst->ips_capab_taskq_lock);
3301 			mp = list_head(&ipst->ips_capab_taskq_list);
3302 		}
3303 
3304 		if (ipst->ips_capab_taskq_quit)
3305 			break;
3306 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3307 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
3308 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
3309 	}
3310 	VERIFY(list_head(&ipst->ips_capab_taskq_list) == NULL);
3311 	CALLB_CPR_EXIT(&cprinfo);
3312 	thread_exit();
3313 }
3314 
3315 /*
3316  * Consume a new-style hardware capabilities negotiation ack.
3317  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
3318  */
3319 static void
3320 ill_capability_ack_thr(void *arg)
3321 {
3322 	mblk_t	*mp = arg;
3323 	dl_capability_ack_t *capp;
3324 	dl_capability_sub_t *subp, *endp;
3325 	ill_t	*ill;
3326 	boolean_t reneg;
3327 
3328 	ill = (ill_t *)mp->b_prev;
3329 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
3330 
3331 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
3332 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
3333 		/*
3334 		 * We have received the ack for our DL_CAPAB reset request.
3335 		 * There isnt' anything in the message that needs processing.
3336 		 * All message based capabilities have been disabled, now
3337 		 * do the function call based capability disable.
3338 		 */
3339 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
3340 		ill_capability_dld_disable(ill);
3341 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
3342 		if (reneg)
3343 			ill_capability_probe(ill);
3344 		goto done;
3345 	}
3346 
3347 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
3348 		ill->ill_dlpi_capab_state = IDCS_OK;
3349 
3350 	capp = (dl_capability_ack_t *)mp->b_rptr;
3351 
3352 	if (capp->dl_sub_length == 0) {
3353 		/* no new-style capabilities */
3354 		goto done;
3355 	}
3356 
3357 	/* make sure the driver supplied correct dl_sub_length */
3358 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3359 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3360 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3361 		goto done;
3362 	}
3363 
3364 
3365 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3366 	/*
3367 	 * There are sub-capabilities. Process the ones we know about.
3368 	 * Loop until we don't have room for another sub-cap header..
3369 	 */
3370 	for (subp = SC(capp, capp->dl_sub_offset),
3371 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3372 	    subp <= endp;
3373 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3374 
3375 		switch (subp->dl_cap) {
3376 		case DL_CAPAB_ID_WRAPPER:
3377 			ill_capability_id_ack(ill, mp, subp);
3378 			break;
3379 		default:
3380 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3381 			break;
3382 		}
3383 	}
3384 #undef SC
3385 done:
3386 	inet_freemsg(mp);
3387 	ill_capability_done(ill);
3388 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3389 }
3390 
3391 /*
3392  * This needs to be started in a taskq thread to provide a cv_waitable
3393  * context.
3394  */
3395 void
3396 ill_capability_ack(ill_t *ill, mblk_t *mp)
3397 {
3398 	ip_stack_t	*ipst = ill->ill_ipst;
3399 
3400 	mp->b_prev = (mblk_t *)ill;
3401 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3402 	    TQ_NOSLEEP) != 0)
3403 		return;
3404 
3405 	/*
3406 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3407 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3408 	 */
3409 	mutex_enter(&ipst->ips_capab_taskq_lock);
3410 	list_insert_tail(&ipst->ips_capab_taskq_list, mp);
3411 	cv_signal(&ipst->ips_capab_taskq_cv);
3412 	mutex_exit(&ipst->ips_capab_taskq_lock);
3413 }
3414 
3415 /*
3416  * This routine is called to scan the fragmentation reassembly table for
3417  * the specified ILL for any packets that are starting to smell.
3418  * dead_interval is the maximum time in seconds that will be tolerated.  It
3419  * will either be the value specified in ip_g_frag_timeout, or zero if the
3420  * ILL is shutting down and it is time to blow everything off.
3421  *
3422  * It returns the number of seconds (as a time_t) that the next frag timer
3423  * should be scheduled for, 0 meaning that the timer doesn't need to be
3424  * re-started.  Note that the method of calculating next_timeout isn't
3425  * entirely accurate since time will flow between the time we grab
3426  * current_time and the time we schedule the next timeout.  This isn't a
3427  * big problem since this is the timer for sending an ICMP reassembly time
3428  * exceeded messages, and it doesn't have to be exactly accurate.
3429  *
3430  * This function is
3431  * sometimes called as writer, although this is not required.
3432  */
3433 time_t
3434 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3435 {
3436 	ipfb_t	*ipfb;
3437 	ipfb_t	*endp;
3438 	ipf_t	*ipf;
3439 	ipf_t	*ipfnext;
3440 	mblk_t	*mp;
3441 	time_t	current_time = gethrestime_sec();
3442 	time_t	next_timeout = 0;
3443 	uint32_t	hdr_length;
3444 	mblk_t	*send_icmp_head;
3445 	mblk_t	*send_icmp_head_v6;
3446 	zoneid_t zoneid;
3447 	ip_stack_t *ipst = ill->ill_ipst;
3448 
3449 	ipfb = ill->ill_frag_hash_tbl;
3450 	if (ipfb == NULL)
3451 		return (B_FALSE);
3452 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3453 	/* Walk the frag hash table. */
3454 	for (; ipfb < endp; ipfb++) {
3455 		send_icmp_head = NULL;
3456 		send_icmp_head_v6 = NULL;
3457 		mutex_enter(&ipfb->ipfb_lock);
3458 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3459 			time_t frag_time = current_time - ipf->ipf_timestamp;
3460 			time_t frag_timeout;
3461 
3462 			if (frag_time < dead_interval) {
3463 				/*
3464 				 * There are some outstanding fragments
3465 				 * that will timeout later.  Make note of
3466 				 * the time so that we can reschedule the
3467 				 * next timeout appropriately.
3468 				 */
3469 				frag_timeout = dead_interval - frag_time;
3470 				if (next_timeout == 0 ||
3471 				    frag_timeout < next_timeout) {
3472 					next_timeout = frag_timeout;
3473 				}
3474 				break;
3475 			}
3476 			/* Time's up.  Get it out of here. */
3477 			hdr_length = ipf->ipf_nf_hdr_len;
3478 			ipfnext = ipf->ipf_hash_next;
3479 			if (ipfnext)
3480 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3481 			*ipf->ipf_ptphn = ipfnext;
3482 			mp = ipf->ipf_mp->b_cont;
3483 			for (; mp; mp = mp->b_cont) {
3484 				/* Extra points for neatness. */
3485 				IP_REASS_SET_START(mp, 0);
3486 				IP_REASS_SET_END(mp, 0);
3487 			}
3488 			mp = ipf->ipf_mp->b_cont;
3489 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3490 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3491 			ipfb->ipfb_count -= ipf->ipf_count;
3492 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3493 			ipfb->ipfb_frag_pkts--;
3494 			/*
3495 			 * We do not send any icmp message from here because
3496 			 * we currently are holding the ipfb_lock for this
3497 			 * hash chain. If we try and send any icmp messages
3498 			 * from here we may end up via a put back into ip
3499 			 * trying to get the same lock, causing a recursive
3500 			 * mutex panic. Instead we build a list and send all
3501 			 * the icmp messages after we have dropped the lock.
3502 			 */
3503 			if (ill->ill_isv6) {
3504 				if (hdr_length != 0) {
3505 					mp->b_next = send_icmp_head_v6;
3506 					send_icmp_head_v6 = mp;
3507 				} else {
3508 					freemsg(mp);
3509 				}
3510 			} else {
3511 				if (hdr_length != 0) {
3512 					mp->b_next = send_icmp_head;
3513 					send_icmp_head = mp;
3514 				} else {
3515 					freemsg(mp);
3516 				}
3517 			}
3518 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3519 			freeb(ipf->ipf_mp);
3520 		}
3521 		mutex_exit(&ipfb->ipfb_lock);
3522 		/*
3523 		 * Now need to send any icmp messages that we delayed from
3524 		 * above.
3525 		 */
3526 		while (send_icmp_head_v6 != NULL) {
3527 			ip6_t *ip6h;
3528 
3529 			mp = send_icmp_head_v6;
3530 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3531 			mp->b_next = NULL;
3532 			if (mp->b_datap->db_type == M_CTL)
3533 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3534 			else
3535 				ip6h = (ip6_t *)mp->b_rptr;
3536 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3537 			    ill, ipst);
3538 			if (zoneid == ALL_ZONES) {
3539 				freemsg(mp);
3540 			} else {
3541 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3542 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3543 				    B_FALSE, zoneid, ipst);
3544 			}
3545 		}
3546 		while (send_icmp_head != NULL) {
3547 			ipaddr_t dst;
3548 
3549 			mp = send_icmp_head;
3550 			send_icmp_head = send_icmp_head->b_next;
3551 			mp->b_next = NULL;
3552 
3553 			if (mp->b_datap->db_type == M_CTL)
3554 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3555 			else
3556 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3557 
3558 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3559 			if (zoneid == ALL_ZONES) {
3560 				freemsg(mp);
3561 			} else {
3562 				icmp_time_exceeded(ill->ill_wq, mp,
3563 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3564 				    ipst);
3565 			}
3566 		}
3567 	}
3568 	/*
3569 	 * A non-dying ILL will use the return value to decide whether to
3570 	 * restart the frag timer, and for how long.
3571 	 */
3572 	return (next_timeout);
3573 }
3574 
3575 /*
3576  * This routine is called when the approximate count of mblk memory used
3577  * for the specified ILL has exceeded max_count.
3578  */
3579 void
3580 ill_frag_prune(ill_t *ill, uint_t max_count)
3581 {
3582 	ipfb_t	*ipfb;
3583 	ipf_t	*ipf;
3584 	size_t	count;
3585 
3586 	/*
3587 	 * If we are here within ip_min_frag_prune_time msecs remove
3588 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3589 	 * ill_frag_free_num_pkts.
3590 	 */
3591 	mutex_enter(&ill->ill_lock);
3592 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3593 	    (ip_min_frag_prune_time != 0 ?
3594 	    ip_min_frag_prune_time : msec_per_tick)) {
3595 
3596 		ill->ill_frag_free_num_pkts++;
3597 
3598 	} else {
3599 		ill->ill_frag_free_num_pkts = 0;
3600 	}
3601 	ill->ill_last_frag_clean_time = lbolt;
3602 	mutex_exit(&ill->ill_lock);
3603 
3604 	/*
3605 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3606 	 */
3607 	if (ill->ill_frag_free_num_pkts != 0) {
3608 		int ix;
3609 
3610 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3611 			ipfb = &ill->ill_frag_hash_tbl[ix];
3612 			mutex_enter(&ipfb->ipfb_lock);
3613 			if (ipfb->ipfb_ipf != NULL) {
3614 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3615 				    ill->ill_frag_free_num_pkts);
3616 			}
3617 			mutex_exit(&ipfb->ipfb_lock);
3618 		}
3619 	}
3620 	/*
3621 	 * While the reassembly list for this ILL is too big, prune a fragment
3622 	 * queue by age, oldest first.
3623 	 */
3624 	while (ill->ill_frag_count > max_count) {
3625 		int	ix;
3626 		ipfb_t	*oipfb = NULL;
3627 		uint_t	oldest = UINT_MAX;
3628 
3629 		count = 0;
3630 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3631 			ipfb = &ill->ill_frag_hash_tbl[ix];
3632 			mutex_enter(&ipfb->ipfb_lock);
3633 			ipf = ipfb->ipfb_ipf;
3634 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3635 				oldest = ipf->ipf_gen;
3636 				oipfb = ipfb;
3637 			}
3638 			count += ipfb->ipfb_count;
3639 			mutex_exit(&ipfb->ipfb_lock);
3640 		}
3641 		if (oipfb == NULL)
3642 			break;
3643 
3644 		if (count <= max_count)
3645 			return;	/* Somebody beat us to it, nothing to do */
3646 		mutex_enter(&oipfb->ipfb_lock);
3647 		ipf = oipfb->ipfb_ipf;
3648 		if (ipf != NULL) {
3649 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3650 		}
3651 		mutex_exit(&oipfb->ipfb_lock);
3652 	}
3653 }
3654 
3655 /*
3656  * free 'free_cnt' fragmented packets starting at ipf.
3657  */
3658 void
3659 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3660 {
3661 	size_t	count;
3662 	mblk_t	*mp;
3663 	mblk_t	*tmp;
3664 	ipf_t **ipfp = ipf->ipf_ptphn;
3665 
3666 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3667 	ASSERT(ipfp != NULL);
3668 	ASSERT(ipf != NULL);
3669 
3670 	while (ipf != NULL && free_cnt-- > 0) {
3671 		count = ipf->ipf_count;
3672 		mp = ipf->ipf_mp;
3673 		ipf = ipf->ipf_hash_next;
3674 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3675 			IP_REASS_SET_START(tmp, 0);
3676 			IP_REASS_SET_END(tmp, 0);
3677 		}
3678 		atomic_add_32(&ill->ill_frag_count, -count);
3679 		ASSERT(ipfb->ipfb_count >= count);
3680 		ipfb->ipfb_count -= count;
3681 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3682 		ipfb->ipfb_frag_pkts--;
3683 		freemsg(mp);
3684 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3685 	}
3686 
3687 	if (ipf)
3688 		ipf->ipf_ptphn = ipfp;
3689 	ipfp[0] = ipf;
3690 }
3691 
3692 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3693 	"obsolete and may be removed in a future release of Solaris.  Use " \
3694 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3695 
3696 /*
3697  * For obsolete per-interface forwarding configuration;
3698  * called in response to ND_GET.
3699  */
3700 /* ARGSUSED */
3701 static int
3702 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3703 {
3704 	ill_t *ill = (ill_t *)cp;
3705 
3706 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3707 
3708 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3709 	return (0);
3710 }
3711 
3712 /*
3713  * For obsolete per-interface forwarding configuration;
3714  * called in response to ND_SET.
3715  */
3716 /* ARGSUSED */
3717 static int
3718 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3719     cred_t *ioc_cr)
3720 {
3721 	long value;
3722 	int retval;
3723 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3724 
3725 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3726 
3727 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3728 	    value < 0 || value > 1) {
3729 		return (EINVAL);
3730 	}
3731 
3732 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3733 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3734 	rw_exit(&ipst->ips_ill_g_lock);
3735 	return (retval);
3736 }
3737 
3738 /*
3739  * Helper function for ill_forward_set().
3740  */
3741 static void
3742 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
3743 {
3744 	ip_stack_t	*ipst = ill->ill_ipst;
3745 
3746 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3747 
3748 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3749 	    (enable ? "Enabling" : "Disabling"),
3750 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3751 	mutex_enter(&ill->ill_lock);
3752 	if (enable)
3753 		ill->ill_flags |= ILLF_ROUTER;
3754 	else
3755 		ill->ill_flags &= ~ILLF_ROUTER;
3756 	mutex_exit(&ill->ill_lock);
3757 	if (ill->ill_isv6)
3758 		ill_set_nce_router_flags(ill, enable);
3759 	/* Notify routing socket listeners of this change. */
3760 	ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
3761 }
3762 
3763 /*
3764  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
3765  * socket messages for each interface whose flags we change.
3766  */
3767 int
3768 ill_forward_set(ill_t *ill, boolean_t enable)
3769 {
3770 	ipmp_illgrp_t *illg;
3771 	ip_stack_t *ipst = ill->ill_ipst;
3772 
3773 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3774 
3775 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3776 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3777 		return (0);
3778 
3779 	if (IS_LOOPBACK(ill))
3780 		return (EINVAL);
3781 
3782 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
3783 		/*
3784 		 * Update all of the interfaces in the group.
3785 		 */
3786 		illg = ill->ill_grp;
3787 		ill = list_head(&illg->ig_if);
3788 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
3789 			ill_forward_set_on_ill(ill, enable);
3790 
3791 		/*
3792 		 * Update the IPMP meta-interface.
3793 		 */
3794 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
3795 		return (0);
3796 	}
3797 
3798 	ill_forward_set_on_ill(ill, enable);
3799 	return (0);
3800 }
3801 
3802 /*
3803  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3804  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3805  * set or clear.
3806  */
3807 static void
3808 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3809 {
3810 	ipif_t *ipif;
3811 	nce_t *nce;
3812 
3813 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3814 		/*
3815 		 * NOTE: we're called separately for each ill in an illgrp,
3816 		 * so don't match across the illgrp.
3817 		 */
3818 		nce = ndp_lookup_v6(ill, B_FALSE, &ipif->ipif_v6lcl_addr,
3819 		    B_FALSE);
3820 		if (nce != NULL) {
3821 			mutex_enter(&nce->nce_lock);
3822 			if (enable)
3823 				nce->nce_flags |= NCE_F_ISROUTER;
3824 			else
3825 				nce->nce_flags &= ~NCE_F_ISROUTER;
3826 			mutex_exit(&nce->nce_lock);
3827 			NCE_REFRELE(nce);
3828 		}
3829 	}
3830 }
3831 
3832 /*
3833  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3834  * for this ill.  Make sure the v6/v4 question has been answered about this
3835  * ill.  The creation of this ndd variable is only for backwards compatibility.
3836  * The preferred way to control per-interface IP forwarding is through the
3837  * ILLF_ROUTER interface flag.
3838  */
3839 static int
3840 ill_set_ndd_name(ill_t *ill)
3841 {
3842 	char *suffix;
3843 	ip_stack_t	*ipst = ill->ill_ipst;
3844 
3845 	ASSERT(IAM_WRITER_ILL(ill));
3846 
3847 	if (ill->ill_isv6)
3848 		suffix = ipv6_forward_suffix;
3849 	else
3850 		suffix = ipv4_forward_suffix;
3851 
3852 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3853 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3854 	/*
3855 	 * Copies over the '\0'.
3856 	 * Note that strlen(suffix) is always bounded.
3857 	 */
3858 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3859 	    strlen(suffix) + 1);
3860 
3861 	/*
3862 	 * Use of the nd table requires holding the reader lock.
3863 	 * Modifying the nd table thru nd_load/nd_unload requires
3864 	 * the writer lock.
3865 	 */
3866 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3867 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3868 	    nd_ill_forward_set, (caddr_t)ill)) {
3869 		/*
3870 		 * If the nd_load failed, it only meant that it could not
3871 		 * allocate a new bunch of room for further NDD expansion.
3872 		 * Because of that, the ill_ndd_name will be set to 0, and
3873 		 * this interface is at the mercy of the global ip_forwarding
3874 		 * variable.
3875 		 */
3876 		rw_exit(&ipst->ips_ip_g_nd_lock);
3877 		ill->ill_ndd_name = NULL;
3878 		return (ENOMEM);
3879 	}
3880 	rw_exit(&ipst->ips_ip_g_nd_lock);
3881 	return (0);
3882 }
3883 
3884 /*
3885  * Intializes the context structure and returns the first ill in the list
3886  * cuurently start_list and end_list can have values:
3887  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3888  * IP_V4_G_HEAD		Traverse IPV4 list only.
3889  * IP_V6_G_HEAD		Traverse IPV6 list only.
3890  */
3891 
3892 /*
3893  * We don't check for CONDEMNED ills here. Caller must do that if
3894  * necessary under the ill lock.
3895  */
3896 ill_t *
3897 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3898     ip_stack_t *ipst)
3899 {
3900 	ill_if_t *ifp;
3901 	ill_t *ill;
3902 	avl_tree_t *avl_tree;
3903 
3904 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3905 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3906 
3907 	/*
3908 	 * setup the lists to search
3909 	 */
3910 	if (end_list != MAX_G_HEADS) {
3911 		ctx->ctx_current_list = start_list;
3912 		ctx->ctx_last_list = end_list;
3913 	} else {
3914 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3915 		ctx->ctx_current_list = 0;
3916 	}
3917 
3918 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3919 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3920 		if (ifp != (ill_if_t *)
3921 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3922 			avl_tree = &ifp->illif_avl_by_ppa;
3923 			ill = avl_first(avl_tree);
3924 			/*
3925 			 * ill is guaranteed to be non NULL or ifp should have
3926 			 * not existed.
3927 			 */
3928 			ASSERT(ill != NULL);
3929 			return (ill);
3930 		}
3931 		ctx->ctx_current_list++;
3932 	}
3933 
3934 	return (NULL);
3935 }
3936 
3937 /*
3938  * returns the next ill in the list. ill_first() must have been called
3939  * before calling ill_next() or bad things will happen.
3940  */
3941 
3942 /*
3943  * We don't check for CONDEMNED ills here. Caller must do that if
3944  * necessary under the ill lock.
3945  */
3946 ill_t *
3947 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3948 {
3949 	ill_if_t *ifp;
3950 	ill_t *ill;
3951 	ip_stack_t	*ipst = lastill->ill_ipst;
3952 
3953 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3954 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3955 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3956 	    AVL_AFTER)) != NULL) {
3957 		return (ill);
3958 	}
3959 
3960 	/* goto next ill_ifp in the list. */
3961 	ifp = lastill->ill_ifptr->illif_next;
3962 
3963 	/* make sure not at end of circular list */
3964 	while (ifp ==
3965 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3966 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3967 			return (NULL);
3968 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3969 	}
3970 
3971 	return (avl_first(&ifp->illif_avl_by_ppa));
3972 }
3973 
3974 /*
3975  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
3976  * The final number (PPA) must not have any leading zeros.  Upon success, a
3977  * pointer to the start of the PPA is returned; otherwise NULL is returned.
3978  */
3979 static char *
3980 ill_get_ppa_ptr(char *name)
3981 {
3982 	int namelen = strlen(name);
3983 	int end_ndx = namelen - 1;
3984 	int ppa_ndx, i;
3985 
3986 	/*
3987 	 * Check that the first character is [a-zA-Z], and that the last
3988 	 * character is [0-9].
3989 	 */
3990 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
3991 		return (NULL);
3992 
3993 	/*
3994 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
3995 	 */
3996 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
3997 		if (!isdigit(name[ppa_ndx - 1]))
3998 			break;
3999 
4000 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
4001 		return (NULL);
4002 
4003 	/*
4004 	 * Check that the intermediate characters are [a-z0-9.]
4005 	 */
4006 	for (i = 1; i < ppa_ndx; i++) {
4007 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
4008 		    name[i] != '.' && name[i] != '_') {
4009 			return (NULL);
4010 		}
4011 	}
4012 
4013 	return (name + ppa_ndx);
4014 }
4015 
4016 /*
4017  * use avl tree to locate the ill.
4018  */
4019 static ill_t *
4020 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4021     ipsq_func_t func, int *error, ip_stack_t *ipst)
4022 {
4023 	char *ppa_ptr = NULL;
4024 	int len;
4025 	uint_t ppa;
4026 	ill_t *ill = NULL;
4027 	ill_if_t *ifp;
4028 	int list;
4029 	ipsq_t *ipsq;
4030 
4031 	if (error != NULL)
4032 		*error = 0;
4033 
4034 	/*
4035 	 * get ppa ptr
4036 	 */
4037 	if (isv6)
4038 		list = IP_V6_G_HEAD;
4039 	else
4040 		list = IP_V4_G_HEAD;
4041 
4042 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4043 		if (error != NULL)
4044 			*error = ENXIO;
4045 		return (NULL);
4046 	}
4047 
4048 	len = ppa_ptr - name + 1;
4049 
4050 	ppa = stoi(&ppa_ptr);
4051 
4052 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4053 
4054 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4055 		/*
4056 		 * match is done on len - 1 as the name is not null
4057 		 * terminated it contains ppa in addition to the interface
4058 		 * name.
4059 		 */
4060 		if ((ifp->illif_name_len == len) &&
4061 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4062 			break;
4063 		} else {
4064 			ifp = ifp->illif_next;
4065 		}
4066 	}
4067 
4068 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4069 		/*
4070 		 * Even the interface type does not exist.
4071 		 */
4072 		if (error != NULL)
4073 			*error = ENXIO;
4074 		return (NULL);
4075 	}
4076 
4077 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4078 	if (ill != NULL) {
4079 		/*
4080 		 * The block comment at the start of ipif_down
4081 		 * explains the use of the macros used below
4082 		 */
4083 		GRAB_CONN_LOCK(q);
4084 		mutex_enter(&ill->ill_lock);
4085 		if (ILL_CAN_LOOKUP(ill)) {
4086 			ill_refhold_locked(ill);
4087 			mutex_exit(&ill->ill_lock);
4088 			RELEASE_CONN_LOCK(q);
4089 			return (ill);
4090 		} else if (ILL_CAN_WAIT(ill, q)) {
4091 			ipsq = ill->ill_phyint->phyint_ipsq;
4092 			mutex_enter(&ipsq->ipsq_lock);
4093 			mutex_enter(&ipsq->ipsq_xop->ipx_lock);
4094 			mutex_exit(&ill->ill_lock);
4095 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4096 			mutex_exit(&ipsq->ipsq_xop->ipx_lock);
4097 			mutex_exit(&ipsq->ipsq_lock);
4098 			RELEASE_CONN_LOCK(q);
4099 			if (error != NULL)
4100 				*error = EINPROGRESS;
4101 			return (NULL);
4102 		}
4103 		mutex_exit(&ill->ill_lock);
4104 		RELEASE_CONN_LOCK(q);
4105 	}
4106 	if (error != NULL)
4107 		*error = ENXIO;
4108 	return (NULL);
4109 }
4110 
4111 /*
4112  * comparison function for use with avl.
4113  */
4114 static int
4115 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4116 {
4117 	uint_t ppa;
4118 	uint_t ill_ppa;
4119 
4120 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4121 
4122 	ppa = *((uint_t *)ppa_ptr);
4123 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4124 	/*
4125 	 * We want the ill with the lowest ppa to be on the
4126 	 * top.
4127 	 */
4128 	if (ill_ppa < ppa)
4129 		return (1);
4130 	if (ill_ppa > ppa)
4131 		return (-1);
4132 	return (0);
4133 }
4134 
4135 /*
4136  * remove an interface type from the global list.
4137  */
4138 static void
4139 ill_delete_interface_type(ill_if_t *interface)
4140 {
4141 	ASSERT(interface != NULL);
4142 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4143 
4144 	avl_destroy(&interface->illif_avl_by_ppa);
4145 	if (interface->illif_ppa_arena != NULL)
4146 		vmem_destroy(interface->illif_ppa_arena);
4147 
4148 	remque(interface);
4149 
4150 	mi_free(interface);
4151 }
4152 
4153 /*
4154  * remove ill from the global list.
4155  */
4156 static void
4157 ill_glist_delete(ill_t *ill)
4158 {
4159 	ip_stack_t	*ipst;
4160 	phyint_t	*phyi;
4161 
4162 	if (ill == NULL)
4163 		return;
4164 	ipst = ill->ill_ipst;
4165 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4166 
4167 	/*
4168 	 * If the ill was never inserted into the AVL tree
4169 	 * we skip the if branch.
4170 	 */
4171 	if (ill->ill_ifptr != NULL) {
4172 		/*
4173 		 * remove from AVL tree and free ppa number
4174 		 */
4175 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4176 
4177 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4178 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4179 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4180 		}
4181 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4182 			ill_delete_interface_type(ill->ill_ifptr);
4183 		}
4184 
4185 		/*
4186 		 * Indicate ill is no longer in the list.
4187 		 */
4188 		ill->ill_ifptr = NULL;
4189 		ill->ill_name_length = 0;
4190 		ill->ill_name[0] = '\0';
4191 		ill->ill_ppa = UINT_MAX;
4192 	}
4193 
4194 	/* Generate one last event for this ill. */
4195 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4196 	    ill->ill_name_length);
4197 
4198 	ASSERT(ill->ill_phyint != NULL);
4199 	phyi = ill->ill_phyint;
4200 	ill->ill_phyint = NULL;
4201 
4202 	/*
4203 	 * ill_init allocates a phyint always to store the copy
4204 	 * of flags relevant to phyint. At that point in time, we could
4205 	 * not assign the name and hence phyint_illv4/v6 could not be
4206 	 * initialized. Later in ipif_set_values, we assign the name to
4207 	 * the ill, at which point in time we assign phyint_illv4/v6.
4208 	 * Thus we don't rely on phyint_illv6 to be initialized always.
4209 	 */
4210 	if (ill->ill_flags & ILLF_IPV6)
4211 		phyi->phyint_illv6 = NULL;
4212 	else
4213 		phyi->phyint_illv4 = NULL;
4214 
4215 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
4216 		rw_exit(&ipst->ips_ill_g_lock);
4217 		return;
4218 	}
4219 
4220 	/*
4221 	 * There are no ills left on this phyint; pull it out of the phyint
4222 	 * avl trees, and free it.
4223 	 */
4224 	if (phyi->phyint_ifindex > 0) {
4225 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4226 		    phyi);
4227 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4228 		    phyi);
4229 	}
4230 	rw_exit(&ipst->ips_ill_g_lock);
4231 
4232 	phyint_free(phyi);
4233 }
4234 
4235 /*
4236  * allocate a ppa, if the number of plumbed interfaces of this type are
4237  * less than ill_no_arena do a linear search to find a unused ppa.
4238  * When the number goes beyond ill_no_arena switch to using an arena.
4239  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4240  * is the return value for an error condition, so allocation starts at one
4241  * and is decremented by one.
4242  */
4243 static int
4244 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4245 {
4246 	ill_t *tmp_ill;
4247 	uint_t start, end;
4248 	int ppa;
4249 
4250 	if (ifp->illif_ppa_arena == NULL &&
4251 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4252 		/*
4253 		 * Create an arena.
4254 		 */
4255 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4256 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4257 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4258 			/* allocate what has already been assigned */
4259 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4260 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4261 		    tmp_ill, AVL_AFTER)) {
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 			    /* minaddr */
4268 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4269 			    /* maxaddr */
4270 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4271 			    VM_NOSLEEP|VM_FIRSTFIT);
4272 			if (ppa == 0) {
4273 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4274 				    " failed while switching"));
4275 				vmem_destroy(ifp->illif_ppa_arena);
4276 				ifp->illif_ppa_arena = NULL;
4277 				break;
4278 			}
4279 		}
4280 	}
4281 
4282 	if (ifp->illif_ppa_arena != NULL) {
4283 		if (ill->ill_ppa == UINT_MAX) {
4284 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4285 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4286 			if (ppa == 0)
4287 				return (EAGAIN);
4288 			ill->ill_ppa = --ppa;
4289 		} else {
4290 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4291 			    1, 		/* size */
4292 			    1, 		/* align/quantum */
4293 			    0, 		/* phase */
4294 			    0, 		/* nocross */
4295 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4296 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4297 			    VM_NOSLEEP|VM_FIRSTFIT);
4298 			/*
4299 			 * Most likely the allocation failed because
4300 			 * the requested ppa was in use.
4301 			 */
4302 			if (ppa == 0)
4303 				return (EEXIST);
4304 		}
4305 		return (0);
4306 	}
4307 
4308 	/*
4309 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4310 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4311 	 */
4312 	if (ill->ill_ppa == UINT_MAX) {
4313 		end = UINT_MAX - 1;
4314 		start = 0;
4315 	} else {
4316 		end = start = ill->ill_ppa;
4317 	}
4318 
4319 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4320 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4321 		if (start++ >= end) {
4322 			if (ill->ill_ppa == UINT_MAX)
4323 				return (EAGAIN);
4324 			else
4325 				return (EEXIST);
4326 		}
4327 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4328 	}
4329 	ill->ill_ppa = start;
4330 	return (0);
4331 }
4332 
4333 /*
4334  * Insert ill into the list of configured ill's. Once this function completes,
4335  * the ill is globally visible and is available through lookups. More precisely
4336  * this happens after the caller drops the ill_g_lock.
4337  */
4338 static int
4339 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4340 {
4341 	ill_if_t *ill_interface;
4342 	avl_index_t where = 0;
4343 	int error;
4344 	int name_length;
4345 	int index;
4346 	boolean_t check_length = B_FALSE;
4347 	ip_stack_t	*ipst = ill->ill_ipst;
4348 
4349 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4350 
4351 	name_length = mi_strlen(name) + 1;
4352 
4353 	if (isv6)
4354 		index = IP_V6_G_HEAD;
4355 	else
4356 		index = IP_V4_G_HEAD;
4357 
4358 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4359 	/*
4360 	 * Search for interface type based on name
4361 	 */
4362 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4363 		if ((ill_interface->illif_name_len == name_length) &&
4364 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4365 			break;
4366 		}
4367 		ill_interface = ill_interface->illif_next;
4368 	}
4369 
4370 	/*
4371 	 * Interface type not found, create one.
4372 	 */
4373 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4374 
4375 		ill_g_head_t ghead;
4376 
4377 		/*
4378 		 * allocate ill_if_t structure
4379 		 */
4380 
4381 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4382 		if (ill_interface == NULL) {
4383 			return (ENOMEM);
4384 		}
4385 
4386 
4387 
4388 		(void) strcpy(ill_interface->illif_name, name);
4389 		ill_interface->illif_name_len = name_length;
4390 
4391 		avl_create(&ill_interface->illif_avl_by_ppa,
4392 		    ill_compare_ppa, sizeof (ill_t),
4393 		    offsetof(struct ill_s, ill_avl_byppa));
4394 
4395 		/*
4396 		 * link the structure in the back to maintain order
4397 		 * of configuration for ifconfig output.
4398 		 */
4399 		ghead = ipst->ips_ill_g_heads[index];
4400 		insque(ill_interface, ghead.ill_g_list_tail);
4401 
4402 	}
4403 
4404 	if (ill->ill_ppa == UINT_MAX)
4405 		check_length = B_TRUE;
4406 
4407 	error = ill_alloc_ppa(ill_interface, ill);
4408 	if (error != 0) {
4409 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4410 			ill_delete_interface_type(ill->ill_ifptr);
4411 		return (error);
4412 	}
4413 
4414 	/*
4415 	 * When the ppa is choosen by the system, check that there is
4416 	 * enough space to insert ppa. if a specific ppa was passed in this
4417 	 * check is not required as the interface name passed in will have
4418 	 * the right ppa in it.
4419 	 */
4420 	if (check_length) {
4421 		/*
4422 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4423 		 */
4424 		char buf[sizeof (uint_t) * 3];
4425 
4426 		/*
4427 		 * convert ppa to string to calculate the amount of space
4428 		 * required for it in the name.
4429 		 */
4430 		numtos(ill->ill_ppa, buf);
4431 
4432 		/* Do we have enough space to insert ppa ? */
4433 
4434 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4435 			/* Free ppa and interface type struct */
4436 			if (ill_interface->illif_ppa_arena != NULL) {
4437 				vmem_free(ill_interface->illif_ppa_arena,
4438 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4439 			}
4440 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4441 				ill_delete_interface_type(ill->ill_ifptr);
4442 
4443 			return (EINVAL);
4444 		}
4445 	}
4446 
4447 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4448 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4449 
4450 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4451 	    &where);
4452 	ill->ill_ifptr = ill_interface;
4453 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4454 
4455 	ill_phyint_reinit(ill);
4456 	return (0);
4457 }
4458 
4459 /* Initialize the per phyint ipsq used for serialization */
4460 static boolean_t
4461 ipsq_init(ill_t *ill, boolean_t enter)
4462 {
4463 	ipsq_t  *ipsq;
4464 	ipxop_t	*ipx;
4465 
4466 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
4467 		return (B_FALSE);
4468 
4469 	ill->ill_phyint->phyint_ipsq = ipsq;
4470 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
4471 	ipx->ipx_ipsq = ipsq;
4472 	ipsq->ipsq_next = ipsq;
4473 	ipsq->ipsq_phyint = ill->ill_phyint;
4474 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4475 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
4476 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4477 	if (enter) {
4478 		ipx->ipx_writer = curthread;
4479 		ipx->ipx_forced = B_FALSE;
4480 		ipx->ipx_reentry_cnt = 1;
4481 #ifdef DEBUG
4482 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
4483 #endif
4484 	}
4485 	return (B_TRUE);
4486 }
4487 
4488 /*
4489  * ill_init is called by ip_open when a device control stream is opened.
4490  * It does a few initializations, and shoots a DL_INFO_REQ message down
4491  * to the driver.  The response is later picked up in ip_rput_dlpi and
4492  * used to set up default mechanisms for talking to the driver.  (Always
4493  * called as writer.)
4494  *
4495  * If this function returns error, ip_open will call ip_close which in
4496  * turn will call ill_delete to clean up any memory allocated here that
4497  * is not yet freed.
4498  */
4499 int
4500 ill_init(queue_t *q, ill_t *ill)
4501 {
4502 	int	count;
4503 	dl_info_req_t	*dlir;
4504 	mblk_t	*info_mp;
4505 	uchar_t *frag_ptr;
4506 
4507 	/*
4508 	 * The ill is initialized to zero by mi_alloc*(). In addition
4509 	 * some fields already contain valid values, initialized in
4510 	 * ip_open(), before we reach here.
4511 	 */
4512 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4513 
4514 	ill->ill_rq = q;
4515 	ill->ill_wq = WR(q);
4516 
4517 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4518 	    BPRI_HI);
4519 	if (info_mp == NULL)
4520 		return (ENOMEM);
4521 
4522 	/*
4523 	 * Allocate sufficient space to contain our fragment hash table and
4524 	 * the device name.
4525 	 */
4526 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4527 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4528 	if (frag_ptr == NULL) {
4529 		freemsg(info_mp);
4530 		return (ENOMEM);
4531 	}
4532 	ill->ill_frag_ptr = frag_ptr;
4533 	ill->ill_frag_free_num_pkts = 0;
4534 	ill->ill_last_frag_clean_time = 0;
4535 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4536 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4537 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4538 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4539 		    NULL, MUTEX_DEFAULT, NULL);
4540 	}
4541 
4542 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4543 	if (ill->ill_phyint == NULL) {
4544 		freemsg(info_mp);
4545 		mi_free(frag_ptr);
4546 		return (ENOMEM);
4547 	}
4548 
4549 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4550 	/*
4551 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4552 	 * at this point because of the following reason. If we can't
4553 	 * enter the ipsq at some point and cv_wait, the writer that
4554 	 * wakes us up tries to locate us using the list of all phyints
4555 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4556 	 * If we don't set it now, we risk a missed wakeup.
4557 	 */
4558 	ill->ill_phyint->phyint_illv4 = ill;
4559 	ill->ill_ppa = UINT_MAX;
4560 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4561 
4562 	if (!ipsq_init(ill, B_TRUE)) {
4563 		freemsg(info_mp);
4564 		mi_free(frag_ptr);
4565 		mi_free(ill->ill_phyint);
4566 		return (ENOMEM);
4567 	}
4568 
4569 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4570 
4571 	/* Frag queue limit stuff */
4572 	ill->ill_frag_count = 0;
4573 	ill->ill_ipf_gen = 0;
4574 
4575 	ill->ill_global_timer = INFINITY;
4576 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4577 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4578 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4579 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4580 
4581 	/*
4582 	 * Initialize IPv6 configuration variables.  The IP module is always
4583 	 * opened as an IPv4 module.  Instead tracking down the cases where
4584 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4585 	 * here for convenience, this has no effect until the ill is set to do
4586 	 * IPv6.
4587 	 */
4588 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4589 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4590 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4591 	ill->ill_max_buf = ND_MAX_Q;
4592 	ill->ill_refcnt = 0;
4593 
4594 	/* Send down the Info Request to the driver. */
4595 	info_mp->b_datap->db_type = M_PCPROTO;
4596 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4597 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4598 	dlir->dl_primitive = DL_INFO_REQ;
4599 
4600 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4601 
4602 	qprocson(q);
4603 	ill_dlpi_send(ill, info_mp);
4604 
4605 	return (0);
4606 }
4607 
4608 /*
4609  * ill_dls_info
4610  * creates datalink socket info from the device.
4611  */
4612 int
4613 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4614 {
4615 	size_t	len;
4616 	ill_t	*ill = ipif->ipif_ill;
4617 
4618 	sdl->sdl_family = AF_LINK;
4619 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4620 	sdl->sdl_type = ill->ill_type;
4621 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4622 	len = strlen(sdl->sdl_data);
4623 	ASSERT(len < 256);
4624 	sdl->sdl_nlen = (uchar_t)len;
4625 	sdl->sdl_alen = ill->ill_phys_addr_length;
4626 	sdl->sdl_slen = 0;
4627 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4628 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4629 
4630 	return (sizeof (struct sockaddr_dl));
4631 }
4632 
4633 /*
4634  * ill_xarp_info
4635  * creates xarp info from the device.
4636  */
4637 static int
4638 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4639 {
4640 	sdl->sdl_family = AF_LINK;
4641 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4642 	sdl->sdl_type = ill->ill_type;
4643 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4644 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4645 	sdl->sdl_alen = ill->ill_phys_addr_length;
4646 	sdl->sdl_slen = 0;
4647 	return (sdl->sdl_nlen);
4648 }
4649 
4650 static int
4651 loopback_kstat_update(kstat_t *ksp, int rw)
4652 {
4653 	kstat_named_t *kn;
4654 	netstackid_t	stackid;
4655 	netstack_t	*ns;
4656 	ip_stack_t	*ipst;
4657 
4658 	if (ksp == NULL || ksp->ks_data == NULL)
4659 		return (EIO);
4660 
4661 	if (rw == KSTAT_WRITE)
4662 		return (EACCES);
4663 
4664 	kn = KSTAT_NAMED_PTR(ksp);
4665 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4666 
4667 	ns = netstack_find_by_stackid(stackid);
4668 	if (ns == NULL)
4669 		return (-1);
4670 
4671 	ipst = ns->netstack_ip;
4672 	if (ipst == NULL) {
4673 		netstack_rele(ns);
4674 		return (-1);
4675 	}
4676 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4677 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4678 	netstack_rele(ns);
4679 	return (0);
4680 }
4681 
4682 /*
4683  * Has ifindex been plumbed already?
4684  */
4685 static boolean_t
4686 phyint_exists(uint_t index, ip_stack_t *ipst)
4687 {
4688 	ASSERT(index != 0);
4689 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4690 
4691 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4692 	    &index, NULL) != NULL);
4693 }
4694 
4695 /* Pick a unique ifindex */
4696 boolean_t
4697 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4698 {
4699 	uint_t starting_index;
4700 
4701 	if (!ipst->ips_ill_index_wrap) {
4702 		*indexp = ipst->ips_ill_index++;
4703 		if (ipst->ips_ill_index == 0) {
4704 			/* Reached the uint_t limit Next time wrap  */
4705 			ipst->ips_ill_index_wrap = B_TRUE;
4706 		}
4707 		return (B_TRUE);
4708 	}
4709 
4710 	/*
4711 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4712 	 * at this point and don't want to call any function that attempts
4713 	 * to get the lock again.
4714 	 */
4715 	starting_index = ipst->ips_ill_index++;
4716 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4717 		if (ipst->ips_ill_index != 0 &&
4718 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4719 			/* found unused index - use it */
4720 			*indexp = ipst->ips_ill_index;
4721 			return (B_TRUE);
4722 		}
4723 	}
4724 
4725 	/*
4726 	 * all interface indicies are inuse.
4727 	 */
4728 	return (B_FALSE);
4729 }
4730 
4731 /*
4732  * Assign a unique interface index for the phyint.
4733  */
4734 static boolean_t
4735 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4736 {
4737 	ASSERT(phyi->phyint_ifindex == 0);
4738 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4739 }
4740 
4741 /*
4742  * Return a pointer to the ill which matches the supplied name.  Note that
4743  * the ill name length includes the null termination character.  (May be
4744  * called as writer.)
4745  * If do_alloc and the interface is "lo0" it will be automatically created.
4746  * Cannot bump up reference on condemned ills. So dup detect can't be done
4747  * using this func.
4748  */
4749 ill_t *
4750 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4751     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4752     ip_stack_t *ipst)
4753 {
4754 	ill_t	*ill;
4755 	ipif_t	*ipif;
4756 	ipsq_t	*ipsq;
4757 	kstat_named_t	*kn;
4758 	boolean_t isloopback;
4759 	in6_addr_t ov6addr;
4760 
4761 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4762 
4763 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4764 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4765 	rw_exit(&ipst->ips_ill_g_lock);
4766 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4767 		return (ill);
4768 
4769 	/*
4770 	 * Couldn't find it.  Does this happen to be a lookup for the
4771 	 * loopback device and are we allowed to allocate it?
4772 	 */
4773 	if (!isloopback || !do_alloc)
4774 		return (NULL);
4775 
4776 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4777 
4778 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4779 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4780 		rw_exit(&ipst->ips_ill_g_lock);
4781 		return (ill);
4782 	}
4783 
4784 	/* Create the loopback device on demand */
4785 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4786 	    sizeof (ipif_loopback_name), BPRI_MED));
4787 	if (ill == NULL)
4788 		goto done;
4789 
4790 	*ill = ill_null;
4791 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4792 	ill->ill_ipst = ipst;
4793 	netstack_hold(ipst->ips_netstack);
4794 	/*
4795 	 * For exclusive stacks we set the zoneid to zero
4796 	 * to make IP operate as if in the global zone.
4797 	 */
4798 	ill->ill_zoneid = GLOBAL_ZONEID;
4799 
4800 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4801 	if (ill->ill_phyint == NULL)
4802 		goto done;
4803 
4804 	if (isv6)
4805 		ill->ill_phyint->phyint_illv6 = ill;
4806 	else
4807 		ill->ill_phyint->phyint_illv4 = ill;
4808 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4809 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4810 	/* Add room for tcp+ip headers */
4811 	if (isv6) {
4812 		ill->ill_isv6 = B_TRUE;
4813 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4814 	} else {
4815 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4816 	}
4817 	if (!ill_allocate_mibs(ill))
4818 		goto done;
4819 	ill->ill_max_mtu = ill->ill_max_frag;
4820 	/*
4821 	 * ipif_loopback_name can't be pointed at directly because its used
4822 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4823 	 * from the glist, ill_glist_delete() sets the first character of
4824 	 * ill_name to '\0'.
4825 	 */
4826 	ill->ill_name = (char *)ill + sizeof (*ill);
4827 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4828 	ill->ill_name_length = sizeof (ipif_loopback_name);
4829 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4830 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4831 
4832 	ill->ill_global_timer = INFINITY;
4833 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4834 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4835 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4836 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4837 
4838 	/* No resolver here. */
4839 	ill->ill_net_type = IRE_LOOPBACK;
4840 
4841 	/* Initialize the ipsq */
4842 	if (!ipsq_init(ill, B_FALSE))
4843 		goto done;
4844 
4845 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4846 	if (ipif == NULL)
4847 		goto done;
4848 
4849 	ill->ill_flags = ILLF_MULTICAST;
4850 
4851 	ov6addr = ipif->ipif_v6lcl_addr;
4852 	/* Set up default loopback address and mask. */
4853 	if (!isv6) {
4854 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4855 
4856 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4857 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4858 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4859 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4860 		    ipif->ipif_v6subnet);
4861 		ill->ill_flags |= ILLF_IPV4;
4862 	} else {
4863 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4864 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4865 		ipif->ipif_v6net_mask = ipv6_all_ones;
4866 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4867 		    ipif->ipif_v6subnet);
4868 		ill->ill_flags |= ILLF_IPV6;
4869 	}
4870 
4871 	/*
4872 	 * Chain us in at the end of the ill list. hold the ill
4873 	 * before we make it globally visible. 1 for the lookup.
4874 	 */
4875 	ill->ill_refcnt = 0;
4876 	ill_refhold(ill);
4877 
4878 	ill->ill_frag_count = 0;
4879 	ill->ill_frag_free_num_pkts = 0;
4880 	ill->ill_last_frag_clean_time = 0;
4881 
4882 	ipsq = ill->ill_phyint->phyint_ipsq;
4883 
4884 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4885 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4886 
4887 	/* Let SCTP know so that it can add this to its list */
4888 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4889 
4890 	/*
4891 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4892 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4893 	 * requires to be after ill_glist_insert() since we need the
4894 	 * ill_index set. Pass on ipv6_loopback as the old address.
4895 	 */
4896 	sctp_update_ipif_addr(ipif, ov6addr);
4897 
4898 	/*
4899 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4900 	 * If so, free our original one.
4901 	 */
4902 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4903 		ipsq_delete(ipsq);
4904 
4905 	/*
4906 	 * Delay this till the ipif is allocated as ipif_allocate
4907 	 * de-references ill_phyint for getting the ifindex. We
4908 	 * can't do this before ipif_allocate because ill_phyint_reinit
4909 	 * -> phyint_assign_ifindex expects ipif to be present.
4910 	 */
4911 	mutex_enter(&ill->ill_phyint->phyint_lock);
4912 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4913 	mutex_exit(&ill->ill_phyint->phyint_lock);
4914 
4915 	if (ipst->ips_loopback_ksp == NULL) {
4916 		/* Export loopback interface statistics */
4917 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4918 		    ipif_loopback_name, "net",
4919 		    KSTAT_TYPE_NAMED, 2, 0,
4920 		    ipst->ips_netstack->netstack_stackid);
4921 		if (ipst->ips_loopback_ksp != NULL) {
4922 			ipst->ips_loopback_ksp->ks_update =
4923 			    loopback_kstat_update;
4924 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4925 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4926 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4927 			ipst->ips_loopback_ksp->ks_private =
4928 			    (void *)(uintptr_t)ipst->ips_netstack->
4929 			    netstack_stackid;
4930 			kstat_install(ipst->ips_loopback_ksp);
4931 		}
4932 	}
4933 
4934 	if (error != NULL)
4935 		*error = 0;
4936 	*did_alloc = B_TRUE;
4937 	rw_exit(&ipst->ips_ill_g_lock);
4938 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4939 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4940 	return (ill);
4941 done:
4942 	if (ill != NULL) {
4943 		if (ill->ill_phyint != NULL) {
4944 			ipsq = ill->ill_phyint->phyint_ipsq;
4945 			if (ipsq != NULL) {
4946 				ipsq->ipsq_phyint = NULL;
4947 				ipsq_delete(ipsq);
4948 			}
4949 			mi_free(ill->ill_phyint);
4950 		}
4951 		ill_free_mib(ill);
4952 		if (ill->ill_ipst != NULL)
4953 			netstack_rele(ill->ill_ipst->ips_netstack);
4954 		mi_free(ill);
4955 	}
4956 	rw_exit(&ipst->ips_ill_g_lock);
4957 	if (error != NULL)
4958 		*error = ENOMEM;
4959 	return (NULL);
4960 }
4961 
4962 /*
4963  * For IPP calls - use the ip_stack_t for global stack.
4964  */
4965 ill_t *
4966 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4967     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4968 {
4969 	ip_stack_t	*ipst;
4970 	ill_t		*ill;
4971 
4972 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4973 	if (ipst == NULL) {
4974 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4975 		return (NULL);
4976 	}
4977 
4978 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4979 	netstack_rele(ipst->ips_netstack);
4980 	return (ill);
4981 }
4982 
4983 /*
4984  * Return a pointer to the ill which matches the index and IP version type.
4985  */
4986 ill_t *
4987 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4988     ipsq_func_t func, int *err, ip_stack_t *ipst)
4989 {
4990 	ill_t	*ill;
4991 	ipsq_t  *ipsq;
4992 	phyint_t *phyi;
4993 
4994 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4995 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4996 
4997 	if (err != NULL)
4998 		*err = 0;
4999 
5000 	/*
5001 	 * Indexes are stored in the phyint - a common structure
5002 	 * to both IPv4 and IPv6.
5003 	 */
5004 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5005 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5006 	    (void *) &index, NULL);
5007 	if (phyi != NULL) {
5008 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5009 		if (ill != NULL) {
5010 			/*
5011 			 * The block comment at the start of ipif_down
5012 			 * explains the use of the macros used below
5013 			 */
5014 			GRAB_CONN_LOCK(q);
5015 			mutex_enter(&ill->ill_lock);
5016 			if (ILL_CAN_LOOKUP(ill)) {
5017 				ill_refhold_locked(ill);
5018 				mutex_exit(&ill->ill_lock);
5019 				RELEASE_CONN_LOCK(q);
5020 				rw_exit(&ipst->ips_ill_g_lock);
5021 				return (ill);
5022 			} else if (ILL_CAN_WAIT(ill, q)) {
5023 				ipsq = ill->ill_phyint->phyint_ipsq;
5024 				mutex_enter(&ipsq->ipsq_lock);
5025 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5026 				rw_exit(&ipst->ips_ill_g_lock);
5027 				mutex_exit(&ill->ill_lock);
5028 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5029 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5030 				mutex_exit(&ipsq->ipsq_lock);
5031 				RELEASE_CONN_LOCK(q);
5032 				if (err != NULL)
5033 					*err = EINPROGRESS;
5034 				return (NULL);
5035 			}
5036 			RELEASE_CONN_LOCK(q);
5037 			mutex_exit(&ill->ill_lock);
5038 		}
5039 	}
5040 	rw_exit(&ipst->ips_ill_g_lock);
5041 	if (err != NULL)
5042 		*err = ENXIO;
5043 	return (NULL);
5044 }
5045 
5046 /*
5047  * Return the ifindex next in sequence after the passed in ifindex.
5048  * If there is no next ifindex for the given protocol, return 0.
5049  */
5050 uint_t
5051 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5052 {
5053 	phyint_t *phyi;
5054 	phyint_t *phyi_initial;
5055 	uint_t   ifindex;
5056 
5057 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5058 
5059 	if (index == 0) {
5060 		phyi = avl_first(
5061 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5062 	} else {
5063 		phyi = phyi_initial = avl_find(
5064 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5065 		    (void *) &index, NULL);
5066 	}
5067 
5068 	for (; phyi != NULL;
5069 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5070 	    phyi, AVL_AFTER)) {
5071 		/*
5072 		 * If we're not returning the first interface in the tree
5073 		 * and we still haven't moved past the phyint_t that
5074 		 * corresponds to index, avl_walk needs to be called again
5075 		 */
5076 		if (!((index != 0) && (phyi == phyi_initial))) {
5077 			if (isv6) {
5078 				if ((phyi->phyint_illv6) &&
5079 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5080 				    (phyi->phyint_illv6->ill_isv6 == 1))
5081 					break;
5082 			} else {
5083 				if ((phyi->phyint_illv4) &&
5084 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5085 				    (phyi->phyint_illv4->ill_isv6 == 0))
5086 					break;
5087 			}
5088 		}
5089 	}
5090 
5091 	rw_exit(&ipst->ips_ill_g_lock);
5092 
5093 	if (phyi != NULL)
5094 		ifindex = phyi->phyint_ifindex;
5095 	else
5096 		ifindex = 0;
5097 
5098 	return (ifindex);
5099 }
5100 
5101 /*
5102  * Return the ifindex for the named interface.
5103  * If there is no next ifindex for the interface, return 0.
5104  */
5105 uint_t
5106 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5107 {
5108 	phyint_t	*phyi;
5109 	avl_index_t	where = 0;
5110 	uint_t		ifindex;
5111 
5112 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5113 
5114 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5115 	    name, &where)) == NULL) {
5116 		rw_exit(&ipst->ips_ill_g_lock);
5117 		return (0);
5118 	}
5119 
5120 	ifindex = phyi->phyint_ifindex;
5121 
5122 	rw_exit(&ipst->ips_ill_g_lock);
5123 
5124 	return (ifindex);
5125 }
5126 
5127 /*
5128  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5129  * that gives a running thread a reference to the ill. This reference must be
5130  * released by the thread when it is done accessing the ill and related
5131  * objects. ill_refcnt can not be used to account for static references
5132  * such as other structures pointing to an ill. Callers must generally
5133  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5134  * or be sure that the ill is not being deleted or changing state before
5135  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5136  * ill won't change any of its critical state such as address, netmask etc.
5137  */
5138 void
5139 ill_refhold(ill_t *ill)
5140 {
5141 	mutex_enter(&ill->ill_lock);
5142 	ill->ill_refcnt++;
5143 	ILL_TRACE_REF(ill);
5144 	mutex_exit(&ill->ill_lock);
5145 }
5146 
5147 void
5148 ill_refhold_locked(ill_t *ill)
5149 {
5150 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5151 	ill->ill_refcnt++;
5152 	ILL_TRACE_REF(ill);
5153 }
5154 
5155 int
5156 ill_check_and_refhold(ill_t *ill)
5157 {
5158 	mutex_enter(&ill->ill_lock);
5159 	if (ILL_CAN_LOOKUP(ill)) {
5160 		ill_refhold_locked(ill);
5161 		mutex_exit(&ill->ill_lock);
5162 		return (0);
5163 	}
5164 	mutex_exit(&ill->ill_lock);
5165 	return (ILL_LOOKUP_FAILED);
5166 }
5167 
5168 /*
5169  * Must not be called while holding any locks. Otherwise if this is
5170  * the last reference to be released, there is a chance of recursive mutex
5171  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5172  * to restart an ioctl.
5173  */
5174 void
5175 ill_refrele(ill_t *ill)
5176 {
5177 	mutex_enter(&ill->ill_lock);
5178 	ASSERT(ill->ill_refcnt != 0);
5179 	ill->ill_refcnt--;
5180 	ILL_UNTRACE_REF(ill);
5181 	if (ill->ill_refcnt != 0) {
5182 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5183 		mutex_exit(&ill->ill_lock);
5184 		return;
5185 	}
5186 
5187 	/* Drops the ill_lock */
5188 	ipif_ill_refrele_tail(ill);
5189 }
5190 
5191 /*
5192  * Obtain a weak reference count on the ill. This reference ensures the
5193  * ill won't be freed, but the ill may change any of its critical state
5194  * such as netmask, address etc. Returns an error if the ill has started
5195  * closing.
5196  */
5197 boolean_t
5198 ill_waiter_inc(ill_t *ill)
5199 {
5200 	mutex_enter(&ill->ill_lock);
5201 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5202 		mutex_exit(&ill->ill_lock);
5203 		return (B_FALSE);
5204 	}
5205 	ill->ill_waiters++;
5206 	mutex_exit(&ill->ill_lock);
5207 	return (B_TRUE);
5208 }
5209 
5210 void
5211 ill_waiter_dcr(ill_t *ill)
5212 {
5213 	mutex_enter(&ill->ill_lock);
5214 	ill->ill_waiters--;
5215 	if (ill->ill_waiters == 0)
5216 		cv_broadcast(&ill->ill_cv);
5217 	mutex_exit(&ill->ill_lock);
5218 }
5219 
5220 /*
5221  * Named Dispatch routine to produce a formatted report on all ILLs.
5222  * This report is accessed by using the ndd utility to "get" ND variable
5223  * "ip_ill_status".
5224  */
5225 /* ARGSUSED */
5226 int
5227 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5228 {
5229 	ill_t		*ill;
5230 	ill_walk_context_t ctx;
5231 	ip_stack_t	*ipst;
5232 
5233 	ipst = CONNQ_TO_IPST(q);
5234 
5235 	(void) mi_mpprintf(mp,
5236 	    "ILL      " MI_COL_HDRPAD_STR
5237 	/*   01234567[89ABCDEF] */
5238 	    "rq       " MI_COL_HDRPAD_STR
5239 	/*   01234567[89ABCDEF] */
5240 	    "wq       " MI_COL_HDRPAD_STR
5241 	/*   01234567[89ABCDEF] */
5242 	    "upcnt mxfrg err name");
5243 	/*   12345 12345 123 xxxxxxxx  */
5244 
5245 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5246 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5247 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5248 		(void) mi_mpprintf(mp,
5249 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5250 		    "%05u %05u %03d %s",
5251 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5252 		    ill->ill_ipif_up_count,
5253 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5254 	}
5255 	rw_exit(&ipst->ips_ill_g_lock);
5256 
5257 	return (0);
5258 }
5259 
5260 /*
5261  * Named Dispatch routine to produce a formatted report on all IPIFs.
5262  * This report is accessed by using the ndd utility to "get" ND variable
5263  * "ip_ipif_status".
5264  */
5265 /* ARGSUSED */
5266 int
5267 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5268 {
5269 	char	buf1[INET6_ADDRSTRLEN];
5270 	char	buf2[INET6_ADDRSTRLEN];
5271 	char	buf3[INET6_ADDRSTRLEN];
5272 	char	buf4[INET6_ADDRSTRLEN];
5273 	char	buf5[INET6_ADDRSTRLEN];
5274 	char	buf6[INET6_ADDRSTRLEN];
5275 	char	buf[LIFNAMSIZ];
5276 	ill_t	*ill;
5277 	ipif_t	*ipif;
5278 	nv_t	*nvp;
5279 	uint64_t flags;
5280 	zoneid_t zoneid;
5281 	ill_walk_context_t ctx;
5282 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5283 
5284 	(void) mi_mpprintf(mp,
5285 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5286 	    "\tlocal address\n"
5287 	    "\tsrc address\n"
5288 	    "\tsubnet\n"
5289 	    "\tmask\n"
5290 	    "\tbroadcast\n"
5291 	    "\tp-p-dst");
5292 
5293 	ASSERT(q->q_next == NULL);
5294 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5295 
5296 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5297 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5298 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5299 		for (ipif = ill->ill_ipif; ipif != NULL;
5300 		    ipif = ipif->ipif_next) {
5301 			if (zoneid != GLOBAL_ZONEID &&
5302 			    zoneid != ipif->ipif_zoneid &&
5303 			    ipif->ipif_zoneid != ALL_ZONES)
5304 				continue;
5305 
5306 			ipif_get_name(ipif, buf, sizeof (buf));
5307 			(void) mi_mpprintf(mp,
5308 			    MI_COL_PTRFMT_STR
5309 			    "%04u %05u %u/%u/%u %s %d",
5310 			    (void *)ipif,
5311 			    ipif->ipif_metric, ipif->ipif_mtu,
5312 			    ipif->ipif_ib_pkt_count,
5313 			    ipif->ipif_ob_pkt_count,
5314 			    ipif->ipif_fo_pkt_count,
5315 			    buf,
5316 			    ipif->ipif_zoneid);
5317 
5318 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5319 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5320 
5321 		/* Tack on text strings for any flags. */
5322 		nvp = ipif_nv_tbl;
5323 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5324 			if (nvp->nv_value & flags)
5325 				(void) mi_mpprintf_nr(mp, " %s",
5326 				    nvp->nv_name);
5327 		}
5328 		(void) mi_mpprintf(mp,
5329 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5330 		    inet_ntop(AF_INET6,
5331 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5332 		    inet_ntop(AF_INET6,
5333 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5334 		    inet_ntop(AF_INET6,
5335 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5336 		    inet_ntop(AF_INET6,
5337 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5338 		    inet_ntop(AF_INET6,
5339 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5340 		    inet_ntop(AF_INET6,
5341 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5342 		}
5343 	}
5344 	rw_exit(&ipst->ips_ill_g_lock);
5345 	return (0);
5346 }
5347 
5348 /*
5349  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5350  * driver.  We construct best guess defaults for lower level information that
5351  * we need.  If an interface is brought up without injection of any overriding
5352  * information from outside, we have to be ready to go with these defaults.
5353  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5354  * we primarely want the dl_provider_style.
5355  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5356  * at which point we assume the other part of the information is valid.
5357  */
5358 void
5359 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5360 {
5361 	uchar_t		*brdcst_addr;
5362 	uint_t		brdcst_addr_length, phys_addr_length;
5363 	t_scalar_t	sap_length;
5364 	dl_info_ack_t	*dlia;
5365 	ip_m_t		*ipm;
5366 	dl_qos_cl_sel1_t *sel1;
5367 	int		min_mtu;
5368 
5369 	ASSERT(IAM_WRITER_ILL(ill));
5370 
5371 	/*
5372 	 * Till the ill is fully up ILL_CHANGING will be set and
5373 	 * the ill is not globally visible. So no need for a lock.
5374 	 */
5375 	dlia = (dl_info_ack_t *)mp->b_rptr;
5376 	ill->ill_mactype = dlia->dl_mac_type;
5377 
5378 	ipm = ip_m_lookup(dlia->dl_mac_type);
5379 	if (ipm == NULL) {
5380 		ipm = ip_m_lookup(DL_OTHER);
5381 		ASSERT(ipm != NULL);
5382 	}
5383 	ill->ill_media = ipm;
5384 
5385 	/*
5386 	 * When the new DLPI stuff is ready we'll pull lengths
5387 	 * from dlia.
5388 	 */
5389 	if (dlia->dl_version == DL_VERSION_2) {
5390 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5391 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5392 		    brdcst_addr_length);
5393 		if (brdcst_addr == NULL) {
5394 			brdcst_addr_length = 0;
5395 		}
5396 		sap_length = dlia->dl_sap_length;
5397 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5398 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5399 		    brdcst_addr_length, sap_length, phys_addr_length));
5400 	} else {
5401 		brdcst_addr_length = 6;
5402 		brdcst_addr = ip_six_byte_all_ones;
5403 		sap_length = -2;
5404 		phys_addr_length = brdcst_addr_length;
5405 	}
5406 
5407 	ill->ill_bcast_addr_length = brdcst_addr_length;
5408 	ill->ill_phys_addr_length = phys_addr_length;
5409 	ill->ill_sap_length = sap_length;
5410 
5411 	/*
5412 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5413 	 * but we must ensure a minimum IP MTU is used since other bits of
5414 	 * IP will fly apart otherwise.
5415 	 */
5416 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5417 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5418 	ill->ill_max_mtu = ill->ill_max_frag;
5419 
5420 	ill->ill_type = ipm->ip_m_type;
5421 
5422 	if (!ill->ill_dlpi_style_set) {
5423 		if (dlia->dl_provider_style == DL_STYLE2)
5424 			ill->ill_needs_attach = 1;
5425 
5426 		/*
5427 		 * Allocate the first ipif on this ill. We don't delay it
5428 		 * further as ioctl handling assumes atleast one ipif to
5429 		 * be present.
5430 		 *
5431 		 * At this point we don't know whether the ill is v4 or v6.
5432 		 * We will know this whan the SIOCSLIFNAME happens and
5433 		 * the correct value for ill_isv6 will be assigned in
5434 		 * ipif_set_values(). We need to hold the ill lock and
5435 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5436 		 * the wakeup.
5437 		 */
5438 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5439 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5440 		mutex_enter(&ill->ill_lock);
5441 		ASSERT(ill->ill_dlpi_style_set == 0);
5442 		ill->ill_dlpi_style_set = 1;
5443 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5444 		cv_broadcast(&ill->ill_cv);
5445 		mutex_exit(&ill->ill_lock);
5446 		freemsg(mp);
5447 		return;
5448 	}
5449 	ASSERT(ill->ill_ipif != NULL);
5450 	/*
5451 	 * We know whether it is IPv4 or IPv6 now, as this is the
5452 	 * second DL_INFO_ACK we are recieving in response to the
5453 	 * DL_INFO_REQ sent in ipif_set_values.
5454 	 */
5455 	if (ill->ill_isv6)
5456 		ill->ill_sap = IP6_DL_SAP;
5457 	else
5458 		ill->ill_sap = IP_DL_SAP;
5459 	/*
5460 	 * Set ipif_mtu which is used to set the IRE's
5461 	 * ire_max_frag value. The driver could have sent
5462 	 * a different mtu from what it sent last time. No
5463 	 * need to call ipif_mtu_change because IREs have
5464 	 * not yet been created.
5465 	 */
5466 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5467 	/*
5468 	 * Clear all the flags that were set based on ill_bcast_addr_length
5469 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5470 	 * changed now and we need to re-evaluate.
5471 	 */
5472 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5473 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5474 
5475 	/*
5476 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5477 	 * changed now.
5478 	 *
5479 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5480 	 * with no underlying interfaces (and thus an unknown broadcast
5481 	 * address length), but we enforce that an interface is broadcast-
5482 	 * capable as part of allowing it to join a group.
5483 	 */
5484 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5485 		if (ill->ill_resolver_mp != NULL)
5486 			freemsg(ill->ill_resolver_mp);
5487 		if (ill->ill_bcast_mp != NULL)
5488 			freemsg(ill->ill_bcast_mp);
5489 		if (ill->ill_flags & ILLF_XRESOLV)
5490 			ill->ill_net_type = IRE_IF_RESOLVER;
5491 		else
5492 			ill->ill_net_type = IRE_IF_NORESOLVER;
5493 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5494 		    ill->ill_phys_addr_length,
5495 		    ill->ill_sap,
5496 		    ill->ill_sap_length);
5497 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5498 
5499 		if (ill->ill_isv6)
5500 			/*
5501 			 * Note: xresolv interfaces will eventually need NOARP
5502 			 * set here as well, but that will require those
5503 			 * external resolvers to have some knowledge of
5504 			 * that flag and act appropriately. Not to be changed
5505 			 * at present.
5506 			 */
5507 			ill->ill_flags |= ILLF_NONUD;
5508 		else
5509 			ill->ill_flags |= ILLF_NOARP;
5510 
5511 		if (ill->ill_phys_addr_length == 0) {
5512 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5513 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5514 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5515 			} else {
5516 				/* pt-pt supports multicast. */
5517 				ill->ill_flags |= ILLF_MULTICAST;
5518 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5519 			}
5520 		}
5521 	} else {
5522 		ill->ill_net_type = IRE_IF_RESOLVER;
5523 		if (ill->ill_bcast_mp != NULL)
5524 			freemsg(ill->ill_bcast_mp);
5525 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5526 		    ill->ill_bcast_addr_length, ill->ill_sap,
5527 		    ill->ill_sap_length);
5528 		/*
5529 		 * Later detect lack of DLPI driver multicast
5530 		 * capability by catching DL_ENABMULTI errors in
5531 		 * ip_rput_dlpi.
5532 		 */
5533 		ill->ill_flags |= ILLF_MULTICAST;
5534 		if (!ill->ill_isv6)
5535 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5536 	}
5537 
5538 	/* For IPMP, PHYI_IPMP should already be set by ipif_allocate() */
5539 	if (ill->ill_mactype == SUNW_DL_IPMP)
5540 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5541 
5542 	/* By default an interface does not support any CoS marking */
5543 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5544 
5545 	/*
5546 	 * If we get QoS information in DL_INFO_ACK, the device supports
5547 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5548 	 */
5549 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5550 	    dlia->dl_qos_length);
5551 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5552 		ill->ill_flags |= ILLF_COS_ENABLED;
5553 	}
5554 
5555 	/* Clear any previous error indication. */
5556 	ill->ill_error = 0;
5557 	freemsg(mp);
5558 }
5559 
5560 /*
5561  * Perform various checks to verify that an address would make sense as a
5562  * local, remote, or subnet interface address.
5563  */
5564 static boolean_t
5565 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5566 {
5567 	ipaddr_t	net_mask;
5568 
5569 	/*
5570 	 * Don't allow all zeroes, or all ones, but allow
5571 	 * all ones netmask.
5572 	 */
5573 	if ((net_mask = ip_net_mask(addr)) == 0)
5574 		return (B_FALSE);
5575 	/* A given netmask overrides the "guess" netmask */
5576 	if (subnet_mask != 0)
5577 		net_mask = subnet_mask;
5578 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5579 	    (addr == (addr | ~net_mask)))) {
5580 		return (B_FALSE);
5581 	}
5582 
5583 	/*
5584 	 * Even if the netmask is all ones, we do not allow address to be
5585 	 * 255.255.255.255
5586 	 */
5587 	if (addr == INADDR_BROADCAST)
5588 		return (B_FALSE);
5589 
5590 	if (CLASSD(addr))
5591 		return (B_FALSE);
5592 
5593 	return (B_TRUE);
5594 }
5595 
5596 #define	V6_IPIF_LINKLOCAL(p)	\
5597 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5598 
5599 /*
5600  * Compare two given ipifs and check if the second one is better than
5601  * the first one using the order of preference (not taking deprecated
5602  * into acount) specified in ipif_lookup_multicast().
5603  */
5604 static boolean_t
5605 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5606 {
5607 	/* Check the least preferred first. */
5608 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5609 		/* If both ipifs are the same, use the first one. */
5610 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5611 			return (B_FALSE);
5612 		else
5613 			return (B_TRUE);
5614 	}
5615 
5616 	/* For IPv6, check for link local address. */
5617 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5618 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5619 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5620 			/* The second one is equal or less preferred. */
5621 			return (B_FALSE);
5622 		} else {
5623 			return (B_TRUE);
5624 		}
5625 	}
5626 
5627 	/* Then check for point to point interface. */
5628 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5629 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5630 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5631 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5632 			return (B_FALSE);
5633 		} else {
5634 			return (B_TRUE);
5635 		}
5636 	}
5637 
5638 	/* old_ipif is a normal interface, so no need to use the new one. */
5639 	return (B_FALSE);
5640 }
5641 
5642 /*
5643  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5644  * The ipif must be up, and its ill must multicast-capable, not
5645  * condemned, not an underlying interface in an IPMP group, and
5646  * not a VNI interface.  Order of preference:
5647  *
5648  * 	1a. normal
5649  * 	1b. normal, but deprecated
5650  * 	2a. point to point
5651  * 	2b. point to point, but deprecated
5652  * 	3a. link local
5653  * 	3b. link local, but deprecated
5654  * 	4. loopback.
5655  */
5656 ipif_t *
5657 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5658 {
5659 	ill_t			*ill;
5660 	ill_walk_context_t	ctx;
5661 	ipif_t			*ipif;
5662 	ipif_t			*saved_ipif = NULL;
5663 	ipif_t			*dep_ipif = NULL;
5664 
5665 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5666 	if (isv6)
5667 		ill = ILL_START_WALK_V6(&ctx, ipst);
5668 	else
5669 		ill = ILL_START_WALK_V4(&ctx, ipst);
5670 
5671 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5672 		mutex_enter(&ill->ill_lock);
5673 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5674 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5675 			mutex_exit(&ill->ill_lock);
5676 			continue;
5677 		}
5678 		for (ipif = ill->ill_ipif; ipif != NULL;
5679 		    ipif = ipif->ipif_next) {
5680 			if (zoneid != ipif->ipif_zoneid &&
5681 			    zoneid != ALL_ZONES &&
5682 			    ipif->ipif_zoneid != ALL_ZONES) {
5683 				continue;
5684 			}
5685 			if (!(ipif->ipif_flags & IPIF_UP) ||
5686 			    !IPIF_CAN_LOOKUP(ipif)) {
5687 				continue;
5688 			}
5689 
5690 			/*
5691 			 * Found one candidate.  If it is deprecated,
5692 			 * remember it in dep_ipif.  If it is not deprecated,
5693 			 * remember it in saved_ipif.
5694 			 */
5695 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5696 				if (dep_ipif == NULL) {
5697 					dep_ipif = ipif;
5698 				} else if (ipif_comp_multi(dep_ipif, ipif,
5699 				    isv6)) {
5700 					/*
5701 					 * If the previous dep_ipif does not
5702 					 * belong to the same ill, we've done
5703 					 * a ipif_refhold() on it.  So we need
5704 					 * to release it.
5705 					 */
5706 					if (dep_ipif->ipif_ill != ill)
5707 						ipif_refrele(dep_ipif);
5708 					dep_ipif = ipif;
5709 				}
5710 				continue;
5711 			}
5712 			if (saved_ipif == NULL) {
5713 				saved_ipif = ipif;
5714 			} else {
5715 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5716 					if (saved_ipif->ipif_ill != ill)
5717 						ipif_refrele(saved_ipif);
5718 					saved_ipif = ipif;
5719 				}
5720 			}
5721 		}
5722 		/*
5723 		 * Before going to the next ill, do a ipif_refhold() on the
5724 		 * saved ones.
5725 		 */
5726 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5727 			ipif_refhold_locked(saved_ipif);
5728 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5729 			ipif_refhold_locked(dep_ipif);
5730 		mutex_exit(&ill->ill_lock);
5731 	}
5732 	rw_exit(&ipst->ips_ill_g_lock);
5733 
5734 	/*
5735 	 * If we have only the saved_ipif, return it.  But if we have both
5736 	 * saved_ipif and dep_ipif, check to see which one is better.
5737 	 */
5738 	if (saved_ipif != NULL) {
5739 		if (dep_ipif != NULL) {
5740 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5741 				ipif_refrele(saved_ipif);
5742 				return (dep_ipif);
5743 			} else {
5744 				ipif_refrele(dep_ipif);
5745 				return (saved_ipif);
5746 			}
5747 		}
5748 		return (saved_ipif);
5749 	} else {
5750 		return (dep_ipif);
5751 	}
5752 }
5753 
5754 /*
5755  * This function is called when an application does not specify an interface
5756  * to be used for multicast traffic (joining a group/sending data).  It
5757  * calls ire_lookup_multi() to look for an interface route for the
5758  * specified multicast group.  Doing this allows the administrator to add
5759  * prefix routes for multicast to indicate which interface to be used for
5760  * multicast traffic in the above scenario.  The route could be for all
5761  * multicast (224.0/4), for a single multicast group (a /32 route) or
5762  * anything in between.  If there is no such multicast route, we just find
5763  * any multicast capable interface and return it.  The returned ipif
5764  * is refhold'ed.
5765  */
5766 ipif_t *
5767 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5768 {
5769 	ire_t			*ire;
5770 	ipif_t			*ipif;
5771 
5772 	ire = ire_lookup_multi(group, zoneid, ipst);
5773 	if (ire != NULL) {
5774 		ipif = ire->ire_ipif;
5775 		ipif_refhold(ipif);
5776 		ire_refrele(ire);
5777 		return (ipif);
5778 	}
5779 
5780 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5781 }
5782 
5783 /*
5784  * Look for an ipif with the specified interface address and destination.
5785  * The destination address is used only for matching point-to-point interfaces.
5786  */
5787 ipif_t *
5788 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5789     ipsq_func_t func, int *error, ip_stack_t *ipst)
5790 {
5791 	ipif_t	*ipif;
5792 	ill_t	*ill;
5793 	ill_walk_context_t ctx;
5794 	ipsq_t	*ipsq;
5795 
5796 	if (error != NULL)
5797 		*error = 0;
5798 
5799 	/*
5800 	 * First match all the point-to-point interfaces
5801 	 * before looking at non-point-to-point interfaces.
5802 	 * This is done to avoid returning non-point-to-point
5803 	 * ipif instead of unnumbered point-to-point ipif.
5804 	 */
5805 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5806 	ill = ILL_START_WALK_V4(&ctx, ipst);
5807 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5808 		GRAB_CONN_LOCK(q);
5809 		mutex_enter(&ill->ill_lock);
5810 		for (ipif = ill->ill_ipif; ipif != NULL;
5811 		    ipif = ipif->ipif_next) {
5812 			/* Allow the ipif to be down */
5813 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5814 			    (ipif->ipif_lcl_addr == if_addr) &&
5815 			    (ipif->ipif_pp_dst_addr == dst)) {
5816 				/*
5817 				 * The block comment at the start of ipif_down
5818 				 * explains the use of the macros used below
5819 				 */
5820 				if (IPIF_CAN_LOOKUP(ipif)) {
5821 					ipif_refhold_locked(ipif);
5822 					mutex_exit(&ill->ill_lock);
5823 					RELEASE_CONN_LOCK(q);
5824 					rw_exit(&ipst->ips_ill_g_lock);
5825 					return (ipif);
5826 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5827 					ipsq = ill->ill_phyint->phyint_ipsq;
5828 					mutex_enter(&ipsq->ipsq_lock);
5829 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5830 					mutex_exit(&ill->ill_lock);
5831 					rw_exit(&ipst->ips_ill_g_lock);
5832 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5833 					    ill);
5834 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5835 					mutex_exit(&ipsq->ipsq_lock);
5836 					RELEASE_CONN_LOCK(q);
5837 					if (error != NULL)
5838 						*error = EINPROGRESS;
5839 					return (NULL);
5840 				}
5841 			}
5842 		}
5843 		mutex_exit(&ill->ill_lock);
5844 		RELEASE_CONN_LOCK(q);
5845 	}
5846 	rw_exit(&ipst->ips_ill_g_lock);
5847 
5848 	/* lookup the ipif based on interface address */
5849 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5850 	    ipst);
5851 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5852 	return (ipif);
5853 }
5854 
5855 /*
5856  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5857  */
5858 static ipif_t *
5859 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5860     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5861     ip_stack_t *ipst)
5862 {
5863 	ipif_t  *ipif;
5864 	ill_t   *ill;
5865 	boolean_t ptp = B_FALSE;
5866 	ipsq_t	*ipsq;
5867 	ill_walk_context_t	ctx;
5868 
5869 	if (error != NULL)
5870 		*error = 0;
5871 
5872 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5873 	/*
5874 	 * Repeat twice, first based on local addresses and
5875 	 * next time for pointopoint.
5876 	 */
5877 repeat:
5878 	ill = ILL_START_WALK_V4(&ctx, ipst);
5879 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5880 		if (match_ill != NULL && ill != match_ill &&
5881 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5882 			continue;
5883 		}
5884 		GRAB_CONN_LOCK(q);
5885 		mutex_enter(&ill->ill_lock);
5886 		for (ipif = ill->ill_ipif; ipif != NULL;
5887 		    ipif = ipif->ipif_next) {
5888 			if (zoneid != ALL_ZONES &&
5889 			    zoneid != ipif->ipif_zoneid &&
5890 			    ipif->ipif_zoneid != ALL_ZONES)
5891 				continue;
5892 			/* Allow the ipif to be down */
5893 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5894 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5895 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5896 			    (ipif->ipif_pp_dst_addr == addr))) {
5897 				/*
5898 				 * The block comment at the start of ipif_down
5899 				 * explains the use of the macros used below
5900 				 */
5901 				if (IPIF_CAN_LOOKUP(ipif)) {
5902 					ipif_refhold_locked(ipif);
5903 					mutex_exit(&ill->ill_lock);
5904 					RELEASE_CONN_LOCK(q);
5905 					rw_exit(&ipst->ips_ill_g_lock);
5906 					return (ipif);
5907 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5908 					ipsq = ill->ill_phyint->phyint_ipsq;
5909 					mutex_enter(&ipsq->ipsq_lock);
5910 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5911 					mutex_exit(&ill->ill_lock);
5912 					rw_exit(&ipst->ips_ill_g_lock);
5913 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5914 					    ill);
5915 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5916 					mutex_exit(&ipsq->ipsq_lock);
5917 					RELEASE_CONN_LOCK(q);
5918 					if (error != NULL)
5919 						*error = EINPROGRESS;
5920 					return (NULL);
5921 				}
5922 			}
5923 		}
5924 		mutex_exit(&ill->ill_lock);
5925 		RELEASE_CONN_LOCK(q);
5926 	}
5927 
5928 	/* If we already did the ptp case, then we are done */
5929 	if (ptp) {
5930 		rw_exit(&ipst->ips_ill_g_lock);
5931 		if (error != NULL)
5932 			*error = ENXIO;
5933 		return (NULL);
5934 	}
5935 	ptp = B_TRUE;
5936 	goto repeat;
5937 }
5938 
5939 /*
5940  * Check if the address exists in the system.
5941  * We don't hold the conn_lock as we will not perform defered ipsqueue
5942  * operation.
5943  */
5944 boolean_t
5945 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5946 {
5947 	ipif_t  *ipif;
5948 	ill_t   *ill;
5949 	ill_walk_context_t	ctx;
5950 
5951 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5952 
5953 	ill = ILL_START_WALK_V4(&ctx, ipst);
5954 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5955 		mutex_enter(&ill->ill_lock);
5956 		for (ipif = ill->ill_ipif; ipif != NULL;
5957 		    ipif = ipif->ipif_next) {
5958 			if (zoneid != ALL_ZONES &&
5959 			    zoneid != ipif->ipif_zoneid &&
5960 			    ipif->ipif_zoneid != ALL_ZONES)
5961 				continue;
5962 			/* Allow the ipif to be down */
5963 			/*
5964 			 * XXX Different from ipif_lookup_addr(), we don't do
5965 			 * twice lookups. As from bind()'s point of view, we
5966 			 * may return once we find a match.
5967 			 */
5968 			if (((ipif->ipif_lcl_addr == addr) &&
5969 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5970 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5971 			    (ipif->ipif_pp_dst_addr == addr))) {
5972 				/*
5973 				 * Allow bind() to be successful even if the
5974 				 * ipif is with IPIF_CHANGING bit set.
5975 				 */
5976 				mutex_exit(&ill->ill_lock);
5977 				rw_exit(&ipst->ips_ill_g_lock);
5978 				return (B_TRUE);
5979 			}
5980 		}
5981 		mutex_exit(&ill->ill_lock);
5982 	}
5983 
5984 	rw_exit(&ipst->ips_ill_g_lock);
5985 	return (B_FALSE);
5986 }
5987 
5988 /*
5989  * Lookup an ipif with the specified address.  For point-to-point links we
5990  * look for matches on either the destination address or the local address,
5991  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5992  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5993  * (or illgrp if `match_ill' is in an IPMP group).
5994  */
5995 ipif_t *
5996 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5997     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5998 {
5999 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
6000 	    func, error, ipst));
6001 }
6002 
6003 /*
6004  * Special abbreviated version of ipif_lookup_addr() that doesn't match
6005  * `match_ill' across the IPMP group.  This function is only needed in some
6006  * corner-cases; almost everything should use ipif_lookup_addr().
6007  */
6008 static ipif_t *
6009 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6010 {
6011 	ASSERT(match_ill != NULL);
6012 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
6013 	    NULL, NULL, NULL, NULL, ipst));
6014 }
6015 
6016 /*
6017  * Look for an ipif with the specified address. For point-point links
6018  * we look for matches on either the destination address and the local
6019  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6020  * is set.
6021  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
6022  * ill (or illgrp if `match_ill' is in an IPMP group).
6023  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6024  */
6025 zoneid_t
6026 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6027 {
6028 	zoneid_t zoneid;
6029 	ipif_t  *ipif;
6030 	ill_t   *ill;
6031 	boolean_t ptp = B_FALSE;
6032 	ill_walk_context_t	ctx;
6033 
6034 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6035 	/*
6036 	 * Repeat twice, first based on local addresses and
6037 	 * next time for pointopoint.
6038 	 */
6039 repeat:
6040 	ill = ILL_START_WALK_V4(&ctx, ipst);
6041 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6042 		if (match_ill != NULL && ill != match_ill &&
6043 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
6044 			continue;
6045 		}
6046 		mutex_enter(&ill->ill_lock);
6047 		for (ipif = ill->ill_ipif; ipif != NULL;
6048 		    ipif = ipif->ipif_next) {
6049 			/* Allow the ipif to be down */
6050 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6051 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6052 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6053 			    (ipif->ipif_pp_dst_addr == addr)) &&
6054 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6055 				zoneid = ipif->ipif_zoneid;
6056 				mutex_exit(&ill->ill_lock);
6057 				rw_exit(&ipst->ips_ill_g_lock);
6058 				/*
6059 				 * If ipif_zoneid was ALL_ZONES then we have
6060 				 * a trusted extensions shared IP address.
6061 				 * In that case GLOBAL_ZONEID works to send.
6062 				 */
6063 				if (zoneid == ALL_ZONES)
6064 					zoneid = GLOBAL_ZONEID;
6065 				return (zoneid);
6066 			}
6067 		}
6068 		mutex_exit(&ill->ill_lock);
6069 	}
6070 
6071 	/* If we already did the ptp case, then we are done */
6072 	if (ptp) {
6073 		rw_exit(&ipst->ips_ill_g_lock);
6074 		return (ALL_ZONES);
6075 	}
6076 	ptp = B_TRUE;
6077 	goto repeat;
6078 }
6079 
6080 /*
6081  * Look for an ipif that matches the specified remote address i.e. the
6082  * ipif that would receive the specified packet.
6083  * First look for directly connected interfaces and then do a recursive
6084  * IRE lookup and pick the first ipif corresponding to the source address in the
6085  * ire.
6086  * Returns: held ipif
6087  */
6088 ipif_t *
6089 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6090 {
6091 	ipif_t	*ipif;
6092 	ire_t	*ire;
6093 	ip_stack_t	*ipst = ill->ill_ipst;
6094 
6095 	ASSERT(!ill->ill_isv6);
6096 
6097 	/*
6098 	 * Someone could be changing this ipif currently or change it
6099 	 * after we return this. Thus  a few packets could use the old
6100 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6101 	 * will atomically be updated or cleaned up with the new value
6102 	 * Thus we don't need a lock to check the flags or other attrs below.
6103 	 */
6104 	mutex_enter(&ill->ill_lock);
6105 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6106 		if (!IPIF_CAN_LOOKUP(ipif))
6107 			continue;
6108 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6109 		    ipif->ipif_zoneid != ALL_ZONES)
6110 			continue;
6111 		/* Allow the ipif to be down */
6112 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6113 			if ((ipif->ipif_pp_dst_addr == addr) ||
6114 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6115 			    ipif->ipif_lcl_addr == addr)) {
6116 				ipif_refhold_locked(ipif);
6117 				mutex_exit(&ill->ill_lock);
6118 				return (ipif);
6119 			}
6120 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6121 			ipif_refhold_locked(ipif);
6122 			mutex_exit(&ill->ill_lock);
6123 			return (ipif);
6124 		}
6125 	}
6126 	mutex_exit(&ill->ill_lock);
6127 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6128 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6129 	if (ire != NULL) {
6130 		/*
6131 		 * The callers of this function wants to know the
6132 		 * interface on which they have to send the replies
6133 		 * back. For IREs that have ire_stq and ire_ipif
6134 		 * derived from different ills, we really don't care
6135 		 * what we return here.
6136 		 */
6137 		ipif = ire->ire_ipif;
6138 		if (ipif != NULL) {
6139 			ipif_refhold(ipif);
6140 			ire_refrele(ire);
6141 			return (ipif);
6142 		}
6143 		ire_refrele(ire);
6144 	}
6145 	/* Pick the first interface */
6146 	ipif = ipif_get_next_ipif(NULL, ill);
6147 	return (ipif);
6148 }
6149 
6150 /*
6151  * This func does not prevent refcnt from increasing. But if
6152  * the caller has taken steps to that effect, then this func
6153  * can be used to determine whether the ill has become quiescent
6154  */
6155 static boolean_t
6156 ill_is_quiescent(ill_t *ill)
6157 {
6158 	ipif_t	*ipif;
6159 
6160 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6161 
6162 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6163 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6164 			return (B_FALSE);
6165 		}
6166 	}
6167 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6168 		return (B_FALSE);
6169 	}
6170 	return (B_TRUE);
6171 }
6172 
6173 boolean_t
6174 ill_is_freeable(ill_t *ill)
6175 {
6176 	ipif_t	*ipif;
6177 
6178 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6179 
6180 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6181 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6182 			return (B_FALSE);
6183 		}
6184 	}
6185 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6186 		return (B_FALSE);
6187 	}
6188 	return (B_TRUE);
6189 }
6190 
6191 /*
6192  * This func does not prevent refcnt from increasing. But if
6193  * the caller has taken steps to that effect, then this func
6194  * can be used to determine whether the ipif has become quiescent
6195  */
6196 static boolean_t
6197 ipif_is_quiescent(ipif_t *ipif)
6198 {
6199 	ill_t *ill;
6200 
6201 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6202 
6203 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6204 		return (B_FALSE);
6205 	}
6206 
6207 	ill = ipif->ipif_ill;
6208 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6209 	    ill->ill_logical_down) {
6210 		return (B_TRUE);
6211 	}
6212 
6213 	/* This is the last ipif going down or being deleted on this ill */
6214 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6215 		return (B_FALSE);
6216 	}
6217 
6218 	return (B_TRUE);
6219 }
6220 
6221 /*
6222  * return true if the ipif can be destroyed: the ipif has to be quiescent
6223  * with zero references from ire/nce/ilm to it.
6224  */
6225 static boolean_t
6226 ipif_is_freeable(ipif_t *ipif)
6227 {
6228 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6229 	ASSERT(ipif->ipif_id != 0);
6230 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6231 }
6232 
6233 /*
6234  * The ipif/ill/ire has been refreled. Do the tail processing.
6235  * Determine if the ipif or ill in question has become quiescent and if so
6236  * wakeup close and/or restart any queued pending ioctl that is waiting
6237  * for the ipif_down (or ill_down)
6238  */
6239 void
6240 ipif_ill_refrele_tail(ill_t *ill)
6241 {
6242 	mblk_t	*mp;
6243 	conn_t	*connp;
6244 	ipsq_t	*ipsq;
6245 	ipxop_t	*ipx;
6246 	ipif_t	*ipif;
6247 	dl_notify_ind_t *dlindp;
6248 
6249 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6250 
6251 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6252 		/* ip_modclose() may be waiting */
6253 		cv_broadcast(&ill->ill_cv);
6254 	}
6255 
6256 	ipsq = ill->ill_phyint->phyint_ipsq;
6257 	mutex_enter(&ipsq->ipsq_lock);
6258 	ipx = ipsq->ipsq_xop;
6259 	mutex_enter(&ipx->ipx_lock);
6260 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6261 		goto unlock;
6262 
6263 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6264 
6265 	ipif = ipx->ipx_pending_ipif;
6266 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6267 		goto unlock;
6268 
6269 	switch (ipx->ipx_waitfor) {
6270 	case IPIF_DOWN:
6271 		if (!ipif_is_quiescent(ipif))
6272 			goto unlock;
6273 		break;
6274 	case IPIF_FREE:
6275 		if (!ipif_is_freeable(ipif))
6276 			goto unlock;
6277 		break;
6278 	case ILL_DOWN:
6279 		if (!ill_is_quiescent(ill))
6280 			goto unlock;
6281 		break;
6282 	case ILL_FREE:
6283 		/*
6284 		 * ILL_FREE is only for loopback; normal ill teardown waits
6285 		 * synchronously in ip_modclose() without using ipx_waitfor,
6286 		 * handled by the cv_broadcast() at the top of this function.
6287 		 */
6288 		if (!ill_is_freeable(ill))
6289 			goto unlock;
6290 		break;
6291 	default:
6292 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6293 		    (void *)ipsq, ipx->ipx_waitfor);
6294 	}
6295 
6296 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6297 	mutex_exit(&ipx->ipx_lock);
6298 	mp = ipsq_pending_mp_get(ipsq, &connp);
6299 	mutex_exit(&ipsq->ipsq_lock);
6300 	mutex_exit(&ill->ill_lock);
6301 
6302 	ASSERT(mp != NULL);
6303 	/*
6304 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6305 	 * we can only get here when the current operation decides it
6306 	 * it needs to quiesce via ipsq_pending_mp_add().
6307 	 */
6308 	switch (mp->b_datap->db_type) {
6309 	case M_PCPROTO:
6310 	case M_PROTO:
6311 		/*
6312 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6313 		 */
6314 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6315 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6316 
6317 		switch (dlindp->dl_notification) {
6318 		case DL_NOTE_PHYS_ADDR:
6319 			qwriter_ip(ill, ill->ill_rq, mp,
6320 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6321 			return;
6322 		default:
6323 			ASSERT(0);
6324 			ill_refrele(ill);
6325 		}
6326 		break;
6327 
6328 	case M_ERROR:
6329 	case M_HANGUP:
6330 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6331 		    B_TRUE);
6332 		return;
6333 
6334 	case M_IOCTL:
6335 	case M_IOCDATA:
6336 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6337 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6338 		return;
6339 
6340 	default:
6341 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6342 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6343 	}
6344 	return;
6345 unlock:
6346 	mutex_exit(&ipsq->ipsq_lock);
6347 	mutex_exit(&ipx->ipx_lock);
6348 	mutex_exit(&ill->ill_lock);
6349 }
6350 
6351 #ifdef DEBUG
6352 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6353 static void
6354 th_trace_rrecord(th_trace_t *th_trace)
6355 {
6356 	tr_buf_t *tr_buf;
6357 	uint_t lastref;
6358 
6359 	lastref = th_trace->th_trace_lastref;
6360 	lastref++;
6361 	if (lastref == TR_BUF_MAX)
6362 		lastref = 0;
6363 	th_trace->th_trace_lastref = lastref;
6364 	tr_buf = &th_trace->th_trbuf[lastref];
6365 	tr_buf->tr_time = lbolt;
6366 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6367 }
6368 
6369 static void
6370 th_trace_free(void *value)
6371 {
6372 	th_trace_t *th_trace = value;
6373 
6374 	ASSERT(th_trace->th_refcnt == 0);
6375 	kmem_free(th_trace, sizeof (*th_trace));
6376 }
6377 
6378 /*
6379  * Find or create the per-thread hash table used to track object references.
6380  * The ipst argument is NULL if we shouldn't allocate.
6381  *
6382  * Accesses per-thread data, so there's no need to lock here.
6383  */
6384 static mod_hash_t *
6385 th_trace_gethash(ip_stack_t *ipst)
6386 {
6387 	th_hash_t *thh;
6388 
6389 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6390 		mod_hash_t *mh;
6391 		char name[256];
6392 		size_t objsize, rshift;
6393 		int retv;
6394 
6395 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6396 			return (NULL);
6397 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6398 		    (void *)curthread);
6399 
6400 		/*
6401 		 * We use mod_hash_create_extended here rather than the more
6402 		 * obvious mod_hash_create_ptrhash because the latter has a
6403 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6404 		 * block.
6405 		 */
6406 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6407 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6408 		rshift = highbit(objsize);
6409 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6410 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6411 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6412 		if (mh == NULL) {
6413 			kmem_free(thh, sizeof (*thh));
6414 			return (NULL);
6415 		}
6416 		thh->thh_hash = mh;
6417 		thh->thh_ipst = ipst;
6418 		/*
6419 		 * We trace ills, ipifs, ires, and nces.  All of these are
6420 		 * per-IP-stack, so the lock on the thread list is as well.
6421 		 */
6422 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6423 		list_insert_tail(&ip_thread_list, thh);
6424 		rw_exit(&ip_thread_rwlock);
6425 		retv = tsd_set(ip_thread_data, thh);
6426 		ASSERT(retv == 0);
6427 	}
6428 	return (thh != NULL ? thh->thh_hash : NULL);
6429 }
6430 
6431 boolean_t
6432 th_trace_ref(const void *obj, ip_stack_t *ipst)
6433 {
6434 	th_trace_t *th_trace;
6435 	mod_hash_t *mh;
6436 	mod_hash_val_t val;
6437 
6438 	if ((mh = th_trace_gethash(ipst)) == NULL)
6439 		return (B_FALSE);
6440 
6441 	/*
6442 	 * Attempt to locate the trace buffer for this obj and thread.
6443 	 * If it does not exist, then allocate a new trace buffer and
6444 	 * insert into the hash.
6445 	 */
6446 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6447 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6448 		if (th_trace == NULL)
6449 			return (B_FALSE);
6450 
6451 		th_trace->th_id = curthread;
6452 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6453 		    (mod_hash_val_t)th_trace) != 0) {
6454 			kmem_free(th_trace, sizeof (th_trace_t));
6455 			return (B_FALSE);
6456 		}
6457 	} else {
6458 		th_trace = (th_trace_t *)val;
6459 	}
6460 
6461 	ASSERT(th_trace->th_refcnt >= 0 &&
6462 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6463 
6464 	th_trace->th_refcnt++;
6465 	th_trace_rrecord(th_trace);
6466 	return (B_TRUE);
6467 }
6468 
6469 /*
6470  * For the purpose of tracing a reference release, we assume that global
6471  * tracing is always on and that the same thread initiated the reference hold
6472  * is releasing.
6473  */
6474 void
6475 th_trace_unref(const void *obj)
6476 {
6477 	int retv;
6478 	mod_hash_t *mh;
6479 	th_trace_t *th_trace;
6480 	mod_hash_val_t val;
6481 
6482 	mh = th_trace_gethash(NULL);
6483 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6484 	ASSERT(retv == 0);
6485 	th_trace = (th_trace_t *)val;
6486 
6487 	ASSERT(th_trace->th_refcnt > 0);
6488 	th_trace->th_refcnt--;
6489 	th_trace_rrecord(th_trace);
6490 }
6491 
6492 /*
6493  * If tracing has been disabled, then we assume that the reference counts are
6494  * now useless, and we clear them out before destroying the entries.
6495  */
6496 void
6497 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6498 {
6499 	th_hash_t	*thh;
6500 	mod_hash_t	*mh;
6501 	mod_hash_val_t	val;
6502 	th_trace_t	*th_trace;
6503 	int		retv;
6504 
6505 	rw_enter(&ip_thread_rwlock, RW_READER);
6506 	for (thh = list_head(&ip_thread_list); thh != NULL;
6507 	    thh = list_next(&ip_thread_list, thh)) {
6508 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6509 		    &val) == 0) {
6510 			th_trace = (th_trace_t *)val;
6511 			if (trace_disable)
6512 				th_trace->th_refcnt = 0;
6513 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6514 			ASSERT(retv == 0);
6515 		}
6516 	}
6517 	rw_exit(&ip_thread_rwlock);
6518 }
6519 
6520 void
6521 ipif_trace_ref(ipif_t *ipif)
6522 {
6523 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6524 
6525 	if (ipif->ipif_trace_disable)
6526 		return;
6527 
6528 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6529 		ipif->ipif_trace_disable = B_TRUE;
6530 		ipif_trace_cleanup(ipif);
6531 	}
6532 }
6533 
6534 void
6535 ipif_untrace_ref(ipif_t *ipif)
6536 {
6537 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6538 
6539 	if (!ipif->ipif_trace_disable)
6540 		th_trace_unref(ipif);
6541 }
6542 
6543 void
6544 ill_trace_ref(ill_t *ill)
6545 {
6546 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6547 
6548 	if (ill->ill_trace_disable)
6549 		return;
6550 
6551 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6552 		ill->ill_trace_disable = B_TRUE;
6553 		ill_trace_cleanup(ill);
6554 	}
6555 }
6556 
6557 void
6558 ill_untrace_ref(ill_t *ill)
6559 {
6560 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6561 
6562 	if (!ill->ill_trace_disable)
6563 		th_trace_unref(ill);
6564 }
6565 
6566 /*
6567  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6568  * failure, ipif_trace_disable is set.
6569  */
6570 static void
6571 ipif_trace_cleanup(const ipif_t *ipif)
6572 {
6573 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6574 }
6575 
6576 /*
6577  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6578  * failure, ill_trace_disable is set.
6579  */
6580 static void
6581 ill_trace_cleanup(const ill_t *ill)
6582 {
6583 	th_trace_cleanup(ill, ill->ill_trace_disable);
6584 }
6585 #endif /* DEBUG */
6586 
6587 void
6588 ipif_refhold_locked(ipif_t *ipif)
6589 {
6590 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6591 	ipif->ipif_refcnt++;
6592 	IPIF_TRACE_REF(ipif);
6593 }
6594 
6595 void
6596 ipif_refhold(ipif_t *ipif)
6597 {
6598 	ill_t	*ill;
6599 
6600 	ill = ipif->ipif_ill;
6601 	mutex_enter(&ill->ill_lock);
6602 	ipif->ipif_refcnt++;
6603 	IPIF_TRACE_REF(ipif);
6604 	mutex_exit(&ill->ill_lock);
6605 }
6606 
6607 /*
6608  * Must not be called while holding any locks. Otherwise if this is
6609  * the last reference to be released there is a chance of recursive mutex
6610  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6611  * to restart an ioctl.
6612  */
6613 void
6614 ipif_refrele(ipif_t *ipif)
6615 {
6616 	ill_t	*ill;
6617 
6618 	ill = ipif->ipif_ill;
6619 
6620 	mutex_enter(&ill->ill_lock);
6621 	ASSERT(ipif->ipif_refcnt != 0);
6622 	ipif->ipif_refcnt--;
6623 	IPIF_UNTRACE_REF(ipif);
6624 	if (ipif->ipif_refcnt != 0) {
6625 		mutex_exit(&ill->ill_lock);
6626 		return;
6627 	}
6628 
6629 	/* Drops the ill_lock */
6630 	ipif_ill_refrele_tail(ill);
6631 }
6632 
6633 ipif_t *
6634 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6635 {
6636 	ipif_t	*ipif;
6637 
6638 	mutex_enter(&ill->ill_lock);
6639 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6640 	    ipif != NULL; ipif = ipif->ipif_next) {
6641 		if (!IPIF_CAN_LOOKUP(ipif))
6642 			continue;
6643 		ipif_refhold_locked(ipif);
6644 		mutex_exit(&ill->ill_lock);
6645 		return (ipif);
6646 	}
6647 	mutex_exit(&ill->ill_lock);
6648 	return (NULL);
6649 }
6650 
6651 /*
6652  * TODO: make this table extendible at run time
6653  * Return a pointer to the mac type info for 'mac_type'
6654  */
6655 static ip_m_t *
6656 ip_m_lookup(t_uscalar_t mac_type)
6657 {
6658 	ip_m_t	*ipm;
6659 
6660 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6661 		if (ipm->ip_m_mac_type == mac_type)
6662 			return (ipm);
6663 	return (NULL);
6664 }
6665 
6666 /*
6667  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6668  * ipif_arg is passed in to associate it with the correct interface.
6669  * We may need to restart this operation if the ipif cannot be looked up
6670  * due to an exclusive operation that is currently in progress. The restart
6671  * entry point is specified by 'func'
6672  */
6673 int
6674 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6675     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6676     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6677     struct rtsa_s *sp, ip_stack_t *ipst)
6678 {
6679 	ire_t	*ire;
6680 	ire_t	*gw_ire = NULL;
6681 	ipif_t	*ipif = NULL;
6682 	boolean_t ipif_refheld = B_FALSE;
6683 	uint_t	type;
6684 	int	match_flags = MATCH_IRE_TYPE;
6685 	int	error;
6686 	tsol_gc_t *gc = NULL;
6687 	tsol_gcgrp_t *gcgrp = NULL;
6688 	boolean_t gcgrp_xtraref = B_FALSE;
6689 
6690 	ip1dbg(("ip_rt_add:"));
6691 
6692 	if (ire_arg != NULL)
6693 		*ire_arg = NULL;
6694 
6695 	/*
6696 	 * If this is the case of RTF_HOST being set, then we set the netmask
6697 	 * to all ones (regardless if one was supplied).
6698 	 */
6699 	if (flags & RTF_HOST)
6700 		mask = IP_HOST_MASK;
6701 
6702 	/*
6703 	 * Prevent routes with a zero gateway from being created (since
6704 	 * interfaces can currently be plumbed and brought up no assigned
6705 	 * address).
6706 	 */
6707 	if (gw_addr == 0)
6708 		return (ENETUNREACH);
6709 	/*
6710 	 * Get the ipif, if any, corresponding to the gw_addr
6711 	 */
6712 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6713 	    ipst);
6714 	if (ipif != NULL) {
6715 		if (IS_VNI(ipif->ipif_ill)) {
6716 			ipif_refrele(ipif);
6717 			return (EINVAL);
6718 		}
6719 		ipif_refheld = B_TRUE;
6720 	} else if (error == EINPROGRESS) {
6721 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6722 		return (EINPROGRESS);
6723 	} else {
6724 		error = 0;
6725 	}
6726 
6727 	if (ipif != NULL) {
6728 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6729 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6730 	} else {
6731 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6732 	}
6733 
6734 	/*
6735 	 * GateD will attempt to create routes with a loopback interface
6736 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6737 	 * these routes to be added, but create them as interface routes
6738 	 * since the gateway is an interface address.
6739 	 */
6740 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6741 		flags &= ~RTF_GATEWAY;
6742 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6743 		    mask == IP_HOST_MASK) {
6744 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6745 			    ALL_ZONES, NULL, match_flags, ipst);
6746 			if (ire != NULL) {
6747 				ire_refrele(ire);
6748 				if (ipif_refheld)
6749 					ipif_refrele(ipif);
6750 				return (EEXIST);
6751 			}
6752 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6753 			    "for 0x%x\n", (void *)ipif,
6754 			    ipif->ipif_ire_type,
6755 			    ntohl(ipif->ipif_lcl_addr)));
6756 			ire = ire_create(
6757 			    (uchar_t *)&dst_addr,	/* dest address */
6758 			    (uchar_t *)&mask,		/* mask */
6759 			    (uchar_t *)&ipif->ipif_src_addr,
6760 			    NULL,			/* no gateway */
6761 			    &ipif->ipif_mtu,
6762 			    NULL,
6763 			    ipif->ipif_rq,		/* recv-from queue */
6764 			    NULL,			/* no send-to queue */
6765 			    ipif->ipif_ire_type,	/* LOOPBACK */
6766 			    ipif,
6767 			    0,
6768 			    0,
6769 			    0,
6770 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6771 			    RTF_PRIVATE : 0,
6772 			    &ire_uinfo_null,
6773 			    NULL,
6774 			    NULL,
6775 			    ipst);
6776 
6777 			if (ire == NULL) {
6778 				if (ipif_refheld)
6779 					ipif_refrele(ipif);
6780 				return (ENOMEM);
6781 			}
6782 			error = ire_add(&ire, q, mp, func, B_FALSE);
6783 			if (error == 0)
6784 				goto save_ire;
6785 			if (ipif_refheld)
6786 				ipif_refrele(ipif);
6787 			return (error);
6788 
6789 		}
6790 	}
6791 
6792 	/*
6793 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6794 	 * and the gateway address provided is one of the system's interface
6795 	 * addresses.  By using the routing socket interface and supplying an
6796 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6797 	 * specifying an interface route to be created is available which uses
6798 	 * the interface index that specifies the outgoing interface rather than
6799 	 * the address of an outgoing interface (which may not be able to
6800 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6801 	 * flag, routes can be specified which not only specify the next-hop to
6802 	 * be used when routing to a certain prefix, but also which outgoing
6803 	 * interface should be used.
6804 	 *
6805 	 * Previously, interfaces would have unique addresses assigned to them
6806 	 * and so the address assigned to a particular interface could be used
6807 	 * to identify a particular interface.  One exception to this was the
6808 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6809 	 *
6810 	 * With the advent of IPv6 and its link-local addresses, this
6811 	 * restriction was relaxed and interfaces could share addresses between
6812 	 * themselves.  In fact, typically all of the link-local interfaces on
6813 	 * an IPv6 node or router will have the same link-local address.  In
6814 	 * order to differentiate between these interfaces, the use of an
6815 	 * interface index is necessary and this index can be carried inside a
6816 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6817 	 * of using the interface index, however, is that all of the ipif's that
6818 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6819 	 * cannot be used to differentiate between ipif's (or logical
6820 	 * interfaces) that belong to the same ill (physical interface).
6821 	 *
6822 	 * For example, in the following case involving IPv4 interfaces and
6823 	 * logical interfaces
6824 	 *
6825 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6826 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6827 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6828 	 *
6829 	 * the ipif's corresponding to each of these interface routes can be
6830 	 * uniquely identified by the "gateway" (actually interface address).
6831 	 *
6832 	 * In this case involving multiple IPv6 default routes to a particular
6833 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6834 	 * default route is of interest:
6835 	 *
6836 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6837 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6838 	 */
6839 
6840 	/* RTF_GATEWAY not set */
6841 	if (!(flags & RTF_GATEWAY)) {
6842 		queue_t	*stq;
6843 
6844 		if (sp != NULL) {
6845 			ip2dbg(("ip_rt_add: gateway security attributes "
6846 			    "cannot be set with interface route\n"));
6847 			if (ipif_refheld)
6848 				ipif_refrele(ipif);
6849 			return (EINVAL);
6850 		}
6851 
6852 		/*
6853 		 * As the interface index specified with the RTA_IFP sockaddr is
6854 		 * the same for all ipif's off of an ill, the matching logic
6855 		 * below uses MATCH_IRE_ILL if such an index was specified.
6856 		 * This means that routes sharing the same prefix when added
6857 		 * using a RTA_IFP sockaddr must have distinct interface
6858 		 * indices (namely, they must be on distinct ill's).
6859 		 *
6860 		 * On the other hand, since the gateway address will usually be
6861 		 * different for each ipif on the system, the matching logic
6862 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6863 		 * route.  This means that interface routes for the same prefix
6864 		 * can be created if they belong to distinct ipif's and if a
6865 		 * RTA_IFP sockaddr is not present.
6866 		 */
6867 		if (ipif_arg != NULL) {
6868 			if (ipif_refheld)  {
6869 				ipif_refrele(ipif);
6870 				ipif_refheld = B_FALSE;
6871 			}
6872 			ipif = ipif_arg;
6873 			match_flags |= MATCH_IRE_ILL;
6874 		} else {
6875 			/*
6876 			 * Check the ipif corresponding to the gw_addr
6877 			 */
6878 			if (ipif == NULL)
6879 				return (ENETUNREACH);
6880 			match_flags |= MATCH_IRE_IPIF;
6881 		}
6882 		ASSERT(ipif != NULL);
6883 
6884 		/*
6885 		 * We check for an existing entry at this point.
6886 		 *
6887 		 * Since a netmask isn't passed in via the ioctl interface
6888 		 * (SIOCADDRT), we don't check for a matching netmask in that
6889 		 * case.
6890 		 */
6891 		if (!ioctl_msg)
6892 			match_flags |= MATCH_IRE_MASK;
6893 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6894 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6895 		if (ire != NULL) {
6896 			ire_refrele(ire);
6897 			if (ipif_refheld)
6898 				ipif_refrele(ipif);
6899 			return (EEXIST);
6900 		}
6901 
6902 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6903 		    ? ipif->ipif_rq : ipif->ipif_wq;
6904 
6905 		/*
6906 		 * Create a copy of the IRE_LOOPBACK,
6907 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6908 		 * the modified address and netmask.
6909 		 */
6910 		ire = ire_create(
6911 		    (uchar_t *)&dst_addr,
6912 		    (uint8_t *)&mask,
6913 		    (uint8_t *)&ipif->ipif_src_addr,
6914 		    NULL,
6915 		    &ipif->ipif_mtu,
6916 		    NULL,
6917 		    NULL,
6918 		    stq,
6919 		    ipif->ipif_net_type,
6920 		    ipif,
6921 		    0,
6922 		    0,
6923 		    0,
6924 		    flags,
6925 		    &ire_uinfo_null,
6926 		    NULL,
6927 		    NULL,
6928 		    ipst);
6929 		if (ire == NULL) {
6930 			if (ipif_refheld)
6931 				ipif_refrele(ipif);
6932 			return (ENOMEM);
6933 		}
6934 
6935 		/*
6936 		 * Some software (for example, GateD and Sun Cluster) attempts
6937 		 * to create (what amount to) IRE_PREFIX routes with the
6938 		 * loopback address as the gateway.  This is primarily done to
6939 		 * set up prefixes with the RTF_REJECT flag set (for example,
6940 		 * when generating aggregate routes.)
6941 		 *
6942 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6943 		 * IRE_LOOPBACK, then we map the request into a
6944 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6945 		 * these interface routes, by definition, can only be that.
6946 		 *
6947 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6948 		 * routine, but rather using ire_create() directly.
6949 		 *
6950 		 */
6951 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6952 			ire->ire_type = IRE_IF_NORESOLVER;
6953 			ire->ire_flags |= RTF_BLACKHOLE;
6954 		}
6955 
6956 		error = ire_add(&ire, q, mp, func, B_FALSE);
6957 		if (error == 0)
6958 			goto save_ire;
6959 
6960 		/*
6961 		 * In the result of failure, ire_add() will have already
6962 		 * deleted the ire in question, so there is no need to
6963 		 * do that here.
6964 		 */
6965 		if (ipif_refheld)
6966 			ipif_refrele(ipif);
6967 		return (error);
6968 	}
6969 	if (ipif_refheld) {
6970 		ipif_refrele(ipif);
6971 		ipif_refheld = B_FALSE;
6972 	}
6973 
6974 	/*
6975 	 * Get an interface IRE for the specified gateway.
6976 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6977 	 * gateway, it is currently unreachable and we fail the request
6978 	 * accordingly.
6979 	 */
6980 	ipif = ipif_arg;
6981 	if (ipif_arg != NULL)
6982 		match_flags |= MATCH_IRE_ILL;
6983 again:
6984 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6985 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6986 	if (gw_ire == NULL) {
6987 		/*
6988 		 * With IPMP, we allow host routes to influence in.mpathd's
6989 		 * target selection.  However, if the test addresses are on
6990 		 * their own network, the above lookup will fail since the
6991 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6992 		 * hidden test IREs to be found and try again.
6993 		 */
6994 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6995 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6996 			goto again;
6997 		}
6998 		return (ENETUNREACH);
6999 	}
7000 
7001 	/*
7002 	 * We create one of three types of IREs as a result of this request
7003 	 * based on the netmask.  A netmask of all ones (which is automatically
7004 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7005 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7006 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7007 	 * destination prefix.
7008 	 */
7009 	if (mask == IP_HOST_MASK)
7010 		type = IRE_HOST;
7011 	else if (mask == 0)
7012 		type = IRE_DEFAULT;
7013 	else
7014 		type = IRE_PREFIX;
7015 
7016 	/* check for a duplicate entry */
7017 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7018 	    NULL, ALL_ZONES, 0, NULL,
7019 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7020 	if (ire != NULL) {
7021 		ire_refrele(gw_ire);
7022 		ire_refrele(ire);
7023 		return (EEXIST);
7024 	}
7025 
7026 	/* Security attribute exists */
7027 	if (sp != NULL) {
7028 		tsol_gcgrp_addr_t ga;
7029 
7030 		/* find or create the gateway credentials group */
7031 		ga.ga_af = AF_INET;
7032 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7033 
7034 		/* we hold reference to it upon success */
7035 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7036 		if (gcgrp == NULL) {
7037 			ire_refrele(gw_ire);
7038 			return (ENOMEM);
7039 		}
7040 
7041 		/*
7042 		 * Create and add the security attribute to the group; a
7043 		 * reference to the group is made upon allocating a new
7044 		 * entry successfully.  If it finds an already-existing
7045 		 * entry for the security attribute in the group, it simply
7046 		 * returns it and no new reference is made to the group.
7047 		 */
7048 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7049 		if (gc == NULL) {
7050 			/* release reference held by gcgrp_lookup */
7051 			GCGRP_REFRELE(gcgrp);
7052 			ire_refrele(gw_ire);
7053 			return (ENOMEM);
7054 		}
7055 	}
7056 
7057 	/* Create the IRE. */
7058 	ire = ire_create(
7059 	    (uchar_t *)&dst_addr,		/* dest address */
7060 	    (uchar_t *)&mask,			/* mask */
7061 	    /* src address assigned by the caller? */
7062 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7063 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7064 	    (uchar_t *)&gw_addr,		/* gateway address */
7065 	    &gw_ire->ire_max_frag,
7066 	    NULL,				/* no src nce */
7067 	    NULL,				/* no recv-from queue */
7068 	    NULL,				/* no send-to queue */
7069 	    (ushort_t)type,			/* IRE type */
7070 	    ipif_arg,
7071 	    0,
7072 	    0,
7073 	    0,
7074 	    flags,
7075 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7076 	    gc,					/* security attribute */
7077 	    NULL,
7078 	    ipst);
7079 
7080 	/*
7081 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7082 	 * reference to the 'gcgrp'. We can now release the extra reference
7083 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7084 	 */
7085 	if (gcgrp_xtraref)
7086 		GCGRP_REFRELE(gcgrp);
7087 	if (ire == NULL) {
7088 		if (gc != NULL)
7089 			GC_REFRELE(gc);
7090 		ire_refrele(gw_ire);
7091 		return (ENOMEM);
7092 	}
7093 
7094 	/*
7095 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7096 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7097 	 */
7098 
7099 	/* Add the new IRE. */
7100 	error = ire_add(&ire, q, mp, func, B_FALSE);
7101 	if (error != 0) {
7102 		/*
7103 		 * In the result of failure, ire_add() will have already
7104 		 * deleted the ire in question, so there is no need to
7105 		 * do that here.
7106 		 */
7107 		ire_refrele(gw_ire);
7108 		return (error);
7109 	}
7110 
7111 	if (flags & RTF_MULTIRT) {
7112 		/*
7113 		 * Invoke the CGTP (multirouting) filtering module
7114 		 * to add the dst address in the filtering database.
7115 		 * Replicated inbound packets coming from that address
7116 		 * will be filtered to discard the duplicates.
7117 		 * It is not necessary to call the CGTP filter hook
7118 		 * when the dst address is a broadcast or multicast,
7119 		 * because an IP source address cannot be a broadcast
7120 		 * or a multicast.
7121 		 */
7122 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7123 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7124 		if (ire_dst != NULL) {
7125 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7126 			ire_refrele(ire_dst);
7127 			goto save_ire;
7128 		}
7129 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7130 		    !CLASSD(ire->ire_addr)) {
7131 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7132 			    ipst->ips_netstack->netstack_stackid,
7133 			    ire->ire_addr,
7134 			    ire->ire_gateway_addr,
7135 			    ire->ire_src_addr,
7136 			    gw_ire->ire_src_addr);
7137 			if (res != 0) {
7138 				ire_refrele(gw_ire);
7139 				ire_delete(ire);
7140 				return (res);
7141 			}
7142 		}
7143 	}
7144 
7145 	/*
7146 	 * Now that the prefix IRE entry has been created, delete any
7147 	 * existing gateway IRE cache entries as well as any IRE caches
7148 	 * using the gateway, and force them to be created through
7149 	 * ip_newroute.
7150 	 */
7151 	if (gc != NULL) {
7152 		ASSERT(gcgrp != NULL);
7153 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7154 	}
7155 
7156 save_ire:
7157 	if (gw_ire != NULL) {
7158 		ire_refrele(gw_ire);
7159 	}
7160 	if (ipif != NULL) {
7161 		/*
7162 		 * Save enough information so that we can recreate the IRE if
7163 		 * the interface goes down and then up.  The metrics associated
7164 		 * with the route will be saved as well when rts_setmetrics() is
7165 		 * called after the IRE has been created.  In the case where
7166 		 * memory cannot be allocated, none of this information will be
7167 		 * saved.
7168 		 */
7169 		ipif_save_ire(ipif, ire);
7170 	}
7171 	if (ioctl_msg)
7172 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7173 	if (ire_arg != NULL) {
7174 		/*
7175 		 * Store the ire that was successfully added into where ire_arg
7176 		 * points to so that callers don't have to look it up
7177 		 * themselves (but they are responsible for ire_refrele()ing
7178 		 * the ire when they are finished with it).
7179 		 */
7180 		*ire_arg = ire;
7181 	} else {
7182 		ire_refrele(ire);		/* Held in ire_add */
7183 	}
7184 	if (ipif_refheld)
7185 		ipif_refrele(ipif);
7186 	return (0);
7187 }
7188 
7189 /*
7190  * ip_rt_delete is called to delete an IPv4 route.
7191  * ipif_arg is passed in to associate it with the correct interface.
7192  * We may need to restart this operation if the ipif cannot be looked up
7193  * due to an exclusive operation that is currently in progress. The restart
7194  * entry point is specified by 'func'
7195  */
7196 /* ARGSUSED4 */
7197 int
7198 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7199     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7200     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7201 {
7202 	ire_t	*ire = NULL;
7203 	ipif_t	*ipif;
7204 	boolean_t ipif_refheld = B_FALSE;
7205 	uint_t	type;
7206 	uint_t	match_flags = MATCH_IRE_TYPE;
7207 	int	err = 0;
7208 
7209 	ip1dbg(("ip_rt_delete:"));
7210 	/*
7211 	 * If this is the case of RTF_HOST being set, then we set the netmask
7212 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7213 	 */
7214 	if (flags & RTF_HOST) {
7215 		mask = IP_HOST_MASK;
7216 		match_flags |= MATCH_IRE_MASK;
7217 	} else if (rtm_addrs & RTA_NETMASK) {
7218 		match_flags |= MATCH_IRE_MASK;
7219 	}
7220 
7221 	/*
7222 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7223 	 * we check if the gateway address is one of our interfaces first,
7224 	 * and fall back on RTF_GATEWAY routes.
7225 	 *
7226 	 * This makes it possible to delete an original
7227 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7228 	 *
7229 	 * As the interface index specified with the RTA_IFP sockaddr is the
7230 	 * same for all ipif's off of an ill, the matching logic below uses
7231 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7232 	 * sharing the same prefix and interface index as the the route
7233 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7234 	 * is specified in the request.
7235 	 *
7236 	 * On the other hand, since the gateway address will usually be
7237 	 * different for each ipif on the system, the matching logic
7238 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7239 	 * route.  This means that interface routes for the same prefix can be
7240 	 * uniquely identified if they belong to distinct ipif's and if a
7241 	 * RTA_IFP sockaddr is not present.
7242 	 *
7243 	 * For more detail on specifying routes by gateway address and by
7244 	 * interface index, see the comments in ip_rt_add().
7245 	 */
7246 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7247 	    ipst);
7248 	if (ipif != NULL)
7249 		ipif_refheld = B_TRUE;
7250 	else if (err == EINPROGRESS)
7251 		return (err);
7252 	else
7253 		err = 0;
7254 	if (ipif != NULL) {
7255 		if (ipif_arg != NULL) {
7256 			if (ipif_refheld) {
7257 				ipif_refrele(ipif);
7258 				ipif_refheld = B_FALSE;
7259 			}
7260 			ipif = ipif_arg;
7261 			match_flags |= MATCH_IRE_ILL;
7262 		} else {
7263 			match_flags |= MATCH_IRE_IPIF;
7264 		}
7265 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7266 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7267 			    ALL_ZONES, NULL, match_flags, ipst);
7268 		}
7269 		if (ire == NULL) {
7270 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7271 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7272 			    match_flags, ipst);
7273 		}
7274 	}
7275 
7276 	if (ire == NULL) {
7277 		/*
7278 		 * At this point, the gateway address is not one of our own
7279 		 * addresses or a matching interface route was not found.  We
7280 		 * set the IRE type to lookup based on whether
7281 		 * this is a host route, a default route or just a prefix.
7282 		 *
7283 		 * If an ipif_arg was passed in, then the lookup is based on an
7284 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7285 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7286 		 * set as the route being looked up is not a traditional
7287 		 * interface route.
7288 		 */
7289 		match_flags &= ~MATCH_IRE_IPIF;
7290 		match_flags |= MATCH_IRE_GW;
7291 		if (ipif_arg != NULL)
7292 			match_flags |= MATCH_IRE_ILL;
7293 		if (mask == IP_HOST_MASK)
7294 			type = IRE_HOST;
7295 		else if (mask == 0)
7296 			type = IRE_DEFAULT;
7297 		else
7298 			type = IRE_PREFIX;
7299 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7300 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7301 	}
7302 
7303 	if (ipif_refheld)
7304 		ipif_refrele(ipif);
7305 
7306 	/* ipif is not refheld anymore */
7307 	if (ire == NULL)
7308 		return (ESRCH);
7309 
7310 	if (ire->ire_flags & RTF_MULTIRT) {
7311 		/*
7312 		 * Invoke the CGTP (multirouting) filtering module
7313 		 * to remove the dst address from the filtering database.
7314 		 * Packets coming from that address will no longer be
7315 		 * filtered to remove duplicates.
7316 		 */
7317 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7318 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7319 			    ipst->ips_netstack->netstack_stackid,
7320 			    ire->ire_addr, ire->ire_gateway_addr);
7321 		}
7322 		ip_cgtp_bcast_delete(ire, ipst);
7323 	}
7324 
7325 	ipif = ire->ire_ipif;
7326 	if (ipif != NULL)
7327 		ipif_remove_ire(ipif, ire);
7328 	if (ioctl_msg)
7329 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7330 	ire_delete(ire);
7331 	ire_refrele(ire);
7332 	return (err);
7333 }
7334 
7335 /*
7336  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7337  */
7338 /* ARGSUSED */
7339 int
7340 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7341     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7342 {
7343 	ipaddr_t dst_addr;
7344 	ipaddr_t gw_addr;
7345 	ipaddr_t mask;
7346 	int error = 0;
7347 	mblk_t *mp1;
7348 	struct rtentry *rt;
7349 	ipif_t *ipif = NULL;
7350 	ip_stack_t	*ipst;
7351 
7352 	ASSERT(q->q_next == NULL);
7353 	ipst = CONNQ_TO_IPST(q);
7354 
7355 	ip1dbg(("ip_siocaddrt:"));
7356 	/* Existence of mp1 verified in ip_wput_nondata */
7357 	mp1 = mp->b_cont->b_cont;
7358 	rt = (struct rtentry *)mp1->b_rptr;
7359 
7360 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7361 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7362 
7363 	/*
7364 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7365 	 * to a particular host address.  In this case, we set the netmask to
7366 	 * all ones for the particular destination address.  Otherwise,
7367 	 * determine the netmask to be used based on dst_addr and the interfaces
7368 	 * in use.
7369 	 */
7370 	if (rt->rt_flags & RTF_HOST) {
7371 		mask = IP_HOST_MASK;
7372 	} else {
7373 		/*
7374 		 * Note that ip_subnet_mask returns a zero mask in the case of
7375 		 * default (an all-zeroes address).
7376 		 */
7377 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7378 	}
7379 
7380 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7381 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7382 	if (ipif != NULL)
7383 		ipif_refrele(ipif);
7384 	return (error);
7385 }
7386 
7387 /*
7388  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7389  */
7390 /* ARGSUSED */
7391 int
7392 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7393     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7394 {
7395 	ipaddr_t dst_addr;
7396 	ipaddr_t gw_addr;
7397 	ipaddr_t mask;
7398 	int error;
7399 	mblk_t *mp1;
7400 	struct rtentry *rt;
7401 	ipif_t *ipif = NULL;
7402 	ip_stack_t	*ipst;
7403 
7404 	ASSERT(q->q_next == NULL);
7405 	ipst = CONNQ_TO_IPST(q);
7406 
7407 	ip1dbg(("ip_siocdelrt:"));
7408 	/* Existence of mp1 verified in ip_wput_nondata */
7409 	mp1 = mp->b_cont->b_cont;
7410 	rt = (struct rtentry *)mp1->b_rptr;
7411 
7412 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7413 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7414 
7415 	/*
7416 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7417 	 * to a particular host address.  In this case, we set the netmask to
7418 	 * all ones for the particular destination address.  Otherwise,
7419 	 * determine the netmask to be used based on dst_addr and the interfaces
7420 	 * in use.
7421 	 */
7422 	if (rt->rt_flags & RTF_HOST) {
7423 		mask = IP_HOST_MASK;
7424 	} else {
7425 		/*
7426 		 * Note that ip_subnet_mask returns a zero mask in the case of
7427 		 * default (an all-zeroes address).
7428 		 */
7429 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7430 	}
7431 
7432 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7433 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7434 	    mp, ip_process_ioctl, ipst);
7435 	if (ipif != NULL)
7436 		ipif_refrele(ipif);
7437 	return (error);
7438 }
7439 
7440 /*
7441  * Enqueue the mp onto the ipsq, chained by b_next.
7442  * b_prev stores the function to be executed later, and b_queue the queue
7443  * where this mp originated.
7444  */
7445 void
7446 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7447     ill_t *pending_ill)
7448 {
7449 	conn_t	*connp;
7450 	ipxop_t *ipx = ipsq->ipsq_xop;
7451 
7452 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7453 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7454 	ASSERT(func != NULL);
7455 
7456 	mp->b_queue = q;
7457 	mp->b_prev = (void *)func;
7458 	mp->b_next = NULL;
7459 
7460 	switch (type) {
7461 	case CUR_OP:
7462 		if (ipx->ipx_mptail != NULL) {
7463 			ASSERT(ipx->ipx_mphead != NULL);
7464 			ipx->ipx_mptail->b_next = mp;
7465 		} else {
7466 			ASSERT(ipx->ipx_mphead == NULL);
7467 			ipx->ipx_mphead = mp;
7468 		}
7469 		ipx->ipx_mptail = mp;
7470 		break;
7471 
7472 	case NEW_OP:
7473 		if (ipsq->ipsq_xopq_mptail != NULL) {
7474 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7475 			ipsq->ipsq_xopq_mptail->b_next = mp;
7476 		} else {
7477 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7478 			ipsq->ipsq_xopq_mphead = mp;
7479 		}
7480 		ipsq->ipsq_xopq_mptail = mp;
7481 		ipx->ipx_ipsq_queued = B_TRUE;
7482 		break;
7483 
7484 	case SWITCH_OP:
7485 		ASSERT(ipsq->ipsq_swxop != NULL);
7486 		/* only one switch operation is currently allowed */
7487 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7488 		ipsq->ipsq_switch_mp = mp;
7489 		ipx->ipx_ipsq_queued = B_TRUE;
7490 		break;
7491 	default:
7492 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7493 	}
7494 
7495 	if (CONN_Q(q) && pending_ill != NULL) {
7496 		connp = Q_TO_CONN(q);
7497 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7498 		connp->conn_oper_pending_ill = pending_ill;
7499 	}
7500 }
7501 
7502 /*
7503  * Dequeue the next message that requested exclusive access to this IPSQ's
7504  * xop.  Specifically:
7505  *
7506  *  1. If we're still processing the current operation on `ipsq', then
7507  *     dequeue the next message for the operation (from ipx_mphead), or
7508  *     return NULL if there are no queued messages for the operation.
7509  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7510  *
7511  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7512  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7513  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7514  *     leaving IPMP groups and require a careful dance -- see the comments
7515  *     in-line below for details.  If we're leaving a group xop or if we're
7516  *     joining a group xop and become writer on it, then we proceed to (3).
7517  *     Otherwise, we return NULL and exit the xop.
7518  *
7519  *  3. For each IPSQ in the xop, return any switch operation stored on
7520  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7521  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7522  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7523  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7524  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7525  *     each phyint in the group, including the IPMP meta-interface phyint.
7526  */
7527 static mblk_t *
7528 ipsq_dq(ipsq_t *ipsq)
7529 {
7530 	ill_t	*illv4, *illv6;
7531 	mblk_t	*mp;
7532 	ipsq_t	*xopipsq;
7533 	ipsq_t	*leftipsq = NULL;
7534 	ipxop_t *ipx;
7535 	phyint_t *phyi = ipsq->ipsq_phyint;
7536 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7537 	boolean_t emptied = B_FALSE;
7538 
7539 	/*
7540 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7541 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7542 	 */
7543 	rw_enter(&ipst->ips_ill_g_lock,
7544 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7545 	mutex_enter(&ipsq->ipsq_lock);
7546 	ipx = ipsq->ipsq_xop;
7547 	mutex_enter(&ipx->ipx_lock);
7548 
7549 	/*
7550 	 * Dequeue the next message associated with the current exclusive
7551 	 * operation, if any.
7552 	 */
7553 	if ((mp = ipx->ipx_mphead) != NULL) {
7554 		ipx->ipx_mphead = mp->b_next;
7555 		if (ipx->ipx_mphead == NULL)
7556 			ipx->ipx_mptail = NULL;
7557 		mp->b_next = (void *)ipsq;
7558 		goto out;
7559 	}
7560 
7561 	if (ipx->ipx_current_ipif != NULL)
7562 		goto empty;
7563 
7564 	if (ipsq->ipsq_swxop != NULL) {
7565 		/*
7566 		 * The exclusive operation that is now being completed has
7567 		 * requested a switch to a different xop.  This happens
7568 		 * when an interface joins or leaves an IPMP group.  Joins
7569 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7570 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7571 		 * (phyint_free()), or interface plumb for an ill type
7572 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7573 		 *
7574 		 * Xop switches are not allowed on the IPMP meta-interface.
7575 		 */
7576 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7577 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7578 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7579 
7580 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7581 			/*
7582 			 * We're switching back to our own xop, so we have two
7583 			 * xop's to drain/exit: our own, and the group xop
7584 			 * that we are leaving.
7585 			 *
7586 			 * First, pull ourselves out of the group ipsq list.
7587 			 * This is safe since we're writer on ill_g_lock.
7588 			 */
7589 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7590 
7591 			xopipsq = ipx->ipx_ipsq;
7592 			while (xopipsq->ipsq_next != ipsq)
7593 				xopipsq = xopipsq->ipsq_next;
7594 
7595 			xopipsq->ipsq_next = ipsq->ipsq_next;
7596 			ipsq->ipsq_next = ipsq;
7597 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7598 			ipsq->ipsq_swxop = NULL;
7599 
7600 			/*
7601 			 * Second, prepare to exit the group xop.  The actual
7602 			 * ipsq_exit() is done at the end of this function
7603 			 * since we cannot hold any locks across ipsq_exit().
7604 			 * Note that although we drop the group's ipx_lock, no
7605 			 * threads can proceed since we're still ipx_writer.
7606 			 */
7607 			leftipsq = xopipsq;
7608 			mutex_exit(&ipx->ipx_lock);
7609 
7610 			/*
7611 			 * Third, set ipx to point to our own xop (which was
7612 			 * inactive and therefore can be entered).
7613 			 */
7614 			ipx = ipsq->ipsq_xop;
7615 			mutex_enter(&ipx->ipx_lock);
7616 			ASSERT(ipx->ipx_writer == NULL);
7617 			ASSERT(ipx->ipx_current_ipif == NULL);
7618 		} else {
7619 			/*
7620 			 * We're switching from our own xop to a group xop.
7621 			 * The requestor of the switch must ensure that the
7622 			 * group xop cannot go away (e.g. by ensuring the
7623 			 * phyint associated with the xop cannot go away).
7624 			 *
7625 			 * If we can become writer on our new xop, then we'll
7626 			 * do the drain.  Otherwise, the current writer of our
7627 			 * new xop will do the drain when it exits.
7628 			 *
7629 			 * First, splice ourselves into the group IPSQ list.
7630 			 * This is safe since we're writer on ill_g_lock.
7631 			 */
7632 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7633 
7634 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7635 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7636 				xopipsq = xopipsq->ipsq_next;
7637 
7638 			xopipsq->ipsq_next = ipsq;
7639 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7640 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7641 			ipsq->ipsq_swxop = NULL;
7642 
7643 			/*
7644 			 * Second, exit our own xop, since it's now unused.
7645 			 * This is safe since we've got the only reference.
7646 			 */
7647 			ASSERT(ipx->ipx_writer == curthread);
7648 			ipx->ipx_writer = NULL;
7649 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7650 			ipx->ipx_ipsq_queued = B_FALSE;
7651 			mutex_exit(&ipx->ipx_lock);
7652 
7653 			/*
7654 			 * Third, set ipx to point to our new xop, and check
7655 			 * if we can become writer on it.  If we cannot, then
7656 			 * the current writer will drain the IPSQ group when
7657 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7658 			 * because we're still holding ipsq_lock.
7659 			 */
7660 			ipx = ipsq->ipsq_xop;
7661 			mutex_enter(&ipx->ipx_lock);
7662 			if (ipx->ipx_writer != NULL ||
7663 			    ipx->ipx_current_ipif != NULL) {
7664 				goto out;
7665 			}
7666 		}
7667 
7668 		/*
7669 		 * Fourth, become writer on our new ipx before we continue
7670 		 * with the drain.  Note that we never dropped ipsq_lock
7671 		 * above, so no other thread could've raced with us to
7672 		 * become writer first.  Also, we're holding ipx_lock, so
7673 		 * no other thread can examine the ipx right now.
7674 		 */
7675 		ASSERT(ipx->ipx_current_ipif == NULL);
7676 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7677 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7678 		ipx->ipx_writer = curthread;
7679 		ipx->ipx_forced = B_FALSE;
7680 #ifdef DEBUG
7681 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7682 #endif
7683 	}
7684 
7685 	xopipsq = ipsq;
7686 	do {
7687 		/*
7688 		 * So that other operations operate on a consistent and
7689 		 * complete phyint, a switch message on an IPSQ must be
7690 		 * handled prior to any other operations on that IPSQ.
7691 		 */
7692 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7693 			xopipsq->ipsq_switch_mp = NULL;
7694 			ASSERT(mp->b_next == NULL);
7695 			mp->b_next = (void *)xopipsq;
7696 			goto out;
7697 		}
7698 
7699 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7700 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7701 			if (xopipsq->ipsq_xopq_mphead == NULL)
7702 				xopipsq->ipsq_xopq_mptail = NULL;
7703 			mp->b_next = (void *)xopipsq;
7704 			goto out;
7705 		}
7706 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7707 empty:
7708 	/*
7709 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7710 	 * new messages can end up on any IPSQ in the xop.
7711 	 */
7712 	ipx->ipx_writer = NULL;
7713 	ipx->ipx_forced = B_FALSE;
7714 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7715 	ipx->ipx_ipsq_queued = B_FALSE;
7716 	emptied = B_TRUE;
7717 #ifdef	DEBUG
7718 	ipx->ipx_depth = 0;
7719 #endif
7720 out:
7721 	mutex_exit(&ipx->ipx_lock);
7722 	mutex_exit(&ipsq->ipsq_lock);
7723 
7724 	/*
7725 	 * If we completely emptied the xop, then wake up any threads waiting
7726 	 * to enter any of the IPSQ's associated with it.
7727 	 */
7728 	if (emptied) {
7729 		xopipsq = ipsq;
7730 		do {
7731 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7732 				continue;
7733 
7734 			illv4 = phyi->phyint_illv4;
7735 			illv6 = phyi->phyint_illv6;
7736 
7737 			GRAB_ILL_LOCKS(illv4, illv6);
7738 			if (illv4 != NULL)
7739 				cv_broadcast(&illv4->ill_cv);
7740 			if (illv6 != NULL)
7741 				cv_broadcast(&illv6->ill_cv);
7742 			RELEASE_ILL_LOCKS(illv4, illv6);
7743 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7744 	}
7745 	rw_exit(&ipst->ips_ill_g_lock);
7746 
7747 	/*
7748 	 * Now that all locks are dropped, exit the IPSQ we left.
7749 	 */
7750 	if (leftipsq != NULL)
7751 		ipsq_exit(leftipsq);
7752 
7753 	return (mp);
7754 }
7755 
7756 /*
7757  * Enter the ipsq corresponding to ill, by waiting synchronously till
7758  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7759  * will have to drain completely before ipsq_enter returns success.
7760  * ipx_current_ipif will be set if some exclusive op is in progress,
7761  * and the ipsq_exit logic will start the next enqueued op after
7762  * completion of the current op. If 'force' is used, we don't wait
7763  * for the enqueued ops. This is needed when a conn_close wants to
7764  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7765  * of an ill can also use this option. But we dont' use it currently.
7766  */
7767 #define	ENTER_SQ_WAIT_TICKS 100
7768 boolean_t
7769 ipsq_enter(ill_t *ill, boolean_t force, int type)
7770 {
7771 	ipsq_t	*ipsq;
7772 	ipxop_t *ipx;
7773 	boolean_t waited_enough = B_FALSE;
7774 
7775 	/*
7776 	 * Note that the relationship between ill and ipsq is fixed as long as
7777 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7778 	 * relationship between the IPSQ and xop cannot change.  However,
7779 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7780 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7781 	 * waking up all ills in the xop when it becomes available.
7782 	 */
7783 	mutex_enter(&ill->ill_lock);
7784 	for (;;) {
7785 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7786 			mutex_exit(&ill->ill_lock);
7787 			return (B_FALSE);
7788 		}
7789 
7790 		ipsq = ill->ill_phyint->phyint_ipsq;
7791 		mutex_enter(&ipsq->ipsq_lock);
7792 		ipx = ipsq->ipsq_xop;
7793 		mutex_enter(&ipx->ipx_lock);
7794 
7795 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7796 		    ipx->ipx_current_ipif == NULL || waited_enough))
7797 			break;
7798 
7799 		if (!force || ipx->ipx_writer != NULL) {
7800 			mutex_exit(&ipx->ipx_lock);
7801 			mutex_exit(&ipsq->ipsq_lock);
7802 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7803 		} else {
7804 			mutex_exit(&ipx->ipx_lock);
7805 			mutex_exit(&ipsq->ipsq_lock);
7806 			(void) cv_timedwait(&ill->ill_cv,
7807 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7808 			waited_enough = B_TRUE;
7809 		}
7810 	}
7811 
7812 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7813 	ASSERT(ipx->ipx_reentry_cnt == 0);
7814 	ipx->ipx_writer = curthread;
7815 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7816 	ipx->ipx_reentry_cnt++;
7817 #ifdef DEBUG
7818 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7819 #endif
7820 	mutex_exit(&ipx->ipx_lock);
7821 	mutex_exit(&ipsq->ipsq_lock);
7822 	mutex_exit(&ill->ill_lock);
7823 	return (B_TRUE);
7824 }
7825 
7826 boolean_t
7827 ill_perim_enter(ill_t *ill)
7828 {
7829 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7830 }
7831 
7832 void
7833 ill_perim_exit(ill_t *ill)
7834 {
7835 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7836 }
7837 
7838 /*
7839  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7840  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7841  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7842  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7843  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7844  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7845  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7846  * up the interface) and are enqueued in ipx_mphead.
7847  *
7848  * If a thread does not want to reenter the ipsq when it is already writer,
7849  * it must make sure that the specified reentry point to be called later
7850  * when the ipsq is empty, nor any code path starting from the specified reentry
7851  * point must never ever try to enter the ipsq again. Otherwise it can lead
7852  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7853  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7854  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7855  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7856  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7857  * ioctl if the current ioctl has completed. If the current ioctl is still
7858  * in progress it simply returns. The current ioctl could be waiting for
7859  * a response from another module (arp or the driver or could be waiting for
7860  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7861  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7862  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7863  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7864  * all associated DLPI operations have completed.
7865  */
7866 
7867 /*
7868  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7869  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7870  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7871  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7872  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7873  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7874  */
7875 ipsq_t *
7876 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7877     ipsq_func_t func, int type, boolean_t reentry_ok)
7878 {
7879 	ipsq_t	*ipsq;
7880 	ipxop_t	*ipx;
7881 
7882 	/* Only 1 of ipif or ill can be specified */
7883 	ASSERT((ipif != NULL) ^ (ill != NULL));
7884 	if (ipif != NULL)
7885 		ill = ipif->ipif_ill;
7886 
7887 	/*
7888 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7889 	 * ipx of an ipsq can't change when ipsq_lock is held.
7890 	 */
7891 	GRAB_CONN_LOCK(q);
7892 	mutex_enter(&ill->ill_lock);
7893 	ipsq = ill->ill_phyint->phyint_ipsq;
7894 	mutex_enter(&ipsq->ipsq_lock);
7895 	ipx = ipsq->ipsq_xop;
7896 	mutex_enter(&ipx->ipx_lock);
7897 
7898 	/*
7899 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7900 	 *    (Note: If the caller does not specify reentry_ok then neither
7901 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7902 	 *    again. Otherwise it can lead to an infinite loop
7903 	 * 2. Enter the ipsq if there is no current writer and this attempted
7904 	 *    entry is part of the current operation
7905 	 * 3. Enter the ipsq if there is no current writer and this is a new
7906 	 *    operation and the operation queue is empty and there is no
7907 	 *    operation currently in progress
7908 	 */
7909 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7910 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7911 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7912 		/* Success. */
7913 		ipx->ipx_reentry_cnt++;
7914 		ipx->ipx_writer = curthread;
7915 		ipx->ipx_forced = B_FALSE;
7916 		mutex_exit(&ipx->ipx_lock);
7917 		mutex_exit(&ipsq->ipsq_lock);
7918 		mutex_exit(&ill->ill_lock);
7919 		RELEASE_CONN_LOCK(q);
7920 #ifdef DEBUG
7921 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7922 #endif
7923 		return (ipsq);
7924 	}
7925 
7926 	if (func != NULL)
7927 		ipsq_enq(ipsq, q, mp, func, type, ill);
7928 
7929 	mutex_exit(&ipx->ipx_lock);
7930 	mutex_exit(&ipsq->ipsq_lock);
7931 	mutex_exit(&ill->ill_lock);
7932 	RELEASE_CONN_LOCK(q);
7933 	return (NULL);
7934 }
7935 
7936 /*
7937  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7938  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7939  * cannot be entered, the mp is queued for completion.
7940  */
7941 void
7942 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7943     boolean_t reentry_ok)
7944 {
7945 	ipsq_t	*ipsq;
7946 
7947 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7948 
7949 	/*
7950 	 * Drop the caller's refhold on the ill.  This is safe since we either
7951 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7952 	 * IPSQ, in which case we return without accessing ill anymore.  This
7953 	 * is needed because func needs to see the correct refcount.
7954 	 * e.g. removeif can work only then.
7955 	 */
7956 	ill_refrele(ill);
7957 	if (ipsq != NULL) {
7958 		(*func)(ipsq, q, mp, NULL);
7959 		ipsq_exit(ipsq);
7960 	}
7961 }
7962 
7963 /*
7964  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7965  * prior to exiting.  Caller must be writer on the specified IPSQ.
7966  */
7967 void
7968 ipsq_exit(ipsq_t *ipsq)
7969 {
7970 	mblk_t *mp;
7971 	ipsq_t *mp_ipsq;
7972 	queue_t	*q;
7973 	phyint_t *phyi;
7974 	ipsq_func_t func;
7975 
7976 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7977 
7978 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7979 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7980 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7981 		return;
7982 	}
7983 
7984 	for (;;) {
7985 		phyi = ipsq->ipsq_phyint;
7986 		mp = ipsq_dq(ipsq);
7987 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7988 
7989 		/*
7990 		 * If we've changed to a new IPSQ, and the phyint associated
7991 		 * with the old one has gone away, free the old IPSQ.  Note
7992 		 * that this cannot happen while the IPSQ is in a group.
7993 		 */
7994 		if (mp_ipsq != ipsq && phyi == NULL) {
7995 			ASSERT(ipsq->ipsq_next == ipsq);
7996 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7997 			ipsq_delete(ipsq);
7998 		}
7999 
8000 		if (mp == NULL)
8001 			break;
8002 
8003 		q = mp->b_queue;
8004 		func = (ipsq_func_t)mp->b_prev;
8005 		ipsq = mp_ipsq;
8006 		mp->b_next = mp->b_prev = NULL;
8007 		mp->b_queue = NULL;
8008 
8009 		/*
8010 		 * If 'q' is an conn queue, it is valid, since we did a
8011 		 * a refhold on the conn at the start of the ioctl.
8012 		 * If 'q' is an ill queue, it is valid, since close of an
8013 		 * ill will clean up its IPSQ.
8014 		 */
8015 		(*func)(ipsq, q, mp, NULL);
8016 	}
8017 }
8018 
8019 /*
8020  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8021  * and `ioccmd'.
8022  */
8023 void
8024 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8025 {
8026 	ipxop_t *ipx = ipsq->ipsq_xop;
8027 
8028 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8029 	ASSERT(ipx->ipx_current_ipif == NULL);
8030 	ASSERT(ipx->ipx_current_ioctl == 0);
8031 
8032 	ipx->ipx_current_done = B_FALSE;
8033 	ipx->ipx_current_ioctl = ioccmd;
8034 	mutex_enter(&ipx->ipx_lock);
8035 	ipx->ipx_current_ipif = ipif;
8036 	mutex_exit(&ipx->ipx_lock);
8037 }
8038 
8039 /*
8040  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8041  * the next exclusive operation to begin once we ipsq_exit().  However, if
8042  * pending DLPI operations remain, then we will wait for the queue to drain
8043  * before allowing the next exclusive operation to begin.  This ensures that
8044  * DLPI operations from one exclusive operation are never improperly processed
8045  * as part of a subsequent exclusive operation.
8046  */
8047 void
8048 ipsq_current_finish(ipsq_t *ipsq)
8049 {
8050 	ipxop_t	*ipx = ipsq->ipsq_xop;
8051 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8052 	ipif_t	*ipif = ipx->ipx_current_ipif;
8053 
8054 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8055 
8056 	/*
8057 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
8058 	 * (but in that case, IPIF_CHANGING will already be clear and no
8059 	 * pending DLPI messages can remain).
8060 	 */
8061 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
8062 		ill_t *ill = ipif->ipif_ill;
8063 
8064 		mutex_enter(&ill->ill_lock);
8065 		dlpi_pending = ill->ill_dlpi_pending;
8066 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8067 		mutex_exit(&ill->ill_lock);
8068 	}
8069 
8070 	ASSERT(!ipx->ipx_current_done);
8071 	ipx->ipx_current_done = B_TRUE;
8072 	ipx->ipx_current_ioctl = 0;
8073 	if (dlpi_pending == DL_PRIM_INVAL) {
8074 		mutex_enter(&ipx->ipx_lock);
8075 		ipx->ipx_current_ipif = NULL;
8076 		mutex_exit(&ipx->ipx_lock);
8077 	}
8078 }
8079 
8080 /*
8081  * The ill is closing. Flush all messages on the ipsq that originated
8082  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8083  * for this ill since ipsq_enter could not have entered until then.
8084  * New messages can't be queued since the CONDEMNED flag is set.
8085  */
8086 static void
8087 ipsq_flush(ill_t *ill)
8088 {
8089 	queue_t	*q;
8090 	mblk_t	*prev;
8091 	mblk_t	*mp;
8092 	mblk_t	*mp_next;
8093 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
8094 
8095 	ASSERT(IAM_WRITER_ILL(ill));
8096 
8097 	/*
8098 	 * Flush any messages sent up by the driver.
8099 	 */
8100 	mutex_enter(&ipx->ipx_lock);
8101 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
8102 		mp_next = mp->b_next;
8103 		q = mp->b_queue;
8104 		if (q == ill->ill_rq || q == ill->ill_wq) {
8105 			/* dequeue mp */
8106 			if (prev == NULL)
8107 				ipx->ipx_mphead = mp->b_next;
8108 			else
8109 				prev->b_next = mp->b_next;
8110 			if (ipx->ipx_mptail == mp) {
8111 				ASSERT(mp_next == NULL);
8112 				ipx->ipx_mptail = prev;
8113 			}
8114 			inet_freemsg(mp);
8115 		} else {
8116 			prev = mp;
8117 		}
8118 	}
8119 	mutex_exit(&ipx->ipx_lock);
8120 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8121 	ipsq_xopq_mp_cleanup(ill, NULL);
8122 	ill_pending_mp_cleanup(ill);
8123 }
8124 
8125 /*
8126  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8127  * refhold and return the associated ipif
8128  */
8129 /* ARGSUSED */
8130 int
8131 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8132     cmd_info_t *ci, ipsq_func_t func)
8133 {
8134 	boolean_t exists;
8135 	struct iftun_req *ta;
8136 	ipif_t  *ipif;
8137 	ill_t   *ill;
8138 	boolean_t isv6;
8139 	mblk_t  *mp1;
8140 	int error;
8141 	conn_t  *connp;
8142 	ip_stack_t  *ipst;
8143 
8144 	/* Existence verified in ip_wput_nondata */
8145 	mp1 = mp->b_cont->b_cont;
8146 	ta = (struct iftun_req *)mp1->b_rptr;
8147 	/*
8148 	 * Null terminate the string to protect against buffer
8149 	 * overrun. String was generated by user code and may not
8150 	 * be trusted.
8151 	 */
8152 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8153 
8154 	connp = Q_TO_CONN(q);
8155 	isv6 = connp->conn_af_isv6;
8156 	ipst = connp->conn_netstack->netstack_ip;
8157 
8158 	/* Disallows implicit create */
8159 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8160 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8161 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8162 	if (ipif == NULL)
8163 		return (error);
8164 
8165 	if (ipif->ipif_id != 0) {
8166 		/*
8167 		 * We really don't want to set/get tunnel parameters
8168 		 * on virtual tunnel interfaces.  Only allow the
8169 		 * base tunnel to do these.
8170 		 */
8171 		ipif_refrele(ipif);
8172 		return (EINVAL);
8173 	}
8174 
8175 	/*
8176 	 * Send down to tunnel mod for ioctl processing.
8177 	 * Will finish ioctl in ip_rput_other().
8178 	 */
8179 	ill = ipif->ipif_ill;
8180 	if (ill->ill_net_type == IRE_LOOPBACK) {
8181 		ipif_refrele(ipif);
8182 		return (EOPNOTSUPP);
8183 	}
8184 
8185 	if (ill->ill_wq == NULL) {
8186 		ipif_refrele(ipif);
8187 		return (ENXIO);
8188 	}
8189 	/*
8190 	 * Mark the ioctl as coming from an IPv6 interface for
8191 	 * tun's convenience.
8192 	 */
8193 	if (ill->ill_isv6)
8194 		ta->ifta_flags |= 0x80000000;
8195 	ci->ci_ipif = ipif;
8196 	return (0);
8197 }
8198 
8199 /*
8200  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8201  * and return the associated ipif.
8202  * Return value:
8203  *	Non zero: An error has occurred. ci may not be filled out.
8204  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8205  *	a held ipif in ci.ci_ipif.
8206  */
8207 int
8208 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8209     cmd_info_t *ci, ipsq_func_t func)
8210 {
8211 	char		*name;
8212 	struct ifreq    *ifr;
8213 	struct lifreq    *lifr;
8214 	ipif_t		*ipif = NULL;
8215 	ill_t		*ill;
8216 	conn_t		*connp;
8217 	boolean_t	isv6;
8218 	boolean_t	exists;
8219 	int		err;
8220 	mblk_t		*mp1;
8221 	zoneid_t	zoneid;
8222 	ip_stack_t	*ipst;
8223 
8224 	if (q->q_next != NULL) {
8225 		ill = (ill_t *)q->q_ptr;
8226 		isv6 = ill->ill_isv6;
8227 		connp = NULL;
8228 		zoneid = ALL_ZONES;
8229 		ipst = ill->ill_ipst;
8230 	} else {
8231 		ill = NULL;
8232 		connp = Q_TO_CONN(q);
8233 		isv6 = connp->conn_af_isv6;
8234 		zoneid = connp->conn_zoneid;
8235 		if (zoneid == GLOBAL_ZONEID) {
8236 			/* global zone can access ipifs in all zones */
8237 			zoneid = ALL_ZONES;
8238 		}
8239 		ipst = connp->conn_netstack->netstack_ip;
8240 	}
8241 
8242 	/* Has been checked in ip_wput_nondata */
8243 	mp1 = mp->b_cont->b_cont;
8244 
8245 	if (ipip->ipi_cmd_type == IF_CMD) {
8246 		/* This a old style SIOC[GS]IF* command */
8247 		ifr = (struct ifreq *)mp1->b_rptr;
8248 		/*
8249 		 * Null terminate the string to protect against buffer
8250 		 * overrun. String was generated by user code and may not
8251 		 * be trusted.
8252 		 */
8253 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8254 		name = ifr->ifr_name;
8255 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8256 		ci->ci_sin6 = NULL;
8257 		ci->ci_lifr = (struct lifreq *)ifr;
8258 	} else {
8259 		/* This a new style SIOC[GS]LIF* command */
8260 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8261 		lifr = (struct lifreq *)mp1->b_rptr;
8262 		/*
8263 		 * Null terminate the string to protect against buffer
8264 		 * overrun. String was generated by user code and may not
8265 		 * be trusted.
8266 		 */
8267 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8268 		name = lifr->lifr_name;
8269 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8270 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8271 		ci->ci_lifr = lifr;
8272 	}
8273 
8274 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8275 		/*
8276 		 * The ioctl will be failed if the ioctl comes down
8277 		 * an conn stream
8278 		 */
8279 		if (ill == NULL) {
8280 			/*
8281 			 * Not an ill queue, return EINVAL same as the
8282 			 * old error code.
8283 			 */
8284 			return (ENXIO);
8285 		}
8286 		ipif = ill->ill_ipif;
8287 		ipif_refhold(ipif);
8288 	} else {
8289 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8290 		    &exists, isv6, zoneid,
8291 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8292 		    ipst);
8293 		if (ipif == NULL) {
8294 			if (err == EINPROGRESS)
8295 				return (err);
8296 			err = 0;	/* Ensure we don't use it below */
8297 		}
8298 	}
8299 
8300 	/*
8301 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8302 	 */
8303 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8304 		ipif_refrele(ipif);
8305 		return (ENXIO);
8306 	}
8307 
8308 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8309 	    name[0] == '\0') {
8310 		/*
8311 		 * Handle a or a SIOC?IF* with a null name
8312 		 * during plumb (on the ill queue before the I_PLINK).
8313 		 */
8314 		ipif = ill->ill_ipif;
8315 		ipif_refhold(ipif);
8316 	}
8317 
8318 	if (ipif == NULL)
8319 		return (ENXIO);
8320 
8321 	ci->ci_ipif = ipif;
8322 	return (0);
8323 }
8324 
8325 /*
8326  * Return the total number of ipifs.
8327  */
8328 static uint_t
8329 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8330 {
8331 	uint_t numifs = 0;
8332 	ill_t	*ill;
8333 	ill_walk_context_t	ctx;
8334 	ipif_t	*ipif;
8335 
8336 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8337 	ill = ILL_START_WALK_V4(&ctx, ipst);
8338 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8339 		if (IS_UNDER_IPMP(ill))
8340 			continue;
8341 		for (ipif = ill->ill_ipif; ipif != NULL;
8342 		    ipif = ipif->ipif_next) {
8343 			if (ipif->ipif_zoneid == zoneid ||
8344 			    ipif->ipif_zoneid == ALL_ZONES)
8345 				numifs++;
8346 		}
8347 	}
8348 	rw_exit(&ipst->ips_ill_g_lock);
8349 	return (numifs);
8350 }
8351 
8352 /*
8353  * Return the total number of ipifs.
8354  */
8355 static uint_t
8356 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8357 {
8358 	uint_t numifs = 0;
8359 	ill_t	*ill;
8360 	ipif_t	*ipif;
8361 	ill_walk_context_t	ctx;
8362 
8363 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8364 
8365 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8366 	if (family == AF_INET)
8367 		ill = ILL_START_WALK_V4(&ctx, ipst);
8368 	else if (family == AF_INET6)
8369 		ill = ILL_START_WALK_V6(&ctx, ipst);
8370 	else
8371 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8372 
8373 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8374 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8375 			continue;
8376 
8377 		for (ipif = ill->ill_ipif; ipif != NULL;
8378 		    ipif = ipif->ipif_next) {
8379 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8380 			    !(lifn_flags & LIFC_NOXMIT))
8381 				continue;
8382 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8383 			    !(lifn_flags & LIFC_TEMPORARY))
8384 				continue;
8385 			if (((ipif->ipif_flags &
8386 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8387 			    IPIF_DEPRECATED)) ||
8388 			    IS_LOOPBACK(ill) ||
8389 			    !(ipif->ipif_flags & IPIF_UP)) &&
8390 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8391 				continue;
8392 
8393 			if (zoneid != ipif->ipif_zoneid &&
8394 			    ipif->ipif_zoneid != ALL_ZONES &&
8395 			    (zoneid != GLOBAL_ZONEID ||
8396 			    !(lifn_flags & LIFC_ALLZONES)))
8397 				continue;
8398 
8399 			numifs++;
8400 		}
8401 	}
8402 	rw_exit(&ipst->ips_ill_g_lock);
8403 	return (numifs);
8404 }
8405 
8406 uint_t
8407 ip_get_lifsrcofnum(ill_t *ill)
8408 {
8409 	uint_t numifs = 0;
8410 	ill_t	*ill_head = ill;
8411 	ip_stack_t	*ipst = ill->ill_ipst;
8412 
8413 	/*
8414 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8415 	 * other thread may be trying to relink the ILLs in this usesrc group
8416 	 * and adjusting the ill_usesrc_grp_next pointers
8417 	 */
8418 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8419 	if ((ill->ill_usesrc_ifindex == 0) &&
8420 	    (ill->ill_usesrc_grp_next != NULL)) {
8421 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8422 		    ill = ill->ill_usesrc_grp_next)
8423 			numifs++;
8424 	}
8425 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8426 
8427 	return (numifs);
8428 }
8429 
8430 /* Null values are passed in for ipif, sin, and ifreq */
8431 /* ARGSUSED */
8432 int
8433 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8434     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8435 {
8436 	int *nump;
8437 	conn_t *connp = Q_TO_CONN(q);
8438 
8439 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8440 
8441 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8442 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8443 
8444 	*nump = ip_get_numifs(connp->conn_zoneid,
8445 	    connp->conn_netstack->netstack_ip);
8446 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8447 	return (0);
8448 }
8449 
8450 /* Null values are passed in for ipif, sin, and ifreq */
8451 /* ARGSUSED */
8452 int
8453 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8454     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8455 {
8456 	struct lifnum *lifn;
8457 	mblk_t	*mp1;
8458 	conn_t *connp = Q_TO_CONN(q);
8459 
8460 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8461 
8462 	/* Existence checked in ip_wput_nondata */
8463 	mp1 = mp->b_cont->b_cont;
8464 
8465 	lifn = (struct lifnum *)mp1->b_rptr;
8466 	switch (lifn->lifn_family) {
8467 	case AF_UNSPEC:
8468 	case AF_INET:
8469 	case AF_INET6:
8470 		break;
8471 	default:
8472 		return (EAFNOSUPPORT);
8473 	}
8474 
8475 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8476 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8477 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8478 	return (0);
8479 }
8480 
8481 /* ARGSUSED */
8482 int
8483 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8484     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8485 {
8486 	STRUCT_HANDLE(ifconf, ifc);
8487 	mblk_t *mp1;
8488 	struct iocblk *iocp;
8489 	struct ifreq *ifr;
8490 	ill_walk_context_t	ctx;
8491 	ill_t	*ill;
8492 	ipif_t	*ipif;
8493 	struct sockaddr_in *sin;
8494 	int32_t	ifclen;
8495 	zoneid_t zoneid;
8496 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8497 
8498 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8499 
8500 	ip1dbg(("ip_sioctl_get_ifconf"));
8501 	/* Existence verified in ip_wput_nondata */
8502 	mp1 = mp->b_cont->b_cont;
8503 	iocp = (struct iocblk *)mp->b_rptr;
8504 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8505 
8506 	/*
8507 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8508 	 * the user buffer address and length into which the list of struct
8509 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8510 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8511 	 * the SIOCGIFCONF operation was redefined to simply provide
8512 	 * a large output buffer into which we are supposed to jam the ifreq
8513 	 * array.  The same ioctl command code was used, despite the fact that
8514 	 * both the applications and the kernel code had to change, thus making
8515 	 * it impossible to support both interfaces.
8516 	 *
8517 	 * For reasons not good enough to try to explain, the following
8518 	 * algorithm is used for deciding what to do with one of these:
8519 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8520 	 * form with the output buffer coming down as the continuation message.
8521 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8522 	 * and we have to copy in the ifconf structure to find out how big the
8523 	 * output buffer is and where to copy out to.  Sure no problem...
8524 	 *
8525 	 */
8526 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8527 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8528 		int numifs = 0;
8529 		size_t ifc_bufsize;
8530 
8531 		/*
8532 		 * Must be (better be!) continuation of a TRANSPARENT
8533 		 * IOCTL.  We just copied in the ifconf structure.
8534 		 */
8535 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8536 		    (struct ifconf *)mp1->b_rptr);
8537 
8538 		/*
8539 		 * Allocate a buffer to hold requested information.
8540 		 *
8541 		 * If ifc_len is larger than what is needed, we only
8542 		 * allocate what we will use.
8543 		 *
8544 		 * If ifc_len is smaller than what is needed, return
8545 		 * EINVAL.
8546 		 *
8547 		 * XXX: the ill_t structure can hava 2 counters, for
8548 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8549 		 * number of interfaces for a device, so we don't need
8550 		 * to count them here...
8551 		 */
8552 		numifs = ip_get_numifs(zoneid, ipst);
8553 
8554 		ifclen = STRUCT_FGET(ifc, ifc_len);
8555 		ifc_bufsize = numifs * sizeof (struct ifreq);
8556 		if (ifc_bufsize > ifclen) {
8557 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8558 				/* old behaviour */
8559 				return (EINVAL);
8560 			} else {
8561 				ifc_bufsize = ifclen;
8562 			}
8563 		}
8564 
8565 		mp1 = mi_copyout_alloc(q, mp,
8566 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8567 		if (mp1 == NULL)
8568 			return (ENOMEM);
8569 
8570 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8571 	}
8572 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8573 	/*
8574 	 * the SIOCGIFCONF ioctl only knows about
8575 	 * IPv4 addresses, so don't try to tell
8576 	 * it about interfaces with IPv6-only
8577 	 * addresses. (Last parm 'isv6' is B_FALSE)
8578 	 */
8579 
8580 	ifr = (struct ifreq *)mp1->b_rptr;
8581 
8582 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8583 	ill = ILL_START_WALK_V4(&ctx, ipst);
8584 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8585 		if (IS_UNDER_IPMP(ill))
8586 			continue;
8587 		for (ipif = ill->ill_ipif; ipif != NULL;
8588 		    ipif = ipif->ipif_next) {
8589 			if (zoneid != ipif->ipif_zoneid &&
8590 			    ipif->ipif_zoneid != ALL_ZONES)
8591 				continue;
8592 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8593 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8594 					/* old behaviour */
8595 					rw_exit(&ipst->ips_ill_g_lock);
8596 					return (EINVAL);
8597 				} else {
8598 					goto if_copydone;
8599 				}
8600 			}
8601 			ipif_get_name(ipif, ifr->ifr_name,
8602 			    sizeof (ifr->ifr_name));
8603 			sin = (sin_t *)&ifr->ifr_addr;
8604 			*sin = sin_null;
8605 			sin->sin_family = AF_INET;
8606 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8607 			ifr++;
8608 		}
8609 	}
8610 if_copydone:
8611 	rw_exit(&ipst->ips_ill_g_lock);
8612 	mp1->b_wptr = (uchar_t *)ifr;
8613 
8614 	if (STRUCT_BUF(ifc) != NULL) {
8615 		STRUCT_FSET(ifc, ifc_len,
8616 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8617 	}
8618 	return (0);
8619 }
8620 
8621 /*
8622  * Get the interfaces using the address hosted on the interface passed in,
8623  * as a source adddress
8624  */
8625 /* ARGSUSED */
8626 int
8627 ip_sioctl_get_lifsrcof(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 	ill_t	*ill, *ill_head;
8632 	ipif_t	*ipif, *orig_ipif;
8633 	int	numlifs = 0;
8634 	size_t	lifs_bufsize, lifsmaxlen;
8635 	struct	lifreq *lifr;
8636 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8637 	uint_t	ifindex;
8638 	zoneid_t zoneid;
8639 	int err = 0;
8640 	boolean_t isv6 = B_FALSE;
8641 	struct	sockaddr_in	*sin;
8642 	struct	sockaddr_in6	*sin6;
8643 	STRUCT_HANDLE(lifsrcof, lifs);
8644 	ip_stack_t		*ipst;
8645 
8646 	ipst = CONNQ_TO_IPST(q);
8647 
8648 	ASSERT(q->q_next == NULL);
8649 
8650 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8651 
8652 	/* Existence verified in ip_wput_nondata */
8653 	mp1 = mp->b_cont->b_cont;
8654 
8655 	/*
8656 	 * Must be (better be!) continuation of a TRANSPARENT
8657 	 * IOCTL.  We just copied in the lifsrcof structure.
8658 	 */
8659 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8660 	    (struct lifsrcof *)mp1->b_rptr);
8661 
8662 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8663 		return (EINVAL);
8664 
8665 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8666 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8667 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8668 	    ip_process_ioctl, &err, ipst);
8669 	if (ipif == NULL) {
8670 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8671 		    ifindex));
8672 		return (err);
8673 	}
8674 
8675 	/* Allocate a buffer to hold requested information */
8676 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8677 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8678 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8679 	/* The actual size needed is always returned in lifs_len */
8680 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8681 
8682 	/* If the amount we need is more than what is passed in, abort */
8683 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8684 		ipif_refrele(ipif);
8685 		return (0);
8686 	}
8687 
8688 	mp1 = mi_copyout_alloc(q, mp,
8689 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8690 	if (mp1 == NULL) {
8691 		ipif_refrele(ipif);
8692 		return (ENOMEM);
8693 	}
8694 
8695 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8696 	bzero(mp1->b_rptr, lifs_bufsize);
8697 
8698 	lifr = (struct lifreq *)mp1->b_rptr;
8699 
8700 	ill = ill_head = ipif->ipif_ill;
8701 	orig_ipif = ipif;
8702 
8703 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8704 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8705 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8706 
8707 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8708 	for (; (ill != NULL) && (ill != ill_head);
8709 	    ill = ill->ill_usesrc_grp_next) {
8710 
8711 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8712 			break;
8713 
8714 		ipif = ill->ill_ipif;
8715 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8716 		if (ipif->ipif_isv6) {
8717 			sin6 = (sin6_t *)&lifr->lifr_addr;
8718 			*sin6 = sin6_null;
8719 			sin6->sin6_family = AF_INET6;
8720 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8721 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8722 			    &ipif->ipif_v6net_mask);
8723 		} else {
8724 			sin = (sin_t *)&lifr->lifr_addr;
8725 			*sin = sin_null;
8726 			sin->sin_family = AF_INET;
8727 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8728 			lifr->lifr_addrlen = ip_mask_to_plen(
8729 			    ipif->ipif_net_mask);
8730 		}
8731 		lifr++;
8732 	}
8733 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8734 	rw_exit(&ipst->ips_ill_g_lock);
8735 	ipif_refrele(orig_ipif);
8736 	mp1->b_wptr = (uchar_t *)lifr;
8737 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8738 
8739 	return (0);
8740 }
8741 
8742 /* ARGSUSED */
8743 int
8744 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8745     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8746 {
8747 	mblk_t *mp1;
8748 	int	list;
8749 	ill_t	*ill;
8750 	ipif_t	*ipif;
8751 	int	flags;
8752 	int	numlifs = 0;
8753 	size_t	lifc_bufsize;
8754 	struct	lifreq *lifr;
8755 	sa_family_t	family;
8756 	struct	sockaddr_in	*sin;
8757 	struct	sockaddr_in6	*sin6;
8758 	ill_walk_context_t	ctx;
8759 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8760 	int32_t	lifclen;
8761 	zoneid_t zoneid;
8762 	STRUCT_HANDLE(lifconf, lifc);
8763 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8764 
8765 	ip1dbg(("ip_sioctl_get_lifconf"));
8766 
8767 	ASSERT(q->q_next == NULL);
8768 
8769 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8770 
8771 	/* Existence verified in ip_wput_nondata */
8772 	mp1 = mp->b_cont->b_cont;
8773 
8774 	/*
8775 	 * An extended version of SIOCGIFCONF that takes an
8776 	 * additional address family and flags field.
8777 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8778 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8779 	 * interfaces are omitted.
8780 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8781 	 * unless LIFC_TEMPORARY is specified.
8782 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8783 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8784 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8785 	 * has priority over LIFC_NOXMIT.
8786 	 */
8787 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8788 
8789 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8790 		return (EINVAL);
8791 
8792 	/*
8793 	 * Must be (better be!) continuation of a TRANSPARENT
8794 	 * IOCTL.  We just copied in the lifconf structure.
8795 	 */
8796 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8797 
8798 	family = STRUCT_FGET(lifc, lifc_family);
8799 	flags = STRUCT_FGET(lifc, lifc_flags);
8800 
8801 	switch (family) {
8802 	case AF_UNSPEC:
8803 		/*
8804 		 * walk all ILL's.
8805 		 */
8806 		list = MAX_G_HEADS;
8807 		break;
8808 	case AF_INET:
8809 		/*
8810 		 * walk only IPV4 ILL's.
8811 		 */
8812 		list = IP_V4_G_HEAD;
8813 		break;
8814 	case AF_INET6:
8815 		/*
8816 		 * walk only IPV6 ILL's.
8817 		 */
8818 		list = IP_V6_G_HEAD;
8819 		break;
8820 	default:
8821 		return (EAFNOSUPPORT);
8822 	}
8823 
8824 	/*
8825 	 * Allocate a buffer to hold requested information.
8826 	 *
8827 	 * If lifc_len is larger than what is needed, we only
8828 	 * allocate what we will use.
8829 	 *
8830 	 * If lifc_len is smaller than what is needed, return
8831 	 * EINVAL.
8832 	 */
8833 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8834 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8835 	lifclen = STRUCT_FGET(lifc, lifc_len);
8836 	if (lifc_bufsize > lifclen) {
8837 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8838 			return (EINVAL);
8839 		else
8840 			lifc_bufsize = lifclen;
8841 	}
8842 
8843 	mp1 = mi_copyout_alloc(q, mp,
8844 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8845 	if (mp1 == NULL)
8846 		return (ENOMEM);
8847 
8848 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8849 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8850 
8851 	lifr = (struct lifreq *)mp1->b_rptr;
8852 
8853 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8854 	ill = ill_first(list, list, &ctx, ipst);
8855 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8856 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8857 			continue;
8858 
8859 		for (ipif = ill->ill_ipif; ipif != NULL;
8860 		    ipif = ipif->ipif_next) {
8861 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8862 			    !(flags & LIFC_NOXMIT))
8863 				continue;
8864 
8865 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8866 			    !(flags & LIFC_TEMPORARY))
8867 				continue;
8868 
8869 			if (((ipif->ipif_flags &
8870 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8871 			    IPIF_DEPRECATED)) ||
8872 			    IS_LOOPBACK(ill) ||
8873 			    !(ipif->ipif_flags & IPIF_UP)) &&
8874 			    (flags & LIFC_EXTERNAL_SOURCE))
8875 				continue;
8876 
8877 			if (zoneid != ipif->ipif_zoneid &&
8878 			    ipif->ipif_zoneid != ALL_ZONES &&
8879 			    (zoneid != GLOBAL_ZONEID ||
8880 			    !(flags & LIFC_ALLZONES)))
8881 				continue;
8882 
8883 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8884 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8885 					rw_exit(&ipst->ips_ill_g_lock);
8886 					return (EINVAL);
8887 				} else {
8888 					goto lif_copydone;
8889 				}
8890 			}
8891 
8892 			ipif_get_name(ipif, lifr->lifr_name,
8893 			    sizeof (lifr->lifr_name));
8894 			lifr->lifr_type = ill->ill_type;
8895 			if (ipif->ipif_isv6) {
8896 				sin6 = (sin6_t *)&lifr->lifr_addr;
8897 				*sin6 = sin6_null;
8898 				sin6->sin6_family = AF_INET6;
8899 				sin6->sin6_addr =
8900 				    ipif->ipif_v6lcl_addr;
8901 				lifr->lifr_addrlen =
8902 				    ip_mask_to_plen_v6(
8903 				    &ipif->ipif_v6net_mask);
8904 			} else {
8905 				sin = (sin_t *)&lifr->lifr_addr;
8906 				*sin = sin_null;
8907 				sin->sin_family = AF_INET;
8908 				sin->sin_addr.s_addr =
8909 				    ipif->ipif_lcl_addr;
8910 				lifr->lifr_addrlen =
8911 				    ip_mask_to_plen(
8912 				    ipif->ipif_net_mask);
8913 			}
8914 			lifr++;
8915 		}
8916 	}
8917 lif_copydone:
8918 	rw_exit(&ipst->ips_ill_g_lock);
8919 
8920 	mp1->b_wptr = (uchar_t *)lifr;
8921 	if (STRUCT_BUF(lifc) != NULL) {
8922 		STRUCT_FSET(lifc, lifc_len,
8923 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8924 	}
8925 	return (0);
8926 }
8927 
8928 static void
8929 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8930 {
8931 	ip6_asp_t *table;
8932 	size_t table_size;
8933 	mblk_t *data_mp;
8934 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8935 	ip_stack_t	*ipst;
8936 
8937 	if (q->q_next == NULL)
8938 		ipst = CONNQ_TO_IPST(q);
8939 	else
8940 		ipst = ILLQ_TO_IPST(q);
8941 
8942 	/* These two ioctls are I_STR only */
8943 	if (iocp->ioc_count == TRANSPARENT) {
8944 		miocnak(q, mp, 0, EINVAL);
8945 		return;
8946 	}
8947 
8948 	data_mp = mp->b_cont;
8949 	if (data_mp == NULL) {
8950 		/* The user passed us a NULL argument */
8951 		table = NULL;
8952 		table_size = iocp->ioc_count;
8953 	} else {
8954 		/*
8955 		 * The user provided a table.  The stream head
8956 		 * may have copied in the user data in chunks,
8957 		 * so make sure everything is pulled up
8958 		 * properly.
8959 		 */
8960 		if (MBLKL(data_mp) < iocp->ioc_count) {
8961 			mblk_t *new_data_mp;
8962 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8963 			    NULL) {
8964 				miocnak(q, mp, 0, ENOMEM);
8965 				return;
8966 			}
8967 			freemsg(data_mp);
8968 			data_mp = new_data_mp;
8969 			mp->b_cont = data_mp;
8970 		}
8971 		table = (ip6_asp_t *)data_mp->b_rptr;
8972 		table_size = iocp->ioc_count;
8973 	}
8974 
8975 	switch (iocp->ioc_cmd) {
8976 	case SIOCGIP6ADDRPOLICY:
8977 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8978 		if (iocp->ioc_rval == -1)
8979 			iocp->ioc_error = EINVAL;
8980 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8981 		else if (table != NULL &&
8982 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8983 			ip6_asp_t *src = table;
8984 			ip6_asp32_t *dst = (void *)table;
8985 			int count = table_size / sizeof (ip6_asp_t);
8986 			int i;
8987 
8988 			/*
8989 			 * We need to do an in-place shrink of the array
8990 			 * to match the alignment attributes of the
8991 			 * 32-bit ABI looking at it.
8992 			 */
8993 			/* LINTED: logical expression always true: op "||" */
8994 			ASSERT(sizeof (*src) > sizeof (*dst));
8995 			for (i = 1; i < count; i++)
8996 				bcopy(src + i, dst + i, sizeof (*dst));
8997 		}
8998 #endif
8999 		break;
9000 
9001 	case SIOCSIP6ADDRPOLICY:
9002 		ASSERT(mp->b_prev == NULL);
9003 		mp->b_prev = (void *)q;
9004 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9005 		/*
9006 		 * We pass in the datamodel here so that the ip6_asp_replace()
9007 		 * routine can handle converting from 32-bit to native formats
9008 		 * where necessary.
9009 		 *
9010 		 * A better way to handle this might be to convert the inbound
9011 		 * data structure here, and hang it off a new 'mp'; thus the
9012 		 * ip6_asp_replace() logic would always be dealing with native
9013 		 * format data structures..
9014 		 *
9015 		 * (An even simpler way to handle these ioctls is to just
9016 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9017 		 * and just recompile everything that depends on it.)
9018 		 */
9019 #endif
9020 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9021 		    iocp->ioc_flag & IOC_MODELS);
9022 		return;
9023 	}
9024 
9025 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9026 	qreply(q, mp);
9027 }
9028 
9029 static void
9030 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9031 {
9032 	mblk_t 		*data_mp;
9033 	struct dstinforeq	*dir;
9034 	uint8_t		*end, *cur;
9035 	in6_addr_t	*daddr, *saddr;
9036 	ipaddr_t	v4daddr;
9037 	ire_t		*ire;
9038 	char		*slabel, *dlabel;
9039 	boolean_t	isipv4;
9040 	int		match_ire;
9041 	ill_t		*dst_ill;
9042 	ipif_t		*src_ipif, *ire_ipif;
9043 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9044 	zoneid_t	zoneid;
9045 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9046 
9047 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9048 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9049 
9050 	/*
9051 	 * This ioctl is I_STR only, and must have a
9052 	 * data mblk following the M_IOCTL mblk.
9053 	 */
9054 	data_mp = mp->b_cont;
9055 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9056 		miocnak(q, mp, 0, EINVAL);
9057 		return;
9058 	}
9059 
9060 	if (MBLKL(data_mp) < iocp->ioc_count) {
9061 		mblk_t *new_data_mp;
9062 
9063 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9064 			miocnak(q, mp, 0, ENOMEM);
9065 			return;
9066 		}
9067 		freemsg(data_mp);
9068 		data_mp = new_data_mp;
9069 		mp->b_cont = data_mp;
9070 	}
9071 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9072 
9073 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9074 	    end - cur >= sizeof (struct dstinforeq);
9075 	    cur += sizeof (struct dstinforeq)) {
9076 		dir = (struct dstinforeq *)cur;
9077 		daddr = &dir->dir_daddr;
9078 		saddr = &dir->dir_saddr;
9079 
9080 		/*
9081 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9082 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9083 		 * and ipif_select_source[_v6]() do not.
9084 		 */
9085 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9086 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9087 
9088 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9089 		if (isipv4) {
9090 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9091 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9092 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9093 		} else {
9094 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9095 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9096 		}
9097 		if (ire == NULL) {
9098 			dir->dir_dreachable = 0;
9099 
9100 			/* move on to next dst addr */
9101 			continue;
9102 		}
9103 		dir->dir_dreachable = 1;
9104 
9105 		ire_ipif = ire->ire_ipif;
9106 		if (ire_ipif == NULL)
9107 			goto next_dst;
9108 
9109 		/*
9110 		 * We expect to get back an interface ire or a
9111 		 * gateway ire cache entry.  For both types, the
9112 		 * output interface is ire_ipif->ipif_ill.
9113 		 */
9114 		dst_ill = ire_ipif->ipif_ill;
9115 		dir->dir_dmactype = dst_ill->ill_mactype;
9116 
9117 		if (isipv4) {
9118 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9119 		} else {
9120 			src_ipif = ipif_select_source_v6(dst_ill,
9121 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9122 		}
9123 		if (src_ipif == NULL)
9124 			goto next_dst;
9125 
9126 		*saddr = src_ipif->ipif_v6lcl_addr;
9127 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9128 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9129 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9130 		dir->dir_sdeprecated =
9131 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9132 		ipif_refrele(src_ipif);
9133 next_dst:
9134 		ire_refrele(ire);
9135 	}
9136 	miocack(q, mp, iocp->ioc_count, 0);
9137 }
9138 
9139 /*
9140  * Check if this is an address assigned to this machine.
9141  * Skips interfaces that are down by using ire checks.
9142  * Translates mapped addresses to v4 addresses and then
9143  * treats them as such, returning true if the v4 address
9144  * associated with this mapped address is configured.
9145  * Note: Applications will have to be careful what they do
9146  * with the response; use of mapped addresses limits
9147  * what can be done with the socket, especially with
9148  * respect to socket options and ioctls - neither IPv4
9149  * options nor IPv6 sticky options/ancillary data options
9150  * may be used.
9151  */
9152 /* ARGSUSED */
9153 int
9154 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9155     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9156 {
9157 	struct sioc_addrreq *sia;
9158 	sin_t *sin;
9159 	ire_t *ire;
9160 	mblk_t *mp1;
9161 	zoneid_t zoneid;
9162 	ip_stack_t	*ipst;
9163 
9164 	ip1dbg(("ip_sioctl_tmyaddr"));
9165 
9166 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9167 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9168 	ipst = CONNQ_TO_IPST(q);
9169 
9170 	/* Existence verified in ip_wput_nondata */
9171 	mp1 = mp->b_cont->b_cont;
9172 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9173 	sin = (sin_t *)&sia->sa_addr;
9174 	switch (sin->sin_family) {
9175 	case AF_INET6: {
9176 		sin6_t *sin6 = (sin6_t *)sin;
9177 
9178 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9179 			ipaddr_t v4_addr;
9180 
9181 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9182 			    v4_addr);
9183 			ire = ire_ctable_lookup(v4_addr, 0,
9184 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9185 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9186 		} else {
9187 			in6_addr_t v6addr;
9188 
9189 			v6addr = sin6->sin6_addr;
9190 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9191 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9192 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9193 		}
9194 		break;
9195 	}
9196 	case AF_INET: {
9197 		ipaddr_t v4addr;
9198 
9199 		v4addr = sin->sin_addr.s_addr;
9200 		ire = ire_ctable_lookup(v4addr, 0,
9201 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9202 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9203 		break;
9204 	}
9205 	default:
9206 		return (EAFNOSUPPORT);
9207 	}
9208 	if (ire != NULL) {
9209 		sia->sa_res = 1;
9210 		ire_refrele(ire);
9211 	} else {
9212 		sia->sa_res = 0;
9213 	}
9214 	return (0);
9215 }
9216 
9217 /*
9218  * Check if this is an address assigned on-link i.e. neighbor,
9219  * and makes sure it's reachable from the current zone.
9220  * Returns true for my addresses as well.
9221  * Translates mapped addresses to v4 addresses and then
9222  * treats them as such, returning true if the v4 address
9223  * associated with this mapped address is configured.
9224  * Note: Applications will have to be careful what they do
9225  * with the response; use of mapped addresses limits
9226  * what can be done with the socket, especially with
9227  * respect to socket options and ioctls - neither IPv4
9228  * options nor IPv6 sticky options/ancillary data options
9229  * may be used.
9230  */
9231 /* ARGSUSED */
9232 int
9233 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9234     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9235 {
9236 	struct sioc_addrreq *sia;
9237 	sin_t *sin;
9238 	mblk_t	*mp1;
9239 	ire_t *ire = NULL;
9240 	zoneid_t zoneid;
9241 	ip_stack_t	*ipst;
9242 
9243 	ip1dbg(("ip_sioctl_tonlink"));
9244 
9245 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9246 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9247 	ipst = CONNQ_TO_IPST(q);
9248 
9249 	/* Existence verified in ip_wput_nondata */
9250 	mp1 = mp->b_cont->b_cont;
9251 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9252 	sin = (sin_t *)&sia->sa_addr;
9253 
9254 	/*
9255 	 * Match addresses with a zero gateway field to avoid
9256 	 * routes going through a router.
9257 	 * Exclude broadcast and multicast addresses.
9258 	 */
9259 	switch (sin->sin_family) {
9260 	case AF_INET6: {
9261 		sin6_t *sin6 = (sin6_t *)sin;
9262 
9263 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9264 			ipaddr_t v4_addr;
9265 
9266 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9267 			    v4_addr);
9268 			if (!CLASSD(v4_addr)) {
9269 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9270 				    NULL, NULL, zoneid, NULL,
9271 				    MATCH_IRE_GW, ipst);
9272 			}
9273 		} else {
9274 			in6_addr_t v6addr;
9275 			in6_addr_t v6gw;
9276 
9277 			v6addr = sin6->sin6_addr;
9278 			v6gw = ipv6_all_zeros;
9279 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9280 				ire = ire_route_lookup_v6(&v6addr, 0,
9281 				    &v6gw, 0, NULL, NULL, zoneid,
9282 				    NULL, MATCH_IRE_GW, ipst);
9283 			}
9284 		}
9285 		break;
9286 	}
9287 	case AF_INET: {
9288 		ipaddr_t v4addr;
9289 
9290 		v4addr = sin->sin_addr.s_addr;
9291 		if (!CLASSD(v4addr)) {
9292 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9293 			    NULL, NULL, zoneid, NULL,
9294 			    MATCH_IRE_GW, ipst);
9295 		}
9296 		break;
9297 	}
9298 	default:
9299 		return (EAFNOSUPPORT);
9300 	}
9301 	sia->sa_res = 0;
9302 	if (ire != NULL) {
9303 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9304 		    IRE_LOCAL|IRE_LOOPBACK)) {
9305 			sia->sa_res = 1;
9306 		}
9307 		ire_refrele(ire);
9308 	}
9309 	return (0);
9310 }
9311 
9312 /*
9313  * TBD: implement when kernel maintaines a list of site prefixes.
9314  */
9315 /* ARGSUSED */
9316 int
9317 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9318     ip_ioctl_cmd_t *ipip, void *ifreq)
9319 {
9320 	return (ENXIO);
9321 }
9322 
9323 /* ARGSUSED */
9324 int
9325 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9326     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9327 {
9328 	ill_t		*ill;
9329 	mblk_t		*mp1;
9330 	conn_t		*connp;
9331 	boolean_t	success;
9332 
9333 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9334 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9335 	/* ioctl comes down on an conn */
9336 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9337 	connp = Q_TO_CONN(q);
9338 
9339 	mp->b_datap->db_type = M_IOCTL;
9340 
9341 	/*
9342 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9343 	 * The original mp contains contaminated b_next values due to 'mi',
9344 	 * which is needed to do the mi_copy_done. Unfortunately if we
9345 	 * send down the original mblk itself and if we are popped due to an
9346 	 * an unplumb before the response comes back from tunnel,
9347 	 * the streamhead (which does a freemsg) will see this contaminated
9348 	 * message and the assertion in freemsg about non-null b_next/b_prev
9349 	 * will panic a DEBUG kernel.
9350 	 */
9351 	mp1 = copymsg(mp);
9352 	if (mp1 == NULL)
9353 		return (ENOMEM);
9354 
9355 	ill = ipif->ipif_ill;
9356 	mutex_enter(&connp->conn_lock);
9357 	mutex_enter(&ill->ill_lock);
9358 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9359 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9360 		    mp, 0);
9361 	} else {
9362 		success = ill_pending_mp_add(ill, connp, mp);
9363 	}
9364 	mutex_exit(&ill->ill_lock);
9365 	mutex_exit(&connp->conn_lock);
9366 
9367 	if (success) {
9368 		ip1dbg(("sending down tunparam request "));
9369 		putnext(ill->ill_wq, mp1);
9370 		return (EINPROGRESS);
9371 	} else {
9372 		/* The conn has started closing */
9373 		freemsg(mp1);
9374 		return (EINTR);
9375 	}
9376 }
9377 
9378 /*
9379  * ARP IOCTLs.
9380  * How does IP get in the business of fronting ARP configuration/queries?
9381  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9382  * are by tradition passed in through a datagram socket.  That lands in IP.
9383  * As it happens, this is just as well since the interface is quite crude in
9384  * that it passes in no information about protocol or hardware types, or
9385  * interface association.  After making the protocol assumption, IP is in
9386  * the position to look up the name of the ILL, which ARP will need, and
9387  * format a request that can be handled by ARP.  The request is passed up
9388  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9389  * back a response.  ARP supports its own set of more general IOCTLs, in
9390  * case anyone is interested.
9391  */
9392 /* ARGSUSED */
9393 int
9394 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9395     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9396 {
9397 	mblk_t *mp1;
9398 	mblk_t *mp2;
9399 	mblk_t *pending_mp;
9400 	ipaddr_t ipaddr;
9401 	area_t *area;
9402 	struct iocblk *iocp;
9403 	conn_t *connp;
9404 	struct arpreq *ar;
9405 	struct xarpreq *xar;
9406 	int flags, alength;
9407 	uchar_t *lladdr;
9408 	ire_t *ire;
9409 	ip_stack_t *ipst;
9410 	ill_t *ill = ipif->ipif_ill;
9411 	ill_t *proxy_ill = NULL;
9412 	ipmp_arpent_t *entp = NULL;
9413 	boolean_t if_arp_ioctl = B_FALSE;
9414 	boolean_t proxyarp = B_FALSE;
9415 
9416 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9417 	connp = Q_TO_CONN(q);
9418 	ipst = connp->conn_netstack->netstack_ip;
9419 
9420 	if (ipip->ipi_cmd_type == XARP_CMD) {
9421 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9422 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9423 		ar = NULL;
9424 
9425 		flags = xar->xarp_flags;
9426 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9427 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9428 		/*
9429 		 * Validate against user's link layer address length
9430 		 * input and name and addr length limits.
9431 		 */
9432 		alength = ill->ill_phys_addr_length;
9433 		if (ipip->ipi_cmd == SIOCSXARP) {
9434 			if (alength != xar->xarp_ha.sdl_alen ||
9435 			    (alength + xar->xarp_ha.sdl_nlen >
9436 			    sizeof (xar->xarp_ha.sdl_data)))
9437 				return (EINVAL);
9438 		}
9439 	} else {
9440 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9441 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9442 		xar = NULL;
9443 
9444 		flags = ar->arp_flags;
9445 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9446 		/*
9447 		 * Theoretically, the sa_family could tell us what link
9448 		 * layer type this operation is trying to deal with. By
9449 		 * common usage AF_UNSPEC means ethernet. We'll assume
9450 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9451 		 * for now. Our new SIOC*XARP ioctls can be used more
9452 		 * generally.
9453 		 *
9454 		 * If the underlying media happens to have a non 6 byte
9455 		 * address, arp module will fail set/get, but the del
9456 		 * operation will succeed.
9457 		 */
9458 		alength = 6;
9459 		if ((ipip->ipi_cmd != SIOCDARP) &&
9460 		    (alength != ill->ill_phys_addr_length)) {
9461 			return (EINVAL);
9462 		}
9463 	}
9464 
9465 	ipaddr = sin->sin_addr.s_addr;
9466 
9467 	/*
9468 	 * IPMP ARP special handling:
9469 	 *
9470 	 * 1. Since ARP mappings must appear consistent across the group,
9471 	 *    prohibit changing ARP mappings on the underlying interfaces.
9472 	 *
9473 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9474 	 *    IP itself, prohibit changing them.
9475 	 *
9476 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9477 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9478 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9479 	 */
9480 	if (IS_UNDER_IPMP(ill)) {
9481 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9482 			return (EPERM);
9483 	}
9484 	if (IS_IPMP(ill)) {
9485 		ipmp_illgrp_t *illg = ill->ill_grp;
9486 
9487 		switch (ipip->ipi_cmd) {
9488 		case SIOCSARP:
9489 		case SIOCSXARP:
9490 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9491 			if (proxy_ill != NULL) {
9492 				proxyarp = B_TRUE;
9493 				if (!ipmp_ill_is_active(proxy_ill))
9494 					proxy_ill = ipmp_illgrp_next_ill(illg);
9495 				if (proxy_ill != NULL)
9496 					lladdr = proxy_ill->ill_phys_addr;
9497 			}
9498 			/* FALLTHRU */
9499 		case SIOCDARP:
9500 		case SIOCDXARP:
9501 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9502 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9503 			if (ire != NULL) {
9504 				ire_refrele(ire);
9505 				return (EPERM);
9506 			}
9507 		}
9508 	}
9509 
9510 	/*
9511 	 * We are going to pass up to ARP a packet chain that looks
9512 	 * like:
9513 	 *
9514 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9515 	 *
9516 	 * Get a copy of the original IOCTL mblk to head the chain,
9517 	 * to be sent up (in mp1). Also get another copy to store
9518 	 * in the ill_pending_mp list, for matching the response
9519 	 * when it comes back from ARP.
9520 	 */
9521 	mp1 = copyb(mp);
9522 	pending_mp = copymsg(mp);
9523 	if (mp1 == NULL || pending_mp == NULL) {
9524 		if (mp1 != NULL)
9525 			freeb(mp1);
9526 		if (pending_mp != NULL)
9527 			inet_freemsg(pending_mp);
9528 		return (ENOMEM);
9529 	}
9530 
9531 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9532 	    (caddr_t)&ipaddr);
9533 	if (mp2 == NULL) {
9534 		freeb(mp1);
9535 		inet_freemsg(pending_mp);
9536 		return (ENOMEM);
9537 	}
9538 	/* Put together the chain. */
9539 	mp1->b_cont = mp2;
9540 	mp1->b_datap->db_type = M_IOCTL;
9541 	mp2->b_cont = mp;
9542 	mp2->b_datap->db_type = M_DATA;
9543 
9544 	iocp = (struct iocblk *)mp1->b_rptr;
9545 
9546 	/*
9547 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9548 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9549 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9550 	 * ioc_count field; set ioc_count to be correct.
9551 	 */
9552 	iocp->ioc_count = MBLKL(mp1->b_cont);
9553 
9554 	/*
9555 	 * Set the proper command in the ARP message.
9556 	 * Convert the SIOC{G|S|D}ARP calls into our
9557 	 * AR_ENTRY_xxx calls.
9558 	 */
9559 	area = (area_t *)mp2->b_rptr;
9560 	switch (iocp->ioc_cmd) {
9561 	case SIOCDARP:
9562 	case SIOCDXARP:
9563 		/*
9564 		 * We defer deleting the corresponding IRE until
9565 		 * we return from arp.
9566 		 */
9567 		area->area_cmd = AR_ENTRY_DELETE;
9568 		area->area_proto_mask_offset = 0;
9569 		break;
9570 	case SIOCGARP:
9571 	case SIOCGXARP:
9572 		area->area_cmd = AR_ENTRY_SQUERY;
9573 		area->area_proto_mask_offset = 0;
9574 		break;
9575 	case SIOCSARP:
9576 	case SIOCSXARP:
9577 		/*
9578 		 * Delete the corresponding ire to make sure IP will
9579 		 * pick up any change from arp.
9580 		 */
9581 		if (!if_arp_ioctl) {
9582 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9583 		} else {
9584 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9585 			if (ipif != NULL) {
9586 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9587 				    ipst);
9588 				ipif_refrele(ipif);
9589 			}
9590 		}
9591 		break;
9592 	}
9593 	iocp->ioc_cmd = area->area_cmd;
9594 
9595 	/*
9596 	 * Fill in the rest of the ARP operation fields.
9597 	 */
9598 	area->area_hw_addr_length = alength;
9599 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9600 
9601 	/* Translate the flags. */
9602 	if (flags & ATF_PERM)
9603 		area->area_flags |= ACE_F_PERMANENT;
9604 	if (flags & ATF_PUBL)
9605 		area->area_flags |= ACE_F_PUBLISH;
9606 	if (flags & ATF_AUTHORITY)
9607 		area->area_flags |= ACE_F_AUTHORITY;
9608 
9609 	/*
9610 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9611 	 * so that IP can update ARP as the active ills in the group change.
9612 	 */
9613 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9614 	    (area->area_flags & ACE_F_PERMANENT)) {
9615 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9616 
9617 		/*
9618 		 * The second part of the conditional below handles a corner
9619 		 * case: if this is proxy ARP and the IPMP group has no active
9620 		 * interfaces, we can't send the request to ARP now since it
9621 		 * won't be able to build an ACE.  So we return success and
9622 		 * notify ARP about the proxy ARP entry once an interface
9623 		 * becomes active.
9624 		 */
9625 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9626 			mp2->b_cont = NULL;
9627 			inet_freemsg(mp1);
9628 			inet_freemsg(pending_mp);
9629 			return (entp == NULL ? ENOMEM : 0);
9630 		}
9631 	}
9632 
9633 	/*
9634 	 * Before sending 'mp' to ARP, we have to clear the b_next
9635 	 * and b_prev. Otherwise if STREAMS encounters such a message
9636 	 * in freemsg(), (because ARP can close any time) it can cause
9637 	 * a panic. But mi code needs the b_next and b_prev values of
9638 	 * mp->b_cont, to complete the ioctl. So we store it here
9639 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9640 	 * when the response comes down from ARP.
9641 	 */
9642 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9643 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9644 	mp->b_cont->b_next = NULL;
9645 	mp->b_cont->b_prev = NULL;
9646 
9647 	mutex_enter(&connp->conn_lock);
9648 	mutex_enter(&ill->ill_lock);
9649 	/* conn has not yet started closing, hence this can't fail */
9650 	if (ipip->ipi_flags & IPI_WR) {
9651 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9652 		    pending_mp, 0) != 0);
9653 	} else {
9654 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9655 	}
9656 	mutex_exit(&ill->ill_lock);
9657 	mutex_exit(&connp->conn_lock);
9658 
9659 	/*
9660 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9661 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9662 	 */
9663 	putnext(ill->ill_rq, mp1);
9664 
9665 	/*
9666 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9667 	 */
9668 	if (entp != NULL)
9669 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9670 
9671 	return (EINPROGRESS);
9672 }
9673 
9674 /*
9675  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9676  * the associated sin and refhold and return the associated ipif via `ci'.
9677  */
9678 int
9679 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9680     cmd_info_t *ci, ipsq_func_t func)
9681 {
9682 	mblk_t	*mp1;
9683 	int	err;
9684 	sin_t	*sin;
9685 	conn_t	*connp;
9686 	ipif_t	*ipif;
9687 	ire_t	*ire = NULL;
9688 	ill_t	*ill = NULL;
9689 	boolean_t exists;
9690 	ip_stack_t *ipst;
9691 	struct arpreq *ar;
9692 	struct xarpreq *xar;
9693 	struct sockaddr_dl *sdl;
9694 
9695 	/* ioctl comes down on a conn */
9696 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9697 	connp = Q_TO_CONN(q);
9698 	if (connp->conn_af_isv6)
9699 		return (ENXIO);
9700 
9701 	ipst = connp->conn_netstack->netstack_ip;
9702 
9703 	/* Verified in ip_wput_nondata */
9704 	mp1 = mp->b_cont->b_cont;
9705 
9706 	if (ipip->ipi_cmd_type == XARP_CMD) {
9707 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9708 		xar = (struct xarpreq *)mp1->b_rptr;
9709 		sin = (sin_t *)&xar->xarp_pa;
9710 		sdl = &xar->xarp_ha;
9711 
9712 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9713 			return (ENXIO);
9714 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9715 			return (EINVAL);
9716 	} else {
9717 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9718 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9719 		ar = (struct arpreq *)mp1->b_rptr;
9720 		sin = (sin_t *)&ar->arp_pa;
9721 	}
9722 
9723 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9724 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9725 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9726 		    mp, func, &err, ipst);
9727 		if (ipif == NULL)
9728 			return (err);
9729 		if (ipif->ipif_id != 0) {
9730 			ipif_refrele(ipif);
9731 			return (ENXIO);
9732 		}
9733 	} else {
9734 		/*
9735 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9736 		 * of 0: use the IP address to find the ipif.  If the IP
9737 		 * address is an IPMP test address, ire_ftable_lookup() will
9738 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9739 		 */
9740 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9741 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9742 		if (ipif == NULL) {
9743 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9744 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9745 			    MATCH_IRE_TYPE, ipst);
9746 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9747 				if (ire != NULL)
9748 					ire_refrele(ire);
9749 				return (ENXIO);
9750 			}
9751 			ipif = ill->ill_ipif;
9752 			ipif_refhold(ipif);
9753 			ire_refrele(ire);
9754 		}
9755 	}
9756 
9757 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9758 		ipif_refrele(ipif);
9759 		return (ENXIO);
9760 	}
9761 
9762 	ci->ci_sin = sin;
9763 	ci->ci_ipif = ipif;
9764 	return (0);
9765 }
9766 
9767 /*
9768  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9769  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9770  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9771  * up and thus an ill can join that illgrp.
9772  *
9773  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9774  * open()/close() primarily because close() is not allowed to fail or block
9775  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9776  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9777  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9778  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9779  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9780  * state if I_UNLINK didn't occur.
9781  *
9782  * Note that for each plumb/unplumb operation, we may end up here more than
9783  * once because of the way ifconfig works.  However, it's OK to link the same
9784  * illgrp more than once, or unlink an illgrp that's already unlinked.
9785  */
9786 static int
9787 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9788 {
9789 	int err;
9790 	ip_stack_t *ipst = ill->ill_ipst;
9791 
9792 	ASSERT(IS_IPMP(ill));
9793 	ASSERT(IAM_WRITER_ILL(ill));
9794 
9795 	switch (ioccmd) {
9796 	case I_LINK:
9797 		return (ENOTSUP);
9798 
9799 	case I_PLINK:
9800 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9801 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9802 		rw_exit(&ipst->ips_ipmp_lock);
9803 		break;
9804 
9805 	case I_PUNLINK:
9806 		/*
9807 		 * Require all UP ipifs be brought down prior to unlinking the
9808 		 * illgrp so any associated IREs (and other state) is torched.
9809 		 */
9810 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9811 			return (EBUSY);
9812 
9813 		/*
9814 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9815 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9816 		 * join this group.  Specifically: ills trying to join grab
9817 		 * ipmp_lock and bump a "pending join" counter checked by
9818 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9819 		 * joins can occur (since we have ipmp_lock).  Once we drop
9820 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9821 		 * find the illgrp (since we unlinked it) and will return
9822 		 * EAFNOSUPPORT.  This will then take them back through the
9823 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9824 		 * back through I_PLINK above.
9825 		 */
9826 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9827 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9828 		rw_exit(&ipst->ips_ipmp_lock);
9829 		return (err);
9830 	default:
9831 		break;
9832 	}
9833 	return (0);
9834 }
9835 
9836 /*
9837  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9838  * atomically set/clear the muxids. Also complete the ioctl by acking or
9839  * naking it.  Note that the code is structured such that the link type,
9840  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9841  * its clones use the persistent link, while pppd(1M) and perhaps many
9842  * other daemons may use non-persistent link.  When combined with some
9843  * ill_t states, linking and unlinking lower streams may be used as
9844  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9845  */
9846 /* ARGSUSED */
9847 void
9848 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9849 {
9850 	mblk_t		*mp1, *mp2;
9851 	struct linkblk	*li;
9852 	struct ipmx_s	*ipmxp;
9853 	ill_t		*ill;
9854 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9855 	int		err = 0;
9856 	boolean_t	entered_ipsq = B_FALSE;
9857 	boolean_t	islink;
9858 	ip_stack_t	*ipst;
9859 
9860 	if (CONN_Q(q))
9861 		ipst = CONNQ_TO_IPST(q);
9862 	else
9863 		ipst = ILLQ_TO_IPST(q);
9864 
9865 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9866 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9867 
9868 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9869 
9870 	mp1 = mp->b_cont;	/* This is the linkblk info */
9871 	li = (struct linkblk *)mp1->b_rptr;
9872 
9873 	/*
9874 	 * ARP has added this special mblk, and the utility is asking us
9875 	 * to perform consistency checks, and also atomically set the
9876 	 * muxid. Ifconfig is an example.  It achieves this by using
9877 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9878 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9879 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9880 	 * and other comments in this routine for more details.
9881 	 */
9882 	mp2 = mp1->b_cont;	/* This is added by ARP */
9883 
9884 	/*
9885 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9886 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9887 	 * get the special mblk above.  For backward compatibility, we
9888 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9889 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9890 	 * not atomic, and can leave the streams unplumbable if the utility
9891 	 * is interrupted before it does the SIOCSLIFMUXID.
9892 	 */
9893 	if (mp2 == NULL) {
9894 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9895 		if (err == EINPROGRESS)
9896 			return;
9897 		goto done;
9898 	}
9899 
9900 	/*
9901 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9902 	 * ARP has appended this last mblk to tell us whether the lower stream
9903 	 * is an arp-dev stream or an IP module stream.
9904 	 */
9905 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9906 	if (ipmxp->ipmx_arpdev_stream) {
9907 		/*
9908 		 * The lower stream is the arp-dev stream.
9909 		 */
9910 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9911 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9912 		if (ill == NULL) {
9913 			if (err == EINPROGRESS)
9914 				return;
9915 			err = EINVAL;
9916 			goto done;
9917 		}
9918 
9919 		if (ipsq == NULL) {
9920 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9921 			    NEW_OP, B_FALSE);
9922 			if (ipsq == NULL) {
9923 				ill_refrele(ill);
9924 				return;
9925 			}
9926 			entered_ipsq = B_TRUE;
9927 		}
9928 		ASSERT(IAM_WRITER_ILL(ill));
9929 		ill_refrele(ill);
9930 
9931 		/*
9932 		 * To ensure consistency between IP and ARP, the following
9933 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9934 		 * This is because the muxid's are stored in the IP stream on
9935 		 * the ill.
9936 		 *
9937 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9938 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9939 		 * not yet plinked, and it also checks that the corresponding
9940 		 * IP stream is already plinked.
9941 		 *
9942 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9943 		 * punlinking the IP stream. IP does not allow punlink of the
9944 		 * IP stream unless the arp stream has been punlinked.
9945 		 */
9946 		if ((islink &&
9947 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9948 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9949 			err = EINVAL;
9950 			goto done;
9951 		}
9952 
9953 		if (IS_IPMP(ill) &&
9954 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9955 			goto done;
9956 
9957 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9958 	} else {
9959 		/*
9960 		 * The lower stream is probably an IP module stream.  Do
9961 		 * consistency checking.
9962 		 */
9963 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9964 		if (err == EINPROGRESS)
9965 			return;
9966 	}
9967 done:
9968 	if (err == 0)
9969 		miocack(q, mp, 0, 0);
9970 	else
9971 		miocnak(q, mp, 0, err);
9972 
9973 	/* Conn was refheld in ip_sioctl_copyin_setup */
9974 	if (CONN_Q(q))
9975 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9976 	if (entered_ipsq)
9977 		ipsq_exit(ipsq);
9978 }
9979 
9980 /*
9981  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9982  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9983  * module stream).  If `doconsist' is set, then do the extended consistency
9984  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9985  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9986  * an error code on failure.
9987  */
9988 static int
9989 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9990     struct linkblk *li, boolean_t doconsist)
9991 {
9992 	int		err = 0;
9993 	ill_t  		*ill;
9994 	queue_t		*ipwq, *dwq;
9995 	const char	*name;
9996 	struct qinit	*qinfo;
9997 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9998 	boolean_t	entered_ipsq = B_FALSE;
9999 
10000 	/*
10001 	 * Walk the lower stream to verify it's the IP module stream.
10002 	 * The IP module is identified by its name, wput function,
10003 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10004 	 * (li->l_qbot) will not vanish until this ioctl completes.
10005 	 */
10006 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10007 		qinfo = ipwq->q_qinfo;
10008 		name = qinfo->qi_minfo->mi_idname;
10009 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10010 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10011 			break;
10012 		}
10013 	}
10014 
10015 	/*
10016 	 * If this isn't an IP module stream, bail.
10017 	 */
10018 	if (ipwq == NULL)
10019 		return (0);
10020 
10021 	ill = ipwq->q_ptr;
10022 	ASSERT(ill != NULL);
10023 
10024 	if (ipsq == NULL) {
10025 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10026 		    NEW_OP, B_FALSE);
10027 		if (ipsq == NULL)
10028 			return (EINPROGRESS);
10029 		entered_ipsq = B_TRUE;
10030 	}
10031 	ASSERT(IAM_WRITER_ILL(ill));
10032 
10033 	if (doconsist) {
10034 		/*
10035 		 * Consistency checking requires that I_{P}LINK occurs
10036 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10037 		 * occurs prior to clearing ill_arp_muxid.
10038 		 */
10039 		if ((islink && ill->ill_ip_muxid != 0) ||
10040 		    (!islink && ill->ill_arp_muxid != 0)) {
10041 			err = EINVAL;
10042 			goto done;
10043 		}
10044 	}
10045 
10046 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
10047 		goto done;
10048 
10049 	/*
10050 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10051 	 * the read queue of the module immediately below IP in the ill.
10052 	 * These are used during the capability negotiation below.
10053 	 */
10054 	ill->ill_lmod_rq = NULL;
10055 	ill->ill_lmod_cnt = 0;
10056 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10057 		ill->ill_lmod_rq = RD(dwq);
10058 		for (; dwq != NULL; dwq = dwq->q_next)
10059 			ill->ill_lmod_cnt++;
10060 	}
10061 
10062 	if (doconsist)
10063 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10064 
10065 	/*
10066 	 * Mark the ipsq busy until the capability operations initiated below
10067 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
10068 	 * returns, but the capability operation may complete asynchronously
10069 	 * much later.
10070 	 */
10071 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
10072 	/*
10073 	 * If there's at least one up ipif on this ill, then we're bound to
10074 	 * the underlying driver via DLPI.  In that case, renegotiate
10075 	 * capabilities to account for any possible change in modules
10076 	 * interposed between IP and the driver.
10077 	 */
10078 	if (ill->ill_ipif_up_count > 0) {
10079 		if (islink)
10080 			ill_capability_probe(ill);
10081 		else
10082 			ill_capability_reset(ill, B_FALSE);
10083 	}
10084 	ipsq_current_finish(ipsq);
10085 done:
10086 	if (entered_ipsq)
10087 		ipsq_exit(ipsq);
10088 
10089 	return (err);
10090 }
10091 
10092 /*
10093  * Search the ioctl command in the ioctl tables and return a pointer
10094  * to the ioctl command information. The ioctl command tables are
10095  * static and fully populated at compile time.
10096  */
10097 ip_ioctl_cmd_t *
10098 ip_sioctl_lookup(int ioc_cmd)
10099 {
10100 	int index;
10101 	ip_ioctl_cmd_t *ipip;
10102 	ip_ioctl_cmd_t *ipip_end;
10103 
10104 	if (ioc_cmd == IPI_DONTCARE)
10105 		return (NULL);
10106 
10107 	/*
10108 	 * Do a 2 step search. First search the indexed table
10109 	 * based on the least significant byte of the ioctl cmd.
10110 	 * If we don't find a match, then search the misc table
10111 	 * serially.
10112 	 */
10113 	index = ioc_cmd & 0xFF;
10114 	if (index < ip_ndx_ioctl_count) {
10115 		ipip = &ip_ndx_ioctl_table[index];
10116 		if (ipip->ipi_cmd == ioc_cmd) {
10117 			/* Found a match in the ndx table */
10118 			return (ipip);
10119 		}
10120 	}
10121 
10122 	/* Search the misc table */
10123 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10124 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10125 		if (ipip->ipi_cmd == ioc_cmd)
10126 			/* Found a match in the misc table */
10127 			return (ipip);
10128 	}
10129 
10130 	return (NULL);
10131 }
10132 
10133 /*
10134  * Wrapper function for resuming deferred ioctl processing
10135  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10136  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10137  */
10138 /* ARGSUSED */
10139 void
10140 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10141     void *dummy_arg)
10142 {
10143 	ip_sioctl_copyin_setup(q, mp);
10144 }
10145 
10146 /*
10147  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10148  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10149  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10150  * We establish here the size of the block to be copied in.  mi_copyin
10151  * arranges for this to happen, an processing continues in ip_wput with
10152  * an M_IOCDATA message.
10153  */
10154 void
10155 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10156 {
10157 	int	copyin_size;
10158 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10159 	ip_ioctl_cmd_t *ipip;
10160 	cred_t *cr;
10161 	ip_stack_t	*ipst;
10162 
10163 	if (CONN_Q(q))
10164 		ipst = CONNQ_TO_IPST(q);
10165 	else
10166 		ipst = ILLQ_TO_IPST(q);
10167 
10168 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10169 	if (ipip == NULL) {
10170 		/*
10171 		 * The ioctl is not one we understand or own.
10172 		 * Pass it along to be processed down stream,
10173 		 * if this is a module instance of IP, else nak
10174 		 * the ioctl.
10175 		 */
10176 		if (q->q_next == NULL) {
10177 			goto nak;
10178 		} else {
10179 			putnext(q, mp);
10180 			return;
10181 		}
10182 	}
10183 
10184 	/*
10185 	 * If this is deferred, then we will do all the checks when we
10186 	 * come back.
10187 	 */
10188 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10189 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10190 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10191 		return;
10192 	}
10193 
10194 	/*
10195 	 * Only allow a very small subset of IP ioctls on this stream if
10196 	 * IP is a module and not a driver. Allowing ioctls to be processed
10197 	 * in this case may cause assert failures or data corruption.
10198 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10199 	 * ioctls allowed on an IP module stream, after which this stream
10200 	 * normally becomes a multiplexor (at which time the stream head
10201 	 * will fail all ioctls).
10202 	 */
10203 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10204 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10205 			/*
10206 			 * Pass common Streams ioctls which the IP
10207 			 * module does not own or consume along to
10208 			 * be processed down stream.
10209 			 */
10210 			putnext(q, mp);
10211 			return;
10212 		} else {
10213 			goto nak;
10214 		}
10215 	}
10216 
10217 	/* Make sure we have ioctl data to process. */
10218 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10219 		goto nak;
10220 
10221 	/*
10222 	 * Prefer dblk credential over ioctl credential; some synthesized
10223 	 * ioctls have kcred set because there's no way to crhold()
10224 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10225 	 * the framework; the caller of ioctl needs to hold the reference
10226 	 * for the duration of the call).
10227 	 */
10228 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10229 
10230 	/* Make sure normal users don't send down privileged ioctls */
10231 	if ((ipip->ipi_flags & IPI_PRIV) &&
10232 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10233 		/* We checked the privilege earlier but log it here */
10234 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10235 		return;
10236 	}
10237 
10238 	/*
10239 	 * The ioctl command tables can only encode fixed length
10240 	 * ioctl data. If the length is variable, the table will
10241 	 * encode the length as zero. Such special cases are handled
10242 	 * below in the switch.
10243 	 */
10244 	if (ipip->ipi_copyin_size != 0) {
10245 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10246 		return;
10247 	}
10248 
10249 	switch (iocp->ioc_cmd) {
10250 	case O_SIOCGIFCONF:
10251 	case SIOCGIFCONF:
10252 		/*
10253 		 * This IOCTL is hilarious.  See comments in
10254 		 * ip_sioctl_get_ifconf for the story.
10255 		 */
10256 		if (iocp->ioc_count == TRANSPARENT)
10257 			copyin_size = SIZEOF_STRUCT(ifconf,
10258 			    iocp->ioc_flag);
10259 		else
10260 			copyin_size = iocp->ioc_count;
10261 		mi_copyin(q, mp, NULL, copyin_size);
10262 		return;
10263 
10264 	case O_SIOCGLIFCONF:
10265 	case SIOCGLIFCONF:
10266 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10267 		mi_copyin(q, mp, NULL, copyin_size);
10268 		return;
10269 
10270 	case SIOCGLIFSRCOF:
10271 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10272 		mi_copyin(q, mp, NULL, copyin_size);
10273 		return;
10274 	case SIOCGIP6ADDRPOLICY:
10275 		ip_sioctl_ip6addrpolicy(q, mp);
10276 		ip6_asp_table_refrele(ipst);
10277 		return;
10278 
10279 	case SIOCSIP6ADDRPOLICY:
10280 		ip_sioctl_ip6addrpolicy(q, mp);
10281 		return;
10282 
10283 	case SIOCGDSTINFO:
10284 		ip_sioctl_dstinfo(q, mp);
10285 		ip6_asp_table_refrele(ipst);
10286 		return;
10287 
10288 	case I_PLINK:
10289 	case I_PUNLINK:
10290 	case I_LINK:
10291 	case I_UNLINK:
10292 		/*
10293 		 * We treat non-persistent link similarly as the persistent
10294 		 * link case, in terms of plumbing/unplumbing, as well as
10295 		 * dynamic re-plumbing events indicator.  See comments
10296 		 * in ip_sioctl_plink() for more.
10297 		 *
10298 		 * Request can be enqueued in the 'ipsq' while waiting
10299 		 * to become exclusive. So bump up the conn ref.
10300 		 */
10301 		if (CONN_Q(q))
10302 			CONN_INC_REF(Q_TO_CONN(q));
10303 		ip_sioctl_plink(NULL, q, mp, NULL);
10304 		return;
10305 
10306 	case ND_GET:
10307 	case ND_SET:
10308 		/*
10309 		 * Use of the nd table requires holding the reader lock.
10310 		 * Modifying the nd table thru nd_load/nd_unload requires
10311 		 * the writer lock.
10312 		 */
10313 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10314 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10315 			rw_exit(&ipst->ips_ip_g_nd_lock);
10316 
10317 			if (iocp->ioc_error)
10318 				iocp->ioc_count = 0;
10319 			mp->b_datap->db_type = M_IOCACK;
10320 			qreply(q, mp);
10321 			return;
10322 		}
10323 		rw_exit(&ipst->ips_ip_g_nd_lock);
10324 		/*
10325 		 * We don't understand this subioctl of ND_GET / ND_SET.
10326 		 * Maybe intended for some driver / module below us
10327 		 */
10328 		if (q->q_next) {
10329 			putnext(q, mp);
10330 		} else {
10331 			iocp->ioc_error = ENOENT;
10332 			mp->b_datap->db_type = M_IOCNAK;
10333 			iocp->ioc_count = 0;
10334 			qreply(q, mp);
10335 		}
10336 		return;
10337 
10338 	case IP_IOCTL:
10339 		ip_wput_ioctl(q, mp);
10340 		return;
10341 	default:
10342 		cmn_err(CE_PANIC, "should not happen ");
10343 	}
10344 nak:
10345 	if (mp->b_cont != NULL) {
10346 		freemsg(mp->b_cont);
10347 		mp->b_cont = NULL;
10348 	}
10349 	iocp->ioc_error = EINVAL;
10350 	mp->b_datap->db_type = M_IOCNAK;
10351 	iocp->ioc_count = 0;
10352 	qreply(q, mp);
10353 }
10354 
10355 /* ip_wput hands off ARP IOCTL responses to us */
10356 /* ARGSUSED3 */
10357 void
10358 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10359 {
10360 	struct arpreq *ar;
10361 	struct xarpreq *xar;
10362 	area_t	*area;
10363 	mblk_t	*area_mp;
10364 	struct iocblk *iocp;
10365 	mblk_t	*orig_ioc_mp, *tmp;
10366 	struct iocblk	*orig_iocp;
10367 	ill_t *ill;
10368 	conn_t *connp = NULL;
10369 	mblk_t *pending_mp;
10370 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10371 	int *flagsp;
10372 	char *storage = NULL;
10373 	sin_t *sin;
10374 	ipaddr_t addr;
10375 	int err;
10376 	ip_stack_t *ipst;
10377 
10378 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10379 	ill = q->q_ptr;
10380 	ASSERT(ill != NULL);
10381 	ipst = ill->ill_ipst;
10382 
10383 	/*
10384 	 * We should get back from ARP a packet chain that looks like:
10385 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10386 	 */
10387 	if (!(area_mp = mp->b_cont) ||
10388 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10389 	    !(orig_ioc_mp = area_mp->b_cont) ||
10390 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10391 		freemsg(mp);
10392 		return;
10393 	}
10394 
10395 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10396 
10397 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10398 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10399 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10400 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10401 		x_arp_ioctl = B_TRUE;
10402 		xar = (struct xarpreq *)tmp->b_rptr;
10403 		sin = (sin_t *)&xar->xarp_pa;
10404 		flagsp = &xar->xarp_flags;
10405 		storage = xar->xarp_ha.sdl_data;
10406 		if (xar->xarp_ha.sdl_nlen != 0)
10407 			ifx_arp_ioctl = B_TRUE;
10408 	} else {
10409 		ar = (struct arpreq *)tmp->b_rptr;
10410 		sin = (sin_t *)&ar->arp_pa;
10411 		flagsp = &ar->arp_flags;
10412 		storage = ar->arp_ha.sa_data;
10413 	}
10414 
10415 	iocp = (struct iocblk *)mp->b_rptr;
10416 
10417 	/*
10418 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10419 	 * Otherwise, we can find it from our ioc_id.
10420 	 */
10421 	if (ipsq != NULL)
10422 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10423 	else
10424 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10425 
10426 	if (pending_mp == NULL) {
10427 		ASSERT(connp == NULL);
10428 		inet_freemsg(mp);
10429 		return;
10430 	}
10431 	ASSERT(connp != NULL);
10432 	q = CONNP_TO_WQ(connp);
10433 
10434 	/* Uncouple the internally generated IOCTL from the original one */
10435 	area = (area_t *)area_mp->b_rptr;
10436 	area_mp->b_cont = NULL;
10437 
10438 	/*
10439 	 * Restore the b_next and b_prev used by mi code. This is needed
10440 	 * to complete the ioctl using mi* functions. We stored them in
10441 	 * the pending mp prior to sending the request to ARP.
10442 	 */
10443 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10444 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10445 	inet_freemsg(pending_mp);
10446 
10447 	/*
10448 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10449 	 * Catch the case where there is an IRE_CACHE by no entry in the
10450 	 * arp table.
10451 	 */
10452 	addr = sin->sin_addr.s_addr;
10453 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10454 		ire_t			*ire;
10455 		dl_unitdata_req_t	*dlup;
10456 		mblk_t			*llmp;
10457 		int			addr_len;
10458 		ill_t			*ipsqill = NULL;
10459 
10460 		if (ifx_arp_ioctl) {
10461 			/*
10462 			 * There's no need to lookup the ill, since
10463 			 * we've already done that when we started
10464 			 * processing the ioctl and sent the message
10465 			 * to ARP on that ill.  So use the ill that
10466 			 * is stored in q->q_ptr.
10467 			 */
10468 			ipsqill = ill;
10469 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10470 			    ipsqill->ill_ipif, ALL_ZONES,
10471 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10472 		} else {
10473 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10474 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10475 			if (ire != NULL)
10476 				ipsqill = ire_to_ill(ire);
10477 		}
10478 
10479 		if ((x_arp_ioctl) && (ipsqill != NULL))
10480 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10481 
10482 		if (ire != NULL) {
10483 			/*
10484 			 * Since the ire obtained from cachetable is used for
10485 			 * mac addr copying below, treat an incomplete ire as if
10486 			 * as if we never found it.
10487 			 */
10488 			if (ire->ire_nce != NULL &&
10489 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10490 				ire_refrele(ire);
10491 				ire = NULL;
10492 				ipsqill = NULL;
10493 				goto errack;
10494 			}
10495 			*flagsp = ATF_INUSE;
10496 			llmp = (ire->ire_nce != NULL ?
10497 			    ire->ire_nce->nce_res_mp : NULL);
10498 			if (llmp != NULL && ipsqill != NULL) {
10499 				uchar_t *macaddr;
10500 
10501 				addr_len = ipsqill->ill_phys_addr_length;
10502 				if (x_arp_ioctl && ((addr_len +
10503 				    ipsqill->ill_name_length) >
10504 				    sizeof (xar->xarp_ha.sdl_data))) {
10505 					ire_refrele(ire);
10506 					freemsg(mp);
10507 					ip_ioctl_finish(q, orig_ioc_mp,
10508 					    EINVAL, NO_COPYOUT, ipsq);
10509 					return;
10510 				}
10511 				*flagsp |= ATF_COM;
10512 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10513 				if (ipsqill->ill_sap_length < 0)
10514 					macaddr = llmp->b_rptr +
10515 					    dlup->dl_dest_addr_offset;
10516 				else
10517 					macaddr = llmp->b_rptr +
10518 					    dlup->dl_dest_addr_offset +
10519 					    ipsqill->ill_sap_length;
10520 				/*
10521 				 * For SIOCGARP, MAC address length
10522 				 * validation has already been done
10523 				 * before the ioctl was issued to ARP to
10524 				 * allow it to progress only on 6 byte
10525 				 * addressable (ethernet like) media. Thus
10526 				 * the mac address copying can not overwrite
10527 				 * the sa_data area below.
10528 				 */
10529 				bcopy(macaddr, storage, addr_len);
10530 			}
10531 			/* Ditch the internal IOCTL. */
10532 			freemsg(mp);
10533 			ire_refrele(ire);
10534 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10535 			return;
10536 		}
10537 	}
10538 
10539 	/*
10540 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10541 	 * on the IPMP meta-interface, ensure any ARP entries added in
10542 	 * ip_sioctl_arp() are deleted.
10543 	 */
10544 	if (IS_IPMP(ill) &&
10545 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10546 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10547 		ipmp_illgrp_t *illg = ill->ill_grp;
10548 		ipmp_arpent_t *entp;
10549 
10550 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10551 			ipmp_illgrp_destroy_arpent(illg, entp);
10552 	}
10553 
10554 	/*
10555 	 * Delete the coresponding IRE_CACHE if any.
10556 	 * Reset the error if there was one (in case there was no entry
10557 	 * in arp.)
10558 	 */
10559 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10560 		ipif_t *ipintf = NULL;
10561 
10562 		if (ifx_arp_ioctl) {
10563 			/*
10564 			 * There's no need to lookup the ill, since
10565 			 * we've already done that when we started
10566 			 * processing the ioctl and sent the message
10567 			 * to ARP on that ill.  So use the ill that
10568 			 * is stored in q->q_ptr.
10569 			 */
10570 			ipintf = ill->ill_ipif;
10571 		}
10572 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10573 			/*
10574 			 * The address in "addr" may be an entry for a
10575 			 * router. If that's true, then any off-net
10576 			 * IRE_CACHE entries that go through the router
10577 			 * with address "addr" must be clobbered. Use
10578 			 * ire_walk to achieve this goal.
10579 			 */
10580 			if (ifx_arp_ioctl)
10581 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10582 				    ire_delete_cache_gw, (char *)&addr, ill);
10583 			else
10584 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10585 				    ALL_ZONES, ipst);
10586 			iocp->ioc_error = 0;
10587 		}
10588 	}
10589 errack:
10590 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10591 		err = iocp->ioc_error;
10592 		freemsg(mp);
10593 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10594 		return;
10595 	}
10596 
10597 	/*
10598 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10599 	 * the area_t into the struct {x}arpreq.
10600 	 */
10601 	if (x_arp_ioctl) {
10602 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10603 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10604 		    sizeof (xar->xarp_ha.sdl_data)) {
10605 			freemsg(mp);
10606 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10607 			    ipsq);
10608 			return;
10609 		}
10610 	}
10611 	*flagsp = ATF_INUSE;
10612 	if (area->area_flags & ACE_F_PERMANENT)
10613 		*flagsp |= ATF_PERM;
10614 	if (area->area_flags & ACE_F_PUBLISH)
10615 		*flagsp |= ATF_PUBL;
10616 	if (area->area_flags & ACE_F_AUTHORITY)
10617 		*flagsp |= ATF_AUTHORITY;
10618 	if (area->area_hw_addr_length != 0) {
10619 		*flagsp |= ATF_COM;
10620 		/*
10621 		 * For SIOCGARP, MAC address length validation has
10622 		 * already been done before the ioctl was issued to ARP
10623 		 * to allow it to progress only on 6 byte addressable
10624 		 * (ethernet like) media. Thus the mac address copying
10625 		 * can not overwrite the sa_data area below.
10626 		 */
10627 		bcopy((char *)area + area->area_hw_addr_offset,
10628 		    storage, area->area_hw_addr_length);
10629 	}
10630 
10631 	/* Ditch the internal IOCTL. */
10632 	freemsg(mp);
10633 	/* Complete the original. */
10634 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10635 }
10636 
10637 /*
10638  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10639  * interface) create the next available logical interface for this
10640  * physical interface.
10641  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10642  * ipif with the specified name.
10643  *
10644  * If the address family is not AF_UNSPEC then set the address as well.
10645  *
10646  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10647  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10648  *
10649  * Executed as a writer on the ill.
10650  * So no lock is needed to traverse the ipif chain, or examine the
10651  * phyint flags.
10652  */
10653 /* ARGSUSED */
10654 int
10655 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10656     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10657 {
10658 	mblk_t	*mp1;
10659 	struct lifreq *lifr;
10660 	boolean_t	isv6;
10661 	boolean_t	exists;
10662 	char 	*name;
10663 	char	*endp;
10664 	char	*cp;
10665 	int	namelen;
10666 	ipif_t	*ipif;
10667 	long	id;
10668 	ipsq_t	*ipsq;
10669 	ill_t	*ill;
10670 	sin_t	*sin;
10671 	int	err = 0;
10672 	boolean_t found_sep = B_FALSE;
10673 	conn_t	*connp;
10674 	zoneid_t zoneid;
10675 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10676 
10677 	ASSERT(q->q_next == NULL);
10678 	ip1dbg(("ip_sioctl_addif\n"));
10679 	/* Existence of mp1 has been checked in ip_wput_nondata */
10680 	mp1 = mp->b_cont->b_cont;
10681 	/*
10682 	 * Null terminate the string to protect against buffer
10683 	 * overrun. String was generated by user code and may not
10684 	 * be trusted.
10685 	 */
10686 	lifr = (struct lifreq *)mp1->b_rptr;
10687 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10688 	name = lifr->lifr_name;
10689 	ASSERT(CONN_Q(q));
10690 	connp = Q_TO_CONN(q);
10691 	isv6 = connp->conn_af_isv6;
10692 	zoneid = connp->conn_zoneid;
10693 	namelen = mi_strlen(name);
10694 	if (namelen == 0)
10695 		return (EINVAL);
10696 
10697 	exists = B_FALSE;
10698 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10699 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10700 		/*
10701 		 * Allow creating lo0 using SIOCLIFADDIF.
10702 		 * can't be any other writer thread. So can pass null below
10703 		 * for the last 4 args to ipif_lookup_name.
10704 		 */
10705 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10706 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10707 		/* Prevent any further action */
10708 		if (ipif == NULL) {
10709 			return (ENOBUFS);
10710 		} else if (!exists) {
10711 			/* We created the ipif now and as writer */
10712 			ipif_refrele(ipif);
10713 			return (0);
10714 		} else {
10715 			ill = ipif->ipif_ill;
10716 			ill_refhold(ill);
10717 			ipif_refrele(ipif);
10718 		}
10719 	} else {
10720 		/* Look for a colon in the name. */
10721 		endp = &name[namelen];
10722 		for (cp = endp; --cp > name; ) {
10723 			if (*cp == IPIF_SEPARATOR_CHAR) {
10724 				found_sep = B_TRUE;
10725 				/*
10726 				 * Reject any non-decimal aliases for plumbing
10727 				 * of logical interfaces. Aliases with leading
10728 				 * zeroes are also rejected as they introduce
10729 				 * ambiguity in the naming of the interfaces.
10730 				 * Comparing with "0" takes care of all such
10731 				 * cases.
10732 				 */
10733 				if ((strncmp("0", cp+1, 1)) == 0)
10734 					return (EINVAL);
10735 
10736 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10737 				    id <= 0 || *endp != '\0') {
10738 					return (EINVAL);
10739 				}
10740 				*cp = '\0';
10741 				break;
10742 			}
10743 		}
10744 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10745 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10746 		if (found_sep)
10747 			*cp = IPIF_SEPARATOR_CHAR;
10748 		if (ill == NULL)
10749 			return (err);
10750 	}
10751 
10752 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10753 	    B_TRUE);
10754 
10755 	/*
10756 	 * Release the refhold due to the lookup, now that we are excl
10757 	 * or we are just returning
10758 	 */
10759 	ill_refrele(ill);
10760 
10761 	if (ipsq == NULL)
10762 		return (EINPROGRESS);
10763 
10764 	/* We are now exclusive on the IPSQ */
10765 	ASSERT(IAM_WRITER_ILL(ill));
10766 
10767 	if (found_sep) {
10768 		/* Now see if there is an IPIF with this unit number. */
10769 		for (ipif = ill->ill_ipif; ipif != NULL;
10770 		    ipif = ipif->ipif_next) {
10771 			if (ipif->ipif_id == id) {
10772 				err = EEXIST;
10773 				goto done;
10774 			}
10775 		}
10776 	}
10777 
10778 	/*
10779 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10780 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10781 	 * instead.
10782 	 */
10783 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10784 	    B_TRUE, B_TRUE)) == NULL) {
10785 		err = ENOBUFS;
10786 		goto done;
10787 	}
10788 
10789 	/* Return created name with ioctl */
10790 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10791 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10792 	ip1dbg(("created %s\n", lifr->lifr_name));
10793 
10794 	/* Set address */
10795 	sin = (sin_t *)&lifr->lifr_addr;
10796 	if (sin->sin_family != AF_UNSPEC) {
10797 		err = ip_sioctl_addr(ipif, sin, q, mp,
10798 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10799 	}
10800 
10801 done:
10802 	ipsq_exit(ipsq);
10803 	return (err);
10804 }
10805 
10806 /*
10807  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10808  * interface) delete it based on the IP address (on this physical interface).
10809  * Otherwise delete it based on the ipif_id.
10810  * Also, special handling to allow a removeif of lo0.
10811  */
10812 /* ARGSUSED */
10813 int
10814 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10815     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10816 {
10817 	conn_t		*connp;
10818 	ill_t		*ill = ipif->ipif_ill;
10819 	boolean_t	 success;
10820 	ip_stack_t	*ipst;
10821 
10822 	ipst = CONNQ_TO_IPST(q);
10823 
10824 	ASSERT(q->q_next == NULL);
10825 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10826 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10827 	ASSERT(IAM_WRITER_IPIF(ipif));
10828 
10829 	connp = Q_TO_CONN(q);
10830 	/*
10831 	 * Special case for unplumbing lo0 (the loopback physical interface).
10832 	 * If unplumbing lo0, the incoming address structure has been
10833 	 * initialized to all zeros. When unplumbing lo0, all its logical
10834 	 * interfaces must be removed too.
10835 	 *
10836 	 * Note that this interface may be called to remove a specific
10837 	 * loopback logical interface (eg, lo0:1). But in that case
10838 	 * ipif->ipif_id != 0 so that the code path for that case is the
10839 	 * same as any other interface (meaning it skips the code directly
10840 	 * below).
10841 	 */
10842 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10843 		if (sin->sin_family == AF_UNSPEC &&
10844 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10845 			/*
10846 			 * Mark it condemned. No new ref. will be made to ill.
10847 			 */
10848 			mutex_enter(&ill->ill_lock);
10849 			ill->ill_state_flags |= ILL_CONDEMNED;
10850 			for (ipif = ill->ill_ipif; ipif != NULL;
10851 			    ipif = ipif->ipif_next) {
10852 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10853 			}
10854 			mutex_exit(&ill->ill_lock);
10855 
10856 			ipif = ill->ill_ipif;
10857 			/* unplumb the loopback interface */
10858 			ill_delete(ill);
10859 			mutex_enter(&connp->conn_lock);
10860 			mutex_enter(&ill->ill_lock);
10861 
10862 			/* Are any references to this ill active */
10863 			if (ill_is_freeable(ill)) {
10864 				mutex_exit(&ill->ill_lock);
10865 				mutex_exit(&connp->conn_lock);
10866 				ill_delete_tail(ill);
10867 				mi_free(ill);
10868 				return (0);
10869 			}
10870 			success = ipsq_pending_mp_add(connp, ipif,
10871 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10872 			mutex_exit(&connp->conn_lock);
10873 			mutex_exit(&ill->ill_lock);
10874 			if (success)
10875 				return (EINPROGRESS);
10876 			else
10877 				return (EINTR);
10878 		}
10879 	}
10880 
10881 	if (ipif->ipif_id == 0) {
10882 		ipsq_t *ipsq;
10883 
10884 		/* Find based on address */
10885 		if (ipif->ipif_isv6) {
10886 			sin6_t *sin6;
10887 
10888 			if (sin->sin_family != AF_INET6)
10889 				return (EAFNOSUPPORT);
10890 
10891 			sin6 = (sin6_t *)sin;
10892 			/* We are a writer, so we should be able to lookup */
10893 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10894 			    ipst);
10895 		} else {
10896 			if (sin->sin_family != AF_INET)
10897 				return (EAFNOSUPPORT);
10898 
10899 			/* We are a writer, so we should be able to lookup */
10900 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10901 			    ipst);
10902 		}
10903 		if (ipif == NULL) {
10904 			return (EADDRNOTAVAIL);
10905 		}
10906 
10907 		/*
10908 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10909 		 * lifr_name of the physical interface but with an ip address
10910 		 * lifr_addr of a logical interface plumbed over it.
10911 		 * So update ipx_current_ipif now that ipif points to the
10912 		 * correct one.
10913 		 */
10914 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10915 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10916 
10917 		/* This is a writer */
10918 		ipif_refrele(ipif);
10919 	}
10920 
10921 	/*
10922 	 * Can not delete instance zero since it is tied to the ill.
10923 	 */
10924 	if (ipif->ipif_id == 0)
10925 		return (EBUSY);
10926 
10927 	mutex_enter(&ill->ill_lock);
10928 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10929 	mutex_exit(&ill->ill_lock);
10930 
10931 	ipif_free(ipif);
10932 
10933 	mutex_enter(&connp->conn_lock);
10934 	mutex_enter(&ill->ill_lock);
10935 
10936 
10937 	/* Are any references to this ipif active */
10938 	if (ipif_is_freeable(ipif)) {
10939 		mutex_exit(&ill->ill_lock);
10940 		mutex_exit(&connp->conn_lock);
10941 		ipif_non_duplicate(ipif);
10942 		ipif_down_tail(ipif);
10943 		ipif_free_tail(ipif); /* frees ipif */
10944 		return (0);
10945 	}
10946 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10947 	    IPIF_FREE);
10948 	mutex_exit(&ill->ill_lock);
10949 	mutex_exit(&connp->conn_lock);
10950 	if (success)
10951 		return (EINPROGRESS);
10952 	else
10953 		return (EINTR);
10954 }
10955 
10956 /*
10957  * Restart the removeif ioctl. The refcnt has gone down to 0.
10958  * The ipif is already condemned. So can't find it thru lookups.
10959  */
10960 /* ARGSUSED */
10961 int
10962 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10963     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10964 {
10965 	ill_t *ill = ipif->ipif_ill;
10966 
10967 	ASSERT(IAM_WRITER_IPIF(ipif));
10968 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10969 
10970 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10971 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10972 
10973 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10974 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10975 		ill_delete_tail(ill);
10976 		mi_free(ill);
10977 		return (0);
10978 	}
10979 
10980 	ipif_non_duplicate(ipif);
10981 	ipif_down_tail(ipif);
10982 	ipif_free_tail(ipif);
10983 
10984 	ILL_UNMARK_CHANGING(ill);
10985 	return (0);
10986 }
10987 
10988 /*
10989  * Set the local interface address.
10990  * Allow an address of all zero when the interface is down.
10991  */
10992 /* ARGSUSED */
10993 int
10994 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10995     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10996 {
10997 	int err = 0;
10998 	in6_addr_t v6addr;
10999 	boolean_t need_up = B_FALSE;
11000 
11001 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11002 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11003 
11004 	ASSERT(IAM_WRITER_IPIF(ipif));
11005 
11006 	if (ipif->ipif_isv6) {
11007 		sin6_t *sin6;
11008 		ill_t *ill;
11009 		phyint_t *phyi;
11010 
11011 		if (sin->sin_family != AF_INET6)
11012 			return (EAFNOSUPPORT);
11013 
11014 		sin6 = (sin6_t *)sin;
11015 		v6addr = sin6->sin6_addr;
11016 		ill = ipif->ipif_ill;
11017 		phyi = ill->ill_phyint;
11018 
11019 		/*
11020 		 * Enforce that true multicast interfaces have a link-local
11021 		 * address for logical unit 0.
11022 		 */
11023 		if (ipif->ipif_id == 0 &&
11024 		    (ill->ill_flags & ILLF_MULTICAST) &&
11025 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11026 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11027 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11028 			return (EADDRNOTAVAIL);
11029 		}
11030 
11031 		/*
11032 		 * up interfaces shouldn't have the unspecified address
11033 		 * unless they also have the IPIF_NOLOCAL flags set and
11034 		 * have a subnet assigned.
11035 		 */
11036 		if ((ipif->ipif_flags & IPIF_UP) &&
11037 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11038 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11039 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11040 			return (EADDRNOTAVAIL);
11041 		}
11042 
11043 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11044 			return (EADDRNOTAVAIL);
11045 	} else {
11046 		ipaddr_t addr;
11047 
11048 		if (sin->sin_family != AF_INET)
11049 			return (EAFNOSUPPORT);
11050 
11051 		addr = sin->sin_addr.s_addr;
11052 
11053 		/* Allow 0 as the local address. */
11054 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11055 			return (EADDRNOTAVAIL);
11056 
11057 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11058 	}
11059 
11060 	/*
11061 	 * Even if there is no change we redo things just to rerun
11062 	 * ipif_set_default.
11063 	 */
11064 	if (ipif->ipif_flags & IPIF_UP) {
11065 		/*
11066 		 * Setting a new local address, make sure
11067 		 * we have net and subnet bcast ire's for
11068 		 * the old address if we need them.
11069 		 */
11070 		if (!ipif->ipif_isv6)
11071 			ipif_check_bcast_ires(ipif);
11072 		/*
11073 		 * If the interface is already marked up,
11074 		 * we call ipif_down which will take care
11075 		 * of ditching any IREs that have been set
11076 		 * up based on the old interface address.
11077 		 */
11078 		err = ipif_logical_down(ipif, q, mp);
11079 		if (err == EINPROGRESS)
11080 			return (err);
11081 		ipif_down_tail(ipif);
11082 		need_up = 1;
11083 	}
11084 
11085 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11086 	return (err);
11087 }
11088 
11089 int
11090 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11091     boolean_t need_up)
11092 {
11093 	in6_addr_t v6addr;
11094 	in6_addr_t ov6addr;
11095 	ipaddr_t addr;
11096 	sin6_t	*sin6;
11097 	int	sinlen;
11098 	int	err = 0;
11099 	ill_t	*ill = ipif->ipif_ill;
11100 	boolean_t need_dl_down;
11101 	boolean_t need_arp_down;
11102 	struct iocblk *iocp;
11103 
11104 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11105 
11106 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11107 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11108 	ASSERT(IAM_WRITER_IPIF(ipif));
11109 
11110 	/* Must cancel any pending timer before taking the ill_lock */
11111 	if (ipif->ipif_recovery_id != 0)
11112 		(void) untimeout(ipif->ipif_recovery_id);
11113 	ipif->ipif_recovery_id = 0;
11114 
11115 	if (ipif->ipif_isv6) {
11116 		sin6 = (sin6_t *)sin;
11117 		v6addr = sin6->sin6_addr;
11118 		sinlen = sizeof (struct sockaddr_in6);
11119 	} else {
11120 		addr = sin->sin_addr.s_addr;
11121 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11122 		sinlen = sizeof (struct sockaddr_in);
11123 	}
11124 	mutex_enter(&ill->ill_lock);
11125 	ov6addr = ipif->ipif_v6lcl_addr;
11126 	ipif->ipif_v6lcl_addr = v6addr;
11127 	sctp_update_ipif_addr(ipif, ov6addr);
11128 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11129 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11130 	} else {
11131 		ipif->ipif_v6src_addr = v6addr;
11132 	}
11133 	ipif->ipif_addr_ready = 0;
11134 
11135 	/*
11136 	 * If the interface was previously marked as a duplicate, then since
11137 	 * we've now got a "new" address, it should no longer be considered a
11138 	 * duplicate -- even if the "new" address is the same as the old one.
11139 	 * Note that if all ipifs are down, we may have a pending ARP down
11140 	 * event to handle.  This is because we want to recover from duplicates
11141 	 * and thus delay tearing down ARP until the duplicates have been
11142 	 * removed or disabled.
11143 	 */
11144 	need_dl_down = need_arp_down = B_FALSE;
11145 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11146 		need_arp_down = !need_up;
11147 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11148 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11149 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11150 			need_dl_down = B_TRUE;
11151 		}
11152 	}
11153 
11154 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11155 	    !ill->ill_is_6to4tun) {
11156 		queue_t *wqp = ill->ill_wq;
11157 
11158 		/*
11159 		 * The local address of this interface is a 6to4 address,
11160 		 * check if this interface is in fact a 6to4 tunnel or just
11161 		 * an interface configured with a 6to4 address.  We are only
11162 		 * interested in the former.
11163 		 */
11164 		if (wqp != NULL) {
11165 			while ((wqp->q_next != NULL) &&
11166 			    (wqp->q_next->q_qinfo != NULL) &&
11167 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11168 
11169 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11170 				    == TUN6TO4_MODID) {
11171 					/* set for use in IP */
11172 					ill->ill_is_6to4tun = 1;
11173 					break;
11174 				}
11175 				wqp = wqp->q_next;
11176 			}
11177 		}
11178 	}
11179 
11180 	ipif_set_default(ipif);
11181 
11182 	/*
11183 	 * When publishing an interface address change event, we only notify
11184 	 * the event listeners of the new address.  It is assumed that if they
11185 	 * actively care about the addresses assigned that they will have
11186 	 * already discovered the previous address assigned (if there was one.)
11187 	 *
11188 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11189 	 */
11190 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11191 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11192 		    NE_ADDRESS_CHANGE, sin, sinlen);
11193 	}
11194 
11195 	mutex_exit(&ill->ill_lock);
11196 
11197 	if (need_up) {
11198 		/*
11199 		 * Now bring the interface back up.  If this
11200 		 * is the only IPIF for the ILL, ipif_up
11201 		 * will have to re-bind to the device, so
11202 		 * we may get back EINPROGRESS, in which
11203 		 * case, this IOCTL will get completed in
11204 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11205 		 */
11206 		err = ipif_up(ipif, q, mp);
11207 	}
11208 
11209 	if (need_dl_down)
11210 		ill_dl_down(ill);
11211 	if (need_arp_down)
11212 		ipif_resolver_down(ipif);
11213 
11214 	return (err);
11215 }
11216 
11217 
11218 /*
11219  * Restart entry point to restart the address set operation after the
11220  * refcounts have dropped to zero.
11221  */
11222 /* ARGSUSED */
11223 int
11224 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11225     ip_ioctl_cmd_t *ipip, void *ifreq)
11226 {
11227 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11228 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11229 	ASSERT(IAM_WRITER_IPIF(ipif));
11230 	ipif_down_tail(ipif);
11231 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11232 }
11233 
11234 /* ARGSUSED */
11235 int
11236 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11237     ip_ioctl_cmd_t *ipip, void *if_req)
11238 {
11239 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11240 	struct lifreq *lifr = (struct lifreq *)if_req;
11241 
11242 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11243 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11244 	/*
11245 	 * The net mask and address can't change since we have a
11246 	 * reference to the ipif. So no lock is necessary.
11247 	 */
11248 	if (ipif->ipif_isv6) {
11249 		*sin6 = sin6_null;
11250 		sin6->sin6_family = AF_INET6;
11251 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11252 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11253 		lifr->lifr_addrlen =
11254 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11255 	} else {
11256 		*sin = sin_null;
11257 		sin->sin_family = AF_INET;
11258 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11259 		if (ipip->ipi_cmd_type == LIF_CMD) {
11260 			lifr->lifr_addrlen =
11261 			    ip_mask_to_plen(ipif->ipif_net_mask);
11262 		}
11263 	}
11264 	return (0);
11265 }
11266 
11267 /*
11268  * Set the destination address for a pt-pt interface.
11269  */
11270 /* ARGSUSED */
11271 int
11272 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11273     ip_ioctl_cmd_t *ipip, void *if_req)
11274 {
11275 	int err = 0;
11276 	in6_addr_t v6addr;
11277 	boolean_t need_up = B_FALSE;
11278 
11279 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11280 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11281 	ASSERT(IAM_WRITER_IPIF(ipif));
11282 
11283 	if (ipif->ipif_isv6) {
11284 		sin6_t *sin6;
11285 
11286 		if (sin->sin_family != AF_INET6)
11287 			return (EAFNOSUPPORT);
11288 
11289 		sin6 = (sin6_t *)sin;
11290 		v6addr = sin6->sin6_addr;
11291 
11292 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11293 			return (EADDRNOTAVAIL);
11294 	} else {
11295 		ipaddr_t addr;
11296 
11297 		if (sin->sin_family != AF_INET)
11298 			return (EAFNOSUPPORT);
11299 
11300 		addr = sin->sin_addr.s_addr;
11301 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11302 			return (EADDRNOTAVAIL);
11303 
11304 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11305 	}
11306 
11307 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11308 		return (0);	/* No change */
11309 
11310 	if (ipif->ipif_flags & IPIF_UP) {
11311 		/*
11312 		 * If the interface is already marked up,
11313 		 * we call ipif_down which will take care
11314 		 * of ditching any IREs that have been set
11315 		 * up based on the old pp dst address.
11316 		 */
11317 		err = ipif_logical_down(ipif, q, mp);
11318 		if (err == EINPROGRESS)
11319 			return (err);
11320 		ipif_down_tail(ipif);
11321 		need_up = B_TRUE;
11322 	}
11323 	/*
11324 	 * could return EINPROGRESS. If so ioctl will complete in
11325 	 * ip_rput_dlpi_writer
11326 	 */
11327 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11328 	return (err);
11329 }
11330 
11331 static int
11332 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11333     boolean_t need_up)
11334 {
11335 	in6_addr_t v6addr;
11336 	ill_t	*ill = ipif->ipif_ill;
11337 	int	err = 0;
11338 	boolean_t need_dl_down;
11339 	boolean_t need_arp_down;
11340 
11341 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11342 	    ipif->ipif_id, (void *)ipif));
11343 
11344 	/* Must cancel any pending timer before taking the ill_lock */
11345 	if (ipif->ipif_recovery_id != 0)
11346 		(void) untimeout(ipif->ipif_recovery_id);
11347 	ipif->ipif_recovery_id = 0;
11348 
11349 	if (ipif->ipif_isv6) {
11350 		sin6_t *sin6;
11351 
11352 		sin6 = (sin6_t *)sin;
11353 		v6addr = sin6->sin6_addr;
11354 	} else {
11355 		ipaddr_t addr;
11356 
11357 		addr = sin->sin_addr.s_addr;
11358 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11359 	}
11360 	mutex_enter(&ill->ill_lock);
11361 	/* Set point to point destination address. */
11362 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11363 		/*
11364 		 * Allow this as a means of creating logical
11365 		 * pt-pt interfaces on top of e.g. an Ethernet.
11366 		 * XXX Undocumented HACK for testing.
11367 		 * pt-pt interfaces are created with NUD disabled.
11368 		 */
11369 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11370 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11371 		if (ipif->ipif_isv6)
11372 			ill->ill_flags |= ILLF_NONUD;
11373 	}
11374 
11375 	/*
11376 	 * If the interface was previously marked as a duplicate, then since
11377 	 * we've now got a "new" address, it should no longer be considered a
11378 	 * duplicate -- even if the "new" address is the same as the old one.
11379 	 * Note that if all ipifs are down, we may have a pending ARP down
11380 	 * event to handle.
11381 	 */
11382 	need_dl_down = need_arp_down = B_FALSE;
11383 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11384 		need_arp_down = !need_up;
11385 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11386 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11387 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11388 			need_dl_down = B_TRUE;
11389 		}
11390 	}
11391 
11392 	/* Set the new address. */
11393 	ipif->ipif_v6pp_dst_addr = v6addr;
11394 	/* Make sure subnet tracks pp_dst */
11395 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11396 	mutex_exit(&ill->ill_lock);
11397 
11398 	if (need_up) {
11399 		/*
11400 		 * Now bring the interface back up.  If this
11401 		 * is the only IPIF for the ILL, ipif_up
11402 		 * will have to re-bind to the device, so
11403 		 * we may get back EINPROGRESS, in which
11404 		 * case, this IOCTL will get completed in
11405 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11406 		 */
11407 		err = ipif_up(ipif, q, mp);
11408 	}
11409 
11410 	if (need_dl_down)
11411 		ill_dl_down(ill);
11412 	if (need_arp_down)
11413 		ipif_resolver_down(ipif);
11414 
11415 	return (err);
11416 }
11417 
11418 /*
11419  * Restart entry point to restart the dstaddress set operation after the
11420  * refcounts have dropped to zero.
11421  */
11422 /* ARGSUSED */
11423 int
11424 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11425     ip_ioctl_cmd_t *ipip, void *ifreq)
11426 {
11427 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11428 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11429 	ipif_down_tail(ipif);
11430 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11431 }
11432 
11433 /* ARGSUSED */
11434 int
11435 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11436     ip_ioctl_cmd_t *ipip, void *if_req)
11437 {
11438 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11439 
11440 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11441 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11442 	/*
11443 	 * Get point to point destination address. The addresses can't
11444 	 * change since we hold a reference to the ipif.
11445 	 */
11446 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11447 		return (EADDRNOTAVAIL);
11448 
11449 	if (ipif->ipif_isv6) {
11450 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11451 		*sin6 = sin6_null;
11452 		sin6->sin6_family = AF_INET6;
11453 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11454 	} else {
11455 		*sin = sin_null;
11456 		sin->sin_family = AF_INET;
11457 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11458 	}
11459 	return (0);
11460 }
11461 
11462 /*
11463  * Set interface flags.  Many flags require special handling (e.g.,
11464  * bringing the interface down); see below for details.
11465  *
11466  * NOTE : We really don't enforce that ipif_id zero should be used
11467  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11468  *	  is because applications generally does SICGLIFFLAGS and
11469  *	  ORs in the new flags (that affects the logical) and does a
11470  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11471  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11472  *	  flags that will be turned on is correct with respect to
11473  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11474  */
11475 /* ARGSUSED */
11476 int
11477 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11478     ip_ioctl_cmd_t *ipip, void *if_req)
11479 {
11480 	uint64_t turn_on;
11481 	uint64_t turn_off;
11482 	int	err = 0;
11483 	phyint_t *phyi;
11484 	ill_t *ill;
11485 	uint64_t intf_flags, cantchange_flags;
11486 	boolean_t phyint_flags_modified = B_FALSE;
11487 	uint64_t flags;
11488 	struct ifreq *ifr;
11489 	struct lifreq *lifr;
11490 	boolean_t set_linklocal = B_FALSE;
11491 	boolean_t zero_source = B_FALSE;
11492 
11493 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11494 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11495 
11496 	ASSERT(IAM_WRITER_IPIF(ipif));
11497 
11498 	ill = ipif->ipif_ill;
11499 	phyi = ill->ill_phyint;
11500 
11501 	if (ipip->ipi_cmd_type == IF_CMD) {
11502 		ifr = (struct ifreq *)if_req;
11503 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11504 	} else {
11505 		lifr = (struct lifreq *)if_req;
11506 		flags = lifr->lifr_flags;
11507 	}
11508 
11509 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11510 
11511 	/*
11512 	 * Have the flags been set correctly until now?
11513 	 */
11514 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11515 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11516 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11517 	/*
11518 	 * Compare the new flags to the old, and partition
11519 	 * into those coming on and those going off.
11520 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11521 	 */
11522 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11523 		flags |= intf_flags & ~0xFFFF;
11524 
11525 	/*
11526 	 * Explicitly fail attempts to change flags that are always invalid on
11527 	 * an IPMP meta-interface.
11528 	 */
11529 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11530 		return (EINVAL);
11531 
11532 	/*
11533 	 * Check which flags will change; silently ignore flags which userland
11534 	 * is not allowed to control.  (Because these flags may change between
11535 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11536 	 * control, we need to silently ignore them rather than fail.)
11537 	 */
11538 	cantchange_flags = IFF_CANTCHANGE;
11539 	if (IS_IPMP(ill))
11540 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11541 
11542 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11543 	if (turn_on == 0)
11544 		return (0);	/* No change */
11545 
11546 	turn_off = intf_flags & turn_on;
11547 	turn_on ^= turn_off;
11548 
11549 	/*
11550 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11551 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11552 	 * allow it to be turned off.
11553 	 */
11554 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11555 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11556 		return (EINVAL);
11557 
11558 	if (turn_on & IFF_NOFAILOVER) {
11559 		turn_on |= IFF_DEPRECATED;
11560 		flags |= IFF_DEPRECATED;
11561 	}
11562 
11563 	/*
11564 	 * On underlying interfaces, only allow applications to manage test
11565 	 * addresses -- otherwise, they may get confused when the address
11566 	 * moves as part of being brought up.  Likewise, prevent an
11567 	 * application-managed test address from being converted to a data
11568 	 * address.  To prevent migration of administratively up addresses in
11569 	 * the kernel, we don't allow them to be converted either.
11570 	 */
11571 	if (IS_UNDER_IPMP(ill)) {
11572 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11573 
11574 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11575 			return (EINVAL);
11576 
11577 		if ((turn_off & IFF_NOFAILOVER) &&
11578 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11579 			return (EINVAL);
11580 	}
11581 
11582 	/*
11583 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11584 	 * IPv6 interfaces.
11585 	 */
11586 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11587 		return (EINVAL);
11588 
11589 	/*
11590 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11591 	 */
11592 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11593 		return (EINVAL);
11594 
11595 	/*
11596 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11597 	 * interfaces.  It makes no sense in that context.
11598 	 */
11599 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11600 		return (EINVAL);
11601 
11602 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11603 		zero_source = B_TRUE;
11604 
11605 	/*
11606 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11607 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11608 	 * If the link local address isn't set, and can be set, it will get
11609 	 * set later on in this function.
11610 	 */
11611 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11612 	    (flags & IFF_UP) && !zero_source &&
11613 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11614 		if (ipif_cant_setlinklocal(ipif))
11615 			return (EINVAL);
11616 		set_linklocal = B_TRUE;
11617 	}
11618 
11619 	/*
11620 	 * If we modify physical interface flags, we'll potentially need to
11621 	 * send up two routing socket messages for the changes (one for the
11622 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11623 	 */
11624 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11625 		phyint_flags_modified = B_TRUE;
11626 
11627 	/*
11628 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11629 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11630 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11631 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11632 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11633 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11634 	 * will not be honored.
11635 	 */
11636 	if (turn_on & PHYI_STANDBY) {
11637 		/*
11638 		 * No need to grab ill_g_usesrc_lock here; see the
11639 		 * synchronization notes in ip.c.
11640 		 */
11641 		if (ill->ill_usesrc_grp_next != NULL ||
11642 		    intf_flags & PHYI_INACTIVE)
11643 			return (EINVAL);
11644 		if (!(flags & PHYI_FAILED)) {
11645 			flags |= PHYI_INACTIVE;
11646 			turn_on |= PHYI_INACTIVE;
11647 		}
11648 	}
11649 
11650 	if (turn_off & PHYI_STANDBY) {
11651 		flags &= ~PHYI_INACTIVE;
11652 		turn_off |= PHYI_INACTIVE;
11653 	}
11654 
11655 	/*
11656 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11657 	 * would end up on.
11658 	 */
11659 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11660 	    (PHYI_FAILED | PHYI_INACTIVE))
11661 		return (EINVAL);
11662 
11663 	/*
11664 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11665 	 * status of the interface.
11666 	 */
11667 	if ((turn_on | turn_off) & ILLF_ROUTER)
11668 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11669 
11670 	/*
11671 	 * If the interface is not UP and we are not going to
11672 	 * bring it UP, record the flags and return. When the
11673 	 * interface comes UP later, the right actions will be
11674 	 * taken.
11675 	 */
11676 	if (!(ipif->ipif_flags & IPIF_UP) &&
11677 	    !(turn_on & IPIF_UP)) {
11678 		/* Record new flags in their respective places. */
11679 		mutex_enter(&ill->ill_lock);
11680 		mutex_enter(&ill->ill_phyint->phyint_lock);
11681 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11682 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11683 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11684 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11685 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11686 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11687 		mutex_exit(&ill->ill_lock);
11688 		mutex_exit(&ill->ill_phyint->phyint_lock);
11689 
11690 		/*
11691 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11692 		 * same to the kernel: if any of them has been set by
11693 		 * userland, the interface cannot be used for data traffic.
11694 		 */
11695 		if ((turn_on|turn_off) &
11696 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11697 			ASSERT(!IS_IPMP(ill));
11698 			/*
11699 			 * It's possible the ill is part of an "anonymous"
11700 			 * IPMP group rather than a real group.  In that case,
11701 			 * there are no other interfaces in the group and thus
11702 			 * no need to call ipmp_phyint_refresh_active().
11703 			 */
11704 			if (IS_UNDER_IPMP(ill))
11705 				ipmp_phyint_refresh_active(phyi);
11706 		}
11707 
11708 		if (phyint_flags_modified) {
11709 			if (phyi->phyint_illv4 != NULL) {
11710 				ip_rts_ifmsg(phyi->phyint_illv4->
11711 				    ill_ipif, RTSQ_DEFAULT);
11712 			}
11713 			if (phyi->phyint_illv6 != NULL) {
11714 				ip_rts_ifmsg(phyi->phyint_illv6->
11715 				    ill_ipif, RTSQ_DEFAULT);
11716 			}
11717 		}
11718 		return (0);
11719 	} else if (set_linklocal || zero_source) {
11720 		mutex_enter(&ill->ill_lock);
11721 		if (set_linklocal)
11722 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11723 		if (zero_source)
11724 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11725 		mutex_exit(&ill->ill_lock);
11726 	}
11727 
11728 	/*
11729 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11730 	 * or point-to-point interfaces with an unspecified destination. We do
11731 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11732 	 * have a subnet assigned, which is how in.ndpd currently manages its
11733 	 * onlink prefix list when no addresses are configured with those
11734 	 * prefixes.
11735 	 */
11736 	if (ipif->ipif_isv6 &&
11737 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11738 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11739 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11740 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11741 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11742 		return (EINVAL);
11743 	}
11744 
11745 	/*
11746 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11747 	 * from being brought up.
11748 	 */
11749 	if (!ipif->ipif_isv6 &&
11750 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11751 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11752 		return (EINVAL);
11753 	}
11754 
11755 	/*
11756 	 * The only flag changes that we currently take specific action on are
11757 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11758 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11759 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11760 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11761 	 * of bringing it back up will trigger the address to be moved.
11762 	 */
11763 	if ((turn_on|turn_off) &
11764 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11765 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11766 	    IPIF_NOFAILOVER)) {
11767 		/*
11768 		 * Taking this ipif down, make sure we have
11769 		 * valid net and subnet bcast ire's for other
11770 		 * logical interfaces, if we need them.
11771 		 */
11772 		if (!ipif->ipif_isv6)
11773 			ipif_check_bcast_ires(ipif);
11774 
11775 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11776 		    !(turn_off & IPIF_UP)) {
11777 			if (ipif->ipif_flags & IPIF_UP)
11778 				ill->ill_logical_down = 1;
11779 			turn_on &= ~IPIF_UP;
11780 		}
11781 		err = ipif_down(ipif, q, mp);
11782 		ip1dbg(("ipif_down returns %d err ", err));
11783 		if (err == EINPROGRESS)
11784 			return (err);
11785 		ipif_down_tail(ipif);
11786 	}
11787 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11788 }
11789 
11790 static int
11791 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11792 {
11793 	ill_t	*ill;
11794 	phyint_t *phyi;
11795 	uint64_t turn_on, turn_off;
11796 	uint64_t intf_flags, cantchange_flags;
11797 	boolean_t phyint_flags_modified = B_FALSE;
11798 	int	err = 0;
11799 	boolean_t set_linklocal = B_FALSE;
11800 	boolean_t zero_source = B_FALSE;
11801 
11802 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11803 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11804 
11805 	ASSERT(IAM_WRITER_IPIF(ipif));
11806 
11807 	ill = ipif->ipif_ill;
11808 	phyi = ill->ill_phyint;
11809 
11810 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11811 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11812 	if (IS_IPMP(ill))
11813 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11814 
11815 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11816 	turn_off = intf_flags & turn_on;
11817 	turn_on ^= turn_off;
11818 
11819 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11820 		phyint_flags_modified = B_TRUE;
11821 
11822 	/*
11823 	 * Now we change the flags. Track current value of
11824 	 * other flags in their respective places.
11825 	 */
11826 	mutex_enter(&ill->ill_lock);
11827 	mutex_enter(&phyi->phyint_lock);
11828 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11829 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11830 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11831 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11832 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11833 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11834 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11835 		set_linklocal = B_TRUE;
11836 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11837 	}
11838 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11839 		zero_source = B_TRUE;
11840 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11841 	}
11842 	mutex_exit(&ill->ill_lock);
11843 	mutex_exit(&phyi->phyint_lock);
11844 
11845 	if (set_linklocal)
11846 		(void) ipif_setlinklocal(ipif);
11847 
11848 	if (zero_source)
11849 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11850 	else
11851 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11852 
11853 	/*
11854 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11855 	 * the kernel: if any of them has been set by userland, the interface
11856 	 * cannot be used for data traffic.
11857 	 */
11858 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11859 		ASSERT(!IS_IPMP(ill));
11860 		/*
11861 		 * It's possible the ill is part of an "anonymous" IPMP group
11862 		 * rather than a real group.  In that case, there are no other
11863 		 * interfaces in the group and thus no need for us to call
11864 		 * ipmp_phyint_refresh_active().
11865 		 */
11866 		if (IS_UNDER_IPMP(ill))
11867 			ipmp_phyint_refresh_active(phyi);
11868 	}
11869 
11870 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11871 		/*
11872 		 * XXX ipif_up really does not know whether a phyint flags
11873 		 * was modified or not. So, it sends up information on
11874 		 * only one routing sockets message. As we don't bring up
11875 		 * the interface and also set PHYI_ flags simultaneously
11876 		 * it should be okay.
11877 		 */
11878 		err = ipif_up(ipif, q, mp);
11879 	} else {
11880 		/*
11881 		 * Make sure routing socket sees all changes to the flags.
11882 		 * ipif_up_done* handles this when we use ipif_up.
11883 		 */
11884 		if (phyint_flags_modified) {
11885 			if (phyi->phyint_illv4 != NULL) {
11886 				ip_rts_ifmsg(phyi->phyint_illv4->
11887 				    ill_ipif, RTSQ_DEFAULT);
11888 			}
11889 			if (phyi->phyint_illv6 != NULL) {
11890 				ip_rts_ifmsg(phyi->phyint_illv6->
11891 				    ill_ipif, RTSQ_DEFAULT);
11892 			}
11893 		} else {
11894 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11895 		}
11896 		/*
11897 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11898 		 * this in need_up case.
11899 		 */
11900 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11901 	}
11902 	return (err);
11903 }
11904 
11905 /*
11906  * Restart the flags operation now that the refcounts have dropped to zero.
11907  */
11908 /* ARGSUSED */
11909 int
11910 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11911     ip_ioctl_cmd_t *ipip, void *if_req)
11912 {
11913 	uint64_t flags;
11914 	struct ifreq *ifr = if_req;
11915 	struct lifreq *lifr = if_req;
11916 
11917 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11918 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11919 
11920 	ipif_down_tail(ipif);
11921 	if (ipip->ipi_cmd_type == IF_CMD) {
11922 		/* cast to uint16_t prevents unwanted sign extension */
11923 		flags = (uint16_t)ifr->ifr_flags;
11924 	} else {
11925 		flags = lifr->lifr_flags;
11926 	}
11927 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11928 }
11929 
11930 /*
11931  * Can operate on either a module or a driver queue.
11932  */
11933 /* ARGSUSED */
11934 int
11935 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11936     ip_ioctl_cmd_t *ipip, void *if_req)
11937 {
11938 	/*
11939 	 * Has the flags been set correctly till now ?
11940 	 */
11941 	ill_t *ill = ipif->ipif_ill;
11942 	phyint_t *phyi = ill->ill_phyint;
11943 
11944 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11945 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11946 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11947 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11948 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11949 
11950 	/*
11951 	 * Need a lock since some flags can be set even when there are
11952 	 * references to the ipif.
11953 	 */
11954 	mutex_enter(&ill->ill_lock);
11955 	if (ipip->ipi_cmd_type == IF_CMD) {
11956 		struct ifreq *ifr = (struct ifreq *)if_req;
11957 
11958 		/* Get interface flags (low 16 only). */
11959 		ifr->ifr_flags = ((ipif->ipif_flags |
11960 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11961 	} else {
11962 		struct lifreq *lifr = (struct lifreq *)if_req;
11963 
11964 		/* Get interface flags. */
11965 		lifr->lifr_flags = ipif->ipif_flags |
11966 		    ill->ill_flags | phyi->phyint_flags;
11967 	}
11968 	mutex_exit(&ill->ill_lock);
11969 	return (0);
11970 }
11971 
11972 /* ARGSUSED */
11973 int
11974 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11975     ip_ioctl_cmd_t *ipip, void *if_req)
11976 {
11977 	int mtu;
11978 	int ip_min_mtu;
11979 	struct ifreq	*ifr;
11980 	struct lifreq *lifr;
11981 	ire_t	*ire;
11982 	ip_stack_t *ipst;
11983 
11984 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11985 	    ipif->ipif_id, (void *)ipif));
11986 	if (ipip->ipi_cmd_type == IF_CMD) {
11987 		ifr = (struct ifreq *)if_req;
11988 		mtu = ifr->ifr_metric;
11989 	} else {
11990 		lifr = (struct lifreq *)if_req;
11991 		mtu = lifr->lifr_mtu;
11992 	}
11993 
11994 	if (ipif->ipif_isv6)
11995 		ip_min_mtu = IPV6_MIN_MTU;
11996 	else
11997 		ip_min_mtu = IP_MIN_MTU;
11998 
11999 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12000 		return (EINVAL);
12001 
12002 	/*
12003 	 * Change the MTU size in all relevant ire's.
12004 	 * Mtu change Vs. new ire creation - protocol below.
12005 	 * First change ipif_mtu and the ire_max_frag of the
12006 	 * interface ire. Then do an ire walk and change the
12007 	 * ire_max_frag of all affected ires. During ire_add
12008 	 * under the bucket lock, set the ire_max_frag of the
12009 	 * new ire being created from the ipif/ire from which
12010 	 * it is being derived. If an mtu change happens after
12011 	 * the ire is added, the new ire will be cleaned up.
12012 	 * Conversely if the mtu change happens before the ire
12013 	 * is added, ire_add will see the new value of the mtu.
12014 	 */
12015 	ipif->ipif_mtu = mtu;
12016 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12017 
12018 	if (ipif->ipif_isv6)
12019 		ire = ipif_to_ire_v6(ipif);
12020 	else
12021 		ire = ipif_to_ire(ipif);
12022 	if (ire != NULL) {
12023 		ire->ire_max_frag = ipif->ipif_mtu;
12024 		ire_refrele(ire);
12025 	}
12026 	ipst = ipif->ipif_ill->ill_ipst;
12027 	if (ipif->ipif_flags & IPIF_UP) {
12028 		if (ipif->ipif_isv6)
12029 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12030 			    ipst);
12031 		else
12032 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12033 			    ipst);
12034 	}
12035 	/* Update the MTU in SCTP's list */
12036 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12037 	return (0);
12038 }
12039 
12040 /* Get interface MTU. */
12041 /* ARGSUSED */
12042 int
12043 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12044 	ip_ioctl_cmd_t *ipip, void *if_req)
12045 {
12046 	struct ifreq	*ifr;
12047 	struct lifreq	*lifr;
12048 
12049 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12050 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12051 	if (ipip->ipi_cmd_type == IF_CMD) {
12052 		ifr = (struct ifreq *)if_req;
12053 		ifr->ifr_metric = ipif->ipif_mtu;
12054 	} else {
12055 		lifr = (struct lifreq *)if_req;
12056 		lifr->lifr_mtu = ipif->ipif_mtu;
12057 	}
12058 	return (0);
12059 }
12060 
12061 /* Set interface broadcast address. */
12062 /* ARGSUSED2 */
12063 int
12064 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12065 	ip_ioctl_cmd_t *ipip, void *if_req)
12066 {
12067 	ipaddr_t addr;
12068 	ire_t	*ire;
12069 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12070 
12071 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12072 	    ipif->ipif_id));
12073 
12074 	ASSERT(IAM_WRITER_IPIF(ipif));
12075 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12076 		return (EADDRNOTAVAIL);
12077 
12078 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12079 
12080 	if (sin->sin_family != AF_INET)
12081 		return (EAFNOSUPPORT);
12082 
12083 	addr = sin->sin_addr.s_addr;
12084 	if (ipif->ipif_flags & IPIF_UP) {
12085 		/*
12086 		 * If we are already up, make sure the new
12087 		 * broadcast address makes sense.  If it does,
12088 		 * there should be an IRE for it already.
12089 		 * Don't match on ipif, only on the ill
12090 		 * since we are sharing these now.
12091 		 */
12092 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12093 		    ipif, ALL_ZONES, NULL,
12094 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12095 		if (ire == NULL) {
12096 			return (EINVAL);
12097 		} else {
12098 			ire_refrele(ire);
12099 		}
12100 	}
12101 	/*
12102 	 * Changing the broadcast addr for this ipif.
12103 	 * Make sure we have valid net and subnet bcast
12104 	 * ire's for other logical interfaces, if needed.
12105 	 */
12106 	if (addr != ipif->ipif_brd_addr)
12107 		ipif_check_bcast_ires(ipif);
12108 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12109 	return (0);
12110 }
12111 
12112 /* Get interface broadcast address. */
12113 /* ARGSUSED */
12114 int
12115 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12116     ip_ioctl_cmd_t *ipip, void *if_req)
12117 {
12118 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12119 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12120 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12121 		return (EADDRNOTAVAIL);
12122 
12123 	/* IPIF_BROADCAST not possible with IPv6 */
12124 	ASSERT(!ipif->ipif_isv6);
12125 	*sin = sin_null;
12126 	sin->sin_family = AF_INET;
12127 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12128 	return (0);
12129 }
12130 
12131 /*
12132  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12133  */
12134 /* ARGSUSED */
12135 int
12136 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12137     ip_ioctl_cmd_t *ipip, void *if_req)
12138 {
12139 	int err = 0;
12140 	in6_addr_t v6mask;
12141 
12142 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12143 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12144 
12145 	ASSERT(IAM_WRITER_IPIF(ipif));
12146 
12147 	if (ipif->ipif_isv6) {
12148 		sin6_t *sin6;
12149 
12150 		if (sin->sin_family != AF_INET6)
12151 			return (EAFNOSUPPORT);
12152 
12153 		sin6 = (sin6_t *)sin;
12154 		v6mask = sin6->sin6_addr;
12155 	} else {
12156 		ipaddr_t mask;
12157 
12158 		if (sin->sin_family != AF_INET)
12159 			return (EAFNOSUPPORT);
12160 
12161 		mask = sin->sin_addr.s_addr;
12162 		V4MASK_TO_V6(mask, v6mask);
12163 	}
12164 
12165 	/*
12166 	 * No big deal if the interface isn't already up, or the mask
12167 	 * isn't really changing, or this is pt-pt.
12168 	 */
12169 	if (!(ipif->ipif_flags & IPIF_UP) ||
12170 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12171 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12172 		ipif->ipif_v6net_mask = v6mask;
12173 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12174 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12175 			    ipif->ipif_v6net_mask,
12176 			    ipif->ipif_v6subnet);
12177 		}
12178 		return (0);
12179 	}
12180 	/*
12181 	 * Make sure we have valid net and subnet broadcast ire's
12182 	 * for the old netmask, if needed by other logical interfaces.
12183 	 */
12184 	if (!ipif->ipif_isv6)
12185 		ipif_check_bcast_ires(ipif);
12186 
12187 	err = ipif_logical_down(ipif, q, mp);
12188 	if (err == EINPROGRESS)
12189 		return (err);
12190 	ipif_down_tail(ipif);
12191 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12192 	return (err);
12193 }
12194 
12195 static int
12196 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12197 {
12198 	in6_addr_t v6mask;
12199 	int err = 0;
12200 
12201 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12202 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12203 
12204 	if (ipif->ipif_isv6) {
12205 		sin6_t *sin6;
12206 
12207 		sin6 = (sin6_t *)sin;
12208 		v6mask = sin6->sin6_addr;
12209 	} else {
12210 		ipaddr_t mask;
12211 
12212 		mask = sin->sin_addr.s_addr;
12213 		V4MASK_TO_V6(mask, v6mask);
12214 	}
12215 
12216 	ipif->ipif_v6net_mask = v6mask;
12217 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12218 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12219 		    ipif->ipif_v6subnet);
12220 	}
12221 	err = ipif_up(ipif, q, mp);
12222 
12223 	if (err == 0 || err == EINPROGRESS) {
12224 		/*
12225 		 * The interface must be DL_BOUND if this packet has to
12226 		 * go out on the wire. Since we only go through a logical
12227 		 * down and are bound with the driver during an internal
12228 		 * down/up that is satisfied.
12229 		 */
12230 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12231 			/* Potentially broadcast an address mask reply. */
12232 			ipif_mask_reply(ipif);
12233 		}
12234 	}
12235 	return (err);
12236 }
12237 
12238 /* ARGSUSED */
12239 int
12240 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12241     ip_ioctl_cmd_t *ipip, void *if_req)
12242 {
12243 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12244 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12245 	ipif_down_tail(ipif);
12246 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12247 }
12248 
12249 /* Get interface net mask. */
12250 /* ARGSUSED */
12251 int
12252 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12253     ip_ioctl_cmd_t *ipip, void *if_req)
12254 {
12255 	struct lifreq *lifr = (struct lifreq *)if_req;
12256 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12257 
12258 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12259 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12260 
12261 	/*
12262 	 * net mask can't change since we have a reference to the ipif.
12263 	 */
12264 	if (ipif->ipif_isv6) {
12265 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12266 		*sin6 = sin6_null;
12267 		sin6->sin6_family = AF_INET6;
12268 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12269 		lifr->lifr_addrlen =
12270 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12271 	} else {
12272 		*sin = sin_null;
12273 		sin->sin_family = AF_INET;
12274 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12275 		if (ipip->ipi_cmd_type == LIF_CMD) {
12276 			lifr->lifr_addrlen =
12277 			    ip_mask_to_plen(ipif->ipif_net_mask);
12278 		}
12279 	}
12280 	return (0);
12281 }
12282 
12283 /* ARGSUSED */
12284 int
12285 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12286     ip_ioctl_cmd_t *ipip, void *if_req)
12287 {
12288 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12289 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12290 
12291 	/*
12292 	 * Since no applications should ever be setting metrics on underlying
12293 	 * interfaces, we explicitly fail to smoke 'em out.
12294 	 */
12295 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12296 		return (EINVAL);
12297 
12298 	/*
12299 	 * Set interface metric.  We don't use this for
12300 	 * anything but we keep track of it in case it is
12301 	 * important to routing applications or such.
12302 	 */
12303 	if (ipip->ipi_cmd_type == IF_CMD) {
12304 		struct ifreq    *ifr;
12305 
12306 		ifr = (struct ifreq *)if_req;
12307 		ipif->ipif_metric = ifr->ifr_metric;
12308 	} else {
12309 		struct lifreq   *lifr;
12310 
12311 		lifr = (struct lifreq *)if_req;
12312 		ipif->ipif_metric = lifr->lifr_metric;
12313 	}
12314 	return (0);
12315 }
12316 
12317 /* ARGSUSED */
12318 int
12319 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12320     ip_ioctl_cmd_t *ipip, void *if_req)
12321 {
12322 	/* Get interface metric. */
12323 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12324 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12325 
12326 	if (ipip->ipi_cmd_type == IF_CMD) {
12327 		struct ifreq    *ifr;
12328 
12329 		ifr = (struct ifreq *)if_req;
12330 		ifr->ifr_metric = ipif->ipif_metric;
12331 	} else {
12332 		struct lifreq   *lifr;
12333 
12334 		lifr = (struct lifreq *)if_req;
12335 		lifr->lifr_metric = ipif->ipif_metric;
12336 	}
12337 
12338 	return (0);
12339 }
12340 
12341 /* ARGSUSED */
12342 int
12343 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12344     ip_ioctl_cmd_t *ipip, void *if_req)
12345 {
12346 
12347 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12348 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12349 	/*
12350 	 * Set the muxid returned from I_PLINK.
12351 	 */
12352 	if (ipip->ipi_cmd_type == IF_CMD) {
12353 		struct ifreq *ifr = (struct ifreq *)if_req;
12354 
12355 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12356 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12357 	} else {
12358 		struct lifreq *lifr = (struct lifreq *)if_req;
12359 
12360 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12361 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12362 	}
12363 	return (0);
12364 }
12365 
12366 /* ARGSUSED */
12367 int
12368 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12369     ip_ioctl_cmd_t *ipip, void *if_req)
12370 {
12371 
12372 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12373 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12374 	/*
12375 	 * Get the muxid saved in ill for I_PUNLINK.
12376 	 */
12377 	if (ipip->ipi_cmd_type == IF_CMD) {
12378 		struct ifreq *ifr = (struct ifreq *)if_req;
12379 
12380 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12381 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12382 	} else {
12383 		struct lifreq *lifr = (struct lifreq *)if_req;
12384 
12385 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12386 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12387 	}
12388 	return (0);
12389 }
12390 
12391 /*
12392  * Set the subnet prefix. Does not modify the broadcast address.
12393  */
12394 /* ARGSUSED */
12395 int
12396 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12397     ip_ioctl_cmd_t *ipip, void *if_req)
12398 {
12399 	int err = 0;
12400 	in6_addr_t v6addr;
12401 	in6_addr_t v6mask;
12402 	boolean_t need_up = B_FALSE;
12403 	int addrlen;
12404 
12405 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12406 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12407 
12408 	ASSERT(IAM_WRITER_IPIF(ipif));
12409 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12410 
12411 	if (ipif->ipif_isv6) {
12412 		sin6_t *sin6;
12413 
12414 		if (sin->sin_family != AF_INET6)
12415 			return (EAFNOSUPPORT);
12416 
12417 		sin6 = (sin6_t *)sin;
12418 		v6addr = sin6->sin6_addr;
12419 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12420 			return (EADDRNOTAVAIL);
12421 	} else {
12422 		ipaddr_t addr;
12423 
12424 		if (sin->sin_family != AF_INET)
12425 			return (EAFNOSUPPORT);
12426 
12427 		addr = sin->sin_addr.s_addr;
12428 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12429 			return (EADDRNOTAVAIL);
12430 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12431 		/* Add 96 bits */
12432 		addrlen += IPV6_ABITS - IP_ABITS;
12433 	}
12434 
12435 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12436 		return (EINVAL);
12437 
12438 	/* Check if bits in the address is set past the mask */
12439 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12440 		return (EINVAL);
12441 
12442 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12443 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12444 		return (0);	/* No change */
12445 
12446 	if (ipif->ipif_flags & IPIF_UP) {
12447 		/*
12448 		 * If the interface is already marked up,
12449 		 * we call ipif_down which will take care
12450 		 * of ditching any IREs that have been set
12451 		 * up based on the old interface address.
12452 		 */
12453 		err = ipif_logical_down(ipif, q, mp);
12454 		if (err == EINPROGRESS)
12455 			return (err);
12456 		ipif_down_tail(ipif);
12457 		need_up = B_TRUE;
12458 	}
12459 
12460 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12461 	return (err);
12462 }
12463 
12464 static int
12465 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12466     queue_t *q, mblk_t *mp, boolean_t need_up)
12467 {
12468 	ill_t	*ill = ipif->ipif_ill;
12469 	int	err = 0;
12470 
12471 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12472 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12473 
12474 	/* Set the new address. */
12475 	mutex_enter(&ill->ill_lock);
12476 	ipif->ipif_v6net_mask = v6mask;
12477 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12478 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12479 		    ipif->ipif_v6subnet);
12480 	}
12481 	mutex_exit(&ill->ill_lock);
12482 
12483 	if (need_up) {
12484 		/*
12485 		 * Now bring the interface back up.  If this
12486 		 * is the only IPIF for the ILL, ipif_up
12487 		 * will have to re-bind to the device, so
12488 		 * we may get back EINPROGRESS, in which
12489 		 * case, this IOCTL will get completed in
12490 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12491 		 */
12492 		err = ipif_up(ipif, q, mp);
12493 		if (err == EINPROGRESS)
12494 			return (err);
12495 	}
12496 	return (err);
12497 }
12498 
12499 /* ARGSUSED */
12500 int
12501 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12502     ip_ioctl_cmd_t *ipip, void *if_req)
12503 {
12504 	int	addrlen;
12505 	in6_addr_t v6addr;
12506 	in6_addr_t v6mask;
12507 	struct lifreq *lifr = (struct lifreq *)if_req;
12508 
12509 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12510 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12511 	ipif_down_tail(ipif);
12512 
12513 	addrlen = lifr->lifr_addrlen;
12514 	if (ipif->ipif_isv6) {
12515 		sin6_t *sin6;
12516 
12517 		sin6 = (sin6_t *)sin;
12518 		v6addr = sin6->sin6_addr;
12519 	} else {
12520 		ipaddr_t addr;
12521 
12522 		addr = sin->sin_addr.s_addr;
12523 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12524 		addrlen += IPV6_ABITS - IP_ABITS;
12525 	}
12526 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12527 
12528 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12529 }
12530 
12531 /* ARGSUSED */
12532 int
12533 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12534     ip_ioctl_cmd_t *ipip, void *if_req)
12535 {
12536 	struct lifreq *lifr = (struct lifreq *)if_req;
12537 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12538 
12539 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12540 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12541 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12542 
12543 	if (ipif->ipif_isv6) {
12544 		*sin6 = sin6_null;
12545 		sin6->sin6_family = AF_INET6;
12546 		sin6->sin6_addr = ipif->ipif_v6subnet;
12547 		lifr->lifr_addrlen =
12548 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12549 	} else {
12550 		*sin = sin_null;
12551 		sin->sin_family = AF_INET;
12552 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12553 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12554 	}
12555 	return (0);
12556 }
12557 
12558 /*
12559  * Set the IPv6 address token.
12560  */
12561 /* ARGSUSED */
12562 int
12563 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12564     ip_ioctl_cmd_t *ipi, void *if_req)
12565 {
12566 	ill_t *ill = ipif->ipif_ill;
12567 	int err;
12568 	in6_addr_t v6addr;
12569 	in6_addr_t v6mask;
12570 	boolean_t need_up = B_FALSE;
12571 	int i;
12572 	sin6_t *sin6 = (sin6_t *)sin;
12573 	struct lifreq *lifr = (struct lifreq *)if_req;
12574 	int addrlen;
12575 
12576 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12577 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12578 	ASSERT(IAM_WRITER_IPIF(ipif));
12579 
12580 	addrlen = lifr->lifr_addrlen;
12581 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12582 	if (ipif->ipif_id != 0)
12583 		return (EINVAL);
12584 
12585 	if (!ipif->ipif_isv6)
12586 		return (EINVAL);
12587 
12588 	if (addrlen > IPV6_ABITS)
12589 		return (EINVAL);
12590 
12591 	v6addr = sin6->sin6_addr;
12592 
12593 	/*
12594 	 * The length of the token is the length from the end.  To get
12595 	 * the proper mask for this, compute the mask of the bits not
12596 	 * in the token; ie. the prefix, and then xor to get the mask.
12597 	 */
12598 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12599 		return (EINVAL);
12600 	for (i = 0; i < 4; i++) {
12601 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12602 	}
12603 
12604 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12605 	    ill->ill_token_length == addrlen)
12606 		return (0);	/* No change */
12607 
12608 	if (ipif->ipif_flags & IPIF_UP) {
12609 		err = ipif_logical_down(ipif, q, mp);
12610 		if (err == EINPROGRESS)
12611 			return (err);
12612 		ipif_down_tail(ipif);
12613 		need_up = B_TRUE;
12614 	}
12615 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12616 	return (err);
12617 }
12618 
12619 static int
12620 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12621     mblk_t *mp, boolean_t need_up)
12622 {
12623 	in6_addr_t v6addr;
12624 	in6_addr_t v6mask;
12625 	ill_t	*ill = ipif->ipif_ill;
12626 	int	i;
12627 	int	err = 0;
12628 
12629 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12630 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12631 	v6addr = sin6->sin6_addr;
12632 	/*
12633 	 * The length of the token is the length from the end.  To get
12634 	 * the proper mask for this, compute the mask of the bits not
12635 	 * in the token; ie. the prefix, and then xor to get the mask.
12636 	 */
12637 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12638 	for (i = 0; i < 4; i++)
12639 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12640 
12641 	mutex_enter(&ill->ill_lock);
12642 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12643 	ill->ill_token_length = addrlen;
12644 	mutex_exit(&ill->ill_lock);
12645 
12646 	if (need_up) {
12647 		/*
12648 		 * Now bring the interface back up.  If this
12649 		 * is the only IPIF for the ILL, ipif_up
12650 		 * will have to re-bind to the device, so
12651 		 * we may get back EINPROGRESS, in which
12652 		 * case, this IOCTL will get completed in
12653 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12654 		 */
12655 		err = ipif_up(ipif, q, mp);
12656 		if (err == EINPROGRESS)
12657 			return (err);
12658 	}
12659 	return (err);
12660 }
12661 
12662 /* ARGSUSED */
12663 int
12664 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12665     ip_ioctl_cmd_t *ipi, void *if_req)
12666 {
12667 	ill_t *ill;
12668 	sin6_t *sin6 = (sin6_t *)sin;
12669 	struct lifreq *lifr = (struct lifreq *)if_req;
12670 
12671 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12672 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12673 	if (ipif->ipif_id != 0)
12674 		return (EINVAL);
12675 
12676 	ill = ipif->ipif_ill;
12677 	if (!ill->ill_isv6)
12678 		return (ENXIO);
12679 
12680 	*sin6 = sin6_null;
12681 	sin6->sin6_family = AF_INET6;
12682 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12683 	sin6->sin6_addr = ill->ill_token;
12684 	lifr->lifr_addrlen = ill->ill_token_length;
12685 	return (0);
12686 }
12687 
12688 /*
12689  * Set (hardware) link specific information that might override
12690  * what was acquired through the DL_INFO_ACK.
12691  * The logic is as follows.
12692  *
12693  * become exclusive
12694  * set CHANGING flag
12695  * change mtu on affected IREs
12696  * clear CHANGING flag
12697  *
12698  * An ire add that occurs before the CHANGING flag is set will have its mtu
12699  * changed by the ip_sioctl_lnkinfo.
12700  *
12701  * During the time the CHANGING flag is set, no new ires will be added to the
12702  * bucket, and ire add will fail (due the CHANGING flag).
12703  *
12704  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12705  * before it is added to the bucket.
12706  *
12707  * Obviously only 1 thread can set the CHANGING flag and we need to become
12708  * exclusive to set the flag.
12709  */
12710 /* ARGSUSED */
12711 int
12712 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12713     ip_ioctl_cmd_t *ipi, void *if_req)
12714 {
12715 	ill_t		*ill = ipif->ipif_ill;
12716 	ipif_t		*nipif;
12717 	int		ip_min_mtu;
12718 	boolean_t	mtu_walk = B_FALSE;
12719 	struct lifreq	*lifr = (struct lifreq *)if_req;
12720 	lif_ifinfo_req_t *lir;
12721 	ire_t		*ire;
12722 
12723 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12724 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12725 	lir = &lifr->lifr_ifinfo;
12726 	ASSERT(IAM_WRITER_IPIF(ipif));
12727 
12728 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12729 	if (ipif->ipif_id != 0)
12730 		return (EINVAL);
12731 
12732 	/* Set interface MTU. */
12733 	if (ipif->ipif_isv6)
12734 		ip_min_mtu = IPV6_MIN_MTU;
12735 	else
12736 		ip_min_mtu = IP_MIN_MTU;
12737 
12738 	/*
12739 	 * Verify values before we set anything. Allow zero to
12740 	 * mean unspecified.
12741 	 */
12742 	if (lir->lir_maxmtu != 0 &&
12743 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12744 	    lir->lir_maxmtu < ip_min_mtu))
12745 		return (EINVAL);
12746 	if (lir->lir_reachtime != 0 &&
12747 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12748 		return (EINVAL);
12749 	if (lir->lir_reachretrans != 0 &&
12750 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12751 		return (EINVAL);
12752 
12753 	mutex_enter(&ill->ill_lock);
12754 	ill->ill_state_flags |= ILL_CHANGING;
12755 	for (nipif = ill->ill_ipif; nipif != NULL;
12756 	    nipif = nipif->ipif_next) {
12757 		nipif->ipif_state_flags |= IPIF_CHANGING;
12758 	}
12759 
12760 	if (lir->lir_maxmtu != 0) {
12761 		ill->ill_max_mtu = lir->lir_maxmtu;
12762 		ill->ill_user_mtu = lir->lir_maxmtu;
12763 		mtu_walk = B_TRUE;
12764 	}
12765 	mutex_exit(&ill->ill_lock);
12766 
12767 	if (lir->lir_reachtime != 0)
12768 		ill->ill_reachable_time = lir->lir_reachtime;
12769 
12770 	if (lir->lir_reachretrans != 0)
12771 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12772 
12773 	ill->ill_max_hops = lir->lir_maxhops;
12774 
12775 	ill->ill_max_buf = ND_MAX_Q;
12776 
12777 	if (mtu_walk) {
12778 		/*
12779 		 * Set the MTU on all ipifs associated with this ill except
12780 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12781 		 */
12782 		for (nipif = ill->ill_ipif; nipif != NULL;
12783 		    nipif = nipif->ipif_next) {
12784 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12785 				continue;
12786 
12787 			nipif->ipif_mtu = ill->ill_max_mtu;
12788 
12789 			if (!(nipif->ipif_flags & IPIF_UP))
12790 				continue;
12791 
12792 			if (nipif->ipif_isv6)
12793 				ire = ipif_to_ire_v6(nipif);
12794 			else
12795 				ire = ipif_to_ire(nipif);
12796 			if (ire != NULL) {
12797 				ire->ire_max_frag = ipif->ipif_mtu;
12798 				ire_refrele(ire);
12799 			}
12800 
12801 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12802 			    nipif, ill);
12803 		}
12804 	}
12805 
12806 	mutex_enter(&ill->ill_lock);
12807 	for (nipif = ill->ill_ipif; nipif != NULL;
12808 	    nipif = nipif->ipif_next) {
12809 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12810 	}
12811 	ILL_UNMARK_CHANGING(ill);
12812 	mutex_exit(&ill->ill_lock);
12813 
12814 	/*
12815 	 * Refresh IPMP meta-interface MTU if necessary.
12816 	 */
12817 	if (IS_UNDER_IPMP(ill))
12818 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12819 
12820 	return (0);
12821 }
12822 
12823 /* ARGSUSED */
12824 int
12825 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12826     ip_ioctl_cmd_t *ipi, void *if_req)
12827 {
12828 	struct lif_ifinfo_req *lir;
12829 	ill_t *ill = ipif->ipif_ill;
12830 
12831 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12832 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12833 	if (ipif->ipif_id != 0)
12834 		return (EINVAL);
12835 
12836 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12837 	lir->lir_maxhops = ill->ill_max_hops;
12838 	lir->lir_reachtime = ill->ill_reachable_time;
12839 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12840 	lir->lir_maxmtu = ill->ill_max_mtu;
12841 
12842 	return (0);
12843 }
12844 
12845 /*
12846  * Return best guess as to the subnet mask for the specified address.
12847  * Based on the subnet masks for all the configured interfaces.
12848  *
12849  * We end up returning a zero mask in the case of default, multicast or
12850  * experimental.
12851  */
12852 static ipaddr_t
12853 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12854 {
12855 	ipaddr_t net_mask;
12856 	ill_t	*ill;
12857 	ipif_t	*ipif;
12858 	ill_walk_context_t ctx;
12859 	ipif_t	*fallback_ipif = NULL;
12860 
12861 	net_mask = ip_net_mask(addr);
12862 	if (net_mask == 0) {
12863 		*ipifp = NULL;
12864 		return (0);
12865 	}
12866 
12867 	/* Let's check to see if this is maybe a local subnet route. */
12868 	/* this function only applies to IPv4 interfaces */
12869 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12870 	ill = ILL_START_WALK_V4(&ctx, ipst);
12871 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12872 		mutex_enter(&ill->ill_lock);
12873 		for (ipif = ill->ill_ipif; ipif != NULL;
12874 		    ipif = ipif->ipif_next) {
12875 			if (!IPIF_CAN_LOOKUP(ipif))
12876 				continue;
12877 			if (!(ipif->ipif_flags & IPIF_UP))
12878 				continue;
12879 			if ((ipif->ipif_subnet & net_mask) ==
12880 			    (addr & net_mask)) {
12881 				/*
12882 				 * Don't trust pt-pt interfaces if there are
12883 				 * other interfaces.
12884 				 */
12885 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12886 					if (fallback_ipif == NULL) {
12887 						ipif_refhold_locked(ipif);
12888 						fallback_ipif = ipif;
12889 					}
12890 					continue;
12891 				}
12892 
12893 				/*
12894 				 * Fine. Just assume the same net mask as the
12895 				 * directly attached subnet interface is using.
12896 				 */
12897 				ipif_refhold_locked(ipif);
12898 				mutex_exit(&ill->ill_lock);
12899 				rw_exit(&ipst->ips_ill_g_lock);
12900 				if (fallback_ipif != NULL)
12901 					ipif_refrele(fallback_ipif);
12902 				*ipifp = ipif;
12903 				return (ipif->ipif_net_mask);
12904 			}
12905 		}
12906 		mutex_exit(&ill->ill_lock);
12907 	}
12908 	rw_exit(&ipst->ips_ill_g_lock);
12909 
12910 	*ipifp = fallback_ipif;
12911 	return ((fallback_ipif != NULL) ?
12912 	    fallback_ipif->ipif_net_mask : net_mask);
12913 }
12914 
12915 /*
12916  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12917  */
12918 static void
12919 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12920 {
12921 	IOCP	iocp;
12922 	ipft_t	*ipft;
12923 	ipllc_t	*ipllc;
12924 	mblk_t	*mp1;
12925 	cred_t	*cr;
12926 	int	error = 0;
12927 	conn_t	*connp;
12928 
12929 	ip1dbg(("ip_wput_ioctl"));
12930 	iocp = (IOCP)mp->b_rptr;
12931 	mp1 = mp->b_cont;
12932 	if (mp1 == NULL) {
12933 		iocp->ioc_error = EINVAL;
12934 		mp->b_datap->db_type = M_IOCNAK;
12935 		iocp->ioc_count = 0;
12936 		qreply(q, mp);
12937 		return;
12938 	}
12939 
12940 	/*
12941 	 * These IOCTLs provide various control capabilities to
12942 	 * upstream agents such as ULPs and processes.	There
12943 	 * are currently two such IOCTLs implemented.  They
12944 	 * are used by TCP to provide update information for
12945 	 * existing IREs and to forcibly delete an IRE for a
12946 	 * host that is not responding, thereby forcing an
12947 	 * attempt at a new route.
12948 	 */
12949 	iocp->ioc_error = EINVAL;
12950 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12951 		goto done;
12952 
12953 	ipllc = (ipllc_t *)mp1->b_rptr;
12954 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12955 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12956 			break;
12957 	}
12958 	/*
12959 	 * prefer credential from mblk over ioctl;
12960 	 * see ip_sioctl_copyin_setup
12961 	 */
12962 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12963 
12964 	/*
12965 	 * Refhold the conn in case the request gets queued up in some lookup
12966 	 */
12967 	ASSERT(CONN_Q(q));
12968 	connp = Q_TO_CONN(q);
12969 	CONN_INC_REF(connp);
12970 	if (ipft->ipft_pfi &&
12971 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12972 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12973 		error = (*ipft->ipft_pfi)(q,
12974 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12975 	}
12976 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12977 		/*
12978 		 * CONN_OPER_PENDING_DONE happens in the function called
12979 		 * through ipft_pfi above.
12980 		 */
12981 		return;
12982 	}
12983 
12984 	CONN_OPER_PENDING_DONE(connp);
12985 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12986 		freemsg(mp);
12987 		return;
12988 	}
12989 	iocp->ioc_error = error;
12990 
12991 done:
12992 	mp->b_datap->db_type = M_IOCACK;
12993 	if (iocp->ioc_error)
12994 		iocp->ioc_count = 0;
12995 	qreply(q, mp);
12996 }
12997 
12998 /*
12999  * Lookup an ipif using the sequence id (ipif_seqid)
13000  */
13001 ipif_t *
13002 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13003 {
13004 	ipif_t *ipif;
13005 
13006 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13007 
13008 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13009 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13010 			return (ipif);
13011 	}
13012 	return (NULL);
13013 }
13014 
13015 /*
13016  * Assign a unique id for the ipif. This is used later when we send
13017  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13018  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13019  * IRE is added, we verify that ipif has not disappeared.
13020  */
13021 
13022 static void
13023 ipif_assign_seqid(ipif_t *ipif)
13024 {
13025 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13026 
13027 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13028 }
13029 
13030 /*
13031  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
13032  * administratively down (i.e., no DAD), of the same type, and locked.  Note
13033  * that the clone is complete -- including the seqid -- and the expectation is
13034  * that the caller will either free or overwrite `sipif' before it's unlocked.
13035  */
13036 static void
13037 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
13038 {
13039 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
13040 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
13041 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13042 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13043 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
13044 	ASSERT(sipif->ipif_arp_del_mp == NULL);
13045 	ASSERT(dipif->ipif_arp_del_mp == NULL);
13046 	ASSERT(sipif->ipif_igmp_rpt == NULL);
13047 	ASSERT(dipif->ipif_igmp_rpt == NULL);
13048 	ASSERT(sipif->ipif_multicast_up == 0);
13049 	ASSERT(dipif->ipif_multicast_up == 0);
13050 	ASSERT(sipif->ipif_joined_allhosts == 0);
13051 	ASSERT(dipif->ipif_joined_allhosts == 0);
13052 
13053 	dipif->ipif_mtu = sipif->ipif_mtu;
13054 	dipif->ipif_flags = sipif->ipif_flags;
13055 	dipif->ipif_metric = sipif->ipif_metric;
13056 	dipif->ipif_zoneid = sipif->ipif_zoneid;
13057 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
13058 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
13059 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
13060 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
13061 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
13062 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
13063 
13064 	/*
13065 	 * While dipif is down right now, it might've been up before.  Since
13066 	 * it's changing identity, its packet counters need to be reset.
13067 	 */
13068 	dipif->ipif_ib_pkt_count = 0;
13069 	dipif->ipif_ob_pkt_count = 0;
13070 	dipif->ipif_fo_pkt_count = 0;
13071 
13072 	/*
13073 	 * As per the comment atop the function, we assume that these sipif
13074 	 * fields will be changed before sipif is unlocked.
13075 	 */
13076 	dipif->ipif_seqid = sipif->ipif_seqid;
13077 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
13078 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
13079 	dipif->ipif_state_flags = sipif->ipif_state_flags;
13080 }
13081 
13082 /*
13083  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
13084  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
13085  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
13086  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
13087  * down (i.e., no DAD), of the same type, and unlocked.
13088  */
13089 static void
13090 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
13091 {
13092 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
13093 	int ipx_current_ioctl;
13094 
13095 	ASSERT(sipif != dipif);
13096 	ASSERT(sipif != virgipif);
13097 
13098 	/*
13099 	 * Grab all of the locks that protect the ipif in a defined order.
13100 	 */
13101 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13102 	if (sipif > dipif) {
13103 		mutex_enter(&sipif->ipif_saved_ire_lock);
13104 		mutex_enter(&dipif->ipif_saved_ire_lock);
13105 	} else {
13106 		mutex_enter(&dipif->ipif_saved_ire_lock);
13107 		mutex_enter(&sipif->ipif_saved_ire_lock);
13108 	}
13109 
13110 	ipif_clone(sipif, dipif);
13111 	if (virgipif != NULL) {
13112 		ipif_clone(virgipif, sipif);
13113 		mi_free(virgipif);
13114 	}
13115 
13116 	mutex_exit(&sipif->ipif_saved_ire_lock);
13117 	mutex_exit(&dipif->ipif_saved_ire_lock);
13118 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13119 
13120 	/*
13121 	 * Transfer ownership of the current xop, if necessary.
13122 	 */
13123 	if (ipsq->ipsq_xop->ipx_current_ipif == sipif) {
13124 		ASSERT(ipsq->ipsq_xop->ipx_pending_ipif == NULL);
13125 		ipx_current_ioctl = ipsq->ipsq_xop->ipx_current_ioctl;
13126 		ipsq_current_finish(ipsq);
13127 		ipsq_current_start(ipsq, dipif, ipx_current_ioctl);
13128 	}
13129 
13130 	if (virgipif == NULL)
13131 		mi_free(sipif);
13132 }
13133 
13134 /*
13135  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13136  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13137  * be inserted into the first space available in the list. The value of
13138  * ipif_id will then be set to the appropriate value for its position.
13139  */
13140 static int
13141 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13142 {
13143 	ill_t *ill;
13144 	ipif_t *tipif;
13145 	ipif_t **tipifp;
13146 	int id;
13147 	ip_stack_t	*ipst;
13148 
13149 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13150 	    IAM_WRITER_IPIF(ipif));
13151 
13152 	ill = ipif->ipif_ill;
13153 	ASSERT(ill != NULL);
13154 	ipst = ill->ill_ipst;
13155 
13156 	/*
13157 	 * In the case of lo0:0 we already hold the ill_g_lock.
13158 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13159 	 * ipif_insert.
13160 	 */
13161 	if (acquire_g_lock)
13162 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13163 	mutex_enter(&ill->ill_lock);
13164 	id = ipif->ipif_id;
13165 	tipifp = &(ill->ill_ipif);
13166 	if (id == -1) {	/* need to find a real id */
13167 		id = 0;
13168 		while ((tipif = *tipifp) != NULL) {
13169 			ASSERT(tipif->ipif_id >= id);
13170 			if (tipif->ipif_id != id)
13171 				break; /* non-consecutive id */
13172 			id++;
13173 			tipifp = &(tipif->ipif_next);
13174 		}
13175 		/* limit number of logical interfaces */
13176 		if (id >= ipst->ips_ip_addrs_per_if) {
13177 			mutex_exit(&ill->ill_lock);
13178 			if (acquire_g_lock)
13179 				rw_exit(&ipst->ips_ill_g_lock);
13180 			return (-1);
13181 		}
13182 		ipif->ipif_id = id; /* assign new id */
13183 	} else if (id < ipst->ips_ip_addrs_per_if) {
13184 		/* we have a real id; insert ipif in the right place */
13185 		while ((tipif = *tipifp) != NULL) {
13186 			ASSERT(tipif->ipif_id != id);
13187 			if (tipif->ipif_id > id)
13188 				break; /* found correct location */
13189 			tipifp = &(tipif->ipif_next);
13190 		}
13191 	} else {
13192 		mutex_exit(&ill->ill_lock);
13193 		if (acquire_g_lock)
13194 			rw_exit(&ipst->ips_ill_g_lock);
13195 		return (-1);
13196 	}
13197 
13198 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13199 
13200 	ipif->ipif_next = tipif;
13201 	*tipifp = ipif;
13202 	mutex_exit(&ill->ill_lock);
13203 	if (acquire_g_lock)
13204 		rw_exit(&ipst->ips_ill_g_lock);
13205 
13206 	return (0);
13207 }
13208 
13209 static void
13210 ipif_remove(ipif_t *ipif)
13211 {
13212 	ipif_t	**ipifp;
13213 	ill_t	*ill = ipif->ipif_ill;
13214 
13215 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13216 
13217 	mutex_enter(&ill->ill_lock);
13218 	ipifp = &ill->ill_ipif;
13219 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13220 		if (*ipifp == ipif) {
13221 			*ipifp = ipif->ipif_next;
13222 			break;
13223 		}
13224 	}
13225 	mutex_exit(&ill->ill_lock);
13226 }
13227 
13228 /*
13229  * Allocate and initialize a new interface control structure.  (Always
13230  * called as writer.)
13231  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13232  * is not part of the global linked list of ills. ipif_seqid is unique
13233  * in the system and to preserve the uniqueness, it is assigned only
13234  * when ill becomes part of the global list. At that point ill will
13235  * have a name. If it doesn't get assigned here, it will get assigned
13236  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13237  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13238  * the interface flags or any other information from the DL_INFO_ACK for
13239  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13240  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13241  * second DL_INFO_ACK comes in from the driver.
13242  */
13243 static ipif_t *
13244 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13245     boolean_t insert)
13246 {
13247 	ipif_t	*ipif;
13248 	phyint_t *phyi = ill->ill_phyint;
13249 	ip_stack_t *ipst = ill->ill_ipst;
13250 
13251 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13252 	    ill->ill_name, id, (void *)ill));
13253 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13254 
13255 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13256 		return (NULL);
13257 	*ipif = ipif_zero;	/* start clean */
13258 
13259 	ipif->ipif_ill = ill;
13260 	ipif->ipif_id = id;	/* could be -1 */
13261 	/*
13262 	 * Inherit the zoneid from the ill; for the shared stack instance
13263 	 * this is always the global zone
13264 	 */
13265 	ipif->ipif_zoneid = ill->ill_zoneid;
13266 
13267 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13268 
13269 	ipif->ipif_refcnt = 0;
13270 	ipif->ipif_saved_ire_cnt = 0;
13271 
13272 	if (insert) {
13273 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13274 			mi_free(ipif);
13275 			return (NULL);
13276 		}
13277 		/* -1 id should have been replaced by real id */
13278 		id = ipif->ipif_id;
13279 		ASSERT(id >= 0);
13280 	}
13281 
13282 	if (ill->ill_name[0] != '\0')
13283 		ipif_assign_seqid(ipif);
13284 
13285 	/*
13286 	 * If this is ipif zero, configure ill/phyint-wide information.
13287 	 * Defer most configuration until we're guaranteed we're attached.
13288 	 */
13289 	if (id == 0) {
13290 		if (ill->ill_mactype == SUNW_DL_IPMP) {
13291 			/*
13292 			 * Set PHYI_IPMP and also set PHYI_FAILED since there
13293 			 * are no active interfaces.  Similarly, PHYI_RUNNING
13294 			 * isn't set until the group has an active interface.
13295 			 */
13296 			mutex_enter(&phyi->phyint_lock);
13297 			phyi->phyint_flags |= (PHYI_IPMP | PHYI_FAILED);
13298 			mutex_exit(&phyi->phyint_lock);
13299 
13300 			/*
13301 			 * Create the illgrp (which must not exist yet because
13302 			 * the zeroth ipif is created once per ill).  However,
13303 			 * do not not link it to the ipmp_grp_t until I_PLINK
13304 			 * is called; see ip_sioctl_plink_ipmp() for details.
13305 			 */
13306 			if (ipmp_illgrp_create(ill) == NULL) {
13307 				if (insert) {
13308 					rw_enter(&ipst->ips_ill_g_lock,
13309 					    RW_WRITER);
13310 					ipif_remove(ipif);
13311 					rw_exit(&ipst->ips_ill_g_lock);
13312 				}
13313 				mi_free(ipif);
13314 				return (NULL);
13315 			}
13316 		} else {
13317 			/*
13318 			 * By default, PHYI_RUNNING is set when the zeroth
13319 			 * ipif is created.  For other ipifs, we don't touch
13320 			 * it since DLPI notifications may have changed it.
13321 			 */
13322 			mutex_enter(&phyi->phyint_lock);
13323 			phyi->phyint_flags |= PHYI_RUNNING;
13324 			mutex_exit(&phyi->phyint_lock);
13325 		}
13326 	}
13327 
13328 	/*
13329 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13330 	 * The ipif is still not up and can't be looked up until the
13331 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13332 	 */
13333 	mutex_enter(&ill->ill_lock);
13334 	mutex_enter(&phyi->phyint_lock);
13335 
13336 	ipif->ipif_ire_type = ire_type;
13337 
13338 	if (ipif->ipif_isv6) {
13339 		ill->ill_flags |= ILLF_IPV6;
13340 	} else {
13341 		ipaddr_t inaddr_any = INADDR_ANY;
13342 
13343 		ill->ill_flags |= ILLF_IPV4;
13344 
13345 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13346 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13347 		    &ipif->ipif_v6lcl_addr);
13348 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13349 		    &ipif->ipif_v6src_addr);
13350 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13351 		    &ipif->ipif_v6subnet);
13352 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13353 		    &ipif->ipif_v6net_mask);
13354 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13355 		    &ipif->ipif_v6brd_addr);
13356 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13357 		    &ipif->ipif_v6pp_dst_addr);
13358 	}
13359 
13360 	/*
13361 	 * Don't set the interface flags etc. now, will do it in
13362 	 * ip_ll_subnet_defaults.
13363 	 */
13364 	if (!initialize)
13365 		goto out;
13366 
13367 	ipif->ipif_mtu = ill->ill_max_mtu;
13368 
13369 	/*
13370 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13371 	 * with no underlying interfaces (and thus an unknown broadcast
13372 	 * address length), but all interfaces that can be placed into an IPMP
13373 	 * group are required to be broadcast-capable.
13374 	 */
13375 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13376 		/*
13377 		 * Later detect lack of DLPI driver multicast
13378 		 * capability by catching DL_ENABMULTI errors in
13379 		 * ip_rput_dlpi.
13380 		 */
13381 		ill->ill_flags |= ILLF_MULTICAST;
13382 		if (!ipif->ipif_isv6)
13383 			ipif->ipif_flags |= IPIF_BROADCAST;
13384 	} else {
13385 		if (ill->ill_net_type != IRE_LOOPBACK) {
13386 			if (ipif->ipif_isv6)
13387 				/*
13388 				 * Note: xresolv interfaces will eventually need
13389 				 * NOARP set here as well, but that will require
13390 				 * those external resolvers to have some
13391 				 * knowledge of that flag and act appropriately.
13392 				 * Not to be changed at present.
13393 				 */
13394 				ill->ill_flags |= ILLF_NONUD;
13395 			else
13396 				ill->ill_flags |= ILLF_NOARP;
13397 		}
13398 		if (ill->ill_phys_addr_length == 0) {
13399 			if (ill->ill_mactype == SUNW_DL_VNI) {
13400 				ipif->ipif_flags |= IPIF_NOXMIT;
13401 				phyi->phyint_flags |= PHYI_VIRTUAL;
13402 			} else {
13403 				/* pt-pt supports multicast. */
13404 				ill->ill_flags |= ILLF_MULTICAST;
13405 				if (ill->ill_net_type == IRE_LOOPBACK) {
13406 					phyi->phyint_flags |=
13407 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13408 				} else {
13409 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13410 				}
13411 			}
13412 		}
13413 	}
13414 out:
13415 	mutex_exit(&phyi->phyint_lock);
13416 	mutex_exit(&ill->ill_lock);
13417 	return (ipif);
13418 }
13419 
13420 /*
13421  * If appropriate, send a message up to the resolver delete the entry
13422  * for the address of this interface which is going out of business.
13423  * (Always called as writer).
13424  *
13425  * NOTE : We need to check for NULL mps as some of the fields are
13426  *	  initialized only for some interface types. See ipif_resolver_up()
13427  *	  for details.
13428  */
13429 void
13430 ipif_resolver_down(ipif_t *ipif)
13431 {
13432 	mblk_t	*mp;
13433 	ill_t	*ill = ipif->ipif_ill;
13434 
13435 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13436 	ASSERT(IAM_WRITER_IPIF(ipif));
13437 
13438 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13439 		return;
13440 
13441 	/* Delete the mapping for the local address */
13442 	mp = ipif->ipif_arp_del_mp;
13443 	if (mp != NULL) {
13444 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13445 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13446 		putnext(ill->ill_rq, mp);
13447 		ipif->ipif_arp_del_mp = NULL;
13448 	}
13449 
13450 	/*
13451 	 * Make IPMP aware of the deleted data address.
13452 	 */
13453 	if (IS_IPMP(ill))
13454 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13455 
13456 	/*
13457 	 * If this is the last ipif that is going down and there are no
13458 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13459 	 * clean up ARP completely.
13460 	 */
13461 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13462 		/*
13463 		 * If this was the last ipif on an IPMP interface, purge any
13464 		 * IPMP ARP entries associated with it.
13465 		 */
13466 		if (IS_IPMP(ill))
13467 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13468 
13469 		/* Send up AR_INTERFACE_DOWN message */
13470 		mp = ill->ill_arp_down_mp;
13471 		if (mp != NULL) {
13472 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13473 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13474 			    ipif->ipif_id));
13475 			putnext(ill->ill_rq, mp);
13476 			ill->ill_arp_down_mp = NULL;
13477 		}
13478 
13479 		/* Tell ARP to delete the multicast mappings */
13480 		mp = ill->ill_arp_del_mapping_mp;
13481 		if (mp != NULL) {
13482 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13483 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13484 			    ipif->ipif_id));
13485 			putnext(ill->ill_rq, mp);
13486 			ill->ill_arp_del_mapping_mp = NULL;
13487 		}
13488 	}
13489 }
13490 
13491 /*
13492  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13493  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13494  * that it wants the add_mp allocated in this function to be returned
13495  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13496  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13497  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13498  * as it does a ipif_arp_down after calling this function - which will
13499  * remove what we add here.
13500  *
13501  * Returns -1 on failures and 0 on success.
13502  */
13503 int
13504 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13505 {
13506 	mblk_t	*del_mp = NULL;
13507 	mblk_t *add_mp = NULL;
13508 	mblk_t *mp;
13509 	ill_t	*ill = ipif->ipif_ill;
13510 	phyint_t *phyi = ill->ill_phyint;
13511 	ipaddr_t addr, mask, extract_mask = 0;
13512 	arma_t	*arma;
13513 	uint8_t *maddr, *bphys_addr;
13514 	uint32_t hw_start;
13515 	dl_unitdata_req_t *dlur;
13516 
13517 	ASSERT(IAM_WRITER_IPIF(ipif));
13518 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13519 		return (0);
13520 
13521 	/*
13522 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13523 	 * and instead use the ones on the underlying interfaces.
13524 	 */
13525 	if (IS_IPMP(ill))
13526 		return (0);
13527 
13528 	/*
13529 	 * Delete the existing mapping from ARP. Normally ipif_down
13530 	 * -> ipif_arp_down should send this up to ARP. The only
13531 	 * reason we would find this when we are switching from
13532 	 * Multicast to Broadcast where we did not do a down.
13533 	 */
13534 	mp = ill->ill_arp_del_mapping_mp;
13535 	if (mp != NULL) {
13536 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13537 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13538 		putnext(ill->ill_rq, mp);
13539 		ill->ill_arp_del_mapping_mp = NULL;
13540 	}
13541 
13542 	if (arp_add_mapping_mp != NULL)
13543 		*arp_add_mapping_mp = NULL;
13544 
13545 	/*
13546 	 * Check that the address is not to long for the constant
13547 	 * length reserved in the template arma_t.
13548 	 */
13549 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13550 		return (-1);
13551 
13552 	/* Add mapping mblk */
13553 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13554 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13555 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13556 	    (caddr_t)&addr);
13557 	if (add_mp == NULL)
13558 		return (-1);
13559 	arma = (arma_t *)add_mp->b_rptr;
13560 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13561 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13562 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13563 
13564 	/*
13565 	 * Determine the broadcast address.
13566 	 */
13567 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13568 	if (ill->ill_sap_length < 0)
13569 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13570 	else
13571 		bphys_addr = (uchar_t *)dlur +
13572 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13573 	/*
13574 	 * Check PHYI_MULTI_BCAST and length of physical
13575 	 * address to determine if we use the mapping or the
13576 	 * broadcast address.
13577 	 */
13578 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13579 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13580 		    bphys_addr, maddr, &hw_start, &extract_mask))
13581 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13582 
13583 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13584 	    (ill->ill_flags & ILLF_MULTICAST)) {
13585 		/* Make sure this will not match the "exact" entry. */
13586 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13587 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13588 		    (caddr_t)&addr);
13589 		if (del_mp == NULL) {
13590 			freemsg(add_mp);
13591 			return (-1);
13592 		}
13593 		bcopy(&extract_mask, (char *)arma +
13594 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13595 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13596 			/* Use link-layer broadcast address for MULTI_BCAST */
13597 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13598 			ip2dbg(("ipif_arp_setup_multicast: adding"
13599 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13600 		} else {
13601 			arma->arma_hw_mapping_start = hw_start;
13602 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13603 			    " ARP setup for %s\n", ill->ill_name));
13604 		}
13605 	} else {
13606 		freemsg(add_mp);
13607 		ASSERT(del_mp == NULL);
13608 		/* It is neither MULTICAST nor MULTI_BCAST */
13609 		return (0);
13610 	}
13611 	ASSERT(add_mp != NULL && del_mp != NULL);
13612 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13613 	ill->ill_arp_del_mapping_mp = del_mp;
13614 	if (arp_add_mapping_mp != NULL) {
13615 		/* The caller just wants the mblks allocated */
13616 		*arp_add_mapping_mp = add_mp;
13617 	} else {
13618 		/* The caller wants us to send it to arp */
13619 		putnext(ill->ill_rq, add_mp);
13620 	}
13621 	return (0);
13622 }
13623 
13624 /*
13625  * Get the resolver set up for a new IP address.  (Always called as writer.)
13626  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13627  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13628  *
13629  * The enumerated value res_act tunes the behavior:
13630  * 	* Res_act_initial: set up all the resolver structures for a new
13631  *	  IP address.
13632  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13633  *	  ARP message in defense of the address.
13634  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13635  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13636  *
13637  * Returns error on failure.
13638  */
13639 int
13640 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13641 {
13642 	mblk_t	*arp_up_mp = NULL;
13643 	mblk_t	*arp_down_mp = NULL;
13644 	mblk_t	*arp_add_mp = NULL;
13645 	mblk_t	*arp_del_mp = NULL;
13646 	mblk_t	*arp_add_mapping_mp = NULL;
13647 	mblk_t	*arp_del_mapping_mp = NULL;
13648 	ill_t	*ill = ipif->ipif_ill;
13649 	int	err = ENOMEM;
13650 	boolean_t added_ipif = B_FALSE;
13651 	boolean_t publish;
13652 	boolean_t was_dup;
13653 
13654 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13655 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13656 	ASSERT(IAM_WRITER_IPIF(ipif));
13657 
13658 	was_dup = B_FALSE;
13659 	if (res_act == Res_act_initial) {
13660 		ipif->ipif_addr_ready = 0;
13661 		/*
13662 		 * We're bringing an interface up here.  There's no way that we
13663 		 * should need to shut down ARP now.
13664 		 */
13665 		mutex_enter(&ill->ill_lock);
13666 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13667 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13668 			ill->ill_ipif_dup_count--;
13669 			was_dup = B_TRUE;
13670 		}
13671 		mutex_exit(&ill->ill_lock);
13672 	}
13673 	if (ipif->ipif_recovery_id != 0)
13674 		(void) untimeout(ipif->ipif_recovery_id);
13675 	ipif->ipif_recovery_id = 0;
13676 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13677 		ipif->ipif_addr_ready = 1;
13678 		return (0);
13679 	}
13680 	/* NDP will set the ipif_addr_ready flag when it's ready */
13681 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13682 		return (0);
13683 
13684 	if (ill->ill_isv6) {
13685 		/*
13686 		 * External resolver for IPv6
13687 		 */
13688 		ASSERT(res_act == Res_act_initial);
13689 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13690 	} else {
13691 		/*
13692 		 * IPv4 arp case. If the ARP stream has already started
13693 		 * closing, fail this request for ARP bringup. Else
13694 		 * record the fact that an ARP bringup is pending.
13695 		 */
13696 		mutex_enter(&ill->ill_lock);
13697 		if (ill->ill_arp_closing) {
13698 			mutex_exit(&ill->ill_lock);
13699 			err = EINVAL;
13700 			goto failed;
13701 		} else {
13702 			if (ill->ill_ipif_up_count == 0 &&
13703 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13704 				ill->ill_arp_bringup_pending = 1;
13705 			mutex_exit(&ill->ill_lock);
13706 		}
13707 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13708 	}
13709 
13710 	if (IS_IPMP(ill) && publish) {
13711 		/*
13712 		 * If we're here via ipif_up(), then the ipif won't be bound
13713 		 * yet -- add it to the group, which will bind it if possible.
13714 		 * (We would add it in ipif_up(), but deleting on failure
13715 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13716 		 * then the ipif has already been added to the group and we
13717 		 * just need to use the binding.
13718 		 */
13719 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13720 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13721 				/*
13722 				 * We couldn't bind the ipif to an ill yet,
13723 				 * so we have nothing to publish.
13724 				 */
13725 				publish = B_FALSE;
13726 			}
13727 			added_ipif = B_TRUE;
13728 		}
13729 	}
13730 
13731 	/*
13732 	 * Add an entry for the local address in ARP only if it
13733 	 * is not UNNUMBERED and it is suitable for publishing.
13734 	 */
13735 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13736 		if (res_act == Res_act_defend) {
13737 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13738 			if (arp_add_mp == NULL)
13739 				goto failed;
13740 			/*
13741 			 * If we're just defending our address now, then
13742 			 * there's no need to set up ARP multicast mappings.
13743 			 * The publish command is enough.
13744 			 */
13745 			goto done;
13746 		}
13747 
13748 		/*
13749 		 * Allocate an ARP add message and an ARP delete message (the
13750 		 * latter is saved for use when the address goes down).
13751 		 */
13752 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13753 			goto failed;
13754 
13755 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13756 			goto failed;
13757 
13758 		if (res_act != Res_act_initial)
13759 			goto arp_setup_multicast;
13760 	} else {
13761 		if (res_act != Res_act_initial)
13762 			goto done;
13763 	}
13764 	/*
13765 	 * Need to bring up ARP or setup multicast mapping only
13766 	 * when the first interface is coming UP.
13767 	 */
13768 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13769 		goto done;
13770 
13771 	/*
13772 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13773 	 */
13774 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13775 	if (arp_down_mp == NULL)
13776 		goto failed;
13777 
13778 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13779 	if (arp_up_mp == NULL)
13780 		goto failed;
13781 
13782 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13783 		goto done;
13784 
13785 arp_setup_multicast:
13786 	/*
13787 	 * Setup the multicast mappings. This function initializes
13788 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13789 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13790 	 */
13791 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13792 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13793 		if (err != 0)
13794 			goto failed;
13795 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13796 		ASSERT(arp_add_mapping_mp != NULL);
13797 	}
13798 done:
13799 	if (arp_up_mp != NULL) {
13800 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13801 		    ill->ill_name, ipif->ipif_id));
13802 		putnext(ill->ill_rq, arp_up_mp);
13803 		arp_up_mp = NULL;
13804 	}
13805 	if (arp_add_mp != NULL) {
13806 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13807 		    ill->ill_name, ipif->ipif_id));
13808 		/*
13809 		 * If it's an extended ARP implementation, then we'll wait to
13810 		 * hear that DAD has finished before using the interface.
13811 		 */
13812 		if (!ill->ill_arp_extend)
13813 			ipif->ipif_addr_ready = 1;
13814 		putnext(ill->ill_rq, arp_add_mp);
13815 		arp_add_mp = NULL;
13816 	} else {
13817 		ipif->ipif_addr_ready = 1;
13818 	}
13819 	if (arp_add_mapping_mp != NULL) {
13820 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13821 		    ill->ill_name, ipif->ipif_id));
13822 		putnext(ill->ill_rq, arp_add_mapping_mp);
13823 		arp_add_mapping_mp = NULL;
13824 	}
13825 
13826 	if (res_act == Res_act_initial) {
13827 		if (ill->ill_flags & ILLF_NOARP)
13828 			err = ill_arp_off(ill);
13829 		else
13830 			err = ill_arp_on(ill);
13831 		if (err != 0) {
13832 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13833 			    err));
13834 			goto failed;
13835 		}
13836 	}
13837 
13838 	if (arp_del_mp != NULL) {
13839 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13840 		ipif->ipif_arp_del_mp = arp_del_mp;
13841 	}
13842 	if (arp_down_mp != NULL) {
13843 		ASSERT(ill->ill_arp_down_mp == NULL);
13844 		ill->ill_arp_down_mp = arp_down_mp;
13845 	}
13846 	if (arp_del_mapping_mp != NULL) {
13847 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13848 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13849 	}
13850 
13851 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13852 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13853 failed:
13854 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13855 	if (added_ipif)
13856 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13857 	freemsg(arp_add_mp);
13858 	freemsg(arp_del_mp);
13859 	freemsg(arp_add_mapping_mp);
13860 	freemsg(arp_up_mp);
13861 	freemsg(arp_down_mp);
13862 	ill->ill_arp_bringup_pending = 0;
13863 	return (err);
13864 }
13865 
13866 /*
13867  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13868  * just gone back up.
13869  */
13870 static void
13871 ipif_arp_start_dad(ipif_t *ipif)
13872 {
13873 	ill_t *ill = ipif->ipif_ill;
13874 	mblk_t *arp_add_mp;
13875 
13876 	/* ACE_F_UNVERIFIED restarts DAD */
13877 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13878 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13879 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13880 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13881 		/*
13882 		 * If we can't contact ARP for some reason, that's not really a
13883 		 * problem.  Just send out the routing socket notification that
13884 		 * DAD completion would have done, and continue.
13885 		 */
13886 		ipif_mask_reply(ipif);
13887 		ipif_up_notify(ipif);
13888 		ipif->ipif_addr_ready = 1;
13889 		return;
13890 	}
13891 
13892 	putnext(ill->ill_rq, arp_add_mp);
13893 }
13894 
13895 static void
13896 ipif_ndp_start_dad(ipif_t *ipif)
13897 {
13898 	nce_t *nce;
13899 
13900 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13901 	    B_FALSE);
13902 	if (nce == NULL)
13903 		return;
13904 
13905 	if (!ndp_restart_dad(nce)) {
13906 		/*
13907 		 * If we can't restart DAD for some reason, that's not really a
13908 		 * problem.  Just send out the routing socket notification that
13909 		 * DAD completion would have done, and continue.
13910 		 */
13911 		ipif_up_notify(ipif);
13912 		ipif->ipif_addr_ready = 1;
13913 	}
13914 	NCE_REFRELE(nce);
13915 }
13916 
13917 /*
13918  * Restart duplicate address detection on all interfaces on the given ill.
13919  *
13920  * This is called when an interface transitions from down to up
13921  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13922  *
13923  * Note that since the underlying physical link has transitioned, we must cause
13924  * at least one routing socket message to be sent here, either via DAD
13925  * completion or just by default on the first ipif.  (If we don't do this, then
13926  * in.mpathd will see long delays when doing link-based failure recovery.)
13927  */
13928 void
13929 ill_restart_dad(ill_t *ill, boolean_t went_up)
13930 {
13931 	ipif_t *ipif;
13932 
13933 	if (ill == NULL)
13934 		return;
13935 
13936 	/*
13937 	 * If layer two doesn't support duplicate address detection, then just
13938 	 * send the routing socket message now and be done with it.
13939 	 */
13940 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13941 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13942 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13943 		return;
13944 	}
13945 
13946 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13947 		if (went_up) {
13948 			if (ipif->ipif_flags & IPIF_UP) {
13949 				if (ill->ill_isv6)
13950 					ipif_ndp_start_dad(ipif);
13951 				else
13952 					ipif_arp_start_dad(ipif);
13953 			} else if (ill->ill_isv6 &&
13954 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13955 				/*
13956 				 * For IPv4, the ARP module itself will
13957 				 * automatically start the DAD process when it
13958 				 * sees DL_NOTE_LINK_UP.  We respond to the
13959 				 * AR_CN_READY at the completion of that task.
13960 				 * For IPv6, we must kick off the bring-up
13961 				 * process now.
13962 				 */
13963 				ndp_do_recovery(ipif);
13964 			} else {
13965 				/*
13966 				 * Unfortunately, the first ipif is "special"
13967 				 * and represents the underlying ill in the
13968 				 * routing socket messages.  Thus, when this
13969 				 * one ipif is down, we must still notify so
13970 				 * that the user knows the IFF_RUNNING status
13971 				 * change.  (If the first ipif is up, then
13972 				 * we'll handle eventual routing socket
13973 				 * notification via DAD completion.)
13974 				 */
13975 				if (ipif == ill->ill_ipif) {
13976 					ip_rts_ifmsg(ill->ill_ipif,
13977 					    RTSQ_DEFAULT);
13978 				}
13979 			}
13980 		} else {
13981 			/*
13982 			 * After link down, we'll need to send a new routing
13983 			 * message when the link comes back, so clear
13984 			 * ipif_addr_ready.
13985 			 */
13986 			ipif->ipif_addr_ready = 0;
13987 		}
13988 	}
13989 
13990 	/*
13991 	 * If we've torn down links, then notify the user right away.
13992 	 */
13993 	if (!went_up)
13994 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13995 }
13996 
13997 static void
13998 ipsq_delete(ipsq_t *ipsq)
13999 {
14000 	ipxop_t *ipx = ipsq->ipsq_xop;
14001 
14002 	ipsq->ipsq_ipst = NULL;
14003 	ASSERT(ipsq->ipsq_phyint == NULL);
14004 	ASSERT(ipsq->ipsq_xop != NULL);
14005 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
14006 	ASSERT(ipx->ipx_pending_mp == NULL);
14007 	kmem_free(ipsq, sizeof (ipsq_t));
14008 }
14009 
14010 static int
14011 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
14012 {
14013 	int err;
14014 	ipif_t *ipif;
14015 
14016 	if (ill == NULL)
14017 		return (0);
14018 
14019 	/*
14020 	 * Except for ipif_state_flags and ill_state_flags the other
14021 	 * fields of the ipif/ill that are modified below are protected
14022 	 * implicitly since we are a writer. We would have tried to down
14023 	 * even an ipif that was already down, in ill_down_ipifs. So we
14024 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14025 	 */
14026 	ASSERT(IAM_WRITER_ILL(ill));
14027 
14028 	ill->ill_up_ipifs = B_TRUE;
14029 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14030 		mutex_enter(&ill->ill_lock);
14031 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
14032 		mutex_exit(&ill->ill_lock);
14033 		if (ipif->ipif_was_up) {
14034 			if (!(ipif->ipif_flags & IPIF_UP))
14035 				err = ipif_up(ipif, q, mp);
14036 			ipif->ipif_was_up = B_FALSE;
14037 			if (err != 0) {
14038 				ASSERT(err == EINPROGRESS);
14039 				return (err);
14040 			}
14041 		}
14042 	}
14043 	mutex_enter(&ill->ill_lock);
14044 	ill->ill_state_flags &= ~ILL_CHANGING;
14045 	mutex_exit(&ill->ill_lock);
14046 	ill->ill_up_ipifs = B_FALSE;
14047 	return (0);
14048 }
14049 
14050 /*
14051  * This function is called to bring up all the ipifs that were up before
14052  * bringing the ill down via ill_down_ipifs().
14053  */
14054 int
14055 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14056 {
14057 	int err;
14058 
14059 	ASSERT(IAM_WRITER_ILL(ill));
14060 
14061 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
14062 	if (err != 0)
14063 		return (err);
14064 
14065 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
14066 }
14067 
14068 /*
14069  * Bring down any IPIF_UP ipifs on ill.
14070  */
14071 static void
14072 ill_down_ipifs(ill_t *ill)
14073 {
14074 	ipif_t *ipif;
14075 
14076 	ASSERT(IAM_WRITER_ILL(ill));
14077 
14078 	/*
14079 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14080 	 * are modified below are protected implicitly since we are a writer
14081 	 */
14082 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14083 		/*
14084 		 * We go through the ipif_down logic even if the ipif
14085 		 * is already down, since routes can be added based
14086 		 * on down ipifs. Going through ipif_down once again
14087 		 * will delete any IREs created based on these routes.
14088 		 */
14089 		if (ipif->ipif_flags & IPIF_UP)
14090 			ipif->ipif_was_up = B_TRUE;
14091 
14092 		mutex_enter(&ill->ill_lock);
14093 		ipif->ipif_state_flags |= IPIF_CHANGING;
14094 		mutex_exit(&ill->ill_lock);
14095 
14096 		/*
14097 		 * Need to re-create net/subnet bcast ires if
14098 		 * they are dependent on ipif.
14099 		 */
14100 		if (!ipif->ipif_isv6)
14101 			ipif_check_bcast_ires(ipif);
14102 		(void) ipif_logical_down(ipif, NULL, NULL);
14103 		ipif_non_duplicate(ipif);
14104 		ipif_down_tail(ipif);
14105 	}
14106 }
14107 
14108 void
14109 ill_lock_ills(ill_t **list, int cnt)
14110 {
14111 	int	i;
14112 
14113 	if (cnt > 1) {
14114 		boolean_t try_again;
14115 		do {
14116 			try_again = B_FALSE;
14117 			for (i = 0; i < cnt - 1; i++) {
14118 				if (list[i] < list[i + 1]) {
14119 					ill_t	*tmp;
14120 
14121 					/* swap the elements */
14122 					tmp = list[i];
14123 					list[i] = list[i + 1];
14124 					list[i + 1] = tmp;
14125 					try_again = B_TRUE;
14126 				}
14127 			}
14128 		} while (try_again);
14129 	}
14130 
14131 	for (i = 0; i < cnt; i++) {
14132 		if (i == 0) {
14133 			if (list[i] != NULL)
14134 				mutex_enter(&list[i]->ill_lock);
14135 			else
14136 				return;
14137 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14138 			mutex_enter(&list[i]->ill_lock);
14139 		}
14140 	}
14141 }
14142 
14143 void
14144 ill_unlock_ills(ill_t **list, int cnt)
14145 {
14146 	int	i;
14147 
14148 	for (i = 0; i < cnt; i++) {
14149 		if ((i == 0) && (list[i] != NULL)) {
14150 			mutex_exit(&list[i]->ill_lock);
14151 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14152 			mutex_exit(&list[i]->ill_lock);
14153 		}
14154 	}
14155 }
14156 
14157 /*
14158  * Redo source address selection.  This is called when a
14159  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
14160  */
14161 void
14162 ill_update_source_selection(ill_t *ill)
14163 {
14164 	ipif_t *ipif;
14165 
14166 	ASSERT(IAM_WRITER_ILL(ill));
14167 
14168 	/*
14169 	 * Underlying interfaces are only used for test traffic and thus
14170 	 * should always send with their (deprecated) source addresses.
14171 	 */
14172 	if (IS_UNDER_IPMP(ill))
14173 		return;
14174 
14175 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14176 		if (ill->ill_isv6)
14177 			ipif_recreate_interface_routes_v6(NULL, ipif);
14178 		else
14179 			ipif_recreate_interface_routes(NULL, ipif);
14180 	}
14181 }
14182 
14183 /*
14184  * Finish the group join started in ip_sioctl_groupname().
14185  */
14186 /* ARGSUSED */
14187 static void
14188 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14189 {
14190 	ill_t		*ill = q->q_ptr;
14191 	phyint_t	*phyi = ill->ill_phyint;
14192 	ipmp_grp_t	*grp = phyi->phyint_grp;
14193 	ip_stack_t	*ipst = ill->ill_ipst;
14194 
14195 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14196 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14197 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14198 
14199 	if (phyi->phyint_illv4 != NULL) {
14200 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14201 		VERIFY(grp->gr_pendv4-- > 0);
14202 		rw_exit(&ipst->ips_ipmp_lock);
14203 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14204 	}
14205 	if (phyi->phyint_illv6 != NULL) {
14206 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14207 		VERIFY(grp->gr_pendv6-- > 0);
14208 		rw_exit(&ipst->ips_ipmp_lock);
14209 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14210 	}
14211 	freemsg(mp);
14212 }
14213 
14214 /*
14215  * Process an SIOCSLIFGROUPNAME request.
14216  */
14217 /* ARGSUSED */
14218 int
14219 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14220     ip_ioctl_cmd_t *ipip, void *ifreq)
14221 {
14222 	struct lifreq	*lifr = ifreq;
14223 	ill_t		*ill = ipif->ipif_ill;
14224 	ip_stack_t	*ipst = ill->ill_ipst;
14225 	phyint_t	*phyi = ill->ill_phyint;
14226 	ipmp_grp_t	*grp = phyi->phyint_grp;
14227 	mblk_t		*ipsq_mp;
14228 	int		err = 0;
14229 
14230 	/*
14231 	 * Note that phyint_grp can only change here, where we're exclusive.
14232 	 */
14233 	ASSERT(IAM_WRITER_ILL(ill));
14234 
14235 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14236 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14237 		return (EINVAL);
14238 
14239 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14240 
14241 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14242 
14243 	/*
14244 	 * If the name hasn't changed, there's nothing to do.
14245 	 */
14246 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14247 		goto unlock;
14248 
14249 	/*
14250 	 * Handle requests to rename an IPMP meta-interface.
14251 	 *
14252 	 * Note that creation of the IPMP meta-interface is handled in
14253 	 * userland through the standard plumbing sequence.  As part of the
14254 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14255 	 * the name of the interface (see ipif_set_values_tail()).
14256 	 */
14257 	if (IS_IPMP(ill)) {
14258 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14259 		goto unlock;
14260 	}
14261 
14262 	/*
14263 	 * Handle requests to add or remove an IP interface from a group.
14264 	 */
14265 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14266 		/*
14267 		 * Moves are handled by first removing the interface from
14268 		 * its existing group, and then adding it to another group.
14269 		 * So, fail if it's already in a group.
14270 		 */
14271 		if (IS_UNDER_IPMP(ill)) {
14272 			err = EALREADY;
14273 			goto unlock;
14274 		}
14275 
14276 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14277 		if (grp == NULL) {
14278 			err = ENOENT;
14279 			goto unlock;
14280 		}
14281 
14282 		/*
14283 		 * Check if the phyint and its ills are suitable for
14284 		 * inclusion into the group.
14285 		 */
14286 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14287 			goto unlock;
14288 
14289 		/*
14290 		 * Checks pass; join the group, and enqueue the remaining
14291 		 * illgrp joins for when we've become part of the group xop
14292 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14293 		 * requires an mblk_t to scribble on, and since `mp' will be
14294 		 * freed as part of completing the ioctl, allocate another.
14295 		 */
14296 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14297 			err = ENOMEM;
14298 			goto unlock;
14299 		}
14300 
14301 		/*
14302 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14303 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14304 		 * before ip_join_illgrps() is called back.  See the comments
14305 		 * in ip_sioctl_plink_ipmp() for more.
14306 		 */
14307 		if (phyi->phyint_illv4 != NULL)
14308 			grp->gr_pendv4++;
14309 		if (phyi->phyint_illv6 != NULL)
14310 			grp->gr_pendv6++;
14311 
14312 		rw_exit(&ipst->ips_ipmp_lock);
14313 
14314 		ipmp_phyint_join_grp(phyi, grp);
14315 		ill_refhold(ill);
14316 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14317 		    SWITCH_OP, B_FALSE);
14318 		return (0);
14319 	} else {
14320 		/*
14321 		 * Request to remove the interface from a group.  If the
14322 		 * interface is not in a group, this trivially succeeds.
14323 		 */
14324 		rw_exit(&ipst->ips_ipmp_lock);
14325 		if (IS_UNDER_IPMP(ill))
14326 			ipmp_phyint_leave_grp(phyi);
14327 		return (0);
14328 	}
14329 unlock:
14330 	rw_exit(&ipst->ips_ipmp_lock);
14331 	return (err);
14332 }
14333 
14334 /*
14335  * Process an SIOCGLIFBINDING request.
14336  */
14337 /* ARGSUSED */
14338 int
14339 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14340     ip_ioctl_cmd_t *ipip, void *ifreq)
14341 {
14342 	ill_t		*bound_ill;
14343 	struct lifreq	*lifr = ifreq;
14344 
14345 	if (!IS_IPMP(ipif->ipif_ill))
14346 		return (EINVAL);
14347 
14348 	if ((bound_ill = ipmp_ipif_hold_bound_ill(ipif)) == NULL) {
14349 		lifr->lifr_binding[0] = '\0';
14350 		return (0);
14351 	}
14352 
14353 	(void) strlcpy(lifr->lifr_binding, bound_ill->ill_name, LIFNAMSIZ);
14354 	ill_refrele(bound_ill);
14355 	return (0);
14356 }
14357 
14358 /*
14359  * Process an SIOCGLIFGROUPNAME request.
14360  */
14361 /* ARGSUSED */
14362 int
14363 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14364     ip_ioctl_cmd_t *ipip, void *ifreq)
14365 {
14366 	ipmp_grp_t	*grp;
14367 	struct lifreq	*lifr = ifreq;
14368 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14369 
14370 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14371 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14372 		lifr->lifr_groupname[0] = '\0';
14373 	else
14374 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14375 	rw_exit(&ipst->ips_ipmp_lock);
14376 	return (0);
14377 }
14378 
14379 /*
14380  * Process an SIOCGLIFGROUPINFO request.
14381  */
14382 /* ARGSUSED */
14383 int
14384 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14385     ip_ioctl_cmd_t *ipip, void *dummy)
14386 {
14387 	lifgroupinfo_t	*lifgr;
14388 	ipmp_grp_t	*grp;
14389 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14390 
14391 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14392 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14393 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14394 
14395 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14396 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14397 		rw_exit(&ipst->ips_ipmp_lock);
14398 		return (ENOENT);
14399 	}
14400 	ipmp_grp_info(grp, lifgr);
14401 	rw_exit(&ipst->ips_ipmp_lock);
14402 	return (0);
14403 }
14404 
14405 static void
14406 ill_dl_down(ill_t *ill)
14407 {
14408 	/*
14409 	 * The ill is down; unbind but stay attached since we're still
14410 	 * associated with a PPA. If we have negotiated DLPI capabilites
14411 	 * with the data link service provider (IDS_OK) then reset them.
14412 	 * The interval between unbinding and rebinding is potentially
14413 	 * unbounded hence we cannot assume things will be the same.
14414 	 * The DLPI capabilities will be probed again when the data link
14415 	 * is brought up.
14416 	 */
14417 	mblk_t	*mp = ill->ill_unbind_mp;
14418 
14419 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14420 
14421 	ill->ill_unbind_mp = NULL;
14422 	if (mp != NULL) {
14423 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14424 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14425 		    ill->ill_name));
14426 		mutex_enter(&ill->ill_lock);
14427 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14428 		mutex_exit(&ill->ill_lock);
14429 		/*
14430 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14431 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14432 		 * ill_capability_dld_disable disable rightaway. If this is not
14433 		 * an unplumb operation then the disable happens on receipt of
14434 		 * the capab ack via ip_rput_dlpi_writer ->
14435 		 * ill_capability_ack_thr. In both cases the order of
14436 		 * the operations seen by DLD is capability disable followed
14437 		 * by DL_UNBIND. Also the DLD capability disable needs a
14438 		 * cv_wait'able context.
14439 		 */
14440 		if (ill->ill_state_flags & ILL_CONDEMNED)
14441 			ill_capability_dld_disable(ill);
14442 		ill_capability_reset(ill, B_FALSE);
14443 		ill_dlpi_send(ill, mp);
14444 	}
14445 
14446 	/*
14447 	 * Toss all of our multicast memberships.  We could keep them, but
14448 	 * then we'd have to do bookkeeping of any joins and leaves performed
14449 	 * by the application while the the interface is down (we can't just
14450 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14451 	 * on a downed interface).
14452 	 */
14453 	ill_leave_multicast(ill);
14454 
14455 	mutex_enter(&ill->ill_lock);
14456 	ill->ill_dl_up = 0;
14457 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14458 	mutex_exit(&ill->ill_lock);
14459 }
14460 
14461 static void
14462 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14463 {
14464 	union DL_primitives *dlp;
14465 	t_uscalar_t prim;
14466 
14467 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14468 
14469 	dlp = (union DL_primitives *)mp->b_rptr;
14470 	prim = dlp->dl_primitive;
14471 
14472 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14473 	    dl_primstr(prim), prim, ill->ill_name));
14474 
14475 	switch (prim) {
14476 	case DL_PHYS_ADDR_REQ:
14477 	{
14478 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14479 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14480 		break;
14481 	}
14482 	case DL_BIND_REQ:
14483 		mutex_enter(&ill->ill_lock);
14484 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14485 		mutex_exit(&ill->ill_lock);
14486 		break;
14487 	}
14488 
14489 	/*
14490 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14491 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14492 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14493 	 */
14494 	mutex_enter(&ill->ill_lock);
14495 	if (!(ill->ill_state_flags & ILL_CONDEMNED) || (prim == DL_UNBIND_REQ))
14496 		ill->ill_dlpi_pending = prim;
14497 
14498 	mutex_exit(&ill->ill_lock);
14499 	putnext(ill->ill_wq, mp);
14500 }
14501 
14502 /*
14503  * Helper function for ill_dlpi_send().
14504  */
14505 /* ARGSUSED */
14506 static void
14507 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14508 {
14509 	ill_dlpi_send(q->q_ptr, mp);
14510 }
14511 
14512 /*
14513  * Send a DLPI control message to the driver but make sure there
14514  * is only one outstanding message. Uses ill_dlpi_pending to tell
14515  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14516  * when an ACK or a NAK is received to process the next queued message.
14517  */
14518 void
14519 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14520 {
14521 	mblk_t **mpp;
14522 
14523 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14524 
14525 	/*
14526 	 * To ensure that any DLPI requests for current exclusive operation
14527 	 * are always completely sent before any DLPI messages for other
14528 	 * operations, require writer access before enqueuing.
14529 	 */
14530 	if (!IAM_WRITER_ILL(ill)) {
14531 		ill_refhold(ill);
14532 		/* qwriter_ip() does the ill_refrele() */
14533 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14534 		    NEW_OP, B_TRUE);
14535 		return;
14536 	}
14537 
14538 	mutex_enter(&ill->ill_lock);
14539 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14540 		/* Must queue message. Tail insertion */
14541 		mpp = &ill->ill_dlpi_deferred;
14542 		while (*mpp != NULL)
14543 			mpp = &((*mpp)->b_next);
14544 
14545 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14546 		    ill->ill_name));
14547 
14548 		*mpp = mp;
14549 		mutex_exit(&ill->ill_lock);
14550 		return;
14551 	}
14552 	mutex_exit(&ill->ill_lock);
14553 	ill_dlpi_dispatch(ill, mp);
14554 }
14555 
14556 static void
14557 ill_capability_send(ill_t *ill, mblk_t *mp)
14558 {
14559 	ill->ill_capab_pending_cnt++;
14560 	ill_dlpi_send(ill, mp);
14561 }
14562 
14563 void
14564 ill_capability_done(ill_t *ill)
14565 {
14566 	ASSERT(ill->ill_capab_pending_cnt != 0);
14567 
14568 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14569 
14570 	ill->ill_capab_pending_cnt--;
14571 	if (ill->ill_capab_pending_cnt == 0 &&
14572 	    ill->ill_dlpi_capab_state == IDCS_OK)
14573 		ill_capability_reset_alloc(ill);
14574 }
14575 
14576 /*
14577  * Send all deferred DLPI messages without waiting for their ACKs.
14578  */
14579 void
14580 ill_dlpi_send_deferred(ill_t *ill)
14581 {
14582 	mblk_t *mp, *nextmp;
14583 
14584 	/*
14585 	 * Clear ill_dlpi_pending so that the message is not queued in
14586 	 * ill_dlpi_send().
14587 	 */
14588 	mutex_enter(&ill->ill_lock);
14589 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14590 	mp = ill->ill_dlpi_deferred;
14591 	ill->ill_dlpi_deferred = NULL;
14592 	mutex_exit(&ill->ill_lock);
14593 
14594 	for (; mp != NULL; mp = nextmp) {
14595 		nextmp = mp->b_next;
14596 		mp->b_next = NULL;
14597 		ill_dlpi_send(ill, mp);
14598 	}
14599 }
14600 
14601 /*
14602  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14603  */
14604 boolean_t
14605 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14606 {
14607 	t_uscalar_t pending;
14608 
14609 	mutex_enter(&ill->ill_lock);
14610 	if (ill->ill_dlpi_pending == prim) {
14611 		mutex_exit(&ill->ill_lock);
14612 		return (B_TRUE);
14613 	}
14614 
14615 	/*
14616 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14617 	 * without waiting, so don't print any warnings in that case.
14618 	 */
14619 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14620 		mutex_exit(&ill->ill_lock);
14621 		return (B_FALSE);
14622 	}
14623 	pending = ill->ill_dlpi_pending;
14624 	mutex_exit(&ill->ill_lock);
14625 
14626 	if (pending == DL_PRIM_INVAL) {
14627 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14628 		    "received unsolicited ack for %s on %s\n",
14629 		    dl_primstr(prim), ill->ill_name);
14630 	} else {
14631 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14632 		    "received unexpected ack for %s on %s (expecting %s)\n",
14633 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14634 	}
14635 	return (B_FALSE);
14636 }
14637 
14638 /*
14639  * Complete the current DLPI operation associated with `prim' on `ill' and
14640  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14641  * operations and the ill's current exclusive IPSQ operation has finished
14642  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14643  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14644  * the comments above ipsq_current_finish() for details.
14645  */
14646 void
14647 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14648 {
14649 	mblk_t *mp;
14650 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14651 	ipxop_t *ipx = ipsq->ipsq_xop;
14652 
14653 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14654 	mutex_enter(&ill->ill_lock);
14655 
14656 	ASSERT(prim != DL_PRIM_INVAL);
14657 	ASSERT(ill->ill_dlpi_pending == prim);
14658 
14659 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14660 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14661 
14662 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14663 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14664 		if (ipx->ipx_current_done) {
14665 			mutex_enter(&ipx->ipx_lock);
14666 			ipx->ipx_current_ipif = NULL;
14667 			mutex_exit(&ipx->ipx_lock);
14668 		}
14669 		cv_signal(&ill->ill_cv);
14670 		mutex_exit(&ill->ill_lock);
14671 		return;
14672 	}
14673 
14674 	ill->ill_dlpi_deferred = mp->b_next;
14675 	mp->b_next = NULL;
14676 	mutex_exit(&ill->ill_lock);
14677 
14678 	ill_dlpi_dispatch(ill, mp);
14679 }
14680 
14681 void
14682 conn_delete_ire(conn_t *connp, caddr_t arg)
14683 {
14684 	ipif_t	*ipif = (ipif_t *)arg;
14685 	ire_t	*ire;
14686 
14687 	/*
14688 	 * Look at the cached ires on conns which has pointers to ipifs.
14689 	 * We just call ire_refrele which clears up the reference
14690 	 * to ire. Called when a conn closes. Also called from ipif_free
14691 	 * to cleanup indirect references to the stale ipif via the cached ire.
14692 	 */
14693 	mutex_enter(&connp->conn_lock);
14694 	ire = connp->conn_ire_cache;
14695 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14696 		connp->conn_ire_cache = NULL;
14697 		mutex_exit(&connp->conn_lock);
14698 		IRE_REFRELE_NOTR(ire);
14699 		return;
14700 	}
14701 	mutex_exit(&connp->conn_lock);
14702 
14703 }
14704 
14705 /*
14706  * Some operations (e.g., ipif_down()) conditionally delete a number
14707  * of IREs. Those IREs may have been previously cached in the conn structure.
14708  * This ipcl_walk() walker function releases all references to such IREs based
14709  * on the condemned flag.
14710  */
14711 /* ARGSUSED */
14712 void
14713 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14714 {
14715 	ire_t	*ire;
14716 
14717 	mutex_enter(&connp->conn_lock);
14718 	ire = connp->conn_ire_cache;
14719 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14720 		connp->conn_ire_cache = NULL;
14721 		mutex_exit(&connp->conn_lock);
14722 		IRE_REFRELE_NOTR(ire);
14723 		return;
14724 	}
14725 	mutex_exit(&connp->conn_lock);
14726 }
14727 
14728 /*
14729  * Take down a specific interface, but don't lose any information about it.
14730  * (Always called as writer.)
14731  * This function goes through the down sequence even if the interface is
14732  * already down. There are 2 reasons.
14733  * a. Currently we permit interface routes that depend on down interfaces
14734  *    to be added. This behaviour itself is questionable. However it appears
14735  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14736  *    time. We go thru the cleanup in order to remove these routes.
14737  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14738  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14739  *    down, but we need to cleanup i.e. do ill_dl_down and
14740  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14741  *
14742  * IP-MT notes:
14743  *
14744  * Model of reference to interfaces.
14745  *
14746  * The following members in ipif_t track references to the ipif.
14747  *	int     ipif_refcnt;    Active reference count
14748  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14749  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14750  *
14751  * The following members in ill_t track references to the ill.
14752  *	int             ill_refcnt;     active refcnt
14753  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14754  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14755  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14756  *
14757  * Reference to an ipif or ill can be obtained in any of the following ways.
14758  *
14759  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14760  * Pointers to ipif / ill from other data structures viz ire and conn.
14761  * Implicit reference to the ipif / ill by holding a reference to the ire.
14762  *
14763  * The ipif/ill lookup functions return a reference held ipif / ill.
14764  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14765  * This is a purely dynamic reference count associated with threads holding
14766  * references to the ipif / ill. Pointers from other structures do not
14767  * count towards this reference count.
14768  *
14769  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14770  * associated with the ipif/ill. This is incremented whenever a new
14771  * ire is created referencing the ipif/ill. This is done atomically inside
14772  * ire_add_v[46] where the ire is actually added to the ire hash table.
14773  * The count is decremented in ire_inactive where the ire is destroyed.
14774  *
14775  * nce's reference ill's thru nce_ill and the count of nce's associated with
14776  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14777  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14778  * table. Similarly it is decremented in ndp_inactive() where the nce
14779  * is destroyed.
14780  *
14781  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14782  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14783  * in ilm_walker_cleanup() or ilm_delete().
14784  *
14785  * Flow of ioctls involving interface down/up
14786  *
14787  * The following is the sequence of an attempt to set some critical flags on an
14788  * up interface.
14789  * ip_sioctl_flags
14790  * ipif_down
14791  * wait for ipif to be quiescent
14792  * ipif_down_tail
14793  * ip_sioctl_flags_tail
14794  *
14795  * All set ioctls that involve down/up sequence would have a skeleton similar
14796  * to the above. All the *tail functions are called after the refcounts have
14797  * dropped to the appropriate values.
14798  *
14799  * The mechanism to quiesce an ipif is as follows.
14800  *
14801  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14802  * on the ipif. Callers either pass a flag requesting wait or the lookup
14803  *  functions will return NULL.
14804  *
14805  * Delete all ires referencing this ipif
14806  *
14807  * Any thread attempting to do an ipif_refhold on an ipif that has been
14808  * obtained thru a cached pointer will first make sure that
14809  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14810  * increment the refcount.
14811  *
14812  * The above guarantees that the ipif refcount will eventually come down to
14813  * zero and the ipif will quiesce, once all threads that currently hold a
14814  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14815  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14816  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14817  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14818  * in ip.h
14819  *
14820  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14821  *
14822  * Threads trying to lookup an ipif or ill can pass a flag requesting
14823  * wait and restart if the ipif / ill cannot be looked up currently.
14824  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14825  * failure if the ipif is currently undergoing an exclusive operation, and
14826  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14827  * is restarted by ipsq_exit() when the current exclusive operation completes.
14828  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14829  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14830  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14831  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14832  * until we release the ipsq_lock, even though the the ill/ipif state flags
14833  * can change after we drop the ill_lock.
14834  *
14835  * An attempt to send out a packet using an ipif that is currently
14836  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14837  * operation and restart it later when the exclusive condition on the ipif ends.
14838  * This is an example of not passing the wait flag to the lookup functions. For
14839  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14840  * out a multicast packet on that ipif will fail while the ipif is
14841  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14842  * currently IPIF_CHANGING will also fail.
14843  */
14844 int
14845 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14846 {
14847 	ill_t		*ill = ipif->ipif_ill;
14848 	conn_t		*connp;
14849 	boolean_t	success;
14850 	boolean_t	ipif_was_up = B_FALSE;
14851 	ip_stack_t	*ipst = ill->ill_ipst;
14852 
14853 	ASSERT(IAM_WRITER_IPIF(ipif));
14854 
14855 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14856 
14857 	if (ipif->ipif_flags & IPIF_UP) {
14858 		mutex_enter(&ill->ill_lock);
14859 		ipif->ipif_flags &= ~IPIF_UP;
14860 		ASSERT(ill->ill_ipif_up_count > 0);
14861 		--ill->ill_ipif_up_count;
14862 		mutex_exit(&ill->ill_lock);
14863 		ipif_was_up = B_TRUE;
14864 		/* Update status in SCTP's list */
14865 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14866 		ill_nic_event_dispatch(ipif->ipif_ill,
14867 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14868 	}
14869 
14870 	/*
14871 	 * Blow away memberships we established in ipif_multicast_up().
14872 	 */
14873 	ipif_multicast_down(ipif);
14874 
14875 	/*
14876 	 * Remove from the mapping for __sin6_src_id. We insert only
14877 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14878 	 * stored as mapped addresses, we need to check for mapped
14879 	 * INADDR_ANY also.
14880 	 */
14881 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14882 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14883 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14884 		int err;
14885 
14886 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14887 		    ipif->ipif_zoneid, ipst);
14888 		if (err != 0) {
14889 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14890 		}
14891 	}
14892 
14893 	/*
14894 	 * Delete all IRE's pointing at this ipif or its source address.
14895 	 */
14896 	if (ipif->ipif_isv6) {
14897 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14898 		    ipst);
14899 	} else {
14900 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14901 		    ipst);
14902 	}
14903 
14904 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14905 		/*
14906 		 * Since the interface is now down, it may have just become
14907 		 * inactive.  Note that this needs to be done even for a
14908 		 * lll_logical_down(), or ARP entries will not get correctly
14909 		 * restored when the interface comes back up.
14910 		 */
14911 		if (IS_UNDER_IPMP(ill))
14912 			ipmp_ill_refresh_active(ill);
14913 	}
14914 
14915 	/*
14916 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14917 	 * ires have been deleted above. Otherwise a thread could end up
14918 	 * caching an ire in a conn after we have finished the cleanup of the
14919 	 * conn. The caching is done after making sure that the ire is not yet
14920 	 * condemned. Also documented in the block comment above ip_output
14921 	 */
14922 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14923 	/* Also, delete the ires cached in SCTP */
14924 	sctp_ire_cache_flush(ipif);
14925 
14926 	/*
14927 	 * Update any other ipifs which have used "our" local address as
14928 	 * a source address. This entails removing and recreating IRE_INTERFACE
14929 	 * entries for such ipifs.
14930 	 */
14931 	if (ipif->ipif_isv6)
14932 		ipif_update_other_ipifs_v6(ipif);
14933 	else
14934 		ipif_update_other_ipifs(ipif);
14935 
14936 	/*
14937 	 * neighbor-discovery or arp entries for this interface.
14938 	 */
14939 	ipif_ndp_down(ipif);
14940 
14941 	/*
14942 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14943 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14944 	 * and ill_delete -> ipif_free -> ipif_down
14945 	 */
14946 	if (mp == NULL) {
14947 		ASSERT(q == NULL);
14948 		return (0);
14949 	}
14950 
14951 	if (CONN_Q(q)) {
14952 		connp = Q_TO_CONN(q);
14953 		mutex_enter(&connp->conn_lock);
14954 	} else {
14955 		connp = NULL;
14956 	}
14957 	mutex_enter(&ill->ill_lock);
14958 	/*
14959 	 * Are there any ire's pointing to this ipif that are still active ?
14960 	 * If this is the last ipif going down, are there any ire's pointing
14961 	 * to this ill that are still active ?
14962 	 */
14963 	if (ipif_is_quiescent(ipif)) {
14964 		mutex_exit(&ill->ill_lock);
14965 		if (connp != NULL)
14966 			mutex_exit(&connp->conn_lock);
14967 		return (0);
14968 	}
14969 
14970 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14971 	    ill->ill_name, (void *)ill));
14972 	/*
14973 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14974 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14975 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14976 	 */
14977 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14978 	if (!success) {
14979 		/* The conn is closing. So just return */
14980 		ASSERT(connp != NULL);
14981 		mutex_exit(&ill->ill_lock);
14982 		mutex_exit(&connp->conn_lock);
14983 		return (EINTR);
14984 	}
14985 
14986 	mutex_exit(&ill->ill_lock);
14987 	if (connp != NULL)
14988 		mutex_exit(&connp->conn_lock);
14989 	return (EINPROGRESS);
14990 }
14991 
14992 void
14993 ipif_down_tail(ipif_t *ipif)
14994 {
14995 	ill_t	*ill = ipif->ipif_ill;
14996 
14997 	/*
14998 	 * Skip any loopback interface (null wq).
14999 	 * If this is the last logical interface on the ill
15000 	 * have ill_dl_down tell the driver we are gone (unbind)
15001 	 * Note that lun 0 can ipif_down even though
15002 	 * there are other logical units that are up.
15003 	 * This occurs e.g. when we change a "significant" IFF_ flag.
15004 	 */
15005 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
15006 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
15007 	    ill->ill_dl_up) {
15008 		ill_dl_down(ill);
15009 	}
15010 	ill->ill_logical_down = 0;
15011 
15012 	/*
15013 	 * Has to be after removing the routes in ipif_down_delete_ire.
15014 	 */
15015 	ipif_resolver_down(ipif);
15016 
15017 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15018 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
15019 }
15020 
15021 /*
15022  * Bring interface logically down without bringing the physical interface
15023  * down e.g. when the netmask is changed. This avoids long lasting link
15024  * negotiations between an ethernet interface and a certain switches.
15025  */
15026 static int
15027 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
15028 {
15029 	/*
15030 	 * The ill_logical_down flag is a transient flag. It is set here
15031 	 * and is cleared once the down has completed in ipif_down_tail.
15032 	 * This flag does not indicate whether the ill stream is in the
15033 	 * DL_BOUND state with the driver. Instead this flag is used by
15034 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
15035 	 * the driver. The state of the ill stream i.e. whether it is
15036 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
15037 	 */
15038 	ipif->ipif_ill->ill_logical_down = 1;
15039 	return (ipif_down(ipif, q, mp));
15040 }
15041 
15042 /*
15043  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
15044  * If the usesrc client ILL is already part of a usesrc group or not,
15045  * in either case a ire_stq with the matching usesrc client ILL will
15046  * locate the IRE's that need to be deleted. We want IREs to be created
15047  * with the new source address.
15048  */
15049 static void
15050 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
15051 {
15052 	ill_t	*ucill = (ill_t *)ill_arg;
15053 
15054 	ASSERT(IAM_WRITER_ILL(ucill));
15055 
15056 	if (ire->ire_stq == NULL)
15057 		return;
15058 
15059 	if ((ire->ire_type == IRE_CACHE) &&
15060 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
15061 		ire_delete(ire);
15062 }
15063 
15064 /*
15065  * ire_walk routine to delete every IRE dependent on the interface
15066  * address that is going down.	(Always called as writer.)
15067  * Works for both v4 and v6.
15068  * In addition for checking for ire_ipif matches it also checks for
15069  * IRE_CACHE entries which have the same source address as the
15070  * disappearing ipif since ipif_select_source might have picked
15071  * that source. Note that ipif_down/ipif_update_other_ipifs takes
15072  * care of any IRE_INTERFACE with the disappearing source address.
15073  */
15074 static void
15075 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
15076 {
15077 	ipif_t	*ipif = (ipif_t *)ipif_arg;
15078 
15079 	ASSERT(IAM_WRITER_IPIF(ipif));
15080 	if (ire->ire_ipif == NULL)
15081 		return;
15082 
15083 	if (ire->ire_ipif != ipif) {
15084 		/*
15085 		 * Look for a matching source address.
15086 		 */
15087 		if (ire->ire_type != IRE_CACHE)
15088 			return;
15089 		if (ipif->ipif_flags & IPIF_NOLOCAL)
15090 			return;
15091 
15092 		if (ire->ire_ipversion == IPV4_VERSION) {
15093 			if (ire->ire_src_addr != ipif->ipif_src_addr)
15094 				return;
15095 		} else {
15096 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
15097 			    &ipif->ipif_v6lcl_addr))
15098 				return;
15099 		}
15100 		ire_delete(ire);
15101 		return;
15102 	}
15103 	/*
15104 	 * ire_delete() will do an ire_flush_cache which will delete
15105 	 * all ire_ipif matches
15106 	 */
15107 	ire_delete(ire);
15108 }
15109 
15110 /*
15111  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
15112  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
15113  * 2) when an interface is brought up or down (on that ill).
15114  * This ensures that the IRE_CACHE entries don't retain stale source
15115  * address selection results.
15116  */
15117 void
15118 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
15119 {
15120 	ill_t	*ill = (ill_t *)ill_arg;
15121 
15122 	ASSERT(IAM_WRITER_ILL(ill));
15123 	ASSERT(ire->ire_type == IRE_CACHE);
15124 
15125 	/*
15126 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15127 	 * ill, but we only want to delete the IRE if ire_ipif matches.
15128 	 */
15129 	ASSERT(ire->ire_ipif != NULL);
15130 	if (ill == ire->ire_ipif->ipif_ill)
15131 		ire_delete(ire);
15132 }
15133 
15134 /*
15135  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
15136  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
15137  * the IPMP ill.
15138  */
15139 void
15140 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
15141 {
15142 	ill_t	*ill = (ill_t *)ill_arg;
15143 
15144 	ASSERT(IAM_WRITER_ILL(ill));
15145 	ASSERT(ire->ire_type == IRE_CACHE);
15146 
15147 	/*
15148 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15149 	 * ill, but we only want to delete the IRE if ire_stq matches.
15150 	 */
15151 	if (ire->ire_stq->q_ptr == ill_arg)
15152 		ire_delete(ire);
15153 }
15154 
15155 /*
15156  * Delete all broadcast IREs with a source address on `ill_arg'.
15157  */
15158 static void
15159 ill_broadcast_delete(ire_t *ire, char *ill_arg)
15160 {
15161 	ill_t *ill = (ill_t *)ill_arg;
15162 
15163 	ASSERT(IAM_WRITER_ILL(ill));
15164 	ASSERT(ire->ire_type == IRE_BROADCAST);
15165 
15166 	if (ire->ire_ipif->ipif_ill == ill)
15167 		ire_delete(ire);
15168 }
15169 
15170 /*
15171  * Initiate deallocate of an IPIF. Always called as writer. Called by
15172  * ill_delete or ip_sioctl_removeif.
15173  */
15174 static void
15175 ipif_free(ipif_t *ipif)
15176 {
15177 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15178 
15179 	ASSERT(IAM_WRITER_IPIF(ipif));
15180 
15181 	if (ipif->ipif_recovery_id != 0)
15182 		(void) untimeout(ipif->ipif_recovery_id);
15183 	ipif->ipif_recovery_id = 0;
15184 
15185 	/* Remove conn references */
15186 	reset_conn_ipif(ipif);
15187 
15188 	/*
15189 	 * Make sure we have valid net and subnet broadcast ire's for the
15190 	 * other ipif's which share them with this ipif.
15191 	 */
15192 	if (!ipif->ipif_isv6)
15193 		ipif_check_bcast_ires(ipif);
15194 
15195 	/*
15196 	 * Take down the interface. We can be called either from ill_delete
15197 	 * or from ip_sioctl_removeif.
15198 	 */
15199 	(void) ipif_down(ipif, NULL, NULL);
15200 
15201 	/*
15202 	 * Now that the interface is down, there's no chance it can still
15203 	 * become a duplicate.  Cancel any timer that may have been set while
15204 	 * tearing down.
15205 	 */
15206 	if (ipif->ipif_recovery_id != 0)
15207 		(void) untimeout(ipif->ipif_recovery_id);
15208 	ipif->ipif_recovery_id = 0;
15209 
15210 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15211 	/* Remove pointers to this ill in the multicast routing tables */
15212 	reset_mrt_vif_ipif(ipif);
15213 	/* If necessary, clear the cached source ipif rotor. */
15214 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15215 		ipif->ipif_ill->ill_src_ipif = NULL;
15216 	rw_exit(&ipst->ips_ill_g_lock);
15217 }
15218 
15219 static void
15220 ipif_free_tail(ipif_t *ipif)
15221 {
15222 	mblk_t	*mp;
15223 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15224 
15225 	/*
15226 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15227 	 */
15228 	mutex_enter(&ipif->ipif_saved_ire_lock);
15229 	mp = ipif->ipif_saved_ire_mp;
15230 	ipif->ipif_saved_ire_mp = NULL;
15231 	mutex_exit(&ipif->ipif_saved_ire_lock);
15232 	freemsg(mp);
15233 
15234 	/*
15235 	 * Need to hold both ill_g_lock and ill_lock while
15236 	 * inserting or removing an ipif from the linked list
15237 	 * of ipifs hanging off the ill.
15238 	 */
15239 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15240 
15241 	ASSERT(ilm_walk_ipif(ipif) == 0);
15242 
15243 #ifdef DEBUG
15244 	ipif_trace_cleanup(ipif);
15245 #endif
15246 
15247 	/* Ask SCTP to take it out of it list */
15248 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15249 
15250 	/* Get it out of the ILL interface list. */
15251 	ipif_remove(ipif);
15252 	rw_exit(&ipst->ips_ill_g_lock);
15253 
15254 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15255 
15256 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15257 	ASSERT(ipif->ipif_recovery_id == 0);
15258 
15259 	/* Free the memory. */
15260 	mi_free(ipif);
15261 }
15262 
15263 /*
15264  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15265  * is zero.
15266  */
15267 void
15268 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15269 {
15270 	char	lbuf[LIFNAMSIZ];
15271 	char	*name;
15272 	size_t	name_len;
15273 
15274 	buf[0] = '\0';
15275 	name = ipif->ipif_ill->ill_name;
15276 	name_len = ipif->ipif_ill->ill_name_length;
15277 	if (ipif->ipif_id != 0) {
15278 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15279 		    ipif->ipif_id);
15280 		name = lbuf;
15281 		name_len = mi_strlen(name) + 1;
15282 	}
15283 	len -= 1;
15284 	buf[len] = '\0';
15285 	len = MIN(len, name_len);
15286 	bcopy(name, buf, len);
15287 }
15288 
15289 /*
15290  * Find an IPIF based on the name passed in.  Names can be of the
15291  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15292  * The <phys> string can have forms like <dev><#> (e.g., le0),
15293  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15294  * When there is no colon, the implied unit id is zero. <phys> must
15295  * correspond to the name of an ILL.  (May be called as writer.)
15296  */
15297 static ipif_t *
15298 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15299     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15300     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15301 {
15302 	char	*cp;
15303 	char	*endp;
15304 	long	id;
15305 	ill_t	*ill;
15306 	ipif_t	*ipif;
15307 	uint_t	ire_type;
15308 	boolean_t did_alloc = B_FALSE;
15309 	ipsq_t	*ipsq;
15310 
15311 	if (error != NULL)
15312 		*error = 0;
15313 
15314 	/*
15315 	 * If the caller wants to us to create the ipif, make sure we have a
15316 	 * valid zoneid
15317 	 */
15318 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15319 
15320 	if (namelen == 0) {
15321 		if (error != NULL)
15322 			*error = ENXIO;
15323 		return (NULL);
15324 	}
15325 
15326 	*exists = B_FALSE;
15327 	/* Look for a colon in the name. */
15328 	endp = &name[namelen];
15329 	for (cp = endp; --cp > name; ) {
15330 		if (*cp == IPIF_SEPARATOR_CHAR)
15331 			break;
15332 	}
15333 
15334 	if (*cp == IPIF_SEPARATOR_CHAR) {
15335 		/*
15336 		 * Reject any non-decimal aliases for logical
15337 		 * interfaces. Aliases with leading zeroes
15338 		 * are also rejected as they introduce ambiguity
15339 		 * in the naming of the interfaces.
15340 		 * In order to confirm with existing semantics,
15341 		 * and to not break any programs/script relying
15342 		 * on that behaviour, if<0>:0 is considered to be
15343 		 * a valid interface.
15344 		 *
15345 		 * If alias has two or more digits and the first
15346 		 * is zero, fail.
15347 		 */
15348 		if (&cp[2] < endp && cp[1] == '0') {
15349 			if (error != NULL)
15350 				*error = EINVAL;
15351 			return (NULL);
15352 		}
15353 	}
15354 
15355 	if (cp <= name) {
15356 		cp = endp;
15357 	} else {
15358 		*cp = '\0';
15359 	}
15360 
15361 	/*
15362 	 * Look up the ILL, based on the portion of the name
15363 	 * before the slash. ill_lookup_on_name returns a held ill.
15364 	 * Temporary to check whether ill exists already. If so
15365 	 * ill_lookup_on_name will clear it.
15366 	 */
15367 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15368 	    q, mp, func, error, &did_alloc, ipst);
15369 	if (cp != endp)
15370 		*cp = IPIF_SEPARATOR_CHAR;
15371 	if (ill == NULL)
15372 		return (NULL);
15373 
15374 	/* Establish the unit number in the name. */
15375 	id = 0;
15376 	if (cp < endp && *endp == '\0') {
15377 		/* If there was a colon, the unit number follows. */
15378 		cp++;
15379 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15380 			ill_refrele(ill);
15381 			if (error != NULL)
15382 				*error = ENXIO;
15383 			return (NULL);
15384 		}
15385 	}
15386 
15387 	GRAB_CONN_LOCK(q);
15388 	mutex_enter(&ill->ill_lock);
15389 	/* Now see if there is an IPIF with this unit number. */
15390 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15391 		if (ipif->ipif_id == id) {
15392 			if (zoneid != ALL_ZONES &&
15393 			    zoneid != ipif->ipif_zoneid &&
15394 			    ipif->ipif_zoneid != ALL_ZONES) {
15395 				mutex_exit(&ill->ill_lock);
15396 				RELEASE_CONN_LOCK(q);
15397 				ill_refrele(ill);
15398 				if (error != NULL)
15399 					*error = ENXIO;
15400 				return (NULL);
15401 			}
15402 			/*
15403 			 * The block comment at the start of ipif_down
15404 			 * explains the use of the macros used below
15405 			 */
15406 			if (IPIF_CAN_LOOKUP(ipif)) {
15407 				ipif_refhold_locked(ipif);
15408 				mutex_exit(&ill->ill_lock);
15409 				if (!did_alloc)
15410 					*exists = B_TRUE;
15411 				/*
15412 				 * Drop locks before calling ill_refrele
15413 				 * since it can potentially call into
15414 				 * ipif_ill_refrele_tail which can end up
15415 				 * in trying to acquire any lock.
15416 				 */
15417 				RELEASE_CONN_LOCK(q);
15418 				ill_refrele(ill);
15419 				return (ipif);
15420 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15421 				ipsq = ill->ill_phyint->phyint_ipsq;
15422 				mutex_enter(&ipsq->ipsq_lock);
15423 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15424 				mutex_exit(&ill->ill_lock);
15425 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15426 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15427 				mutex_exit(&ipsq->ipsq_lock);
15428 				RELEASE_CONN_LOCK(q);
15429 				ill_refrele(ill);
15430 				if (error != NULL)
15431 					*error = EINPROGRESS;
15432 				return (NULL);
15433 			}
15434 		}
15435 	}
15436 	RELEASE_CONN_LOCK(q);
15437 
15438 	if (!do_alloc) {
15439 		mutex_exit(&ill->ill_lock);
15440 		ill_refrele(ill);
15441 		if (error != NULL)
15442 			*error = ENXIO;
15443 		return (NULL);
15444 	}
15445 
15446 	/*
15447 	 * If none found, atomically allocate and return a new one.
15448 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15449 	 * to support "receive only" use of lo0:1 etc. as is still done
15450 	 * below as an initial guess.
15451 	 * However, this is now likely to be overriden later in ipif_up_done()
15452 	 * when we know for sure what address has been configured on the
15453 	 * interface, since we might have more than one loopback interface
15454 	 * with a loopback address, e.g. in the case of zones, and all the
15455 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15456 	 */
15457 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15458 		ire_type = IRE_LOOPBACK;
15459 	else
15460 		ire_type = IRE_LOCAL;
15461 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15462 	if (ipif != NULL)
15463 		ipif_refhold_locked(ipif);
15464 	else if (error != NULL)
15465 		*error = ENOMEM;
15466 	mutex_exit(&ill->ill_lock);
15467 	ill_refrele(ill);
15468 	return (ipif);
15469 }
15470 
15471 /*
15472  * This routine is called whenever a new address comes up on an ipif.  If
15473  * we are configured to respond to address mask requests, then we are supposed
15474  * to broadcast an address mask reply at this time.  This routine is also
15475  * called if we are already up, but a netmask change is made.  This is legal
15476  * but might not make the system manager very popular.	(May be called
15477  * as writer.)
15478  */
15479 void
15480 ipif_mask_reply(ipif_t *ipif)
15481 {
15482 	icmph_t	*icmph;
15483 	ipha_t	*ipha;
15484 	mblk_t	*mp;
15485 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15486 
15487 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15488 
15489 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15490 		return;
15491 
15492 	/* ICMP mask reply is IPv4 only */
15493 	ASSERT(!ipif->ipif_isv6);
15494 	/* ICMP mask reply is not for a loopback interface */
15495 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15496 
15497 	mp = allocb(REPLY_LEN, BPRI_HI);
15498 	if (mp == NULL)
15499 		return;
15500 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15501 
15502 	ipha = (ipha_t *)mp->b_rptr;
15503 	bzero(ipha, REPLY_LEN);
15504 	*ipha = icmp_ipha;
15505 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15506 	ipha->ipha_src = ipif->ipif_src_addr;
15507 	ipha->ipha_dst = ipif->ipif_brd_addr;
15508 	ipha->ipha_length = htons(REPLY_LEN);
15509 	ipha->ipha_ident = 0;
15510 
15511 	icmph = (icmph_t *)&ipha[1];
15512 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15513 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15514 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15515 
15516 	put(ipif->ipif_wq, mp);
15517 
15518 #undef	REPLY_LEN
15519 }
15520 
15521 /*
15522  * When the mtu in the ipif changes, we call this routine through ire_walk
15523  * to update all the relevant IREs.
15524  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15525  */
15526 static void
15527 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15528 {
15529 	ipif_t *ipif = (ipif_t *)ipif_arg;
15530 
15531 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15532 		return;
15533 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15534 }
15535 
15536 /*
15537  * When the mtu in the ill changes, we call this routine through ire_walk
15538  * to update all the relevant IREs.
15539  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15540  */
15541 void
15542 ill_mtu_change(ire_t *ire, char *ill_arg)
15543 {
15544 	ill_t	*ill = (ill_t *)ill_arg;
15545 
15546 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15547 		return;
15548 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
15549 }
15550 
15551 /*
15552  * Join the ipif specific multicast groups.
15553  * Must be called after a mapping has been set up in the resolver.  (Always
15554  * called as writer.)
15555  */
15556 void
15557 ipif_multicast_up(ipif_t *ipif)
15558 {
15559 	int err;
15560 	ill_t *ill;
15561 
15562 	ASSERT(IAM_WRITER_IPIF(ipif));
15563 
15564 	ill = ipif->ipif_ill;
15565 
15566 	ip1dbg(("ipif_multicast_up\n"));
15567 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15568 		return;
15569 
15570 	if (ipif->ipif_isv6) {
15571 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15572 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15573 
15574 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15575 
15576 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15577 			return;
15578 
15579 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15580 
15581 		/*
15582 		 * Join the all hosts multicast address.  We skip this for
15583 		 * underlying IPMP interfaces since they should be invisible.
15584 		 */
15585 		if (!IS_UNDER_IPMP(ill)) {
15586 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15587 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15588 			if (err != 0) {
15589 				ip0dbg(("ipif_multicast_up: "
15590 				    "all_hosts_mcast failed %d\n", err));
15591 				return;
15592 			}
15593 			ipif->ipif_joined_allhosts = 1;
15594 		}
15595 
15596 		/*
15597 		 * Enable multicast for the solicited node multicast address
15598 		 */
15599 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15600 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15601 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15602 			if (err != 0) {
15603 				ip0dbg(("ipif_multicast_up: solicited MC"
15604 				    " failed %d\n", err));
15605 				if (ipif->ipif_joined_allhosts) {
15606 					(void) ip_delmulti_v6(&v6allmc, ill,
15607 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15608 					ipif->ipif_joined_allhosts = 0;
15609 				}
15610 				return;
15611 			}
15612 		}
15613 	} else {
15614 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15615 			return;
15616 
15617 		/* Join the all hosts multicast address */
15618 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15619 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15620 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15621 		if (err) {
15622 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15623 			return;
15624 		}
15625 	}
15626 	ipif->ipif_multicast_up = 1;
15627 }
15628 
15629 /*
15630  * Blow away any multicast groups that we joined in ipif_multicast_up().
15631  * (Explicit memberships are blown away in ill_leave_multicast() when the
15632  * ill is brought down.)
15633  */
15634 void
15635 ipif_multicast_down(ipif_t *ipif)
15636 {
15637 	int err;
15638 
15639 	ASSERT(IAM_WRITER_IPIF(ipif));
15640 
15641 	ip1dbg(("ipif_multicast_down\n"));
15642 	if (!ipif->ipif_multicast_up)
15643 		return;
15644 
15645 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15646 
15647 	if (!ipif->ipif_isv6) {
15648 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15649 		    B_TRUE);
15650 		if (err != 0)
15651 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15652 
15653 		ipif->ipif_multicast_up = 0;
15654 		return;
15655 	}
15656 
15657 	/*
15658 	 * Leave the all-hosts multicast address.
15659 	 */
15660 	if (ipif->ipif_joined_allhosts) {
15661 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15662 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15663 		if (err != 0) {
15664 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15665 			    "failed %d\n", err));
15666 		}
15667 		ipif->ipif_joined_allhosts = 0;
15668 	}
15669 
15670 	/*
15671 	 * Disable multicast for the solicited node multicast address
15672 	 */
15673 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15674 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15675 
15676 		ipv6_multi.s6_addr32[3] |=
15677 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15678 
15679 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15680 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15681 		if (err != 0) {
15682 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15683 			    err));
15684 		}
15685 	}
15686 
15687 	ipif->ipif_multicast_up = 0;
15688 }
15689 
15690 /*
15691  * Used when an interface comes up to recreate any extra routes on this
15692  * interface.
15693  */
15694 static ire_t **
15695 ipif_recover_ire(ipif_t *ipif)
15696 {
15697 	mblk_t	*mp;
15698 	ire_t	**ipif_saved_irep;
15699 	ire_t	**irep;
15700 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15701 
15702 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15703 	    ipif->ipif_id));
15704 
15705 	mutex_enter(&ipif->ipif_saved_ire_lock);
15706 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15707 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15708 	if (ipif_saved_irep == NULL) {
15709 		mutex_exit(&ipif->ipif_saved_ire_lock);
15710 		return (NULL);
15711 	}
15712 
15713 	irep = ipif_saved_irep;
15714 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15715 		ire_t		*ire;
15716 		queue_t		*rfq;
15717 		queue_t		*stq;
15718 		ifrt_t		*ifrt;
15719 		uchar_t		*src_addr;
15720 		uchar_t		*gateway_addr;
15721 		ushort_t	type;
15722 
15723 		/*
15724 		 * When the ire was initially created and then added in
15725 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15726 		 * in the case of a traditional interface route, or as one of
15727 		 * the IRE_OFFSUBNET types (with the exception of
15728 		 * IRE_HOST types ire which is created by icmp_redirect() and
15729 		 * which we don't need to save or recover).  In the case where
15730 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15731 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15732 		 * to satisfy software like GateD and Sun Cluster which creates
15733 		 * routes using the the loopback interface's address as a
15734 		 * gateway.
15735 		 *
15736 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15737 		 * ire_create() will be called in the same way here as
15738 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15739 		 * the route looks like a traditional interface route (where
15740 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15741 		 * the saved ifrt->ifrt_type.  This means that in the case where
15742 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15743 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15744 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15745 		 */
15746 		ifrt = (ifrt_t *)mp->b_rptr;
15747 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15748 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15749 			rfq = NULL;
15750 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15751 			    ? ipif->ipif_rq : ipif->ipif_wq;
15752 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15753 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15754 			    : (uint8_t *)&ipif->ipif_src_addr;
15755 			gateway_addr = NULL;
15756 			type = ipif->ipif_net_type;
15757 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15758 			/* Recover multiroute broadcast IRE. */
15759 			rfq = ipif->ipif_rq;
15760 			stq = ipif->ipif_wq;
15761 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15762 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15763 			    : (uint8_t *)&ipif->ipif_src_addr;
15764 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15765 			type = ifrt->ifrt_type;
15766 		} else {
15767 			rfq = NULL;
15768 			stq = NULL;
15769 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15770 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15771 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15772 			type = ifrt->ifrt_type;
15773 		}
15774 
15775 		/*
15776 		 * Create a copy of the IRE with the saved address and netmask.
15777 		 */
15778 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15779 		    "0x%x/0x%x\n",
15780 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15781 		    ntohl(ifrt->ifrt_addr),
15782 		    ntohl(ifrt->ifrt_mask)));
15783 		ire = ire_create(
15784 		    (uint8_t *)&ifrt->ifrt_addr,
15785 		    (uint8_t *)&ifrt->ifrt_mask,
15786 		    src_addr,
15787 		    gateway_addr,
15788 		    &ifrt->ifrt_max_frag,
15789 		    NULL,
15790 		    rfq,
15791 		    stq,
15792 		    type,
15793 		    ipif,
15794 		    0,
15795 		    0,
15796 		    0,
15797 		    ifrt->ifrt_flags,
15798 		    &ifrt->ifrt_iulp_info,
15799 		    NULL,
15800 		    NULL,
15801 		    ipst);
15802 
15803 		if (ire == NULL) {
15804 			mutex_exit(&ipif->ipif_saved_ire_lock);
15805 			kmem_free(ipif_saved_irep,
15806 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15807 			return (NULL);
15808 		}
15809 
15810 		/*
15811 		 * Some software (for example, GateD and Sun Cluster) attempts
15812 		 * to create (what amount to) IRE_PREFIX routes with the
15813 		 * loopback address as the gateway.  This is primarily done to
15814 		 * set up prefixes with the RTF_REJECT flag set (for example,
15815 		 * when generating aggregate routes.)
15816 		 *
15817 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15818 		 * IRE_LOOPBACK, then we map the request into a
15819 		 * IRE_IF_NORESOLVER.
15820 		 */
15821 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15822 			ire->ire_type = IRE_IF_NORESOLVER;
15823 		/*
15824 		 * ire held by ire_add, will be refreled' towards the
15825 		 * the end of ipif_up_done
15826 		 */
15827 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15828 		*irep = ire;
15829 		irep++;
15830 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15831 	}
15832 	mutex_exit(&ipif->ipif_saved_ire_lock);
15833 	return (ipif_saved_irep);
15834 }
15835 
15836 /*
15837  * Used to set the netmask and broadcast address to default values when the
15838  * interface is brought up.  (Always called as writer.)
15839  */
15840 static void
15841 ipif_set_default(ipif_t *ipif)
15842 {
15843 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15844 
15845 	if (!ipif->ipif_isv6) {
15846 		/*
15847 		 * Interface holds an IPv4 address. Default
15848 		 * mask is the natural netmask.
15849 		 */
15850 		if (!ipif->ipif_net_mask) {
15851 			ipaddr_t	v4mask;
15852 
15853 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15854 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15855 		}
15856 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15857 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15858 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15859 		} else {
15860 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15861 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15862 		}
15863 		/*
15864 		 * NOTE: SunOS 4.X does this even if the broadcast address
15865 		 * has been already set thus we do the same here.
15866 		 */
15867 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15868 			ipaddr_t	v4addr;
15869 
15870 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15871 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15872 		}
15873 	} else {
15874 		/*
15875 		 * Interface holds an IPv6-only address.  Default
15876 		 * mask is all-ones.
15877 		 */
15878 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15879 			ipif->ipif_v6net_mask = ipv6_all_ones;
15880 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15881 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15882 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15883 		} else {
15884 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15885 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15886 		}
15887 	}
15888 }
15889 
15890 /*
15891  * Return 0 if this address can be used as local address without causing
15892  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15893  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15894  * Note that the same IPv6 link-local address is allowed as long as the ills
15895  * are not on the same link.
15896  */
15897 int
15898 ip_addr_availability_check(ipif_t *new_ipif)
15899 {
15900 	in6_addr_t our_v6addr;
15901 	ill_t *ill;
15902 	ipif_t *ipif;
15903 	ill_walk_context_t ctx;
15904 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15905 
15906 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15907 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15908 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15909 
15910 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15911 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15912 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15913 		return (0);
15914 
15915 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15916 
15917 	if (new_ipif->ipif_isv6)
15918 		ill = ILL_START_WALK_V6(&ctx, ipst);
15919 	else
15920 		ill = ILL_START_WALK_V4(&ctx, ipst);
15921 
15922 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15923 		for (ipif = ill->ill_ipif; ipif != NULL;
15924 		    ipif = ipif->ipif_next) {
15925 			if ((ipif == new_ipif) ||
15926 			    !(ipif->ipif_flags & IPIF_UP) ||
15927 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15928 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15929 			    &our_v6addr))
15930 				continue;
15931 
15932 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15933 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15934 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15935 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15936 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15937 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15938 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15939 				continue;
15940 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15941 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15942 				continue;
15943 			else if (new_ipif->ipif_ill == ill)
15944 				return (EADDRINUSE);
15945 			else
15946 				return (EADDRNOTAVAIL);
15947 		}
15948 	}
15949 
15950 	return (0);
15951 }
15952 
15953 /*
15954  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15955  * IREs for the ipif.
15956  * When the routine returns EINPROGRESS then mp has been consumed and
15957  * the ioctl will be acked from ip_rput_dlpi.
15958  */
15959 int
15960 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15961 {
15962 	ill_t		*ill = ipif->ipif_ill;
15963 	boolean_t 	isv6 = ipif->ipif_isv6;
15964 	int		err = 0;
15965 	boolean_t	success;
15966 	uint_t		ipif_orig_id;
15967 	ip_stack_t	*ipst = ill->ill_ipst;
15968 
15969 	ASSERT(IAM_WRITER_IPIF(ipif));
15970 
15971 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15972 
15973 	/* Shouldn't get here if it is already up. */
15974 	if (ipif->ipif_flags & IPIF_UP)
15975 		return (EALREADY);
15976 
15977 	/*
15978 	 * If this is a request to bring up a data address on an interface
15979 	 * under IPMP, then move the address to its IPMP meta-interface and
15980 	 * try to bring it up.  One complication is that the zeroth ipif for
15981 	 * an ill is special, in that every ill always has one, and that code
15982 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15983 	 */
15984 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15985 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15986 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15987 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15988 
15989 		/*
15990 		 * The ipif being brought up should be quiesced.  If it's not,
15991 		 * something has gone amiss and we need to bail out.  (If it's
15992 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15993 		 */
15994 		mutex_enter(&ill->ill_lock);
15995 		if (!ipif_is_quiescent(ipif)) {
15996 			mutex_exit(&ill->ill_lock);
15997 			return (EINVAL);
15998 		}
15999 		mutex_exit(&ill->ill_lock);
16000 
16001 		/*
16002 		 * If we're going to need to allocate ipifs, do it prior
16003 		 * to starting the move (and grabbing locks).
16004 		 */
16005 		if (ipif->ipif_id == 0) {
16006 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
16007 			    B_FALSE);
16008 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
16009 			    B_FALSE);
16010 			if (moveipif == NULL || stubipif == NULL) {
16011 				mi_free(moveipif);
16012 				mi_free(stubipif);
16013 				return (ENOMEM);
16014 			}
16015 		}
16016 
16017 		/*
16018 		 * Grab or transfer the ipif to move.  During the move, keep
16019 		 * ill_g_lock held to prevent any ill walker threads from
16020 		 * seeing things in an inconsistent state.
16021 		 */
16022 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16023 		if (ipif->ipif_id != 0) {
16024 			ipif_remove(ipif);
16025 		} else {
16026 			ipif_transfer(ipif, moveipif, stubipif);
16027 			ipif = moveipif;
16028 		}
16029 
16030 		/*
16031 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
16032 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
16033 		 * replace that one.  Otherwise, pick the next available slot.
16034 		 */
16035 		ipif->ipif_ill = ipmp_ill;
16036 		ipif_orig_id = ipif->ipif_id;
16037 
16038 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
16039 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
16040 			ipif = ipmp_ill->ill_ipif;
16041 		} else {
16042 			ipif->ipif_id = -1;
16043 			if (ipif_insert(ipif, B_FALSE) != 0) {
16044 				/*
16045 				 * No more available ipif_id's -- put it back
16046 				 * on the original ill and fail the operation.
16047 				 * Since we're writer on the ill, we can be
16048 				 * sure our old slot is still available.
16049 				 */
16050 				ipif->ipif_id = ipif_orig_id;
16051 				ipif->ipif_ill = ill;
16052 				if (ipif_orig_id == 0) {
16053 					ipif_transfer(ipif, ill->ill_ipif,
16054 					    NULL);
16055 				} else {
16056 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
16057 				}
16058 				rw_exit(&ipst->ips_ill_g_lock);
16059 				return (ENOMEM);
16060 			}
16061 		}
16062 		rw_exit(&ipst->ips_ill_g_lock);
16063 
16064 		/*
16065 		 * Tell SCTP that the ipif has moved.  Note that even if we
16066 		 * had to allocate a new ipif, the original sequence id was
16067 		 * preserved and therefore SCTP won't know.
16068 		 */
16069 		sctp_move_ipif(ipif, ill, ipmp_ill);
16070 
16071 		/*
16072 		 * If the ipif being brought up was on slot zero, then we
16073 		 * first need to bring up the placeholder we stuck there.  In
16074 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
16075 		 * to ipif_up() itself, if we successfully bring up the
16076 		 * placeholder, we'll check ill_move_ipif and bring it up too.
16077 		 */
16078 		if (ipif_orig_id == 0) {
16079 			ASSERT(ill->ill_move_ipif == NULL);
16080 			ill->ill_move_ipif = ipif;
16081 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
16082 				ASSERT(ill->ill_move_ipif == NULL);
16083 			if (err != EINPROGRESS)
16084 				ill->ill_move_ipif = NULL;
16085 			return (err);
16086 		}
16087 
16088 		/*
16089 		 * Bring it up on the IPMP ill.
16090 		 */
16091 		return (ipif_up(ipif, q, mp));
16092 	}
16093 
16094 	/* Skip arp/ndp for any loopback interface. */
16095 	if (ill->ill_wq != NULL) {
16096 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16097 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
16098 
16099 		if (!ill->ill_dl_up) {
16100 			/*
16101 			 * ill_dl_up is not yet set. i.e. we are yet to
16102 			 * DL_BIND with the driver and this is the first
16103 			 * logical interface on the ill to become "up".
16104 			 * Tell the driver to get going (via DL_BIND_REQ).
16105 			 * Note that changing "significant" IFF_ flags
16106 			 * address/netmask etc cause a down/up dance, but
16107 			 * does not cause an unbind (DL_UNBIND) with the driver
16108 			 */
16109 			return (ill_dl_up(ill, ipif, mp, q));
16110 		}
16111 
16112 		/*
16113 		 * ipif_resolver_up may end up sending an
16114 		 * AR_INTERFACE_UP message to ARP, which would, in
16115 		 * turn send a DLPI message to the driver. ioctls are
16116 		 * serialized and so we cannot send more than one
16117 		 * interface up message at a time. If ipif_resolver_up
16118 		 * does send an interface up message to ARP, we get
16119 		 * EINPROGRESS and we will complete in ip_arp_done.
16120 		 */
16121 
16122 		ASSERT(connp != NULL || !CONN_Q(q));
16123 		if (connp != NULL)
16124 			mutex_enter(&connp->conn_lock);
16125 		mutex_enter(&ill->ill_lock);
16126 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16127 		mutex_exit(&ill->ill_lock);
16128 		if (connp != NULL)
16129 			mutex_exit(&connp->conn_lock);
16130 		if (!success)
16131 			return (EINTR);
16132 
16133 		/*
16134 		 * Crank up the resolver.  For IPv6, this cranks up the
16135 		 * external resolver if one is configured, but even if an
16136 		 * external resolver isn't configured, it must be called to
16137 		 * reset DAD state.  For IPv6, if an external resolver is not
16138 		 * being used, ipif_resolver_up() will never return
16139 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
16140 		 * Note that if an external resolver is being used, there's no
16141 		 * need to call ipif_ndp_up() since it will do nothing.
16142 		 */
16143 		err = ipif_resolver_up(ipif, Res_act_initial);
16144 		if (err == EINPROGRESS) {
16145 			/* We will complete it in ip_arp_done() */
16146 			return (err);
16147 		}
16148 
16149 		if (isv6 && err == 0)
16150 			err = ipif_ndp_up(ipif, B_TRUE);
16151 
16152 		ASSERT(err != EINPROGRESS);
16153 		mp = ipsq_pending_mp_get(ipsq, &connp);
16154 		ASSERT(mp != NULL);
16155 		if (err != 0)
16156 			return (err);
16157 	} else {
16158 		/*
16159 		 * Interfaces without underlying hardware don't do duplicate
16160 		 * address detection.
16161 		 */
16162 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16163 		ipif->ipif_addr_ready = 1;
16164 	}
16165 
16166 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16167 	if (err == 0 && ill->ill_move_ipif != NULL) {
16168 		ipif = ill->ill_move_ipif;
16169 		ill->ill_move_ipif = NULL;
16170 		return (ipif_up(ipif, q, mp));
16171 	}
16172 	return (err);
16173 }
16174 
16175 /*
16176  * Perform a bind for the physical device.
16177  * When the routine returns EINPROGRESS then mp has been consumed and
16178  * the ioctl will be acked from ip_rput_dlpi.
16179  * Allocate an unbind message and save it until ipif_down.
16180  */
16181 static int
16182 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16183 {
16184 	areq_t	*areq;
16185 	mblk_t	*areq_mp = NULL;
16186 	mblk_t	*bind_mp = NULL;
16187 	mblk_t	*unbind_mp = NULL;
16188 	conn_t	*connp;
16189 	boolean_t success;
16190 	uint16_t sap_addr;
16191 
16192 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16193 	ASSERT(IAM_WRITER_ILL(ill));
16194 	ASSERT(mp != NULL);
16195 
16196 	/* Create a resolver cookie for ARP */
16197 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16198 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16199 		if (areq_mp == NULL)
16200 			return (ENOMEM);
16201 
16202 		freemsg(ill->ill_resolver_mp);
16203 		ill->ill_resolver_mp = areq_mp;
16204 		areq = (areq_t *)areq_mp->b_rptr;
16205 		sap_addr = ill->ill_sap;
16206 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16207 	}
16208 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16209 	    DL_BIND_REQ);
16210 	if (bind_mp == NULL)
16211 		goto bad;
16212 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16213 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16214 
16215 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16216 	if (unbind_mp == NULL)
16217 		goto bad;
16218 
16219 	/*
16220 	 * Record state needed to complete this operation when the
16221 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16222 	 */
16223 	ASSERT(WR(q)->q_next == NULL);
16224 	connp = Q_TO_CONN(q);
16225 
16226 	mutex_enter(&connp->conn_lock);
16227 	mutex_enter(&ipif->ipif_ill->ill_lock);
16228 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16229 	mutex_exit(&ipif->ipif_ill->ill_lock);
16230 	mutex_exit(&connp->conn_lock);
16231 	if (!success)
16232 		goto bad;
16233 
16234 	/*
16235 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16236 	 * the interface goes down.
16237 	 */
16238 	ASSERT(ill->ill_unbind_mp == NULL);
16239 	ill->ill_unbind_mp = unbind_mp;
16240 
16241 	ill_dlpi_send(ill, bind_mp);
16242 	/* Send down link-layer capabilities probe if not already done. */
16243 	ill_capability_probe(ill);
16244 
16245 	/*
16246 	 * Sysid used to rely on the fact that netboots set domainname
16247 	 * and the like. Now that miniroot boots aren't strictly netboots
16248 	 * and miniroot network configuration is driven from userland
16249 	 * these things still need to be set. This situation can be detected
16250 	 * by comparing the interface being configured here to the one
16251 	 * dhcifname was set to reference by the boot loader. Once sysid is
16252 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16253 	 */
16254 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16255 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16256 	    (strlen(srpc_domain) == 0)) {
16257 		if (dhcpinit() != 0)
16258 			cmn_err(CE_WARN, "no cached dhcp response");
16259 	}
16260 
16261 	/*
16262 	 * This operation will complete in ip_rput_dlpi with either
16263 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16264 	 */
16265 	return (EINPROGRESS);
16266 bad:
16267 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16268 
16269 	freemsg(bind_mp);
16270 	freemsg(unbind_mp);
16271 	return (ENOMEM);
16272 }
16273 
16274 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16275 
16276 /*
16277  * DLPI and ARP is up.
16278  * Create all the IREs associated with an interface bring up multicast.
16279  * Set the interface flag and finish other initialization
16280  * that potentially had to be differed to after DL_BIND_ACK.
16281  */
16282 int
16283 ipif_up_done(ipif_t *ipif)
16284 {
16285 	ire_t	*ire_array[20];
16286 	ire_t	**irep = ire_array;
16287 	ire_t	**irep1;
16288 	ipaddr_t net_mask = 0;
16289 	ipaddr_t subnet_mask, route_mask;
16290 	ill_t	*ill = ipif->ipif_ill;
16291 	queue_t	*stq;
16292 	ipif_t	 *src_ipif;
16293 	ipif_t   *tmp_ipif;
16294 	boolean_t	flush_ire_cache = B_TRUE;
16295 	int	err = 0;
16296 	ire_t	**ipif_saved_irep = NULL;
16297 	int ipif_saved_ire_cnt;
16298 	int	cnt;
16299 	boolean_t	src_ipif_held = B_FALSE;
16300 	boolean_t	loopback = B_FALSE;
16301 	ip_stack_t	*ipst = ill->ill_ipst;
16302 
16303 	ip1dbg(("ipif_up_done(%s:%u)\n",
16304 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16305 	/* Check if this is a loopback interface */
16306 	if (ipif->ipif_ill->ill_wq == NULL)
16307 		loopback = B_TRUE;
16308 
16309 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16310 	/*
16311 	 * If all other interfaces for this ill are down or DEPRECATED,
16312 	 * or otherwise unsuitable for source address selection, remove
16313 	 * any IRE_CACHE entries for this ill to make sure source
16314 	 * address selection gets to take this new ipif into account.
16315 	 * No need to hold ill_lock while traversing the ipif list since
16316 	 * we are writer
16317 	 */
16318 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16319 	    tmp_ipif = tmp_ipif->ipif_next) {
16320 		if (((tmp_ipif->ipif_flags &
16321 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16322 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16323 		    (tmp_ipif == ipif))
16324 			continue;
16325 		/* first useable pre-existing interface */
16326 		flush_ire_cache = B_FALSE;
16327 		break;
16328 	}
16329 	if (flush_ire_cache)
16330 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16331 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16332 
16333 	/*
16334 	 * Figure out which way the send-to queue should go.  Only
16335 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16336 	 * should show up here.
16337 	 */
16338 	switch (ill->ill_net_type) {
16339 	case IRE_IF_RESOLVER:
16340 		stq = ill->ill_rq;
16341 		break;
16342 	case IRE_IF_NORESOLVER:
16343 	case IRE_LOOPBACK:
16344 		stq = ill->ill_wq;
16345 		break;
16346 	default:
16347 		return (EINVAL);
16348 	}
16349 
16350 	if (IS_LOOPBACK(ill)) {
16351 		/*
16352 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16353 		 * ipif_lookup_on_name(), but in the case of zones we can have
16354 		 * several loopback addresses on lo0. So all the interfaces with
16355 		 * loopback addresses need to be marked IRE_LOOPBACK.
16356 		 */
16357 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16358 		    htonl(INADDR_LOOPBACK))
16359 			ipif->ipif_ire_type = IRE_LOOPBACK;
16360 		else
16361 			ipif->ipif_ire_type = IRE_LOCAL;
16362 	}
16363 
16364 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16365 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16366 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16367 		/*
16368 		 * Can't use our source address. Select a different
16369 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16370 		 */
16371 		src_ipif = ipif_select_source(ipif->ipif_ill,
16372 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16373 		if (src_ipif == NULL)
16374 			src_ipif = ipif;	/* Last resort */
16375 		else
16376 			src_ipif_held = B_TRUE;
16377 	} else {
16378 		src_ipif = ipif;
16379 	}
16380 
16381 	/* Create all the IREs associated with this interface */
16382 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16383 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16384 
16385 		/*
16386 		 * If we're on a labeled system then make sure that zone-
16387 		 * private addresses have proper remote host database entries.
16388 		 */
16389 		if (is_system_labeled() &&
16390 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16391 		    !tsol_check_interface_address(ipif))
16392 			return (EINVAL);
16393 
16394 		/* Register the source address for __sin6_src_id */
16395 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16396 		    ipif->ipif_zoneid, ipst);
16397 		if (err != 0) {
16398 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16399 			return (err);
16400 		}
16401 
16402 		/* If the interface address is set, create the local IRE. */
16403 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16404 		    (void *)ipif,
16405 		    ipif->ipif_ire_type,
16406 		    ntohl(ipif->ipif_lcl_addr)));
16407 		*irep++ = ire_create(
16408 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16409 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16410 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16411 		    NULL,				/* no gateway */
16412 		    &ip_loopback_mtuplus,		/* max frag size */
16413 		    NULL,
16414 		    ipif->ipif_rq,			/* recv-from queue */
16415 		    NULL,				/* no send-to queue */
16416 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16417 		    ipif,
16418 		    0,
16419 		    0,
16420 		    0,
16421 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16422 		    RTF_PRIVATE : 0,
16423 		    &ire_uinfo_null,
16424 		    NULL,
16425 		    NULL,
16426 		    ipst);
16427 	} else {
16428 		ip1dbg((
16429 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16430 		    ipif->ipif_ire_type,
16431 		    ntohl(ipif->ipif_lcl_addr),
16432 		    (uint_t)ipif->ipif_flags));
16433 	}
16434 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16435 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16436 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16437 	} else {
16438 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16439 	}
16440 
16441 	subnet_mask = ipif->ipif_net_mask;
16442 
16443 	/*
16444 	 * If mask was not specified, use natural netmask of
16445 	 * interface address. Also, store this mask back into the
16446 	 * ipif struct.
16447 	 */
16448 	if (subnet_mask == 0) {
16449 		subnet_mask = net_mask;
16450 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16451 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16452 		    ipif->ipif_v6subnet);
16453 	}
16454 
16455 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16456 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16457 	    ipif->ipif_subnet != INADDR_ANY) {
16458 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16459 
16460 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16461 			route_mask = IP_HOST_MASK;
16462 		} else {
16463 			route_mask = subnet_mask;
16464 		}
16465 
16466 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16467 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16468 		    (void *)ipif, (void *)ill,
16469 		    ill->ill_net_type,
16470 		    ntohl(ipif->ipif_subnet)));
16471 		*irep++ = ire_create(
16472 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16473 		    (uchar_t *)&route_mask,		/* mask */
16474 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16475 		    NULL,				/* no gateway */
16476 		    &ipif->ipif_mtu,			/* max frag */
16477 		    NULL,
16478 		    NULL,				/* no recv queue */
16479 		    stq,				/* send-to queue */
16480 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16481 		    ipif,
16482 		    0,
16483 		    0,
16484 		    0,
16485 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16486 		    &ire_uinfo_null,
16487 		    NULL,
16488 		    NULL,
16489 		    ipst);
16490 	}
16491 
16492 	/*
16493 	 * Create any necessary broadcast IREs.
16494 	 */
16495 	if (ipif->ipif_flags & IPIF_BROADCAST)
16496 		irep = ipif_create_bcast_ires(ipif, irep);
16497 
16498 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16499 
16500 	/* If an earlier ire_create failed, get out now */
16501 	for (irep1 = irep; irep1 > ire_array; ) {
16502 		irep1--;
16503 		if (*irep1 == NULL) {
16504 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16505 			err = ENOMEM;
16506 			goto bad;
16507 		}
16508 	}
16509 
16510 	/*
16511 	 * Need to atomically check for IP address availability under
16512 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16513 	 * ills or new ipifs can be added while we are checking availability.
16514 	 */
16515 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16516 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16517 	/* Mark it up, and increment counters. */
16518 	ipif->ipif_flags |= IPIF_UP;
16519 	ill->ill_ipif_up_count++;
16520 	err = ip_addr_availability_check(ipif);
16521 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16522 	rw_exit(&ipst->ips_ill_g_lock);
16523 
16524 	if (err != 0) {
16525 		/*
16526 		 * Our address may already be up on the same ill. In this case,
16527 		 * the ARP entry for our ipif replaced the one for the other
16528 		 * ipif. So we don't want to delete it (otherwise the other ipif
16529 		 * would be unable to send packets).
16530 		 * ip_addr_availability_check() identifies this case for us and
16531 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16532 		 * which is the expected error code.
16533 		 */
16534 		if (err == EADDRINUSE) {
16535 			freemsg(ipif->ipif_arp_del_mp);
16536 			ipif->ipif_arp_del_mp = NULL;
16537 			err = EADDRNOTAVAIL;
16538 		}
16539 		ill->ill_ipif_up_count--;
16540 		ipif->ipif_flags &= ~IPIF_UP;
16541 		goto bad;
16542 	}
16543 
16544 	/*
16545 	 * Add in all newly created IREs.  ire_create_bcast() has
16546 	 * already checked for duplicates of the IRE_BROADCAST type.
16547 	 */
16548 	for (irep1 = irep; irep1 > ire_array; ) {
16549 		irep1--;
16550 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16551 		/*
16552 		 * refheld by ire_add. refele towards the end of the func
16553 		 */
16554 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16555 	}
16556 
16557 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16558 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16559 	ipif_saved_irep = ipif_recover_ire(ipif);
16560 
16561 	if (!loopback) {
16562 		/*
16563 		 * If the broadcast address has been set, make sure it makes
16564 		 * sense based on the interface address.
16565 		 * Only match on ill since we are sharing broadcast addresses.
16566 		 */
16567 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16568 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16569 			ire_t	*ire;
16570 
16571 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16572 			    IRE_BROADCAST, ipif, ALL_ZONES,
16573 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16574 
16575 			if (ire == NULL) {
16576 				/*
16577 				 * If there isn't a matching broadcast IRE,
16578 				 * revert to the default for this netmask.
16579 				 */
16580 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16581 				mutex_enter(&ipif->ipif_ill->ill_lock);
16582 				ipif_set_default(ipif);
16583 				mutex_exit(&ipif->ipif_ill->ill_lock);
16584 			} else {
16585 				ire_refrele(ire);
16586 			}
16587 		}
16588 
16589 	}
16590 
16591 	if (ill->ill_need_recover_multicast) {
16592 		/*
16593 		 * Need to recover all multicast memberships in the driver.
16594 		 * This had to be deferred until we had attached.  The same
16595 		 * code exists in ipif_up_done_v6() to recover IPv6
16596 		 * memberships.
16597 		 *
16598 		 * Note that it would be preferable to unconditionally do the
16599 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16600 		 * that since ill_join_allmulti() depends on ill_dl_up being
16601 		 * set, and it is not set until we receive a DL_BIND_ACK after
16602 		 * having called ill_dl_up().
16603 		 */
16604 		ill_recover_multicast(ill);
16605 	}
16606 
16607 	if (ill->ill_ipif_up_count == 1) {
16608 		/*
16609 		 * Since the interface is now up, it may now be active.
16610 		 */
16611 		if (IS_UNDER_IPMP(ill))
16612 			ipmp_ill_refresh_active(ill);
16613 
16614 		/*
16615 		 * If this is an IPMP interface, we may now be able to
16616 		 * establish ARP entries.
16617 		 */
16618 		if (IS_IPMP(ill))
16619 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16620 	}
16621 
16622 	/* Join the allhosts multicast address */
16623 	ipif_multicast_up(ipif);
16624 
16625 	/*
16626 	 * See if anybody else would benefit from our new ipif.
16627 	 */
16628 	if (!loopback &&
16629 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16630 		ill_update_source_selection(ill);
16631 	}
16632 
16633 	for (irep1 = irep; irep1 > ire_array; ) {
16634 		irep1--;
16635 		if (*irep1 != NULL) {
16636 			/* was held in ire_add */
16637 			ire_refrele(*irep1);
16638 		}
16639 	}
16640 
16641 	cnt = ipif_saved_ire_cnt;
16642 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16643 		if (*irep1 != NULL) {
16644 			/* was held in ire_add */
16645 			ire_refrele(*irep1);
16646 		}
16647 	}
16648 
16649 	if (!loopback && ipif->ipif_addr_ready) {
16650 		/* Broadcast an address mask reply. */
16651 		ipif_mask_reply(ipif);
16652 	}
16653 	if (ipif_saved_irep != NULL) {
16654 		kmem_free(ipif_saved_irep,
16655 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16656 	}
16657 	if (src_ipif_held)
16658 		ipif_refrele(src_ipif);
16659 
16660 	/*
16661 	 * This had to be deferred until we had bound.  Tell routing sockets and
16662 	 * others that this interface is up if it looks like the address has
16663 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16664 	 * duplicate address detection to do its thing.
16665 	 */
16666 	if (ipif->ipif_addr_ready)
16667 		ipif_up_notify(ipif);
16668 	return (0);
16669 
16670 bad:
16671 	ip1dbg(("ipif_up_done: FAILED \n"));
16672 
16673 	while (irep > ire_array) {
16674 		irep--;
16675 		if (*irep != NULL)
16676 			ire_delete(*irep);
16677 	}
16678 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16679 
16680 	if (ipif_saved_irep != NULL) {
16681 		kmem_free(ipif_saved_irep,
16682 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16683 	}
16684 	if (src_ipif_held)
16685 		ipif_refrele(src_ipif);
16686 
16687 	ipif_resolver_down(ipif);
16688 	return (err);
16689 }
16690 
16691 /*
16692  * Turn off the ARP with the ILLF_NOARP flag.
16693  */
16694 static int
16695 ill_arp_off(ill_t *ill)
16696 {
16697 	mblk_t	*arp_off_mp = NULL;
16698 	mblk_t	*arp_on_mp = NULL;
16699 
16700 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16701 
16702 	ASSERT(IAM_WRITER_ILL(ill));
16703 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16704 
16705 	/*
16706 	 * If the on message is still around we've already done
16707 	 * an arp_off without doing an arp_on thus there is no
16708 	 * work needed.
16709 	 */
16710 	if (ill->ill_arp_on_mp != NULL)
16711 		return (0);
16712 
16713 	/*
16714 	 * Allocate an ARP on message (to be saved) and an ARP off message
16715 	 */
16716 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16717 	if (!arp_off_mp)
16718 		return (ENOMEM);
16719 
16720 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16721 	if (!arp_on_mp)
16722 		goto failed;
16723 
16724 	ASSERT(ill->ill_arp_on_mp == NULL);
16725 	ill->ill_arp_on_mp = arp_on_mp;
16726 
16727 	/* Send an AR_INTERFACE_OFF request */
16728 	putnext(ill->ill_rq, arp_off_mp);
16729 	return (0);
16730 failed:
16731 
16732 	if (arp_off_mp)
16733 		freemsg(arp_off_mp);
16734 	return (ENOMEM);
16735 }
16736 
16737 /*
16738  * Turn on ARP by turning off the ILLF_NOARP flag.
16739  */
16740 static int
16741 ill_arp_on(ill_t *ill)
16742 {
16743 	mblk_t	*mp;
16744 
16745 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16746 
16747 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16748 
16749 	ASSERT(IAM_WRITER_ILL(ill));
16750 	/*
16751 	 * Send an AR_INTERFACE_ON request if we have already done
16752 	 * an arp_off (which allocated the message).
16753 	 */
16754 	if (ill->ill_arp_on_mp != NULL) {
16755 		mp = ill->ill_arp_on_mp;
16756 		ill->ill_arp_on_mp = NULL;
16757 		putnext(ill->ill_rq, mp);
16758 	}
16759 	return (0);
16760 }
16761 
16762 /*
16763  * Checks for availbility of a usable source address (if there is one) when the
16764  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16765  * this selection is done regardless of the destination.
16766  */
16767 boolean_t
16768 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16769 {
16770 	uint_t	ifindex;
16771 	ipif_t	*ipif = NULL;
16772 	ill_t	*uill;
16773 	boolean_t isv6;
16774 	ip_stack_t	*ipst = ill->ill_ipst;
16775 
16776 	ASSERT(ill != NULL);
16777 
16778 	isv6 = ill->ill_isv6;
16779 	ifindex = ill->ill_usesrc_ifindex;
16780 	if (ifindex != 0) {
16781 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16782 		    NULL, ipst);
16783 		if (uill == NULL)
16784 			return (NULL);
16785 		mutex_enter(&uill->ill_lock);
16786 		for (ipif = uill->ill_ipif; ipif != NULL;
16787 		    ipif = ipif->ipif_next) {
16788 			if (!IPIF_CAN_LOOKUP(ipif))
16789 				continue;
16790 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16791 				continue;
16792 			if (!(ipif->ipif_flags & IPIF_UP))
16793 				continue;
16794 			if (ipif->ipif_zoneid != zoneid)
16795 				continue;
16796 			if ((isv6 &&
16797 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16798 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16799 				continue;
16800 			mutex_exit(&uill->ill_lock);
16801 			ill_refrele(uill);
16802 			return (B_TRUE);
16803 		}
16804 		mutex_exit(&uill->ill_lock);
16805 		ill_refrele(uill);
16806 	}
16807 	return (B_FALSE);
16808 }
16809 
16810 /*
16811  * IP source address type, sorted from worst to best.  For a given type,
16812  * always prefer IP addresses on the same subnet.  All-zones addresses are
16813  * suboptimal because they pose problems with unlabeled destinations.
16814  */
16815 typedef enum {
16816 	IPIF_NONE,
16817 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16818 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16819 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16820 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16821 	IPIF_DIFFNET,			/* normal and different subnet */
16822 	IPIF_SAMENET			/* normal and same subnet */
16823 } ipif_type_t;
16824 
16825 /*
16826  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16827  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16828  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16829  * the first one, unless IPMP is used in which case we round-robin among them;
16830  * see below for more.
16831  *
16832  * Returns NULL if there is no suitable source address for the ill.
16833  * This only occurs when there is no valid source address for the ill.
16834  */
16835 ipif_t *
16836 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16837 {
16838 	ill_t	*usill = NULL;
16839 	ill_t	*ipmp_ill = NULL;
16840 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16841 	ipif_type_t type, best_type;
16842 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16843 	ip_stack_t *ipst = ill->ill_ipst;
16844 	boolean_t samenet;
16845 
16846 	if (ill->ill_usesrc_ifindex != 0) {
16847 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16848 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16849 		if (usill != NULL)
16850 			ill = usill;	/* Select source from usesrc ILL */
16851 		else
16852 			return (NULL);
16853 	}
16854 
16855 	/*
16856 	 * Test addresses should never be used for source address selection,
16857 	 * so if we were passed one, switch to the IPMP meta-interface.
16858 	 */
16859 	if (IS_UNDER_IPMP(ill)) {
16860 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16861 			ill = ipmp_ill;	/* Select source from IPMP ill */
16862 		else
16863 			return (NULL);
16864 	}
16865 
16866 	/*
16867 	 * If we're dealing with an unlabeled destination on a labeled system,
16868 	 * make sure that we ignore source addresses that are incompatible with
16869 	 * the destination's default label.  That destination's default label
16870 	 * must dominate the minimum label on the source address.
16871 	 */
16872 	dst_rhtp = NULL;
16873 	if (is_system_labeled()) {
16874 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16875 		if (dst_rhtp == NULL)
16876 			return (NULL);
16877 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16878 			TPC_RELE(dst_rhtp);
16879 			dst_rhtp = NULL;
16880 		}
16881 	}
16882 
16883 	/*
16884 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16885 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16886 	 * After selecting the right ipif, under ill_lock make sure ipif is
16887 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16888 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16889 	 * but not under a lock.
16890 	 */
16891 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16892 retry:
16893 	/*
16894 	 * For source address selection, we treat the ipif list as circular
16895 	 * and continue until we get back to where we started.  This allows
16896 	 * IPMP to vary source address selection (which improves inbound load
16897 	 * spreading) by caching its last ending point and starting from
16898 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16899 	 * ills since that can't happen on the IPMP ill.
16900 	 */
16901 	start_ipif = ill->ill_ipif;
16902 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16903 		start_ipif = ill->ill_src_ipif;
16904 
16905 	ipif = start_ipif;
16906 	best_ipif = NULL;
16907 	best_type = IPIF_NONE;
16908 	do {
16909 		if ((next_ipif = ipif->ipif_next) == NULL)
16910 			next_ipif = ill->ill_ipif;
16911 
16912 		if (!IPIF_CAN_LOOKUP(ipif))
16913 			continue;
16914 		/* Always skip NOLOCAL and ANYCAST interfaces */
16915 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16916 			continue;
16917 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16918 			continue;
16919 		if (ipif->ipif_zoneid != zoneid &&
16920 		    ipif->ipif_zoneid != ALL_ZONES)
16921 			continue;
16922 
16923 		/*
16924 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16925 		 * are not valid as source addresses.
16926 		 */
16927 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16928 			continue;
16929 
16930 		/*
16931 		 * Check compatibility of local address for destination's
16932 		 * default label if we're on a labeled system.	Incompatible
16933 		 * addresses can't be used at all.
16934 		 */
16935 		if (dst_rhtp != NULL) {
16936 			boolean_t incompat;
16937 
16938 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16939 			    IPV4_VERSION, B_FALSE);
16940 			if (src_rhtp == NULL)
16941 				continue;
16942 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16943 			    src_rhtp->tpc_tp.tp_doi !=
16944 			    dst_rhtp->tpc_tp.tp_doi ||
16945 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16946 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16947 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16948 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16949 			TPC_RELE(src_rhtp);
16950 			if (incompat)
16951 				continue;
16952 		}
16953 
16954 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16955 
16956 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16957 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16958 			    IPIF_DIFFNET_DEPRECATED;
16959 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16960 			type = samenet ? IPIF_SAMENET_ALLZONES :
16961 			    IPIF_DIFFNET_ALLZONES;
16962 		} else {
16963 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16964 		}
16965 
16966 		if (type > best_type) {
16967 			best_type = type;
16968 			best_ipif = ipif;
16969 			if (best_type == IPIF_SAMENET)
16970 				break; /* can't get better */
16971 		}
16972 	} while ((ipif = next_ipif) != start_ipif);
16973 
16974 	if ((ipif = best_ipif) != NULL) {
16975 		mutex_enter(&ipif->ipif_ill->ill_lock);
16976 		if (!IPIF_CAN_LOOKUP(ipif)) {
16977 			mutex_exit(&ipif->ipif_ill->ill_lock);
16978 			goto retry;
16979 		}
16980 		ipif_refhold_locked(ipif);
16981 
16982 		/*
16983 		 * For IPMP, update the source ipif rotor to the next ipif,
16984 		 * provided we can look it up.  (We must not use it if it's
16985 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16986 		 * ipif_free() checked ill_src_ipif.)
16987 		 */
16988 		if (IS_IPMP(ill) && ipif != NULL) {
16989 			next_ipif = ipif->ipif_next;
16990 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16991 				ill->ill_src_ipif = next_ipif;
16992 			else
16993 				ill->ill_src_ipif = NULL;
16994 		}
16995 		mutex_exit(&ipif->ipif_ill->ill_lock);
16996 	}
16997 
16998 	rw_exit(&ipst->ips_ill_g_lock);
16999 	if (usill != NULL)
17000 		ill_refrele(usill);
17001 	if (ipmp_ill != NULL)
17002 		ill_refrele(ipmp_ill);
17003 	if (dst_rhtp != NULL)
17004 		TPC_RELE(dst_rhtp);
17005 
17006 #ifdef DEBUG
17007 	if (ipif == NULL) {
17008 		char buf1[INET6_ADDRSTRLEN];
17009 
17010 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
17011 		    ill->ill_name,
17012 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
17013 	} else {
17014 		char buf1[INET6_ADDRSTRLEN];
17015 		char buf2[INET6_ADDRSTRLEN];
17016 
17017 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
17018 		    ipif->ipif_ill->ill_name,
17019 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
17020 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
17021 		    buf2, sizeof (buf2))));
17022 	}
17023 #endif /* DEBUG */
17024 	return (ipif);
17025 }
17026 
17027 
17028 /*
17029  * If old_ipif is not NULL, see if ipif was derived from old
17030  * ipif and if so, recreate the interface route by re-doing
17031  * source address selection. This happens when ipif_down ->
17032  * ipif_update_other_ipifs calls us.
17033  *
17034  * If old_ipif is NULL, just redo the source address selection
17035  * if needed. This happens when ipif_up_done calls us.
17036  */
17037 static void
17038 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
17039 {
17040 	ire_t *ire;
17041 	ire_t *ipif_ire;
17042 	queue_t *stq;
17043 	ipif_t *nipif;
17044 	ill_t *ill;
17045 	boolean_t need_rele = B_FALSE;
17046 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17047 
17048 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
17049 	ASSERT(IAM_WRITER_IPIF(ipif));
17050 
17051 	ill = ipif->ipif_ill;
17052 	if (!(ipif->ipif_flags &
17053 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
17054 		/*
17055 		 * Can't possibly have borrowed the source
17056 		 * from old_ipif.
17057 		 */
17058 		return;
17059 	}
17060 
17061 	/*
17062 	 * Is there any work to be done? No work if the address
17063 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
17064 	 * ipif_select_source() does not borrow addresses from
17065 	 * NOLOCAL and ANYCAST interfaces).
17066 	 */
17067 	if ((old_ipif != NULL) &&
17068 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
17069 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
17070 	    (old_ipif->ipif_flags &
17071 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
17072 		return;
17073 	}
17074 
17075 	/*
17076 	 * Perform the same checks as when creating the
17077 	 * IRE_INTERFACE in ipif_up_done.
17078 	 */
17079 	if (!(ipif->ipif_flags & IPIF_UP))
17080 		return;
17081 
17082 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
17083 	    (ipif->ipif_subnet == INADDR_ANY))
17084 		return;
17085 
17086 	ipif_ire = ipif_to_ire(ipif);
17087 	if (ipif_ire == NULL)
17088 		return;
17089 
17090 	/*
17091 	 * We know that ipif uses some other source for its
17092 	 * IRE_INTERFACE. Is it using the source of this
17093 	 * old_ipif?
17094 	 */
17095 	if (old_ipif != NULL &&
17096 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
17097 		ire_refrele(ipif_ire);
17098 		return;
17099 	}
17100 	if (ip_debug > 2) {
17101 		/* ip1dbg */
17102 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
17103 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
17104 	}
17105 
17106 	stq = ipif_ire->ire_stq;
17107 
17108 	/*
17109 	 * Can't use our source address. Select a different
17110 	 * source address for the IRE_INTERFACE.
17111 	 */
17112 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
17113 	if (nipif == NULL) {
17114 		/* Last resort - all ipif's have IPIF_NOLOCAL */
17115 		nipif = ipif;
17116 	} else {
17117 		need_rele = B_TRUE;
17118 	}
17119 
17120 	ire = ire_create(
17121 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
17122 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
17123 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
17124 	    NULL,				/* no gateway */
17125 	    &ipif->ipif_mtu,			/* max frag */
17126 	    NULL,				/* no src nce */
17127 	    NULL,				/* no recv from queue */
17128 	    stq,				/* send-to queue */
17129 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
17130 	    ipif,
17131 	    0,
17132 	    0,
17133 	    0,
17134 	    0,
17135 	    &ire_uinfo_null,
17136 	    NULL,
17137 	    NULL,
17138 	    ipst);
17139 
17140 	if (ire != NULL) {
17141 		ire_t *ret_ire;
17142 		int error;
17143 
17144 		/*
17145 		 * We don't need ipif_ire anymore. We need to delete
17146 		 * before we add so that ire_add does not detect
17147 		 * duplicates.
17148 		 */
17149 		ire_delete(ipif_ire);
17150 		ret_ire = ire;
17151 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
17152 		ASSERT(error == 0);
17153 		ASSERT(ire == ret_ire);
17154 		/* Held in ire_add */
17155 		ire_refrele(ret_ire);
17156 	}
17157 	/*
17158 	 * Either we are falling through from above or could not
17159 	 * allocate a replacement.
17160 	 */
17161 	ire_refrele(ipif_ire);
17162 	if (need_rele)
17163 		ipif_refrele(nipif);
17164 }
17165 
17166 /*
17167  * This old_ipif is going away.
17168  *
17169  * Determine if any other ipif's are using our address as
17170  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17171  * IPIF_DEPRECATED).
17172  * Find the IRE_INTERFACE for such ipifs and recreate them
17173  * to use an different source address following the rules in
17174  * ipif_up_done.
17175  */
17176 static void
17177 ipif_update_other_ipifs(ipif_t *old_ipif)
17178 {
17179 	ipif_t	*ipif;
17180 	ill_t	*ill;
17181 	char	buf[INET6_ADDRSTRLEN];
17182 
17183 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17184 
17185 	ill = old_ipif->ipif_ill;
17186 
17187 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17188 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17189 
17190 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17191 		if (ipif == old_ipif)
17192 			continue;
17193 		ipif_recreate_interface_routes(old_ipif, ipif);
17194 	}
17195 }
17196 
17197 /* ARGSUSED */
17198 int
17199 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17200 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17201 {
17202 	/*
17203 	 * ill_phyint_reinit merged the v4 and v6 into a single
17204 	 * ipsq.  We might not have been able to complete the
17205 	 * operation in ipif_set_values, if we could not become
17206 	 * exclusive.  If so restart it here.
17207 	 */
17208 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17209 }
17210 
17211 /*
17212  * Can operate on either a module or a driver queue.
17213  * Returns an error if not a module queue.
17214  */
17215 /* ARGSUSED */
17216 int
17217 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17218     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17219 {
17220 	queue_t		*q1 = q;
17221 	char 		*cp;
17222 	char		interf_name[LIFNAMSIZ];
17223 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17224 
17225 	if (q->q_next == NULL) {
17226 		ip1dbg((
17227 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17228 		return (EINVAL);
17229 	}
17230 
17231 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17232 		return (EALREADY);
17233 
17234 	do {
17235 		q1 = q1->q_next;
17236 	} while (q1->q_next);
17237 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17238 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17239 
17240 	/*
17241 	 * Here we are not going to delay the ioack until after
17242 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17243 	 * original ioctl message before sending the requests.
17244 	 */
17245 	return (ipif_set_values(q, mp, interf_name, &ppa));
17246 }
17247 
17248 /* ARGSUSED */
17249 int
17250 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17251     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17252 {
17253 	return (ENXIO);
17254 }
17255 
17256 /*
17257  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17258  * minimum (but complete) set exist.  This is necessary when adding or
17259  * removing an interface to/from an IPMP group, since interfaces in an
17260  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17261  * its test address subnets overlap with IPMP data addresses).	It's also
17262  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17263  * interface when the nominated broadcast interface changes.
17264  */
17265 void
17266 ill_refresh_bcast(ill_t *ill)
17267 {
17268 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17269 	ire_t **irep;
17270 	ipif_t *ipif;
17271 
17272 	ASSERT(!ill->ill_isv6);
17273 	ASSERT(IAM_WRITER_ILL(ill));
17274 
17275 	/*
17276 	 * Remove any old broadcast IREs.
17277 	 */
17278 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17279 	    ill_broadcast_delete, ill, ill);
17280 
17281 	/*
17282 	 * Create new ones for any ipifs that are up and broadcast-capable.
17283 	 */
17284 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17285 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17286 		    (IPIF_UP|IPIF_BROADCAST))
17287 			continue;
17288 
17289 		irep = ipif_create_bcast_ires(ipif, ire_array);
17290 		while (irep-- > ire_array) {
17291 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17292 			if (*irep != NULL)
17293 				ire_refrele(*irep);
17294 		}
17295 	}
17296 }
17297 
17298 /*
17299  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17300  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17301  * ire_check_and_create_bcast()).
17302  */
17303 static ire_t **
17304 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17305 {
17306 	ipaddr_t addr;
17307 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17308 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17309 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17310 
17311 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17312 
17313 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17314 
17315 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17316 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17317 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17318 
17319 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17320 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17321 
17322 	/*
17323 	 * For backward compatibility, we create net broadcast IREs based on
17324 	 * the old "IP address class system", since some old machines only
17325 	 * respond to these class derived net broadcast.  However, we must not
17326 	 * create these net broadcast IREs if the subnetmask is shorter than
17327 	 * the IP address class based derived netmask.  Otherwise, we may
17328 	 * create a net broadcast address which is the same as an IP address
17329 	 * on the subnet -- and then TCP will refuse to talk to that address.
17330 	 */
17331 	if (netmask < subnetmask) {
17332 		addr = netmask & ipif->ipif_subnet;
17333 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17334 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17335 		    flags);
17336 	}
17337 
17338 	/*
17339 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17340 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17341 	 * created.  Creating these broadcast IREs will only create confusion
17342 	 * as `addr' will be the same as the IP address.
17343 	 */
17344 	if (subnetmask != 0xFFFFFFFF) {
17345 		addr = ipif->ipif_subnet;
17346 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17347 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17348 		    irep, flags);
17349 	}
17350 
17351 	return (irep);
17352 }
17353 
17354 /*
17355  * Broadcast IRE info structure used in the functions below.  Since we
17356  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17357  */
17358 typedef struct bcast_ireinfo {
17359 	uchar_t		bi_type;	/* BCAST_* value from below */
17360 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17361 			bi_needrep:1,	/* do we need to replace it? */
17362 			bi_haverep:1,	/* have we replaced it? */
17363 			bi_pad:5;
17364 	ipaddr_t	bi_addr;	/* IRE address */
17365 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17366 } bcast_ireinfo_t;
17367 
17368 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17369 
17370 /*
17371  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17372  * return B_TRUE if it should immediately be used to recreate the IRE.
17373  */
17374 static boolean_t
17375 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17376 {
17377 	ipaddr_t addr;
17378 
17379 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17380 
17381 	switch (bireinfop->bi_type) {
17382 	case BCAST_NET:
17383 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17384 		if (addr != bireinfop->bi_addr)
17385 			return (B_FALSE);
17386 		break;
17387 	case BCAST_SUBNET:
17388 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17389 			return (B_FALSE);
17390 		break;
17391 	}
17392 
17393 	bireinfop->bi_needrep = 1;
17394 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17395 		if (bireinfop->bi_backup == NULL)
17396 			bireinfop->bi_backup = ipif;
17397 		return (B_FALSE);
17398 	}
17399 	return (B_TRUE);
17400 }
17401 
17402 /*
17403  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17404  * them ala ire_check_and_create_bcast().
17405  */
17406 static ire_t **
17407 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17408 {
17409 	ipaddr_t mask, addr;
17410 
17411 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17412 
17413 	addr = bireinfop->bi_addr;
17414 	irep = ire_create_bcast(ipif, addr, irep);
17415 
17416 	switch (bireinfop->bi_type) {
17417 	case BCAST_NET:
17418 		mask = ip_net_mask(ipif->ipif_subnet);
17419 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17420 		break;
17421 	case BCAST_SUBNET:
17422 		mask = ipif->ipif_net_mask;
17423 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17424 		break;
17425 	}
17426 
17427 	bireinfop->bi_haverep = 1;
17428 	return (irep);
17429 }
17430 
17431 /*
17432  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17433  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17434  * that are going away are still needed.  If so, have ipif_create_bcast()
17435  * recreate them (except for the deprecated case, as explained below).
17436  */
17437 static ire_t **
17438 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17439     ire_t **irep)
17440 {
17441 	int i;
17442 	ipif_t *ipif;
17443 
17444 	ASSERT(!ill->ill_isv6);
17445 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17446 		/*
17447 		 * Skip this ipif if it's (a) the one being taken down, (b)
17448 		 * not in the same zone, or (c) has no valid local address.
17449 		 */
17450 		if (ipif == test_ipif ||
17451 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17452 		    ipif->ipif_subnet == 0 ||
17453 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17454 		    (IPIF_UP|IPIF_BROADCAST))
17455 			continue;
17456 
17457 		/*
17458 		 * For each dying IRE that hasn't yet been replaced, see if
17459 		 * `ipif' needs it and whether the IRE should be recreated on
17460 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17461 		 * will return B_FALSE even if `ipif' needs the IRE on the
17462 		 * hopes that we'll later find a needy non-deprecated ipif.
17463 		 * However, the ipif is recorded in bi_backup for possible
17464 		 * subsequent use by ipif_check_bcast_ires().
17465 		 */
17466 		for (i = 0; i < BCAST_COUNT; i++) {
17467 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17468 				continue;
17469 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17470 				continue;
17471 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17472 		}
17473 
17474 		/*
17475 		 * If we've replaced all of the broadcast IREs that are going
17476 		 * to be taken down, we know we're done.
17477 		 */
17478 		for (i = 0; i < BCAST_COUNT; i++) {
17479 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17480 				break;
17481 		}
17482 		if (i == BCAST_COUNT)
17483 			break;
17484 	}
17485 	return (irep);
17486 }
17487 
17488 /*
17489  * Check if `test_ipif' (which is going away) is associated with any existing
17490  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17491  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17492  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17493  *
17494  * This is necessary because broadcast IREs are shared.  In particular, a
17495  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17496  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17497  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17498  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17499  * same zone, they will share the same set of broadcast IREs.
17500  *
17501  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17502  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17503  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17504  */
17505 static void
17506 ipif_check_bcast_ires(ipif_t *test_ipif)
17507 {
17508 	ill_t		*ill = test_ipif->ipif_ill;
17509 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17510 	ire_t		**irep1, **irep = &ire_array[0];
17511 	uint_t 		i, willdie;
17512 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17513 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17514 
17515 	ASSERT(!test_ipif->ipif_isv6);
17516 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17517 
17518 	/*
17519 	 * No broadcast IREs for the LOOPBACK interface
17520 	 * or others such as point to point and IPIF_NOXMIT.
17521 	 */
17522 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17523 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17524 		return;
17525 
17526 	bzero(bireinfo, sizeof (bireinfo));
17527 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17528 	bireinfo[0].bi_addr = 0;
17529 
17530 	bireinfo[1].bi_type = BCAST_ALLONES;
17531 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17532 
17533 	bireinfo[2].bi_type = BCAST_NET;
17534 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17535 
17536 	if (test_ipif->ipif_net_mask != 0)
17537 		mask = test_ipif->ipif_net_mask;
17538 	bireinfo[3].bi_type = BCAST_SUBNET;
17539 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17540 
17541 	/*
17542 	 * Figure out what (if any) broadcast IREs will die as a result of
17543 	 * `test_ipif' going away.  If none will die, we're done.
17544 	 */
17545 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17546 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17547 		    test_ipif, ALL_ZONES, NULL,
17548 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17549 		if (ire != NULL) {
17550 			willdie++;
17551 			bireinfo[i].bi_willdie = 1;
17552 			ire_refrele(ire);
17553 		}
17554 	}
17555 
17556 	if (willdie == 0)
17557 		return;
17558 
17559 	/*
17560 	 * Walk through all the ipifs that will be affected by the dying IREs,
17561 	 * and recreate the IREs as necessary. Note that all interfaces in an
17562 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17563 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17564 	 * that broadcast IREs end up on it whenever possible).
17565 	 */
17566 	if (IS_UNDER_IPMP(ill))
17567 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17568 
17569 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17570 
17571 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17572 		ipmp_illgrp_t *illg = ill->ill_grp;
17573 
17574 		ill = list_head(&illg->ig_if);
17575 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17576 			for (i = 0; i < BCAST_COUNT; i++) {
17577 				if (bireinfo[i].bi_willdie &&
17578 				    !bireinfo[i].bi_haverep)
17579 					break;
17580 			}
17581 			if (i == BCAST_COUNT)
17582 				break;
17583 
17584 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17585 		}
17586 	}
17587 
17588 	/*
17589 	 * Scan through the set of broadcast IREs and see if there are any
17590 	 * that we need to replace that have not yet been replaced.  If so,
17591 	 * replace them using the appropriate backup ipif.
17592 	 */
17593 	for (i = 0; i < BCAST_COUNT; i++) {
17594 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17595 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17596 			    &bireinfo[i], irep);
17597 	}
17598 
17599 	/*
17600 	 * If we can't create all of them, don't add any of them.  (Code in
17601 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17602 	 * non-loopback copy and loopback copy for a given address.)
17603 	 */
17604 	for (irep1 = irep; irep1 > ire_array; ) {
17605 		irep1--;
17606 		if (*irep1 == NULL) {
17607 			ip0dbg(("ipif_check_bcast_ires: can't create "
17608 			    "IRE_BROADCAST, memory allocation failure\n"));
17609 			while (irep > ire_array) {
17610 				irep--;
17611 				if (*irep != NULL)
17612 					ire_delete(*irep);
17613 			}
17614 			return;
17615 		}
17616 	}
17617 
17618 	for (irep1 = irep; irep1 > ire_array; ) {
17619 		irep1--;
17620 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17621 			ire_refrele(*irep1);		/* Held in ire_add */
17622 	}
17623 }
17624 
17625 /*
17626  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17627  * from lifr_flags and the name from lifr_name.
17628  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17629  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17630  * Returns EINPROGRESS when mp has been consumed by queueing it on
17631  * ill_pending_mp and the ioctl will complete in ip_rput.
17632  *
17633  * Can operate on either a module or a driver queue.
17634  * Returns an error if not a module queue.
17635  */
17636 /* ARGSUSED */
17637 int
17638 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17639     ip_ioctl_cmd_t *ipip, void *if_req)
17640 {
17641 	ill_t	*ill = q->q_ptr;
17642 	phyint_t *phyi;
17643 	ip_stack_t *ipst;
17644 	struct lifreq *lifr = if_req;
17645 
17646 	ASSERT(ipif != NULL);
17647 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17648 
17649 	if (q->q_next == NULL) {
17650 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17651 		return (EINVAL);
17652 	}
17653 
17654 	/*
17655 	 * If we are not writer on 'q' then this interface exists already
17656 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17657 	 * so return EALREADY.
17658 	 */
17659 	if (ill != ipif->ipif_ill)
17660 		return (EALREADY);
17661 
17662 	if (ill->ill_name[0] != '\0')
17663 		return (EALREADY);
17664 
17665 	/*
17666 	 * Set all the flags. Allows all kinds of override. Provide some
17667 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
17668 	 * unless there is either multicast/broadcast support in the driver
17669 	 * or it is a pt-pt link.
17670 	 */
17671 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
17672 		/* Meaningless to IP thus don't allow them to be set. */
17673 		ip1dbg(("ip_setname: EINVAL 1\n"));
17674 		return (EINVAL);
17675 	}
17676 
17677 	/*
17678 	 * If there's another ill already with the requested name, ensure
17679 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17680 	 * fuse together two unrelated ills, which will cause chaos.
17681 	 */
17682 	ipst = ill->ill_ipst;
17683 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17684 	    lifr->lifr_name, NULL);
17685 	if (phyi != NULL) {
17686 		ill_t *ill_mate = phyi->phyint_illv4;
17687 
17688 		if (ill_mate == NULL)
17689 			ill_mate = phyi->phyint_illv6;
17690 		ASSERT(ill_mate != NULL);
17691 
17692 		if (ill_mate->ill_media->ip_m_mac_type !=
17693 		    ill->ill_media->ip_m_mac_type) {
17694 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17695 			    "use the same ill name on differing media\n"));
17696 			return (EINVAL);
17697 		}
17698 	}
17699 
17700 	/*
17701 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
17702 	 * ill_bcast_addr_length info.
17703 	 */
17704 	if (!ill->ill_needs_attach &&
17705 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
17706 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
17707 	    ill->ill_bcast_addr_length == 0)) {
17708 		/* Link not broadcast/pt-pt capable i.e. no multicast */
17709 		ip1dbg(("ip_setname: EINVAL 2\n"));
17710 		return (EINVAL);
17711 	}
17712 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17713 	    ((lifr->lifr_flags & IFF_IPV6) ||
17714 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17715 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
17716 		ip1dbg(("ip_setname: EINVAL 3\n"));
17717 		return (EINVAL);
17718 	}
17719 	if (lifr->lifr_flags & IFF_UP) {
17720 		/* Can only be set with SIOCSLIFFLAGS */
17721 		ip1dbg(("ip_setname: EINVAL 4\n"));
17722 		return (EINVAL);
17723 	}
17724 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
17725 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
17726 		ip1dbg(("ip_setname: EINVAL 5\n"));
17727 		return (EINVAL);
17728 	}
17729 	/*
17730 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17731 	 */
17732 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
17733 	    !(lifr->lifr_flags & IFF_IPV6) &&
17734 	    !(ipif->ipif_isv6)) {
17735 		ip1dbg(("ip_setname: EINVAL 6\n"));
17736 		return (EINVAL);
17737 	}
17738 
17739 	/*
17740 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
17741 	 * we have all the flags here. So, we assign rather than we OR.
17742 	 * We can't OR the flags here because we don't want to set
17743 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
17744 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
17745 	 * on lifr_flags value here.
17746 	 */
17747 	/*
17748 	 * This ill has not been inserted into the global list.
17749 	 * So we are still single threaded and don't need any lock
17750 	 */
17751 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
17752 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
17753 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
17754 
17755 	/* We started off as V4. */
17756 	if (ill->ill_flags & ILLF_IPV6) {
17757 		ill->ill_phyint->phyint_illv6 = ill;
17758 		ill->ill_phyint->phyint_illv4 = NULL;
17759 	}
17760 
17761 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17762 }
17763 
17764 /* ARGSUSED */
17765 int
17766 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17767     ip_ioctl_cmd_t *ipip, void *if_req)
17768 {
17769 	/*
17770 	 * ill_phyint_reinit merged the v4 and v6 into a single
17771 	 * ipsq.  We might not have been able to complete the
17772 	 * slifname in ipif_set_values, if we could not become
17773 	 * exclusive.  If so restart it here
17774 	 */
17775 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17776 }
17777 
17778 /*
17779  * Return a pointer to the ipif which matches the index, IP version type and
17780  * zoneid.
17781  */
17782 ipif_t *
17783 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17784     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17785 {
17786 	ill_t	*ill;
17787 	ipif_t	*ipif = NULL;
17788 
17789 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17790 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17791 
17792 	if (err != NULL)
17793 		*err = 0;
17794 
17795 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17796 	if (ill != NULL) {
17797 		mutex_enter(&ill->ill_lock);
17798 		for (ipif = ill->ill_ipif; ipif != NULL;
17799 		    ipif = ipif->ipif_next) {
17800 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17801 			    zoneid == ipif->ipif_zoneid ||
17802 			    ipif->ipif_zoneid == ALL_ZONES)) {
17803 				ipif_refhold_locked(ipif);
17804 				break;
17805 			}
17806 		}
17807 		mutex_exit(&ill->ill_lock);
17808 		ill_refrele(ill);
17809 		if (ipif == NULL && err != NULL)
17810 			*err = ENXIO;
17811 	}
17812 	return (ipif);
17813 }
17814 
17815 /*
17816  * We first need to ensure that the new index is unique, and
17817  * then carry the change across both v4 and v6 ill representation
17818  * of the physical interface.
17819  */
17820 /* ARGSUSED */
17821 int
17822 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17823     ip_ioctl_cmd_t *ipip, void *ifreq)
17824 {
17825 	ill_t		*ill;
17826 	phyint_t	*phyi;
17827 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17828 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17829 	uint_t	old_index, index;
17830 	ill_t	*ill_v4;
17831 	ill_t	*ill_v6;
17832 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17833 
17834 	if (ipip->ipi_cmd_type == IF_CMD)
17835 		index = ifr->ifr_index;
17836 	else
17837 		index = lifr->lifr_index;
17838 
17839 	/*
17840 	 * Only allow on physical interface. Also, index zero is illegal.
17841 	 */
17842 	ill = ipif->ipif_ill;
17843 	phyi = ill->ill_phyint;
17844 	if (ipif->ipif_id != 0 || index == 0) {
17845 		return (EINVAL);
17846 	}
17847 
17848 	/* If the index is not changing, no work to do */
17849 	if (phyi->phyint_ifindex == index)
17850 		return (0);
17851 
17852 	/*
17853 	 * Use ill_lookup_on_ifindex to determine if the
17854 	 * new index is unused and if so allow the change.
17855 	 */
17856 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
17857 	    ipst);
17858 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
17859 	    ipst);
17860 	if (ill_v6 != NULL || ill_v4 != NULL) {
17861 		if (ill_v4 != NULL)
17862 			ill_refrele(ill_v4);
17863 		if (ill_v6 != NULL)
17864 			ill_refrele(ill_v6);
17865 		return (EBUSY);
17866 	}
17867 
17868 	/* The new index is unused. Set it in the phyint. */
17869 	old_index = phyi->phyint_ifindex;
17870 	phyi->phyint_ifindex = index;
17871 
17872 	/* Update SCTP's ILL list */
17873 	sctp_ill_reindex(ill, old_index);
17874 
17875 	/* Send the routing sockets message */
17876 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17877 	if (ILL_OTHER(ill))
17878 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17879 
17880 	return (0);
17881 }
17882 
17883 /* ARGSUSED */
17884 int
17885 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17886     ip_ioctl_cmd_t *ipip, void *ifreq)
17887 {
17888 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17889 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17890 
17891 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17892 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17893 	/* Get the interface index */
17894 	if (ipip->ipi_cmd_type == IF_CMD) {
17895 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17896 	} else {
17897 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17898 	}
17899 	return (0);
17900 }
17901 
17902 /* ARGSUSED */
17903 int
17904 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17905     ip_ioctl_cmd_t *ipip, void *ifreq)
17906 {
17907 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17908 
17909 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17910 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17911 	/* Get the interface zone */
17912 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17913 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17914 	return (0);
17915 }
17916 
17917 /*
17918  * Set the zoneid of an interface.
17919  */
17920 /* ARGSUSED */
17921 int
17922 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17923     ip_ioctl_cmd_t *ipip, void *ifreq)
17924 {
17925 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17926 	int err = 0;
17927 	boolean_t need_up = B_FALSE;
17928 	zone_t *zptr;
17929 	zone_status_t status;
17930 	zoneid_t zoneid;
17931 
17932 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17933 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17934 		if (!is_system_labeled())
17935 			return (ENOTSUP);
17936 		zoneid = GLOBAL_ZONEID;
17937 	}
17938 
17939 	/* cannot assign instance zero to a non-global zone */
17940 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17941 		return (ENOTSUP);
17942 
17943 	/*
17944 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17945 	 * the event of a race with the zone shutdown processing, since IP
17946 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17947 	 * interface will be cleaned up even if the zone is shut down
17948 	 * immediately after the status check. If the interface can't be brought
17949 	 * down right away, and the zone is shut down before the restart
17950 	 * function is called, we resolve the possible races by rechecking the
17951 	 * zone status in the restart function.
17952 	 */
17953 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17954 		return (EINVAL);
17955 	status = zone_status_get(zptr);
17956 	zone_rele(zptr);
17957 
17958 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17959 		return (EINVAL);
17960 
17961 	if (ipif->ipif_flags & IPIF_UP) {
17962 		/*
17963 		 * If the interface is already marked up,
17964 		 * we call ipif_down which will take care
17965 		 * of ditching any IREs that have been set
17966 		 * up based on the old interface address.
17967 		 */
17968 		err = ipif_logical_down(ipif, q, mp);
17969 		if (err == EINPROGRESS)
17970 			return (err);
17971 		ipif_down_tail(ipif);
17972 		need_up = B_TRUE;
17973 	}
17974 
17975 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17976 	return (err);
17977 }
17978 
17979 static int
17980 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17981     queue_t *q, mblk_t *mp, boolean_t need_up)
17982 {
17983 	int	err = 0;
17984 	ip_stack_t	*ipst;
17985 
17986 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17987 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17988 
17989 	if (CONN_Q(q))
17990 		ipst = CONNQ_TO_IPST(q);
17991 	else
17992 		ipst = ILLQ_TO_IPST(q);
17993 
17994 	/*
17995 	 * For exclusive stacks we don't allow a different zoneid than
17996 	 * global.
17997 	 */
17998 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17999 	    zoneid != GLOBAL_ZONEID)
18000 		return (EINVAL);
18001 
18002 	/* Set the new zone id. */
18003 	ipif->ipif_zoneid = zoneid;
18004 
18005 	/* Update sctp list */
18006 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
18007 
18008 	if (need_up) {
18009 		/*
18010 		 * Now bring the interface back up.  If this
18011 		 * is the only IPIF for the ILL, ipif_up
18012 		 * will have to re-bind to the device, so
18013 		 * we may get back EINPROGRESS, in which
18014 		 * case, this IOCTL will get completed in
18015 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
18016 		 */
18017 		err = ipif_up(ipif, q, mp);
18018 	}
18019 	return (err);
18020 }
18021 
18022 /* ARGSUSED */
18023 int
18024 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18025     ip_ioctl_cmd_t *ipip, void *if_req)
18026 {
18027 	struct lifreq *lifr = (struct lifreq *)if_req;
18028 	zoneid_t zoneid;
18029 	zone_t *zptr;
18030 	zone_status_t status;
18031 
18032 	ASSERT(ipif->ipif_id != 0);
18033 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
18034 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
18035 		zoneid = GLOBAL_ZONEID;
18036 
18037 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
18038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
18039 
18040 	/*
18041 	 * We recheck the zone status to resolve the following race condition:
18042 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
18043 	 * 2) hme0:1 is up and can't be brought down right away;
18044 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
18045 	 * 3) zone "myzone" is halted; the zone status switches to
18046 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
18047 	 * the interfaces to remove - hme0:1 is not returned because it's not
18048 	 * yet in "myzone", so it won't be removed;
18049 	 * 4) the restart function for SIOCSLIFZONE is called; without the
18050 	 * status check here, we would have hme0:1 in "myzone" after it's been
18051 	 * destroyed.
18052 	 * Note that if the status check fails, we need to bring the interface
18053 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
18054 	 * ipif_up_done[_v6]().
18055 	 */
18056 	status = ZONE_IS_UNINITIALIZED;
18057 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
18058 		status = zone_status_get(zptr);
18059 		zone_rele(zptr);
18060 	}
18061 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
18062 		if (ipif->ipif_isv6) {
18063 			(void) ipif_up_done_v6(ipif);
18064 		} else {
18065 			(void) ipif_up_done(ipif);
18066 		}
18067 		return (EINVAL);
18068 	}
18069 
18070 	ipif_down_tail(ipif);
18071 
18072 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
18073 	    B_TRUE));
18074 }
18075 
18076 /*
18077  * Return the number of addresses on `ill' with one or more of the values
18078  * in `set' set and all of the values in `clear' clear.
18079  */
18080 static uint_t
18081 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
18082 {
18083 	ipif_t	*ipif;
18084 	uint_t	cnt = 0;
18085 
18086 	ASSERT(IAM_WRITER_ILL(ill));
18087 
18088 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
18089 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
18090 			cnt++;
18091 
18092 	return (cnt);
18093 }
18094 
18095 /*
18096  * Return the number of migratable addresses on `ill' that are under
18097  * application control.
18098  */
18099 uint_t
18100 ill_appaddr_cnt(const ill_t *ill)
18101 {
18102 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
18103 	    IPIF_NOFAILOVER));
18104 }
18105 
18106 /*
18107  * Return the number of point-to-point addresses on `ill'.
18108  */
18109 uint_t
18110 ill_ptpaddr_cnt(const ill_t *ill)
18111 {
18112 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
18113 }
18114 
18115 /* ARGSUSED */
18116 int
18117 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18118 	ip_ioctl_cmd_t *ipip, void *ifreq)
18119 {
18120 	struct lifreq	*lifr = ifreq;
18121 
18122 	ASSERT(q->q_next == NULL);
18123 	ASSERT(CONN_Q(q));
18124 
18125 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
18126 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
18127 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
18128 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
18129 
18130 	return (0);
18131 }
18132 
18133 /* Find the previous ILL in this usesrc group */
18134 static ill_t *
18135 ill_prev_usesrc(ill_t *uill)
18136 {
18137 	ill_t *ill;
18138 
18139 	for (ill = uill->ill_usesrc_grp_next;
18140 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
18141 	    ill = ill->ill_usesrc_grp_next)
18142 		/* do nothing */;
18143 	return (ill);
18144 }
18145 
18146 /*
18147  * Release all members of the usesrc group. This routine is called
18148  * from ill_delete when the interface being unplumbed is the
18149  * group head.
18150  */
18151 static void
18152 ill_disband_usesrc_group(ill_t *uill)
18153 {
18154 	ill_t *next_ill, *tmp_ill;
18155 	ip_stack_t	*ipst = uill->ill_ipst;
18156 
18157 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18158 	next_ill = uill->ill_usesrc_grp_next;
18159 
18160 	do {
18161 		ASSERT(next_ill != NULL);
18162 		tmp_ill = next_ill->ill_usesrc_grp_next;
18163 		ASSERT(tmp_ill != NULL);
18164 		next_ill->ill_usesrc_grp_next = NULL;
18165 		next_ill->ill_usesrc_ifindex = 0;
18166 		next_ill = tmp_ill;
18167 	} while (next_ill->ill_usesrc_ifindex != 0);
18168 	uill->ill_usesrc_grp_next = NULL;
18169 }
18170 
18171 /*
18172  * Remove the client usesrc ILL from the list and relink to a new list
18173  */
18174 int
18175 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18176 {
18177 	ill_t *ill, *tmp_ill;
18178 	ip_stack_t	*ipst = ucill->ill_ipst;
18179 
18180 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18181 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18182 
18183 	/*
18184 	 * Check if the usesrc client ILL passed in is not already
18185 	 * in use as a usesrc ILL i.e one whose source address is
18186 	 * in use OR a usesrc ILL is not already in use as a usesrc
18187 	 * client ILL
18188 	 */
18189 	if ((ucill->ill_usesrc_ifindex == 0) ||
18190 	    (uill->ill_usesrc_ifindex != 0)) {
18191 		return (-1);
18192 	}
18193 
18194 	ill = ill_prev_usesrc(ucill);
18195 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18196 
18197 	/* Remove from the current list */
18198 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18199 		/* Only two elements in the list */
18200 		ASSERT(ill->ill_usesrc_ifindex == 0);
18201 		ill->ill_usesrc_grp_next = NULL;
18202 	} else {
18203 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18204 	}
18205 
18206 	if (ifindex == 0) {
18207 		ucill->ill_usesrc_ifindex = 0;
18208 		ucill->ill_usesrc_grp_next = NULL;
18209 		return (0);
18210 	}
18211 
18212 	ucill->ill_usesrc_ifindex = ifindex;
18213 	tmp_ill = uill->ill_usesrc_grp_next;
18214 	uill->ill_usesrc_grp_next = ucill;
18215 	ucill->ill_usesrc_grp_next =
18216 	    (tmp_ill != NULL) ? tmp_ill : uill;
18217 	return (0);
18218 }
18219 
18220 /*
18221  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18222  * ip.c for locking details.
18223  */
18224 /* ARGSUSED */
18225 int
18226 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18227     ip_ioctl_cmd_t *ipip, void *ifreq)
18228 {
18229 	struct lifreq *lifr = (struct lifreq *)ifreq;
18230 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18231 	    ill_flag_changed = B_FALSE;
18232 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18233 	int err = 0, ret;
18234 	uint_t ifindex;
18235 	ipsq_t *ipsq = NULL;
18236 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18237 
18238 	ASSERT(IAM_WRITER_IPIF(ipif));
18239 	ASSERT(q->q_next == NULL);
18240 	ASSERT(CONN_Q(q));
18241 
18242 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18243 
18244 	ifindex = lifr->lifr_index;
18245 	if (ifindex == 0) {
18246 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18247 			/* non usesrc group interface, nothing to reset */
18248 			return (0);
18249 		}
18250 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18251 		/* valid reset request */
18252 		reset_flg = B_TRUE;
18253 	}
18254 
18255 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18256 	    ip_process_ioctl, &err, ipst);
18257 	if (usesrc_ill == NULL) {
18258 		return (err);
18259 	}
18260 
18261 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18262 	    NEW_OP, B_TRUE);
18263 	if (ipsq == NULL) {
18264 		err = EINPROGRESS;
18265 		/* Operation enqueued on the ipsq of the usesrc ILL */
18266 		goto done;
18267 	}
18268 
18269 	/* USESRC isn't currently supported with IPMP */
18270 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18271 		err = ENOTSUP;
18272 		goto done;
18273 	}
18274 
18275 	/*
18276 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18277 	 * used by IPMP underlying interfaces, but someone might think it's
18278 	 * more general and try to use it independently with VNI.)
18279 	 */
18280 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18281 		err = ENOTSUP;
18282 		goto done;
18283 	}
18284 
18285 	/*
18286 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18287 	 * already a client then return EINVAL
18288 	 */
18289 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18290 		err = EINVAL;
18291 		goto done;
18292 	}
18293 
18294 	/*
18295 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18296 	 * be then this is a duplicate operation.
18297 	 */
18298 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18299 		err = 0;
18300 		goto done;
18301 	}
18302 
18303 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18304 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18305 	    usesrc_ill->ill_isv6));
18306 
18307 	/*
18308 	 * The next step ensures that no new ires will be created referencing
18309 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18310 	 * we go through an ire walk deleting all ire caches that reference
18311 	 * the client ill. New ires referencing the client ill that are added
18312 	 * to the ire table before the ILL_CHANGING flag is set, will be
18313 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18314 	 * the client ill while the ILL_CHANGING flag is set will be failed
18315 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18316 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18317 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18318 	 * belong to the same usesrc group.
18319 	 */
18320 	mutex_enter(&usesrc_cli_ill->ill_lock);
18321 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18322 	mutex_exit(&usesrc_cli_ill->ill_lock);
18323 	ill_flag_changed = B_TRUE;
18324 
18325 	if (ipif->ipif_isv6)
18326 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18327 		    ALL_ZONES, ipst);
18328 	else
18329 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18330 		    ALL_ZONES, ipst);
18331 
18332 	/*
18333 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18334 	 * and the ill_usesrc_ifindex fields
18335 	 */
18336 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18337 
18338 	if (reset_flg) {
18339 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18340 		if (ret != 0) {
18341 			err = EINVAL;
18342 		}
18343 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18344 		goto done;
18345 	}
18346 
18347 	/*
18348 	 * Four possibilities to consider:
18349 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18350 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18351 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18352 	 * 4. Both are part of their respective usesrc groups
18353 	 */
18354 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18355 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18356 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18357 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18358 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18359 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18360 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18361 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18362 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18363 		/* Insert at head of list */
18364 		usesrc_cli_ill->ill_usesrc_grp_next =
18365 		    usesrc_ill->ill_usesrc_grp_next;
18366 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18367 	} else {
18368 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18369 		    ifindex);
18370 		if (ret != 0)
18371 			err = EINVAL;
18372 	}
18373 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18374 
18375 done:
18376 	if (ill_flag_changed) {
18377 		mutex_enter(&usesrc_cli_ill->ill_lock);
18378 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18379 		mutex_exit(&usesrc_cli_ill->ill_lock);
18380 	}
18381 	if (ipsq != NULL)
18382 		ipsq_exit(ipsq);
18383 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18384 	ill_refrele(usesrc_ill);
18385 	return (err);
18386 }
18387 
18388 /*
18389  * comparison function used by avl.
18390  */
18391 static int
18392 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18393 {
18394 
18395 	uint_t index;
18396 
18397 	ASSERT(phyip != NULL && index_ptr != NULL);
18398 
18399 	index = *((uint_t *)index_ptr);
18400 	/*
18401 	 * let the phyint with the lowest index be on top.
18402 	 */
18403 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18404 		return (1);
18405 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18406 		return (-1);
18407 	return (0);
18408 }
18409 
18410 /*
18411  * comparison function used by avl.
18412  */
18413 static int
18414 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18415 {
18416 	ill_t *ill;
18417 	int res = 0;
18418 
18419 	ASSERT(phyip != NULL && name_ptr != NULL);
18420 
18421 	if (((phyint_t *)phyip)->phyint_illv4)
18422 		ill = ((phyint_t *)phyip)->phyint_illv4;
18423 	else
18424 		ill = ((phyint_t *)phyip)->phyint_illv6;
18425 	ASSERT(ill != NULL);
18426 
18427 	res = strcmp(ill->ill_name, (char *)name_ptr);
18428 	if (res > 0)
18429 		return (1);
18430 	else if (res < 0)
18431 		return (-1);
18432 	return (0);
18433 }
18434 
18435 /*
18436  * This function is called on the unplumb path via ill_glist_delete() when
18437  * there are no ills left on the phyint and thus the phyint can be freed.
18438  */
18439 static void
18440 phyint_free(phyint_t *phyi)
18441 {
18442 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18443 
18444 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18445 
18446 	/*
18447 	 * If this phyint was an IPMP meta-interface, blow away the group.
18448 	 * This is safe to do because all of the illgrps have already been
18449 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18450 	 * If we're cleaning up as a result of failed initialization,
18451 	 * phyint_grp may be NULL.
18452 	 */
18453 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18454 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18455 		ipmp_grp_destroy(phyi->phyint_grp);
18456 		phyi->phyint_grp = NULL;
18457 		rw_exit(&ipst->ips_ipmp_lock);
18458 	}
18459 
18460 	/*
18461 	 * If this interface was under IPMP, take it out of the group.
18462 	 */
18463 	if (phyi->phyint_grp != NULL)
18464 		ipmp_phyint_leave_grp(phyi);
18465 
18466 	/*
18467 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18468 	 * will be freed in ipsq_exit().
18469 	 */
18470 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18471 	phyi->phyint_name[0] = '\0';
18472 
18473 	mi_free(phyi);
18474 }
18475 
18476 /*
18477  * Attach the ill to the phyint structure which can be shared by both
18478  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18479  * function is called from ipif_set_values and ill_lookup_on_name (for
18480  * loopback) where we know the name of the ill. We lookup the ill and if
18481  * there is one present already with the name use that phyint. Otherwise
18482  * reuse the one allocated by ill_init.
18483  */
18484 static void
18485 ill_phyint_reinit(ill_t *ill)
18486 {
18487 	boolean_t isv6 = ill->ill_isv6;
18488 	phyint_t *phyi_old;
18489 	phyint_t *phyi;
18490 	avl_index_t where = 0;
18491 	ill_t	*ill_other = NULL;
18492 	ip_stack_t	*ipst = ill->ill_ipst;
18493 
18494 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18495 
18496 	phyi_old = ill->ill_phyint;
18497 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18498 	    phyi_old->phyint_illv6 == NULL));
18499 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18500 	    phyi_old->phyint_illv4 == NULL));
18501 	ASSERT(phyi_old->phyint_ifindex == 0);
18502 
18503 	/*
18504 	 * Now that our ill has a name, set it in the phyint.
18505 	 */
18506 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18507 
18508 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18509 	    ill->ill_name, &where);
18510 
18511 	/*
18512 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18513 	 *    the global list of ills. So no other thread could have located
18514 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18515 	 * 2. Now locate the other protocol instance of this ill.
18516 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18517 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18518 	 *    of neither ill can change.
18519 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18520 	 *    other ill.
18521 	 * 5. Release all locks.
18522 	 */
18523 
18524 	/*
18525 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18526 	 * we are initializing IPv4.
18527 	 */
18528 	if (phyi != NULL) {
18529 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18530 		ASSERT(ill_other->ill_phyint != NULL);
18531 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18532 		    (!isv6 && ill_other->ill_isv6));
18533 		GRAB_ILL_LOCKS(ill, ill_other);
18534 		/*
18535 		 * We are potentially throwing away phyint_flags which
18536 		 * could be different from the one that we obtain from
18537 		 * ill_other->ill_phyint. But it is okay as we are assuming
18538 		 * that the state maintained within IP is correct.
18539 		 */
18540 		mutex_enter(&phyi->phyint_lock);
18541 		if (isv6) {
18542 			ASSERT(phyi->phyint_illv6 == NULL);
18543 			phyi->phyint_illv6 = ill;
18544 		} else {
18545 			ASSERT(phyi->phyint_illv4 == NULL);
18546 			phyi->phyint_illv4 = ill;
18547 		}
18548 
18549 		/*
18550 		 * Delete the old phyint and make its ipsq eligible
18551 		 * to be freed in ipsq_exit().
18552 		 */
18553 		phyi_old->phyint_illv4 = NULL;
18554 		phyi_old->phyint_illv6 = NULL;
18555 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18556 		phyi_old->phyint_name[0] = '\0';
18557 		mi_free(phyi_old);
18558 	} else {
18559 		mutex_enter(&ill->ill_lock);
18560 		/*
18561 		 * We don't need to acquire any lock, since
18562 		 * the ill is not yet visible globally  and we
18563 		 * have not yet released the ill_g_lock.
18564 		 */
18565 		phyi = phyi_old;
18566 		mutex_enter(&phyi->phyint_lock);
18567 		/* XXX We need a recovery strategy here. */
18568 		if (!phyint_assign_ifindex(phyi, ipst))
18569 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18570 
18571 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18572 		    (void *)phyi, where);
18573 
18574 		(void) avl_find(&ipst->ips_phyint_g_list->
18575 		    phyint_list_avl_by_index,
18576 		    &phyi->phyint_ifindex, &where);
18577 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18578 		    (void *)phyi, where);
18579 	}
18580 
18581 	/*
18582 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18583 	 * pending mp is not affected because that is per ill basis.
18584 	 */
18585 	ill->ill_phyint = phyi;
18586 
18587 	/*
18588 	 * Now that the phyint's ifindex has been assigned, complete the
18589 	 * remaining
18590 	 */
18591 
18592 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18593 	if (ill->ill_isv6) {
18594 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18595 		    ill->ill_phyint->phyint_ifindex;
18596 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18597 	} else {
18598 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18599 	}
18600 
18601 	/*
18602 	 * Generate an event within the hooks framework to indicate that
18603 	 * a new interface has just been added to IP.  For this event to
18604 	 * be generated, the network interface must, at least, have an
18605 	 * ifindex assigned to it.
18606 	 *
18607 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18608 	 * that the ordering of delivered events to listeners matches the
18609 	 * order of them in the kernel.
18610 	 *
18611 	 * This function could be called from ill_lookup_on_name. In that case
18612 	 * the interface is loopback "lo", which will not generate a NIC event.
18613 	 */
18614 	if (ill->ill_name_length <= 2 ||
18615 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
18616 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18617 		    ill->ill_name_length);
18618 	}
18619 	RELEASE_ILL_LOCKS(ill, ill_other);
18620 	mutex_exit(&phyi->phyint_lock);
18621 }
18622 
18623 /*
18624  * Notify any downstream modules of the name of this interface.
18625  * An M_IOCTL is used even though we don't expect a successful reply.
18626  * Any reply message from the driver (presumably an M_IOCNAK) will
18627  * eventually get discarded somewhere upstream.  The message format is
18628  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18629  * to IP.
18630  */
18631 static void
18632 ip_ifname_notify(ill_t *ill, queue_t *q)
18633 {
18634 	mblk_t *mp1, *mp2;
18635 	struct iocblk *iocp;
18636 	struct lifreq *lifr;
18637 
18638 	mp1 = mkiocb(SIOCSLIFNAME);
18639 	if (mp1 == NULL)
18640 		return;
18641 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18642 	if (mp2 == NULL) {
18643 		freeb(mp1);
18644 		return;
18645 	}
18646 
18647 	mp1->b_cont = mp2;
18648 	iocp = (struct iocblk *)mp1->b_rptr;
18649 	iocp->ioc_count = sizeof (struct lifreq);
18650 
18651 	lifr = (struct lifreq *)mp2->b_rptr;
18652 	mp2->b_wptr += sizeof (struct lifreq);
18653 	bzero(lifr, sizeof (struct lifreq));
18654 
18655 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18656 	lifr->lifr_ppa = ill->ill_ppa;
18657 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18658 
18659 	putnext(q, mp1);
18660 }
18661 
18662 static int
18663 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18664 {
18665 	int		err;
18666 	ip_stack_t	*ipst = ill->ill_ipst;
18667 	phyint_t	*phyi = ill->ill_phyint;
18668 
18669 	/* Set the obsolete NDD per-interface forwarding name. */
18670 	err = ill_set_ndd_name(ill);
18671 	if (err != 0) {
18672 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18673 		    err);
18674 	}
18675 
18676 	/*
18677 	 * Now that ill_name is set, the configuration for the IPMP
18678 	 * meta-interface can be performed.
18679 	 */
18680 	if (IS_IPMP(ill)) {
18681 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18682 		/*
18683 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18684 		 * meta-interface and we need to create the IPMP group.
18685 		 */
18686 		if (phyi->phyint_grp == NULL) {
18687 			/*
18688 			 * If someone has renamed another IPMP group to have
18689 			 * the same name as our interface, bail.
18690 			 */
18691 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18692 				rw_exit(&ipst->ips_ipmp_lock);
18693 				return (EEXIST);
18694 			}
18695 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18696 			if (phyi->phyint_grp == NULL) {
18697 				rw_exit(&ipst->ips_ipmp_lock);
18698 				return (ENOMEM);
18699 			}
18700 		}
18701 		rw_exit(&ipst->ips_ipmp_lock);
18702 	}
18703 
18704 	/* Tell downstream modules where they are. */
18705 	ip_ifname_notify(ill, q);
18706 
18707 	/*
18708 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18709 	 * Error cases are ENOMEM ...
18710 	 */
18711 	err = ill_dl_phys(ill, ipif, mp, q);
18712 
18713 	/*
18714 	 * If there is no IRE expiration timer running, get one started.
18715 	 * igmp and mld timers will be triggered by the first multicast
18716 	 */
18717 	if (ipst->ips_ip_ire_expire_id == 0) {
18718 		/*
18719 		 * acquire the lock and check again.
18720 		 */
18721 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18722 		if (ipst->ips_ip_ire_expire_id == 0) {
18723 			ipst->ips_ip_ire_expire_id = timeout(
18724 			    ip_trash_timer_expire, ipst,
18725 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18726 		}
18727 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18728 	}
18729 
18730 	if (ill->ill_isv6) {
18731 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18732 		if (ipst->ips_mld_slowtimeout_id == 0) {
18733 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18734 			    (void *)ipst,
18735 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18736 		}
18737 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18738 	} else {
18739 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18740 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18741 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18742 			    (void *)ipst,
18743 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18744 		}
18745 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18746 	}
18747 
18748 	return (err);
18749 }
18750 
18751 /*
18752  * Common routine for ppa and ifname setting. Should be called exclusive.
18753  *
18754  * Returns EINPROGRESS when mp has been consumed by queueing it on
18755  * ill_pending_mp and the ioctl will complete in ip_rput.
18756  *
18757  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18758  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18759  * For SLIFNAME, we pass these values back to the userland.
18760  */
18761 static int
18762 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18763 {
18764 	ill_t	*ill;
18765 	ipif_t	*ipif;
18766 	ipsq_t	*ipsq;
18767 	char	*ppa_ptr;
18768 	char	*old_ptr;
18769 	char	old_char;
18770 	int	error;
18771 	ip_stack_t	*ipst;
18772 
18773 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18774 	ASSERT(q->q_next != NULL);
18775 	ASSERT(interf_name != NULL);
18776 
18777 	ill = (ill_t *)q->q_ptr;
18778 	ipst = ill->ill_ipst;
18779 
18780 	ASSERT(ill->ill_ipst != NULL);
18781 	ASSERT(ill->ill_name[0] == '\0');
18782 	ASSERT(IAM_WRITER_ILL(ill));
18783 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18784 	ASSERT(ill->ill_ppa == UINT_MAX);
18785 
18786 	/* The ppa is sent down by ifconfig or is chosen */
18787 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18788 		return (EINVAL);
18789 	}
18790 
18791 	/*
18792 	 * make sure ppa passed in is same as ppa in the name.
18793 	 * This check is not made when ppa == UINT_MAX in that case ppa
18794 	 * in the name could be anything. System will choose a ppa and
18795 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18796 	 */
18797 	if (*new_ppa_ptr != UINT_MAX) {
18798 		/* stoi changes the pointer */
18799 		old_ptr = ppa_ptr;
18800 		/*
18801 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18802 		 * (they don't have an externally visible ppa).  We assign one
18803 		 * here so that we can manage the interface.  Note that in
18804 		 * the past this value was always 0 for DLPI 1 drivers.
18805 		 */
18806 		if (*new_ppa_ptr == 0)
18807 			*new_ppa_ptr = stoi(&old_ptr);
18808 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18809 			return (EINVAL);
18810 	}
18811 	/*
18812 	 * terminate string before ppa
18813 	 * save char at that location.
18814 	 */
18815 	old_char = ppa_ptr[0];
18816 	ppa_ptr[0] = '\0';
18817 
18818 	ill->ill_ppa = *new_ppa_ptr;
18819 	/*
18820 	 * Finish as much work now as possible before calling ill_glist_insert
18821 	 * which makes the ill globally visible and also merges it with the
18822 	 * other protocol instance of this phyint. The remaining work is
18823 	 * done after entering the ipsq which may happen sometime later.
18824 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18825 	 */
18826 	ipif = ill->ill_ipif;
18827 
18828 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18829 	ipif_assign_seqid(ipif);
18830 
18831 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18832 		ill->ill_flags |= ILLF_IPV4;
18833 
18834 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18835 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18836 
18837 	if (ill->ill_flags & ILLF_IPV6) {
18838 
18839 		ill->ill_isv6 = B_TRUE;
18840 		if (ill->ill_rq != NULL) {
18841 			ill->ill_rq->q_qinfo = &iprinitv6;
18842 			ill->ill_wq->q_qinfo = &ipwinitv6;
18843 		}
18844 
18845 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18846 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18847 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18848 		ipif->ipif_v6subnet = ipv6_all_zeros;
18849 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18850 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18851 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18852 		/*
18853 		 * point-to-point or Non-mulicast capable
18854 		 * interfaces won't do NUD unless explicitly
18855 		 * configured to do so.
18856 		 */
18857 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18858 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18859 			ill->ill_flags |= ILLF_NONUD;
18860 		}
18861 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18862 		if (ill->ill_flags & ILLF_NOARP) {
18863 			/*
18864 			 * Note: xresolv interfaces will eventually need
18865 			 * NOARP set here as well, but that will require
18866 			 * those external resolvers to have some
18867 			 * knowledge of that flag and act appropriately.
18868 			 * Not to be changed at present.
18869 			 */
18870 			ill->ill_flags &= ~ILLF_NOARP;
18871 		}
18872 		/*
18873 		 * Set the ILLF_ROUTER flag according to the global
18874 		 * IPv6 forwarding policy.
18875 		 */
18876 		if (ipst->ips_ipv6_forward != 0)
18877 			ill->ill_flags |= ILLF_ROUTER;
18878 	} else if (ill->ill_flags & ILLF_IPV4) {
18879 		ill->ill_isv6 = B_FALSE;
18880 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18881 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18882 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18883 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18884 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18885 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18886 		/*
18887 		 * Set the ILLF_ROUTER flag according to the global
18888 		 * IPv4 forwarding policy.
18889 		 */
18890 		if (ipst->ips_ip_g_forward != 0)
18891 			ill->ill_flags |= ILLF_ROUTER;
18892 	}
18893 
18894 	ASSERT(ill->ill_phyint != NULL);
18895 
18896 	/*
18897 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18898 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18899 	 */
18900 	if (!ill_allocate_mibs(ill))
18901 		return (ENOMEM);
18902 
18903 	/*
18904 	 * Pick a default sap until we get the DL_INFO_ACK back from
18905 	 * the driver.
18906 	 */
18907 	if (ill->ill_sap == 0) {
18908 		if (ill->ill_isv6)
18909 			ill->ill_sap = IP6_DL_SAP;
18910 		else
18911 			ill->ill_sap = IP_DL_SAP;
18912 	}
18913 
18914 	ill->ill_ifname_pending = 1;
18915 	ill->ill_ifname_pending_err = 0;
18916 
18917 	/*
18918 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18919 	 * that were joined while this ill was not bound to the DLPI link need
18920 	 * to be recovered by ill_recover_multicast().
18921 	 */
18922 	ill->ill_need_recover_multicast = 1;
18923 
18924 	ill_refhold(ill);
18925 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18926 	if ((error = ill_glist_insert(ill, interf_name,
18927 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18928 		ill->ill_ppa = UINT_MAX;
18929 		ill->ill_name[0] = '\0';
18930 		/*
18931 		 * undo null termination done above.
18932 		 */
18933 		ppa_ptr[0] = old_char;
18934 		rw_exit(&ipst->ips_ill_g_lock);
18935 		ill_refrele(ill);
18936 		return (error);
18937 	}
18938 
18939 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18940 
18941 	/*
18942 	 * When we return the buffer pointed to by interf_name should contain
18943 	 * the same name as in ill_name.
18944 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18945 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18946 	 * so copy full name and update the ppa ptr.
18947 	 * When ppa passed in != UINT_MAX all values are correct just undo
18948 	 * null termination, this saves a bcopy.
18949 	 */
18950 	if (*new_ppa_ptr == UINT_MAX) {
18951 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18952 		*new_ppa_ptr = ill->ill_ppa;
18953 	} else {
18954 		/*
18955 		 * undo null termination done above.
18956 		 */
18957 		ppa_ptr[0] = old_char;
18958 	}
18959 
18960 	/* Let SCTP know about this ILL */
18961 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18962 
18963 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18964 	    B_TRUE);
18965 
18966 	rw_exit(&ipst->ips_ill_g_lock);
18967 	ill_refrele(ill);
18968 	if (ipsq == NULL)
18969 		return (EINPROGRESS);
18970 
18971 	/*
18972 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18973 	 */
18974 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18975 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18976 	else
18977 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18978 
18979 	error = ipif_set_values_tail(ill, ipif, mp, q);
18980 	ipsq_exit(ipsq);
18981 	if (error != 0 && error != EINPROGRESS) {
18982 		/*
18983 		 * restore previous values
18984 		 */
18985 		ill->ill_isv6 = B_FALSE;
18986 	}
18987 	return (error);
18988 }
18989 
18990 
18991 void
18992 ipif_init(ip_stack_t *ipst)
18993 {
18994 	int i;
18995 
18996 	for (i = 0; i < MAX_G_HEADS; i++) {
18997 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18998 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18999 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
19000 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
19001 	}
19002 
19003 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
19004 	    ill_phyint_compare_index,
19005 	    sizeof (phyint_t),
19006 	    offsetof(struct phyint, phyint_avl_by_index));
19007 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
19008 	    ill_phyint_compare_name,
19009 	    sizeof (phyint_t),
19010 	    offsetof(struct phyint, phyint_avl_by_name));
19011 }
19012 
19013 /*
19014  * Lookup the ipif corresponding to the onlink destination address. For
19015  * point-to-point interfaces, it matches with remote endpoint destination
19016  * address. For point-to-multipoint interfaces it only tries to match the
19017  * destination with the interface's subnet address. The longest, most specific
19018  * match is found to take care of such rare network configurations like -
19019  * le0: 129.146.1.1/16
19020  * le1: 129.146.2.2/24
19021  *
19022  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
19023  * supported on underlying interfaces in an IPMP group, underlying interfaces
19024  * are ignored when looking up a match.  (If we didn't ignore them, we'd
19025  * risk using a test address as a source for outgoing traffic.)
19026  */
19027 ipif_t *
19028 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
19029 {
19030 	ipif_t	*ipif, *best_ipif;
19031 	ill_t	*ill;
19032 	ill_walk_context_t ctx;
19033 
19034 	ASSERT(zoneid != ALL_ZONES);
19035 	best_ipif = NULL;
19036 
19037 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
19038 	ill = ILL_START_WALK_V4(&ctx, ipst);
19039 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19040 		if (IS_UNDER_IPMP(ill))
19041 			continue;
19042 		mutex_enter(&ill->ill_lock);
19043 		for (ipif = ill->ill_ipif; ipif != NULL;
19044 		    ipif = ipif->ipif_next) {
19045 			if (!IPIF_CAN_LOOKUP(ipif))
19046 				continue;
19047 			if (ipif->ipif_zoneid != zoneid &&
19048 			    ipif->ipif_zoneid != ALL_ZONES)
19049 				continue;
19050 			/*
19051 			 * Point-to-point case. Look for exact match with
19052 			 * destination address.
19053 			 */
19054 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19055 				if (ipif->ipif_pp_dst_addr == addr) {
19056 					ipif_refhold_locked(ipif);
19057 					mutex_exit(&ill->ill_lock);
19058 					rw_exit(&ipst->ips_ill_g_lock);
19059 					if (best_ipif != NULL)
19060 						ipif_refrele(best_ipif);
19061 					return (ipif);
19062 				}
19063 			} else if (ipif->ipif_subnet == (addr &
19064 			    ipif->ipif_net_mask)) {
19065 				/*
19066 				 * Point-to-multipoint case. Looping through to
19067 				 * find the most specific match. If there are
19068 				 * multiple best match ipif's then prefer ipif's
19069 				 * that are UP. If there is only one best match
19070 				 * ipif and it is DOWN we must still return it.
19071 				 */
19072 				if ((best_ipif == NULL) ||
19073 				    (ipif->ipif_net_mask >
19074 				    best_ipif->ipif_net_mask) ||
19075 				    ((ipif->ipif_net_mask ==
19076 				    best_ipif->ipif_net_mask) &&
19077 				    ((ipif->ipif_flags & IPIF_UP) &&
19078 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
19079 					ipif_refhold_locked(ipif);
19080 					mutex_exit(&ill->ill_lock);
19081 					rw_exit(&ipst->ips_ill_g_lock);
19082 					if (best_ipif != NULL)
19083 						ipif_refrele(best_ipif);
19084 					best_ipif = ipif;
19085 					rw_enter(&ipst->ips_ill_g_lock,
19086 					    RW_READER);
19087 					mutex_enter(&ill->ill_lock);
19088 				}
19089 			}
19090 		}
19091 		mutex_exit(&ill->ill_lock);
19092 	}
19093 	rw_exit(&ipst->ips_ill_g_lock);
19094 	return (best_ipif);
19095 }
19096 
19097 /*
19098  * Save enough information so that we can recreate the IRE if
19099  * the interface goes down and then up.
19100  */
19101 static void
19102 ipif_save_ire(ipif_t *ipif, ire_t *ire)
19103 {
19104 	mblk_t	*save_mp;
19105 
19106 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
19107 	if (save_mp != NULL) {
19108 		ifrt_t	*ifrt;
19109 
19110 		save_mp->b_wptr += sizeof (ifrt_t);
19111 		ifrt = (ifrt_t *)save_mp->b_rptr;
19112 		bzero(ifrt, sizeof (ifrt_t));
19113 		ifrt->ifrt_type = ire->ire_type;
19114 		ifrt->ifrt_addr = ire->ire_addr;
19115 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
19116 		ifrt->ifrt_src_addr = ire->ire_src_addr;
19117 		ifrt->ifrt_mask = ire->ire_mask;
19118 		ifrt->ifrt_flags = ire->ire_flags;
19119 		ifrt->ifrt_max_frag = ire->ire_max_frag;
19120 		mutex_enter(&ipif->ipif_saved_ire_lock);
19121 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
19122 		ipif->ipif_saved_ire_mp = save_mp;
19123 		ipif->ipif_saved_ire_cnt++;
19124 		mutex_exit(&ipif->ipif_saved_ire_lock);
19125 	}
19126 }
19127 
19128 static void
19129 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
19130 {
19131 	mblk_t	**mpp;
19132 	mblk_t	*mp;
19133 	ifrt_t	*ifrt;
19134 
19135 	/* Remove from ipif_saved_ire_mp list if it is there */
19136 	mutex_enter(&ipif->ipif_saved_ire_lock);
19137 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
19138 	    mpp = &(*mpp)->b_cont) {
19139 		/*
19140 		 * On a given ipif, the triple of address, gateway and
19141 		 * mask is unique for each saved IRE (in the case of
19142 		 * ordinary interface routes, the gateway address is
19143 		 * all-zeroes).
19144 		 */
19145 		mp = *mpp;
19146 		ifrt = (ifrt_t *)mp->b_rptr;
19147 		if (ifrt->ifrt_addr == ire->ire_addr &&
19148 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
19149 		    ifrt->ifrt_mask == ire->ire_mask) {
19150 			*mpp = mp->b_cont;
19151 			ipif->ipif_saved_ire_cnt--;
19152 			freeb(mp);
19153 			break;
19154 		}
19155 	}
19156 	mutex_exit(&ipif->ipif_saved_ire_lock);
19157 }
19158 
19159 /*
19160  * IP multirouting broadcast routes handling
19161  * Append CGTP broadcast IREs to regular ones created
19162  * at ifconfig time.
19163  */
19164 static void
19165 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
19166 {
19167 	ire_t *ire_prim;
19168 
19169 	ASSERT(ire != NULL);
19170 	ASSERT(ire_dst != NULL);
19171 
19172 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19173 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19174 	if (ire_prim != NULL) {
19175 		/*
19176 		 * We are in the special case of broadcasts for
19177 		 * CGTP. We add an IRE_BROADCAST that holds
19178 		 * the RTF_MULTIRT flag, the destination
19179 		 * address of ire_dst and the low level
19180 		 * info of ire_prim. In other words, CGTP
19181 		 * broadcast is added to the redundant ipif.
19182 		 */
19183 		ipif_t *ipif_prim;
19184 		ire_t  *bcast_ire;
19185 
19186 		ipif_prim = ire_prim->ire_ipif;
19187 
19188 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19189 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19190 		    (void *)ire_dst, (void *)ire_prim,
19191 		    (void *)ipif_prim));
19192 
19193 		bcast_ire = ire_create(
19194 		    (uchar_t *)&ire->ire_addr,
19195 		    (uchar_t *)&ip_g_all_ones,
19196 		    (uchar_t *)&ire_dst->ire_src_addr,
19197 		    (uchar_t *)&ire->ire_gateway_addr,
19198 		    &ipif_prim->ipif_mtu,
19199 		    NULL,
19200 		    ipif_prim->ipif_rq,
19201 		    ipif_prim->ipif_wq,
19202 		    IRE_BROADCAST,
19203 		    ipif_prim,
19204 		    0,
19205 		    0,
19206 		    0,
19207 		    ire->ire_flags,
19208 		    &ire_uinfo_null,
19209 		    NULL,
19210 		    NULL,
19211 		    ipst);
19212 
19213 		if (bcast_ire != NULL) {
19214 
19215 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19216 			    B_FALSE) == 0) {
19217 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19218 				    "added bcast_ire %p\n",
19219 				    (void *)bcast_ire));
19220 
19221 				ipif_save_ire(bcast_ire->ire_ipif,
19222 				    bcast_ire);
19223 				ire_refrele(bcast_ire);
19224 			}
19225 		}
19226 		ire_refrele(ire_prim);
19227 	}
19228 }
19229 
19230 
19231 /*
19232  * IP multirouting broadcast routes handling
19233  * Remove the broadcast ire
19234  */
19235 static void
19236 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19237 {
19238 	ire_t *ire_dst;
19239 
19240 	ASSERT(ire != NULL);
19241 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19242 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19243 	if (ire_dst != NULL) {
19244 		ire_t *ire_prim;
19245 
19246 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19247 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19248 		if (ire_prim != NULL) {
19249 			ipif_t *ipif_prim;
19250 			ire_t  *bcast_ire;
19251 
19252 			ipif_prim = ire_prim->ire_ipif;
19253 
19254 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19255 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19256 			    (void *)ire_dst, (void *)ire_prim,
19257 			    (void *)ipif_prim));
19258 
19259 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19260 			    ire->ire_gateway_addr,
19261 			    IRE_BROADCAST,
19262 			    ipif_prim, ALL_ZONES,
19263 			    NULL,
19264 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19265 			    MATCH_IRE_MASK, ipst);
19266 
19267 			if (bcast_ire != NULL) {
19268 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19269 				    "looked up bcast_ire %p\n",
19270 				    (void *)bcast_ire));
19271 				ipif_remove_ire(bcast_ire->ire_ipif,
19272 				    bcast_ire);
19273 				ire_delete(bcast_ire);
19274 				ire_refrele(bcast_ire);
19275 			}
19276 			ire_refrele(ire_prim);
19277 		}
19278 		ire_refrele(ire_dst);
19279 	}
19280 }
19281 
19282 /*
19283  * IPsec hardware acceleration capabilities related functions.
19284  */
19285 
19286 /*
19287  * Free a per-ill IPsec capabilities structure.
19288  */
19289 static void
19290 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19291 {
19292 	if (capab->auth_hw_algs != NULL)
19293 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19294 	if (capab->encr_hw_algs != NULL)
19295 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19296 	if (capab->encr_algparm != NULL)
19297 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19298 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19299 }
19300 
19301 /*
19302  * Allocate a new per-ill IPsec capabilities structure. This structure
19303  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19304  * an array which specifies, for each algorithm, whether this algorithm
19305  * is supported by the ill or not.
19306  */
19307 static ill_ipsec_capab_t *
19308 ill_ipsec_capab_alloc(void)
19309 {
19310 	ill_ipsec_capab_t *capab;
19311 	uint_t nelems;
19312 
19313 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19314 	if (capab == NULL)
19315 		return (NULL);
19316 
19317 	/* we need one bit per algorithm */
19318 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19319 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19320 
19321 	/* allocate memory to store algorithm flags */
19322 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19323 	if (capab->encr_hw_algs == NULL)
19324 		goto nomem;
19325 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19326 	if (capab->auth_hw_algs == NULL)
19327 		goto nomem;
19328 	/*
19329 	 * Leave encr_algparm NULL for now since we won't need it half
19330 	 * the time
19331 	 */
19332 	return (capab);
19333 
19334 nomem:
19335 	ill_ipsec_capab_free(capab);
19336 	return (NULL);
19337 }
19338 
19339 /*
19340  * Resize capability array.  Since we're exclusive, this is OK.
19341  */
19342 static boolean_t
19343 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19344 {
19345 	ipsec_capab_algparm_t *nalp, *oalp;
19346 	uint32_t olen, nlen;
19347 
19348 	oalp = capab->encr_algparm;
19349 	olen = capab->encr_algparm_size;
19350 
19351 	if (oalp != NULL) {
19352 		if (algid < capab->encr_algparm_end)
19353 			return (B_TRUE);
19354 	}
19355 
19356 	nlen = (algid + 1) * sizeof (*nalp);
19357 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19358 	if (nalp == NULL)
19359 		return (B_FALSE);
19360 
19361 	if (oalp != NULL) {
19362 		bcopy(oalp, nalp, olen);
19363 		kmem_free(oalp, olen);
19364 	}
19365 	capab->encr_algparm = nalp;
19366 	capab->encr_algparm_size = nlen;
19367 	capab->encr_algparm_end = algid + 1;
19368 
19369 	return (B_TRUE);
19370 }
19371 
19372 /*
19373  * Compare the capabilities of the specified ill with the protocol
19374  * and algorithms specified by the SA passed as argument.
19375  * If they match, returns B_TRUE, B_FALSE if they do not match.
19376  *
19377  * The ill can be passed as a pointer to it, or by specifying its index
19378  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19379  *
19380  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19381  * packet is eligible for hardware acceleration, and by
19382  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19383  * to a particular ill.
19384  */
19385 boolean_t
19386 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19387     ipsa_t *sa, netstack_t *ns)
19388 {
19389 	boolean_t sa_isv6;
19390 	uint_t algid;
19391 	struct ill_ipsec_capab_s *cpp;
19392 	boolean_t need_refrele = B_FALSE;
19393 	ip_stack_t	*ipst = ns->netstack_ip;
19394 
19395 	if (ill == NULL) {
19396 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19397 		    NULL, NULL, NULL, ipst);
19398 		if (ill == NULL) {
19399 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19400 			return (B_FALSE);
19401 		}
19402 		need_refrele = B_TRUE;
19403 	}
19404 
19405 	/*
19406 	 * Use the address length specified by the SA to determine
19407 	 * if it corresponds to a IPv6 address, and fail the matching
19408 	 * if the isv6 flag passed as argument does not match.
19409 	 * Note: this check is used for SADB capability checking before
19410 	 * sending SA information to an ill.
19411 	 */
19412 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19413 	if (sa_isv6 != ill_isv6)
19414 		/* protocol mismatch */
19415 		goto done;
19416 
19417 	/*
19418 	 * Check if the ill supports the protocol, algorithm(s) and
19419 	 * key size(s) specified by the SA, and get the pointers to
19420 	 * the algorithms supported by the ill.
19421 	 */
19422 	switch (sa->ipsa_type) {
19423 
19424 	case SADB_SATYPE_ESP:
19425 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19426 			/* ill does not support ESP acceleration */
19427 			goto done;
19428 		cpp = ill->ill_ipsec_capab_esp;
19429 		algid = sa->ipsa_auth_alg;
19430 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19431 			goto done;
19432 		algid = sa->ipsa_encr_alg;
19433 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19434 			goto done;
19435 		if (algid < cpp->encr_algparm_end) {
19436 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19437 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19438 				goto done;
19439 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19440 				goto done;
19441 		}
19442 		break;
19443 
19444 	case SADB_SATYPE_AH:
19445 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19446 			/* ill does not support AH acceleration */
19447 			goto done;
19448 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19449 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19450 			goto done;
19451 		break;
19452 	}
19453 
19454 	if (need_refrele)
19455 		ill_refrele(ill);
19456 	return (B_TRUE);
19457 done:
19458 	if (need_refrele)
19459 		ill_refrele(ill);
19460 	return (B_FALSE);
19461 }
19462 
19463 /*
19464  * Add a new ill to the list of IPsec capable ills.
19465  * Called from ill_capability_ipsec_ack() when an ACK was received
19466  * indicating that IPsec hardware processing was enabled for an ill.
19467  *
19468  * ill must point to the ill for which acceleration was enabled.
19469  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19470  */
19471 static void
19472 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19473 {
19474 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19475 	uint_t sa_type;
19476 	uint_t ipproto;
19477 	ip_stack_t	*ipst = ill->ill_ipst;
19478 
19479 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19480 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19481 
19482 	switch (dl_cap) {
19483 	case DL_CAPAB_IPSEC_AH:
19484 		sa_type = SADB_SATYPE_AH;
19485 		ills = &ipst->ips_ipsec_capab_ills_ah;
19486 		ipproto = IPPROTO_AH;
19487 		break;
19488 	case DL_CAPAB_IPSEC_ESP:
19489 		sa_type = SADB_SATYPE_ESP;
19490 		ills = &ipst->ips_ipsec_capab_ills_esp;
19491 		ipproto = IPPROTO_ESP;
19492 		break;
19493 	}
19494 
19495 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19496 
19497 	/*
19498 	 * Add ill index to list of hardware accelerators. If
19499 	 * already in list, do nothing.
19500 	 */
19501 	for (cur_ill = *ills; cur_ill != NULL &&
19502 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19503 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19504 		;
19505 
19506 	if (cur_ill == NULL) {
19507 		/* if this is a new entry for this ill */
19508 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19509 		if (new_ill == NULL) {
19510 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19511 			return;
19512 		}
19513 
19514 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19515 		new_ill->ill_isv6 = ill->ill_isv6;
19516 		new_ill->next = *ills;
19517 		*ills = new_ill;
19518 	} else if (!sadb_resync) {
19519 		/* not resync'ing SADB and an entry exists for this ill */
19520 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19521 		return;
19522 	}
19523 
19524 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19525 
19526 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19527 		/*
19528 		 * IPsec module for protocol loaded, initiate dump
19529 		 * of the SADB to this ill.
19530 		 */
19531 		sadb_ill_download(ill, sa_type);
19532 }
19533 
19534 /*
19535  * Remove an ill from the list of IPsec capable ills.
19536  */
19537 static void
19538 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19539 {
19540 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19541 	ip_stack_t	*ipst = ill->ill_ipst;
19542 
19543 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19544 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19545 
19546 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19547 	    &ipst->ips_ipsec_capab_ills_esp;
19548 
19549 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19550 
19551 	prev_ill = NULL;
19552 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19553 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19554 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19555 		;
19556 	if (cur_ill == NULL) {
19557 		/* entry not found */
19558 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19559 		return;
19560 	}
19561 	if (prev_ill == NULL) {
19562 		/* entry at front of list */
19563 		*ills = NULL;
19564 	} else {
19565 		prev_ill->next = cur_ill->next;
19566 	}
19567 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19568 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19569 }
19570 
19571 /*
19572  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19573  * supporting the specified IPsec protocol acceleration.
19574  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19575  * We free the mblk and, if sa is non-null, release the held referece.
19576  */
19577 void
19578 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19579     netstack_t *ns)
19580 {
19581 	ipsec_capab_ill_t *ici, *cur_ici;
19582 	ill_t *ill;
19583 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19584 	ip_stack_t	*ipst = ns->netstack_ip;
19585 
19586 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19587 	    ipst->ips_ipsec_capab_ills_esp;
19588 
19589 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19590 
19591 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19592 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19593 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19594 
19595 		/*
19596 		 * Handle the case where the ill goes away while the SADB is
19597 		 * attempting to send messages.  If it's going away, it's
19598 		 * nuking its shadow SADB, so we don't care..
19599 		 */
19600 
19601 		if (ill == NULL)
19602 			continue;
19603 
19604 		if (sa != NULL) {
19605 			/*
19606 			 * Make sure capabilities match before
19607 			 * sending SA to ill.
19608 			 */
19609 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19610 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19611 				ill_refrele(ill);
19612 				continue;
19613 			}
19614 
19615 			mutex_enter(&sa->ipsa_lock);
19616 			sa->ipsa_flags |= IPSA_F_HW;
19617 			mutex_exit(&sa->ipsa_lock);
19618 		}
19619 
19620 		/*
19621 		 * Copy template message, and add it to the front
19622 		 * of the mblk ship list. We want to avoid holding
19623 		 * the ipsec_capab_ills_lock while sending the
19624 		 * message to the ills.
19625 		 *
19626 		 * The b_next and b_prev are temporarily used
19627 		 * to build a list of mblks to be sent down, and to
19628 		 * save the ill to which they must be sent.
19629 		 */
19630 		nmp = copymsg(mp);
19631 		if (nmp == NULL) {
19632 			ill_refrele(ill);
19633 			continue;
19634 		}
19635 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19636 		nmp->b_next = mp_ship_list;
19637 		mp_ship_list = nmp;
19638 		nmp->b_prev = (mblk_t *)ill;
19639 	}
19640 
19641 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19642 
19643 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19644 		/* restore the mblk to a sane state */
19645 		next_mp = nmp->b_next;
19646 		nmp->b_next = NULL;
19647 		ill = (ill_t *)nmp->b_prev;
19648 		nmp->b_prev = NULL;
19649 
19650 		ill_dlpi_send(ill, nmp);
19651 		ill_refrele(ill);
19652 	}
19653 
19654 	if (sa != NULL)
19655 		IPSA_REFRELE(sa);
19656 	freemsg(mp);
19657 }
19658 
19659 /*
19660  * Derive an interface id from the link layer address.
19661  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19662  */
19663 static boolean_t
19664 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19665 {
19666 	char		*addr;
19667 
19668 	if (ill->ill_phys_addr_length != ETHERADDRL)
19669 		return (B_FALSE);
19670 
19671 	/* Form EUI-64 like address */
19672 	addr = (char *)&v6addr->s6_addr32[2];
19673 	bcopy(ill->ill_phys_addr, addr, 3);
19674 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19675 	addr[3] = (char)0xff;
19676 	addr[4] = (char)0xfe;
19677 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19678 	return (B_TRUE);
19679 }
19680 
19681 /* ARGSUSED */
19682 static boolean_t
19683 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19684 {
19685 	return (B_FALSE);
19686 }
19687 
19688 typedef struct ipmp_ifcookie {
19689 	uint32_t	ic_hostid;
19690 	char		ic_ifname[LIFNAMSIZ];
19691 	char		ic_zonename[ZONENAME_MAX];
19692 } ipmp_ifcookie_t;
19693 
19694 /*
19695  * Construct a pseudo-random interface ID for the IPMP interface that's both
19696  * predictable and (almost) guaranteed to be unique.
19697  */
19698 static boolean_t
19699 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19700 {
19701 	zone_t		*zp;
19702 	uint8_t		*addr;
19703 	uchar_t		hash[16];
19704 	ulong_t 	hostid;
19705 	MD5_CTX		ctx;
19706 	ipmp_ifcookie_t	ic = { 0 };
19707 
19708 	ASSERT(IS_IPMP(ill));
19709 
19710 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19711 	ic.ic_hostid = htonl((uint32_t)hostid);
19712 
19713 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19714 
19715 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19716 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19717 		zone_rele(zp);
19718 	}
19719 
19720 	MD5Init(&ctx);
19721 	MD5Update(&ctx, &ic, sizeof (ic));
19722 	MD5Final(hash, &ctx);
19723 
19724 	/*
19725 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19726 	 */
19727 	addr = &v6addr->s6_addr8[8];
19728 	bcopy(hash + 8, addr, sizeof (uint64_t));
19729 	addr[0] &= ~0x2;				/* set local bit */
19730 
19731 	return (B_TRUE);
19732 }
19733 
19734 /* ARGSUSED */
19735 static boolean_t
19736 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19737     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19738 {
19739 	/*
19740 	 * Multicast address mappings used over Ethernet/802.X.
19741 	 * This address is used as a base for mappings.
19742 	 */
19743 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19744 	    0x00, 0x00, 0x00};
19745 
19746 	/*
19747 	 * Extract low order 32 bits from IPv6 multicast address.
19748 	 * Or that into the link layer address, starting from the
19749 	 * second byte.
19750 	 */
19751 	*hw_start = 2;
19752 	v6_extract_mask->s6_addr32[0] = 0;
19753 	v6_extract_mask->s6_addr32[1] = 0;
19754 	v6_extract_mask->s6_addr32[2] = 0;
19755 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19756 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19757 	return (B_TRUE);
19758 }
19759 
19760 /*
19761  * Indicate by return value whether multicast is supported. If not,
19762  * this code should not touch/change any parameters.
19763  */
19764 /* ARGSUSED */
19765 static boolean_t
19766 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19767     uint32_t *hw_start, ipaddr_t *extract_mask)
19768 {
19769 	/*
19770 	 * Multicast address mappings used over Ethernet/802.X.
19771 	 * This address is used as a base for mappings.
19772 	 */
19773 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19774 	    0x00, 0x00, 0x00 };
19775 
19776 	if (phys_length != ETHERADDRL)
19777 		return (B_FALSE);
19778 
19779 	*extract_mask = htonl(0x007fffff);
19780 	*hw_start = 2;
19781 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19782 	return (B_TRUE);
19783 }
19784 
19785 /*
19786  * Derive IPoIB interface id from the link layer address.
19787  */
19788 static boolean_t
19789 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19790 {
19791 	char		*addr;
19792 
19793 	if (ill->ill_phys_addr_length != 20)
19794 		return (B_FALSE);
19795 	addr = (char *)&v6addr->s6_addr32[2];
19796 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19797 	/*
19798 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19799 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19800 	 * rules. In these cases, the IBA considers these GUIDs to be in
19801 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19802 	 * required; vendors are required not to assign global EUI-64's
19803 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19804 	 * of the interface identifier. Whether the GUID is in modified
19805 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19806 	 * bit set to 1.
19807 	 */
19808 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19809 	return (B_TRUE);
19810 }
19811 
19812 /*
19813  * Note on mapping from multicast IP addresses to IPoIB multicast link
19814  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19815  * The format of an IPoIB multicast address is:
19816  *
19817  *  4 byte QPN      Scope Sign.  Pkey
19818  * +--------------------------------------------+
19819  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19820  * +--------------------------------------------+
19821  *
19822  * The Scope and Pkey components are properties of the IBA port and
19823  * network interface. They can be ascertained from the broadcast address.
19824  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19825  */
19826 
19827 static boolean_t
19828 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19829     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19830 {
19831 	/*
19832 	 * Base IPoIB IPv6 multicast address used for mappings.
19833 	 * Does not contain the IBA scope/Pkey values.
19834 	 */
19835 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19836 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19837 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19838 
19839 	/*
19840 	 * Extract low order 80 bits from IPv6 multicast address.
19841 	 * Or that into the link layer address, starting from the
19842 	 * sixth byte.
19843 	 */
19844 	*hw_start = 6;
19845 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19846 
19847 	/*
19848 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19849 	 */
19850 	*(maddr + 5) = *(bphys_addr + 5);
19851 	*(maddr + 8) = *(bphys_addr + 8);
19852 	*(maddr + 9) = *(bphys_addr + 9);
19853 
19854 	v6_extract_mask->s6_addr32[0] = 0;
19855 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19856 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19857 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19858 	return (B_TRUE);
19859 }
19860 
19861 static boolean_t
19862 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19863     uint32_t *hw_start, ipaddr_t *extract_mask)
19864 {
19865 	/*
19866 	 * Base IPoIB IPv4 multicast address used for mappings.
19867 	 * Does not contain the IBA scope/Pkey values.
19868 	 */
19869 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19870 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19871 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19872 
19873 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19874 		return (B_FALSE);
19875 
19876 	/*
19877 	 * Extract low order 28 bits from IPv4 multicast address.
19878 	 * Or that into the link layer address, starting from the
19879 	 * sixteenth byte.
19880 	 */
19881 	*extract_mask = htonl(0x0fffffff);
19882 	*hw_start = 16;
19883 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19884 
19885 	/*
19886 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19887 	 */
19888 	*(maddr + 5) = *(bphys_addr + 5);
19889 	*(maddr + 8) = *(bphys_addr + 8);
19890 	*(maddr + 9) = *(bphys_addr + 9);
19891 	return (B_TRUE);
19892 }
19893 
19894 /*
19895  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19896  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19897  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19898  * the link-local address is preferred.
19899  */
19900 boolean_t
19901 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19902 {
19903 	ipif_t	*ipif;
19904 	ipif_t	*maybe_ipif = NULL;
19905 
19906 	mutex_enter(&ill->ill_lock);
19907 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19908 		mutex_exit(&ill->ill_lock);
19909 		if (ipifp != NULL)
19910 			*ipifp = NULL;
19911 		return (B_FALSE);
19912 	}
19913 
19914 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19915 		if (!IPIF_CAN_LOOKUP(ipif))
19916 			continue;
19917 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19918 		    ipif->ipif_zoneid != ALL_ZONES)
19919 			continue;
19920 		if ((ipif->ipif_flags & flags) != flags)
19921 			continue;
19922 
19923 		if (ipifp == NULL) {
19924 			mutex_exit(&ill->ill_lock);
19925 			ASSERT(maybe_ipif == NULL);
19926 			return (B_TRUE);
19927 		}
19928 		if (!ill->ill_isv6 ||
19929 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19930 			ipif_refhold_locked(ipif);
19931 			mutex_exit(&ill->ill_lock);
19932 			*ipifp = ipif;
19933 			return (B_TRUE);
19934 		}
19935 		if (maybe_ipif == NULL)
19936 			maybe_ipif = ipif;
19937 	}
19938 	if (ipifp != NULL) {
19939 		if (maybe_ipif != NULL)
19940 			ipif_refhold_locked(maybe_ipif);
19941 		*ipifp = maybe_ipif;
19942 	}
19943 	mutex_exit(&ill->ill_lock);
19944 	return (maybe_ipif != NULL);
19945 }
19946 
19947 /*
19948  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19949  * If a pointer to an ipif_t is returned then the caller will need to do
19950  * an ill_refrele().
19951  */
19952 ipif_t *
19953 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19954     ip_stack_t *ipst)
19955 {
19956 	ipif_t *ipif;
19957 	ill_t *ill;
19958 
19959 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19960 	    ipst);
19961 	if (ill == NULL)
19962 		return (NULL);
19963 
19964 	mutex_enter(&ill->ill_lock);
19965 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19966 		mutex_exit(&ill->ill_lock);
19967 		ill_refrele(ill);
19968 		return (NULL);
19969 	}
19970 
19971 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19972 		if (!IPIF_CAN_LOOKUP(ipif))
19973 			continue;
19974 		if (lifidx == ipif->ipif_id) {
19975 			ipif_refhold_locked(ipif);
19976 			break;
19977 		}
19978 	}
19979 
19980 	mutex_exit(&ill->ill_lock);
19981 	ill_refrele(ill);
19982 	return (ipif);
19983 }
19984 
19985 /*
19986  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19987  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19988  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19989  * for details.
19990  */
19991 void
19992 ill_fastpath_flush(ill_t *ill)
19993 {
19994 	ip_stack_t *ipst = ill->ill_ipst;
19995 
19996 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19997 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19998 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19999 }
20000 
20001 /*
20002  * Set the physical address information for `ill' to the contents of the
20003  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
20004  * asynchronous if `ill' cannot immediately be quiesced -- in which case
20005  * EINPROGRESS will be returned.
20006  */
20007 int
20008 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
20009 {
20010 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
20011 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
20012 
20013 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20014 
20015 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
20016 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
20017 		/* Changing DL_IPV6_TOKEN is not yet supported */
20018 		return (0);
20019 	}
20020 
20021 	/*
20022 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
20023 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
20024 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
20025 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
20026 	 */
20027 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
20028 		freemsg(mp);
20029 		return (ENOMEM);
20030 	}
20031 
20032 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
20033 
20034 	/*
20035 	 * If we can quiesce the ill, then set the address.  If not, then
20036 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
20037 	 */
20038 	ill_down_ipifs(ill);
20039 	mutex_enter(&ill->ill_lock);
20040 	if (!ill_is_quiescent(ill)) {
20041 		/* call cannot fail since `conn_t *' argument is NULL */
20042 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
20043 		    mp, ILL_DOWN);
20044 		mutex_exit(&ill->ill_lock);
20045 		return (EINPROGRESS);
20046 	}
20047 	mutex_exit(&ill->ill_lock);
20048 
20049 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
20050 	return (0);
20051 }
20052 
20053 /*
20054  * Once the ill associated with `q' has quiesced, set its physical address
20055  * information to the values in `addrmp'.  Note that two copies of `addrmp'
20056  * are passed (linked by b_cont), since we sometimes need to save two distinct
20057  * copies in the ill_t, and our context doesn't permit sleeping or allocation
20058  * failure (we'll free the other copy if it's not needed).  Since the ill_t
20059  * is quiesced, we know any stale IREs with the old address information have
20060  * already been removed, so we don't need to call ill_fastpath_flush().
20061  */
20062 /* ARGSUSED */
20063 static void
20064 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
20065 {
20066 	ill_t		*ill = q->q_ptr;
20067 	mblk_t		*addrmp2 = unlinkb(addrmp);
20068 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
20069 	uint_t		addrlen, addroff;
20070 
20071 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20072 
20073 	addroff	= dlindp->dl_addr_offset;
20074 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
20075 
20076 	switch (dlindp->dl_data) {
20077 	case DL_IPV6_LINK_LAYER_ADDR:
20078 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
20079 		freemsg(addrmp2);
20080 		break;
20081 
20082 	case DL_CURR_PHYS_ADDR:
20083 		freemsg(ill->ill_phys_addr_mp);
20084 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
20085 		ill->ill_phys_addr_mp = addrmp;
20086 		ill->ill_phys_addr_length = addrlen;
20087 
20088 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
20089 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
20090 		else
20091 			freemsg(addrmp2);
20092 		break;
20093 	default:
20094 		ASSERT(0);
20095 	}
20096 
20097 	/*
20098 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20099 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20100 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20101 	 * brought up.
20102 	 */
20103 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
20104 		ipsq_current_finish(ipsq);
20105 }
20106 
20107 /*
20108  * Helper routine for setting the ill_nd_lla fields.
20109  */
20110 void
20111 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
20112 {
20113 	freemsg(ill->ill_nd_lla_mp);
20114 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
20115 	ill->ill_nd_lla_mp = ndmp;
20116 	ill->ill_nd_lla_len = addrlen;
20117 }
20118 
20119 major_t IP_MAJ;
20120 #define	IP	"ip"
20121 
20122 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20123 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20124 
20125 /*
20126  * Issue REMOVEIF ioctls to have the loopback interfaces
20127  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20128  * the former going away when the user-level processes in the zone
20129  * are killed  * and the latter are cleaned up by the stream head
20130  * str_stack_shutdown callback that undoes all I_PLINKs.
20131  */
20132 void
20133 ip_loopback_cleanup(ip_stack_t *ipst)
20134 {
20135 	int error;
20136 	ldi_handle_t	lh = NULL;
20137 	ldi_ident_t	li = NULL;
20138 	int		rval;
20139 	cred_t		*cr;
20140 	struct strioctl iocb;
20141 	struct lifreq	lifreq;
20142 
20143 	IP_MAJ = ddi_name_to_major(IP);
20144 
20145 #ifdef NS_DEBUG
20146 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20147 	    ipst->ips_netstack->netstack_stackid);
20148 #endif
20149 
20150 	bzero(&lifreq, sizeof (lifreq));
20151 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20152 
20153 	error = ldi_ident_from_major(IP_MAJ, &li);
20154 	if (error) {
20155 #ifdef DEBUG
20156 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20157 		    error);
20158 #endif
20159 		return;
20160 	}
20161 
20162 	cr = zone_get_kcred(netstackid_to_zoneid(
20163 	    ipst->ips_netstack->netstack_stackid));
20164 	ASSERT(cr != NULL);
20165 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20166 	if (error) {
20167 #ifdef DEBUG
20168 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20169 		    error);
20170 #endif
20171 		goto out;
20172 	}
20173 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20174 	iocb.ic_timout = 15;
20175 	iocb.ic_len = sizeof (lifreq);
20176 	iocb.ic_dp = (char *)&lifreq;
20177 
20178 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20179 	/* LINTED - statement has no consequent */
20180 	if (error) {
20181 #ifdef NS_DEBUG
20182 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20183 		    "UDP6 error %d\n", error);
20184 #endif
20185 	}
20186 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20187 	lh = NULL;
20188 
20189 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20190 	if (error) {
20191 #ifdef NS_DEBUG
20192 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20193 		    error);
20194 #endif
20195 		goto out;
20196 	}
20197 
20198 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20199 	iocb.ic_timout = 15;
20200 	iocb.ic_len = sizeof (lifreq);
20201 	iocb.ic_dp = (char *)&lifreq;
20202 
20203 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20204 	/* LINTED - statement has no consequent */
20205 	if (error) {
20206 #ifdef NS_DEBUG
20207 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20208 		    "UDP error %d\n", error);
20209 #endif
20210 	}
20211 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20212 	lh = NULL;
20213 
20214 out:
20215 	/* Close layered handles */
20216 	if (lh)
20217 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20218 	if (li)
20219 		ldi_ident_release(li);
20220 
20221 	crfree(cr);
20222 }
20223 
20224 /*
20225  * This needs to be in-sync with nic_event_t definition
20226  */
20227 static const char *
20228 ill_hook_event2str(nic_event_t event)
20229 {
20230 	switch (event) {
20231 	case NE_PLUMB:
20232 		return ("PLUMB");
20233 	case NE_UNPLUMB:
20234 		return ("UNPLUMB");
20235 	case NE_UP:
20236 		return ("UP");
20237 	case NE_DOWN:
20238 		return ("DOWN");
20239 	case NE_ADDRESS_CHANGE:
20240 		return ("ADDRESS_CHANGE");
20241 	case NE_LIF_UP:
20242 		return ("LIF_UP");
20243 	case NE_LIF_DOWN:
20244 		return ("LIF_DOWN");
20245 	default:
20246 		return ("UNKNOWN");
20247 	}
20248 }
20249 
20250 void
20251 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20252     nic_event_data_t data, size_t datalen)
20253 {
20254 	ip_stack_t		*ipst = ill->ill_ipst;
20255 	hook_nic_event_int_t	*info;
20256 	const char		*str = NULL;
20257 
20258 	/* create a new nic event info */
20259 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20260 		goto fail;
20261 
20262 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20263 	info->hnei_event.hne_lif = lif;
20264 	info->hnei_event.hne_event = event;
20265 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20266 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20267 	info->hnei_event.hne_data = NULL;
20268 	info->hnei_event.hne_datalen = 0;
20269 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20270 
20271 	if (data != NULL && datalen != 0) {
20272 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20273 		if (info->hnei_event.hne_data == NULL)
20274 			goto fail;
20275 		bcopy(data, info->hnei_event.hne_data, datalen);
20276 		info->hnei_event.hne_datalen = datalen;
20277 	}
20278 
20279 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20280 	    DDI_NOSLEEP) == DDI_SUCCESS)
20281 		return;
20282 
20283 fail:
20284 	if (info != NULL) {
20285 		if (info->hnei_event.hne_data != NULL) {
20286 			kmem_free(info->hnei_event.hne_data,
20287 			    info->hnei_event.hne_datalen);
20288 		}
20289 		kmem_free(info, sizeof (hook_nic_event_t));
20290 	}
20291 	str = ill_hook_event2str(event);
20292 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20293 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20294 }
20295 
20296 void
20297 ipif_up_notify(ipif_t *ipif)
20298 {
20299 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20300 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20301 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20302 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20303 	    NE_LIF_UP, NULL, 0);
20304 }
20305